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 LABEL_POSITION,
42 };
44 static const char *const tag_labels[] = {
45 [MPD_TAG_ARTIST] = N_("Artist"),
46 [MPD_TAG_TITLE] = N_("Title"),
47 [MPD_TAG_ALBUM] = N_("Album"),
48 [LABEL_LENGTH] = N_("Length"),
49 [LABEL_POSITION] = N_("Position"),
50 [MPD_TAG_COMPOSER] = N_("Composer"),
51 [MPD_TAG_NAME] = N_("Name"),
52 [MPD_TAG_DISC] = N_("Disc"),
53 [MPD_TAG_TRACK] = N_("Track"),
54 [MPD_TAG_DATE] = N_("Date"),
55 [MPD_TAG_GENRE] = N_("Genre"),
56 [MPD_TAG_COMMENT] = N_("Comment"),
57 [LABEL_PATH] = N_("Path"),
58 [LABEL_BITRATE] = N_("Bitrate"),
59 };
61 static unsigned max_tag_label_width;
63 enum stats_label {
64 STATS_ARTISTS,
65 STATS_ALBUMS,
66 STATS_SONGS,
67 STATS_UPTIME,
68 STATS_DBUPTIME,
69 STATS_PLAYTIME,
70 STATS_DBPLAYTIME,
71 };
73 static const char *const stats_labels[] = {
74 [STATS_ARTISTS] = N_("Number of artists"),
75 [STATS_ALBUMS] = N_("Number of albums"),
76 [STATS_SONGS] = N_("Number of songs"),
77 [STATS_UPTIME] = N_("Uptime"),
78 [STATS_DBUPTIME] = N_("Most recent db update"),
79 [STATS_PLAYTIME] = N_("Playtime"),
80 [STATS_DBPLAYTIME] = N_("DB playtime"),
81 };
83 static unsigned max_stats_label_width;
85 static struct list_window *lw;
87 static struct mpd_song *next_song;
89 static struct {
90 struct mpd_song *selected_song;
91 struct mpd_song *played_song;
92 GPtrArray *lines;
93 } current;
95 static void
96 screen_song_clear(void)
97 {
98 for (guint i = 0; i < current.lines->len; ++i)
99 g_free(g_ptr_array_index(current.lines, i));
101 g_ptr_array_set_size(current.lines, 0);
103 if (current.selected_song != NULL) {
104 mpd_song_free(current.selected_song);
105 current.selected_song = NULL;
106 }
107 if (current.played_song != NULL) {
108 mpd_song_free(current.played_song);
109 current.played_song = NULL;
110 }
111 }
113 static void
114 screen_song_paint(void);
116 /**
117 * Repaint and update the screen.
118 */
119 static void
120 screen_song_repaint(void)
121 {
122 screen_song_paint();
123 wrefresh(lw->w);
124 }
126 static const char *
127 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED void *data)
128 {
129 assert(idx < current.lines->len);
131 return g_ptr_array_index(current.lines, idx);
132 }
135 static void
136 screen_song_init(WINDOW *w, int cols, int rows)
137 {
138 for (unsigned i = 0; i < G_N_ELEMENTS(tag_labels); ++i) {
139 if (tag_labels[i] != NULL) {
140 unsigned width = utf8_width(_(tag_labels[i]));
142 if (width > max_tag_label_width)
143 max_tag_label_width = width;
144 }
145 }
147 for (unsigned i = 0; i < G_N_ELEMENTS(stats_labels); ++i) {
148 if (stats_labels[i] != NULL) {
149 unsigned width = utf8_width(_(stats_labels[i]));
151 if (width > max_stats_label_width)
152 max_stats_label_width = width;
153 }
154 }
156 /* We will need at least 10 lines, so this saves 10 reallocations :) */
157 current.lines = g_ptr_array_sized_new(10);
158 lw = list_window_init(w, cols, rows);
159 lw->hide_cursor = true;
160 }
162 static void
163 screen_song_exit(void)
164 {
165 list_window_free(lw);
167 screen_song_clear();
169 g_ptr_array_free(current.lines, TRUE);
170 current.lines = NULL;
171 }
173 static void
174 screen_song_resize(int cols, int rows)
175 {
176 list_window_resize(lw, cols, rows);
177 }
179 static const char *
180 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
181 {
182 return _("Song viewer");
183 }
185 static void
186 screen_song_paint(void)
187 {
188 list_window_paint(lw, screen_song_list_callback, NULL);
189 }
191 /* Appends a line with a fixed width for the label column
192 * Handles NULL strings gracefully */
193 static void
194 screen_song_append(const char *label, const char *value, unsigned label_col)
195 {
196 unsigned label_width = locale_width(label) + 2;
197 int value_col, label_size;
198 gchar *entry, *entry_iter;
199 const gchar *value_iter;
200 char *p, *q;
201 unsigned width;
203 assert(label != NULL);
204 assert(value != NULL);
205 assert(g_utf8_validate(value, -1, NULL));
207 /* +2 for ': ' */
208 label_col += 2;
209 value_col = lw->cols - label_col;
210 /* calculate the number of required linebreaks */
211 value_iter = value;
212 label_size = strlen(label) + label_col;
214 while (*value_iter != 0) {
215 entry = g_malloc(label_size);
216 if (value_iter == value) {
217 entry_iter = entry + g_sprintf(entry, "%s: ", label);
218 /* fill the label column with whitespaces */
219 memset(entry_iter, ' ', label_col - label_width);
220 entry_iter += label_col - label_width;
221 }
222 else {
223 /* fill the label column with whitespaces */
224 memset(entry, ' ', label_col);
225 entry_iter = entry + label_col;
226 }
227 /* skip whitespaces */
228 while (g_ascii_isspace(*value_iter)) ++value_iter;
230 p = g_strdup(value_iter);
231 width = utf8_cut_width(p, value_col);
232 if (width == 0)
233 /* not enough room for anything - bail out */
234 break;
236 *entry_iter = 0;
238 value_iter += strlen(p);
239 p = replace_utf8_to_locale(p);
240 q = g_strconcat(entry, p, NULL);
241 g_free(entry);
242 g_free(p);
244 g_ptr_array_add(current.lines, q);
245 }
246 }
248 static void
249 screen_song_append_tag(const struct mpd_song *song, enum mpd_tag_type tag)
250 {
251 const char *label = _(tag_labels[tag]);
252 unsigned i = 0;
253 const char *value;
255 assert((unsigned)tag < G_N_ELEMENTS(tag_labels));
256 assert(label != NULL);
258 while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
259 screen_song_append(label, value, max_tag_label_width);
260 }
262 static void
263 screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
264 {
265 assert(song != NULL);
267 char songpos[16];
268 g_snprintf(songpos, sizeof(songpos), "%d", mpd_song_get_pos(song) + 1);
269 screen_song_append(_(tag_labels[LABEL_POSITION]), songpos,
270 max_tag_label_width);
272 screen_song_append_tag(song, MPD_TAG_ARTIST);
273 screen_song_append_tag(song, MPD_TAG_TITLE);
274 screen_song_append_tag(song, MPD_TAG_ALBUM);
276 /* create time string and add it */
277 if (mpd_song_get_duration(song) > 0) {
278 char length[16];
279 format_duration_short(length, sizeof(length),
280 mpd_song_get_duration(song));
282 const char *value = length;
284 #if LIBMPDCLIENT_CHECK_VERSION(2,3,0)
285 char buffer[64];
287 if (mpd_song_get_end(song) > 0) {
288 char start[16], end[16];
289 format_duration_short(start, sizeof(start),
290 mpd_song_get_start(song));
291 format_duration_short(end, sizeof(end),
292 mpd_song_get_end(song));
294 snprintf(buffer, sizeof(buffer), "%s [%s-%s]\n",
295 length, start, end);
296 value = buffer;
297 } else if (mpd_song_get_start(song) > 0) {
298 char start[16];
299 format_duration_short(start, sizeof(start),
300 mpd_song_get_start(song));
302 snprintf(buffer, sizeof(buffer), "%s [%s-]\n",
303 length, start);
304 value = buffer;
305 }
306 #endif
308 screen_song_append(_(tag_labels[LABEL_LENGTH]), value,
309 max_tag_label_width);
310 }
312 screen_song_append_tag(song, MPD_TAG_COMPOSER);
313 screen_song_append_tag(song, MPD_TAG_NAME);
314 screen_song_append_tag(song, MPD_TAG_DISC);
315 screen_song_append_tag(song, MPD_TAG_TRACK);
316 screen_song_append_tag(song, MPD_TAG_DATE);
317 screen_song_append_tag(song, MPD_TAG_GENRE);
318 screen_song_append_tag(song, MPD_TAG_COMMENT);
320 screen_song_append(_(tag_labels[LABEL_PATH]), mpd_song_get_uri(song),
321 max_tag_label_width);
322 if (mpdclient_is_playing(c) && c->song != NULL &&
323 strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
324 mpd_status_get_kbit_rate(c->status)) {
325 char buf[16];
326 g_snprintf(buf, sizeof(buf), _("%d kbps"),
327 mpd_status_get_kbit_rate(c->status));
328 screen_song_append(_(tag_labels[LABEL_BITRATE]), buf,
329 max_tag_label_width);
330 }
331 }
333 static void
334 screen_song_append_stats(enum stats_label label, const char *value)
335 {
336 screen_song_append(_(stats_labels[label]), value,
337 max_stats_label_width);
338 }
340 static bool
341 screen_song_add_stats(struct mpd_connection *connection)
342 {
343 char buf[64];
344 GDate *date;
345 struct mpd_stats *mpd_stats;
347 mpd_stats = mpd_run_stats(connection);
348 if (mpd_stats == NULL)
349 return false;
351 g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
352 g_snprintf(buf, sizeof(buf), "%d",
353 mpd_stats_get_number_of_artists(mpd_stats));
354 screen_song_append_stats(STATS_ARTISTS, buf);
355 g_snprintf(buf, sizeof(buf), "%d",
356 mpd_stats_get_number_of_albums(mpd_stats));
357 screen_song_append_stats(STATS_ALBUMS, buf);
358 g_snprintf(buf, sizeof(buf), "%d",
359 mpd_stats_get_number_of_songs(mpd_stats));
360 screen_song_append_stats(STATS_SONGS, buf);
362 format_duration_long(buf, sizeof(buf),
363 mpd_stats_get_db_play_time(mpd_stats));
364 screen_song_append_stats(STATS_DBPLAYTIME, buf);
366 format_duration_long(buf, sizeof(buf),
367 mpd_stats_get_play_time(mpd_stats));
368 screen_song_append_stats(STATS_PLAYTIME, buf);
370 format_duration_long(buf, sizeof(buf),
371 mpd_stats_get_uptime(mpd_stats));
372 screen_song_append_stats(STATS_UPTIME, buf);
374 date = g_date_new();
375 g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
376 g_date_strftime(buf, sizeof(buf), "%x", date);
377 screen_song_append_stats(STATS_DBUPTIME, buf);
378 g_date_free(date);
380 mpd_stats_free(mpd_stats);
381 return true;
382 }
384 static void
385 screen_song_update(struct mpdclient *c)
386 {
387 struct mpd_connection *connection;
389 /* Clear all lines */
390 for (guint i = 0; i < current.lines->len; ++i)
391 g_free(g_ptr_array_index(current.lines, i));
392 g_ptr_array_set_size(current.lines, 0);
394 /* If a song was selected before the song screen was opened */
395 if (next_song != NULL) {
396 assert(current.selected_song == NULL);
397 current.selected_song = next_song;
398 next_song = NULL;
399 }
401 if (current.selected_song != NULL &&
402 (c->song == NULL ||
403 strcmp(mpd_song_get_uri(current.selected_song),
404 mpd_song_get_uri(c->song)) != 0 ||
405 !mpdclient_is_playing(c))) {
406 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
407 screen_song_add_song(current.selected_song, c);
408 g_ptr_array_add(current.lines, g_strdup("\0"));
409 }
411 if (c->song != NULL && mpdclient_is_playing(c)) {
412 if (current.played_song != NULL) {
413 mpd_song_free(current.played_song);
414 }
415 current.played_song = mpd_song_dup(c->song);
416 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
417 screen_song_add_song(current.played_song, c);
418 g_ptr_array_add(current.lines, g_strdup("\0"));
419 }
421 /* Add some statistics about mpd */
422 connection = mpdclient_get_connection(c);
423 if (connection != NULL && !screen_song_add_stats(connection))
424 mpdclient_handle_error(c);
426 list_window_set_length(lw, current.lines->len);
427 screen_song_repaint();
428 }
430 static bool
431 screen_song_cmd(struct mpdclient *c, command_t cmd)
432 {
433 if (list_window_scroll_cmd(lw, cmd)) {
434 screen_song_repaint();
435 return true;
436 }
438 switch(cmd) {
439 case CMD_LOCATE:
440 if (current.selected_song != NULL) {
441 screen_file_goto_song(c, current.selected_song);
442 return true;
443 }
444 if (current.played_song != NULL) {
445 screen_file_goto_song(c, current.played_song);
446 return true;
447 }
449 return false;
451 #ifdef ENABLE_LYRICS_SCREEN
452 case CMD_SCREEN_LYRICS:
453 if (current.selected_song != NULL) {
454 screen_lyrics_switch(c, current.selected_song, false);
455 return true;
456 }
457 if (current.played_song != NULL) {
458 screen_lyrics_switch(c, current.played_song, true);
459 return true;
460 }
461 return false;
463 #endif
465 case CMD_SCREEN_SWAP:
466 if (current.selected_song != NULL)
467 screen_swap(c, current.selected_song);
468 else
469 // No need to check if this is null - we'd pass null anyway
470 screen_swap(c, current.played_song);
471 return true;
473 default:
474 break;
475 }
477 if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
478 /* center the row */
479 list_window_center(lw, lw->selected);
480 screen_song_repaint();
481 return true;
482 }
484 return false;
485 }
487 const struct screen_functions screen_song = {
488 .init = screen_song_init,
489 .exit = screen_song_exit,
490 .open = screen_song_update,
491 .close = screen_song_clear,
492 .resize = screen_song_resize,
493 .paint = screen_song_paint,
494 .update = screen_song_update,
495 .cmd = screen_song_cmd,
496 .get_title = screen_song_title,
497 };
499 void
500 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
501 {
502 assert(song != NULL);
503 assert(current.selected_song == NULL);
504 assert(current.played_song == NULL);
506 next_song = mpd_song_dup(song);
507 screen_switch(&screen_song, c);
508 }