Code

Merge remote branches 'jn/cosmetics', 'jn/doxygen' and 'jn/renames'
[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
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 };
43 static const char *const tag_labels[] = {
44         [MPD_TAG_ARTIST] = N_("Artist"),
45         [MPD_TAG_TITLE] = N_("Title"),
46         [MPD_TAG_ALBUM] = N_("Album"),
47         [LABEL_LENGTH] = N_("Length"),
48         [MPD_TAG_COMPOSER] = N_("Composer"),
49         [MPD_TAG_NAME] = N_("Name"),
50         [MPD_TAG_DISC] = N_("Disc"),
51         [MPD_TAG_TRACK] = N_("Track"),
52         [MPD_TAG_DATE] = N_("Date"),
53         [MPD_TAG_GENRE] = N_("Genre"),
54         [MPD_TAG_COMMENT] = N_("Comment"),
55         [LABEL_PATH] = N_("Path"),
56         [LABEL_BITRATE] = N_("Bitrate"),
57 };
59 static unsigned max_tag_label_width;
61 enum stats_label {
62         STATS_ARTISTS,
63         STATS_ALBUMS,
64         STATS_SONGS,
65         STATS_UPTIME,
66         STATS_DBUPTIME,
67         STATS_PLAYTIME,
68         STATS_DBPLAYTIME,
69 };
71 static const char *const stats_labels[] = {
72         [STATS_ARTISTS] = N_("Number of artists"),
73         [STATS_ALBUMS] = N_("Number of albums"),
74         [STATS_SONGS] = N_("Number of songs"),
75         [STATS_UPTIME] = N_("Uptime"),
76         [STATS_DBUPTIME] = N_("Most recent db update"),
77         [STATS_PLAYTIME] = N_("Playtime"),
78         [STATS_DBPLAYTIME] = N_("DB playtime"),
79 };
81 static unsigned max_stats_label_width;
83 static struct list_window *lw;
85 static struct mpd_song *next_song;
87 static struct {
88         struct mpd_song *selected_song;
89         struct mpd_song *played_song;
90         GPtrArray *lines;
91 } current;
93 static void
94 screen_song_clear(void)
95 {
96         for (guint i = 0; i < current.lines->len; ++i)
97                 g_free(g_ptr_array_index(current.lines, i));
99         g_ptr_array_set_size(current.lines, 0);
101         if (current.selected_song != NULL) {
102                 mpd_song_free(current.selected_song);
103                 current.selected_song = NULL;
104         }
105         if (current.played_song != NULL) {
106                 mpd_song_free(current.played_song);
107                 current.played_song = NULL;
108         }
111 static void
112 screen_song_paint(void);
114 /**
115  * Repaint and update the screen.
116  */
117 static void
118 screen_song_repaint(void)
120         screen_song_paint();
121         wrefresh(lw->w);
124 static const char *
125 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED void *data)
127         assert(idx < current.lines->len);
129         return g_ptr_array_index(current.lines, idx);
133 static void
134 screen_song_init(WINDOW *w, int cols, int rows)
136         for (unsigned i = 0; i < G_N_ELEMENTS(tag_labels); ++i) {
137                 if (tag_labels[i] != NULL) {
138                         unsigned width = utf8_width(_(tag_labels[i]));
140                         if (width > max_tag_label_width)
141                                 max_tag_label_width = width;
142                 }
143         }
145         for (unsigned i = 0; i < G_N_ELEMENTS(stats_labels); ++i) {
146                 if (stats_labels[i] != NULL) {
147                         unsigned width = utf8_width(_(stats_labels[i]));
149                         if (width > max_stats_label_width)
150                                 max_stats_label_width = width;
151                 }
152         }
154         /* We will need at least 10 lines, so this saves 10 reallocations :) */
155         current.lines = g_ptr_array_sized_new(10);
156         lw = list_window_init(w, cols, rows);
157         lw->hide_cursor = true;
160 static void
161 screen_song_exit(void)
163         list_window_free(lw);
165         screen_song_clear();
167         g_ptr_array_free(current.lines, TRUE);
168         current.lines = NULL;
171 static void
172 screen_song_resize(int cols, int rows)
174         list_window_resize(lw, cols, rows);
177 static const char *
178 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
180         return _("Song viewer");
183 static void
184 screen_song_paint(void)
186         list_window_paint(lw, screen_song_list_callback, NULL);
189 /* Appends a line with a fixed width for the label column
190  * Handles NULL strings gracefully */
191 static void
192 screen_song_append(const char *label, const char *value, unsigned label_col)
194         unsigned label_width = locale_width(label) + 2;
195         int value_col, label_size;
196         gchar *entry, *entry_iter;
197         const gchar *value_iter;
198         char *p, *q;
199         unsigned width;
201         assert(label != NULL);
202         assert(value != NULL);
203         assert(g_utf8_validate(value, -1, NULL));
205         /* +2 for ': ' */
206         label_col += 2;
207         value_col = lw->cols - label_col;
208         /* calculate the number of required linebreaks */
209         value_iter = value;
210         label_size = strlen(label) + label_col;
212         while (*value_iter != 0) {
213                 entry = g_malloc(label_size);
214                 if (value_iter == value) {
215                         entry_iter = entry + g_sprintf(entry, "%s: ", label);
216                         /* fill the label column with whitespaces */
217                         memset(entry_iter, ' ', label_col - label_width);
218                         entry_iter += label_col - label_width;
219                 }
220                 else {
221                         /* fill the label column with whitespaces */
222                         memset(entry, ' ', label_col);
223                         entry_iter = entry + label_col;
224                 }
225                 /* skip whitespaces */
226                 while (g_ascii_isspace(*value_iter)) ++value_iter;
228                 p = g_strdup(value_iter);
229                 width = utf8_cut_width(p, value_col);
230                 if (width == 0)
231                         /* not enough room for anything - bail out */
232                         break;
234                 *entry_iter = 0;
236                 value_iter += strlen(p);
237                 p = replace_utf8_to_locale(p);
238                 q = g_strconcat(entry, p, NULL);
239                 g_free(entry);
240                 g_free(p);
242                 g_ptr_array_add(current.lines, q);
243         }
246 static void
247 screen_song_append_tag(const struct mpd_song *song, enum mpd_tag_type tag)
249         const char *label = _(tag_labels[tag]);
250         unsigned i = 0;
251         const char *value;
253         assert((unsigned)tag < G_N_ELEMENTS(tag_labels));
254         assert(label != NULL);
256         while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
257                 screen_song_append(label, value, max_tag_label_width);
260 static void
261 screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
263         assert(song != NULL);
265         screen_song_append_tag(song, MPD_TAG_ARTIST);
266         screen_song_append_tag(song, MPD_TAG_TITLE);
267         screen_song_append_tag(song, MPD_TAG_ALBUM);
269         /* create time string and add it */
270         if (mpd_song_get_duration(song) > 0) {
271                 char length[16];
272                 format_duration_short(length, sizeof(length),
273                                       mpd_song_get_duration(song));
275                 const char *value = length;
277 #if LIBMPDCLIENT_CHECK_VERSION(2,3,0)
278                 char buffer[64];
280                 if (mpd_song_get_end(song) > 0) {
281                         char start[16], end[16];
282                         format_duration_short(start, sizeof(start),
283                                               mpd_song_get_start(song));
284                         format_duration_short(end, sizeof(end),
285                                               mpd_song_get_end(song));
287                         snprintf(buffer, sizeof(buffer), "%s [%s-%s]\n",
288                                  length, start, end);
289                         value = buffer;
290                 } else if (mpd_song_get_start(song) > 0) {
291                         char start[16];
292                         format_duration_short(start, sizeof(start),
293                                               mpd_song_get_start(song));
295                         snprintf(buffer, sizeof(buffer), "%s [%s-]\n",
296                                  length, start);
297                         value = buffer;
298                 }
299 #endif
301                 screen_song_append(_(tag_labels[LABEL_LENGTH]), value,
302                                    max_tag_label_width);
303         }
305         screen_song_append_tag(song, MPD_TAG_COMPOSER);
306         screen_song_append_tag(song, MPD_TAG_NAME);
307         screen_song_append_tag(song, MPD_TAG_DISC);
308         screen_song_append_tag(song, MPD_TAG_TRACK);
309         screen_song_append_tag(song, MPD_TAG_DATE);
310         screen_song_append_tag(song, MPD_TAG_GENRE);
311         screen_song_append_tag(song, MPD_TAG_COMMENT);
313         screen_song_append(_(tag_labels[LABEL_PATH]), mpd_song_get_uri(song),
314                            max_tag_label_width);
315         if (mpdclient_is_playing(c) && c->song != NULL &&
316             strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
317             mpd_status_get_kbit_rate(c->status)) {
318                 char buf[16];
319                 g_snprintf(buf, sizeof(buf), _("%d kbps"),
320                            mpd_status_get_kbit_rate(c->status));
321                 screen_song_append(_(tag_labels[LABEL_BITRATE]), buf,
322                                    max_tag_label_width);
323         }
326 static void
327 screen_song_append_stats(enum stats_label label, const char *value)
329         screen_song_append(_(stats_labels[label]), value,
330                            max_stats_label_width);
333 static bool
334 screen_song_add_stats(struct mpd_connection *connection)
336         char buf[64];
337         GDate *date;
338         struct mpd_stats *mpd_stats;
340         mpd_stats = mpd_run_stats(connection);
341         if (mpd_stats == NULL)
342                 return false;
344         g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
345         g_snprintf(buf, sizeof(buf), "%d",
346                    mpd_stats_get_number_of_artists(mpd_stats));
347         screen_song_append_stats(STATS_ARTISTS, buf);
348         g_snprintf(buf, sizeof(buf), "%d",
349                    mpd_stats_get_number_of_albums(mpd_stats));
350         screen_song_append_stats(STATS_ALBUMS, buf);
351         g_snprintf(buf, sizeof(buf), "%d",
352                    mpd_stats_get_number_of_songs(mpd_stats));
353         screen_song_append_stats(STATS_SONGS, buf);
355         format_duration_long(buf, sizeof(buf),
356                              mpd_stats_get_db_play_time(mpd_stats));
357         screen_song_append_stats(STATS_DBPLAYTIME, buf);
359         format_duration_long(buf, sizeof(buf),
360                              mpd_stats_get_play_time(mpd_stats));
361         screen_song_append_stats(STATS_PLAYTIME, buf);
363         format_duration_long(buf, sizeof(buf),
364                              mpd_stats_get_uptime(mpd_stats));
365         screen_song_append_stats(STATS_UPTIME, buf);
367         date = g_date_new();
368         g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
369         g_date_strftime(buf, sizeof(buf), "%x", date);
370         screen_song_append_stats(STATS_DBUPTIME, buf);
371         g_date_free(date);
373         mpd_stats_free(mpd_stats);
374         return true;
377 static void
378 screen_song_update(struct mpdclient *c)
380         struct mpd_connection *connection;
382         /* Clear all lines */
383         for (guint i = 0; i < current.lines->len; ++i)
384                 g_free(g_ptr_array_index(current.lines, i));
385         g_ptr_array_set_size(current.lines, 0);
387         /* If a song was selected before the song screen was opened */
388         if (next_song != NULL) {
389                 assert(current.selected_song == NULL);
390                 current.selected_song = next_song;
391                 next_song = NULL;
392         }
394         if (current.selected_song != NULL &&
395                         (c->song == NULL ||
396                          strcmp(mpd_song_get_uri(current.selected_song),
397                                 mpd_song_get_uri(c->song)) != 0 ||
398                          !mpdclient_is_playing(c))) {
399                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
400                 screen_song_add_song(current.selected_song, c);
401                 g_ptr_array_add(current.lines, g_strdup("\0"));
402         }
404         if (c->song != NULL && mpdclient_is_playing(c)) {
405                 if (current.played_song != NULL) {
406                         mpd_song_free(current.played_song);
407                 }
408                 current.played_song = mpd_song_dup(c->song);
409                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
410                 screen_song_add_song(current.played_song, c);
411                 g_ptr_array_add(current.lines, g_strdup("\0"));
412         }
414         /* Add some statistics about mpd */
415         connection = mpdclient_get_connection(c);
416         if (connection != NULL && !screen_song_add_stats(connection))
417                 mpdclient_handle_error(c);
419         list_window_set_length(lw, current.lines->len);
420         screen_song_repaint();
423 static bool
424 screen_song_cmd(struct mpdclient *c, command_t cmd)
426         if (list_window_scroll_cmd(lw, cmd)) {
427                 screen_song_repaint();
428                 return true;
429         }
431         switch(cmd) {
432         case CMD_LOCATE:
433                 if (current.selected_song != NULL) {
434                         screen_file_goto_song(c, current.selected_song);
435                         return true;
436                 }
437                 if (current.played_song != NULL) {
438                         screen_file_goto_song(c, current.played_song);
439                         return true;
440                 }
442                 return false;
444 #ifdef ENABLE_LYRICS_SCREEN
445         case CMD_SCREEN_LYRICS:
446                 if (current.selected_song != NULL) {
447                         screen_lyrics_switch(c, current.selected_song, false);
448                         return true;
449                 }
450                 if (current.played_song != NULL) {
451                         screen_lyrics_switch(c, current.played_song, true);
452                         return true;
453                 }
454                 return false;
456 #endif
458         case CMD_SCREEN_SWAP:
459                 if (current.selected_song != NULL)
460                         screen_swap(c, current.selected_song);
461                 else
462                 // No need to check if this is null - we'd pass null anyway
463                         screen_swap(c, current.played_song);
464                 return true;
466         default:
467                 break;
468         }
470         if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
471                 /* center the row */
472                 list_window_center(lw, lw->selected);
473                 screen_song_repaint();
474                 return true;
475         }
477         return false;
480 const struct screen_functions screen_song = {
481         .init = screen_song_init,
482         .exit = screen_song_exit,
483         .open = screen_song_update,
484         .close = screen_song_clear,
485         .resize = screen_song_resize,
486         .paint = screen_song_paint,
487         .update = screen_song_update,
488         .cmd = screen_song_cmd,
489         .get_title = screen_song_title,
490 };
492 void
493 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
495         assert(song != NULL);
496         assert(current.selected_song == NULL);
497         assert(current.played_song == NULL);
499         next_song = mpd_song_dup(song);
500         screen_switch(&screen_song, c);