Code

use libmpdclient2
[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 "i18n.h"
21 #include "screen.h"
22 #include "screen_utils.h"
23 #include "charset.h"
24 #include "utils.h"
26 #include <mpd/client.h>
28 #include <glib/gprintf.h>
29 #include <string.h>
31 static list_window_t *lw;
33 static struct mpd_song *next_song;
35 static struct {
36         struct mpd_song *selected_song;
37         struct mpd_song *played_song;
38         GPtrArray *lines;
39 } current;
41 static void
42 screen_song_clear(void)
43 {
44         for (guint i = 0; i < current.lines->len; ++i)
45                 g_free(g_ptr_array_index(current.lines, i));
47         g_ptr_array_set_size(current.lines, 0);
49         if (current.selected_song != NULL) {
50                 mpd_song_free(current.selected_song);
51                 current.selected_song = NULL;
52         }
53         if (current.played_song != NULL) {
54                 mpd_song_free(current.played_song);
55                 current.played_song = NULL;
56         }
57 }
59 static void
60 screen_song_paint(void);
62 /**
63  * Repaint and update the screen.
64  */
65 static void
66 screen_song_repaint(void)
67 {
68         screen_song_paint();
69         wrefresh(lw->w);
70 }
72 static const char *
73 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
74                           G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
75 {
76         static char buffer[256];
77         char *value;
79         if (idx >= current.lines->len)
80                 return NULL;
82         value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
83         g_strlcpy(buffer, value, sizeof(buffer));
84         g_free(value);
86         return buffer;
87 }
90 static void
91 screen_song_init(WINDOW *w, int cols, int rows)
92 {
93         /* We will need at least 10 lines, so this saves 10 reallocations :) */
94         current.lines = g_ptr_array_sized_new(10);
95         lw = list_window_init(w, cols, rows);
96         lw->hide_cursor = true;
97 }
99 static void
100 screen_song_exit(void)
102         list_window_free(lw);
104         screen_song_clear();
106         g_ptr_array_free(current.lines, TRUE);
107         current.lines = NULL;
110 static void
111 screen_song_resize(int cols, int rows)
113         lw->cols = cols;
114         lw->rows = rows;
117 static const char *
118 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
120         return _("Song viewer");
123 static void
124 screen_song_paint(void)
126         list_window_paint(lw, screen_song_list_callback, NULL);
129 /* Appends a line with a fixed width for the label column
130  * Handles NULL strings gracefully */
131 static void
132 screen_song_append(const char *label, const char *value, unsigned label_col)
134         int value_col, linebreaks, entry_size, label_size;
135         int i, k;
136         gchar *entry, *entry_iter;
137         const gchar *value_iter;
139         assert(label != NULL);
140         assert(g_utf8_validate(label, -1, NULL));
142         if (value != NULL) {
143                 assert(g_utf8_validate(value, -1, NULL));
144                 /* +2 for ': ' */
145                 label_col += 2;
146                 value_col = lw->cols - label_col;
147                 /* calculate the number of required linebreaks */
148                 linebreaks = (utf8_width(value) - 1) / value_col + 1;
149                 value_iter = value;
150                 label_size = strlen(label) + label_col - utf8_width(label);
151                 entry_size = label_size + strlen(value) + 2;
153                 for (i = 0; i < linebreaks; ++i)
154                 {
155                         entry = g_malloc(entry_size);
156                         if (i == 0) {
157                                 entry_iter = entry + g_sprintf(entry, "%s: ", label);
158                                 /* fill the label column with whitespaces */
159                                 for ( ; entry_iter < entry + label_size; ++entry_iter)
160                                         *entry_iter = ' ';
161                         }
162                         else {
163                                 entry_iter = entry;
164                                 /* fill the label column with whitespaces */
165                                 for ( ; entry_iter < entry + label_col; ++entry_iter)
166                                         *entry_iter = ' ';
167                         }
168                         /* skip whitespaces */
169                         while (g_ascii_isspace(*value_iter)) ++value_iter;
170                         k = 0;
171                         while (value_iter && k < value_col)
172                         {
173                                 g_utf8_strncpy(entry_iter, value_iter, 1);
174                                 value_iter = g_utf8_find_next_char(value_iter, NULL);
175                                 entry_iter = g_utf8_find_next_char(entry_iter, NULL);
176                                 ++k;
177                         }
178                         *entry_iter = '\0';
179                         g_ptr_array_add(current.lines, entry);
180                 }
181         }
184 static void
185 screen_song_append_tag(const char *label, const struct mpd_song *song,
186                        enum mpd_tag_type tag, unsigned label_col)
188         unsigned i = 0;
189         const char *value;
191         while ((value = mpd_song_get_tag(song, tag, i++)) != NULL)
192                 screen_song_append(label, value, label_col);
195 static void
196 screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c)
198         unsigned i, max_label_width;
199         enum label {
200                 ARTIST, TITLE, ALBUM, LENGTH, COMPOSER, NAME, DISC, TRACK,
201                 DATE, GENRE, COMMENT, BITRATE
202         };
203         const char *labels[] = { [ARTIST] = _("Artist"),
204                 [TITLE] = _("Title"),
205                 [ALBUM] = _("Album"),
206                 [LENGTH] = _("Length"),
207                 [COMPOSER] = _("Composer"),
208                 [NAME] = _("Name"),
209                 [DISC] = _("Disc"),
210                 [TRACK] = _("Track"),
211                 [DATE] = _("Date"),
212                 [GENRE] = _("Genre"),
213                 [COMMENT] = _("Comment"),
214                 [BITRATE] = _("Bitrate"),
215         };
216         /* Determine the width of the longest label */
217         max_label_width = utf8_width(labels[0]);
218         for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
219                 if (utf8_width(labels[i]) > max_label_width)
220                         max_label_width = utf8_width(labels[i]);
221         }
223         assert(song != NULL);
225         screen_song_append_tag(labels[ARTIST], song, MPD_TAG_ARTIST,
226                                max_label_width);
227         screen_song_append_tag(labels[TITLE], song, MPD_TAG_TITLE,
228                                max_label_width);
229         screen_song_append_tag(labels[ALBUM], song, MPD_TAG_ALBUM,
230                                max_label_width);
231         /* create time string and add it */
232         if (mpd_song_get_duration(song) > 0) {
233                 unsigned t = mpd_song_get_duration(song);
234                 char length[16];
236                 /*write out the time, using hours if time over 60 minutes*/
237                 if (t > 3600) {
238                         g_snprintf(length, sizeof(length),
239                                         "%i:%02i:%02i",
240                                         t/3600, (t%3600)/60, t%60);
241                 } else {
242                         g_snprintf(length, sizeof(length),
243                                         "%i:%02i", t/60, t%60);
244                 }
245                 screen_song_append(labels[LENGTH], length, max_label_width);
246         }
247         screen_song_append_tag(labels[COMPOSER], song, MPD_TAG_COMPOSER,
248                                max_label_width);
249         screen_song_append_tag(labels[NAME], song, MPD_TAG_NAME,
250                                max_label_width);
251         screen_song_append_tag(labels[DISC], song, MPD_TAG_DISC,
252                                max_label_width);
253         screen_song_append_tag(labels[TRACK], song, MPD_TAG_TRACK,
254                                max_label_width);
255         screen_song_append_tag(labels[DATE], song, MPD_TAG_DATE,
256                                max_label_width);
257         screen_song_append_tag(labels[GENRE], song, MPD_TAG_GENRE,
258                                max_label_width);
259         screen_song_append_tag(labels[COMMENT], song, MPD_TAG_COMMENT,
260                                max_label_width);
261         screen_song_append(_("Path"), mpd_song_get_uri(song), max_label_width);
262         if (c->status != NULL && c->song != NULL &&
263             strcmp(mpd_song_get_uri(c->song), mpd_song_get_uri(song)) == 0 &&
264             (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
265              mpd_status_get_state(c->status) == MPD_STATE_PAUSE)) {
266                 char buf[16];
267                 g_snprintf(buf, sizeof(buf), _("%d kbps"),
268                            mpd_status_get_kbit_rate(c->status));
269                 screen_song_append(labels[BITRATE], buf, max_label_width);
270         }
273 static void
274 screen_song_add_stats(const mpdclient_t *c)
276         unsigned i, max_label_width;
277         char buf[64];
278         char *duration;
279         GDate *date;
280         enum label {
281                 ARTISTS, ALBUMS, SONGS, UPTIME,
282                 DBUPTIME, PLAYTIME, DBPLAYTIME
283         };
284         const char *labels[] = { [ARTISTS] = _("Number of artists"),
285                 [ALBUMS] = _("Number of albums"),
286                 [SONGS] = _("Number of songs"),
287                 [UPTIME] = _("Uptime"),
288                 [DBUPTIME] =_("Most recent db update"),
289                 [PLAYTIME] = _("Playtime"),
290                 [DBPLAYTIME] = _("DB playtime")
291         };
292         struct mpd_stats *mpd_stats = NULL;
294         if (c->connection != NULL) {
295                 mpd_stats = mpd_run_stats(c->connection);
296         }
298         if (mpd_stats != NULL) {
299                 /* Determine the width of the longest label */
300                 max_label_width = utf8_width(labels[0]);
301                 for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
302                         if (utf8_width(labels[i]) > max_label_width)
303                                 max_label_width = utf8_width(labels[i]);
304                 }
306                 g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
307                 g_snprintf(buf, sizeof(buf), "%d",
308                            mpd_stats_get_number_of_artists(mpd_stats));
309                 screen_song_append(labels[ARTISTS], buf, max_label_width);
310                 g_snprintf(buf, sizeof(buf), "%d",
311                            mpd_stats_get_number_of_albums(mpd_stats));
312                 screen_song_append(labels[ALBUMS], buf, max_label_width);
313                 g_snprintf(buf, sizeof(buf), "%d",
314                            mpd_stats_get_number_of_songs(mpd_stats));
315                 screen_song_append(labels[SONGS], buf, max_label_width);
316                 duration = time_seconds_to_durationstr(mpd_stats_get_db_play_time(mpd_stats));
317                 screen_song_append(labels[DBPLAYTIME], duration, max_label_width);
318                 g_free(duration);
319                 duration = time_seconds_to_durationstr(mpd_stats_get_play_time(mpd_stats));
320                 screen_song_append(labels[PLAYTIME], duration, max_label_width);
321                 g_free(duration);
322                 duration = time_seconds_to_durationstr(mpd_stats_get_uptime(mpd_stats));
323                 screen_song_append(labels[UPTIME], duration, max_label_width);
324                 g_free(duration);
325                 date = g_date_new();
326                 g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats));
327                 g_date_strftime(buf, sizeof(buf), "%x", date);
328                 screen_song_append(labels[DBUPTIME], buf, max_label_width);
329                 g_date_free(date);
331                 mpd_stats_free(mpd_stats);
332         }
335 static void
336 screen_song_update(mpdclient_t *c)
338         /* Clear all lines */
339         for (guint i = 0; i < current.lines->len; ++i)
340                 g_free(g_ptr_array_index(current.lines, i));
341         g_ptr_array_set_size(current.lines, 0);
343         /* If a song was selected before the song screen was opened */
344         if (next_song != NULL) {
345                 assert(current.selected_song == NULL);
346                 current.selected_song = next_song;
347                 next_song = NULL;
348         }
350         if (current.selected_song != NULL &&
351                         (c->song == NULL ||
352                          strcmp(mpd_song_get_uri(current.selected_song),
353                                 mpd_song_get_uri(c->song)) != 0 ||
354                          c->status == NULL ||
355                          (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
356                           mpd_status_get_state(c->status) != MPD_STATE_PAUSE))) {
357                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
358                 screen_song_add_song(current.selected_song, c);
359                 g_ptr_array_add(current.lines, g_strdup("\0"));
360         }
362         if (c->song != NULL && c->status != NULL &&
363             (mpd_status_get_state(c->status) != MPD_STATE_PLAY &&
364              mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) {
365                 if (current.played_song != NULL) {
366                         mpd_song_free(current.played_song);
367                 }
368                 current.played_song = mpd_song_dup(c->song);
369                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
370                 screen_song_add_song(current.played_song, c);
371                 g_ptr_array_add(current.lines, g_strdup("\0"));
372         }
374         /* Add some statistics about mpd */
375         if (c->connection != NULL)
376                 screen_song_add_stats(c);
378         screen_song_repaint();
381 static bool
382 screen_song_cmd(mpdclient_t *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(mpdclient_t *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);