screen_song: show audio format
[ncmpc.git] / src / screen_song.c
blob2a69860ae33e9466c96ec89e02cee843d0773fd7
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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.
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,
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"),
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,
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"),
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)
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;
109 if (current.played_song != NULL) {
110 mpd_song_free(current.played_song);
111 current.played_song = NULL;
115 static void
116 screen_song_paint(void);
119 * Repaint and update the screen.
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;
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;
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;
219 else {
220 /* fill the label column with whitespaces */
221 memset(entry, ' ', label_col);
222 entry_iter = entry + label_col;
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);
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, const struct mpdclient *c)
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;
303 screen_song_append(_(tag_labels[LABEL_LENGTH]), value,
304 max_tag_label_width);
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 if (mpdclient_is_playing(c) && c->song != NULL &&
318 strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
319 mpd_status_get_kbit_rate(c->status)) {
320 char buf[16];
321 g_snprintf(buf, sizeof(buf), _("%d kbps"),
322 mpd_status_get_kbit_rate(c->status));
323 screen_song_append(_(tag_labels[LABEL_BITRATE]), buf,
324 max_tag_label_width);
326 const struct mpd_audio_format *format =
327 mpd_status_get_audio_format(c->status);
328 if (format) {
329 g_snprintf(buf, sizeof(buf), _("%u:%u:%u"),
330 format->sample_rate, format->bits,
331 format->channels);
332 screen_song_append(_(tag_labels[LABEL_FORMAT]), buf,
333 max_tag_label_width);
338 static void
339 screen_song_append_stats(enum stats_label label, const char *value)
341 screen_song_append(_(stats_labels[label]), value,
342 max_stats_label_width);
345 static bool
346 screen_song_add_stats(struct mpd_connection *connection)
348 struct mpd_stats *mpd_stats = mpd_run_stats(connection);
349 if (mpd_stats == NULL)
350 return false;
352 g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
354 char buf[64];
355 g_snprintf(buf, sizeof(buf), "%d",
356 mpd_stats_get_number_of_artists(mpd_stats));
357 screen_song_append_stats(STATS_ARTISTS, buf);
358 g_snprintf(buf, sizeof(buf), "%d",
359 mpd_stats_get_number_of_albums(mpd_stats));
360 screen_song_append_stats(STATS_ALBUMS, buf);
361 g_snprintf(buf, sizeof(buf), "%d",
362 mpd_stats_get_number_of_songs(mpd_stats));
363 screen_song_append_stats(STATS_SONGS, buf);
365 format_duration_long(buf, sizeof(buf),
366 mpd_stats_get_db_play_time(mpd_stats));
367 screen_song_append_stats(STATS_DBPLAYTIME, buf);
369 format_duration_long(buf, sizeof(buf),
370 mpd_stats_get_play_time(mpd_stats));
371 screen_song_append_stats(STATS_PLAYTIME, buf);
373 format_duration_long(buf, sizeof(buf),
374 mpd_stats_get_uptime(mpd_stats));
375 screen_song_append_stats(STATS_UPTIME, buf);
377 GDate *date = g_date_new();
378 g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
379 g_date_strftime(buf, sizeof(buf), "%x", date);
380 screen_song_append_stats(STATS_DBUPTIME, buf);
381 g_date_free(date);
383 mpd_stats_free(mpd_stats);
384 return true;
387 static void
388 screen_song_update(struct mpdclient *c)
390 /* Clear all lines */
391 for (guint i = 0; i < current.lines->len; ++i)
392 g_free(g_ptr_array_index(current.lines, i));
393 g_ptr_array_set_size(current.lines, 0);
395 /* If a song was selected before the song screen was opened */
396 if (next_song != NULL) {
397 assert(current.selected_song == NULL);
398 current.selected_song = next_song;
399 next_song = NULL;
402 if (current.selected_song != NULL &&
403 (c->song == NULL ||
404 strcmp(mpd_song_get_uri(current.selected_song),
405 mpd_song_get_uri(c->song)) != 0 ||
406 !mpdclient_is_playing(c))) {
407 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
408 screen_song_add_song(current.selected_song, c);
409 g_ptr_array_add(current.lines, g_strdup("\0"));
412 if (c->song != NULL && mpdclient_is_playing(c)) {
413 if (current.played_song != NULL) {
414 mpd_song_free(current.played_song);
416 current.played_song = mpd_song_dup(c->song);
417 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
418 screen_song_add_song(current.played_song, c);
419 g_ptr_array_add(current.lines, g_strdup("\0"));
422 /* Add some statistics about mpd */
423 struct mpd_connection *connection = mpdclient_get_connection(c);
424 if (connection != NULL && !screen_song_add_stats(connection))
425 mpdclient_handle_error(c);
427 list_window_set_length(lw, current.lines->len);
428 screen_song_repaint();
431 static bool
432 screen_song_cmd(struct mpdclient *c, command_t cmd)
434 if (list_window_scroll_cmd(lw, cmd)) {
435 screen_song_repaint();
436 return true;
439 switch(cmd) {
440 case CMD_LOCATE:
441 if (current.selected_song != NULL) {
442 screen_file_goto_song(c, current.selected_song);
443 return true;
445 if (current.played_song != NULL) {
446 screen_file_goto_song(c, current.played_song);
447 return true;
450 return false;
452 #ifdef ENABLE_LYRICS_SCREEN
453 case CMD_SCREEN_LYRICS:
454 if (current.selected_song != NULL) {
455 screen_lyrics_switch(c, current.selected_song, false);
456 return true;
458 if (current.played_song != NULL) {
459 screen_lyrics_switch(c, current.played_song, true);
460 return true;
462 return false;
464 #endif
466 case CMD_SCREEN_SWAP:
467 if (current.selected_song != NULL)
468 screen_swap(c, current.selected_song);
469 else
470 // No need to check if this is null - we'd pass null anyway
471 screen_swap(c, current.played_song);
472 return true;
474 default:
475 break;
478 if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
479 /* center the row */
480 list_window_center(lw, lw->selected);
481 screen_song_repaint();
482 return true;
485 return false;
488 const struct screen_functions screen_song = {
489 .init = screen_song_init,
490 .exit = screen_song_exit,
491 .open = screen_song_update,
492 .close = screen_song_clear,
493 .resize = screen_song_resize,
494 .paint = screen_song_paint,
495 .update = screen_song_update,
496 .cmd = screen_song_cmd,
497 .get_title = screen_song_title,
500 void
501 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
503 assert(song != NULL);
504 assert(current.selected_song == NULL);
505 assert(current.played_song == NULL);
507 next_song = mpd_song_dup(song);
508 screen_switch(&screen_song, c);