Code

screen_song: show song range
[ncmpc.git] / src / screen_song.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 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 enum {
38         LABEL_LENGTH = MPD_TAG_COUNT,
39         LABEL_BITRATE,
40 };
42 static const char *const tag_labels[] = {
43         [MPD_TAG_ARTIST] = N_("Artist"),
44         [MPD_TAG_TITLE] = N_("Title"),
45         [MPD_TAG_ALBUM] = N_("Album"),
46         [LABEL_LENGTH] = N_("Length"),
47         [MPD_TAG_COMPOSER] = N_("Composer"),
48         [MPD_TAG_NAME] = N_("Name"),
49         [MPD_TAG_DISC] = N_("Disc"),
50         [MPD_TAG_TRACK] = N_("Track"),
51         [MPD_TAG_DATE] = N_("Date"),
52         [MPD_TAG_GENRE] = N_("Genre"),
53         [MPD_TAG_COMMENT] = N_("Comment"),
54         [LABEL_BITRATE] = N_("Bitrate"),
55 };
57 static unsigned max_tag_label_width;
59 enum stats_label {
60         STATS_ARTISTS,
61         STATS_ALBUMS,
62         STATS_SONGS,
63         STATS_UPTIME,
64         STATS_DBUPTIME,
65         STATS_PLAYTIME,
66         STATS_DBPLAYTIME,
67 };
69 static const char *const stats_labels[] = {
70         [STATS_ARTISTS] = N_("Number of artists"),
71         [STATS_ALBUMS] = N_("Number of albums"),
72         [STATS_SONGS] = N_("Number of songs"),
73         [STATS_UPTIME] = N_("Uptime"),
74         [STATS_DBUPTIME] = N_("Most recent db update"),
75         [STATS_PLAYTIME] = N_("Playtime"),
76         [STATS_DBPLAYTIME] = N_("DB playtime"),
77 };
79 static unsigned max_stats_label_width;
81 static struct list_window *lw;
83 static struct mpd_song *next_song;
85 static struct {
86         struct mpd_song *selected_song;
87         struct mpd_song *played_song;
88         GPtrArray *lines;
89 } current;
91 static void
92 screen_song_clear(void)
93 {
94         for (guint i = 0; i < current.lines->len; ++i)
95                 g_free(g_ptr_array_index(current.lines, i));
97         g_ptr_array_set_size(current.lines, 0);
99         if (current.selected_song != NULL) {
100                 mpd_song_free(current.selected_song);
101                 current.selected_song = NULL;
102         }
103         if (current.played_song != NULL) {
104                 mpd_song_free(current.played_song);
105                 current.played_song = NULL;
106         }
109 static void
110 screen_song_paint(void);
112 /**
113  * Repaint and update the screen.
114  */
115 static void
116 screen_song_repaint(void)
118         screen_song_paint();
119         wrefresh(lw->w);
122 static const char *
123 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED void *data)
125         assert(idx < current.lines->len);
127         return g_ptr_array_index(current.lines, idx);
131 static void
132 screen_song_init(WINDOW *w, int cols, int rows)
134         for (unsigned i = 0; i < G_N_ELEMENTS(tag_labels); ++i) {
135                 if (tag_labels[i] != NULL) {
136                         unsigned width = utf8_width(_(tag_labels[i]));
138                         if (width > max_tag_label_width)
139                                 max_tag_label_width = width;
140                 }
141         }
143         for (unsigned i = 0; i < G_N_ELEMENTS(stats_labels); ++i) {
144                 if (stats_labels[i] != NULL) {
145                         unsigned width = utf8_width(_(stats_labels[i]));
147                         if (width > max_stats_label_width)
148                                 max_stats_label_width = width;
149                 }
150         }
152         /* We will need at least 10 lines, so this saves 10 reallocations :) */
153         current.lines = g_ptr_array_sized_new(10);
154         lw = list_window_init(w, cols, rows);
155         lw->hide_cursor = true;
158 static void
159 screen_song_exit(void)
161         list_window_free(lw);
163         screen_song_clear();
165         g_ptr_array_free(current.lines, TRUE);
166         current.lines = NULL;
169 static void
170 screen_song_resize(int cols, int rows)
172         list_window_resize(lw, cols, rows);
175 static const char *
176 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
178         return _("Song viewer");
181 static void
182 screen_song_paint(void)
184         list_window_paint(lw, screen_song_list_callback, NULL);
187 /* Appends a line with a fixed width for the label column
188  * Handles NULL strings gracefully */
189 static void
190 screen_song_append(const char *label, const char *value, unsigned label_col)
192         unsigned label_width = locale_width(label) + 2;
193         int value_col, label_size;
194         gchar *entry, *entry_iter;
195         const gchar *value_iter;
196         char *p, *q;
197         unsigned width;
199         assert(label != NULL);
200         assert(value != NULL);
201         assert(g_utf8_validate(value, -1, NULL));
203         /* +2 for ': ' */
204         label_col += 2;
205         value_col = lw->cols - label_col;
206         /* calculate the number of required linebreaks */
207         value_iter = value;
208         label_size = strlen(label) + label_col;
210         while (*value_iter != 0) {
211                 entry = g_malloc(label_size);
212                 if (value_iter == value) {
213                         entry_iter = entry + g_sprintf(entry, "%s: ", label);
214                         /* fill the label column with whitespaces */
215                         memset(entry_iter, ' ', label_col - label_width);
216                         entry_iter += label_col - label_width;
217                 }
218                 else {
219                         /* fill the label column with whitespaces */
220                         memset(entry, ' ', label_col);
221                         entry_iter = entry + label_col;
222                 }
223                 /* skip whitespaces */
224                 while (g_ascii_isspace(*value_iter)) ++value_iter;
226                 p = g_strdup(value_iter);
227                 width = utf8_cut_width(p, value_col);
228                 if (width == 0)
229                         /* not enough room for anything - bail out */
230                         break;
232                 *entry_iter = 0;
234                 value_iter += strlen(p);
235                 p = replace_utf8_to_locale(p);
236                 q = g_strconcat(entry, p, NULL);
237                 g_free(entry);
238                 g_free(p);
240                 g_ptr_array_add(current.lines, q);
241         }
244 static void
245 screen_song_append_tag(const struct mpd_song *song, enum mpd_tag_type tag)
247         const char *label = _(tag_labels[tag]);
248         unsigned i = 0;
249         const char *value;
251         assert((unsigned)tag < G_N_ELEMENTS(tag_labels));
252         assert(label != NULL);
254         while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
255                 screen_song_append(label, value, max_tag_label_width);
258 static void
259 screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
261         assert(song != NULL);
263         screen_song_append_tag(song, MPD_TAG_ARTIST);
264         screen_song_append_tag(song, MPD_TAG_TITLE);
265         screen_song_append_tag(song, MPD_TAG_ALBUM);
267         /* create time string and add it */
268         if (mpd_song_get_duration(song) > 0) {
269                 char length[16];
270                 format_duration_short(length, sizeof(length),
271                                       mpd_song_get_duration(song));
273                 const char *value = length;
275 #if LIBMPDCLIENT_CHECK_VERSION(2,3,0)
276                 char buffer[64];
278                 if (mpd_song_get_end(song) > 0) {
279                         char start[16], end[16];
280                         format_duration_short(start, sizeof(start),
281                                               mpd_song_get_start(song));
282                         format_duration_short(end, sizeof(end),
283                                               mpd_song_get_end(song));
285                         snprintf(buffer, sizeof(buffer), "%s [%s-%s]\n",
286                                  length, start, end);
287                         value = buffer;
288                 } else if (mpd_song_get_start(song) > 0) {
289                         char start[16];
290                         format_duration_short(start, sizeof(start),
291                                               mpd_song_get_start(song));
293                         snprintf(buffer, sizeof(buffer), "%s [%s-]\n",
294                                  length, start);
295                         value = buffer;
296                 }
297 #endif
299                 screen_song_append(_(tag_labels[LABEL_LENGTH]), value,
300                                    max_tag_label_width);
301         }
303         screen_song_append_tag(song, MPD_TAG_COMPOSER);
304         screen_song_append_tag(song, MPD_TAG_NAME);
305         screen_song_append_tag(song, MPD_TAG_DISC);
306         screen_song_append_tag(song, MPD_TAG_TRACK);
307         screen_song_append_tag(song, MPD_TAG_DATE);
308         screen_song_append_tag(song, MPD_TAG_GENRE);
309         screen_song_append_tag(song, MPD_TAG_COMMENT);
311         screen_song_append(_("Path"), mpd_song_get_uri(song),
312                            max_tag_label_width);
313         if (mpdclient_is_playing(c) && c->song != NULL &&
314             strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0) {
315                 char buf[16];
316                 g_snprintf(buf, sizeof(buf), _("%d kbps"),
317                            mpd_status_get_kbit_rate(c->status));
318                 screen_song_append(_(tag_labels[LABEL_BITRATE]), buf,
319                                    max_tag_label_width);
320         }
323 static void
324 screen_song_append_stats(enum stats_label label, const char *value)
326         screen_song_append(_(stats_labels[label]), value,
327                            max_stats_label_width);
330 static bool
331 screen_song_add_stats(struct mpd_connection *connection)
333         char buf[64];
334         GDate *date;
335         struct mpd_stats *mpd_stats;
337         mpd_stats = mpd_run_stats(connection);
338         if (mpd_stats == NULL)
339                 return false;
341         g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
342         g_snprintf(buf, sizeof(buf), "%d",
343                    mpd_stats_get_number_of_artists(mpd_stats));
344         screen_song_append_stats(STATS_ARTISTS, buf);
345         g_snprintf(buf, sizeof(buf), "%d",
346                    mpd_stats_get_number_of_albums(mpd_stats));
347         screen_song_append_stats(STATS_ALBUMS, buf);
348         g_snprintf(buf, sizeof(buf), "%d",
349                    mpd_stats_get_number_of_songs(mpd_stats));
350         screen_song_append_stats(STATS_SONGS, buf);
352         format_duration_long(buf, sizeof(buf),
353                              mpd_stats_get_db_play_time(mpd_stats));
354         screen_song_append_stats(STATS_DBPLAYTIME, buf);
356         format_duration_long(buf, sizeof(buf),
357                              mpd_stats_get_play_time(mpd_stats));
358         screen_song_append_stats(STATS_PLAYTIME, buf);
360         format_duration_long(buf, sizeof(buf),
361                              mpd_stats_get_uptime(mpd_stats));
362         screen_song_append_stats(STATS_UPTIME, buf);
364         date = g_date_new();
365         g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
366         g_date_strftime(buf, sizeof(buf), "%x", date);
367         screen_song_append_stats(STATS_DBUPTIME, buf);
368         g_date_free(date);
370         mpd_stats_free(mpd_stats);
371         return true;
374 static void
375 screen_song_update(struct mpdclient *c)
377         struct mpd_connection *connection;
379         /* Clear all lines */
380         for (guint i = 0; i < current.lines->len; ++i)
381                 g_free(g_ptr_array_index(current.lines, i));
382         g_ptr_array_set_size(current.lines, 0);
384         /* If a song was selected before the song screen was opened */
385         if (next_song != NULL) {
386                 assert(current.selected_song == NULL);
387                 current.selected_song = next_song;
388                 next_song = NULL;
389         }
391         if (current.selected_song != NULL &&
392                         (c->song == NULL ||
393                          strcmp(mpd_song_get_uri(current.selected_song),
394                                 mpd_song_get_uri(c->song)) != 0 ||
395                          !mpdclient_is_playing(c))) {
396                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
397                 screen_song_add_song(current.selected_song, c);
398                 g_ptr_array_add(current.lines, g_strdup("\0"));
399         }
401         if (c->song != NULL && mpdclient_is_playing(c)) {
402                 if (current.played_song != NULL) {
403                         mpd_song_free(current.played_song);
404                 }
405                 current.played_song = mpd_song_dup(c->song);
406                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
407                 screen_song_add_song(current.played_song, c);
408                 g_ptr_array_add(current.lines, g_strdup("\0"));
409         }
411         /* Add some statistics about mpd */
412         connection = mpdclient_get_connection(c);
413         if (connection != NULL && !screen_song_add_stats(connection))
414                 mpdclient_handle_error(c);
416         list_window_set_length(lw, current.lines->len);
417         screen_song_repaint();
420 static bool
421 screen_song_cmd(struct mpdclient *c, command_t cmd)
423         if (list_window_scroll_cmd(lw, cmd)) {
424                 screen_song_repaint();
425                 return true;
426         }
428         switch(cmd) {
429         case CMD_LOCATE:
430                 if (current.selected_song != NULL) {
431                         screen_file_goto_song(c, current.selected_song);
432                         return true;
433                 }
434                 if (current.played_song != NULL) {
435                         screen_file_goto_song(c, current.played_song);
436                         return true;
437                 }
439                 return false;
441 #ifdef ENABLE_LYRICS_SCREEN
442         case CMD_SCREEN_LYRICS:
443                 if (current.selected_song != NULL) {
444                         screen_lyrics_switch(c, current.selected_song, false);
445                         return true;
446                 }
447                 if (current.played_song != NULL) {
448                         screen_lyrics_switch(c, current.played_song, true);
449                         return true;
450                 }
451                 return false;
453 #endif
455         case CMD_SCREEN_SWAP:
456                 if (current.selected_song != NULL)
457                         screen_swap(c, current.selected_song);
458                 else
459                 // No need to check if this is null - we'd pass null anyway
460                         screen_swap(c, current.played_song);
461                 return true;
463         default:
464                 break;
465         }
467         if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
468                 /* center the row */
469                 list_window_center(lw, lw->selected);
470                 screen_song_repaint();
471                 return true;
472         }
474         return false;
477 const struct screen_functions screen_song = {
478         .init = screen_song_init,
479         .exit = screen_song_exit,
480         .open = screen_song_update,
481         .close = screen_song_clear,
482         .resize = screen_song_resize,
483         .paint = screen_song_paint,
484         .update = screen_song_update,
485         .cmd = screen_song_cmd,
486         .get_title = screen_song_title,
487 };
489 void
490 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
492         assert(song != NULL);
493         assert(current.selected_song == NULL);
494         assert(current.played_song == NULL);
496         next_song = mpd_song_dup(song);
497         screen_switch(&screen_song, c);