Code

dd27df04cce48fd6c1fd5a6ea9dc581aed72021c
[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 struct queue_screen_data {
65         unsigned last_connection_id;
66         char *connection_name;
67 } queue_screen;
69 static void
70 screen_queue_paint(void);
72 static void
73 screen_queue_repaint(void)
74 {
75         screen_queue_paint();
76         wrefresh(lw->w);
77 }
79 static const struct mpd_song *
80 screen_queue_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 screen_queue_save_selection(void)
90 {
91         selected_song_id = screen_queue_selected_song() != NULL
92                 ? (int)mpd_song_get_id(screen_queue_selected_song())
93                 : -1;
94 }
96 static void
97 screen_queue_restore_selection(void)
98 {
99         list_window_set_length(lw, playlist_length(playlist));
101         if (selected_song_id < 0)
102                 /* there was no selection */
103                 return;
105         const struct mpd_song *song = screen_queue_selected_song();
106         if (song != NULL &&
107             mpd_song_get_id(song) == (unsigned)selected_song_id)
108                 /* selection is still valid */
109                 return;
111         int pos = playlist_get_index_from_id(playlist, selected_song_id);
112         if (pos >= 0)
113                 list_window_set_cursor(lw, pos);
115         screen_queue_save_selection();
118 static const char *
119 screen_queue_lw_callback(unsigned idx, gcc_unused void *data)
121         static char songname[MAX_SONG_LENGTH];
123         assert(playlist != NULL);
124         assert(idx < playlist_length(playlist));
126         struct mpd_song *song = playlist_get(playlist, idx);
128         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
130         return songname;
133 static void
134 center_playing_item(const struct mpd_status *status, bool center_cursor)
136         if (status == NULL ||
137             (mpd_status_get_state(status) != MPD_STATE_PLAY &&
138              mpd_status_get_state(status) != MPD_STATE_PAUSE))
139                 return;
141         /* try to center the song that are playing */
142         int idx = mpd_status_get_song_pos(status);
143         if (idx < 0)
144                 return;
146         list_window_center(lw, idx);
148         if (center_cursor) {
149                 list_window_set_cursor(lw, idx);
150                 return;
151         }
153         /* make sure the cursor is in the window */
154         list_window_fetch_cursor(lw);
157 gcc_pure
158 static int
159 get_current_song_id(const struct mpd_status *status)
161         return status != NULL &&
162                 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
163                  mpd_status_get_state(status) == MPD_STATE_PAUSE)
164                 ? (int)mpd_status_get_song_id(status)
165                 : -1;
168 static bool
169 screen_queue_song_change(const struct mpd_status *status)
171         if (get_current_song_id(status) == current_song_id)
172                 return false;
174         current_song_id = get_current_song_id(status);
176         /* center the cursor */
177         if (options.auto_center && !lw->range_selection)
178                 center_playing_item(status, false);
180         return true;
183 #ifndef NCMPC_MINI
184 /**
185  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
186  * g_completion_set_compare(), because strncmp() takes size_t where
187  * g_completion_set_compare passes a gsize value.
188  */
189 static gint
190 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
192         return strncmp(s1, s2, n);
194 #endif
196 #ifndef NCMPC_MINI
197 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
198                     GList **list, struct mpdclient *c)
200         g_completion_remove_items(gcmp, *list);
201         *list = string_list_remove(*list, dir);
202         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
203         g_completion_add_items(gcmp, *list);
204         *dir_list = g_list_append(*dir_list, g_strdup(dir));
207 struct completion_callback_data {
208         GList **list;
209         GList **dir_list;
210         struct mpdclient *c;
211 };
213 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
215         struct completion_callback_data *tmp = data;
216         GList **dir_list = tmp->dir_list;
217         GList **list = tmp->list;
218         struct mpdclient *c = tmp->c;
220         if (*list == NULL) {
221                 /* create initial list */
222                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
223                 g_completion_add_items(gcmp, *list);
224         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
225                    string_list_find(*dir_list, line) == NULL) {
226                 /* add directory content to list */
227                 add_dir(gcmp, line, dir_list, list, c);
228         }
231 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
232                                    GList *items, void *data)
234         struct completion_callback_data *tmp = data;
235         GList **dir_list = tmp->dir_list;
236         GList **list = tmp->list;
237         struct mpdclient *c = tmp->c;
239         if (g_list_length(items) >= 1)
240                 screen_display_completion_list(items);
242         if (line && line[0] && line[strlen(line) - 1] == '/' &&
243             string_list_find(*dir_list, line) == NULL) {
244                 /* add directory content to list */
245                 add_dir(gcmp, line, dir_list, list, c);
246         }
248 #endif
250 static int
251 handle_add_to_playlist(struct mpdclient *c)
253 #ifndef NCMPC_MINI
254         /* initialize completion support */
255         GCompletion *gcmp = g_completion_new(NULL);
256         g_completion_set_compare(gcmp, completion_strncmp);
258         GList *list = NULL;
259         GList *dir_list = NULL;
260         struct completion_callback_data data = {
261                 .list = &list,
262                 .dir_list = &dir_list,
263                 .c = c,
264         };
266         wrln_completion_callback_data = &data;
267         wrln_pre_completion_callback = add_pre_completion_cb;
268         wrln_post_completion_callback = add_post_completion_cb;
269 #else
270         GCompletion *gcmp = NULL;
271 #endif
273         /* get path */
274         char *path = screen_readln(_("Add"),
275                                    NULL,
276                                    NULL,
277                                    gcmp);
279         /* destroy completion data */
280 #ifndef NCMPC_MINI
281         wrln_completion_callback_data = NULL;
282         wrln_pre_completion_callback = NULL;
283         wrln_post_completion_callback = NULL;
284         g_completion_free(gcmp);
285         string_list_free(list);
286         string_list_free(dir_list);
287 #endif
289         /* add the path to the playlist */
290         if (path != NULL) {
291                 char *path_utf8 = locale_to_utf8(path);
292                 mpdclient_cmd_add_path(c, path_utf8);
293                 g_free(path_utf8);
294         }
296         g_free(path);
297         return 0;
300 static void
301 screen_queue_init(WINDOW *w, unsigned cols, unsigned rows)
303         lw = list_window_init(w, cols, rows);
305 #ifndef NCMPC_MINI
306         if (options.scroll)
307                 hscroll_init(&hscroll, w, options.scroll_sep);
308 #endif
311 static gboolean
312 timer_hide_cursor(gpointer data)
314         struct mpdclient *c = data;
316         assert(options.hide_cursor > 0);
317         assert(timer_hide_cursor_id != 0);
319         timer_hide_cursor_id = 0;
321         /* hide the cursor when mpd is playing and the user is inactive */
323         if (c->status != NULL &&
324             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
325                 lw->hide_cursor = true;
326                 screen_queue_repaint();
327         } else
328                 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
329                                                              timer_hide_cursor, c);
331         return FALSE;
334 static void
335 screen_queue_open(struct mpdclient *c)
337         playlist = &c->playlist;
339         assert(timer_hide_cursor_id == 0);
340         if (options.hide_cursor > 0) {
341                 lw->hide_cursor = false;
342                 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
343                                                              timer_hide_cursor, c);
344         }
346         screen_queue_restore_selection();
347         screen_queue_song_change(c->status);
350 static void
351 screen_queue_close(void)
353         if (timer_hide_cursor_id != 0) {
354                 g_source_remove(timer_hide_cursor_id);
355                 timer_hide_cursor_id = 0;
356         }
358 #ifndef NCMPC_MINI
359         if (options.scroll)
360                 hscroll_clear(&hscroll);
361 #endif
364 static void
365 screen_queue_resize(unsigned cols, unsigned rows)
367         list_window_resize(lw, cols, rows);
371 static void
372 screen_queue_exit(void)
374         list_window_free(lw);
375         g_free(queue_screen.connection_name);
378 static const char *
379 screen_queue_title(char *str, size_t size)
381        if (queue_screen.connection_name == NULL)
382                return _("Queue");
384        g_snprintf(str, size, _("Queue on %s"), queue_screen.connection_name);
385         return str;
388 static void
389 screen_queue_paint_callback(WINDOW *w, unsigned i,
390                             unsigned y, unsigned width,
391                             bool selected, gcc_unused const void *data)
393         assert(playlist != NULL);
394         assert(i < playlist_length(playlist));
396         const struct mpd_song *song = playlist_get(playlist, i);
398         struct hscroll *row_hscroll = NULL;
399 #ifndef NCMPC_MINI
400         row_hscroll = selected && options.scroll && lw->selected == i
401                 ? &hscroll : NULL;
402 #endif
404         paint_song_row(w, y, width, selected,
405                        (int)mpd_song_get_id(song) == current_song_id,
406                        song, row_hscroll, options.list_format);
409 static void
410 screen_queue_paint(void)
412 #ifndef NCMPC_MINI
413         if (options.scroll)
414                 hscroll_clear(&hscroll);
415 #endif
417         list_window_paint2(lw, screen_queue_paint_callback, NULL);
420 static void
421 screen_queue_update(struct mpdclient *c)
423         if (c->connection_id != queue_screen.last_connection_id) {
424                 queue_screen.last_connection_id = c->connection_id;
425                 g_free(queue_screen.connection_name);
426                 queue_screen.connection_name = mpdclient_settings_name(c);
427         }
429         if (c->events & MPD_IDLE_QUEUE)
430                 screen_queue_restore_selection();
431         else
432                 /* the queue size may have changed, even if we havn't
433                    received the QUEUE idle event yet */
434                 list_window_set_length(lw, playlist_length(playlist));
436         if (((c->events & MPD_IDLE_PLAYER) != 0 &&
437              screen_queue_song_change(c->status)) ||
438             c->events & MPD_IDLE_QUEUE)
439                 /* the queue or the current song has changed, we must
440                    paint the new version */
441                 screen_queue_paint();
444 #ifdef HAVE_GETMOUSE
445 static bool
446 handle_mouse_event(struct mpdclient *c)
448         unsigned long bstate;
449         int row;
450         if (screen_get_mouse_event(c, &bstate, &row) ||
451             list_window_mouse(lw, bstate, row)) {
452                 screen_queue_paint();
453                 return true;
454         }
456         if (bstate & BUTTON1_DOUBLE_CLICKED) {
457                 /* stop */
458                 screen_cmd(c, CMD_STOP);
459                 return true;
460         }
462         const unsigned old_selected = lw->selected;
463         list_window_set_cursor(lw, lw->start + row);
465         if (bstate & BUTTON1_CLICKED) {
466                 /* play */
467                 const struct mpd_song *song = screen_queue_selected_song();
468                 if (song != NULL) {
469                         struct mpd_connection *connection =
470                                 mpdclient_get_connection(c);
472                         if (connection != NULL &&
473                             !mpd_run_play_id(connection,
474                                              mpd_song_get_id(song)))
475                                 mpdclient_handle_error(c);
476                 }
477         } else if (bstate & BUTTON3_CLICKED) {
478                 /* delete */
479                 if (lw->selected == old_selected)
480                         mpdclient_cmd_delete(c, lw->selected);
482                 list_window_set_length(lw, playlist_length(playlist));
483         }
485         screen_queue_save_selection();
486         screen_queue_paint();
488         return true;
490 #endif
492 static bool
493 screen_queue_cmd(struct mpdclient *c, command_t cmd)
495         struct mpd_connection *connection;
496         static command_t cached_cmd = CMD_NONE;
498         const command_t prev_cmd = cached_cmd;
499         cached_cmd = cmd;
501         lw->hide_cursor = false;
503         if (options.hide_cursor > 0) {
504                 if (timer_hide_cursor_id != 0)
505                         g_source_remove(timer_hide_cursor_id);
506                 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
507                                                              timer_hide_cursor, c);
508         }
510         if (list_window_cmd(lw, cmd)) {
511                 screen_queue_save_selection();
512                 screen_queue_paint();
513                 return true;
514         }
516         switch(cmd) {
517         case CMD_SCREEN_UPDATE:
518                 center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
519                 screen_queue_paint();
520                 return false;
521         case CMD_SELECT_PLAYING:
522                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
523                                                               c->song));
524                 screen_queue_save_selection();
525                 screen_queue_paint();
526                 return true;
528         case CMD_LIST_FIND:
529         case CMD_LIST_RFIND:
530         case CMD_LIST_FIND_NEXT:
531         case CMD_LIST_RFIND_NEXT:
532                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
533                 screen_queue_save_selection();
534                 screen_queue_paint();
535                 return true;
536         case CMD_LIST_JUMP:
537                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL, NULL);
538                 screen_queue_save_selection();
539                 screen_queue_paint();
540                 return true;
542 #ifdef HAVE_GETMOUSE
543         case CMD_MOUSE_EVENT:
544                 return handle_mouse_event(c);
545 #endif
547 #ifdef ENABLE_SONG_SCREEN
548         case CMD_SCREEN_SONG:
549                 if (screen_queue_selected_song() != NULL) {
550                         screen_song_switch(c, screen_queue_selected_song());
551                         return true;
552                 }
554                 break;
555 #endif
557 #ifdef ENABLE_LYRICS_SCREEN
558         case CMD_SCREEN_LYRICS:
559                 if (lw->selected < playlist_length(&c->playlist)) {
560                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
561                         bool follow = false;
563                         if (c->song && selected &&
564                             !strcmp(mpd_song_get_uri(selected),
565                                     mpd_song_get_uri(c->song)))
566                                 follow = true;
568                         screen_lyrics_switch(c, selected, follow);
569                         return true;
570                 }
572                 break;
573 #endif
574         case CMD_SCREEN_SWAP:
575                 if (playlist_length(&c->playlist) > 0)
576                         screen_swap(c, playlist_get(&c->playlist, lw->selected));
577                 else
578                         screen_swap(c, NULL);
579                 return true;
581         default:
582                 break;
583         }
585         if (!mpdclient_is_connected(c))
586                 return false;
588         switch(cmd) {
589                 const struct mpd_song *song;
590                 struct list_window_range range;
592         case CMD_PLAY:
593                 song = screen_queue_selected_song();
594                 if (song == NULL)
595                         return false;
597                 connection = mpdclient_get_connection(c);
598                 if (connection != NULL &&
599                     !mpd_run_play_id(connection, mpd_song_get_id(song)))
600                         mpdclient_handle_error(c);
602                 return true;
604         case CMD_DELETE:
605                 list_window_get_range(lw, &range);
606                 mpdclient_cmd_delete_range(c, range.start, range.end);
608                 list_window_set_cursor(lw, range.start);
609                 return true;
611         case CMD_SAVE_PLAYLIST:
612                 playlist_save(c, NULL, NULL);
613                 return true;
615         case CMD_ADD:
616                 handle_add_to_playlist(c);
617                 return true;
619         case CMD_SHUFFLE:
620                 list_window_get_range(lw, &range);
621                 if (range.end <= range.start + 1)
622                         /* No range selection, shuffle all list. */
623                         break;
625                 connection = mpdclient_get_connection(c);
626                 if (connection == NULL)
627                         return true;
629                 if (mpd_run_shuffle_range(connection, range.start, range.end))
630                         screen_status_message(_("Shuffled queue"));
631                 else
632                         mpdclient_handle_error(c);
633                 return true;
635         case CMD_LIST_MOVE_UP:
636                 list_window_get_range(lw, &range);
637                 if (range.start == 0 || range.end <= range.start)
638                         return false;
640                 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
641                         return true;
643                 lw->selected--;
644                 lw->range_base--;
646                 if (lw->range_selection)
647                         list_window_scroll_to(lw, lw->range_base);
648                 list_window_scroll_to(lw, lw->selected);
650                 screen_queue_save_selection();
651                 return true;
653         case CMD_LIST_MOVE_DOWN:
654                 list_window_get_range(lw, &range);
655                 if (range.end >= playlist_length(&c->playlist))
656                         return false;
658                 if (!mpdclient_cmd_move(c, range.start, range.end))
659                         return true;
661                 lw->selected++;
662                 lw->range_base++;
664                 if (lw->range_selection)
665                         list_window_scroll_to(lw, lw->range_base);
666                 list_window_scroll_to(lw, lw->selected);
668                 screen_queue_save_selection();
669                 return true;
671         case CMD_LOCATE:
672                 if (screen_queue_selected_song() != NULL) {
673                         screen_file_goto_song(c, screen_queue_selected_song());
674                         return true;
675                 }
677                 break;
679         default:
680                 break;
681         }
683         return false;
686 const struct screen_functions screen_queue = {
687         .init = screen_queue_init,
688         .exit = screen_queue_exit,
689         .open = screen_queue_open,
690         .close = screen_queue_close,
691         .resize = screen_queue_resize,
692         .paint = screen_queue_paint,
693         .update = screen_queue_update,
694         .cmd = screen_queue_cmd,
695         .get_title = screen_queue_title,
696 };