Code

Merge branch 'master' of git://git.musicpd.org/avuton/ncmpc
[ncmpc.git] / src / screen_song.c
index 29c0f2df7a140d554241df6ddcb1b88f9cbb5b4b..2c451c1599c38d2043fafa9ee97d3289c8daf24c 100644 (file)
@@ -1,7 +1,7 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2010 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
+
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include "screen_song.h"
+#include "screen_interface.h"
+#include "screen_file.h"
+#include "screen_lyrics.h"
+#include "screen_find.h"
 #include "i18n.h"
 #include "screen.h"
-#include "screen_utils.h"
 #include "charset.h"
 #include "utils.h"
+#include "mpdclient.h"
+
+#include <mpd/client.h>
 
 #include <glib/gprintf.h>
+#include <assert.h>
 #include <string.h>
 
-static list_window_t *lw;
+enum {
+       LABEL_LENGTH = MPD_TAG_COUNT,
+       LABEL_BITRATE,
+};
+
+static const char *const tag_labels[] = {
+       [MPD_TAG_ARTIST] = N_("Artist"),
+       [MPD_TAG_TITLE] = N_("Title"),
+       [MPD_TAG_ALBUM] = N_("Album"),
+       [LABEL_LENGTH] = N_("Length"),
+       [MPD_TAG_COMPOSER] = N_("Composer"),
+       [MPD_TAG_NAME] = N_("Name"),
+       [MPD_TAG_DISC] = N_("Disc"),
+       [MPD_TAG_TRACK] = N_("Track"),
+       [MPD_TAG_DATE] = N_("Date"),
+       [MPD_TAG_GENRE] = N_("Genre"),
+       [MPD_TAG_COMMENT] = N_("Comment"),
+       [LABEL_BITRATE] = N_("Bitrate"),
+};
+
+static unsigned max_tag_label_width;
+
+enum stats_label {
+       STATS_ARTISTS,
+       STATS_ALBUMS,
+       STATS_SONGS,
+       STATS_UPTIME,
+       STATS_DBUPTIME,
+       STATS_PLAYTIME,
+       STATS_DBPLAYTIME,
+};
+
+static const char *const stats_labels[] = {
+       [STATS_ARTISTS] = N_("Number of artists"),
+       [STATS_ALBUMS] = N_("Number of albums"),
+       [STATS_SONGS] = N_("Number of songs"),
+       [STATS_UPTIME] = N_("Uptime"),
+       [STATS_DBUPTIME] = N_("Most recent db update"),
+       [STATS_PLAYTIME] = N_("Playtime"),
+       [STATS_DBPLAYTIME] = N_("DB playtime"),
+};
+
+static unsigned max_stats_label_width;
+
+static struct list_window *lw;
 
-static const struct mpd_song *next_song;
+static struct mpd_song *next_song;
 
 static struct {
        struct mpd_song *selected_song;
@@ -45,11 +97,11 @@ screen_song_clear(void)
        g_ptr_array_set_size(current.lines, 0);
 
        if (current.selected_song != NULL) {
-               mpd_freeSong(current.selected_song);
+               mpd_song_free(current.selected_song);
                current.selected_song = NULL;
        }
        if (current.played_song != NULL) {
-               mpd_freeSong(current.played_song);
+               mpd_song_free(current.played_song);
                current.played_song = NULL;
        }
 }
@@ -68,26 +120,35 @@ screen_song_repaint(void)
 }
 
 static const char *
-screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
-                         G_GNUC_UNUSED void *data)
+screen_song_list_callback(unsigned idx, G_GNUC_UNUSED void *data)
 {
-       static char buffer[256];
-       char *value;
+       assert(idx < current.lines->len);
 
-       if (idx >= current.lines->len)
-               return NULL;
-
-       value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
-       g_strlcpy(buffer, value, sizeof(buffer));
-       g_free(value);
-
-       return buffer;
+       return g_ptr_array_index(current.lines, idx);
 }
 
 
 static void
 screen_song_init(WINDOW *w, int cols, int rows)
 {
+       for (unsigned i = 0; i < G_N_ELEMENTS(tag_labels); ++i) {
+               if (tag_labels[i] != NULL) {
+                       unsigned width = utf8_width(_(tag_labels[i]));
+
+                       if (width > max_tag_label_width)
+                               max_tag_label_width = width;
+               }
+       }
+
+       for (unsigned i = 0; i < G_N_ELEMENTS(stats_labels); ++i) {
+               if (stats_labels[i] != NULL) {
+                       unsigned width = utf8_width(_(stats_labels[i]));
+
+                       if (width > max_stats_label_width)
+                               max_stats_label_width = width;
+               }
+       }
+
        /* We will need at least 10 lines, so this saves 10 reallocations :) */
        current.lines = g_ptr_array_sized_new(10);
        lw = list_window_init(w, cols, rows);
@@ -108,8 +169,7 @@ screen_song_exit(void)
 static void
 screen_song_resize(int cols, int rows)
 {
-       lw->cols = cols;
-       lw->rows = rows;
+       list_window_resize(lw, cols, rows);
 }
 
 static const char *
@@ -129,167 +189,166 @@ screen_song_paint(void)
 static void
 screen_song_append(const char *label, const char *value, unsigned label_col)
 {
-       int value_col, linebreaks, entry_size, label_size;
-       int i, k;
+       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;
 
        assert(label != NULL);
-       assert(g_utf8_validate(label, -1, NULL));
-
-       if (value != NULL) {
-               assert(g_utf8_validate(value, -1, NULL));
-               /* +2 for ': ' */
-               label_col += 2;
-               value_col = lw->cols - label_col;
-               /* calculate the number of required linebreaks */
-               linebreaks = (utf8_width(value) - 1) / value_col + 1;
-               value_iter = value;
-               label_size = strlen(label) + label_col - utf8_width(label);
-               entry_size = label_size + strlen(value) + 2;
-
-               for (i = 0; i < linebreaks; ++i)
-               {
-                       entry = g_malloc(entry_size);
-                       if (i == 0) {
-                               entry_iter = entry + g_sprintf(entry, "%s: ", label);
-                               /* fill the label column with whitespaces */
-                               for ( ; entry_iter < entry + label_size; ++entry_iter)
-                                       *entry_iter = ' ';
-                       }
-                       else {
-                               entry_iter = entry;
-                               /* fill the label column with whitespaces */
-                               for ( ; entry_iter < entry + label_col; ++entry_iter)
-                                       *entry_iter = ' ';
-                       }
-                       /* skip whitespaces */
-                       while (g_ascii_isspace(*value_iter)) ++value_iter;
-                       k = 0;
-                       while (value_iter && k < value_col)
-                       {
-                               g_utf8_strncpy(entry_iter, value_iter, 1);
-                               value_iter = g_utf8_find_next_char(value_iter, NULL);
-                               entry_iter = g_utf8_find_next_char(entry_iter, NULL);
-                               ++k;
-                       }
-                       *entry_iter = '\0';
-                       g_ptr_array_add(current.lines, entry);
+       assert(value != NULL);
+       assert(g_utf8_validate(value, -1, NULL));
+
+       /* +2 for ': ' */
+       label_col += 2;
+       value_col = lw->cols - label_col;
+       /* calculate the number of required linebreaks */
+       value_iter = value;
+       label_size = strlen(label) + label_col;
+
+       while (*value_iter != 0) {
+               entry = g_malloc(label_size);
+               if (value_iter == value) {
+                       entry_iter = entry + g_sprintf(entry, "%s: ", label);
+                       /* fill the label column with whitespaces */
+                       memset(entry_iter, ' ', label_col - label_width);
+                       entry_iter += label_col - label_width;
+               }
+               else {
+                       /* fill the label column with whitespaces */
+                       memset(entry, ' ', label_col);
+                       entry_iter = entry + label_col;
                }
+               /* skip whitespaces */
+               while (g_ascii_isspace(*value_iter)) ++value_iter;
+
+               p = g_strdup(value_iter);
+               width = utf8_cut_width(p, value_col);
+               if (width == 0)
+                       /* not enough room for anything - bail out */
+                       break;
+
+               *entry_iter = 0;
+
+               value_iter += strlen(p);
+               p = replace_utf8_to_locale(p);
+               q = g_strconcat(entry, p, NULL);
+               g_free(entry);
+               g_free(p);
+
+               g_ptr_array_add(current.lines, q);
        }
 }
 
 static void
-screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c)
+screen_song_append_tag(const struct mpd_song *song, enum mpd_tag_type tag)
 {
-       unsigned i, max_label_width;
-       enum label {
-               ARTIST, TITLE, ALBUM, COMPOSER, NAME, DISC, TRACK,
-               DATE, GENRE, COMMENT, PATH, BITRATE
-       };
-       const char *labels[] = { [ARTIST] = _("Artist"),
-               [TITLE] = _("Title"),
-               [ALBUM] = _("Album"),
-               [COMPOSER] = _("Composer"),
-               [NAME] = _("Name"),
-               [DISC] = _("Disc"),
-               [TRACK] = _("Track"),
-               [DATE] = _("Date"),
-               [GENRE] = _("Genre"),
-               [COMMENT] = _("Comment"),
-               [PATH] = _("Path"),
-               [BITRATE] = _("Bitrate"),
-       };
-       /* Determine the width of the longest label */
-       max_label_width = utf8_width(labels[0]);
-       for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
-               if (utf8_width(labels[i]) > max_label_width)
-                       max_label_width = utf8_width(labels[i]);
-       }
+       const char *label = _(tag_labels[tag]);
+       unsigned i = 0;
+       const char *value;
 
+       assert((unsigned)tag < G_N_ELEMENTS(tag_labels));
+       assert(label != NULL);
+
+       while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
+               screen_song_append(label, value, max_tag_label_width);
+}
+
+static void
+screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
+{
        assert(song != NULL);
 
-       screen_song_append(labels[ARTIST], song->artist, max_label_width);
-       screen_song_append(labels[TITLE], song->title, max_label_width);
-       screen_song_append(labels[ALBUM], song->album, max_label_width);
-       screen_song_append(labels[COMPOSER], song->composer, max_label_width);
-       screen_song_append(labels[NAME], song->name, max_label_width);
-       screen_song_append(labels[DISC], song->disc, max_label_width);
-       screen_song_append(labels[TRACK], song->track, max_label_width);
-       screen_song_append(labels[DATE], song->date, max_label_width);
-       screen_song_append(labels[GENRE], song->genre, max_label_width);
-       screen_song_append(labels[COMMENT], song->comment, max_label_width);
-       screen_song_append(labels[PATH], song->file, max_label_width);
-       if (c->status != NULL && c->song != NULL &&
-                        g_strcmp0(c->song->file, song->file) == 0 &&
-                       (c->status->state == MPD_STATUS_STATE_PLAY ||
-                        c->status->state == MPD_STATUS_STATE_PAUSE) ) {
+       screen_song_append_tag(song, MPD_TAG_ARTIST);
+       screen_song_append_tag(song, MPD_TAG_TITLE);
+       screen_song_append_tag(song, MPD_TAG_ALBUM);
+
+       /* create time string and add it */
+       if (mpd_song_get_duration(song) > 0) {
+               char length[16];
+               format_duration_short(length, sizeof(length),
+                                     mpd_song_get_duration(song));
+               screen_song_append(_(tag_labels[LABEL_LENGTH]), length,
+                                  max_tag_label_width);
+       }
+
+       screen_song_append_tag(song, MPD_TAG_COMPOSER);
+       screen_song_append_tag(song, MPD_TAG_NAME);
+       screen_song_append_tag(song, MPD_TAG_DISC);
+       screen_song_append_tag(song, MPD_TAG_TRACK);
+       screen_song_append_tag(song, MPD_TAG_DATE);
+       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),
+                          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"), c->status->bitRate);
-               screen_song_append(labels[BITRATE], buf, max_label_width);
+               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
-screen_song_add_stats(const mpdclient_t *c)
+screen_song_append_stats(enum stats_label label, const char *value)
+{
+       screen_song_append(_(stats_labels[label]), value,
+                          max_stats_label_width);
+}
+
+static bool
+screen_song_add_stats(struct mpd_connection *connection)
 {
-       unsigned i, max_label_width;
        char buf[64];
-       char *duration;
        GDate *date;
-       enum label {
-               ARTISTS, ALBUMS, SONGS, UPTIME,
-               DBUPTIME, PLAYTIME, DBPLAYTIME
-       };
-       const char *labels[] = { [ARTISTS] = _("Number of artists"),
-               [ALBUMS] = _("Number of albums"),
-               [SONGS] = _("Number of songs"),
-               [UPTIME] = _("Uptime"),
-               [DBUPTIME] =_("Most recent db update"),
-               [PLAYTIME] = _("Playtime"),
-               [DBPLAYTIME] = _("DB playtime")
-       };
-       mpd_Stats *mpd_stats = NULL;
-       if (c->connection != NULL) {
-               mpd_sendStatsCommand(c->connection);
-               mpd_stats = mpd_getStats(c->connection);
-       }
+       struct mpd_stats *mpd_stats;
 
-       if (mpd_stats != NULL) {
-               /* Determine the width of the longest label */
-               max_label_width = utf8_width(labels[0]);
-               for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
-                       if (utf8_width(labels[i]) > max_label_width)
-                               max_label_width = utf8_width(labels[i]);
-               }
+       mpd_stats = mpd_run_stats(connection);
+       if (mpd_stats == NULL)
+               return false;
 
-               g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
-               g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfArtists);
-               screen_song_append(labels[ARTISTS], buf, max_label_width);
-               g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfAlbums);
-               screen_song_append(labels[ALBUMS], buf, max_label_width);
-               g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfSongs);
-               screen_song_append(labels[SONGS], buf, max_label_width);
-               duration = time_seconds_to_durationstr(mpd_stats->dbPlayTime);
-               screen_song_append(labels[DBPLAYTIME], duration, max_label_width);
-               g_free(duration);
-               duration = time_seconds_to_durationstr(mpd_stats->playTime);
-               screen_song_append(labels[PLAYTIME], duration, max_label_width);
-               g_free(duration);
-               duration = time_seconds_to_durationstr(mpd_stats->uptime);
-               screen_song_append(labels[UPTIME], duration, max_label_width);
-               g_free(duration);
-               date = g_date_new();
-               g_date_set_time_t(date, mpd_stats->dbUpdateTime);
-               g_date_strftime(buf, sizeof(buf), "%x", date);
-               screen_song_append(labels[DBUPTIME], buf, max_label_width);
-               g_date_free(date);
-       }
+       g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
+       g_snprintf(buf, sizeof(buf), "%d",
+                  mpd_stats_get_number_of_artists(mpd_stats));
+       screen_song_append_stats(STATS_ARTISTS, buf);
+       g_snprintf(buf, sizeof(buf), "%d",
+                  mpd_stats_get_number_of_albums(mpd_stats));
+       screen_song_append_stats(STATS_ALBUMS, buf);
+       g_snprintf(buf, sizeof(buf), "%d",
+                  mpd_stats_get_number_of_songs(mpd_stats));
+       screen_song_append_stats(STATS_SONGS, buf);
+
+       format_duration_long(buf, sizeof(buf),
+                            mpd_stats_get_db_play_time(mpd_stats));
+       screen_song_append_stats(STATS_DBPLAYTIME, buf);
+
+       format_duration_long(buf, sizeof(buf),
+                            mpd_stats_get_play_time(mpd_stats));
+       screen_song_append_stats(STATS_PLAYTIME, buf);
+
+       format_duration_long(buf, sizeof(buf),
+                            mpd_stats_get_uptime(mpd_stats));
+       screen_song_append_stats(STATS_UPTIME, buf);
+
+       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);
+       g_date_free(date);
+
+       mpd_stats_free(mpd_stats);
+       return true;
 }
 
 static void
-screen_song_update(mpdclient_t *c)
+screen_song_update(struct mpdclient *c)
 {
+       struct mpd_connection *connection;
+
        /* Clear all lines */
        for (guint i = 0; i < current.lines->len; ++i)
                g_free(g_ptr_array_index(current.lines, i));
@@ -298,44 +357,43 @@ screen_song_update(mpdclient_t *c)
        /* If a song was selected before the song screen was opened */
        if (next_song != NULL) {
                assert(current.selected_song == NULL);
-               current.selected_song = mpd_songDup(next_song);
+               current.selected_song = next_song;
                next_song = NULL;
        }
 
        if (current.selected_song != NULL &&
                        (c->song == NULL ||
-                        g_strcmp0(current.selected_song->file, c->song->file) != 0 ||
-                        c->status == NULL ||
-                       (c->status->state != MPD_STATUS_STATE_PLAY &&
-                        c->status->state != MPD_STATUS_STATE_PAUSE)) ) {
+                        strcmp(mpd_song_get_uri(current.selected_song),
+                               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);
                g_ptr_array_add(current.lines, g_strdup("\0"));
        }
 
-       if (c->song != NULL && c->status != NULL &&
-                       (c->status->state == MPD_STATUS_STATE_PLAY ||
-                        c->status->state == MPD_STATUS_STATE_PAUSE) ) {
+       if (c->song != NULL && mpdclient_is_playing(c)) {
                if (current.played_song != NULL) {
-                       mpd_freeSong(current.played_song);
+                       mpd_song_free(current.played_song);
                }
-               current.played_song = mpd_songDup(c->song);
+               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);
                g_ptr_array_add(current.lines, g_strdup("\0"));
        }
 
        /* Add some statistics about mpd */
-       if (c->connection != NULL)
-               screen_song_add_stats(c);
+       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();
 }
 
 static bool
-screen_song_cmd(mpdclient_t *c, command_t cmd)
+screen_song_cmd(struct mpdclient *c, command_t cmd)
 {
-       if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
+       if (list_window_scroll_cmd(lw, cmd)) {
                screen_song_repaint();
                return true;
        }
@@ -356,11 +414,11 @@ screen_song_cmd(mpdclient_t *c, command_t cmd)
 #ifdef ENABLE_LYRICS_SCREEN
        case CMD_SCREEN_LYRICS:
                if (current.selected_song != NULL) {
-                       screen_lyrics_switch(c, current.selected_song);
+                       screen_lyrics_switch(c, current.selected_song, false);
                        return true;
                }
                if (current.played_song != NULL) {
-                       screen_lyrics_switch(c, current.played_song);
+                       screen_lyrics_switch(c, current.played_song, true);
                        return true;
                }
                return false;
@@ -379,10 +437,9 @@ screen_song_cmd(mpdclient_t *c, command_t cmd)
                break;
        }
 
-       if (screen_find(lw, current.lines->len,
-                       cmd, screen_song_list_callback, NULL)) {
+       if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
                /* center the row */
-               list_window_center(lw, current.lines->len, lw->selected);
+               list_window_center(lw, lw->selected);
                screen_song_repaint();
                return true;
        }
@@ -403,12 +460,12 @@ const struct screen_functions screen_song = {
 };
 
 void
-screen_song_switch(mpdclient_t *c, const struct mpd_song *song)
+screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
 {
        assert(song != NULL);
        assert(current.selected_song == NULL);
        assert(current.played_song == NULL);
 
-       next_song = song;
+       next_song = mpd_song_dup(song);
        screen_switch(&screen_song, c);
 }