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