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 "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 "song_paint.h"
34 #include "screen.h"
35 #include "screen_utils.h"
36 #include "screen_song.h"
37 #include "screen_lyrics.h"
38 #include "Compiler.h"
40 #ifndef NCMPC_MINI
41 #include "hscroll.h"
42 #endif
44 #include <mpd/client.h>
46 #include <ctype.h>
47 #include <string.h>
48 #include <glib.h>
50 #define MAX_SONG_LENGTH 512
52 #ifndef NCMPC_MINI
53 typedef struct
54 {
55 GList **list;
56 GList **dir_list;
57 struct mpdclient *c;
58 } completion_callback_data_t;
60 static struct hscroll hscroll;
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_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 static void
185 save_pre_completion_cb(GCompletion *gcmp, gcc_unused gchar *line,
186 void *data)
187 {
188 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
189 GList **list = tmp->list;
190 struct mpdclient *c = tmp->c;
192 if( *list == NULL ) {
193 /* create completion list */
194 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
195 g_completion_add_items(gcmp, *list);
196 }
197 }
199 static void
200 save_post_completion_cb(gcc_unused GCompletion *gcmp,
201 gcc_unused gchar *line, GList *items,
202 gcc_unused void *data)
203 {
204 if (g_list_length(items) >= 1)
205 screen_display_completion_list(items);
206 }
207 #endif
209 #ifndef NCMPC_MINI
210 /**
211 * Wrapper for strncmp(). We are not allowed to pass &strncmp to
212 * g_completion_set_compare(), because strncmp() takes size_t where
213 * g_completion_set_compare passes a gsize value.
214 */
215 static gint
216 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
217 {
218 return strncmp(s1, s2, n);
219 }
220 #endif
222 int
223 playlist_save(struct mpdclient *c, char *name, char *defaultname)
224 {
225 struct mpd_connection *connection;
226 gchar *filename;
228 #ifdef NCMPC_MINI
229 (void)defaultname;
230 #endif
232 #ifndef NCMPC_MINI
233 if (name == NULL) {
234 /* initialize completion support */
235 GCompletion *gcmp = g_completion_new(NULL);
236 g_completion_set_compare(gcmp, completion_strncmp);
237 GList *list = NULL;
238 completion_callback_data_t data = {
239 .list = &list,
240 .dir_list = NULL,
241 .c = c,
242 };
243 wrln_completion_callback_data = &data;
244 wrln_pre_completion_callback = save_pre_completion_cb;
245 wrln_post_completion_callback = save_post_completion_cb;
248 /* query the user for a filename */
249 filename = screen_readln(_("Save queue as"),
250 defaultname,
251 NULL,
252 gcmp);
254 /* destroy completion support */
255 wrln_completion_callback_data = NULL;
256 wrln_pre_completion_callback = NULL;
257 wrln_post_completion_callback = NULL;
258 g_completion_free(gcmp);
259 list = string_list_free(list);
260 if( filename )
261 filename=g_strstrip(filename);
262 } else
263 #endif
264 filename=g_strdup(name);
266 if (filename == NULL)
267 return -1;
269 /* send save command to mpd */
271 connection = mpdclient_get_connection(c);
272 if (connection == NULL) {
273 g_free(filename);
274 return -1;
275 }
277 char *filename_utf8 = locale_to_utf8(filename);
278 if (!mpd_run_save(connection, filename_utf8)) {
279 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
280 mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
281 mpd_connection_clear_error(connection)) {
282 char *buf = g_strdup_printf(_("Replace %s?"), filename);
283 bool replace = screen_get_yesno(buf, false);
284 g_free(buf);
286 if (!replace) {
287 g_free(filename_utf8);
288 g_free(filename);
289 screen_status_printf(_("Aborted"));
290 return -1;
291 }
293 if (!mpd_run_rm(connection, filename_utf8) ||
294 !mpd_run_save(connection, filename_utf8)) {
295 mpdclient_handle_error(c);
296 g_free(filename_utf8);
297 g_free(filename);
298 return -1;
299 }
300 } else {
301 mpdclient_handle_error(c);
302 g_free(filename_utf8);
303 g_free(filename);
304 return -1;
305 }
306 }
308 c->events |= MPD_IDLE_STORED_PLAYLIST;
310 g_free(filename_utf8);
312 /* success */
313 screen_status_printf(_("Saved %s"), filename);
314 g_free(filename);
315 return 0;
316 }
318 #ifndef NCMPC_MINI
319 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
320 GList **list, struct mpdclient *c)
321 {
322 g_completion_remove_items(gcmp, *list);
323 *list = string_list_remove(*list, dir);
324 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
325 g_completion_add_items(gcmp, *list);
326 *dir_list = g_list_append(*dir_list, g_strdup(dir));
327 }
329 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
330 {
331 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
332 GList **dir_list = tmp->dir_list;
333 GList **list = tmp->list;
334 struct mpdclient *c = tmp->c;
336 if (*list == NULL) {
337 /* create initial list */
338 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
339 g_completion_add_items(gcmp, *list);
340 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
341 string_list_find(*dir_list, line) == NULL) {
342 /* add directory content to list */
343 add_dir(gcmp, line, dir_list, list, c);
344 }
345 }
347 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
348 GList *items, void *data)
349 {
350 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
351 GList **dir_list = tmp->dir_list;
352 GList **list = tmp->list;
353 struct mpdclient *c = tmp->c;
355 if (g_list_length(items) >= 1)
356 screen_display_completion_list(items);
358 if (line && line[0] && line[strlen(line) - 1] == '/' &&
359 string_list_find(*dir_list, line) == NULL) {
360 /* add directory content to list */
361 add_dir(gcmp, line, dir_list, list, c);
362 }
363 }
364 #endif
366 static int
367 handle_add_to_playlist(struct mpdclient *c)
368 {
369 #ifndef NCMPC_MINI
370 /* initialize completion support */
371 GCompletion *gcmp = g_completion_new(NULL);
372 g_completion_set_compare(gcmp, completion_strncmp);
374 GList *list = NULL;
375 GList *dir_list = NULL;
376 completion_callback_data_t data = {
377 .list = &list,
378 .dir_list = &dir_list,
379 .c = c,
380 };
382 wrln_completion_callback_data = &data;
383 wrln_pre_completion_callback = add_pre_completion_cb;
384 wrln_post_completion_callback = add_post_completion_cb;
385 #else
386 GCompletion *gcmp = NULL;
387 #endif
389 /* get path */
390 char *path = screen_readln(_("Add"),
391 NULL,
392 NULL,
393 gcmp);
395 /* destroy completion data */
396 #ifndef NCMPC_MINI
397 wrln_completion_callback_data = NULL;
398 wrln_pre_completion_callback = NULL;
399 wrln_post_completion_callback = NULL;
400 g_completion_free(gcmp);
401 string_list_free(list);
402 string_list_free(dir_list);
403 #endif
405 /* add the path to the playlist */
406 if (path != NULL) {
407 char *path_utf8 = locale_to_utf8(path);
408 mpdclient_cmd_add_path(c, path_utf8);
409 g_free(path_utf8);
410 }
412 g_free(path);
413 return 0;
414 }
416 static void
417 screen_queue_init(WINDOW *w, unsigned cols, unsigned rows)
418 {
419 lw = list_window_init(w, cols, rows);
421 #ifndef NCMPC_MINI
422 if (options.scroll)
423 hscroll_init(&hscroll, w, options.scroll_sep);
424 #endif
425 }
427 static gboolean
428 timer_hide_cursor(gpointer data)
429 {
430 struct mpdclient *c = data;
432 assert(options.hide_cursor > 0);
433 assert(timer_hide_cursor_id != 0);
435 timer_hide_cursor_id = 0;
437 /* hide the cursor when mpd is playing and the user is inactive */
439 if (c->status != NULL &&
440 mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
441 lw->hide_cursor = true;
442 screen_queue_repaint();
443 } else
444 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
445 timer_hide_cursor, c);
447 return FALSE;
448 }
450 static void
451 screen_queue_open(struct mpdclient *c)
452 {
453 playlist = &c->playlist;
455 assert(timer_hide_cursor_id == 0);
456 if (options.hide_cursor > 0) {
457 lw->hide_cursor = false;
458 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
459 timer_hide_cursor, c);
460 }
462 screen_queue_restore_selection();
463 screen_queue_song_change(c->status);
464 }
466 static void
467 screen_queue_close(void)
468 {
469 if (timer_hide_cursor_id != 0) {
470 g_source_remove(timer_hide_cursor_id);
471 timer_hide_cursor_id = 0;
472 }
474 #ifndef NCMPC_MINI
475 if (options.scroll)
476 hscroll_clear(&hscroll);
477 #endif
478 }
480 static void
481 screen_queue_resize(unsigned cols, unsigned rows)
482 {
483 list_window_resize(lw, cols, rows);
484 }
487 static void
488 screen_queue_exit(void)
489 {
490 list_window_free(lw);
491 }
493 static const char *
494 screen_queue_title(char *str, size_t size)
495 {
496 if (options.host == NULL)
497 return _("Queue");
499 g_snprintf(str, size, _("Queue on %s"), options.host);
500 return str;
501 }
503 static void
504 screen_queue_paint_callback(WINDOW *w, unsigned i,
505 unsigned y, unsigned width,
506 bool selected, gcc_unused const void *data)
507 {
508 assert(playlist != NULL);
509 assert(i < playlist_length(playlist));
511 const struct mpd_song *song = playlist_get(playlist, i);
513 struct hscroll *row_hscroll = NULL;
514 #ifndef NCMPC_MINI
515 row_hscroll = selected && options.scroll && lw->selected == i
516 ? &hscroll : NULL;
517 #endif
519 paint_song_row(w, y, width, selected,
520 (int)mpd_song_get_id(song) == current_song_id,
521 song, row_hscroll, options.list_format);
522 }
524 static void
525 screen_queue_paint(void)
526 {
527 #ifndef NCMPC_MINI
528 if (options.scroll)
529 hscroll_clear(&hscroll);
530 #endif
532 list_window_paint2(lw, screen_queue_paint_callback, NULL);
533 }
535 static void
536 screen_queue_update(struct mpdclient *c)
537 {
538 if (c->events & MPD_IDLE_QUEUE)
539 screen_queue_restore_selection();
540 else
541 /* the queue size may have changed, even if we havn't
542 received the QUEUE idle event yet */
543 list_window_set_length(lw, playlist_length(playlist));
545 if (((c->events & MPD_IDLE_PLAYER) != 0 &&
546 screen_queue_song_change(c->status)) ||
547 c->events & MPD_IDLE_QUEUE)
548 /* the queue or the current song has changed, we must
549 paint the new version */
550 screen_queue_paint();
551 }
553 #ifdef HAVE_GETMOUSE
554 static bool
555 handle_mouse_event(struct mpdclient *c)
556 {
557 unsigned long bstate;
558 int row;
559 if (screen_get_mouse_event(c, &bstate, &row) ||
560 list_window_mouse(lw, bstate, row)) {
561 screen_queue_paint();
562 return true;
563 }
565 if (bstate & BUTTON1_DOUBLE_CLICKED) {
566 /* stop */
567 screen_cmd(c, CMD_STOP);
568 return true;
569 }
571 const unsigned old_selected = lw->selected;
572 list_window_set_cursor(lw, lw->start + row);
574 if (bstate & BUTTON1_CLICKED) {
575 /* play */
576 const struct mpd_song *song = screen_queue_selected_song();
577 if (song != NULL) {
578 struct mpd_connection *connection =
579 mpdclient_get_connection(c);
581 if (connection != NULL &&
582 !mpd_run_play_id(connection,
583 mpd_song_get_id(song)))
584 mpdclient_handle_error(c);
585 }
586 } else if (bstate & BUTTON3_CLICKED) {
587 /* delete */
588 if (lw->selected == old_selected)
589 mpdclient_cmd_delete(c, lw->selected);
591 list_window_set_length(lw, playlist_length(playlist));
592 }
594 screen_queue_save_selection();
595 screen_queue_paint();
597 return true;
598 }
599 #endif
601 static bool
602 screen_queue_cmd(struct mpdclient *c, command_t cmd)
603 {
604 struct mpd_connection *connection;
605 static command_t cached_cmd = CMD_NONE;
607 const command_t prev_cmd = cached_cmd;
608 cached_cmd = cmd;
610 lw->hide_cursor = false;
612 if (options.hide_cursor > 0) {
613 if (timer_hide_cursor_id != 0)
614 g_source_remove(timer_hide_cursor_id);
615 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
616 timer_hide_cursor, c);
617 }
619 if (list_window_cmd(lw, cmd)) {
620 screen_queue_save_selection();
621 screen_queue_paint();
622 return true;
623 }
625 switch(cmd) {
626 case CMD_SCREEN_UPDATE:
627 center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
628 screen_queue_paint();
629 return false;
630 case CMD_SELECT_PLAYING:
631 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
632 c->song));
633 screen_queue_save_selection();
634 screen_queue_paint();
635 return true;
637 case CMD_LIST_FIND:
638 case CMD_LIST_RFIND:
639 case CMD_LIST_FIND_NEXT:
640 case CMD_LIST_RFIND_NEXT:
641 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
642 screen_queue_save_selection();
643 screen_queue_paint();
644 return true;
645 case CMD_LIST_JUMP:
646 screen_jump(lw, screen_queue_lw_callback, NULL, NULL, NULL);
647 screen_queue_save_selection();
648 screen_queue_paint();
649 return true;
651 #ifdef HAVE_GETMOUSE
652 case CMD_MOUSE_EVENT:
653 return handle_mouse_event(c);
654 #endif
656 #ifdef ENABLE_SONG_SCREEN
657 case CMD_SCREEN_SONG:
658 if (screen_queue_selected_song() != NULL) {
659 screen_song_switch(c, screen_queue_selected_song());
660 return true;
661 }
663 break;
664 #endif
666 #ifdef ENABLE_LYRICS_SCREEN
667 case CMD_SCREEN_LYRICS:
668 if (lw->selected < playlist_length(&c->playlist)) {
669 struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
670 bool follow = false;
672 if (c->song && selected &&
673 !strcmp(mpd_song_get_uri(selected),
674 mpd_song_get_uri(c->song)))
675 follow = true;
677 screen_lyrics_switch(c, selected, follow);
678 return true;
679 }
681 break;
682 #endif
683 case CMD_SCREEN_SWAP:
684 if (playlist_length(&c->playlist) > 0)
685 screen_swap(c, playlist_get(&c->playlist, lw->selected));
686 else
687 screen_swap(c, NULL);
688 return true;
690 default:
691 break;
692 }
694 if (!mpdclient_is_connected(c))
695 return false;
697 switch(cmd) {
698 const struct mpd_song *song;
699 struct list_window_range range;
701 case CMD_PLAY:
702 song = screen_queue_selected_song();
703 if (song == NULL)
704 return false;
706 connection = mpdclient_get_connection(c);
707 if (connection != NULL &&
708 !mpd_run_play_id(connection, mpd_song_get_id(song)))
709 mpdclient_handle_error(c);
711 return true;
713 case CMD_DELETE:
714 list_window_get_range(lw, &range);
715 mpdclient_cmd_delete_range(c, range.start, range.end);
717 list_window_set_cursor(lw, range.start);
718 return true;
720 case CMD_SAVE_PLAYLIST:
721 playlist_save(c, NULL, NULL);
722 return true;
724 case CMD_ADD:
725 handle_add_to_playlist(c);
726 return true;
728 case CMD_SHUFFLE:
729 list_window_get_range(lw, &range);
730 if (range.end <= range.start + 1)
731 /* No range selection, shuffle all list. */
732 break;
734 connection = mpdclient_get_connection(c);
735 if (connection == NULL)
736 return true;
738 if (mpd_run_shuffle_range(connection, range.start, range.end))
739 screen_status_message(_("Shuffled queue"));
740 else
741 mpdclient_handle_error(c);
742 return true;
744 case CMD_LIST_MOVE_UP:
745 list_window_get_range(lw, &range);
746 if (range.start == 0 || range.end <= range.start)
747 return false;
749 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
750 return true;
752 lw->selected--;
753 lw->range_base--;
755 if (lw->range_selection)
756 list_window_scroll_to(lw, lw->range_base);
757 list_window_scroll_to(lw, lw->selected);
759 screen_queue_save_selection();
760 return true;
762 case CMD_LIST_MOVE_DOWN:
763 list_window_get_range(lw, &range);
764 if (range.end >= playlist_length(&c->playlist))
765 return false;
767 if (!mpdclient_cmd_move(c, range.start, range.end))
768 return true;
770 lw->selected++;
771 lw->range_base++;
773 if (lw->range_selection)
774 list_window_scroll_to(lw, lw->range_base);
775 list_window_scroll_to(lw, lw->selected);
777 screen_queue_save_selection();
778 return true;
780 case CMD_LOCATE:
781 if (screen_queue_selected_song() != NULL) {
782 screen_file_goto_song(c, screen_queue_selected_song());
783 return true;
784 }
786 break;
788 default:
789 break;
790 }
792 return false;
793 }
795 const struct screen_functions screen_queue = {
796 .init = screen_queue_init,
797 .exit = screen_queue_exit,
798 .open = screen_queue_open,
799 .close = screen_queue_close,
800 .resize = screen_queue_resize,
801 .paint = screen_queue_paint,
802 .update = screen_queue_update,
803 .cmd = screen_queue_cmd,
804 .get_title = screen_queue_title,
805 };