Code

screen_browser: display song duration
[ncmpc.git] / src / screen_play.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_play.h"
21 #include "screen_interface.h"
22 #include "screen_file.h"
23 #include "screen_message.h"
24 #include "screen_find.h"
25 #include "config.h"
26 #include "i18n.h"
27 #include "charset.h"
28 #include "options.h"
29 #include "mpdclient.h"
30 #include "utils.h"
31 #include "strfsong.h"
32 #include "wreadln.h"
33 #include "colors.h"
34 #include "screen.h"
35 #include "screen_utils.h"
36 #include "screen_song.h"
37 #include "screen_lyrics.h"
39 #ifndef NCMPC_MINI
40 #include "hscroll.h"
41 #endif
43 #include <mpd/client.h>
45 #include <ctype.h>
46 #include <string.h>
47 #include <glib.h>
49 #define MAX_SONG_LENGTH 512
51 #ifndef NCMPC_MINI
52 typedef struct
53 {
54         GList **list;
55         GList **dir_list;
56         struct mpdclient *c;
57 } completion_callback_data_t;
59 static struct hscroll hscroll;
60 static guint scroll_source_id;
61 #endif
63 static struct mpdclient_playlist *playlist;
64 static int current_song_id = -1;
65 static int selected_song_id = -1;
66 static struct list_window *lw;
67 static guint timer_hide_cursor_id;
69 static void
70 screen_playlist_paint(void);
72 static void
73 playlist_repaint(void)
74 {
75         screen_playlist_paint();
76         wrefresh(lw->w);
77 }
79 static const struct mpd_song *
80 playlist_selected_song(void)
81 {
82         return !lw->range_selection &&
83                 lw->selected < playlist_length(playlist)
84                 ? playlist_get(playlist, lw->selected)
85                 : NULL;
86 }
88 static void
89 playlist_save_selection(void)
90 {
91         selected_song_id = playlist_selected_song() != NULL
92                 ? (int)mpd_song_get_id(playlist_selected_song())
93                 : -1;
94 }
96 static void
97 playlist_restore_selection(void)
98 {
99         const struct mpd_song *song;
100         int pos;
102         list_window_set_length(lw, playlist_length(playlist));
104         if (selected_song_id < 0)
105                 /* there was no selection */
106                 return;
108         song = playlist_selected_song();
109         if (song != NULL &&
110             mpd_song_get_id(song) == (unsigned)selected_song_id)
111                 /* selection is still valid */
112                 return;
114         pos = playlist_get_index_from_id(playlist, selected_song_id);
115         if (pos >= 0)
116                 list_window_set_cursor(lw, pos);
118         playlist_save_selection();
121 #ifndef NCMPC_MINI
122 static gboolean
123 scroll_timer_callback(G_GNUC_UNUSED gpointer data)
125         scroll_source_id = 0;
127         hscroll_step(&hscroll);
128         playlist_repaint();
129         return false;
131 #endif
133 static const char *
134 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
136         static char songname[MAX_SONG_LENGTH];
137         struct mpd_song *song;
139         assert(playlist != NULL);
140         assert(idx < playlist_length(playlist));
142         song = playlist_get(playlist, idx);
143         if ((int)mpd_song_get_id(song) == current_song_id)
144                 *highlight = true;
146         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
148 #ifndef NCMPC_MINI
149         if (second_column != NULL && mpd_song_get_duration(song) > 0) {
150                 char duration[32];
151                 format_duration_short(duration, sizeof(duration),
152                                       mpd_song_get_duration(song));
153                 *second_column = g_strdup(duration);
154         }
156         if (idx == lw->selected)
157         {
158                 int second_column_len = 0;
159                 if (second_column != NULL && *second_column != NULL)
160                         second_column_len = strlen(*second_column);
161                 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
162                 {
163                         static unsigned current_song;
164                         char *tmp;
166                         if (current_song != lw->selected) {
167                                 hscroll_reset(&hscroll);
168                                 current_song = lw->selected;
169                         }
171                         tmp = strscroll(&hscroll, songname, options.scroll_sep,
172                                         MAX_SONG_LENGTH);
173                         g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
174                         g_free(tmp);
176                         if (scroll_source_id == 0)
177                                 scroll_source_id =
178                                         g_timeout_add(1000,
179                                                       scroll_timer_callback,
180                                                       NULL);
181                 } else {
182                         hscroll_reset(&hscroll);
184                         if (scroll_source_id != 0) {
185                                 g_source_remove(scroll_source_id);
186                                 scroll_source_id = 0;
187                         }
188                 }
189         }
190 #else
191         (void)second_column;
192 #endif
194         return songname;
197 static void
198 center_playing_item(struct mpdclient *c, bool center_cursor)
200         const struct mpd_song *song;
201         unsigned length = c->playlist.list->len;
202         int idx;
204         song = mpdclient_get_current_song(c);
205         if (song == NULL)
206                 return;
208         /* try to center the song that are playing */
209         idx = playlist_get_index(&c->playlist, c->song);
210         if (idx < 0)
211                 return;
213         if (length < lw->rows)
214         {
215                 if (center_cursor)
216                         list_window_set_cursor(lw, idx);
217                 return;
218         }
220         list_window_center(lw, idx);
222         if (center_cursor) {
223                 list_window_set_cursor(lw, idx);
224                 return;
225         }
227         /* make sure the cursor is in the window */
228         list_window_fetch_cursor(lw);
231 #ifndef NCMPC_MINI
232 static void
233 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
234                        void *data)
236         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
237         GList **list = tmp->list;
238         struct mpdclient *c = tmp->c;
240         if( *list == NULL ) {
241                 /* create completion list */
242                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
243                 g_completion_add_items(gcmp, *list);
244         }
247 static void
248 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
249                         G_GNUC_UNUSED gchar *line, GList *items,
250                         G_GNUC_UNUSED void *data)
252         if (g_list_length(items) >= 1)
253                 screen_display_completion_list(items);
255 #endif
257 #ifndef NCMPC_MINI
258 /**
259  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
260  * g_completion_set_compare(), because strncmp() takes size_t where
261  * g_completion_set_compare passes a gsize value.
262  */
263 static gint
264 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
266         return strncmp(s1, s2, n);
268 #endif
270 int
271 playlist_save(struct mpdclient *c, char *name, char *defaultname)
273         struct mpd_connection *connection;
274         gchar *filename, *filename_utf8;
275 #ifndef NCMPC_MINI
276         GCompletion *gcmp;
277         GList *list = NULL;
278         completion_callback_data_t data;
279 #endif
281 #ifdef NCMPC_MINI
282         (void)defaultname;
283 #endif
285 #ifndef NCMPC_MINI
286         if (name == NULL) {
287                 /* initialize completion support */
288                 gcmp = g_completion_new(NULL);
289                 g_completion_set_compare(gcmp, completion_strncmp);
290                 data.list = &list;
291                 data.dir_list = NULL;
292                 data.c = c;
293                 wrln_completion_callback_data = &data;
294                 wrln_pre_completion_callback = save_pre_completion_cb;
295                 wrln_post_completion_callback = save_post_completion_cb;
298                 /* query the user for a filename */
299                 filename = screen_readln(_("Save playlist as"),
300                                          defaultname,
301                                          NULL,
302                                          gcmp);
304                 /* destroy completion support */
305                 wrln_completion_callback_data = NULL;
306                 wrln_pre_completion_callback = NULL;
307                 wrln_post_completion_callback = NULL;
308                 g_completion_free(gcmp);
309                 list = string_list_free(list);
310                 if( filename )
311                         filename=g_strstrip(filename);
312         } else
313 #endif
314                         filename=g_strdup(name);
316         if (filename == NULL)
317                 return -1;
319         /* send save command to mpd */
321         filename_utf8 = locale_to_utf8(filename);
323         connection = mpdclient_get_connection(c);
324         if (!mpd_run_save(connection, filename_utf8)) {
325                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
326                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
327                     mpd_connection_clear_error(connection)) {
328                         char *buf;
329                         int key;
331                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
332                                               filename, YES, NO);
333                         key = tolower(screen_getch(buf));
334                         g_free(buf);
336                         if (key != YES[0]) {
337                                 g_free(filename_utf8);
338                                 g_free(filename);
339                                 screen_status_printf(_("Aborted"));
340                                 return -1;
341                         }
343                         if (!mpd_run_rm(connection, filename_utf8) ||
344                             !mpd_run_save(connection, filename_utf8)) {
345                                 mpdclient_handle_error(c);
346                                 g_free(filename_utf8);
347                                 g_free(filename);
348                                 return -1;
349                         }
350                 } else {
351                         mpdclient_handle_error(c);
352                         g_free(filename_utf8);
353                         g_free(filename);
354                         return -1;
355                 }
356         }
358         c->events |= MPD_IDLE_STORED_PLAYLIST;
360         g_free(filename_utf8);
362         /* success */
363         screen_status_printf(_("Saved %s"), filename);
364         g_free(filename);
365         return 0;
368 #ifndef NCMPC_MINI
369 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
370                     GList **list, struct mpdclient *c)
372         g_completion_remove_items(gcmp, *list);
373         *list = string_list_remove(*list, dir);
374         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
375         g_completion_add_items(gcmp, *list);
376         *dir_list = g_list_append(*dir_list, g_strdup(dir));
379 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
381         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
382         GList **dir_list = tmp->dir_list;
383         GList **list = tmp->list;
384         struct mpdclient *c = tmp->c;
386         if (*list == NULL) {
387                 /* create initial list */
388                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
389                 g_completion_add_items(gcmp, *list);
390         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
391                    string_list_find(*dir_list, line) == NULL) {
392                 /* add directory content to list */
393                 add_dir(gcmp, line, dir_list, list, c);
394         }
397 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
398                                    GList *items, void *data)
400         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
401         GList **dir_list = tmp->dir_list;
402         GList **list = tmp->list;
403         struct mpdclient *c = tmp->c;
405         if (g_list_length(items) >= 1)
406                 screen_display_completion_list(items);
408         if (line && line[0] && line[strlen(line) - 1] == '/' &&
409             string_list_find(*dir_list, line) == NULL) {
410                 /* add directory content to list */
411                 add_dir(gcmp, line, dir_list, list, c);
412         }
414 #endif
416 static int
417 handle_add_to_playlist(struct mpdclient *c)
419         gchar *path;
420 #ifndef NCMPC_MINI
421         GCompletion *gcmp;
422         GList *list = NULL;
423         GList *dir_list = NULL;
424         completion_callback_data_t data;
426         /* initialize completion support */
427         gcmp = g_completion_new(NULL);
428         g_completion_set_compare(gcmp, completion_strncmp);
429         data.list = &list;
430         data.dir_list = &dir_list;
431         data.c = c;
432         wrln_completion_callback_data = &data;
433         wrln_pre_completion_callback = add_pre_completion_cb;
434         wrln_post_completion_callback = add_post_completion_cb;
435 #endif
437         /* get path */
438         path = screen_readln(_("Add"),
439                              NULL,
440                              NULL,
441 #ifdef NCMPC_MINI
442                              NULL
443 #else
444                              gcmp
445 #endif
446                              );
448         /* destroy completion data */
449 #ifndef NCMPC_MINI
450         wrln_completion_callback_data = NULL;
451         wrln_pre_completion_callback = NULL;
452         wrln_post_completion_callback = NULL;
453         g_completion_free(gcmp);
454         string_list_free(list);
455         string_list_free(dir_list);
456 #endif
458         /* add the path to the playlist */
459         if (path != NULL) {
460                 char *path_utf8 = locale_to_utf8(path);
461                 mpdclient_cmd_add_path(c, path_utf8);
462                 g_free(path_utf8);
463         }
465         g_free(path);
466         return 0;
469 static void
470 screen_playlist_init(WINDOW *w, int cols, int rows)
472         lw = list_window_init(w, cols, rows);
475 static gboolean
476 timer_hide_cursor(gpointer data)
478         struct mpdclient *c = data;
480         assert(options.hide_cursor > 0);
481         assert(timer_hide_cursor_id != 0);
483         timer_hide_cursor_id = 0;
485         /* hide the cursor when mpd is playing and the user is inactive */
487         if (c->status != NULL &&
488             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
489                 lw->hide_cursor = true;
490                 playlist_repaint();
491         } else
492                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
493                                                      timer_hide_cursor, c);
495         return FALSE;
498 static void
499 screen_playlist_open(struct mpdclient *c)
501         playlist = &c->playlist;
503         assert(timer_hide_cursor_id == 0);
504         if (options.hide_cursor > 0) {
505                 lw->hide_cursor = false;
506                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
507                                                      timer_hide_cursor, c);
508         }
510         playlist_restore_selection();
513 static void
514 screen_playlist_close(void)
516         if (timer_hide_cursor_id != 0) {
517                 g_source_remove(timer_hide_cursor_id);
518                 timer_hide_cursor_id = 0;
519         }
522 static void
523 screen_playlist_resize(int cols, int rows)
525         list_window_resize(lw, cols, rows);
529 static void
530 screen_playlist_exit(void)
532         list_window_free(lw);
535 static const char *
536 screen_playlist_title(char *str, size_t size)
538         if (options.host == NULL)
539                 return _("Playlist");
541         g_snprintf(str, size, _("Playlist on %s"), options.host);
542         return str;
545 static void
546 screen_playlist_paint(void)
548         list_window_paint(lw, list_callback, NULL);
551 static void
552 screen_playlist_update(struct mpdclient *c)
554         static int prev_song_id = -1;
556         if (c->events & MPD_IDLE_PLAYLIST)
557                 playlist_restore_selection();
559         current_song_id = c->status != NULL &&
560                 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
561                  mpd_status_get_state(c->status) == MPD_STATE_PAUSE)
562                 ? (int)mpd_status_get_song_id(c->status) : -1;
564         if (current_song_id != prev_song_id) {
565                 prev_song_id = current_song_id;
567                 /* center the cursor */
568                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
569                         center_playing_item(c, false);
571                 playlist_repaint();
572         } else if (c->events & MPD_IDLE_PLAYLIST) {
573                 /* the playlist has changed, we must paint the new
574                    version */
575                 playlist_repaint();
576         }
579 #ifdef HAVE_GETMOUSE
580 static bool
581 handle_mouse_event(struct mpdclient *c)
583         int row;
584         unsigned selected;
585         unsigned long bstate;
587         if (screen_get_mouse_event(c, &bstate, &row) ||
588             list_window_mouse(lw, bstate, row)) {
589                 playlist_repaint();
590                 return true;
591         }
593         if (bstate & BUTTON1_DOUBLE_CLICKED) {
594                 /* stop */
595                 screen_cmd(c, CMD_STOP);
596                 return true;
597         }
599         selected = lw->start + row;
601         if (bstate & BUTTON1_CLICKED) {
602                 /* play */
603                 if (lw->start + row < playlist_length(playlist))
604                         mpdclient_cmd_play(c, lw->start + row);
605         } else if (bstate & BUTTON3_CLICKED) {
606                 /* delete */
607                 if (selected == lw->selected)
608                         mpdclient_cmd_delete(c, lw->selected);
609         }
611         list_window_set_cursor(lw, selected);
612         playlist_save_selection();
613         playlist_repaint();
615         return true;
617 #endif
619 static bool
620 screen_playlist_cmd(struct mpdclient *c, command_t cmd)
622         struct mpd_connection *connection;
623         static command_t cached_cmd = CMD_NONE;
624         command_t prev_cmd = cached_cmd;
625         struct list_window_range range;
627         cached_cmd = cmd;
629         lw->hide_cursor = false;
631         if (options.hide_cursor > 0) {
632                 if (timer_hide_cursor_id != 0)
633                         g_source_remove(timer_hide_cursor_id);
634                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
635                                                      timer_hide_cursor, c);
636         }
638         if (list_window_cmd(lw, cmd)) {
639                 playlist_save_selection();
640                 playlist_repaint();
641                 return true;
642         }
644         switch(cmd) {
645         case CMD_SCREEN_UPDATE:
646                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
647                 playlist_repaint();
648                 return false;
649         case CMD_SELECT_PLAYING:
650                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
651                                                               c->song));
652                 playlist_save_selection();
653                 playlist_repaint();
654                 return true;
656         case CMD_LIST_FIND:
657         case CMD_LIST_RFIND:
658         case CMD_LIST_FIND_NEXT:
659         case CMD_LIST_RFIND_NEXT:
660                 screen_find(lw, cmd, list_callback, NULL);
661                 playlist_save_selection();
662                 playlist_repaint();
663                 return true;
664         case CMD_LIST_JUMP:
665                 screen_jump(lw, list_callback, NULL, NULL);
666                 playlist_save_selection();
667                 playlist_repaint();
668                 return true;
670 #ifdef HAVE_GETMOUSE
671         case CMD_MOUSE_EVENT:
672                 return handle_mouse_event(c);
673 #endif
675 #ifdef ENABLE_SONG_SCREEN
676         case CMD_SCREEN_SONG:
677                 if (playlist_selected_song()) {
678                         screen_song_switch(c, playlist_selected_song());
679                         return true;
680                 }
682                 break;
683 #endif
685 #ifdef ENABLE_LYRICS_SCREEN
686         case CMD_SCREEN_LYRICS:
687                 if (lw->selected < playlist_length(&c->playlist)) {
688                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
689                         bool follow = false;
691                         if (c->song && selected &&
692                             !strcmp(mpd_song_get_uri(selected),
693                                     mpd_song_get_uri(c->song)))
694                                 follow = true;
696                         screen_lyrics_switch(c, selected, follow);
697                         return true;
698                 }
700                 break;
701 #endif
702         case CMD_SCREEN_SWAP:
703                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
704                 return true;
706         default:
707                 break;
708         }
710         if (!mpdclient_is_connected(c))
711                 return false;
713         switch(cmd) {
714         case CMD_PLAY:
715                 mpdclient_cmd_play(c, lw->selected);
716                 return true;
718         case CMD_DELETE:
719                 list_window_get_range(lw, &range);
720                 mpdclient_cmd_delete_range(c, range.start, range.end);
722                 list_window_set_cursor(lw, range.start);
723                 return true;
725         case CMD_SAVE_PLAYLIST:
726                 playlist_save(c, NULL, NULL);
727                 return true;
729         case CMD_ADD:
730                 handle_add_to_playlist(c);
731                 return true;
733         case CMD_SHUFFLE:
734                 list_window_get_range(lw, &range);
735                 if (range.end < range.start + 1)
736                         /* No range selection, shuffle all list. */
737                         break;
739                 connection = mpdclient_get_connection(c);
740                 if (mpd_run_shuffle_range(connection, range.start, range.end))
741                         screen_status_message(_("Shuffled playlist"));
742                 else
743                         mpdclient_handle_error(c);
744                 return true;
746         case CMD_LIST_MOVE_UP:
747                 list_window_get_range(lw, &range);
748                 if (range.start == 0 || range.end <= range.start)
749                         return false;
751                 for (unsigned i = range.start; i < range.end; ++i)
752                         mpdclient_cmd_move(c, i, i - 1);
754                 lw->selected--;
755                 lw->range_base--;
757                 playlist_save_selection();
758                 return true;
760         case CMD_LIST_MOVE_DOWN:
761                 list_window_get_range(lw, &range);
762                 if (range.end >= playlist_length(&c->playlist))
763                         return false;
765                 for (int i = range.end - 1; i >= (int)range.start; --i)
766                         mpdclient_cmd_move(c, i, i + 1);
768                 lw->selected++;
769                 lw->range_base++;
771                 playlist_save_selection();
772                 return true;
774         case CMD_LOCATE:
775                 if (playlist_selected_song()) {
776                         screen_file_goto_song(c, playlist_selected_song());
777                         return true;
778                 }
780                 break;
782         default:
783                 break;
784         }
786         return false;
789 const struct screen_functions screen_playlist = {
790         .init = screen_playlist_init,
791         .exit = screen_playlist_exit,
792         .open = screen_playlist_open,
793         .close = screen_playlist_close,
794         .resize = screen_playlist_resize,
795         .paint = screen_playlist_paint,
796         .update = screen_playlist_update,
797         .cmd = screen_playlist_cmd,
798         .get_title = screen_playlist_title,
799 };