01305203e7c91001508b3f9ab403b8b197251dca
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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_message.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"
39 #ifndef NCMPC_MINI
40 #include "hscroll.h"
41 #endif
43 #include <mpd/client.h>
45 #include <ctype.h>
46 #include <string.h>
47 #include <glib.h>
49 #define MAX_SONG_LENGTH 512
51 #ifndef NCMPC_MINI
52 typedef struct
53 {
54 GList **list;
55 GList **dir_list;
56 struct mpdclient *c;
57 } completion_callback_data_t;
59 /*
60 static struct hscroll hscroll;
61 static guint scroll_source_id;
62 */
63 #endif
65 static struct mpdclient_playlist *playlist;
66 static int current_song_id = -1;
67 static int selected_song_id = -1;
68 static struct list_window *lw;
69 static guint timer_hide_cursor_id;
71 static void
72 screen_queue_paint(void);
74 static void
75 screen_queue_repaint(void)
76 {
77 screen_queue_paint();
78 wrefresh(lw->w);
79 }
81 static const struct mpd_song *
82 screen_queue_selected_song(void)
83 {
84 return !lw->range_selection &&
85 lw->selected < playlist_length(playlist)
86 ? playlist_get(playlist, lw->selected)
87 : NULL;
88 }
90 static void
91 screen_queue_save_selection(void)
92 {
93 selected_song_id = screen_queue_selected_song() != NULL
94 ? (int)mpd_song_get_id(screen_queue_selected_song())
95 : -1;
96 }
98 static void
99 screen_queue_restore_selection(void)
100 {
101 const struct mpd_song *song;
102 int pos;
104 list_window_set_length(lw, playlist_length(playlist));
106 if (selected_song_id < 0)
107 /* there was no selection */
108 return;
110 song = screen_queue_selected_song();
111 if (song != NULL &&
112 mpd_song_get_id(song) == (unsigned)selected_song_id)
113 /* selection is still valid */
114 return;
116 pos = playlist_get_index_from_id(playlist, selected_song_id);
117 if (pos >= 0)
118 list_window_set_cursor(lw, pos);
120 screen_queue_save_selection();
121 }
123 /*
124 #ifndef NCMPC_MINI
125 static gboolean
126 scroll_timer_callback(G_GNUC_UNUSED gpointer data)
127 {
128 scroll_source_id = 0;
130 hscroll_step(&hscroll);
131 screen_queue_repaint();
132 return false;
133 }
134 #endif
135 */
137 static const char *
138 screen_queue_lw_callback(unsigned idx, G_GNUC_UNUSED void *data)
139 {
140 static char songname[MAX_SONG_LENGTH];
141 struct mpd_song *song;
143 assert(playlist != NULL);
144 assert(idx < playlist_length(playlist));
146 song = playlist_get(playlist, idx);
148 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
150 /*
151 #ifndef NCMPC_MINI
152 if (idx == lw->selected)
153 {
154 if (options.scroll && utf8_width(songname) > (unsigned)COLS) {
155 static unsigned current_song;
156 char *tmp;
158 if (current_song != lw->selected) {
159 hscroll_reset(&hscroll);
160 current_song = lw->selected;
161 }
163 tmp = strscroll(&hscroll, songname, options.scroll_sep,
164 MAX_SONG_LENGTH);
165 g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
166 g_free(tmp);
168 if (scroll_source_id == 0)
169 scroll_source_id =
170 g_timeout_add(1000,
171 scroll_timer_callback,
172 NULL);
173 } else {
174 hscroll_reset(&hscroll);
176 if (scroll_source_id != 0) {
177 g_source_remove(scroll_source_id);
178 scroll_source_id = 0;
179 }
180 }
181 }
182 #endif
183 */
185 return songname;
186 }
188 static void
189 center_playing_item(struct mpdclient *c, bool center_cursor)
190 {
191 const struct mpd_song *song;
192 unsigned length = c->playlist.list->len;
193 int idx;
195 song = mpdclient_get_current_song(c);
196 if (song == NULL)
197 return;
199 /* try to center the song that are playing */
200 idx = playlist_get_index(&c->playlist, c->song);
201 if (idx < 0)
202 return;
204 if (length < lw->rows)
205 {
206 if (center_cursor)
207 list_window_set_cursor(lw, idx);
208 return;
209 }
211 list_window_center(lw, idx);
213 if (center_cursor) {
214 list_window_set_cursor(lw, idx);
215 return;
216 }
218 /* make sure the cursor is in the window */
219 list_window_fetch_cursor(lw);
220 }
222 #ifndef NCMPC_MINI
223 static void
224 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
225 void *data)
226 {
227 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
228 GList **list = tmp->list;
229 struct mpdclient *c = tmp->c;
231 if( *list == NULL ) {
232 /* create completion list */
233 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
234 g_completion_add_items(gcmp, *list);
235 }
236 }
238 static void
239 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
240 G_GNUC_UNUSED gchar *line, GList *items,
241 G_GNUC_UNUSED void *data)
242 {
243 if (g_list_length(items) >= 1)
244 screen_display_completion_list(items);
245 }
246 #endif
248 #ifndef NCMPC_MINI
249 /**
250 * Wrapper for strncmp(). We are not allowed to pass &strncmp to
251 * g_completion_set_compare(), because strncmp() takes size_t where
252 * g_completion_set_compare passes a gsize value.
253 */
254 static gint
255 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
256 {
257 return strncmp(s1, s2, n);
258 }
259 #endif
261 int
262 playlist_save(struct mpdclient *c, char *name, char *defaultname)
263 {
264 struct mpd_connection *connection;
265 gchar *filename, *filename_utf8;
266 #ifndef NCMPC_MINI
267 GCompletion *gcmp;
268 GList *list = NULL;
269 completion_callback_data_t data;
270 #endif
272 #ifdef NCMPC_MINI
273 (void)defaultname;
274 #endif
276 #ifndef NCMPC_MINI
277 if (name == NULL) {
278 /* initialize completion support */
279 gcmp = g_completion_new(NULL);
280 g_completion_set_compare(gcmp, completion_strncmp);
281 data.list = &list;
282 data.dir_list = NULL;
283 data.c = c;
284 wrln_completion_callback_data = &data;
285 wrln_pre_completion_callback = save_pre_completion_cb;
286 wrln_post_completion_callback = save_post_completion_cb;
289 /* query the user for a filename */
290 filename = screen_readln(_("Save playlist as"),
291 defaultname,
292 NULL,
293 gcmp);
295 /* destroy completion support */
296 wrln_completion_callback_data = NULL;
297 wrln_pre_completion_callback = NULL;
298 wrln_post_completion_callback = NULL;
299 g_completion_free(gcmp);
300 list = string_list_free(list);
301 if( filename )
302 filename=g_strstrip(filename);
303 } else
304 #endif
305 filename=g_strdup(name);
307 if (filename == NULL)
308 return -1;
310 /* send save command to mpd */
312 connection = mpdclient_get_connection(c);
313 if (connection == NULL)
314 return -1;
316 filename_utf8 = locale_to_utf8(filename);
317 if (!mpd_run_save(connection, filename_utf8)) {
318 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
319 mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
320 mpd_connection_clear_error(connection)) {
321 char *buf;
322 int key;
324 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
325 filename, YES, NO);
326 key = tolower(screen_getch(buf));
327 g_free(buf);
329 if (key != YES[0]) {
330 g_free(filename_utf8);
331 g_free(filename);
332 screen_status_printf(_("Aborted"));
333 return -1;
334 }
336 if (!mpd_run_rm(connection, filename_utf8) ||
337 !mpd_run_save(connection, filename_utf8)) {
338 mpdclient_handle_error(c);
339 g_free(filename_utf8);
340 g_free(filename);
341 return -1;
342 }
343 } else {
344 mpdclient_handle_error(c);
345 g_free(filename_utf8);
346 g_free(filename);
347 return -1;
348 }
349 }
351 c->events |= MPD_IDLE_STORED_PLAYLIST;
353 g_free(filename_utf8);
355 /* success */
356 screen_status_printf(_("Saved %s"), filename);
357 g_free(filename);
358 return 0;
359 }
361 #ifndef NCMPC_MINI
362 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
363 GList **list, struct mpdclient *c)
364 {
365 g_completion_remove_items(gcmp, *list);
366 *list = string_list_remove(*list, dir);
367 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
368 g_completion_add_items(gcmp, *list);
369 *dir_list = g_list_append(*dir_list, g_strdup(dir));
370 }
372 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
373 {
374 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
375 GList **dir_list = tmp->dir_list;
376 GList **list = tmp->list;
377 struct mpdclient *c = tmp->c;
379 if (*list == NULL) {
380 /* create initial list */
381 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
382 g_completion_add_items(gcmp, *list);
383 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
384 string_list_find(*dir_list, line) == NULL) {
385 /* add directory content to list */
386 add_dir(gcmp, line, dir_list, list, c);
387 }
388 }
390 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
391 GList *items, void *data)
392 {
393 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
394 GList **dir_list = tmp->dir_list;
395 GList **list = tmp->list;
396 struct mpdclient *c = tmp->c;
398 if (g_list_length(items) >= 1)
399 screen_display_completion_list(items);
401 if (line && line[0] && line[strlen(line) - 1] == '/' &&
402 string_list_find(*dir_list, line) == NULL) {
403 /* add directory content to list */
404 add_dir(gcmp, line, dir_list, list, c);
405 }
406 }
407 #endif
409 static int
410 handle_add_to_playlist(struct mpdclient *c)
411 {
412 gchar *path;
413 #ifndef NCMPC_MINI
414 GCompletion *gcmp;
415 GList *list = NULL;
416 GList *dir_list = NULL;
417 completion_callback_data_t data;
419 /* initialize completion support */
420 gcmp = g_completion_new(NULL);
421 g_completion_set_compare(gcmp, completion_strncmp);
422 data.list = &list;
423 data.dir_list = &dir_list;
424 data.c = c;
425 wrln_completion_callback_data = &data;
426 wrln_pre_completion_callback = add_pre_completion_cb;
427 wrln_post_completion_callback = add_post_completion_cb;
428 #endif
430 /* get path */
431 path = screen_readln(_("Add"),
432 NULL,
433 NULL,
434 #ifdef NCMPC_MINI
435 NULL
436 #else
437 gcmp
438 #endif
439 );
441 /* destroy completion data */
442 #ifndef NCMPC_MINI
443 wrln_completion_callback_data = NULL;
444 wrln_pre_completion_callback = NULL;
445 wrln_post_completion_callback = NULL;
446 g_completion_free(gcmp);
447 string_list_free(list);
448 string_list_free(dir_list);
449 #endif
451 /* add the path to the playlist */
452 if (path != NULL) {
453 char *path_utf8 = locale_to_utf8(path);
454 mpdclient_cmd_add_path(c, path_utf8);
455 g_free(path_utf8);
456 }
458 g_free(path);
459 return 0;
460 }
462 static void
463 screen_queue_init(WINDOW *w, int cols, int rows)
464 {
465 lw = list_window_init(w, cols, rows);
466 }
468 static gboolean
469 timer_hide_cursor(gpointer data)
470 {
471 struct mpdclient *c = data;
473 assert(options.hide_cursor > 0);
474 assert(timer_hide_cursor_id != 0);
476 timer_hide_cursor_id = 0;
478 /* hide the cursor when mpd is playing and the user is inactive */
480 if (c->status != NULL &&
481 mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
482 lw->hide_cursor = true;
483 screen_queue_repaint();
484 } else
485 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
486 timer_hide_cursor, c);
488 return FALSE;
489 }
491 static void
492 screen_queue_open(struct mpdclient *c)
493 {
494 playlist = &c->playlist;
496 assert(timer_hide_cursor_id == 0);
497 if (options.hide_cursor > 0) {
498 lw->hide_cursor = false;
499 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
500 timer_hide_cursor, c);
501 }
503 screen_queue_restore_selection();
504 }
506 static void
507 screen_queue_close(void)
508 {
509 if (timer_hide_cursor_id != 0) {
510 g_source_remove(timer_hide_cursor_id);
511 timer_hide_cursor_id = 0;
512 }
513 }
515 static void
516 screen_queue_resize(int cols, int rows)
517 {
518 list_window_resize(lw, cols, rows);
519 }
522 static void
523 screen_queue_exit(void)
524 {
525 list_window_free(lw);
526 }
528 static const char *
529 screen_queue_title(char *str, size_t size)
530 {
531 if (options.host == NULL)
532 return _("Playlist");
534 g_snprintf(str, size, _("Playlist on %s"), options.host);
535 return str;
536 }
538 static void
539 screen_queue_paint_callback(WINDOW *w, unsigned i,
540 unsigned y, unsigned width,
541 bool selected, G_GNUC_UNUSED void *data)
542 {
543 const struct mpd_song *song;
545 assert(playlist != NULL);
546 assert(i < playlist_length(playlist));
548 song = playlist_get(playlist, i);
550 paint_song_row(w, y, width, selected,
551 (int)mpd_song_get_id(song) == current_song_id,
552 song);
553 }
555 static void
556 screen_queue_paint(void)
557 {
558 list_window_paint2(lw, screen_queue_paint_callback, NULL);
559 }
561 G_GNUC_PURE
562 static int
563 get_current_song_id(const struct mpd_status *status)
564 {
565 return status != NULL &&
566 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
567 mpd_status_get_state(status) == MPD_STATE_PAUSE)
568 ? (int)mpd_status_get_song_id(status)
569 : -1;
570 }
572 static void
573 screen_queue_update(struct mpdclient *c)
574 {
575 if (c->events & MPD_IDLE_PLAYLIST)
576 screen_queue_restore_selection();
578 if ((c->events & MPD_IDLE_PLAYER) != 0 &&
579 get_current_song_id(c->status) != current_song_id) {
580 current_song_id = get_current_song_id(c->status);
582 /* center the cursor */
583 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
584 center_playing_item(c, false);
586 screen_queue_repaint();
587 } else if (c->events & MPD_IDLE_PLAYLIST) {
588 /* the playlist has changed, we must paint the new
589 version */
590 screen_queue_repaint();
591 }
592 }
594 #ifdef HAVE_GETMOUSE
595 static bool
596 handle_mouse_event(struct mpdclient *c)
597 {
598 int row;
599 unsigned long bstate;
600 unsigned old_selected;
602 if (screen_get_mouse_event(c, &bstate, &row) ||
603 list_window_mouse(lw, bstate, row)) {
604 screen_queue_repaint();
605 return true;
606 }
608 if (bstate & BUTTON1_DOUBLE_CLICKED) {
609 /* stop */
610 screen_cmd(c, CMD_STOP);
611 return true;
612 }
614 old_selected = lw->selected;
615 list_window_set_cursor(lw, lw->start + row);
617 if (bstate & BUTTON1_CLICKED) {
618 /* play */
619 const struct mpd_song *song = screen_queue_selected_song();
620 if (song != NULL) {
621 struct mpd_connection *connection =
622 mpdclient_get_connection(c);
624 if (connection != NULL &&
625 !mpd_run_play_id(connection,
626 mpd_song_get_id(song)))
627 mpdclient_handle_error(c);
628 }
629 } else if (bstate & BUTTON3_CLICKED) {
630 /* delete */
631 if (lw->selected == old_selected)
632 mpdclient_cmd_delete(c, lw->selected);
634 list_window_set_length(lw, playlist_length(playlist));
635 }
637 screen_queue_save_selection();
638 screen_queue_repaint();
640 return true;
641 }
642 #endif
644 static bool
645 screen_queue_cmd(struct mpdclient *c, command_t cmd)
646 {
647 struct mpd_connection *connection;
648 static command_t cached_cmd = CMD_NONE;
649 command_t prev_cmd = cached_cmd;
650 struct list_window_range range;
651 const struct mpd_song *song;
653 cached_cmd = cmd;
655 lw->hide_cursor = false;
657 if (options.hide_cursor > 0) {
658 if (timer_hide_cursor_id != 0)
659 g_source_remove(timer_hide_cursor_id);
660 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
661 timer_hide_cursor, c);
662 }
664 if (list_window_cmd(lw, cmd)) {
665 screen_queue_save_selection();
666 screen_queue_repaint();
667 return true;
668 }
670 switch(cmd) {
671 case CMD_SCREEN_UPDATE:
672 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
673 screen_queue_repaint();
674 return false;
675 case CMD_SELECT_PLAYING:
676 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
677 c->song));
678 screen_queue_save_selection();
679 screen_queue_repaint();
680 return true;
682 case CMD_LIST_FIND:
683 case CMD_LIST_RFIND:
684 case CMD_LIST_FIND_NEXT:
685 case CMD_LIST_RFIND_NEXT:
686 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
687 screen_queue_save_selection();
688 screen_queue_repaint();
689 return true;
690 case CMD_LIST_JUMP:
691 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
692 screen_queue_save_selection();
693 screen_queue_repaint();
694 return true;
696 #ifdef HAVE_GETMOUSE
697 case CMD_MOUSE_EVENT:
698 return handle_mouse_event(c);
699 #endif
701 #ifdef ENABLE_SONG_SCREEN
702 case CMD_SCREEN_SONG:
703 if (screen_queue_selected_song() != NULL) {
704 screen_song_switch(c, screen_queue_selected_song());
705 return true;
706 }
708 break;
709 #endif
711 #ifdef ENABLE_LYRICS_SCREEN
712 case CMD_SCREEN_LYRICS:
713 if (lw->selected < playlist_length(&c->playlist)) {
714 struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
715 bool follow = false;
717 if (c->song && selected &&
718 !strcmp(mpd_song_get_uri(selected),
719 mpd_song_get_uri(c->song)))
720 follow = true;
722 screen_lyrics_switch(c, selected, follow);
723 return true;
724 }
726 break;
727 #endif
728 case CMD_SCREEN_SWAP:
729 screen_swap(c, playlist_get(&c->playlist, lw->selected));
730 return true;
732 default:
733 break;
734 }
736 if (!mpdclient_is_connected(c))
737 return false;
739 switch(cmd) {
740 case CMD_PLAY:
741 song = screen_queue_selected_song();
742 if (song == NULL)
743 return false;
745 connection = mpdclient_get_connection(c);
746 if (connection != NULL &&
747 !mpd_run_play_id(connection, mpd_song_get_id(song)))
748 mpdclient_handle_error(c);
750 return true;
752 case CMD_DELETE:
753 list_window_get_range(lw, &range);
754 mpdclient_cmd_delete_range(c, range.start, range.end);
756 list_window_set_cursor(lw, range.start);
757 return true;
759 case CMD_SAVE_PLAYLIST:
760 playlist_save(c, NULL, NULL);
761 return true;
763 case CMD_ADD:
764 handle_add_to_playlist(c);
765 return true;
767 case CMD_SHUFFLE:
768 list_window_get_range(lw, &range);
769 if (range.end < range.start + 1)
770 /* No range selection, shuffle all list. */
771 break;
773 connection = mpdclient_get_connection(c);
774 if (connection == NULL)
775 return true;
777 if (mpd_run_shuffle_range(connection, range.start, range.end))
778 screen_status_message(_("Shuffled playlist"));
779 else
780 mpdclient_handle_error(c);
781 return true;
783 case CMD_LIST_MOVE_UP:
784 list_window_get_range(lw, &range);
785 if (range.start == 0 || range.end <= range.start)
786 return false;
788 for (unsigned i = range.start; i < range.end; ++i)
789 mpdclient_cmd_move(c, i, i - 1);
791 lw->selected--;
792 lw->range_base--;
794 screen_queue_save_selection();
795 return true;
797 case CMD_LIST_MOVE_DOWN:
798 list_window_get_range(lw, &range);
799 if (range.end >= playlist_length(&c->playlist))
800 return false;
802 for (int i = range.end - 1; i >= (int)range.start; --i)
803 mpdclient_cmd_move(c, i, i + 1);
805 lw->selected++;
806 lw->range_base++;
808 screen_queue_save_selection();
809 return true;
811 case CMD_LOCATE:
812 if (screen_queue_selected_song() != NULL) {
813 screen_file_goto_song(c, screen_queue_selected_song());
814 return true;
815 }
817 break;
819 default:
820 break;
821 }
823 return false;
824 }
826 const struct screen_functions screen_queue = {
827 .init = screen_queue_init,
828 .exit = screen_queue_exit,
829 .open = screen_queue_open,
830 .close = screen_queue_close,
831 .resize = screen_queue_resize,
832 .paint = screen_queue_paint,
833 .update = screen_queue_update,
834 .cmd = screen_queue_cmd,
835 .get_title = screen_queue_title,
836 };