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