Code

mpdclient: wrap access in mpdclient_get_connection()
[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                 char length[16];
240                 format_duration_short(length, sizeof(length),
241                                       mpd_song_get_duration(song));
242                 screen_song_append(labels[LENGTH], length, max_label_width);
243         }
244         screen_song_append_tag(labels[COMPOSER], song, MPD_TAG_COMPOSER,
245                                max_label_width);
246         screen_song_append_tag(labels[NAME], song, MPD_TAG_NAME,
247                                max_label_width);
248         screen_song_append_tag(labels[DISC], song, MPD_TAG_DISC,
249                                max_label_width);
250         screen_song_append_tag(labels[TRACK], song, MPD_TAG_TRACK,
251                                max_label_width);
252         screen_song_append_tag(labels[DATE], song, MPD_TAG_DATE,
253                                max_label_width);
254         screen_song_append_tag(labels[GENRE], song, MPD_TAG_GENRE,
255                                max_label_width);
256         screen_song_append_tag(labels[COMMENT], song, MPD_TAG_COMMENT,
257                                max_label_width);
258         screen_song_append(_("Path"), mpd_song_get_uri(song), max_label_width);
259         if (c->status != NULL && c->song != NULL &&
260             strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
261             (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
262              mpd_status_get_state(c->status) == MPD_STATE_PAUSE)) {
263                 char buf[16];
264                 g_snprintf(buf, sizeof(buf), _("%d kbps"),
265                            mpd_status_get_kbit_rate(c->status));
266                 screen_song_append(labels[BITRATE], buf, max_label_width);
267         }
270 static bool
271 screen_song_add_stats(struct mpd_connection *connection)
273         unsigned i, max_label_width;
274         char buf[64];
275         GDate *date;
276         enum label {
277                 ARTISTS, ALBUMS, SONGS, UPTIME,
278                 DBUPTIME, PLAYTIME, DBPLAYTIME
279         };
280         const char *labels[] = { [ARTISTS] = _("Number of artists"),
281                 [ALBUMS] = _("Number of albums"),
282                 [SONGS] = _("Number of songs"),
283                 [UPTIME] = _("Uptime"),
284                 [DBUPTIME] =_("Most recent db update"),
285                 [PLAYTIME] = _("Playtime"),
286                 [DBPLAYTIME] = _("DB playtime")
287         };
288         struct mpd_stats *mpd_stats;
290         mpd_stats = mpd_run_stats(connection);
291         if (mpd_stats == NULL)
292                 return false;
294         /* Determine the width of the longest label */
295         max_label_width = utf8_width(labels[0]);
296         for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
297                 if (utf8_width(labels[i]) > max_label_width)
298                         max_label_width = utf8_width(labels[i]);
299         }
301         g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
302         g_snprintf(buf, sizeof(buf), "%d",
303                    mpd_stats_get_number_of_artists(mpd_stats));
304         screen_song_append(labels[ARTISTS], buf, max_label_width);
305         g_snprintf(buf, sizeof(buf), "%d",
306                    mpd_stats_get_number_of_albums(mpd_stats));
307         screen_song_append(labels[ALBUMS], buf, max_label_width);
308         g_snprintf(buf, sizeof(buf), "%d",
309                    mpd_stats_get_number_of_songs(mpd_stats));
310         screen_song_append(labels[SONGS], buf, max_label_width);
312         format_duration_long(buf, sizeof(buf),
313                              mpd_stats_get_db_play_time(mpd_stats));
314         screen_song_append(labels[DBPLAYTIME], buf, max_label_width);
316         format_duration_long(buf, sizeof(buf),
317                              mpd_stats_get_play_time(mpd_stats));
318         screen_song_append(labels[PLAYTIME], buf, max_label_width);
320         format_duration_long(buf, sizeof(buf),
321                              mpd_stats_get_uptime(mpd_stats));
322         screen_song_append(labels[UPTIME], buf, max_label_width);
324         date = g_date_new();
325         g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
326         g_date_strftime(buf, sizeof(buf), "%x", date);
327         screen_song_append(labels[DBUPTIME], buf, max_label_width);
328         g_date_free(date);
330         mpd_stats_free(mpd_stats);
331         return true;
334 static void
335 screen_song_update(struct mpdclient *c)
337         /* Clear all lines */
338         for (guint i = 0; i < current.lines->len; ++i)
339                 g_free(g_ptr_array_index(current.lines, i));
340         g_ptr_array_set_size(current.lines, 0);
342         /* If a song was selected before the song screen was opened */
343         if (next_song != NULL) {
344                 assert(current.selected_song == NULL);
345                 current.selected_song = next_song;
346                 next_song = NULL;
347         }
349         if (current.selected_song != NULL &&
350                         (c->song == NULL ||
351                          strcmp(mpd_song_get_uri(current.selected_song),
352                                 mpd_song_get_uri(c->song)) != 0 ||
353                          c->status == NULL ||
354                          (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
355                           mpd_status_get_state(c->status) != MPD_STATE_PAUSE))) {
356                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
357                 screen_song_add_song(current.selected_song, c);
358                 g_ptr_array_add(current.lines, g_strdup("\0"));
359         }
361         if (c->song != NULL && c->status != NULL &&
362             (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
363              mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) {
364                 if (current.played_song != NULL) {
365                         mpd_song_free(current.played_song);
366                 }
367                 current.played_song = mpd_song_dup(c->song);
368                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
369                 screen_song_add_song(current.played_song, c);
370                 g_ptr_array_add(current.lines, g_strdup("\0"));
371         }
373         /* Add some statistics about mpd */
374         if (mpdclient_is_connected(c) &&
375             !screen_song_add_stats(mpdclient_get_connection(c)))
376                 mpdclient_handle_error(c);
378         screen_song_repaint();
381 static bool
382 screen_song_cmd(struct mpdclient *c, command_t cmd)
384         if (list_window_scroll_cmd(lw, current.lines->len, 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, current.lines->len,
429                         cmd, screen_song_list_callback, NULL)) {
430                 /* center the row */
431                 list_window_center(lw, current.lines->len, lw->selected);
432                 screen_song_repaint();
433                 return true;
434         }
436         return false;
439 const struct screen_functions screen_song = {
440         .init = screen_song_init,
441         .exit = screen_song_exit,
442         .open = screen_song_update,
443         .close = screen_song_clear,
444         .resize = screen_song_resize,
445         .paint = screen_song_paint,
446         .update = screen_song_update,
447         .cmd = screen_song_cmd,
448         .get_title = screen_song_title,
449 };
451 void
452 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
454         assert(song != NULL);
455         assert(current.selected_song == NULL);
456         assert(current.played_song == NULL);
458         next_song = mpd_song_dup(song);
459         screen_switch(&screen_song, c);