Code

bc370d338611c08b671cf9f186e5c80090dfa074
[ncmpc.git] / src / screen_queue.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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_queue.h"
21 #include "screen_interface.h"
22 #include "screen_file.h"
23 #include "screen_status.h"
24 #include "screen_find.h"
25 #include "save_playlist.h"
26 #include "config.h"
27 #include "i18n.h"
28 #include "charset.h"
29 #include "options.h"
30 #include "mpdclient.h"
31 #include "utils.h"
32 #include "strfsong.h"
33 #include "wreadln.h"
34 #include "song_paint.h"
35 #include "screen.h"
36 #include "screen_utils.h"
37 #include "screen_song.h"
38 #include "screen_lyrics.h"
39 #include "db_completion.h"
40 #include "Compiler.h"
42 #ifndef NCMPC_MINI
43 #include "hscroll.h"
44 #endif
46 #include <mpd/client.h>
48 #include <ctype.h>
49 #include <string.h>
50 #include <glib.h>
52 #define MAX_SONG_LENGTH 512
54 #ifndef NCMPC_MINI
55 static struct hscroll hscroll;
56 #endif
58 static struct mpdclient_playlist *playlist;
59 static int current_song_id = -1;
60 static int selected_song_id = -1;
61 static struct list_window *lw;
62 static guint timer_hide_cursor_id;
64 static void
65 screen_queue_paint(void);
67 static void
68 screen_queue_repaint(void)
69 {
70         screen_queue_paint();
71         wrefresh(lw->w);
72 }
74 static const struct mpd_song *
75 screen_queue_selected_song(void)
76 {
77         return !lw->range_selection &&
78                 lw->selected < playlist_length(playlist)
79                 ? playlist_get(playlist, lw->selected)
80                 : NULL;
81 }
83 static void
84 screen_queue_save_selection(void)
85 {
86         selected_song_id = screen_queue_selected_song() != NULL
87                 ? (int)mpd_song_get_id(screen_queue_selected_song())
88                 : -1;
89 }
91 static void
92 screen_queue_restore_selection(void)
93 {
94         list_window_set_length(lw, playlist_length(playlist));
96         if (selected_song_id < 0)
97                 /* there was no selection */
98                 return;
100         const struct mpd_song *song = screen_queue_selected_song();
101         if (song != NULL &&
102             mpd_song_get_id(song) == (unsigned)selected_song_id)
103                 /* selection is still valid */
104                 return;
106         int pos = playlist_get_index_from_id(playlist, selected_song_id);
107         if (pos >= 0)
108                 list_window_set_cursor(lw, pos);
110         screen_queue_save_selection();
113 static const char *
114 screen_queue_lw_callback(unsigned idx, gcc_unused void *data)
116         static char songname[MAX_SONG_LENGTH];
118         assert(playlist != NULL);
119         assert(idx < playlist_length(playlist));
121         struct mpd_song *song = playlist_get(playlist, idx);
123         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
125         return songname;
128 static void
129 center_playing_item(const struct mpd_status *status, bool center_cursor)
131         if (status == NULL ||
132             (mpd_status_get_state(status) != MPD_STATE_PLAY &&
133              mpd_status_get_state(status) != MPD_STATE_PAUSE))
134                 return;
136         /* try to center the song that are playing */
137         int idx = mpd_status_get_song_pos(status);
138         if (idx < 0)
139                 return;
141         list_window_center(lw, idx);
143         if (center_cursor) {
144                 list_window_set_cursor(lw, idx);
145                 return;
146         }
148         /* make sure the cursor is in the window */
149         list_window_fetch_cursor(lw);
152 gcc_pure
153 static int
154 get_current_song_id(const struct mpd_status *status)
156         return status != NULL &&
157                 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
158                  mpd_status_get_state(status) == MPD_STATE_PAUSE)
159                 ? (int)mpd_status_get_song_id(status)
160                 : -1;
163 static bool
164 screen_queue_song_change(const struct mpd_status *status)
166         if (get_current_song_id(status) == current_song_id)
167                 return false;
169         current_song_id = get_current_song_id(status);
171         /* center the cursor */
172         if (options.auto_center && !lw->range_selection)
173                 center_playing_item(status, false);
175         return true;
178 #ifndef NCMPC_MINI
179 /**
180  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
181  * g_completion_set_compare(), because strncmp() takes size_t where
182  * g_completion_set_compare passes a gsize value.
183  */
184 static gint
185 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
187         return strncmp(s1, s2, n);
189 #endif
191 #ifndef NCMPC_MINI
192 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
193                     GList **list, struct mpdclient *c)
195         g_completion_remove_items(gcmp, *list);
196         *list = string_list_remove(*list, dir);
197         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
198         g_completion_add_items(gcmp, *list);
199         *dir_list = g_list_append(*dir_list, g_strdup(dir));
202 typedef struct
204         GList **list;
205         GList **dir_list;
206         struct mpdclient *c;
207 } completion_callback_data_t;
209 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
211         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
212         GList **dir_list = tmp->dir_list;
213         GList **list = tmp->list;
214         struct mpdclient *c = tmp->c;
216         if (*list == NULL) {
217                 /* create initial list */
218                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
219                 g_completion_add_items(gcmp, *list);
220         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
221                    string_list_find(*dir_list, line) == NULL) {
222                 /* add directory content to list */
223                 add_dir(gcmp, line, dir_list, list, c);
224         }
227 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
228                                    GList *items, void *data)
230         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
231         GList **dir_list = tmp->dir_list;
232         GList **list = tmp->list;
233         struct mpdclient *c = tmp->c;
235         if (g_list_length(items) >= 1)
236                 screen_display_completion_list(items);
238         if (line && line[0] && line[strlen(line) - 1] == '/' &&
239             string_list_find(*dir_list, line) == NULL) {
240                 /* add directory content to list */
241                 add_dir(gcmp, line, dir_list, list, c);
242         }
244 #endif
246 static int
247 handle_add_to_playlist(struct mpdclient *c)
249 #ifndef NCMPC_MINI
250         /* initialize completion support */
251         GCompletion *gcmp = g_completion_new(NULL);
252         g_completion_set_compare(gcmp, completion_strncmp);
254         GList *list = NULL;
255         GList *dir_list = NULL;
256         completion_callback_data_t data = {
257                 .list = &list,
258                 .dir_list = &dir_list,
259                 .c = c,
260         };
262         wrln_completion_callback_data = &data;
263         wrln_pre_completion_callback = add_pre_completion_cb;
264         wrln_post_completion_callback = add_post_completion_cb;
265 #else
266         GCompletion *gcmp = NULL;
267 #endif
269         /* get path */
270         char *path = screen_readln(_("Add"),
271                                    NULL,
272                                    NULL,
273                                    gcmp);
275         /* destroy completion data */
276 #ifndef NCMPC_MINI
277         wrln_completion_callback_data = NULL;
278         wrln_pre_completion_callback = NULL;
279         wrln_post_completion_callback = NULL;
280         g_completion_free(gcmp);
281         string_list_free(list);
282         string_list_free(dir_list);
283 #endif
285         /* add the path to the playlist */
286         if (path != NULL) {
287                 char *path_utf8 = locale_to_utf8(path);
288                 mpdclient_cmd_add_path(c, path_utf8);
289                 g_free(path_utf8);
290         }
292         g_free(path);
293         return 0;
296 static void
297 screen_queue_init(WINDOW *w, unsigned cols, unsigned rows)
299         lw = list_window_init(w, cols, rows);
301 #ifndef NCMPC_MINI
302         if (options.scroll)
303                 hscroll_init(&hscroll, w, options.scroll_sep);
304 #endif
307 static gboolean
308 timer_hide_cursor(gpointer data)
310         struct mpdclient *c = data;
312         assert(options.hide_cursor > 0);
313         assert(timer_hide_cursor_id != 0);
315         timer_hide_cursor_id = 0;
317         /* hide the cursor when mpd is playing and the user is inactive */
319         if (c->status != NULL &&
320             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
321                 lw->hide_cursor = true;
322                 screen_queue_repaint();
323         } else
324                 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
325                                                              timer_hide_cursor, c);
327         return FALSE;
330 static void
331 screen_queue_open(struct mpdclient *c)
333         playlist = &c->playlist;
335         assert(timer_hide_cursor_id == 0);
336         if (options.hide_cursor > 0) {
337                 lw->hide_cursor = false;
338                 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
339                                                              timer_hide_cursor, c);
340         }
342         screen_queue_restore_selection();
343         screen_queue_song_change(c->status);
346 static void
347 screen_queue_close(void)
349         if (timer_hide_cursor_id != 0) {
350                 g_source_remove(timer_hide_cursor_id);
351                 timer_hide_cursor_id = 0;
352         }
354 #ifndef NCMPC_MINI
355         if (options.scroll)
356                 hscroll_clear(&hscroll);
357 #endif
360 static void
361 screen_queue_resize(unsigned cols, unsigned rows)
363         list_window_resize(lw, cols, rows);
367 static void
368 screen_queue_exit(void)
370         list_window_free(lw);
373 /**
374  * Extract the host portion (without the optional password) from the
375  * MPD_HOST string.
376  */
377 static const char *
378 host_without_password(const char *host)
380         const char *separator = strchr(host, '@');
381         if (separator != NULL && separator != host && separator[1] != 0)
382                 host = separator + 1;
384         return host;
387 static const char *
388 screen_queue_title(char *str, size_t size)
390         if (options.host == NULL)
391                 return _("Queue");
393         g_snprintf(str, size, _("Queue on %s"),
394                    host_without_password(options.host));
395         return str;
398 static void
399 screen_queue_paint_callback(WINDOW *w, unsigned i,
400                             unsigned y, unsigned width,
401                             bool selected, gcc_unused const void *data)
403         assert(playlist != NULL);
404         assert(i < playlist_length(playlist));
406         const struct mpd_song *song = playlist_get(playlist, i);
408         struct hscroll *row_hscroll = NULL;
409 #ifndef NCMPC_MINI
410         row_hscroll = selected && options.scroll && lw->selected == i
411                 ? &hscroll : NULL;
412 #endif
414         paint_song_row(w, y, width, selected,
415                        (int)mpd_song_get_id(song) == current_song_id,
416                        song, row_hscroll, options.list_format);
419 static void
420 screen_queue_paint(void)
422 #ifndef NCMPC_MINI
423         if (options.scroll)
424                 hscroll_clear(&hscroll);
425 #endif
427         list_window_paint2(lw, screen_queue_paint_callback, NULL);
430 static void
431 screen_queue_update(struct mpdclient *c)
433         if (c->events & MPD_IDLE_QUEUE)
434                 screen_queue_restore_selection();
435         else
436                 /* the queue size may have changed, even if we havn't
437                    received the QUEUE idle event yet */
438                 list_window_set_length(lw, playlist_length(playlist));
440         if (((c->events & MPD_IDLE_PLAYER) != 0 &&
441              screen_queue_song_change(c->status)) ||
442             c->events & MPD_IDLE_QUEUE)
443                 /* the queue or the current song has changed, we must
444                    paint the new version */
445                 screen_queue_paint();
448 #ifdef HAVE_GETMOUSE
449 static bool
450 handle_mouse_event(struct mpdclient *c)
452         unsigned long bstate;
453         int row;
454         if (screen_get_mouse_event(c, &bstate, &row) ||
455             list_window_mouse(lw, bstate, row)) {
456                 screen_queue_paint();
457                 return true;
458         }
460         if (bstate & BUTTON1_DOUBLE_CLICKED) {
461                 /* stop */
462                 screen_cmd(c, CMD_STOP);
463                 return true;
464         }
466         const unsigned old_selected = lw->selected;
467         list_window_set_cursor(lw, lw->start + row);
469         if (bstate & BUTTON1_CLICKED) {
470                 /* play */
471                 const struct mpd_song *song = screen_queue_selected_song();
472                 if (song != NULL) {
473                         struct mpd_connection *connection =
474                                 mpdclient_get_connection(c);
476                         if (connection != NULL &&
477                             !mpd_run_play_id(connection,
478                                              mpd_song_get_id(song)))
479                                 mpdclient_handle_error(c);
480                 }
481         } else if (bstate & BUTTON3_CLICKED) {
482                 /* delete */
483                 if (lw->selected == old_selected)
484                         mpdclient_cmd_delete(c, lw->selected);
486                 list_window_set_length(lw, playlist_length(playlist));
487         }
489         screen_queue_save_selection();
490         screen_queue_paint();
492         return true;
494 #endif
496 static bool
497 screen_queue_cmd(struct mpdclient *c, command_t cmd)
499         struct mpd_connection *connection;
500         static command_t cached_cmd = CMD_NONE;
502         const command_t prev_cmd = cached_cmd;
503         cached_cmd = cmd;
505         lw->hide_cursor = false;
507         if (options.hide_cursor > 0) {
508                 if (timer_hide_cursor_id != 0)
509                         g_source_remove(timer_hide_cursor_id);
510                 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
511                                                              timer_hide_cursor, c);
512         }
514         if (list_window_cmd(lw, cmd)) {
515                 screen_queue_save_selection();
516                 screen_queue_paint();
517                 return true;
518         }
520         switch(cmd) {
521         case CMD_SCREEN_UPDATE:
522                 center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
523                 screen_queue_paint();
524                 return false;
525         case CMD_SELECT_PLAYING:
526                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
527                                                               c->song));
528                 screen_queue_save_selection();
529                 screen_queue_paint();
530                 return true;
532         case CMD_LIST_FIND:
533         case CMD_LIST_RFIND:
534         case CMD_LIST_FIND_NEXT:
535         case CMD_LIST_RFIND_NEXT:
536                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
537                 screen_queue_save_selection();
538                 screen_queue_paint();
539                 return true;
540         case CMD_LIST_JUMP:
541                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL, NULL);
542                 screen_queue_save_selection();
543                 screen_queue_paint();
544                 return true;
546 #ifdef HAVE_GETMOUSE
547         case CMD_MOUSE_EVENT:
548                 return handle_mouse_event(c);
549 #endif
551 #ifdef ENABLE_SONG_SCREEN
552         case CMD_SCREEN_SONG:
553                 if (screen_queue_selected_song() != NULL) {
554                         screen_song_switch(c, screen_queue_selected_song());
555                         return true;
556                 }
558                 break;
559 #endif
561 #ifdef ENABLE_LYRICS_SCREEN
562         case CMD_SCREEN_LYRICS:
563                 if (lw->selected < playlist_length(&c->playlist)) {
564                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
565                         bool follow = false;
567                         if (c->song && selected &&
568                             !strcmp(mpd_song_get_uri(selected),
569                                     mpd_song_get_uri(c->song)))
570                                 follow = true;
572                         screen_lyrics_switch(c, selected, follow);
573                         return true;
574                 }
576                 break;
577 #endif
578         case CMD_SCREEN_SWAP:
579                 if (playlist_length(&c->playlist) > 0)
580                         screen_swap(c, playlist_get(&c->playlist, lw->selected));
581                 else
582                         screen_swap(c, NULL);
583                 return true;
585         default:
586                 break;
587         }
589         if (!mpdclient_is_connected(c))
590                 return false;
592         switch(cmd) {
593                 const struct mpd_song *song;
594                 struct list_window_range range;
596         case CMD_PLAY:
597                 song = screen_queue_selected_song();
598                 if (song == NULL)
599                         return false;
601                 connection = mpdclient_get_connection(c);
602                 if (connection != NULL &&
603                     !mpd_run_play_id(connection, mpd_song_get_id(song)))
604                         mpdclient_handle_error(c);
606                 return true;
608         case CMD_DELETE:
609                 list_window_get_range(lw, &range);
610                 mpdclient_cmd_delete_range(c, range.start, range.end);
612                 list_window_set_cursor(lw, range.start);
613                 return true;
615         case CMD_SAVE_PLAYLIST:
616                 playlist_save(c, NULL, NULL);
617                 return true;
619         case CMD_ADD:
620                 handle_add_to_playlist(c);
621                 return true;
623         case CMD_SHUFFLE:
624                 list_window_get_range(lw, &range);
625                 if (range.end <= range.start + 1)
626                         /* No range selection, shuffle all list. */
627                         break;
629                 connection = mpdclient_get_connection(c);
630                 if (connection == NULL)
631                         return true;
633                 if (mpd_run_shuffle_range(connection, range.start, range.end))
634                         screen_status_message(_("Shuffled queue"));
635                 else
636                         mpdclient_handle_error(c);
637                 return true;
639         case CMD_LIST_MOVE_UP:
640                 list_window_get_range(lw, &range);
641                 if (range.start == 0 || range.end <= range.start)
642                         return false;
644                 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
645                         return true;
647                 lw->selected--;
648                 lw->range_base--;
650                 if (lw->range_selection)
651                         list_window_scroll_to(lw, lw->range_base);
652                 list_window_scroll_to(lw, lw->selected);
654                 screen_queue_save_selection();
655                 return true;
657         case CMD_LIST_MOVE_DOWN:
658                 list_window_get_range(lw, &range);
659                 if (range.end >= playlist_length(&c->playlist))
660                         return false;
662                 if (!mpdclient_cmd_move(c, range.start, range.end))
663                         return true;
665                 lw->selected++;
666                 lw->range_base++;
668                 if (lw->range_selection)
669                         list_window_scroll_to(lw, lw->range_base);
670                 list_window_scroll_to(lw, lw->selected);
672                 screen_queue_save_selection();
673                 return true;
675         case CMD_LOCATE:
676                 if (screen_queue_selected_song() != NULL) {
677                         screen_file_goto_song(c, screen_queue_selected_song());
678                         return true;
679                 }
681                 break;
683         default:
684                 break;
685         }
687         return false;
690 const struct screen_functions screen_queue = {
691         .init = screen_queue_init,
692         .exit = screen_queue_exit,
693         .open = screen_queue_open,
694         .close = screen_queue_close,
695         .resize = screen_queue_resize,
696         .paint = screen_queue_paint,
697         .update = screen_queue_update,
698         .cmd = screen_queue_cmd,
699         .get_title = screen_queue_title,
700 };