1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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.
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 static struct list_window *lw;
39 static struct mpd_song *next_song;
41 static struct {
42 struct mpd_song *selected_song;
43 struct mpd_song *played_song;
44 GPtrArray *lines;
45 } current;
47 static void
48 screen_song_clear(void)
49 {
50 for (guint i = 0; i < current.lines->len; ++i)
51 g_free(g_ptr_array_index(current.lines, i));
53 g_ptr_array_set_size(current.lines, 0);
55 if (current.selected_song != NULL) {
56 mpd_song_free(current.selected_song);
57 current.selected_song = NULL;
58 }
59 if (current.played_song != NULL) {
60 mpd_song_free(current.played_song);
61 current.played_song = NULL;
62 }
63 }
65 static void
66 screen_song_paint(void);
68 /**
69 * Repaint and update the screen.
70 */
71 static void
72 screen_song_repaint(void)
73 {
74 screen_song_paint();
75 wrefresh(lw->w);
76 }
78 static const char *
79 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
80 G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
81 {
82 static char buffer[256];
83 char *value;
85 assert(idx < current.lines->len);
87 value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
88 g_strlcpy(buffer, value, sizeof(buffer));
89 g_free(value);
91 return buffer;
92 }
95 static void
96 screen_song_init(WINDOW *w, int cols, int rows)
97 {
98 /* We will need at least 10 lines, so this saves 10 reallocations :) */
99 current.lines = g_ptr_array_sized_new(10);
100 lw = list_window_init(w, cols, rows);
101 lw->hide_cursor = true;
102 }
104 static void
105 screen_song_exit(void)
106 {
107 list_window_free(lw);
109 screen_song_clear();
111 g_ptr_array_free(current.lines, TRUE);
112 current.lines = NULL;
113 }
115 static void
116 screen_song_resize(int cols, int rows)
117 {
118 list_window_resize(lw, cols, rows);
119 }
121 static const char *
122 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
123 {
124 return _("Song viewer");
125 }
127 static void
128 screen_song_paint(void)
129 {
130 list_window_paint(lw, screen_song_list_callback, NULL);
131 }
133 /* Appends a line with a fixed width for the label column
134 * Handles NULL strings gracefully */
135 static void
136 screen_song_append(const char *label, const char *value, unsigned label_col)
137 {
138 int value_col, linebreaks, entry_size, label_size;
139 int i, k;
140 gchar *entry, *entry_iter;
141 const gchar *value_iter;
143 assert(label != NULL);
144 assert(g_utf8_validate(label, -1, NULL));
146 if (value != NULL) {
147 assert(g_utf8_validate(value, -1, NULL));
148 /* +2 for ': ' */
149 label_col += 2;
150 value_col = lw->cols - label_col;
151 /* calculate the number of required linebreaks */
152 linebreaks = (utf8_width(value) - 1) / value_col + 1;
153 value_iter = value;
154 label_size = strlen(label) + label_col - utf8_width(label);
155 entry_size = label_size + strlen(value) + 2;
157 for (i = 0; i < linebreaks; ++i)
158 {
159 entry = g_malloc(entry_size);
160 if (i == 0) {
161 entry_iter = entry + g_sprintf(entry, "%s: ", label);
162 /* fill the label column with whitespaces */
163 for ( ; entry_iter < entry + label_size; ++entry_iter)
164 *entry_iter = ' ';
165 }
166 else {
167 entry_iter = entry;
168 /* fill the label column with whitespaces */
169 for ( ; entry_iter < entry + label_col; ++entry_iter)
170 *entry_iter = ' ';
171 }
172 /* skip whitespaces */
173 while (g_ascii_isspace(*value_iter)) ++value_iter;
174 k = 0;
175 while (value_iter && k < value_col)
176 {
177 g_utf8_strncpy(entry_iter, value_iter, 1);
178 value_iter = g_utf8_find_next_char(value_iter, NULL);
179 entry_iter = g_utf8_find_next_char(entry_iter, NULL);
180 ++k;
181 }
182 *entry_iter = '\0';
183 g_ptr_array_add(current.lines, entry);
184 }
185 }
186 }
188 static void
189 screen_song_append_tag(const char *label, const struct mpd_song *song,
190 enum mpd_tag_type tag, unsigned label_col)
191 {
192 unsigned i = 0;
193 const char *value;
195 while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
196 screen_song_append(label, value, label_col);
197 }
199 static void
200 screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
201 {
202 unsigned i, max_label_width;
203 enum label {
204 ARTIST, TITLE, ALBUM, LENGTH, COMPOSER, NAME, DISC, TRACK,
205 DATE, GENRE, COMMENT, BITRATE
206 };
207 const char *labels[] = { [ARTIST] = _("Artist"),
208 [TITLE] = _("Title"),
209 [ALBUM] = _("Album"),
210 [LENGTH] = _("Length"),
211 [COMPOSER] = _("Composer"),
212 [NAME] = _("Name"),
213 [DISC] = _("Disc"),
214 [TRACK] = _("Track"),
215 [DATE] = _("Date"),
216 [GENRE] = _("Genre"),
217 [COMMENT] = _("Comment"),
218 [BITRATE] = _("Bitrate"),
219 };
220 /* Determine the width of the longest label */
221 max_label_width = utf8_width(labels[0]);
222 for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
223 if (utf8_width(labels[i]) > max_label_width)
224 max_label_width = utf8_width(labels[i]);
225 }
227 assert(song != NULL);
229 screen_song_append_tag(labels[ARTIST], song, MPD_TAG_ARTIST,
230 max_label_width);
231 screen_song_append_tag(labels[TITLE], song, MPD_TAG_TITLE,
232 max_label_width);
233 screen_song_append_tag(labels[ALBUM], song, MPD_TAG_ALBUM,
234 max_label_width);
235 /* create time string and add it */
236 if (mpd_song_get_duration(song) > 0) {
237 char length[16];
238 format_duration_short(length, sizeof(length),
239 mpd_song_get_duration(song));
240 screen_song_append(labels[LENGTH], length, max_label_width);
241 }
242 screen_song_append_tag(labels[COMPOSER], song, MPD_TAG_COMPOSER,
243 max_label_width);
244 screen_song_append_tag(labels[NAME], song, MPD_TAG_NAME,
245 max_label_width);
246 screen_song_append_tag(labels[DISC], song, MPD_TAG_DISC,
247 max_label_width);
248 screen_song_append_tag(labels[TRACK], song, MPD_TAG_TRACK,
249 max_label_width);
250 screen_song_append_tag(labels[DATE], song, MPD_TAG_DATE,
251 max_label_width);
252 screen_song_append_tag(labels[GENRE], song, MPD_TAG_GENRE,
253 max_label_width);
254 screen_song_append_tag(labels[COMMENT], song, MPD_TAG_COMMENT,
255 max_label_width);
256 screen_song_append(_("Path"), mpd_song_get_uri(song), max_label_width);
257 if (c->status != NULL && c->song != NULL &&
258 strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
259 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
260 mpd_status_get_state(c->status) == MPD_STATE_PAUSE)) {
261 char buf[16];
262 g_snprintf(buf, sizeof(buf), _("%d kbps"),
263 mpd_status_get_kbit_rate(c->status));
264 screen_song_append(labels[BITRATE], buf, max_label_width);
265 }
266 }
268 static bool
269 screen_song_add_stats(struct mpd_connection *connection)
270 {
271 unsigned i, max_label_width;
272 char buf[64];
273 GDate *date;
274 enum label {
275 ARTISTS, ALBUMS, SONGS, UPTIME,
276 DBUPTIME, PLAYTIME, DBPLAYTIME
277 };
278 const char *labels[] = { [ARTISTS] = _("Number of artists"),
279 [ALBUMS] = _("Number of albums"),
280 [SONGS] = _("Number of songs"),
281 [UPTIME] = _("Uptime"),
282 [DBUPTIME] =_("Most recent db update"),
283 [PLAYTIME] = _("Playtime"),
284 [DBPLAYTIME] = _("DB playtime")
285 };
286 struct mpd_stats *mpd_stats;
288 mpd_stats = mpd_run_stats(connection);
289 if (mpd_stats == NULL)
290 return false;
292 /* Determine the width of the longest label */
293 max_label_width = utf8_width(labels[0]);
294 for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
295 if (utf8_width(labels[i]) > max_label_width)
296 max_label_width = utf8_width(labels[i]);
297 }
299 g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
300 g_snprintf(buf, sizeof(buf), "%d",
301 mpd_stats_get_number_of_artists(mpd_stats));
302 screen_song_append(labels[ARTISTS], buf, max_label_width);
303 g_snprintf(buf, sizeof(buf), "%d",
304 mpd_stats_get_number_of_albums(mpd_stats));
305 screen_song_append(labels[ALBUMS], buf, max_label_width);
306 g_snprintf(buf, sizeof(buf), "%d",
307 mpd_stats_get_number_of_songs(mpd_stats));
308 screen_song_append(labels[SONGS], buf, max_label_width);
310 format_duration_long(buf, sizeof(buf),
311 mpd_stats_get_db_play_time(mpd_stats));
312 screen_song_append(labels[DBPLAYTIME], buf, max_label_width);
314 format_duration_long(buf, sizeof(buf),
315 mpd_stats_get_play_time(mpd_stats));
316 screen_song_append(labels[PLAYTIME], buf, max_label_width);
318 format_duration_long(buf, sizeof(buf),
319 mpd_stats_get_uptime(mpd_stats));
320 screen_song_append(labels[UPTIME], buf, max_label_width);
322 date = g_date_new();
323 g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
324 g_date_strftime(buf, sizeof(buf), "%x", date);
325 screen_song_append(labels[DBUPTIME], buf, max_label_width);
326 g_date_free(date);
328 mpd_stats_free(mpd_stats);
329 return true;
330 }
332 static void
333 screen_song_update(struct mpdclient *c)
334 {
335 /* Clear all lines */
336 for (guint i = 0; i < current.lines->len; ++i)
337 g_free(g_ptr_array_index(current.lines, i));
338 g_ptr_array_set_size(current.lines, 0);
340 /* If a song was selected before the song screen was opened */
341 if (next_song != NULL) {
342 assert(current.selected_song == NULL);
343 current.selected_song = next_song;
344 next_song = NULL;
345 }
347 if (current.selected_song != NULL &&
348 (c->song == NULL ||
349 strcmp(mpd_song_get_uri(current.selected_song),
350 mpd_song_get_uri(c->song)) != 0 ||
351 c->status == NULL ||
352 (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
353 mpd_status_get_state(c->status) != MPD_STATE_PAUSE))) {
354 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
355 screen_song_add_song(current.selected_song, c);
356 g_ptr_array_add(current.lines, g_strdup("\0"));
357 }
359 if (c->song != NULL && c->status != NULL &&
360 (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
361 mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) {
362 if (current.played_song != NULL) {
363 mpd_song_free(current.played_song);
364 }
365 current.played_song = mpd_song_dup(c->song);
366 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
367 screen_song_add_song(current.played_song, c);
368 g_ptr_array_add(current.lines, g_strdup("\0"));
369 }
371 /* Add some statistics about mpd */
372 if (mpdclient_is_connected(c) &&
373 !screen_song_add_stats(mpdclient_get_connection(c)))
374 mpdclient_handle_error(c);
376 list_window_set_length(lw, current.lines->len);
377 screen_song_repaint();
378 }
380 static bool
381 screen_song_cmd(struct mpdclient *c, command_t cmd)
382 {
383 if (list_window_scroll_cmd(lw, cmd)) {
384 screen_song_repaint();
385 return true;
386 }
388 switch(cmd) {
389 case CMD_LOCATE:
390 if (current.selected_song != NULL) {
391 screen_file_goto_song(c, current.selected_song);
392 return true;
393 }
394 if (current.played_song != NULL) {
395 screen_file_goto_song(c, current.played_song);
396 return true;
397 }
399 return false;
401 #ifdef ENABLE_LYRICS_SCREEN
402 case CMD_SCREEN_LYRICS:
403 if (current.selected_song != NULL) {
404 screen_lyrics_switch(c, current.selected_song, false);
405 return true;
406 }
407 if (current.played_song != NULL) {
408 screen_lyrics_switch(c, current.played_song, true);
409 return true;
410 }
411 return false;
413 #endif
415 case CMD_SCREEN_SWAP:
416 if (current.selected_song != NULL)
417 screen_swap(c, current.selected_song);
418 else
419 // No need to check if this is null - we'd pass null anyway
420 screen_swap(c, current.played_song);
421 return true;
423 default:
424 break;
425 }
427 if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
428 /* center the row */
429 list_window_center(lw, lw->selected);
430 screen_song_repaint();
431 return true;
432 }
434 return false;
435 }
437 const struct screen_functions screen_song = {
438 .init = screen_song_init,
439 .exit = screen_song_exit,
440 .open = screen_song_update,
441 .close = screen_song_clear,
442 .resize = screen_song_resize,
443 .paint = screen_song_paint,
444 .update = screen_song_update,
445 .cmd = screen_song_cmd,
446 .get_title = screen_song_title,
447 };
449 void
450 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
451 {
452 assert(song != NULL);
453 assert(current.selected_song == NULL);
454 assert(current.played_song == NULL);
456 next_song = mpd_song_dup(song);
457 screen_switch(&screen_song, c);
458 }