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"
29 #include "mpdclient.h"
31 #include <mpd/client.h>
33 #include <glib/gprintf.h>
38 LABEL_LENGTH
= MPD_TAG_COUNT
,
42 static const char *const tag_labels
[] = {
43 [MPD_TAG_ARTIST
] = N_("Artist"),
44 [MPD_TAG_TITLE
] = N_("Title"),
45 [MPD_TAG_ALBUM
] = N_("Album"),
46 [LABEL_LENGTH
] = N_("Length"),
47 [MPD_TAG_COMPOSER
] = N_("Composer"),
48 [MPD_TAG_NAME
] = N_("Name"),
49 [MPD_TAG_DISC
] = N_("Disc"),
50 [MPD_TAG_TRACK
] = N_("Track"),
51 [MPD_TAG_DATE
] = N_("Date"),
52 [MPD_TAG_GENRE
] = N_("Genre"),
53 [MPD_TAG_COMMENT
] = N_("Comment"),
54 [LABEL_BITRATE
] = N_("Bitrate"),
57 static unsigned max_tag_label_width
;
69 static const char *const stats_labels
[] = {
70 [STATS_ARTISTS
] = N_("Number of artists"),
71 [STATS_ALBUMS
] = N_("Number of albums"),
72 [STATS_SONGS
] = N_("Number of songs"),
73 [STATS_UPTIME
] = N_("Uptime"),
74 [STATS_DBUPTIME
] = N_("Most recent db update"),
75 [STATS_PLAYTIME
] = N_("Playtime"),
76 [STATS_DBPLAYTIME
] = N_("DB playtime"),
79 static unsigned max_stats_label_width
;
81 static struct list_window
*lw
;
83 static struct mpd_song
*next_song
;
86 struct mpd_song
*selected_song
;
87 struct mpd_song
*played_song
;
92 screen_song_clear(void)
94 for (guint i
= 0; i
< current
.lines
->len
; ++i
)
95 g_free(g_ptr_array_index(current
.lines
, i
));
97 g_ptr_array_set_size(current
.lines
, 0);
99 if (current
.selected_song
!= NULL
) {
100 mpd_song_free(current
.selected_song
);
101 current
.selected_song
= NULL
;
103 if (current
.played_song
!= NULL
) {
104 mpd_song_free(current
.played_song
);
105 current
.played_song
= NULL
;
110 screen_song_paint(void);
113 * Repaint and update the screen.
116 screen_song_repaint(void)
123 screen_song_list_callback(unsigned idx
, G_GNUC_UNUSED
void *data
)
125 assert(idx
< current
.lines
->len
);
127 return g_ptr_array_index(current
.lines
, idx
);
132 screen_song_init(WINDOW
*w
, int cols
, int rows
)
134 for (unsigned i
= 0; i
< G_N_ELEMENTS(tag_labels
); ++i
) {
135 if (tag_labels
[i
] != NULL
) {
136 unsigned width
= utf8_width(_(tag_labels
[i
]));
138 if (width
> max_tag_label_width
)
139 max_tag_label_width
= width
;
143 for (unsigned i
= 0; i
< G_N_ELEMENTS(stats_labels
); ++i
) {
144 if (stats_labels
[i
] != NULL
) {
145 unsigned width
= utf8_width(_(stats_labels
[i
]));
147 if (width
> max_stats_label_width
)
148 max_stats_label_width
= width
;
152 /* We will need at least 10 lines, so this saves 10 reallocations :) */
153 current
.lines
= g_ptr_array_sized_new(10);
154 lw
= list_window_init(w
, cols
, rows
);
155 lw
->hide_cursor
= true;
159 screen_song_exit(void)
161 list_window_free(lw
);
165 g_ptr_array_free(current
.lines
, TRUE
);
166 current
.lines
= NULL
;
170 screen_song_resize(int cols
, int rows
)
172 list_window_resize(lw
, cols
, rows
);
176 screen_song_title(G_GNUC_UNUSED
char *str
, G_GNUC_UNUSED
size_t size
)
178 return _("Song viewer");
182 screen_song_paint(void)
184 list_window_paint(lw
, screen_song_list_callback
, NULL
);
187 /* Appends a line with a fixed width for the label column
188 * Handles NULL strings gracefully */
190 screen_song_append(const char *label
, const char *value
, unsigned label_col
)
192 unsigned label_width
= locale_width(label
) + 2;
193 int value_col
, label_size
;
194 gchar
*entry
, *entry_iter
;
195 const gchar
*value_iter
;
199 assert(label
!= NULL
);
200 assert(value
!= NULL
);
201 assert(g_utf8_validate(value
, -1, NULL
));
205 value_col
= lw
->cols
- label_col
;
206 /* calculate the number of required linebreaks */
208 label_size
= strlen(label
) + label_col
;
210 while (*value_iter
!= 0) {
211 entry
= g_malloc(label_size
);
212 if (value_iter
== value
) {
213 entry_iter
= entry
+ g_sprintf(entry
, "%s: ", label
);
214 /* fill the label column with whitespaces */
215 memset(entry_iter
, ' ', label_col
- label_width
);
216 entry_iter
+= label_col
- label_width
;
219 /* fill the label column with whitespaces */
220 memset(entry
, ' ', label_col
);
221 entry_iter
= entry
+ label_col
;
223 /* skip whitespaces */
224 while (g_ascii_isspace(*value_iter
)) ++value_iter
;
226 p
= g_strdup(value_iter
);
227 width
= utf8_cut_width(p
, value_col
);
229 /* not enough room for anything - bail out */
234 value_iter
+= strlen(p
);
235 p
= replace_utf8_to_locale(p
);
236 q
= g_strconcat(entry
, p
, NULL
);
240 g_ptr_array_add(current
.lines
, q
);
245 screen_song_append_tag(const struct mpd_song
*song
, enum mpd_tag_type tag
)
247 const char *label
= _(tag_labels
[tag
]);
251 assert((unsigned)tag
< G_N_ELEMENTS(tag_labels
));
252 assert(label
!= NULL
);
254 while ((value
= mpd_song_get_tag(song
, tag
, i
++)) != NULL
)
255 screen_song_append(label
, value
, max_tag_label_width
);
259 screen_song_add_song(const struct mpd_song
*song
, const struct mpdclient
*c
)
261 assert(song
!= NULL
);
263 screen_song_append_tag(song
, MPD_TAG_ARTIST
);
264 screen_song_append_tag(song
, MPD_TAG_TITLE
);
265 screen_song_append_tag(song
, MPD_TAG_ALBUM
);
267 /* create time string and add it */
268 if (mpd_song_get_duration(song
) > 0) {
270 format_duration_short(length
, sizeof(length
),
271 mpd_song_get_duration(song
));
273 const char *value
= length
;
275 #if LIBMPDCLIENT_CHECK_VERSION(2,3,0)
278 if (mpd_song_get_end(song
) > 0) {
279 char start
[16], end
[16];
280 format_duration_short(start
, sizeof(start
),
281 mpd_song_get_start(song
));
282 format_duration_short(end
, sizeof(end
),
283 mpd_song_get_end(song
));
285 snprintf(buffer
, sizeof(buffer
), "%s [%s-%s]\n",
288 } else if (mpd_song_get_start(song
) > 0) {
290 format_duration_short(start
, sizeof(start
),
291 mpd_song_get_start(song
));
293 snprintf(buffer
, sizeof(buffer
), "%s [%s-]\n",
299 screen_song_append(_(tag_labels
[LABEL_LENGTH
]), value
,
300 max_tag_label_width
);
303 screen_song_append_tag(song
, MPD_TAG_COMPOSER
);
304 screen_song_append_tag(song
, MPD_TAG_NAME
);
305 screen_song_append_tag(song
, MPD_TAG_DISC
);
306 screen_song_append_tag(song
, MPD_TAG_TRACK
);
307 screen_song_append_tag(song
, MPD_TAG_DATE
);
308 screen_song_append_tag(song
, MPD_TAG_GENRE
);
309 screen_song_append_tag(song
, MPD_TAG_COMMENT
);
311 screen_song_append(_("Path"), mpd_song_get_uri(song
),
312 max_tag_label_width
);
313 if (mpdclient_is_playing(c
) && c
->song
!= NULL
&&
314 strcmp(mpd_song_get_uri(c
->song
), mpd_song_get_uri(song
)) == 0) {
316 g_snprintf(buf
, sizeof(buf
), _("%d kbps"),
317 mpd_status_get_kbit_rate(c
->status
));
318 screen_song_append(_(tag_labels
[LABEL_BITRATE
]), buf
,
319 max_tag_label_width
);
324 screen_song_append_stats(enum stats_label label
, const char *value
)
326 screen_song_append(_(stats_labels
[label
]), value
,
327 max_stats_label_width
);
331 screen_song_add_stats(struct mpd_connection
*connection
)
335 struct mpd_stats
*mpd_stats
;
337 mpd_stats
= mpd_run_stats(connection
);
338 if (mpd_stats
== NULL
)
341 g_ptr_array_add(current
.lines
, g_strdup(_("MPD statistics")) );
342 g_snprintf(buf
, sizeof(buf
), "%d",
343 mpd_stats_get_number_of_artists(mpd_stats
));
344 screen_song_append_stats(STATS_ARTISTS
, buf
);
345 g_snprintf(buf
, sizeof(buf
), "%d",
346 mpd_stats_get_number_of_albums(mpd_stats
));
347 screen_song_append_stats(STATS_ALBUMS
, buf
);
348 g_snprintf(buf
, sizeof(buf
), "%d",
349 mpd_stats_get_number_of_songs(mpd_stats
));
350 screen_song_append_stats(STATS_SONGS
, buf
);
352 format_duration_long(buf
, sizeof(buf
),
353 mpd_stats_get_db_play_time(mpd_stats
));
354 screen_song_append_stats(STATS_DBPLAYTIME
, buf
);
356 format_duration_long(buf
, sizeof(buf
),
357 mpd_stats_get_play_time(mpd_stats
));
358 screen_song_append_stats(STATS_PLAYTIME
, buf
);
360 format_duration_long(buf
, sizeof(buf
),
361 mpd_stats_get_uptime(mpd_stats
));
362 screen_song_append_stats(STATS_UPTIME
, buf
);
365 g_date_set_time_t(date
, mpd_stats_get_db_update_time(mpd_stats
));
366 g_date_strftime(buf
, sizeof(buf
), "%x", date
);
367 screen_song_append_stats(STATS_DBUPTIME
, buf
);
370 mpd_stats_free(mpd_stats
);
375 screen_song_update(struct mpdclient
*c
)
377 struct mpd_connection
*connection
;
379 /* Clear all lines */
380 for (guint i
= 0; i
< current
.lines
->len
; ++i
)
381 g_free(g_ptr_array_index(current
.lines
, i
));
382 g_ptr_array_set_size(current
.lines
, 0);
384 /* If a song was selected before the song screen was opened */
385 if (next_song
!= NULL
) {
386 assert(current
.selected_song
== NULL
);
387 current
.selected_song
= next_song
;
391 if (current
.selected_song
!= NULL
&&
393 strcmp(mpd_song_get_uri(current
.selected_song
),
394 mpd_song_get_uri(c
->song
)) != 0 ||
395 !mpdclient_is_playing(c
))) {
396 g_ptr_array_add(current
.lines
, g_strdup(_("Selected song")) );
397 screen_song_add_song(current
.selected_song
, c
);
398 g_ptr_array_add(current
.lines
, g_strdup("\0"));
401 if (c
->song
!= NULL
&& mpdclient_is_playing(c
)) {
402 if (current
.played_song
!= NULL
) {
403 mpd_song_free(current
.played_song
);
405 current
.played_song
= mpd_song_dup(c
->song
);
406 g_ptr_array_add(current
.lines
, g_strdup(_("Currently playing song")));
407 screen_song_add_song(current
.played_song
, c
);
408 g_ptr_array_add(current
.lines
, g_strdup("\0"));
411 /* Add some statistics about mpd */
412 connection
= mpdclient_get_connection(c
);
413 if (connection
!= NULL
&& !screen_song_add_stats(connection
))
414 mpdclient_handle_error(c
);
416 list_window_set_length(lw
, current
.lines
->len
);
417 screen_song_repaint();
421 screen_song_cmd(struct mpdclient
*c
, command_t cmd
)
423 if (list_window_scroll_cmd(lw
, cmd
)) {
424 screen_song_repaint();
430 if (current
.selected_song
!= NULL
) {
431 screen_file_goto_song(c
, current
.selected_song
);
434 if (current
.played_song
!= NULL
) {
435 screen_file_goto_song(c
, current
.played_song
);
441 #ifdef ENABLE_LYRICS_SCREEN
442 case CMD_SCREEN_LYRICS
:
443 if (current
.selected_song
!= NULL
) {
444 screen_lyrics_switch(c
, current
.selected_song
, false);
447 if (current
.played_song
!= NULL
) {
448 screen_lyrics_switch(c
, current
.played_song
, true);
455 case CMD_SCREEN_SWAP
:
456 if (current
.selected_song
!= NULL
)
457 screen_swap(c
, current
.selected_song
);
459 // No need to check if this is null - we'd pass null anyway
460 screen_swap(c
, current
.played_song
);
467 if (screen_find(lw
, cmd
, screen_song_list_callback
, NULL
)) {
469 list_window_center(lw
, lw
->selected
);
470 screen_song_repaint();
477 const struct screen_functions screen_song
= {
478 .init
= screen_song_init
,
479 .exit
= screen_song_exit
,
480 .open
= screen_song_update
,
481 .close
= screen_song_clear
,
482 .resize
= screen_song_resize
,
483 .paint
= screen_song_paint
,
484 .update
= screen_song_update
,
485 .cmd
= screen_song_cmd
,
486 .get_title
= screen_song_title
,
490 screen_song_switch(struct mpdclient
*c
, const struct mpd_song
*song
)
492 assert(song
!= NULL
);
493 assert(current
.selected_song
== NULL
);
494 assert(current
.played_song
== NULL
);
496 next_song
= mpd_song_dup(song
);
497 screen_switch(&screen_song
, c
);