7bb30aefd79973a292d74c63a5739fd97ae3f763
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();
116 }
118 static const char *
119 screen_queue_lw_callback(unsigned idx, gcc_unused void *data)
120 {
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;
131 }
133 static void
134 center_playing_item(const struct mpd_status *status, bool center_cursor)
135 {
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);
155 }
157 gcc_pure
158 static int
159 get_current_song_id(const struct mpd_status *status)
160 {
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;
166 }
168 static bool
169 screen_queue_song_change(const struct mpd_status *status)
170 {
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;
181 }
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)
191 {
192 return strncmp(s1, s2, n);
193 }
194 #endif
196 #ifndef NCMPC_MINI
197 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
198 GList **list, struct mpdclient *c)
199 {
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));
205 }
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)
214 {
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 }
229 }
231 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
232 GList *items, void *data)
233 {
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 }
247 }
248 #endif
250 static int
251 handle_add_to_playlist(struct mpdclient *c)
252 {
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;
298 }
300 static void
301 screen_queue_init(WINDOW *w, unsigned cols, unsigned rows)
302 {
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
309 }
311 static gboolean
312 timer_hide_cursor(gpointer data)
313 {
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;
332 }
334 static void
335 screen_queue_open(struct mpdclient *c)
336 {
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);
348 }
350 static void
351 screen_queue_close(void)
352 {
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
362 }
364 static void
365 screen_queue_resize(unsigned cols, unsigned rows)
366 {
367 list_window_resize(lw, cols, rows);
368 }
371 static void
372 screen_queue_exit(void)
373 {
374 list_window_free(lw);
375 g_free(queue_screen.connection_name);
376 }
378 static const char *
379 screen_queue_title(char *str, size_t size)
380 {
381 g_snprintf(str, size, _("Queue on %s"), queue_screen.connection_name);
382 return str;
383 }
385 static void
386 screen_queue_paint_callback(WINDOW *w, unsigned i,
387 unsigned y, unsigned width,
388 bool selected, gcc_unused const void *data)
389 {
390 assert(playlist != NULL);
391 assert(i < playlist_length(playlist));
393 const struct mpd_song *song = playlist_get(playlist, i);
395 struct hscroll *row_hscroll = NULL;
396 #ifndef NCMPC_MINI
397 row_hscroll = selected && options.scroll && lw->selected == i
398 ? &hscroll : NULL;
399 #endif
401 paint_song_row(w, y, width, selected,
402 (int)mpd_song_get_id(song) == current_song_id,
403 song, row_hscroll, options.list_format);
404 }
406 static void
407 screen_queue_paint(void)
408 {
409 #ifndef NCMPC_MINI
410 if (options.scroll)
411 hscroll_clear(&hscroll);
412 #endif
414 list_window_paint2(lw, screen_queue_paint_callback, NULL);
415 }
417 static void
418 screen_queue_update(struct mpdclient *c)
419 {
420 if (c->connection_id != queue_screen.last_connection_id) {
421 queue_screen.last_connection_id = c->connection_id;
422 g_free(queue_screen.connection_name);
423 queue_screen.connection_name = mpdclient_settings_name(c);
424 }
426 if (c->events & MPD_IDLE_QUEUE)
427 screen_queue_restore_selection();
428 else
429 /* the queue size may have changed, even if we havn't
430 received the QUEUE idle event yet */
431 list_window_set_length(lw, playlist_length(playlist));
433 if (((c->events & MPD_IDLE_PLAYER) != 0 &&
434 screen_queue_song_change(c->status)) ||
435 c->events & MPD_IDLE_QUEUE)
436 /* the queue or the current song has changed, we must
437 paint the new version */
438 screen_queue_paint();
439 }
441 #ifdef HAVE_GETMOUSE
442 static bool
443 handle_mouse_event(struct mpdclient *c)
444 {
445 unsigned long bstate;
446 int row;
447 if (screen_get_mouse_event(c, &bstate, &row) ||
448 list_window_mouse(lw, bstate, row)) {
449 screen_queue_paint();
450 return true;
451 }
453 if (bstate & BUTTON1_DOUBLE_CLICKED) {
454 /* stop */
455 screen_cmd(c, CMD_STOP);
456 return true;
457 }
459 const unsigned old_selected = lw->selected;
460 list_window_set_cursor(lw, lw->start + row);
462 if (bstate & BUTTON1_CLICKED) {
463 /* play */
464 const struct mpd_song *song = screen_queue_selected_song();
465 if (song != NULL) {
466 struct mpd_connection *connection =
467 mpdclient_get_connection(c);
469 if (connection != NULL &&
470 !mpd_run_play_id(connection,
471 mpd_song_get_id(song)))
472 mpdclient_handle_error(c);
473 }
474 } else if (bstate & BUTTON3_CLICKED) {
475 /* delete */
476 if (lw->selected == old_selected)
477 mpdclient_cmd_delete(c, lw->selected);
479 list_window_set_length(lw, playlist_length(playlist));
480 }
482 screen_queue_save_selection();
483 screen_queue_paint();
485 return true;
486 }
487 #endif
489 static bool
490 screen_queue_cmd(struct mpdclient *c, command_t cmd)
491 {
492 struct mpd_connection *connection;
493 static command_t cached_cmd = CMD_NONE;
495 const command_t prev_cmd = cached_cmd;
496 cached_cmd = cmd;
498 lw->hide_cursor = false;
500 if (options.hide_cursor > 0) {
501 if (timer_hide_cursor_id != 0)
502 g_source_remove(timer_hide_cursor_id);
503 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
504 timer_hide_cursor, c);
505 }
507 if (list_window_cmd(lw, cmd)) {
508 screen_queue_save_selection();
509 screen_queue_paint();
510 return true;
511 }
513 switch(cmd) {
514 case CMD_SCREEN_UPDATE:
515 center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
516 screen_queue_paint();
517 return false;
518 case CMD_SELECT_PLAYING:
519 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
520 c->song));
521 screen_queue_save_selection();
522 screen_queue_paint();
523 return true;
525 case CMD_LIST_FIND:
526 case CMD_LIST_RFIND:
527 case CMD_LIST_FIND_NEXT:
528 case CMD_LIST_RFIND_NEXT:
529 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
530 screen_queue_save_selection();
531 screen_queue_paint();
532 return true;
533 case CMD_LIST_JUMP:
534 screen_jump(lw, screen_queue_lw_callback, NULL, NULL, NULL);
535 screen_queue_save_selection();
536 screen_queue_paint();
537 return true;
539 #ifdef HAVE_GETMOUSE
540 case CMD_MOUSE_EVENT:
541 return handle_mouse_event(c);
542 #endif
544 #ifdef ENABLE_SONG_SCREEN
545 case CMD_SCREEN_SONG:
546 if (screen_queue_selected_song() != NULL) {
547 screen_song_switch(c, screen_queue_selected_song());
548 return true;
549 }
551 break;
552 #endif
554 #ifdef ENABLE_LYRICS_SCREEN
555 case CMD_SCREEN_LYRICS:
556 if (lw->selected < playlist_length(&c->playlist)) {
557 struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
558 bool follow = false;
560 if (c->song && selected &&
561 !strcmp(mpd_song_get_uri(selected),
562 mpd_song_get_uri(c->song)))
563 follow = true;
565 screen_lyrics_switch(c, selected, follow);
566 return true;
567 }
569 break;
570 #endif
571 case CMD_SCREEN_SWAP:
572 if (playlist_length(&c->playlist) > 0)
573 screen_swap(c, playlist_get(&c->playlist, lw->selected));
574 else
575 screen_swap(c, NULL);
576 return true;
578 default:
579 break;
580 }
582 if (!mpdclient_is_connected(c))
583 return false;
585 switch(cmd) {
586 const struct mpd_song *song;
587 struct list_window_range range;
589 case CMD_PLAY:
590 song = screen_queue_selected_song();
591 if (song == NULL)
592 return false;
594 connection = mpdclient_get_connection(c);
595 if (connection != NULL &&
596 !mpd_run_play_id(connection, mpd_song_get_id(song)))
597 mpdclient_handle_error(c);
599 return true;
601 case CMD_DELETE:
602 list_window_get_range(lw, &range);
603 mpdclient_cmd_delete_range(c, range.start, range.end);
605 list_window_set_cursor(lw, range.start);
606 return true;
608 case CMD_SAVE_PLAYLIST:
609 playlist_save(c, NULL, NULL);
610 return true;
612 case CMD_ADD:
613 handle_add_to_playlist(c);
614 return true;
616 case CMD_SHUFFLE:
617 list_window_get_range(lw, &range);
618 if (range.end <= range.start + 1)
619 /* No range selection, shuffle all list. */
620 break;
622 connection = mpdclient_get_connection(c);
623 if (connection == NULL)
624 return true;
626 if (mpd_run_shuffle_range(connection, range.start, range.end))
627 screen_status_message(_("Shuffled queue"));
628 else
629 mpdclient_handle_error(c);
630 return true;
632 case CMD_LIST_MOVE_UP:
633 list_window_get_range(lw, &range);
634 if (range.start == 0 || range.end <= range.start)
635 return false;
637 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
638 return true;
640 lw->selected--;
641 lw->range_base--;
643 if (lw->range_selection)
644 list_window_scroll_to(lw, lw->range_base);
645 list_window_scroll_to(lw, lw->selected);
647 screen_queue_save_selection();
648 return true;
650 case CMD_LIST_MOVE_DOWN:
651 list_window_get_range(lw, &range);
652 if (range.end >= playlist_length(&c->playlist))
653 return false;
655 if (!mpdclient_cmd_move(c, range.start, range.end))
656 return true;
658 lw->selected++;
659 lw->range_base++;
661 if (lw->range_selection)
662 list_window_scroll_to(lw, lw->range_base);
663 list_window_scroll_to(lw, lw->selected);
665 screen_queue_save_selection();
666 return true;
668 case CMD_LOCATE:
669 if (screen_queue_selected_song() != NULL) {
670 screen_file_goto_song(c, screen_queue_selected_song());
671 return true;
672 }
674 break;
676 default:
677 break;
678 }
680 return false;
681 }
683 const struct screen_functions screen_queue = {
684 .init = screen_queue_init,
685 .exit = screen_queue_exit,
686 .open = screen_queue_open,
687 .close = screen_queue_close,
688 .resize = screen_queue_resize,
689 .paint = screen_queue_paint,
690 .update = screen_queue_update,
691 .cmd = screen_queue_cmd,
692 .get_title = screen_queue_title,
693 };