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_interface.h"
21 #include "screen_file.h"
22 #include "i18n.h"
23 #include "screen.h"
24 #include "screen_utils.h"
25 #include "charset.h"
26 #include "utils.h"
27 #include "mpdclient.h"
29 #include <mpd/client.h>
31 #include <glib/gprintf.h>
32 #include <assert.h>
33 #include <string.h>
35 static list_window_t *lw;
37 static struct mpd_song *next_song;
39 static struct {
40 struct mpd_song *selected_song;
41 struct mpd_song *played_song;
42 GPtrArray *lines;
43 } current;
45 static void
46 screen_song_clear(void)
47 {
48 for (guint i = 0; i < current.lines->len; ++i)
49 g_free(g_ptr_array_index(current.lines, i));
51 g_ptr_array_set_size(current.lines, 0);
53 if (current.selected_song != NULL) {
54 mpd_song_free(current.selected_song);
55 current.selected_song = NULL;
56 }
57 if (current.played_song != NULL) {
58 mpd_song_free(current.played_song);
59 current.played_song = NULL;
60 }
61 }
63 static void
64 screen_song_paint(void);
66 /**
67 * Repaint and update the screen.
68 */
69 static void
70 screen_song_repaint(void)
71 {
72 screen_song_paint();
73 wrefresh(lw->w);
74 }
76 static const char *
77 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
78 G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
79 {
80 static char buffer[256];
81 char *value;
83 if (idx >= current.lines->len)
84 return NULL;
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 lw->cols = cols;
118 lw->rows = 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 unsigned t = mpd_song_get_duration(song);
238 char length[16];
240 /*write out the time, using hours if time over 60 minutes*/
241 if (t > 3600) {
242 g_snprintf(length, sizeof(length),
243 "%i:%02i:%02i",
244 t/3600, (t%3600)/60, t%60);
245 } else {
246 g_snprintf(length, sizeof(length),
247 "%i:%02i", t/60, t%60);
248 }
249 screen_song_append(labels[LENGTH], length, max_label_width);
250 }
251 screen_song_append_tag(labels[COMPOSER], song, MPD_TAG_COMPOSER,
252 max_label_width);
253 screen_song_append_tag(labels[NAME], song, MPD_TAG_NAME,
254 max_label_width);
255 screen_song_append_tag(labels[DISC], song, MPD_TAG_DISC,
256 max_label_width);
257 screen_song_append_tag(labels[TRACK], song, MPD_TAG_TRACK,
258 max_label_width);
259 screen_song_append_tag(labels[DATE], song, MPD_TAG_DATE,
260 max_label_width);
261 screen_song_append_tag(labels[GENRE], song, MPD_TAG_GENRE,
262 max_label_width);
263 screen_song_append_tag(labels[COMMENT], song, MPD_TAG_COMMENT,
264 max_label_width);
265 screen_song_append(_("Path"), mpd_song_get_uri(song), max_label_width);
266 if (c->status != NULL && c->song != NULL &&
267 strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
268 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
269 mpd_status_get_state(c->status) == MPD_STATE_PAUSE)) {
270 char buf[16];
271 g_snprintf(buf, sizeof(buf), _("%d kbps"),
272 mpd_status_get_kbit_rate(c->status));
273 screen_song_append(labels[BITRATE], buf, max_label_width);
274 }
275 }
277 static void
278 screen_song_add_stats(const struct mpdclient *c)
279 {
280 unsigned i, max_label_width;
281 char buf[64];
282 char *duration;
283 GDate *date;
284 enum label {
285 ARTISTS, ALBUMS, SONGS, UPTIME,
286 DBUPTIME, PLAYTIME, DBPLAYTIME
287 };
288 const char *labels[] = { [ARTISTS] = _("Number of artists"),
289 [ALBUMS] = _("Number of albums"),
290 [SONGS] = _("Number of songs"),
291 [UPTIME] = _("Uptime"),
292 [DBUPTIME] =_("Most recent db update"),
293 [PLAYTIME] = _("Playtime"),
294 [DBPLAYTIME] = _("DB playtime")
295 };
296 struct mpd_stats *mpd_stats = NULL;
298 if (c->connection != NULL) {
299 mpd_stats = mpd_run_stats(c->connection);
300 }
302 if (mpd_stats != NULL) {
303 /* Determine the width of the longest label */
304 max_label_width = utf8_width(labels[0]);
305 for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
306 if (utf8_width(labels[i]) > max_label_width)
307 max_label_width = utf8_width(labels[i]);
308 }
310 g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
311 g_snprintf(buf, sizeof(buf), "%d",
312 mpd_stats_get_number_of_artists(mpd_stats));
313 screen_song_append(labels[ARTISTS], buf, max_label_width);
314 g_snprintf(buf, sizeof(buf), "%d",
315 mpd_stats_get_number_of_albums(mpd_stats));
316 screen_song_append(labels[ALBUMS], buf, max_label_width);
317 g_snprintf(buf, sizeof(buf), "%d",
318 mpd_stats_get_number_of_songs(mpd_stats));
319 screen_song_append(labels[SONGS], buf, max_label_width);
320 duration = time_seconds_to_durationstr(mpd_stats_get_db_play_time(mpd_stats));
321 screen_song_append(labels[DBPLAYTIME], duration, max_label_width);
322 g_free(duration);
323 duration = time_seconds_to_durationstr(mpd_stats_get_play_time(mpd_stats));
324 screen_song_append(labels[PLAYTIME], duration, max_label_width);
325 g_free(duration);
326 duration = time_seconds_to_durationstr(mpd_stats_get_uptime(mpd_stats));
327 screen_song_append(labels[UPTIME], duration, max_label_width);
328 g_free(duration);
329 date = g_date_new();
330 g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
331 g_date_strftime(buf, sizeof(buf), "%x", date);
332 screen_song_append(labels[DBUPTIME], buf, max_label_width);
333 g_date_free(date);
335 mpd_stats_free(mpd_stats);
336 }
337 }
339 static void
340 screen_song_update(struct mpdclient *c)
341 {
342 /* Clear all lines */
343 for (guint i = 0; i < current.lines->len; ++i)
344 g_free(g_ptr_array_index(current.lines, i));
345 g_ptr_array_set_size(current.lines, 0);
347 /* If a song was selected before the song screen was opened */
348 if (next_song != NULL) {
349 assert(current.selected_song == NULL);
350 current.selected_song = next_song;
351 next_song = NULL;
352 }
354 if (current.selected_song != NULL &&
355 (c->song == NULL ||
356 strcmp(mpd_song_get_uri(current.selected_song),
357 mpd_song_get_uri(c->song)) != 0 ||
358 c->status == NULL ||
359 (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
360 mpd_status_get_state(c->status) != MPD_STATE_PAUSE))) {
361 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
362 screen_song_add_song(current.selected_song, c);
363 g_ptr_array_add(current.lines, g_strdup("\0"));
364 }
366 if (c->song != NULL && c->status != NULL &&
367 (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
368 mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) {
369 if (current.played_song != NULL) {
370 mpd_song_free(current.played_song);
371 }
372 current.played_song = mpd_song_dup(c->song);
373 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
374 screen_song_add_song(current.played_song, c);
375 g_ptr_array_add(current.lines, g_strdup("\0"));
376 }
378 /* Add some statistics about mpd */
379 if (c->connection != NULL)
380 screen_song_add_stats(c);
382 screen_song_repaint();
383 }
385 static bool
386 screen_song_cmd(struct mpdclient *c, command_t cmd)
387 {
388 if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
389 screen_song_repaint();
390 return true;
391 }
393 switch(cmd) {
394 case CMD_LOCATE:
395 if (current.selected_song != NULL) {
396 screen_file_goto_song(c, current.selected_song);
397 return true;
398 }
399 if (current.played_song != NULL) {
400 screen_file_goto_song(c, current.played_song);
401 return true;
402 }
404 return false;
406 #ifdef ENABLE_LYRICS_SCREEN
407 case CMD_SCREEN_LYRICS:
408 if (current.selected_song != NULL) {
409 screen_lyrics_switch(c, current.selected_song, false);
410 return true;
411 }
412 if (current.played_song != NULL) {
413 screen_lyrics_switch(c, current.played_song, true);
414 return true;
415 }
416 return false;
418 #endif
420 case CMD_SCREEN_SWAP:
421 if (current.selected_song != NULL)
422 screen_swap(c, current.selected_song);
423 else
424 // No need to check if this is null - we'd pass null anyway
425 screen_swap(c, current.played_song);
426 return true;
428 default:
429 break;
430 }
432 if (screen_find(lw, current.lines->len,
433 cmd, screen_song_list_callback, NULL)) {
434 /* center the row */
435 list_window_center(lw, current.lines->len, lw->selected);
436 screen_song_repaint();
437 return true;
438 }
440 return false;
441 }
443 const struct screen_functions screen_song = {
444 .init = screen_song_init,
445 .exit = screen_song_exit,
446 .open = screen_song_update,
447 .close = screen_song_clear,
448 .resize = screen_song_resize,
449 .paint = screen_song_paint,
450 .update = screen_song_update,
451 .cmd = screen_song_cmd,
452 .get_title = screen_song_title,
453 };
455 void
456 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
457 {
458 assert(song != NULL);
459 assert(current.selected_song == NULL);
460 assert(current.played_song == NULL);
462 next_song = mpd_song_dup(song);
463 screen_switch(&screen_song, c);
464 }