Code

c83cd45b3a247b24cafc21b527297681437642a7
[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_file.h"
21 #include "i18n.h"
22 #include "screen.h"
23 #include "screen_utils.h"
24 #include "charset.h"
25 #include "utils.h"
26 #include "mpdclient.h"
28 #include <mpd/client.h>
30 #include <glib/gprintf.h>
31 #include <assert.h>
32 #include <string.h>
34 static list_window_t *lw;
36 static struct mpd_song *next_song;
38 static struct {
39         struct mpd_song *selected_song;
40         struct mpd_song *played_song;
41         GPtrArray *lines;
42 } current;
44 static void
45 screen_song_clear(void)
46 {
47         for (guint i = 0; i < current.lines->len; ++i)
48                 g_free(g_ptr_array_index(current.lines, i));
50         g_ptr_array_set_size(current.lines, 0);
52         if (current.selected_song != NULL) {
53                 mpd_song_free(current.selected_song);
54                 current.selected_song = NULL;
55         }
56         if (current.played_song != NULL) {
57                 mpd_song_free(current.played_song);
58                 current.played_song = NULL;
59         }
60 }
62 static void
63 screen_song_paint(void);
65 /**
66  * Repaint and update the screen.
67  */
68 static void
69 screen_song_repaint(void)
70 {
71         screen_song_paint();
72         wrefresh(lw->w);
73 }
75 static const char *
76 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
77                           G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
78 {
79         static char buffer[256];
80         char *value;
82         if (idx >= current.lines->len)
83                 return NULL;
85         value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
86         g_strlcpy(buffer, value, sizeof(buffer));
87         g_free(value);
89         return buffer;
90 }
93 static void
94 screen_song_init(WINDOW *w, int cols, int rows)
95 {
96         /* We will need at least 10 lines, so this saves 10 reallocations :) */
97         current.lines = g_ptr_array_sized_new(10);
98         lw = list_window_init(w, cols, rows);
99         lw->hide_cursor = true;
102 static void
103 screen_song_exit(void)
105         list_window_free(lw);
107         screen_song_clear();
109         g_ptr_array_free(current.lines, TRUE);
110         current.lines = NULL;
113 static void
114 screen_song_resize(int cols, int rows)
116         lw->cols = cols;
117         lw->rows = rows;
120 static const char *
121 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
123         return _("Song viewer");
126 static void
127 screen_song_paint(void)
129         list_window_paint(lw, screen_song_list_callback, NULL);
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)
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         }
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)
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);
198 static void
199 screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
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                 unsigned t = mpd_song_get_duration(song);
237                 char length[16];
239                 /*write out the time, using hours if time over 60 minutes*/
240                 if (t > 3600) {
241                         g_snprintf(length, sizeof(length),
242                                         "%i:%02i:%02i",
243                                         t/3600, (t%3600)/60, t%60);
244                 } else {
245                         g_snprintf(length, sizeof(length),
246                                         "%i:%02i", t/60, t%60);
247                 }
248                 screen_song_append(labels[LENGTH], length, max_label_width);
249         }
250         screen_song_append_tag(labels[COMPOSER], song, MPD_TAG_COMPOSER,
251                                max_label_width);
252         screen_song_append_tag(labels[NAME], song, MPD_TAG_NAME,
253                                max_label_width);
254         screen_song_append_tag(labels[DISC], song, MPD_TAG_DISC,
255                                max_label_width);
256         screen_song_append_tag(labels[TRACK], song, MPD_TAG_TRACK,
257                                max_label_width);
258         screen_song_append_tag(labels[DATE], song, MPD_TAG_DATE,
259                                max_label_width);
260         screen_song_append_tag(labels[GENRE], song, MPD_TAG_GENRE,
261                                max_label_width);
262         screen_song_append_tag(labels[COMMENT], song, MPD_TAG_COMMENT,
263                                max_label_width);
264         screen_song_append(_("Path"), mpd_song_get_uri(song), max_label_width);
265         if (c->status != NULL && c->song != NULL &&
266             strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
267             (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
268              mpd_status_get_state(c->status) == MPD_STATE_PAUSE)) {
269                 char buf[16];
270                 g_snprintf(buf, sizeof(buf), _("%d kbps"),
271                            mpd_status_get_kbit_rate(c->status));
272                 screen_song_append(labels[BITRATE], buf, max_label_width);
273         }
276 static void
277 screen_song_add_stats(const struct mpdclient *c)
279         unsigned i, max_label_width;
280         char buf[64];
281         char *duration;
282         GDate *date;
283         enum label {
284                 ARTISTS, ALBUMS, SONGS, UPTIME,
285                 DBUPTIME, PLAYTIME, DBPLAYTIME
286         };
287         const char *labels[] = { [ARTISTS] = _("Number of artists"),
288                 [ALBUMS] = _("Number of albums"),
289                 [SONGS] = _("Number of songs"),
290                 [UPTIME] = _("Uptime"),
291                 [DBUPTIME] =_("Most recent db update"),
292                 [PLAYTIME] = _("Playtime"),
293                 [DBPLAYTIME] = _("DB playtime")
294         };
295         struct mpd_stats *mpd_stats = NULL;
297         if (c->connection != NULL) {
298                 mpd_stats = mpd_run_stats(c->connection);
299         }
301         if (mpd_stats != NULL) {
302                 /* Determine the width of the longest label */
303                 max_label_width = utf8_width(labels[0]);
304                 for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
305                         if (utf8_width(labels[i]) > max_label_width)
306                                 max_label_width = utf8_width(labels[i]);
307                 }
309                 g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
310                 g_snprintf(buf, sizeof(buf), "%d",
311                            mpd_stats_get_number_of_artists(mpd_stats));
312                 screen_song_append(labels[ARTISTS], buf, max_label_width);
313                 g_snprintf(buf, sizeof(buf), "%d",
314                            mpd_stats_get_number_of_albums(mpd_stats));
315                 screen_song_append(labels[ALBUMS], buf, max_label_width);
316                 g_snprintf(buf, sizeof(buf), "%d",
317                            mpd_stats_get_number_of_songs(mpd_stats));
318                 screen_song_append(labels[SONGS], buf, max_label_width);
319                 duration = time_seconds_to_durationstr(mpd_stats_get_db_play_time(mpd_stats));
320                 screen_song_append(labels[DBPLAYTIME], duration, max_label_width);
321                 g_free(duration);
322                 duration = time_seconds_to_durationstr(mpd_stats_get_play_time(mpd_stats));
323                 screen_song_append(labels[PLAYTIME], duration, max_label_width);
324                 g_free(duration);
325                 duration = time_seconds_to_durationstr(mpd_stats_get_uptime(mpd_stats));
326                 screen_song_append(labels[UPTIME], duration, max_label_width);
327                 g_free(duration);
328                 date = g_date_new();
329                 g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
330                 g_date_strftime(buf, sizeof(buf), "%x", date);
331                 screen_song_append(labels[DBUPTIME], buf, max_label_width);
332                 g_date_free(date);
334                 mpd_stats_free(mpd_stats);
335         }
338 static void
339 screen_song_update(struct mpdclient *c)
341         /* Clear all lines */
342         for (guint i = 0; i < current.lines->len; ++i)
343                 g_free(g_ptr_array_index(current.lines, i));
344         g_ptr_array_set_size(current.lines, 0);
346         /* If a song was selected before the song screen was opened */
347         if (next_song != NULL) {
348                 assert(current.selected_song == NULL);
349                 current.selected_song = next_song;
350                 next_song = NULL;
351         }
353         if (current.selected_song != NULL &&
354                         (c->song == NULL ||
355                          strcmp(mpd_song_get_uri(current.selected_song),
356                                 mpd_song_get_uri(c->song)) != 0 ||
357                          c->status == NULL ||
358                          (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
359                           mpd_status_get_state(c->status) != MPD_STATE_PAUSE))) {
360                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
361                 screen_song_add_song(current.selected_song, c);
362                 g_ptr_array_add(current.lines, g_strdup("\0"));
363         }
365         if (c->song != NULL && c->status != NULL &&
366             (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
367              mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) {
368                 if (current.played_song != NULL) {
369                         mpd_song_free(current.played_song);
370                 }
371                 current.played_song = mpd_song_dup(c->song);
372                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
373                 screen_song_add_song(current.played_song, c);
374                 g_ptr_array_add(current.lines, g_strdup("\0"));
375         }
377         /* Add some statistics about mpd */
378         if (c->connection != NULL)
379                 screen_song_add_stats(c);
381         screen_song_repaint();
384 static bool
385 screen_song_cmd(struct mpdclient *c, command_t cmd)
387         if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
388                 screen_song_repaint();
389                 return true;
390         }
392         switch(cmd) {
393         case CMD_LOCATE:
394                 if (current.selected_song != NULL) {
395                         screen_file_goto_song(c, current.selected_song);
396                         return true;
397                 }
398                 if (current.played_song != NULL) {
399                         screen_file_goto_song(c, current.played_song);
400                         return true;
401                 }
403                 return false;
405 #ifdef ENABLE_LYRICS_SCREEN
406         case CMD_SCREEN_LYRICS:
407                 if (current.selected_song != NULL) {
408                         screen_lyrics_switch(c, current.selected_song, false);
409                         return true;
410                 }
411                 if (current.played_song != NULL) {
412                         screen_lyrics_switch(c, current.played_song, true);
413                         return true;
414                 }
415                 return false;
417 #endif
419         case CMD_SCREEN_SWAP:
420                 if (current.selected_song != NULL)
421                         screen_swap(c, current.selected_song);
422                 else
423                 // No need to check if this is null - we'd pass null anyway
424                         screen_swap(c, current.played_song);
425                 return true;
427         default:
428                 break;
429         }
431         if (screen_find(lw, current.lines->len,
432                         cmd, screen_song_list_callback, NULL)) {
433                 /* center the row */
434                 list_window_center(lw, current.lines->len, lw->selected);
435                 screen_song_repaint();
436                 return true;
437         }
439         return false;
442 const struct screen_functions screen_song = {
443         .init = screen_song_init,
444         .exit = screen_song_exit,
445         .open = screen_song_update,
446         .close = screen_song_clear,
447         .resize = screen_song_resize,
448         .paint = screen_song_paint,
449         .update = screen_song_update,
450         .cmd = screen_song_cmd,
451         .get_title = screen_song_title,
452 };
454 void
455 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
457         assert(song != NULL);
458         assert(current.selected_song == NULL);
459         assert(current.played_song == NULL);
461         next_song = mpd_song_dup(song);
462         screen_switch(&screen_song, c);