Code

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