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_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 }
113 }
115 static void
116 screen_song_paint(void);
118 /**
119 * Repaint and update the screen.
120 */
121 static void
122 screen_song_repaint(void)
123 {
124 screen_song_paint();
125 wrefresh(lw->w);
126 }
128 static const char *
129 screen_song_list_callback(unsigned idx, gcc_unused void *data)
130 {
131 assert(idx < current.lines->len);
133 return g_ptr_array_index(current.lines, idx);
134 }
137 static void
138 screen_song_init(WINDOW *w, int cols, int rows)
139 {
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;
162 }
164 static void
165 screen_song_exit(void)
166 {
167 list_window_free(lw);
169 screen_song_clear();
171 g_ptr_array_free(current.lines, TRUE);
172 current.lines = NULL;
173 }
175 static void
176 screen_song_resize(int cols, int rows)
177 {
178 list_window_resize(lw, cols, rows);
179 }
181 static const char *
182 screen_song_title(gcc_unused char *str, gcc_unused size_t size)
183 {
184 return _("Song viewer");
185 }
187 static void
188 screen_song_paint(void)
189 {
190 list_window_paint(lw, screen_song_list_callback, NULL);
191 }
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)
197 {
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 }
243 }
245 static void
246 screen_song_append_tag(const struct mpd_song *song, enum mpd_tag_type tag)
247 {
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);
257 }
259 static void
260 screen_song_add_song(const struct mpd_song *song)
261 {
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);
317 }
319 static void
320 screen_song_append_stats(enum stats_label label, const char *value)
321 {
322 screen_song_append(_(stats_labels[label]), value,
323 max_stats_label_width);
324 }
326 static bool
327 screen_song_add_stats(struct mpd_connection *connection)
328 {
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;
366 }
368 static void
369 screen_song_update(struct mpdclient *c)
370 {
371 /* Clear all lines */
372 for (guint i = 0; i < current.lines->len; ++i)
373 g_free(g_ptr_array_index(current.lines, i));
374 g_ptr_array_set_size(current.lines, 0);
376 /* If a song was selected before the song screen was opened */
377 if (next_song != NULL) {
378 assert(current.selected_song == NULL);
379 current.selected_song = next_song;
380 next_song = NULL;
381 }
383 if (current.selected_song != NULL &&
384 (c->song == NULL ||
385 strcmp(mpd_song_get_uri(current.selected_song),
386 mpd_song_get_uri(c->song)) != 0 ||
387 !mpdclient_is_playing(c))) {
388 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
389 screen_song_add_song(current.selected_song);
390 g_ptr_array_add(current.lines, g_strdup("\0"));
391 }
393 if (c->song != NULL && mpdclient_is_playing(c)) {
394 if (current.played_song != NULL) {
395 mpd_song_free(current.played_song);
396 }
397 current.played_song = mpd_song_dup(c->song);
398 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
399 screen_song_add_song(current.played_song);
401 if (mpd_status_get_kbit_rate(c->status) > 0) {
402 char buf[16];
403 g_snprintf(buf, sizeof(buf), _("%d kbps"),
404 mpd_status_get_kbit_rate(c->status));
405 screen_song_append(_(tag_labels[LABEL_BITRATE]), buf,
406 max_tag_label_width);
407 }
409 const struct mpd_audio_format *format =
410 mpd_status_get_audio_format(c->status);
411 if (format) {
412 char buf[32];
413 g_snprintf(buf, sizeof(buf), _("%u:%u:%u"),
414 format->sample_rate, format->bits,
415 format->channels);
416 screen_song_append(_(tag_labels[LABEL_FORMAT]), buf,
417 max_tag_label_width);
418 }
420 g_ptr_array_add(current.lines, g_strdup("\0"));
421 }
423 /* Add some statistics about mpd */
424 struct mpd_connection *connection = mpdclient_get_connection(c);
425 if (connection != NULL && !screen_song_add_stats(connection))
426 mpdclient_handle_error(c);
428 list_window_set_length(lw, current.lines->len);
429 screen_song_repaint();
430 }
432 static bool
433 screen_song_cmd(struct mpdclient *c, command_t cmd)
434 {
435 if (list_window_scroll_cmd(lw, cmd)) {
436 screen_song_repaint();
437 return true;
438 }
440 switch(cmd) {
441 case CMD_LOCATE:
442 if (current.selected_song != NULL) {
443 screen_file_goto_song(c, current.selected_song);
444 return true;
445 }
446 if (current.played_song != NULL) {
447 screen_file_goto_song(c, current.played_song);
448 return true;
449 }
451 return false;
453 #ifdef ENABLE_LYRICS_SCREEN
454 case CMD_SCREEN_LYRICS:
455 if (current.selected_song != NULL) {
456 screen_lyrics_switch(c, current.selected_song, false);
457 return true;
458 }
459 if (current.played_song != NULL) {
460 screen_lyrics_switch(c, current.played_song, true);
461 return true;
462 }
463 return false;
465 #endif
467 case CMD_SCREEN_SWAP:
468 if (current.selected_song != NULL)
469 screen_swap(c, current.selected_song);
470 else
471 // No need to check if this is null - we'd pass null anyway
472 screen_swap(c, current.played_song);
473 return true;
475 default:
476 break;
477 }
479 if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
480 /* center the row */
481 list_window_center(lw, lw->selected);
482 screen_song_repaint();
483 return true;
484 }
486 return false;
487 }
489 const struct screen_functions screen_song = {
490 .init = screen_song_init,
491 .exit = screen_song_exit,
492 .open = screen_song_update,
493 .close = screen_song_clear,
494 .resize = screen_song_resize,
495 .paint = screen_song_paint,
496 .update = screen_song_update,
497 .cmd = screen_song_cmd,
498 .get_title = screen_song_title,
499 };
501 void
502 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
503 {
504 assert(song != NULL);
505 assert(current.selected_song == NULL);
506 assert(current.played_song == NULL);
508 next_song = mpd_song_dup(song);
509 screen_switch(&screen_song, c);
510 }