Code

moved struct screen_functions to screen_interface.h
[ncmpc.git] / src / screen_song.c
index 3af2d8bca0dff66ad307f80d707544f65a13a508..7b6ec6fbd5f8ca90ccd9e9b896675398fb04633b 100644 (file)
@@ -1,7 +1,7 @@
 /* ncmpc (Ncurses MPD Client)
  * (c) 2004-2009 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_interface.h"
+#include "screen_file.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;
 
-static const struct mpd_song *next_song;
+static struct mpd_song *next_song;
 
 static struct {
        struct mpd_song *selected_song;
@@ -45,11 +51,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;
        }
 }
@@ -69,7 +75,7 @@ screen_song_repaint(void)
 
 static const char *
 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
-                         G_GNUC_UNUSED void *data)
+                         G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
 {
        static char buffer[256];
        char *value;
@@ -180,16 +186,28 @@ screen_song_append(const char *label, const char *value, unsigned label_col)
 }
 
 static void
-screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c)
+screen_song_append_tag(const char *label, const struct mpd_song *song,
+                      enum mpd_tag_type tag, unsigned label_col)
+{
+       unsigned i = 0;
+       const char *value;
+
+       while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
+               screen_song_append(label, value, label_col);
+}
+
+static void
+screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
 {
        unsigned i, max_label_width;
        enum label {
-               ARTIST, TITLE, ALBUM, COMPOSER, NAME, DISC, TRACK,
-               DATE, GENRE, COMMENT, PATH, BITRATE
+               ARTIST, TITLE, ALBUM, LENGTH, COMPOSER, NAME, DISC, TRACK,
+               DATE, GENRE, COMMENT, BITRATE
        };
        const char *labels[] = { [ARTIST] = _("Artist"),
                [TITLE] = _("Title"),
                [ALBUM] = _("Album"),
+               [LENGTH] = _("Length"),
                [COMPOSER] = _("Composer"),
                [NAME] = _("Name"),
                [DISC] = _("Disc"),
@@ -197,7 +215,6 @@ screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c)
                [DATE] = _("Date"),
                [GENRE] = _("Genre"),
                [COMMENT] = _("Comment"),
-               [PATH] = _("Path"),
                [BITRATE] = _("Bitrate"),
        };
        /* Determine the width of the longest label */
@@ -209,26 +226,56 @@ screen_song_add_song(const struct mpd_song *song, const mpdclient_t *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) {
+       screen_song_append_tag(labels[ARTIST], song, MPD_TAG_ARTIST,
+                              max_label_width);
+       screen_song_append_tag(labels[TITLE], song, MPD_TAG_TITLE,
+                              max_label_width);
+       screen_song_append_tag(labels[ALBUM], song, MPD_TAG_ALBUM,
+                              max_label_width);
+       /* create time string and add it */
+       if (mpd_song_get_duration(song) > 0) {
+               unsigned t = mpd_song_get_duration(song);
+               char length[16];
+
+               /*write out the time, using hours if time over 60 minutes*/
+               if (t > 3600) {
+                       g_snprintf(length, sizeof(length),
+                                       "%i:%02i:%02i",
+                                       t/3600, (t%3600)/60, t%60);
+               } else {
+                       g_snprintf(length, sizeof(length),
+                                       "%i:%02i", t/60, t%60);
+               }
+               screen_song_append(labels[LENGTH], length, max_label_width);
+       }
+       screen_song_append_tag(labels[COMPOSER], song, MPD_TAG_COMPOSER,
+                              max_label_width);
+       screen_song_append_tag(labels[NAME], song, MPD_TAG_NAME,
+                              max_label_width);
+       screen_song_append_tag(labels[DISC], song, MPD_TAG_DISC,
+                              max_label_width);
+       screen_song_append_tag(labels[TRACK], song, MPD_TAG_TRACK,
+                              max_label_width);
+       screen_song_append_tag(labels[DATE], song, MPD_TAG_DATE,
+                              max_label_width);
+       screen_song_append_tag(labels[GENRE], song, MPD_TAG_GENRE,
+                              max_label_width);
+       screen_song_append_tag(labels[COMMENT], song, MPD_TAG_COMMENT,
+                              max_label_width);
+       screen_song_append(_("Path"), mpd_song_get_uri(song), max_label_width);
+       if (c->status != NULL && c->song != NULL &&
+           strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
+           (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
+            mpd_status_get_state(c->status) == MPD_STATE_PAUSE)) {
                char buf[16];
-               g_snprintf(buf, sizeof(buf), _("%d kbps"), c->status->bitRate);
+               g_snprintf(buf, sizeof(buf), _("%d kbps"),
+                          mpd_status_get_kbit_rate(c->status));
                screen_song_append(labels[BITRATE], buf, max_label_width);
        }
 }
 
 static void
-screen_song_add_stats(const mpdclient_t *c)
+screen_song_add_stats(const struct mpdclient *c)
 {
        unsigned i, max_label_width;
        char buf[64];
@@ -242,14 +289,14 @@ screen_song_add_stats(const mpdclient_t *c)
                [ALBUMS] = _("Number of albums"),
                [SONGS] = _("Number of songs"),
                [UPTIME] = _("Uptime"),
-               [DBUPTIME] =_("DB last updated"),
+               [DBUPTIME] =_("Most recent db update"),
                [PLAYTIME] = _("Playtime"),
                [DBPLAYTIME] = _("DB playtime")
        };
-       mpd_Stats *mpd_stats = NULL;
+       struct mpd_stats *mpd_stats = NULL;
+
        if (c->connection != NULL) {
-               mpd_sendStatsCommand(c->connection);
-               mpd_stats = mpd_getStats(c->connection);
+               mpd_stats = mpd_run_stats(c->connection);
        }
 
        if (mpd_stats != NULL) {
@@ -260,65 +307,69 @@ screen_song_add_stats(const mpdclient_t *c)
                                max_label_width = utf8_width(labels[i]);
                }
 
-               g_ptr_array_add(current.lines, g_strdup(_("MPD Statistics")) );
-               g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfArtists);
+               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(labels[ARTISTS], buf, max_label_width);
-               g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfAlbums);
+               g_snprintf(buf, sizeof(buf), "%d",
+                          mpd_stats_get_number_of_albums(mpd_stats));
                screen_song_append(labels[ALBUMS], buf, max_label_width);
-               g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfSongs);
+               g_snprintf(buf, sizeof(buf), "%d",
+                          mpd_stats_get_number_of_songs(mpd_stats));
                screen_song_append(labels[SONGS], buf, max_label_width);
-               duration = time_seconds_to_durationstr(mpd_stats->dbPlayTime);
+               duration = time_seconds_to_durationstr(mpd_stats_get_db_play_time(mpd_stats));
                screen_song_append(labels[DBPLAYTIME], duration, max_label_width);
                g_free(duration);
-               duration = time_seconds_to_durationstr(mpd_stats->playTime);
+               duration = time_seconds_to_durationstr(mpd_stats_get_play_time(mpd_stats));
                screen_song_append(labels[PLAYTIME], duration, max_label_width);
                g_free(duration);
-               duration = time_seconds_to_durationstr(mpd_stats->uptime);
+               duration = time_seconds_to_durationstr(mpd_stats_get_uptime(mpd_stats));
                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_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
                g_date_strftime(buf, sizeof(buf), "%x", date);
                screen_song_append(labels[DBUPTIME], buf, max_label_width);
                g_date_free(date);
+
+               mpd_stats_free(mpd_stats);
        }
 }
 
 static void
-screen_song_update(mpdclient_t *c)
+screen_song_update(struct mpdclient *c)
 {
-       /* if any song changed */
-/*     if ((c->song != NULL &&
-                               (current.played_song == NULL ||
-                                g_strcmp0(c->song->file, current.played_song->file) != 0) ) ||
-                       next_song != NULL)
-       {*/
+       /* Clear all lines */
        for (guint i = 0; i < current.lines->len; ++i)
                g_free(g_ptr_array_index(current.lines, i));
        g_ptr_array_set_size(current.lines, 0);
 
-       /* if a song was selected before the song screen was opened */
+       /* 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->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 ||
+                        c->status == NULL ||
+                        (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
+                         mpd_status_get_state(c->status) != MPD_STATE_PAUSE))) {
                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->state == MPD_STATUS_STATE_PLAY || c->status->state == MPD_STATUS_STATE_PAUSE) ) {
+       if (c->song != NULL && c->status != NULL &&
+           (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
+            mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) {
                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"));
@@ -329,11 +380,10 @@ screen_song_update(mpdclient_t *c)
                screen_song_add_stats(c);
 
        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)) {
                screen_song_repaint();
@@ -356,17 +406,25 @@ 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;
+
 #endif
 
+       case CMD_SCREEN_SWAP:
+               if (current.selected_song != NULL)
+                       screen_swap(c, current.selected_song);
+               else
+               // No need to check if this is null - we'd pass null anyway
+                       screen_swap(c, current.played_song);
+               return true;
+
        default:
                break;
        }
@@ -395,12 +453,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);
 }