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, gcc_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(gcc_unused char *str, gcc_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 char buffer[64];
286 if (mpd_song_get_end(song) > 0) {
287 char start[16], end[16];
288 format_duration_short(start, sizeof(start),
289 mpd_song_get_start(song));
290 format_duration_short(end, sizeof(end),
291 mpd_song_get_end(song));
293 snprintf(buffer, sizeof(buffer), "%s [%s-%s]\n",
294 length, start, end);
295 value = buffer;
296 } else if (mpd_song_get_start(song) > 0) {
297 char start[16];
298 format_duration_short(start, sizeof(start),
299 mpd_song_get_start(song));
301 snprintf(buffer, sizeof(buffer), "%s [%s-]\n",
302 length, start);
303 value = buffer;
304 }
306 screen_song_append(_(tag_labels[LABEL_LENGTH]), value,
307 max_tag_label_width);
308 }
310 screen_song_append_tag(song, MPD_TAG_COMPOSER);
311 screen_song_append_tag(song, MPD_TAG_NAME);
312 screen_song_append_tag(song, MPD_TAG_DISC);
313 screen_song_append_tag(song, MPD_TAG_TRACK);
314 screen_song_append_tag(song, MPD_TAG_DATE);
315 screen_song_append_tag(song, MPD_TAG_GENRE);
316 screen_song_append_tag(song, MPD_TAG_COMMENT);
318 screen_song_append(_(tag_labels[LABEL_PATH]), mpd_song_get_uri(song),
319 max_tag_label_width);
320 if (mpdclient_is_playing(c) && c->song != NULL &&
321 strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
322 mpd_status_get_kbit_rate(c->status)) {
323 char buf[16];
324 g_snprintf(buf, sizeof(buf), _("%d kbps"),
325 mpd_status_get_kbit_rate(c->status));
326 screen_song_append(_(tag_labels[LABEL_BITRATE]), buf,
327 max_tag_label_width);
328 }
329 }
331 static void
332 screen_song_append_stats(enum stats_label label, const char *value)
333 {
334 screen_song_append(_(stats_labels[label]), value,
335 max_stats_label_width);
336 }
338 static bool
339 screen_song_add_stats(struct mpd_connection *connection)
340 {
341 char buf[64];
342 GDate *date;
343 struct mpd_stats *mpd_stats;
345 mpd_stats = mpd_run_stats(connection);
346 if (mpd_stats == NULL)
347 return false;
349 g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
350 g_snprintf(buf, sizeof(buf), "%d",
351 mpd_stats_get_number_of_artists(mpd_stats));
352 screen_song_append_stats(STATS_ARTISTS, buf);
353 g_snprintf(buf, sizeof(buf), "%d",
354 mpd_stats_get_number_of_albums(mpd_stats));
355 screen_song_append_stats(STATS_ALBUMS, buf);
356 g_snprintf(buf, sizeof(buf), "%d",
357 mpd_stats_get_number_of_songs(mpd_stats));
358 screen_song_append_stats(STATS_SONGS, buf);
360 format_duration_long(buf, sizeof(buf),
361 mpd_stats_get_db_play_time(mpd_stats));
362 screen_song_append_stats(STATS_DBPLAYTIME, buf);
364 format_duration_long(buf, sizeof(buf),
365 mpd_stats_get_play_time(mpd_stats));
366 screen_song_append_stats(STATS_PLAYTIME, buf);
368 format_duration_long(buf, sizeof(buf),
369 mpd_stats_get_uptime(mpd_stats));
370 screen_song_append_stats(STATS_UPTIME, buf);
372 date = g_date_new();
373 g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
374 g_date_strftime(buf, sizeof(buf), "%x", date);
375 screen_song_append_stats(STATS_DBUPTIME, buf);
376 g_date_free(date);
378 mpd_stats_free(mpd_stats);
379 return true;
380 }
382 static void
383 screen_song_update(struct mpdclient *c)
384 {
385 struct mpd_connection *connection;
387 /* Clear all lines */
388 for (guint i = 0; i < current.lines->len; ++i)
389 g_free(g_ptr_array_index(current.lines, i));
390 g_ptr_array_set_size(current.lines, 0);
392 /* If a song was selected before the song screen was opened */
393 if (next_song != NULL) {
394 assert(current.selected_song == NULL);
395 current.selected_song = next_song;
396 next_song = NULL;
397 }
399 if (current.selected_song != NULL &&
400 (c->song == NULL ||
401 strcmp(mpd_song_get_uri(current.selected_song),
402 mpd_song_get_uri(c->song)) != 0 ||
403 !mpdclient_is_playing(c))) {
404 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
405 screen_song_add_song(current.selected_song, c);
406 g_ptr_array_add(current.lines, g_strdup("\0"));
407 }
409 if (c->song != NULL && mpdclient_is_playing(c)) {
410 if (current.played_song != NULL) {
411 mpd_song_free(current.played_song);
412 }
413 current.played_song = mpd_song_dup(c->song);
414 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
415 screen_song_add_song(current.played_song, c);
416 g_ptr_array_add(current.lines, g_strdup("\0"));
417 }
419 /* Add some statistics about mpd */
420 connection = mpdclient_get_connection(c);
421 if (connection != NULL && !screen_song_add_stats(connection))
422 mpdclient_handle_error(c);
424 list_window_set_length(lw, current.lines->len);
425 screen_song_repaint();
426 }
428 static bool
429 screen_song_cmd(struct mpdclient *c, command_t cmd)
430 {
431 if (list_window_scroll_cmd(lw, cmd)) {
432 screen_song_repaint();
433 return true;
434 }
436 switch(cmd) {
437 case CMD_LOCATE:
438 if (current.selected_song != NULL) {
439 screen_file_goto_song(c, current.selected_song);
440 return true;
441 }
442 if (current.played_song != NULL) {
443 screen_file_goto_song(c, current.played_song);
444 return true;
445 }
447 return false;
449 #ifdef ENABLE_LYRICS_SCREEN
450 case CMD_SCREEN_LYRICS:
451 if (current.selected_song != NULL) {
452 screen_lyrics_switch(c, current.selected_song, false);
453 return true;
454 }
455 if (current.played_song != NULL) {
456 screen_lyrics_switch(c, current.played_song, true);
457 return true;
458 }
459 return false;
461 #endif
463 case CMD_SCREEN_SWAP:
464 if (current.selected_song != NULL)
465 screen_swap(c, current.selected_song);
466 else
467 // No need to check if this is null - we'd pass null anyway
468 screen_swap(c, current.played_song);
469 return true;
471 default:
472 break;
473 }
475 if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
476 /* center the row */
477 list_window_center(lw, lw->selected);
478 screen_song_repaint();
479 return true;
480 }
482 return false;
483 }
485 const struct screen_functions screen_song = {
486 .init = screen_song_init,
487 .exit = screen_song_exit,
488 .open = screen_song_update,
489 .close = screen_song_clear,
490 .resize = screen_song_resize,
491 .paint = screen_song_paint,
492 .update = screen_song_update,
493 .cmd = screen_song_cmd,
494 .get_title = screen_song_title,
495 };
497 void
498 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
499 {
500 assert(song != NULL);
501 assert(current.selected_song == NULL);
502 assert(current.played_song == NULL);
504 next_song = mpd_song_dup(song);
505 screen_switch(&screen_song, c);
506 }