Code

c6d7d801e47299c3413079bc60577898d704f25f
[ncmpc.git] / src / screen_song.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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_PATH,
40         LABEL_BITRATE,
41         LABEL_FORMAT,
42         LABEL_POSITION,
43 };
45 static const char *const tag_labels[] = {
46         [MPD_TAG_ARTIST] = N_("Artist"),
47         [MPD_TAG_TITLE] = N_("Title"),
48         [MPD_TAG_ALBUM] = N_("Album"),
49         [LABEL_LENGTH] = N_("Length"),
50         [LABEL_POSITION] = N_("Position"),
51         [MPD_TAG_COMPOSER] = N_("Composer"),
52         [MPD_TAG_NAME] = N_("Name"),
53         [MPD_TAG_DISC] = N_("Disc"),
54         [MPD_TAG_TRACK] = N_("Track"),
55         [MPD_TAG_DATE] = N_("Date"),
56         [MPD_TAG_GENRE] = N_("Genre"),
57         [MPD_TAG_COMMENT] = N_("Comment"),
58         [LABEL_PATH] = N_("Path"),
59         [LABEL_BITRATE] = N_("Bitrate"),
60         [LABEL_FORMAT] = N_("Format"),
61 };
63 static unsigned max_tag_label_width;
65 enum stats_label {
66         STATS_ARTISTS,
67         STATS_ALBUMS,
68         STATS_SONGS,
69         STATS_UPTIME,
70         STATS_DBUPTIME,
71         STATS_PLAYTIME,
72         STATS_DBPLAYTIME,
73 };
75 static const char *const stats_labels[] = {
76         [STATS_ARTISTS] = N_("Number of artists"),
77         [STATS_ALBUMS] = N_("Number of albums"),
78         [STATS_SONGS] = N_("Number of songs"),
79         [STATS_UPTIME] = N_("Uptime"),
80         [STATS_DBUPTIME] = N_("Most recent db update"),
81         [STATS_PLAYTIME] = N_("Playtime"),
82         [STATS_DBPLAYTIME] = N_("DB playtime"),
83 };
85 static unsigned max_stats_label_width;
87 static struct list_window *lw;
89 static struct mpd_song *next_song;
91 static struct {
92         struct mpd_song *selected_song;
93         struct mpd_song *played_song;
94         GPtrArray *lines;
95 } current;
97 static void
98 screen_song_clear(void)
99 {
100         for (guint i = 0; i < current.lines->len; ++i)
101                 g_free(g_ptr_array_index(current.lines, i));
103         g_ptr_array_set_size(current.lines, 0);
105         if (current.selected_song != NULL) {
106                 mpd_song_free(current.selected_song);
107                 current.selected_song = NULL;
108         }
109         if (current.played_song != NULL) {
110                 mpd_song_free(current.played_song);
111                 current.played_song = NULL;
112         }
115 static void
116 screen_song_paint(void);
118 /**
119  * Repaint and update the screen.
120  */
121 static void
122 screen_song_repaint(void)
124         screen_song_paint();
125         wrefresh(lw->w);
128 static const char *
129 screen_song_list_callback(unsigned idx, gcc_unused void *data)
131         assert(idx < current.lines->len);
133         return g_ptr_array_index(current.lines, idx);
137 static void
138 screen_song_init(WINDOW *w, int cols, int rows)
140         for (unsigned i = 0; i < G_N_ELEMENTS(tag_labels); ++i) {
141                 if (tag_labels[i] != NULL) {
142                         unsigned width = utf8_width(_(tag_labels[i]));
144                         if (width > max_tag_label_width)
145                                 max_tag_label_width = width;
146                 }
147         }
149         for (unsigned i = 0; i < G_N_ELEMENTS(stats_labels); ++i) {
150                 if (stats_labels[i] != NULL) {
151                         unsigned width = utf8_width(_(stats_labels[i]));
153                         if (width > max_stats_label_width)
154                                 max_stats_label_width = width;
155                 }
156         }
158         /* We will need at least 10 lines, so this saves 10 reallocations :) */
159         current.lines = g_ptr_array_sized_new(10);
160         lw = list_window_init(w, cols, rows);
161         lw->hide_cursor = true;
164 static void
165 screen_song_exit(void)
167         list_window_free(lw);
169         screen_song_clear();
171         g_ptr_array_free(current.lines, TRUE);
172         current.lines = NULL;
175 static void
176 screen_song_resize(int cols, int rows)
178         list_window_resize(lw, cols, rows);
181 static const char *
182 screen_song_title(gcc_unused char *str, gcc_unused size_t size)
184         return _("Song viewer");
187 static void
188 screen_song_paint(void)
190         list_window_paint(lw, screen_song_list_callback, NULL);
193 /* Appends a line with a fixed width for the label column
194  * Handles NULL strings gracefully */
195 static void
196 screen_song_append(const char *label, const char *value, unsigned label_col)
198         const unsigned label_width = locale_width(label) + 2;
200         assert(label != NULL);
201         assert(value != NULL);
202         assert(g_utf8_validate(value, -1, NULL));
204         /* +2 for ': ' */
205         label_col += 2;
206         const int value_col = lw->cols - label_col;
207         /* calculate the number of required linebreaks */
208         const gchar *value_iter = value;
209         const int label_size = strlen(label) + label_col;
211         while (*value_iter != 0) {
212                 char *entry = g_malloc(label_size), *entry_iter;
213                 if (value_iter == value) {
214                         entry_iter = entry + g_sprintf(entry, "%s: ", label);
215                         /* fill the label column with whitespaces */
216                         memset(entry_iter, ' ', label_col - label_width);
217                         entry_iter += label_col - label_width;
218                 }
219                 else {
220                         /* fill the label column with whitespaces */
221                         memset(entry, ' ', label_col);
222                         entry_iter = entry + label_col;
223                 }
224                 /* skip whitespaces */
225                 while (g_ascii_isspace(*value_iter)) ++value_iter;
227                 char *p = g_strdup(value_iter);
228                 unsigned width = utf8_cut_width(p, value_col);
229                 if (width == 0)
230                         /* not enough room for anything - bail out */
231                         break;
233                 *entry_iter = 0;
235                 value_iter += strlen(p);
236                 p = replace_utf8_to_locale(p);
237                 char *q = g_strconcat(entry, p, NULL);
238                 g_free(entry);
239                 g_free(p);
241                 g_ptr_array_add(current.lines, q);
242         }
245 static void
246 screen_song_append_tag(const struct mpd_song *song, enum mpd_tag_type tag)
248         const char *label = _(tag_labels[tag]);
249         unsigned i = 0;
250         const char *value;
252         assert((unsigned)tag < G_N_ELEMENTS(tag_labels));
253         assert(label != NULL);
255         while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
256                 screen_song_append(label, value, max_tag_label_width);
259 static void
260 screen_song_add_song(const struct mpd_song *song)
262         assert(song != NULL);
264         char songpos[16];
265         g_snprintf(songpos, sizeof(songpos), "%d", mpd_song_get_pos(song) + 1);
266         screen_song_append(_(tag_labels[LABEL_POSITION]), songpos,
267                            max_tag_label_width);
269         screen_song_append_tag(song, MPD_TAG_ARTIST);
270         screen_song_append_tag(song, MPD_TAG_TITLE);
271         screen_song_append_tag(song, MPD_TAG_ALBUM);
273         /* create time string and add it */
274         if (mpd_song_get_duration(song) > 0) {
275                 char length[16];
276                 format_duration_short(length, sizeof(length),
277                                       mpd_song_get_duration(song));
279                 const char *value = length;
281                 char buffer[64];
283                 if (mpd_song_get_end(song) > 0) {
284                         char start[16], end[16];
285                         format_duration_short(start, sizeof(start),
286                                               mpd_song_get_start(song));
287                         format_duration_short(end, sizeof(end),
288                                               mpd_song_get_end(song));
290                         snprintf(buffer, sizeof(buffer), "%s [%s-%s]\n",
291                                  length, start, end);
292                         value = buffer;
293                 } else if (mpd_song_get_start(song) > 0) {
294                         char start[16];
295                         format_duration_short(start, sizeof(start),
296                                               mpd_song_get_start(song));
298                         snprintf(buffer, sizeof(buffer), "%s [%s-]\n",
299                                  length, start);
300                         value = buffer;
301                 }
303                 screen_song_append(_(tag_labels[LABEL_LENGTH]), value,
304                                    max_tag_label_width);
305         }
307         screen_song_append_tag(song, MPD_TAG_COMPOSER);
308         screen_song_append_tag(song, MPD_TAG_NAME);
309         screen_song_append_tag(song, MPD_TAG_DISC);
310         screen_song_append_tag(song, MPD_TAG_TRACK);
311         screen_song_append_tag(song, MPD_TAG_DATE);
312         screen_song_append_tag(song, MPD_TAG_GENRE);
313         screen_song_append_tag(song, MPD_TAG_COMMENT);
315         screen_song_append(_(tag_labels[LABEL_PATH]), mpd_song_get_uri(song),
316                            max_tag_label_width);
319 static void
320 screen_song_append_stats(enum stats_label label, const char *value)
322         screen_song_append(_(stats_labels[label]), value,
323                            max_stats_label_width);
326 static bool
327 screen_song_add_stats(struct mpd_connection *connection)
329         struct mpd_stats *mpd_stats = mpd_run_stats(connection);
330         if (mpd_stats == NULL)
331                 return false;
333         g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
335         char buf[64];
336         g_snprintf(buf, sizeof(buf), "%d",
337                    mpd_stats_get_number_of_artists(mpd_stats));
338         screen_song_append_stats(STATS_ARTISTS, buf);
339         g_snprintf(buf, sizeof(buf), "%d",
340                    mpd_stats_get_number_of_albums(mpd_stats));
341         screen_song_append_stats(STATS_ALBUMS, buf);
342         g_snprintf(buf, sizeof(buf), "%d",
343                    mpd_stats_get_number_of_songs(mpd_stats));
344         screen_song_append_stats(STATS_SONGS, buf);
346         format_duration_long(buf, sizeof(buf),
347                              mpd_stats_get_db_play_time(mpd_stats));
348         screen_song_append_stats(STATS_DBPLAYTIME, buf);
350         format_duration_long(buf, sizeof(buf),
351                              mpd_stats_get_play_time(mpd_stats));
352         screen_song_append_stats(STATS_PLAYTIME, buf);
354         format_duration_long(buf, sizeof(buf),
355                              mpd_stats_get_uptime(mpd_stats));
356         screen_song_append_stats(STATS_UPTIME, buf);
358         GDate *date = g_date_new();
359         g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
360         g_date_strftime(buf, sizeof(buf), "%x", date);
361         screen_song_append_stats(STATS_DBUPTIME, buf);
362         g_date_free(date);
364         mpd_stats_free(mpd_stats);
365         return true;
368 static void
369 audio_format_to_string(char *buffer, size_t size,
370                        const struct mpd_audio_format *format)
372 #if LIBMPDCLIENT_CHECK_VERSION(2,10,0)
373         if (format->bits == MPD_SAMPLE_FORMAT_FLOAT) {
374                 g_snprintf(buffer, size, _("%u:f:%u"),
375                            format->sample_rate,
376                            format->channels);
377                 return;
378         }
380         if (format->bits == MPD_SAMPLE_FORMAT_DSD) {
381                 if (format->sample_rate > 0 &&
382                     format->sample_rate % 44100 == 0) {
383                         /* use shortcuts such as "dsd64" which implies the
384                            sample rate */
385                         g_snprintf(buffer, size, _("dsd%u:%u"),
386                                    format->sample_rate * 8 / 44100,
387                                    format->channels);
388                         return;
389                 }
391                 g_snprintf(buffer, size, _("%u:dsd:%u"),
392                            format->sample_rate,
393                            format->channels);
394                 return;
395         }
396 #endif
398         g_snprintf(buffer, size, _("%u:%u:%u"),
399                    format->sample_rate, format->bits,
400                    format->channels);
403 static void
404 screen_song_update(struct mpdclient *c)
406         /* Clear all lines */
407         for (guint i = 0; i < current.lines->len; ++i)
408                 g_free(g_ptr_array_index(current.lines, i));
409         g_ptr_array_set_size(current.lines, 0);
411         /* If a song was selected before the song screen was opened */
412         if (next_song != NULL) {
413                 assert(current.selected_song == NULL);
414                 current.selected_song = next_song;
415                 next_song = NULL;
416         }
418         if (current.selected_song != NULL &&
419                         (c->song == NULL ||
420                          strcmp(mpd_song_get_uri(current.selected_song),
421                                 mpd_song_get_uri(c->song)) != 0 ||
422                          !mpdclient_is_playing(c))) {
423                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
424                 screen_song_add_song(current.selected_song);
425                 g_ptr_array_add(current.lines, g_strdup("\0"));
426         }
428         if (c->song != NULL && mpdclient_is_playing(c)) {
429                 if (current.played_song != NULL) {
430                         mpd_song_free(current.played_song);
431                 }
432                 current.played_song = mpd_song_dup(c->song);
433                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
434                 screen_song_add_song(current.played_song);
436                 if (mpd_status_get_kbit_rate(c->status) > 0) {
437                         char buf[16];
438                         g_snprintf(buf, sizeof(buf), _("%d kbps"),
439                                    mpd_status_get_kbit_rate(c->status));
440                         screen_song_append(_(tag_labels[LABEL_BITRATE]), buf,
441                                            max_tag_label_width);
442                 }
444                 const struct mpd_audio_format *format =
445                         mpd_status_get_audio_format(c->status);
446                 if (format) {
447                         char buf[32];
448                         audio_format_to_string(buf, sizeof(buf), format);
449                         screen_song_append(_(tag_labels[LABEL_FORMAT]), buf,
450                                            max_tag_label_width);
451                 }
453                 g_ptr_array_add(current.lines, g_strdup("\0"));
454         }
456         /* Add some statistics about mpd */
457         struct mpd_connection *connection = mpdclient_get_connection(c);
458         if (connection != NULL && !screen_song_add_stats(connection))
459                 mpdclient_handle_error(c);
461         list_window_set_length(lw, current.lines->len);
462         screen_song_repaint();
465 static bool
466 screen_song_cmd(struct mpdclient *c, command_t cmd)
468         if (list_window_scroll_cmd(lw, cmd)) {
469                 screen_song_repaint();
470                 return true;
471         }
473         switch(cmd) {
474         case CMD_LOCATE:
475                 if (current.selected_song != NULL) {
476                         screen_file_goto_song(c, current.selected_song);
477                         return true;
478                 }
479                 if (current.played_song != NULL) {
480                         screen_file_goto_song(c, current.played_song);
481                         return true;
482                 }
484                 return false;
486 #ifdef ENABLE_LYRICS_SCREEN
487         case CMD_SCREEN_LYRICS:
488                 if (current.selected_song != NULL) {
489                         screen_lyrics_switch(c, current.selected_song, false);
490                         return true;
491                 }
492                 if (current.played_song != NULL) {
493                         screen_lyrics_switch(c, current.played_song, true);
494                         return true;
495                 }
496                 return false;
498 #endif
500         case CMD_SCREEN_SWAP:
501                 if (current.selected_song != NULL)
502                         screen_swap(c, current.selected_song);
503                 else
504                 // No need to check if this is null - we'd pass null anyway
505                         screen_swap(c, current.played_song);
506                 return true;
508         default:
509                 break;
510         }
512         if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
513                 /* center the row */
514                 list_window_center(lw, lw->selected);
515                 screen_song_repaint();
516                 return true;
517         }
519         return false;
522 const struct screen_functions screen_song = {
523         .init = screen_song_init,
524         .exit = screen_song_exit,
525         .open = screen_song_update,
526         .close = screen_song_clear,
527         .resize = screen_song_resize,
528         .paint = screen_song_paint,
529         .update = screen_song_update,
530         .cmd = screen_song_cmd,
531         .get_title = screen_song_title,
532 };
534 void
535 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
537         assert(song != NULL);
538         assert(current.selected_song == NULL);
539         assert(current.played_song == NULL);
541         next_song = mpd_song_dup(song);
542         screen_switch(&screen_song, c);