a5383fa19927d6536db98f7f60cc0aff7450415a
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();
111 }
113 static const char *
114 screen_queue_lw_callback(unsigned idx, gcc_unused void *data)
115 {
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;
126 }
128 static void
129 center_playing_item(const struct mpd_status *status, bool center_cursor)
130 {
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);
150 }
152 gcc_pure
153 static int
154 get_current_song_id(const struct mpd_status *status)
155 {
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;
161 }
163 static bool
164 screen_queue_song_change(const struct mpd_status *status)
165 {
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;
176 }
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)
186 {
187 return strncmp(s1, s2, n);
188 }
189 #endif
191 #ifndef NCMPC_MINI
192 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
193 GList **list, struct mpdclient *c)
194 {
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));
200 }
202 struct completion_callback_data {
203 GList **list;
204 GList **dir_list;
205 struct mpdclient *c;
206 };
208 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
209 {
210 struct completion_callback_data *tmp = data;
211 GList **dir_list = tmp->dir_list;
212 GList **list = tmp->list;
213 struct mpdclient *c = tmp->c;
215 if (*list == NULL) {
216 /* create initial list */
217 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
218 g_completion_add_items(gcmp, *list);
219 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
220 string_list_find(*dir_list, line) == NULL) {
221 /* add directory content to list */
222 add_dir(gcmp, line, dir_list, list, c);
223 }
224 }
226 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
227 GList *items, void *data)
228 {
229 struct completion_callback_data *tmp = data;
230 GList **dir_list = tmp->dir_list;
231 GList **list = tmp->list;
232 struct mpdclient *c = tmp->c;
234 if (g_list_length(items) >= 1)
235 screen_display_completion_list(items);
237 if (line && line[0] && line[strlen(line) - 1] == '/' &&
238 string_list_find(*dir_list, line) == NULL) {
239 /* add directory content to list */
240 add_dir(gcmp, line, dir_list, list, c);
241 }
242 }
243 #endif
245 static int
246 handle_add_to_playlist(struct mpdclient *c)
247 {
248 #ifndef NCMPC_MINI
249 /* initialize completion support */
250 GCompletion *gcmp = g_completion_new(NULL);
251 g_completion_set_compare(gcmp, completion_strncmp);
253 GList *list = NULL;
254 GList *dir_list = NULL;
255 struct completion_callback_data data = {
256 .list = &list,
257 .dir_list = &dir_list,
258 .c = c,
259 };
261 wrln_completion_callback_data = &data;
262 wrln_pre_completion_callback = add_pre_completion_cb;
263 wrln_post_completion_callback = add_post_completion_cb;
264 #else
265 GCompletion *gcmp = NULL;
266 #endif
268 /* get path */
269 char *path = screen_readln(_("Add"),
270 NULL,
271 NULL,
272 gcmp);
274 /* destroy completion data */
275 #ifndef NCMPC_MINI
276 wrln_completion_callback_data = NULL;
277 wrln_pre_completion_callback = NULL;
278 wrln_post_completion_callback = NULL;
279 g_completion_free(gcmp);
280 string_list_free(list);
281 string_list_free(dir_list);
282 #endif
284 /* add the path to the playlist */
285 if (path != NULL) {
286 char *path_utf8 = locale_to_utf8(path);
287 mpdclient_cmd_add_path(c, path_utf8);
288 g_free(path_utf8);
289 }
291 g_free(path);
292 return 0;
293 }
295 static void
296 screen_queue_init(WINDOW *w, unsigned cols, unsigned rows)
297 {
298 lw = list_window_init(w, cols, rows);
300 #ifndef NCMPC_MINI
301 if (options.scroll)
302 hscroll_init(&hscroll, w, options.scroll_sep);
303 #endif
304 }
306 static gboolean
307 timer_hide_cursor(gpointer data)
308 {
309 struct mpdclient *c = data;
311 assert(options.hide_cursor > 0);
312 assert(timer_hide_cursor_id != 0);
314 timer_hide_cursor_id = 0;
316 /* hide the cursor when mpd is playing and the user is inactive */
318 if (c->status != NULL &&
319 mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
320 lw->hide_cursor = true;
321 screen_queue_repaint();
322 } else
323 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
324 timer_hide_cursor, c);
326 return FALSE;
327 }
329 static void
330 screen_queue_open(struct mpdclient *c)
331 {
332 playlist = &c->playlist;
334 assert(timer_hide_cursor_id == 0);
335 if (options.hide_cursor > 0) {
336 lw->hide_cursor = false;
337 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
338 timer_hide_cursor, c);
339 }
341 screen_queue_restore_selection();
342 screen_queue_song_change(c->status);
343 }
345 static void
346 screen_queue_close(void)
347 {
348 if (timer_hide_cursor_id != 0) {
349 g_source_remove(timer_hide_cursor_id);
350 timer_hide_cursor_id = 0;
351 }
353 #ifndef NCMPC_MINI
354 if (options.scroll)
355 hscroll_clear(&hscroll);
356 #endif
357 }
359 static void
360 screen_queue_resize(unsigned cols, unsigned rows)
361 {
362 list_window_resize(lw, cols, rows);
363 }
366 static void
367 screen_queue_exit(void)
368 {
369 list_window_free(lw);
370 }
372 /**
373 * Extract the host portion (without the optional password) from the
374 * MPD_HOST string.
375 */
376 static const char *
377 host_without_password(const char *host)
378 {
379 const char *separator = strchr(host, '@');
380 if (separator != NULL && separator != host && separator[1] != 0)
381 host = separator + 1;
383 return host;
384 }
386 static const char *
387 screen_queue_title(char *str, size_t size)
388 {
389 if (options.host == NULL)
390 return _("Queue");
392 g_snprintf(str, size, _("Queue on %s"),
393 host_without_password(options.host));
394 return str;
395 }
397 static void
398 screen_queue_paint_callback(WINDOW *w, unsigned i,
399 unsigned y, unsigned width,
400 bool selected, gcc_unused const void *data)
401 {
402 assert(playlist != NULL);
403 assert(i < playlist_length(playlist));
405 const struct mpd_song *song = playlist_get(playlist, i);
407 struct hscroll *row_hscroll = NULL;
408 #ifndef NCMPC_MINI
409 row_hscroll = selected && options.scroll && lw->selected == i
410 ? &hscroll : NULL;
411 #endif
413 paint_song_row(w, y, width, selected,
414 (int)mpd_song_get_id(song) == current_song_id,
415 song, row_hscroll, options.list_format);
416 }
418 static void
419 screen_queue_paint(void)
420 {
421 #ifndef NCMPC_MINI
422 if (options.scroll)
423 hscroll_clear(&hscroll);
424 #endif
426 list_window_paint2(lw, screen_queue_paint_callback, NULL);
427 }
429 static void
430 screen_queue_update(struct mpdclient *c)
431 {
432 if (c->events & MPD_IDLE_QUEUE)
433 screen_queue_restore_selection();
434 else
435 /* the queue size may have changed, even if we havn't
436 received the QUEUE idle event yet */
437 list_window_set_length(lw, playlist_length(playlist));
439 if (((c->events & MPD_IDLE_PLAYER) != 0 &&
440 screen_queue_song_change(c->status)) ||
441 c->events & MPD_IDLE_QUEUE)
442 /* the queue or the current song has changed, we must
443 paint the new version */
444 screen_queue_paint();
445 }
447 #ifdef HAVE_GETMOUSE
448 static bool
449 handle_mouse_event(struct mpdclient *c)
450 {
451 unsigned long bstate;
452 int row;
453 if (screen_get_mouse_event(c, &bstate, &row) ||
454 list_window_mouse(lw, bstate, row)) {
455 screen_queue_paint();
456 return true;
457 }
459 if (bstate & BUTTON1_DOUBLE_CLICKED) {
460 /* stop */
461 screen_cmd(c, CMD_STOP);
462 return true;
463 }
465 const unsigned old_selected = lw->selected;
466 list_window_set_cursor(lw, lw->start + row);
468 if (bstate & BUTTON1_CLICKED) {
469 /* play */
470 const struct mpd_song *song = screen_queue_selected_song();
471 if (song != NULL) {
472 struct mpd_connection *connection =
473 mpdclient_get_connection(c);
475 if (connection != NULL &&
476 !mpd_run_play_id(connection,
477 mpd_song_get_id(song)))
478 mpdclient_handle_error(c);
479 }
480 } else if (bstate & BUTTON3_CLICKED) {
481 /* delete */
482 if (lw->selected == old_selected)
483 mpdclient_cmd_delete(c, lw->selected);
485 list_window_set_length(lw, playlist_length(playlist));
486 }
488 screen_queue_save_selection();
489 screen_queue_paint();
491 return true;
492 }
493 #endif
495 static bool
496 screen_queue_cmd(struct mpdclient *c, command_t cmd)
497 {
498 struct mpd_connection *connection;
499 static command_t cached_cmd = CMD_NONE;
501 const command_t prev_cmd = cached_cmd;
502 cached_cmd = cmd;
504 lw->hide_cursor = false;
506 if (options.hide_cursor > 0) {
507 if (timer_hide_cursor_id != 0)
508 g_source_remove(timer_hide_cursor_id);
509 timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
510 timer_hide_cursor, c);
511 }
513 if (list_window_cmd(lw, cmd)) {
514 screen_queue_save_selection();
515 screen_queue_paint();
516 return true;
517 }
519 switch(cmd) {
520 case CMD_SCREEN_UPDATE:
521 center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
522 screen_queue_paint();
523 return false;
524 case CMD_SELECT_PLAYING:
525 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
526 c->song));
527 screen_queue_save_selection();
528 screen_queue_paint();
529 return true;
531 case CMD_LIST_FIND:
532 case CMD_LIST_RFIND:
533 case CMD_LIST_FIND_NEXT:
534 case CMD_LIST_RFIND_NEXT:
535 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
536 screen_queue_save_selection();
537 screen_queue_paint();
538 return true;
539 case CMD_LIST_JUMP:
540 screen_jump(lw, screen_queue_lw_callback, NULL, NULL, NULL);
541 screen_queue_save_selection();
542 screen_queue_paint();
543 return true;
545 #ifdef HAVE_GETMOUSE
546 case CMD_MOUSE_EVENT:
547 return handle_mouse_event(c);
548 #endif
550 #ifdef ENABLE_SONG_SCREEN
551 case CMD_SCREEN_SONG:
552 if (screen_queue_selected_song() != NULL) {
553 screen_song_switch(c, screen_queue_selected_song());
554 return true;
555 }
557 break;
558 #endif
560 #ifdef ENABLE_LYRICS_SCREEN
561 case CMD_SCREEN_LYRICS:
562 if (lw->selected < playlist_length(&c->playlist)) {
563 struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
564 bool follow = false;
566 if (c->song && selected &&
567 !strcmp(mpd_song_get_uri(selected),
568 mpd_song_get_uri(c->song)))
569 follow = true;
571 screen_lyrics_switch(c, selected, follow);
572 return true;
573 }
575 break;
576 #endif
577 case CMD_SCREEN_SWAP:
578 if (playlist_length(&c->playlist) > 0)
579 screen_swap(c, playlist_get(&c->playlist, lw->selected));
580 else
581 screen_swap(c, NULL);
582 return true;
584 default:
585 break;
586 }
588 if (!mpdclient_is_connected(c))
589 return false;
591 switch(cmd) {
592 const struct mpd_song *song;
593 struct list_window_range range;
595 case CMD_PLAY:
596 song = screen_queue_selected_song();
597 if (song == NULL)
598 return false;
600 connection = mpdclient_get_connection(c);
601 if (connection != NULL &&
602 !mpd_run_play_id(connection, mpd_song_get_id(song)))
603 mpdclient_handle_error(c);
605 return true;
607 case CMD_DELETE:
608 list_window_get_range(lw, &range);
609 mpdclient_cmd_delete_range(c, range.start, range.end);
611 list_window_set_cursor(lw, range.start);
612 return true;
614 case CMD_SAVE_PLAYLIST:
615 playlist_save(c, NULL, NULL);
616 return true;
618 case CMD_ADD:
619 handle_add_to_playlist(c);
620 return true;
622 case CMD_SHUFFLE:
623 list_window_get_range(lw, &range);
624 if (range.end <= range.start + 1)
625 /* No range selection, shuffle all list. */
626 break;
628 connection = mpdclient_get_connection(c);
629 if (connection == NULL)
630 return true;
632 if (mpd_run_shuffle_range(connection, range.start, range.end))
633 screen_status_message(_("Shuffled queue"));
634 else
635 mpdclient_handle_error(c);
636 return true;
638 case CMD_LIST_MOVE_UP:
639 list_window_get_range(lw, &range);
640 if (range.start == 0 || range.end <= range.start)
641 return false;
643 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
644 return true;
646 lw->selected--;
647 lw->range_base--;
649 if (lw->range_selection)
650 list_window_scroll_to(lw, lw->range_base);
651 list_window_scroll_to(lw, lw->selected);
653 screen_queue_save_selection();
654 return true;
656 case CMD_LIST_MOVE_DOWN:
657 list_window_get_range(lw, &range);
658 if (range.end >= playlist_length(&c->playlist))
659 return false;
661 if (!mpdclient_cmd_move(c, range.start, range.end))
662 return true;
664 lw->selected++;
665 lw->range_base++;
667 if (lw->range_selection)
668 list_window_scroll_to(lw, lw->range_base);
669 list_window_scroll_to(lw, lw->selected);
671 screen_queue_save_selection();
672 return true;
674 case CMD_LOCATE:
675 if (screen_queue_selected_song() != NULL) {
676 screen_file_goto_song(c, screen_queue_selected_song());
677 return true;
678 }
680 break;
682 default:
683 break;
684 }
686 return false;
687 }
689 const struct screen_functions screen_queue = {
690 .init = screen_queue_init,
691 .exit = screen_queue_exit,
692 .open = screen_queue_open,
693 .close = screen_queue_close,
694 .resize = screen_queue_resize,
695 .paint = screen_queue_paint,
696 .update = screen_queue_update,
697 .cmd = screen_queue_cmd,
698 .get_title = screen_queue_title,
699 };