Code

750beeba2c00e55cde552c2aad3043737a0cab49
[ncmpc.git] / src / screen_song.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "screen_song.h"
21 #include "screen_interface.h"
22 #include "screen_file.h"
23 #include "screen_lyrics.h"
24 #include "screen_find.h"
25 #include "i18n.h"
26 #include "screen.h"
27 #include "charset.h"
28 #include "utils.h"
29 #include "mpdclient.h"
31 #include <mpd/client.h>
33 #include <glib/gprintf.h>
34 #include <assert.h>
35 #include <string.h>
37 static struct list_window *lw;
39 static struct mpd_song *next_song;
41 static struct {
42         struct mpd_song *selected_song;
43         struct mpd_song *played_song;
44         GPtrArray *lines;
45 } current;
47 static void
48 screen_song_clear(void)
49 {
50         for (guint i = 0; i < current.lines->len; ++i)
51                 g_free(g_ptr_array_index(current.lines, i));
53         g_ptr_array_set_size(current.lines, 0);
55         if (current.selected_song != NULL) {
56                 mpd_song_free(current.selected_song);
57                 current.selected_song = NULL;
58         }
59         if (current.played_song != NULL) {
60                 mpd_song_free(current.played_song);
61                 current.played_song = NULL;
62         }
63 }
65 static void
66 screen_song_paint(void);
68 /**
69  * Repaint and update the screen.
70  */
71 static void
72 screen_song_repaint(void)
73 {
74         screen_song_paint();
75         wrefresh(lw->w);
76 }
78 static const char *
79 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED void *data)
80 {
81         static char buffer[256];
82         char *value;
84         assert(idx < current.lines->len);
86         value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
87         g_strlcpy(buffer, value, sizeof(buffer));
88         g_free(value);
90         return buffer;
91 }
94 static void
95 screen_song_init(WINDOW *w, int cols, int rows)
96 {
97         /* We will need at least 10 lines, so this saves 10 reallocations :) */
98         current.lines = g_ptr_array_sized_new(10);
99         lw = list_window_init(w, cols, rows);
100         lw->hide_cursor = true;
103 static void
104 screen_song_exit(void)
106         list_window_free(lw);
108         screen_song_clear();
110         g_ptr_array_free(current.lines, TRUE);
111         current.lines = NULL;
114 static void
115 screen_song_resize(int cols, int rows)
117         list_window_resize(lw, cols, rows);
120 static const char *
121 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
123         return _("Song viewer");
126 static void
127 screen_song_paint(void)
129         list_window_paint(lw, screen_song_list_callback, NULL);
132 /* Appends a line with a fixed width for the label column
133  * Handles NULL strings gracefully */
134 static void
135 screen_song_append(const char *label, const char *value, unsigned label_col)
137         int value_col, linebreaks, entry_size, label_size;
138         int i, k;
139         gchar *entry, *entry_iter;
140         const gchar *value_iter;
142         assert(label != NULL);
143         assert(g_utf8_validate(label, -1, NULL));
144         assert(value != NULL);
145         assert(g_utf8_validate(value, -1, NULL));
147         /* +2 for ': ' */
148         label_col += 2;
149         value_col = lw->cols - label_col;
150         /* calculate the number of required linebreaks */
151         linebreaks = (utf8_width(value) - 1) / value_col + 1;
152         value_iter = value;
153         label_size = strlen(label) + label_col - utf8_width(label);
154         entry_size = label_size + strlen(value) + 2;
156         for (i = 0; i < linebreaks; ++i) {
157                 entry = g_malloc(entry_size);
158                 if (i == 0) {
159                         entry_iter = entry + g_sprintf(entry, "%s: ", label);
160                         /* fill the label column with whitespaces */
161                         for ( ; entry_iter < entry + label_size; ++entry_iter)
162                                 *entry_iter = ' ';
163                 }
164                 else {
165                         entry_iter = entry;
166                         /* fill the label column with whitespaces */
167                         for ( ; entry_iter < entry + label_col; ++entry_iter)
168                                 *entry_iter = ' ';
169                 }
170                 /* skip whitespaces */
171                 while (g_ascii_isspace(*value_iter)) ++value_iter;
172                 k = 0;
173                 while (value_iter && k < value_col) {
174                         g_utf8_strncpy(entry_iter, value_iter, 1);
175                         value_iter = g_utf8_find_next_char(value_iter, NULL);
176                         entry_iter = g_utf8_find_next_char(entry_iter, NULL);
177                         ++k;
178                 }
179                 *entry_iter = '\0';
180                 g_ptr_array_add(current.lines, entry);
181         }
184 static void
185 screen_song_append_tag(const char *label, const struct mpd_song *song,
186                        enum mpd_tag_type tag, unsigned label_col)
188         unsigned i = 0;
189         const char *value;
191         while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
192                 screen_song_append(label, value, label_col);
195 static void
196 screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
198         unsigned i, max_label_width;
199         enum label {
200                 ARTIST, TITLE, ALBUM, LENGTH, COMPOSER, NAME, DISC, TRACK,
201                 DATE, GENRE, COMMENT, BITRATE
202         };
203         const char *labels[] = { [ARTIST] = _("Artist"),
204                 [TITLE] = _("Title"),
205                 [ALBUM] = _("Album"),
206                 [LENGTH] = _("Length"),
207                 [COMPOSER] = _("Composer"),
208                 [NAME] = _("Name"),
209                 [DISC] = _("Disc"),
210                 [TRACK] = _("Track"),
211                 [DATE] = _("Date"),
212                 [GENRE] = _("Genre"),
213                 [COMMENT] = _("Comment"),
214                 [BITRATE] = _("Bitrate"),
215         };
216         /* Determine the width of the longest label */
217         max_label_width = utf8_width(labels[0]);
218         for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
219                 if (utf8_width(labels[i]) > max_label_width)
220                         max_label_width = utf8_width(labels[i]);
221         }
223         assert(song != NULL);
225         screen_song_append_tag(labels[ARTIST], song, MPD_TAG_ARTIST,
226                                max_label_width);
227         screen_song_append_tag(labels[TITLE], song, MPD_TAG_TITLE,
228                                max_label_width);
229         screen_song_append_tag(labels[ALBUM], song, MPD_TAG_ALBUM,
230                                max_label_width);
231         /* create time string and add it */
232         if (mpd_song_get_duration(song) > 0) {
233                 char length[16];
234                 format_duration_short(length, sizeof(length),
235                                       mpd_song_get_duration(song));
236                 screen_song_append(labels[LENGTH], length, max_label_width);
237         }
238         screen_song_append_tag(labels[COMPOSER], song, MPD_TAG_COMPOSER,
239                                max_label_width);
240         screen_song_append_tag(labels[NAME], song, MPD_TAG_NAME,
241                                max_label_width);
242         screen_song_append_tag(labels[DISC], song, MPD_TAG_DISC,
243                                max_label_width);
244         screen_song_append_tag(labels[TRACK], song, MPD_TAG_TRACK,
245                                max_label_width);
246         screen_song_append_tag(labels[DATE], song, MPD_TAG_DATE,
247                                max_label_width);
248         screen_song_append_tag(labels[GENRE], song, MPD_TAG_GENRE,
249                                max_label_width);
250         screen_song_append_tag(labels[COMMENT], song, MPD_TAG_COMMENT,
251                                max_label_width);
252         screen_song_append(_("Path"), mpd_song_get_uri(song), max_label_width);
253         if (mpdclient_is_playing(c) && c->song != NULL &&
254             strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0) {
255                 char buf[16];
256                 g_snprintf(buf, sizeof(buf), _("%d kbps"),
257                            mpd_status_get_kbit_rate(c->status));
258                 screen_song_append(labels[BITRATE], buf, max_label_width);
259         }
262 static bool
263 screen_song_add_stats(struct mpd_connection *connection)
265         unsigned i, max_label_width;
266         char buf[64];
267         GDate *date;
268         enum label {
269                 ARTISTS, ALBUMS, SONGS, UPTIME,
270                 DBUPTIME, PLAYTIME, DBPLAYTIME
271         };
272         const char *labels[] = { [ARTISTS] = _("Number of artists"),
273                 [ALBUMS] = _("Number of albums"),
274                 [SONGS] = _("Number of songs"),
275                 [UPTIME] = _("Uptime"),
276                 [DBUPTIME] =_("Most recent db update"),
277                 [PLAYTIME] = _("Playtime"),
278                 [DBPLAYTIME] = _("DB playtime")
279         };
280         struct mpd_stats *mpd_stats;
282         mpd_stats = mpd_run_stats(connection);
283         if (mpd_stats == NULL)
284                 return false;
286         /* Determine the width of the longest label */
287         max_label_width = utf8_width(labels[0]);
288         for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
289                 if (utf8_width(labels[i]) > max_label_width)
290                         max_label_width = utf8_width(labels[i]);
291         }
293         g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
294         g_snprintf(buf, sizeof(buf), "%d",
295                    mpd_stats_get_number_of_artists(mpd_stats));
296         screen_song_append(labels[ARTISTS], buf, max_label_width);
297         g_snprintf(buf, sizeof(buf), "%d",
298                    mpd_stats_get_number_of_albums(mpd_stats));
299         screen_song_append(labels[ALBUMS], buf, max_label_width);
300         g_snprintf(buf, sizeof(buf), "%d",
301                    mpd_stats_get_number_of_songs(mpd_stats));
302         screen_song_append(labels[SONGS], buf, max_label_width);
304         format_duration_long(buf, sizeof(buf),
305                              mpd_stats_get_db_play_time(mpd_stats));
306         screen_song_append(labels[DBPLAYTIME], buf, max_label_width);
308         format_duration_long(buf, sizeof(buf),
309                              mpd_stats_get_play_time(mpd_stats));
310         screen_song_append(labels[PLAYTIME], buf, max_label_width);
312         format_duration_long(buf, sizeof(buf),
313                              mpd_stats_get_uptime(mpd_stats));
314         screen_song_append(labels[UPTIME], buf, max_label_width);
316         date = g_date_new();
317         g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
318         g_date_strftime(buf, sizeof(buf), "%x", date);
319         screen_song_append(labels[DBUPTIME], buf, max_label_width);
320         g_date_free(date);
322         mpd_stats_free(mpd_stats);
323         return true;
326 static void
327 screen_song_update(struct mpdclient *c)
329         struct mpd_connection *connection;
331         /* Clear all lines */
332         for (guint i = 0; i < current.lines->len; ++i)
333                 g_free(g_ptr_array_index(current.lines, i));
334         g_ptr_array_set_size(current.lines, 0);
336         /* If a song was selected before the song screen was opened */
337         if (next_song != NULL) {
338                 assert(current.selected_song == NULL);
339                 current.selected_song = next_song;
340                 next_song = NULL;
341         }
343         if (current.selected_song != NULL &&
344                         (c->song == NULL ||
345                          strcmp(mpd_song_get_uri(current.selected_song),
346                                 mpd_song_get_uri(c->song)) != 0 ||
347                          !mpdclient_is_playing(c))) {
348                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
349                 screen_song_add_song(current.selected_song, c);
350                 g_ptr_array_add(current.lines, g_strdup("\0"));
351         }
353         if (c->song != NULL && mpdclient_is_playing(c)) {
354                 if (current.played_song != NULL) {
355                         mpd_song_free(current.played_song);
356                 }
357                 current.played_song = mpd_song_dup(c->song);
358                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
359                 screen_song_add_song(current.played_song, c);
360                 g_ptr_array_add(current.lines, g_strdup("\0"));
361         }
363         /* Add some statistics about mpd */
364         connection = mpdclient_get_connection(c);
365         if (connection != NULL && !screen_song_add_stats(connection))
366                 mpdclient_handle_error(c);
368         list_window_set_length(lw, current.lines->len);
369         screen_song_repaint();
372 static bool
373 screen_song_cmd(struct mpdclient *c, command_t cmd)
375         if (list_window_scroll_cmd(lw, cmd)) {
376                 screen_song_repaint();
377                 return true;
378         }
380         switch(cmd) {
381         case CMD_LOCATE:
382                 if (current.selected_song != NULL) {
383                         screen_file_goto_song(c, current.selected_song);
384                         return true;
385                 }
386                 if (current.played_song != NULL) {
387                         screen_file_goto_song(c, current.played_song);
388                         return true;
389                 }
391                 return false;
393 #ifdef ENABLE_LYRICS_SCREEN
394         case CMD_SCREEN_LYRICS:
395                 if (current.selected_song != NULL) {
396                         screen_lyrics_switch(c, current.selected_song, false);
397                         return true;
398                 }
399                 if (current.played_song != NULL) {
400                         screen_lyrics_switch(c, current.played_song, true);
401                         return true;
402                 }
403                 return false;
405 #endif
407         case CMD_SCREEN_SWAP:
408                 if (current.selected_song != NULL)
409                         screen_swap(c, current.selected_song);
410                 else
411                 // No need to check if this is null - we'd pass null anyway
412                         screen_swap(c, current.played_song);
413                 return true;
415         default:
416                 break;
417         }
419         if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
420                 /* center the row */
421                 list_window_center(lw, lw->selected);
422                 screen_song_repaint();
423                 return true;
424         }
426         return false;
429 const struct screen_functions screen_song = {
430         .init = screen_song_init,
431         .exit = screen_song_exit,
432         .open = screen_song_update,
433         .close = screen_song_clear,
434         .resize = screen_song_resize,
435         .paint = screen_song_paint,
436         .update = screen_song_update,
437         .cmd = screen_song_cmd,
438         .get_title = screen_song_title,
439 };
441 void
442 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
444         assert(song != NULL);
445         assert(current.selected_song == NULL);
446         assert(current.played_song == NULL);
448         next_song = mpd_song_dup(song);
449         screen_switch(&screen_song, c);