1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 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"
29 #include "mpdclient.h"
31 #include <mpd/client.h>
33 #include <glib/gprintf.h>
38 LABEL_LENGTH
= MPD_TAG_COUNT
,
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
;
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
;
92 struct mpd_song
*selected_song
;
93 struct mpd_song
*played_song
;
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
;
116 screen_song_list_callback(unsigned idx
, gcc_unused
void *data
)
118 assert(idx
< current
.lines
->len
);
120 return g_ptr_array_index(current
.lines
, idx
);
125 screen_song_init(WINDOW
*w
, unsigned cols
, unsigned rows
)
127 for (unsigned i
= 0; i
< G_N_ELEMENTS(tag_labels
); ++i
) {
128 if (tag_labels
[i
] != NULL
) {
129 unsigned width
= utf8_width(_(tag_labels
[i
]));
131 if (width
> max_tag_label_width
)
132 max_tag_label_width
= width
;
136 for (unsigned i
= 0; i
< G_N_ELEMENTS(stats_labels
); ++i
) {
137 if (stats_labels
[i
] != NULL
) {
138 unsigned width
= utf8_width(_(stats_labels
[i
]));
140 if (width
> max_stats_label_width
)
141 max_stats_label_width
= width
;
145 /* We will need at least 10 lines, so this saves 10 reallocations :) */
146 current
.lines
= g_ptr_array_sized_new(10);
147 lw
= list_window_init(w
, cols
, rows
);
148 lw
->hide_cursor
= true;
152 screen_song_exit(void)
154 list_window_free(lw
);
158 g_ptr_array_free(current
.lines
, TRUE
);
159 current
.lines
= NULL
;
163 screen_song_resize(unsigned cols
, unsigned rows
)
165 list_window_resize(lw
, cols
, rows
);
169 screen_song_title(gcc_unused
char *str
, gcc_unused
size_t size
)
171 return _("Song viewer");
175 screen_song_paint(void)
177 list_window_paint(lw
, screen_song_list_callback
, NULL
);
180 /* Appends a line with a fixed width for the label column
181 * Handles NULL strings gracefully */
183 screen_song_append(const char *label
, const char *value
, unsigned label_col
)
185 const unsigned label_width
= locale_width(label
) + 2;
187 assert(label
!= NULL
);
188 assert(value
!= NULL
);
189 assert(g_utf8_validate(value
, -1, NULL
));
193 const int value_col
= lw
->cols
- label_col
;
194 /* calculate the number of required linebreaks */
195 const gchar
*value_iter
= value
;
196 const int label_size
= strlen(label
) + label_col
;
198 while (*value_iter
!= 0) {
199 char *entry
= g_malloc(label_size
), *entry_iter
;
200 if (value_iter
== value
) {
201 entry_iter
= entry
+ g_sprintf(entry
, "%s: ", label
);
202 /* fill the label column with whitespaces */
203 memset(entry_iter
, ' ', label_col
- label_width
);
204 entry_iter
+= label_col
- label_width
;
207 /* fill the label column with whitespaces */
208 memset(entry
, ' ', label_col
);
209 entry_iter
= entry
+ label_col
;
211 /* skip whitespaces */
212 while (g_ascii_isspace(*value_iter
)) ++value_iter
;
214 char *p
= g_strdup(value_iter
);
215 unsigned width
= utf8_cut_width(p
, value_col
);
217 /* not enough room for anything - bail out */
222 value_iter
+= strlen(p
);
223 p
= replace_utf8_to_locale(p
);
224 char *q
= g_strconcat(entry
, p
, NULL
);
228 g_ptr_array_add(current
.lines
, q
);
233 screen_song_append_tag(const struct mpd_song
*song
, enum mpd_tag_type tag
)
235 const char *label
= _(tag_labels
[tag
]);
239 assert((unsigned)tag
< G_N_ELEMENTS(tag_labels
));
240 assert(label
!= NULL
);
242 while ((value
= mpd_song_get_tag(song
, tag
, i
++)) != NULL
)
243 screen_song_append(label
, value
, max_tag_label_width
);
247 screen_song_add_song(const struct mpd_song
*song
)
249 assert(song
!= NULL
);
252 g_snprintf(songpos
, sizeof(songpos
), "%d", mpd_song_get_pos(song
) + 1);
253 screen_song_append(_(tag_labels
[LABEL_POSITION
]), songpos
,
254 max_tag_label_width
);
256 screen_song_append_tag(song
, MPD_TAG_ARTIST
);
257 screen_song_append_tag(song
, MPD_TAG_TITLE
);
258 screen_song_append_tag(song
, MPD_TAG_ALBUM
);
260 /* create time string and add it */
261 if (mpd_song_get_duration(song
) > 0) {
263 format_duration_short(length
, sizeof(length
),
264 mpd_song_get_duration(song
));
266 const char *value
= length
;
270 if (mpd_song_get_end(song
) > 0) {
271 char start
[16], end
[16];
272 format_duration_short(start
, sizeof(start
),
273 mpd_song_get_start(song
));
274 format_duration_short(end
, sizeof(end
),
275 mpd_song_get_end(song
));
277 snprintf(buffer
, sizeof(buffer
), "%s [%s-%s]\n",
280 } else if (mpd_song_get_start(song
) > 0) {
282 format_duration_short(start
, sizeof(start
),
283 mpd_song_get_start(song
));
285 snprintf(buffer
, sizeof(buffer
), "%s [%s-]\n",
290 screen_song_append(_(tag_labels
[LABEL_LENGTH
]), value
,
291 max_tag_label_width
);
294 screen_song_append_tag(song
, MPD_TAG_COMPOSER
);
295 screen_song_append_tag(song
, MPD_TAG_NAME
);
296 screen_song_append_tag(song
, MPD_TAG_DISC
);
297 screen_song_append_tag(song
, MPD_TAG_TRACK
);
298 screen_song_append_tag(song
, MPD_TAG_DATE
);
299 screen_song_append_tag(song
, MPD_TAG_GENRE
);
300 screen_song_append_tag(song
, MPD_TAG_COMMENT
);
302 screen_song_append(_(tag_labels
[LABEL_PATH
]), mpd_song_get_uri(song
),
303 max_tag_label_width
);
307 screen_song_append_stats(enum stats_label label
, const char *value
)
309 screen_song_append(_(stats_labels
[label
]), value
,
310 max_stats_label_width
);
314 screen_song_add_stats(struct mpd_connection
*connection
)
316 struct mpd_stats
*mpd_stats
= mpd_run_stats(connection
);
317 if (mpd_stats
== NULL
)
320 g_ptr_array_add(current
.lines
, g_strdup(_("MPD statistics")) );
323 g_snprintf(buf
, sizeof(buf
), "%d",
324 mpd_stats_get_number_of_artists(mpd_stats
));
325 screen_song_append_stats(STATS_ARTISTS
, buf
);
326 g_snprintf(buf
, sizeof(buf
), "%d",
327 mpd_stats_get_number_of_albums(mpd_stats
));
328 screen_song_append_stats(STATS_ALBUMS
, buf
);
329 g_snprintf(buf
, sizeof(buf
), "%d",
330 mpd_stats_get_number_of_songs(mpd_stats
));
331 screen_song_append_stats(STATS_SONGS
, buf
);
333 format_duration_long(buf
, sizeof(buf
),
334 mpd_stats_get_db_play_time(mpd_stats
));
335 screen_song_append_stats(STATS_DBPLAYTIME
, buf
);
337 format_duration_long(buf
, sizeof(buf
),
338 mpd_stats_get_play_time(mpd_stats
));
339 screen_song_append_stats(STATS_PLAYTIME
, buf
);
341 format_duration_long(buf
, sizeof(buf
),
342 mpd_stats_get_uptime(mpd_stats
));
343 screen_song_append_stats(STATS_UPTIME
, buf
);
345 GDate
*date
= g_date_new();
346 g_date_set_time_t(date
, mpd_stats_get_db_update_time(mpd_stats
));
347 g_date_strftime(buf
, sizeof(buf
), "%x", date
);
348 screen_song_append_stats(STATS_DBUPTIME
, buf
);
351 mpd_stats_free(mpd_stats
);
356 audio_format_to_string(char *buffer
, size_t size
,
357 const struct mpd_audio_format
*format
)
359 #if LIBMPDCLIENT_CHECK_VERSION(2,10,0)
360 if (format
->bits
== MPD_SAMPLE_FORMAT_FLOAT
) {
361 g_snprintf(buffer
, size
, "%u:f:%u",
367 if (format
->bits
== MPD_SAMPLE_FORMAT_DSD
) {
368 if (format
->sample_rate
> 0 &&
369 format
->sample_rate
% 44100 == 0) {
370 /* use shortcuts such as "dsd64" which implies the
372 g_snprintf(buffer
, size
, "dsd%u:%u",
373 format
->sample_rate
* 8 / 44100,
378 g_snprintf(buffer
, size
, "%u:dsd:%u",
385 g_snprintf(buffer
, size
, "%u:%u:%u",
386 format
->sample_rate
, format
->bits
,
391 screen_song_update(struct mpdclient
*c
)
393 /* Clear all lines */
394 for (guint i
= 0; i
< current
.lines
->len
; ++i
)
395 g_free(g_ptr_array_index(current
.lines
, i
));
396 g_ptr_array_set_size(current
.lines
, 0);
398 /* If a song was selected before the song screen was opened */
399 if (next_song
!= NULL
) {
400 assert(current
.selected_song
== NULL
);
401 current
.selected_song
= next_song
;
405 if (current
.selected_song
!= NULL
&&
407 strcmp(mpd_song_get_uri(current
.selected_song
),
408 mpd_song_get_uri(c
->song
)) != 0 ||
409 !mpdclient_is_playing(c
))) {
410 g_ptr_array_add(current
.lines
, g_strdup(_("Selected song")) );
411 screen_song_add_song(current
.selected_song
);
412 g_ptr_array_add(current
.lines
, g_strdup("\0"));
415 if (c
->song
!= NULL
&& mpdclient_is_playing(c
)) {
416 if (current
.played_song
!= NULL
) {
417 mpd_song_free(current
.played_song
);
419 current
.played_song
= mpd_song_dup(c
->song
);
420 g_ptr_array_add(current
.lines
, g_strdup(_("Currently playing song")));
421 screen_song_add_song(current
.played_song
);
423 if (mpd_status_get_kbit_rate(c
->status
) > 0) {
425 g_snprintf(buf
, sizeof(buf
), _("%d kbps"),
426 mpd_status_get_kbit_rate(c
->status
));
427 screen_song_append(_(tag_labels
[LABEL_BITRATE
]), buf
,
428 max_tag_label_width
);
431 const struct mpd_audio_format
*format
=
432 mpd_status_get_audio_format(c
->status
);
435 audio_format_to_string(buf
, sizeof(buf
), format
);
436 screen_song_append(_(tag_labels
[LABEL_FORMAT
]), buf
,
437 max_tag_label_width
);
440 g_ptr_array_add(current
.lines
, g_strdup("\0"));
443 /* Add some statistics about mpd */
444 struct mpd_connection
*connection
= mpdclient_get_connection(c
);
445 if (connection
!= NULL
&& !screen_song_add_stats(connection
))
446 mpdclient_handle_error(c
);
448 list_window_set_length(lw
, current
.lines
->len
);
453 screen_song_cmd(struct mpdclient
*c
, command_t cmd
)
455 if (list_window_scroll_cmd(lw
, cmd
)) {
462 if (current
.selected_song
!= NULL
) {
463 screen_file_goto_song(c
, current
.selected_song
);
466 if (current
.played_song
!= NULL
) {
467 screen_file_goto_song(c
, current
.played_song
);
473 #ifdef ENABLE_LYRICS_SCREEN
474 case CMD_SCREEN_LYRICS
:
475 if (current
.selected_song
!= NULL
) {
476 screen_lyrics_switch(c
, current
.selected_song
, false);
479 if (current
.played_song
!= NULL
) {
480 screen_lyrics_switch(c
, current
.played_song
, true);
487 case CMD_SCREEN_SWAP
:
488 if (current
.selected_song
!= NULL
)
489 screen_swap(c
, current
.selected_song
);
491 // No need to check if this is null - we'd pass null anyway
492 screen_swap(c
, current
.played_song
);
499 if (screen_find(lw
, cmd
, screen_song_list_callback
, NULL
)) {
501 list_window_center(lw
, lw
->selected
);
509 const struct screen_functions screen_song
= {
510 .init
= screen_song_init
,
511 .exit
= screen_song_exit
,
512 .open
= screen_song_update
,
513 .close
= screen_song_clear
,
514 .resize
= screen_song_resize
,
515 .paint
= screen_song_paint
,
516 .update
= screen_song_update
,
517 .cmd
= screen_song_cmd
,
518 .get_title
= screen_song_title
,
522 screen_song_switch(struct mpdclient
*c
, const struct mpd_song
*song
)
524 assert(song
!= NULL
);
525 assert(current
.selected_song
== NULL
);
526 assert(current
.played_song
== NULL
);
528 next_song
= mpd_song_dup(song
);
529 screen_switch(&screen_song
, c
);