Code

screen_lyrics, screen_song: duplicate "next_song"
[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 <glib/gprintf.h>
27 #include <string.h>
29 static list_window_t *lw;
31 static struct mpd_song *next_song;
33 static struct {
34         struct mpd_song *selected_song;
35         struct mpd_song *played_song;
36         GPtrArray *lines;
37 } current;
39 static void
40 screen_song_clear(void)
41 {
42         for (guint i = 0; i < current.lines->len; ++i)
43                 g_free(g_ptr_array_index(current.lines, i));
45         g_ptr_array_set_size(current.lines, 0);
47         if (current.selected_song != NULL) {
48                 mpd_freeSong(current.selected_song);
49                 current.selected_song = NULL;
50         }
51         if (current.played_song != NULL) {
52                 mpd_freeSong(current.played_song);
53                 current.played_song = NULL;
54         }
55 }
57 static void
58 screen_song_paint(void);
60 /**
61  * Repaint and update the screen.
62  */
63 static void
64 screen_song_repaint(void)
65 {
66         screen_song_paint();
67         wrefresh(lw->w);
68 }
70 static const char *
71 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
72                           G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
73 {
74         static char buffer[256];
75         char *value;
77         if (idx >= current.lines->len)
78                 return NULL;
80         value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
81         g_strlcpy(buffer, value, sizeof(buffer));
82         g_free(value);
84         return buffer;
85 }
88 static void
89 screen_song_init(WINDOW *w, int cols, int rows)
90 {
91         /* We will need at least 10 lines, so this saves 10 reallocations :) */
92         current.lines = g_ptr_array_sized_new(10);
93         lw = list_window_init(w, cols, rows);
94         lw->hide_cursor = true;
95 }
97 static void
98 screen_song_exit(void)
99 {
100         list_window_free(lw);
102         screen_song_clear();
104         g_ptr_array_free(current.lines, TRUE);
105         current.lines = NULL;
108 static void
109 screen_song_resize(int cols, int rows)
111         lw->cols = cols;
112         lw->rows = rows;
115 static const char *
116 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
118         return _("Song viewer");
121 static void
122 screen_song_paint(void)
124         list_window_paint(lw, screen_song_list_callback, NULL);
127 /* Appends a line with a fixed width for the label column
128  * Handles NULL strings gracefully */
129 static void
130 screen_song_append(const char *label, const char *value, unsigned label_col)
132         int value_col, linebreaks, entry_size, label_size;
133         int i, k;
134         gchar *entry, *entry_iter;
135         const gchar *value_iter;
137         assert(label != NULL);
138         assert(g_utf8_validate(label, -1, NULL));
140         if (value != NULL) {
141                 assert(g_utf8_validate(value, -1, NULL));
142                 /* +2 for ': ' */
143                 label_col += 2;
144                 value_col = lw->cols - label_col;
145                 /* calculate the number of required linebreaks */
146                 linebreaks = (utf8_width(value) - 1) / value_col + 1;
147                 value_iter = value;
148                 label_size = strlen(label) + label_col - utf8_width(label);
149                 entry_size = label_size + strlen(value) + 2;
151                 for (i = 0; i < linebreaks; ++i)
152                 {
153                         entry = g_malloc(entry_size);
154                         if (i == 0) {
155                                 entry_iter = entry + g_sprintf(entry, "%s: ", label);
156                                 /* fill the label column with whitespaces */
157                                 for ( ; entry_iter < entry + label_size; ++entry_iter)
158                                         *entry_iter = ' ';
159                         }
160                         else {
161                                 entry_iter = entry;
162                                 /* fill the label column with whitespaces */
163                                 for ( ; entry_iter < entry + label_col; ++entry_iter)
164                                         *entry_iter = ' ';
165                         }
166                         /* skip whitespaces */
167                         while (g_ascii_isspace(*value_iter)) ++value_iter;
168                         k = 0;
169                         while (value_iter && k < value_col)
170                         {
171                                 g_utf8_strncpy(entry_iter, value_iter, 1);
172                                 value_iter = g_utf8_find_next_char(value_iter, NULL);
173                                 entry_iter = g_utf8_find_next_char(entry_iter, NULL);
174                                 ++k;
175                         }
176                         *entry_iter = '\0';
177                         g_ptr_array_add(current.lines, entry);
178                 }
179         }
182 static void
183 screen_song_add_song(const struct mpd_song *song, const mpdclient_t *c)
185         unsigned i, max_label_width;
186         enum label {
187                 ARTIST, TITLE, ALBUM, LENGTH, COMPOSER, NAME, DISC, TRACK,
188                 DATE, GENRE, COMMENT, PATH, BITRATE
189         };
190         const char *labels[] = { [ARTIST] = _("Artist"),
191                 [TITLE] = _("Title"),
192                 [ALBUM] = _("Album"),
193                 [LENGTH] = _("Length"),
194                 [COMPOSER] = _("Composer"),
195                 [NAME] = _("Name"),
196                 [DISC] = _("Disc"),
197                 [TRACK] = _("Track"),
198                 [DATE] = _("Date"),
199                 [GENRE] = _("Genre"),
200                 [COMMENT] = _("Comment"),
201                 [PATH] = _("Path"),
202                 [BITRATE] = _("Bitrate"),
203         };
204         /* Determine the width of the longest label */
205         max_label_width = utf8_width(labels[0]);
206         for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
207                 if (utf8_width(labels[i]) > max_label_width)
208                         max_label_width = utf8_width(labels[i]);
209         }
211         assert(song != NULL);
213         screen_song_append(labels[ARTIST], song->artist, max_label_width);
214         screen_song_append(labels[TITLE], song->title, max_label_width);
215         screen_song_append(labels[ALBUM], song->album, max_label_width);
216         /* create time string and add it */
217         if (song->time != MPD_SONG_NO_TIME) {
218                 char length[16];
219                 /*write out the time, using hours if time over 60 minutes*/
220                 if (song->time > 3600) {
221                         g_snprintf(length, sizeof(length),
222                                         "%i:%02i:%02i",
223                                         song->time/3600, (song->time%3600)/60, song->time%60);
224                 } else {
225                         g_snprintf(length, sizeof(length),
226                                         "%i:%02i", song->time/60, song->time%60);
227                 }
228                 screen_song_append(labels[LENGTH], length, max_label_width);
229         }
230         screen_song_append(labels[COMPOSER], song->composer, max_label_width);
231         screen_song_append(labels[NAME], song->name, max_label_width);
232         screen_song_append(labels[DISC], song->disc, max_label_width);
233         screen_song_append(labels[TRACK], song->track, max_label_width);
234         screen_song_append(labels[DATE], song->date, max_label_width);
235         screen_song_append(labels[GENRE], song->genre, max_label_width);
236         screen_song_append(labels[COMMENT], song->comment, max_label_width);
237         screen_song_append(labels[PATH], song->file, max_label_width);
238         if (c->status != NULL && c->song != NULL &&
239                          g_strcmp0(c->song->file, song->file) == 0 &&
240                         (c->status->state == MPD_STATUS_STATE_PLAY ||
241                          c->status->state == MPD_STATUS_STATE_PAUSE) ) {
242                 char buf[16];
243                 g_snprintf(buf, sizeof(buf), _("%d kbps"), c->status->bitRate);
244                 screen_song_append(labels[BITRATE], buf, max_label_width);
245         }
248 static void
249 screen_song_add_stats(const mpdclient_t *c)
251         unsigned i, max_label_width;
252         char buf[64];
253         char *duration;
254         GDate *date;
255         enum label {
256                 ARTISTS, ALBUMS, SONGS, UPTIME,
257                 DBUPTIME, PLAYTIME, DBPLAYTIME
258         };
259         const char *labels[] = { [ARTISTS] = _("Number of artists"),
260                 [ALBUMS] = _("Number of albums"),
261                 [SONGS] = _("Number of songs"),
262                 [UPTIME] = _("Uptime"),
263                 [DBUPTIME] =_("Most recent db update"),
264                 [PLAYTIME] = _("Playtime"),
265                 [DBPLAYTIME] = _("DB playtime")
266         };
267         mpd_Stats *mpd_stats = NULL;
268         if (c->connection != NULL) {
269                 mpd_sendStatsCommand(c->connection);
270                 mpd_stats = mpd_getStats(c->connection);
271         }
273         if (mpd_stats != NULL) {
274                 /* Determine the width of the longest label */
275                 max_label_width = utf8_width(labels[0]);
276                 for (i = 1; i < G_N_ELEMENTS(labels); ++i) {
277                         if (utf8_width(labels[i]) > max_label_width)
278                                 max_label_width = utf8_width(labels[i]);
279                 }
281                 g_ptr_array_add(current.lines, g_strdup(_("MPD statistics")) );
282                 g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfArtists);
283                 screen_song_append(labels[ARTISTS], buf, max_label_width);
284                 g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfAlbums);
285                 screen_song_append(labels[ALBUMS], buf, max_label_width);
286                 g_snprintf(buf, sizeof(buf), "%d", mpd_stats->numberOfSongs);
287                 screen_song_append(labels[SONGS], buf, max_label_width);
288                 duration = time_seconds_to_durationstr(mpd_stats->dbPlayTime);
289                 screen_song_append(labels[DBPLAYTIME], duration, max_label_width);
290                 g_free(duration);
291                 duration = time_seconds_to_durationstr(mpd_stats->playTime);
292                 screen_song_append(labels[PLAYTIME], duration, max_label_width);
293                 g_free(duration);
294                 duration = time_seconds_to_durationstr(mpd_stats->uptime);
295                 screen_song_append(labels[UPTIME], duration, max_label_width);
296                 g_free(duration);
297                 date = g_date_new();
298                 g_date_set_time_t(date, mpd_stats->dbUpdateTime);
299                 g_date_strftime(buf, sizeof(buf), "%x", date);
300                 screen_song_append(labels[DBUPTIME], buf, max_label_width);
301                 g_date_free(date);
303                 mpd_freeStats(mpd_stats);
304         }
307 static void
308 screen_song_update(mpdclient_t *c)
310         /* Clear all lines */
311         for (guint i = 0; i < current.lines->len; ++i)
312                 g_free(g_ptr_array_index(current.lines, i));
313         g_ptr_array_set_size(current.lines, 0);
315         /* If a song was selected before the song screen was opened */
316         if (next_song != NULL) {
317                 assert(current.selected_song == NULL);
318                 current.selected_song = next_song;
319                 next_song = NULL;
320         }
322         if (current.selected_song != NULL &&
323                         (c->song == NULL ||
324                          g_strcmp0(current.selected_song->file, c->song->file) != 0 ||
325                          c->status == NULL ||
326                         (c->status->state != MPD_STATUS_STATE_PLAY &&
327                          c->status->state != MPD_STATUS_STATE_PAUSE)) ) {
328                 g_ptr_array_add(current.lines, g_strdup(_("Selected song")) );
329                 screen_song_add_song(current.selected_song, c);
330                 g_ptr_array_add(current.lines, g_strdup("\0"));
331         }
333         if (c->song != NULL && c->status != NULL &&
334                         (c->status->state == MPD_STATUS_STATE_PLAY ||
335                          c->status->state == MPD_STATUS_STATE_PAUSE) ) {
336                 if (current.played_song != NULL) {
337                         mpd_freeSong(current.played_song);
338                 }
339                 current.played_song = mpd_songDup(c->song);
340                 g_ptr_array_add(current.lines, g_strdup(_("Currently playing song")));
341                 screen_song_add_song(current.played_song, c);
342                 g_ptr_array_add(current.lines, g_strdup("\0"));
343         }
345         /* Add some statistics about mpd */
346         if (c->connection != NULL)
347                 screen_song_add_stats(c);
349         screen_song_repaint();
352 static bool
353 screen_song_cmd(mpdclient_t *c, command_t cmd)
355         if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
356                 screen_song_repaint();
357                 return true;
358         }
360         switch(cmd) {
361         case CMD_LOCATE:
362                 if (current.selected_song != NULL) {
363                         screen_file_goto_song(c, current.selected_song);
364                         return true;
365                 }
366                 if (current.played_song != NULL) {
367                         screen_file_goto_song(c, current.played_song);
368                         return true;
369                 }
371                 return false;
373 #ifdef ENABLE_LYRICS_SCREEN
374         case CMD_SCREEN_LYRICS:
375                 if (current.selected_song != NULL) {
376                         screen_lyrics_switch(c, current.selected_song, false);
377                         return true;
378                 }
379                 if (current.played_song != NULL) {
380                         screen_lyrics_switch(c, current.played_song, true);
381                         return true;
382                 }
383                 return false;
385 #endif
387         case CMD_SCREEN_SWAP:
388                 if (current.selected_song != NULL)
389                         screen_swap(c, current.selected_song);
390                 else
391                 // No need to check if this is null - we'd pass null anyway
392                         screen_swap(c, current.played_song);
393                 return true;
395         default:
396                 break;
397         }
399         if (screen_find(lw, current.lines->len,
400                         cmd, screen_song_list_callback, NULL)) {
401                 /* center the row */
402                 list_window_center(lw, current.lines->len, lw->selected);
403                 screen_song_repaint();
404                 return true;
405         }
407         return false;
410 const struct screen_functions screen_song = {
411         .init = screen_song_init,
412         .exit = screen_song_exit,
413         .open = screen_song_update,
414         .close = screen_song_clear,
415         .resize = screen_song_resize,
416         .paint = screen_song_paint,
417         .update = screen_song_update,
418         .cmd = screen_song_cmd,
419         .get_title = screen_song_title,
420 };
422 void
423 screen_song_switch(mpdclient_t *c, const struct mpd_song *song)
425         assert(song != NULL);
426         assert(current.selected_song == NULL);
427         assert(current.played_song == NULL);
429         next_song = mpd_songDup(song);
430         screen_switch(&screen_song, c);