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