Code

screen_*: eliminate redundant wrefresh() calls
[ncmpc.git] / src / screen_song.c
index a66f3e1b7217e2d5e873eaf3b0d6c762ec273a22..1463ec6cd59a8289256c42ff558adc18968c8fd0 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2010 The Music Player Daemon Project
+ * (c) 2004-2017 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
 
 enum {
        LABEL_LENGTH = MPD_TAG_COUNT,
+       LABEL_PATH,
        LABEL_BITRATE,
+       LABEL_FORMAT,
+       LABEL_POSITION,
 };
 
 static const char *const tag_labels[] = {
@@ -44,6 +47,7 @@ static const char *const tag_labels[] = {
        [MPD_TAG_TITLE] = N_("Title"),
        [MPD_TAG_ALBUM] = N_("Album"),
        [LABEL_LENGTH] = N_("Length"),
+       [LABEL_POSITION] = N_("Position"),
        [MPD_TAG_COMPOSER] = N_("Composer"),
        [MPD_TAG_NAME] = N_("Name"),
        [MPD_TAG_DISC] = N_("Disc"),
@@ -51,7 +55,9 @@ static const char *const tag_labels[] = {
        [MPD_TAG_DATE] = N_("Date"),
        [MPD_TAG_GENRE] = N_("Genre"),
        [MPD_TAG_COMMENT] = N_("Comment"),
+       [LABEL_PATH] = N_("Path"),
        [LABEL_BITRATE] = N_("Bitrate"),
+       [LABEL_FORMAT] = N_("Format"),
 };
 
 static unsigned max_tag_label_width;
@@ -106,21 +112,8 @@ screen_song_clear(void)
        }
 }
 
-static void
-screen_song_paint(void);
-
-/**
- * Repaint and update the screen.
- */
-static void
-screen_song_repaint(void)
-{
-       screen_song_paint();
-       wrefresh(lw->w);
-}
-
 static const char *
-screen_song_list_callback(unsigned idx, G_GNUC_UNUSED void *data)
+screen_song_list_callback(unsigned idx, gcc_unused void *data)
 {
        assert(idx < current.lines->len);
 
@@ -173,7 +166,7 @@ screen_song_resize(int cols, int rows)
 }
 
 static const char *
-screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
+screen_song_title(gcc_unused char *str, gcc_unused size_t size)
 {
        return _("Song viewer");
 }
@@ -189,12 +182,7 @@ screen_song_paint(void)
 static void
 screen_song_append(const char *label, const char *value, unsigned label_col)
 {
-       unsigned label_width = locale_width(label) + 2;
-       int value_col, label_size;
-       gchar *entry, *entry_iter;
-       const gchar *value_iter;
-       char *p, *q;
-       unsigned width;
+       const unsigned label_width = locale_width(label) + 2;
 
        assert(label != NULL);
        assert(value != NULL);
@@ -202,13 +190,13 @@ screen_song_append(const char *label, const char *value, unsigned label_col)
 
        /* +2 for ': ' */
        label_col += 2;
-       value_col = lw->cols - label_col;
+       const int value_col = lw->cols - label_col;
        /* calculate the number of required linebreaks */
-       value_iter = value;
-       label_size = strlen(label) + label_col;
+       const gchar *value_iter = value;
+       const int label_size = strlen(label) + label_col;
 
        while (*value_iter != 0) {
-               entry = g_malloc(label_size);
+               char *entry = g_malloc(label_size), *entry_iter;
                if (value_iter == value) {
                        entry_iter = entry + g_sprintf(entry, "%s: ", label);
                        /* fill the label column with whitespaces */
@@ -223,8 +211,8 @@ screen_song_append(const char *label, const char *value, unsigned label_col)
                /* skip whitespaces */
                while (g_ascii_isspace(*value_iter)) ++value_iter;
 
-               p = g_strdup(value_iter);
-               width = utf8_cut_width(p, value_col);
+               char *p = g_strdup(value_iter);
+               unsigned width = utf8_cut_width(p, value_col);
                if (width == 0)
                        /* not enough room for anything - bail out */
                        break;
@@ -233,7 +221,7 @@ screen_song_append(const char *label, const char *value, unsigned label_col)
 
                value_iter += strlen(p);
                p = replace_utf8_to_locale(p);
-               q = g_strconcat(entry, p, NULL);
+               char *q = g_strconcat(entry, p, NULL);
                g_free(entry);
                g_free(p);
 
@@ -256,10 +244,15 @@ screen_song_append_tag(const struct mpd_song *song, enum mpd_tag_type tag)
 }
 
 static void
-screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
+screen_song_add_song(const struct mpd_song *song)
 {
        assert(song != NULL);
 
+       char songpos[16];
+       g_snprintf(songpos, sizeof(songpos), "%d", mpd_song_get_pos(song) + 1);
+       screen_song_append(_(tag_labels[LABEL_POSITION]), songpos,
+                          max_tag_label_width);
+
        screen_song_append_tag(song, MPD_TAG_ARTIST);
        screen_song_append_tag(song, MPD_TAG_TITLE);
        screen_song_append_tag(song, MPD_TAG_ALBUM);
@@ -272,7 +265,6 @@ screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
 
                const char *value = length;
 
-#if LIBMPDCLIENT_CHECK_VERSION(2,3,0)
                char buffer[64];
 
                if (mpd_song_get_end(song) > 0) {
@@ -294,7 +286,6 @@ screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
                                 length, start);
                        value = buffer;
                }
-#endif
 
                screen_song_append(_(tag_labels[LABEL_LENGTH]), value,
                                   max_tag_label_width);
@@ -308,16 +299,8 @@ screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
        screen_song_append_tag(song, MPD_TAG_GENRE);
        screen_song_append_tag(song, MPD_TAG_COMMENT);
 
-       screen_song_append(_("Path"), mpd_song_get_uri(song),
+       screen_song_append(_(tag_labels[LABEL_PATH]), mpd_song_get_uri(song),
                           max_tag_label_width);
-       if (mpdclient_is_playing(c) && c->song != NULL &&
-           strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0) {
-               char buf[16];
-               g_snprintf(buf, sizeof(buf), _("%d kbps"),
-                          mpd_status_get_kbit_rate(c->status));
-               screen_song_append(_(tag_labels[LABEL_BITRATE]), buf,
-                                  max_tag_label_width);
-       }
 }
 
 static void
@@ -330,15 +313,13 @@ screen_song_append_stats(enum stats_label label, const char *value)
 static bool
 screen_song_add_stats(struct mpd_connection *connection)
 {
-       char buf[64];
-       GDate *date;
-       struct mpd_stats *mpd_stats;
-
-       mpd_stats = mpd_run_stats(connection);
+       struct mpd_stats *mpd_stats = mpd_run_stats(connection);
        if (mpd_stats == NULL)
                return false;
 
        g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
+
+       char buf[64];
        g_snprintf(buf, sizeof(buf), "%d",
                   mpd_stats_get_number_of_artists(mpd_stats));
        screen_song_append_stats(STATS_ARTISTS, buf);
@@ -361,7 +342,7 @@ screen_song_add_stats(struct mpd_connection *connection)
                             mpd_stats_get_uptime(mpd_stats));
        screen_song_append_stats(STATS_UPTIME, buf);
 
-       date = g_date_new();
+       GDate *date = g_date_new();
        g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
        g_date_strftime(buf, sizeof(buf), "%x", date);
        screen_song_append_stats(STATS_DBUPTIME, buf);
@@ -372,10 +353,43 @@ screen_song_add_stats(struct mpd_connection *connection)
 }
 
 static void
-screen_song_update(struct mpdclient *c)
+audio_format_to_string(char *buffer, size_t size,
+                      const struct mpd_audio_format *format)
 {
-       struct mpd_connection *connection;
+#if LIBMPDCLIENT_CHECK_VERSION(2,10,0)
+       if (format->bits == MPD_SAMPLE_FORMAT_FLOAT) {
+               g_snprintf(buffer, size, _("%u:f:%u"),
+                          format->sample_rate,
+                          format->channels);
+               return;
+       }
+
+       if (format->bits == MPD_SAMPLE_FORMAT_DSD) {
+               if (format->sample_rate > 0 &&
+                   format->sample_rate % 44100 == 0) {
+                       /* use shortcuts such as "dsd64" which implies the
+                          sample rate */
+                       g_snprintf(buffer, size, _("dsd%u:%u"),
+                                  format->sample_rate * 8 / 44100,
+                                  format->channels);
+                       return;
+               }
+
+               g_snprintf(buffer, size, _("%u:dsd:%u"),
+                          format->sample_rate,
+                          format->channels);
+               return;
+       }
+#endif
+
+       g_snprintf(buffer, size, _("%u:%u:%u"),
+                  format->sample_rate, format->bits,
+                  format->channels);
+}
 
+static void
+screen_song_update(struct mpdclient *c)
+{
        /* Clear all lines */
        for (guint i = 0; i < current.lines->len; ++i)
                g_free(g_ptr_array_index(current.lines, i));
@@ -394,7 +408,7 @@ screen_song_update(struct mpdclient *c)
                                mpd_song_get_uri(c->song)) != 0 ||
                         !mpdclient_is_playing(c))) {
                g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
-               screen_song_add_song(current.selected_song, c);
+               screen_song_add_song(current.selected_song);
                g_ptr_array_add(current.lines, g_strdup("\0"));
        }
 
@@ -404,24 +418,42 @@ screen_song_update(struct mpdclient *c)
                }
                current.played_song = mpd_song_dup(c->song);
                g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
-               screen_song_add_song(current.played_song, c);
+               screen_song_add_song(current.played_song);
+
+               if (mpd_status_get_kbit_rate(c->status) > 0) {
+                       char buf[16];
+                       g_snprintf(buf, sizeof(buf), _("%d kbps"),
+                                  mpd_status_get_kbit_rate(c->status));
+                       screen_song_append(_(tag_labels[LABEL_BITRATE]), buf,
+                                          max_tag_label_width);
+               }
+
+               const struct mpd_audio_format *format =
+                       mpd_status_get_audio_format(c->status);
+               if (format) {
+                       char buf[32];
+                       audio_format_to_string(buf, sizeof(buf), format);
+                       screen_song_append(_(tag_labels[LABEL_FORMAT]), buf,
+                                          max_tag_label_width);
+               }
+
                g_ptr_array_add(current.lines, g_strdup("\0"));
        }
 
        /* Add some statistics about mpd */
-       connection = mpdclient_get_connection(c);
+       struct mpd_connection *connection = mpdclient_get_connection(c);
        if (connection != NULL && !screen_song_add_stats(connection))
                mpdclient_handle_error(c);
 
        list_window_set_length(lw, current.lines->len);
-       screen_song_repaint();
+       screen_song_paint();
 }
 
 static bool
 screen_song_cmd(struct mpdclient *c, command_t cmd)
 {
        if (list_window_scroll_cmd(lw, cmd)) {
-               screen_song_repaint();
+               screen_song_paint();
                return true;
        }
 
@@ -467,7 +499,7 @@ screen_song_cmd(struct mpdclient *c, command_t cmd)
        if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
                /* center the row */
                list_window_center(lw, lw->selected);
-               screen_song_repaint();
+               screen_song_paint();
                return true;
        }