Code

list_window: don't invoke callback out-of-range
[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 bool *highlight,
80                           G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
81 {
82         static char buffer[256];
83         char *value;
85         assert(idx < current.lines->len);
87         value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
88         g_strlcpy(buffer, value, sizeof(buffer));
89         g_free(value);
91         return buffer;
92 }
95 static void
96 screen_song_init(WINDOW *w, int cols, int rows)
97 {
98         /* We will need at least 10 lines, so this saves 10 reallocations :) */
99         current.lines = g_ptr_array_sized_new(10);
100         lw = list_window_init(w, cols, rows);
101         lw->hide_cursor = true;
104 static void
105 screen_song_exit(void)
107         list_window_free(lw);
109         screen_song_clear();
111         g_ptr_array_free(current.lines, TRUE);
112         current.lines = NULL;
115 static void
116 screen_song_resize(int cols, int rows)
118         lw->cols = cols;
119         lw->rows = rows;
122 static const char *
123 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
125         return _("Song viewer");
128 static void
129 screen_song_paint(void)
131         list_window_paint(lw, screen_song_list_callback, NULL);
134 /* Appends a line with a fixed width for the label column
135  * Handles NULL strings gracefully */
136 static void
137 screen_song_append(const char *label, const char *value, unsigned label_col)
139         int value_col, linebreaks, entry_size, label_size;
140         int i, k;
141         gchar *entry, *entry_iter;
142         const gchar *value_iter;
144         assert(label != NULL);
145         assert(g_utf8_validate(label, -1, NULL));
147         if (value != NULL) {
148                 assert(g_utf8_validate(value, -1, NULL));
149                 /* +2 for ': ' */
150                 label_col += 2;
151                 value_col = lw->cols - label_col;
152                 /* calculate the number of required linebreaks */
153                 linebreaks = (utf8_width(value) - 1) / value_col + 1;
154                 value_iter = value;
155                 label_size = strlen(label) + label_col - utf8_width(label);
156                 entry_size = label_size + strlen(value) + 2;
158                 for (i = 0; i < linebreaks; ++i)
159                 {
160                         entry = g_malloc(entry_size);
161                         if (i == 0) {
162                                 entry_iter = entry + g_sprintf(entry, "%s: ", label);
163                                 /* fill the label column with whitespaces */
164                                 for ( ; entry_iter < entry + label_size; ++entry_iter)
165                                         *entry_iter = ' ';
166                         }
167                         else {
168                                 entry_iter = entry;
169                                 /* fill the label column with whitespaces */
170                                 for ( ; entry_iter < entry + label_col; ++entry_iter)
171                                         *entry_iter = ' ';
172                         }
173                         /* skip whitespaces */
174                         while (g_ascii_isspace(*value_iter)) ++value_iter;
175                         k = 0;
176                         while (value_iter && k < value_col)
177                         {
178                                 g_utf8_strncpy(entry_iter, value_iter, 1);
179                                 value_iter = g_utf8_find_next_char(value_iter, NULL);
180                                 entry_iter = g_utf8_find_next_char(entry_iter, NULL);
181                                 ++k;
182                         }
183                         *entry_iter = '\0';
184                         g_ptr_array_add(current.lines, entry);
185                 }
186         }
189 static void
190 screen_song_append_tag(const char *label, const struct mpd_song *song,
191                        enum mpd_tag_type tag, unsigned label_col)
193         unsigned i = 0;
194         const char *value;
196         while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
197                 screen_song_append(label, value, label_col);
200 static void
201 screen_song_add_song(const struct mpd_song *song, const struct mpdclient *c)
203         unsigned i, max_label_width;
204         enum label {
205                 ARTIST, TITLE, ALBUM, LENGTH, COMPOSER, NAME, DISC, TRACK,
206                 DATE, GENRE, COMMENT, BITRATE
207         };
208         const char *labels[] = { [ARTIST] = _("Artist"),
209                 [TITLE] = _("Title"),
210                 [ALBUM] = _("Album"),
211                 [LENGTH] = _("Length"),
212                 [COMPOSER] = _("Composer"),
213                 [NAME] = _("Name"),
214                 [DISC] = _("Disc"),
215                 [TRACK] = _("Track"),
216                 [DATE] = _("Date"),
217                 [GENRE] = _("Genre"),
218                 [COMMENT] = _("Comment"),
219                 [BITRATE] = _("Bitrate"),
220         };
221         /* Determine the width of the longest label */
222         max_label_width = utf8_width(labels[0]);
223         for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
224                 if (utf8_width(labels[i]) > max_label_width)
225                         max_label_width = utf8_width(labels[i]);
226         }
228         assert(song != NULL);
230         screen_song_append_tag(labels[ARTIST], song, MPD_TAG_ARTIST,
231                                max_label_width);
232         screen_song_append_tag(labels[TITLE], song, MPD_TAG_TITLE,
233                                max_label_width);
234         screen_song_append_tag(labels[ALBUM], song, MPD_TAG_ALBUM,
235                                max_label_width);
236         /* create time string and add it */
237         if (mpd_song_get_duration(song) > 0) {
238                 char length[16];
239                 format_duration_short(length, sizeof(length),
240                                       mpd_song_get_duration(song));
241                 screen_song_append(labels[LENGTH], length, max_label_width);
242         }
243         screen_song_append_tag(labels[COMPOSER], song, MPD_TAG_COMPOSER,
244                                max_label_width);
245         screen_song_append_tag(labels[NAME], song, MPD_TAG_NAME,
246                                max_label_width);
247         screen_song_append_tag(labels[DISC], song, MPD_TAG_DISC,
248                                max_label_width);
249         screen_song_append_tag(labels[TRACK], song, MPD_TAG_TRACK,
250                                max_label_width);
251         screen_song_append_tag(labels[DATE], song, MPD_TAG_DATE,
252                                max_label_width);
253         screen_song_append_tag(labels[GENRE], song, MPD_TAG_GENRE,
254                                max_label_width);
255         screen_song_append_tag(labels[COMMENT], song, MPD_TAG_COMMENT,
256                                max_label_width);
257         screen_song_append(_("Path"), mpd_song_get_uri(song), max_label_width);
258         if (c->status != NULL && c->song != NULL &&
259             strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
260             (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
261              mpd_status_get_state(c->status) == MPD_STATE_PAUSE)) {
262                 char buf[16];
263                 g_snprintf(buf, sizeof(buf), _("%d kbps"),
264                            mpd_status_get_kbit_rate(c->status));
265                 screen_song_append(labels[BITRATE], buf, max_label_width);
266         }
269 static bool
270 screen_song_add_stats(struct mpd_connection *connection)
272         unsigned i, max_label_width;
273         char buf[64];
274         GDate *date;
275         enum label {
276                 ARTISTS, ALBUMS, SONGS, UPTIME,
277                 DBUPTIME, PLAYTIME, DBPLAYTIME
278         };
279         const char *labels[] = { [ARTISTS] = _("Number of artists"),
280                 [ALBUMS] = _("Number of albums"),
281                 [SONGS] = _("Number of songs"),
282                 [UPTIME] = _("Uptime"),
283                 [DBUPTIME] =_("Most recent db update"),
284                 [PLAYTIME] = _("Playtime"),
285                 [DBPLAYTIME] = _("DB playtime")
286         };
287         struct mpd_stats *mpd_stats;
289         mpd_stats = mpd_run_stats(connection);
290         if (mpd_stats == NULL)
291                 return false;
293         /* Determine the width of the longest label */
294         max_label_width = utf8_width(labels[0]);
295         for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
296                 if (utf8_width(labels[i]) > max_label_width)
297                         max_label_width = utf8_width(labels[i]);
298         }
300         g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
301         g_snprintf(buf, sizeof(buf), "%d",
302                    mpd_stats_get_number_of_artists(mpd_stats));
303         screen_song_append(labels[ARTISTS], buf, max_label_width);
304         g_snprintf(buf, sizeof(buf), "%d",
305                    mpd_stats_get_number_of_albums(mpd_stats));
306         screen_song_append(labels[ALBUMS], buf, max_label_width);
307         g_snprintf(buf, sizeof(buf), "%d",
308                    mpd_stats_get_number_of_songs(mpd_stats));
309         screen_song_append(labels[SONGS], buf, max_label_width);
311         format_duration_long(buf, sizeof(buf),
312                              mpd_stats_get_db_play_time(mpd_stats));
313         screen_song_append(labels[DBPLAYTIME], buf, max_label_width);
315         format_duration_long(buf, sizeof(buf),
316                              mpd_stats_get_play_time(mpd_stats));
317         screen_song_append(labels[PLAYTIME], buf, max_label_width);
319         format_duration_long(buf, sizeof(buf),
320                              mpd_stats_get_uptime(mpd_stats));
321         screen_song_append(labels[UPTIME], buf, max_label_width);
323         date = g_date_new();
324         g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
325         g_date_strftime(buf, sizeof(buf), "%x", date);
326         screen_song_append(labels[DBUPTIME], buf, max_label_width);
327         g_date_free(date);
329         mpd_stats_free(mpd_stats);
330         return true;
333 static void
334 screen_song_update(struct mpdclient *c)
336         /* Clear all lines */
337         for (guint i = 0; i < current.lines->len; ++i)
338                 g_free(g_ptr_array_index(current.lines, i));
339         g_ptr_array_set_size(current.lines, 0);
341         /* If a song was selected before the song screen was opened */
342         if (next_song != NULL) {
343                 assert(current.selected_song == NULL);
344                 current.selected_song = next_song;
345                 next_song = NULL;
346         }
348         if (current.selected_song != NULL &&
349                         (c->song == NULL ||
350                          strcmp(mpd_song_get_uri(current.selected_song),
351                                 mpd_song_get_uri(c->song)) != 0 ||
352                          c->status == NULL ||
353                          (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
354                           mpd_status_get_state(c->status) != MPD_STATE_PAUSE))) {
355                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
356                 screen_song_add_song(current.selected_song, c);
357                 g_ptr_array_add(current.lines, g_strdup("\0"));
358         }
360         if (c->song != NULL && c->status != NULL &&
361             (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
362              mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) {
363                 if (current.played_song != NULL) {
364                         mpd_song_free(current.played_song);
365                 }
366                 current.played_song = mpd_song_dup(c->song);
367                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
368                 screen_song_add_song(current.played_song, c);
369                 g_ptr_array_add(current.lines, g_strdup("\0"));
370         }
372         /* Add some statistics about mpd */
373         if (mpdclient_is_connected(c) &&
374             !screen_song_add_stats(mpdclient_get_connection(c)))
375                 mpdclient_handle_error(c);
377         list_window_set_length(lw, 0);
378         screen_song_repaint();
381 static bool
382 screen_song_cmd(struct mpdclient *c, command_t cmd)
384         if (list_window_scroll_cmd(lw, cmd)) {
385                 screen_song_repaint();
386                 return true;
387         }
389         switch(cmd) {
390         case CMD_LOCATE:
391                 if (current.selected_song != NULL) {
392                         screen_file_goto_song(c, current.selected_song);
393                         return true;
394                 }
395                 if (current.played_song != NULL) {
396                         screen_file_goto_song(c, current.played_song);
397                         return true;
398                 }
400                 return false;
402 #ifdef ENABLE_LYRICS_SCREEN
403         case CMD_SCREEN_LYRICS:
404                 if (current.selected_song != NULL) {
405                         screen_lyrics_switch(c, current.selected_song, false);
406                         return true;
407                 }
408                 if (current.played_song != NULL) {
409                         screen_lyrics_switch(c, current.played_song, true);
410                         return true;
411                 }
412                 return false;
414 #endif
416         case CMD_SCREEN_SWAP:
417                 if (current.selected_song != NULL)
418                         screen_swap(c, current.selected_song);
419                 else
420                 // No need to check if this is null - we'd pass null anyway
421                         screen_swap(c, current.played_song);
422                 return true;
424         default:
425                 break;
426         }
428         if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
429                 /* center the row */
430                 list_window_center(lw, lw->selected);
431                 screen_song_repaint();
432                 return true;
433         }
435         return false;
438 const struct screen_functions screen_song = {
439         .init = screen_song_init,
440         .exit = screen_song_exit,
441         .open = screen_song_update,
442         .close = screen_song_clear,
443         .resize = screen_song_resize,
444         .paint = screen_song_paint,
445         .update = screen_song_update,
446         .cmd = screen_song_cmd,
447         .get_title = screen_song_title,
448 };
450 void
451 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
453         assert(song != NULL);
454         assert(current.selected_song == NULL);
455         assert(current.played_song == NULL);
457         next_song = mpd_song_dup(song);
458         screen_switch(&screen_song, c);