Code

e4fb21c09d5fb23c0b00c87fc43a1c15dc3749c1
[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 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         static char buffer[256];
82         char *value;
84         assert(idx < current.lines->len);
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;
103 static void
104 screen_song_exit(void)
106         list_window_free(lw);
108         screen_song_clear();
110         g_ptr_array_free(current.lines, TRUE);
111         current.lines = NULL;
114 static void
115 screen_song_resize(int cols, int rows)
117         list_window_resize(lw, cols, 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                 char length[16];
237                 format_duration_short(length, sizeof(length),
238                                       mpd_song_get_duration(song));
239                 screen_song_append(labels[LENGTH], length, max_label_width);
240         }
241         screen_song_append_tag(labels[COMPOSER], song, MPD_TAG_COMPOSER,
242                                max_label_width);
243         screen_song_append_tag(labels[NAME], song, MPD_TAG_NAME,
244                                max_label_width);
245         screen_song_append_tag(labels[DISC], song, MPD_TAG_DISC,
246                                max_label_width);
247         screen_song_append_tag(labels[TRACK], song, MPD_TAG_TRACK,
248                                max_label_width);
249         screen_song_append_tag(labels[DATE], song, MPD_TAG_DATE,
250                                max_label_width);
251         screen_song_append_tag(labels[GENRE], song, MPD_TAG_GENRE,
252                                max_label_width);
253         screen_song_append_tag(labels[COMMENT], song, MPD_TAG_COMMENT,
254                                max_label_width);
255         screen_song_append(_("Path"), mpd_song_get_uri(song), max_label_width);
256         if (mpdclient_is_playing(c) && c->song != NULL &&
257             strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0) {
258                 char buf[16];
259                 g_snprintf(buf, sizeof(buf), _("%d kbps"),
260                            mpd_status_get_kbit_rate(c->status));
261                 screen_song_append(labels[BITRATE], buf, max_label_width);
262         }
265 static bool
266 screen_song_add_stats(struct mpd_connection *connection)
268         unsigned i, max_label_width;
269         char buf[64];
270         GDate *date;
271         enum label {
272                 ARTISTS, ALBUMS, SONGS, UPTIME,
273                 DBUPTIME, PLAYTIME, DBPLAYTIME
274         };
275         const char *labels[] = { [ARTISTS] = _("Number of artists"),
276                 [ALBUMS] = _("Number of albums"),
277                 [SONGS] = _("Number of songs"),
278                 [UPTIME] = _("Uptime"),
279                 [DBUPTIME] =_("Most recent db update"),
280                 [PLAYTIME] = _("Playtime"),
281                 [DBPLAYTIME] = _("DB playtime")
282         };
283         struct mpd_stats *mpd_stats;
285         mpd_stats = mpd_run_stats(connection);
286         if (mpd_stats == NULL)
287                 return false;
289         /* Determine the width of the longest label */
290         max_label_width = utf8_width(labels[0]);
291         for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
292                 if (utf8_width(labels[i]) > max_label_width)
293                         max_label_width = utf8_width(labels[i]);
294         }
296         g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
297         g_snprintf(buf, sizeof(buf), "%d",
298                    mpd_stats_get_number_of_artists(mpd_stats));
299         screen_song_append(labels[ARTISTS], buf, max_label_width);
300         g_snprintf(buf, sizeof(buf), "%d",
301                    mpd_stats_get_number_of_albums(mpd_stats));
302         screen_song_append(labels[ALBUMS], buf, max_label_width);
303         g_snprintf(buf, sizeof(buf), "%d",
304                    mpd_stats_get_number_of_songs(mpd_stats));
305         screen_song_append(labels[SONGS], buf, max_label_width);
307         format_duration_long(buf, sizeof(buf),
308                              mpd_stats_get_db_play_time(mpd_stats));
309         screen_song_append(labels[DBPLAYTIME], buf, max_label_width);
311         format_duration_long(buf, sizeof(buf),
312                              mpd_stats_get_play_time(mpd_stats));
313         screen_song_append(labels[PLAYTIME], buf, max_label_width);
315         format_duration_long(buf, sizeof(buf),
316                              mpd_stats_get_uptime(mpd_stats));
317         screen_song_append(labels[UPTIME], buf, max_label_width);
319         date = g_date_new();
320         g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
321         g_date_strftime(buf, sizeof(buf), "%x", date);
322         screen_song_append(labels[DBUPTIME], buf, max_label_width);
323         g_date_free(date);
325         mpd_stats_free(mpd_stats);
326         return true;
329 static void
330 screen_song_update(struct mpdclient *c)
332         struct mpd_connection *connection;
334         /* Clear all lines */
335         for (guint i = 0; i < current.lines->len; ++i)
336                 g_free(g_ptr_array_index(current.lines, i));
337         g_ptr_array_set_size(current.lines, 0);
339         /* If a song was selected before the song screen was opened */
340         if (next_song != NULL) {
341                 assert(current.selected_song == NULL);
342                 current.selected_song = next_song;
343                 next_song = NULL;
344         }
346         if (current.selected_song != NULL &&
347                         (c->song == NULL ||
348                          strcmp(mpd_song_get_uri(current.selected_song),
349                                 mpd_song_get_uri(c->song)) != 0 ||
350                          !mpdclient_is_playing(c))) {
351                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
352                 screen_song_add_song(current.selected_song, c);
353                 g_ptr_array_add(current.lines, g_strdup("\0"));
354         }
356         if (c->song != NULL && mpdclient_is_playing(c)) {
357                 if (current.played_song != NULL) {
358                         mpd_song_free(current.played_song);
359                 }
360                 current.played_song = mpd_song_dup(c->song);
361                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
362                 screen_song_add_song(current.played_song, c);
363                 g_ptr_array_add(current.lines, g_strdup("\0"));
364         }
366         /* Add some statistics about mpd */
367         connection = mpdclient_get_connection(c);
368         if (connection != NULL && !screen_song_add_stats(connection))
369                 mpdclient_handle_error(c);
371         list_window_set_length(lw, current.lines->len);
372         screen_song_repaint();
375 static bool
376 screen_song_cmd(struct mpdclient *c, command_t cmd)
378         if (list_window_scroll_cmd(lw, cmd)) {
379                 screen_song_repaint();
380                 return true;
381         }
383         switch(cmd) {
384         case CMD_LOCATE:
385                 if (current.selected_song != NULL) {
386                         screen_file_goto_song(c, current.selected_song);
387                         return true;
388                 }
389                 if (current.played_song != NULL) {
390                         screen_file_goto_song(c, current.played_song);
391                         return true;
392                 }
394                 return false;
396 #ifdef ENABLE_LYRICS_SCREEN
397         case CMD_SCREEN_LYRICS:
398                 if (current.selected_song != NULL) {
399                         screen_lyrics_switch(c, current.selected_song, false);
400                         return true;
401                 }
402                 if (current.played_song != NULL) {
403                         screen_lyrics_switch(c, current.played_song, true);
404                         return true;
405                 }
406                 return false;
408 #endif
410         case CMD_SCREEN_SWAP:
411                 if (current.selected_song != NULL)
412                         screen_swap(c, current.selected_song);
413                 else
414                 // No need to check if this is null - we'd pass null anyway
415                         screen_swap(c, current.played_song);
416                 return true;
418         default:
419                 break;
420         }
422         if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
423                 /* center the row */
424                 list_window_center(lw, lw->selected);
425                 screen_song_repaint();
426                 return true;
427         }
429         return false;
432 const struct screen_functions screen_song = {
433         .init = screen_song_init,
434         .exit = screen_song_exit,
435         .open = screen_song_update,
436         .close = screen_song_clear,
437         .resize = screen_song_resize,
438         .paint = screen_song_paint,
439         .update = screen_song_update,
440         .cmd = screen_song_cmd,
441         .get_title = screen_song_title,
442 };
444 void
445 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
447         assert(song != NULL);
448         assert(current.selected_song == NULL);
449         assert(current.played_song == NULL);
451         next_song = mpd_song_dup(song);
452         screen_switch(&screen_song, c);