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 static struct hscroll hscroll;
60 #endif
62 static struct mpdclient_playlist *playlist;
63 static int current_song_id = -1;
64 static int selected_song_id = -1;
65 static struct list_window *lw;
66 static guint timer_hide_cursor_id;
68 static void
69 screen_queue_paint(void);
71 static void
72 screen_queue_repaint(void)
73 {
74 screen_queue_paint();
75 wrefresh(lw->w);
76 }
78 static const struct mpd_song *
79 screen_queue_selected_song(void)
80 {
81 return !lw->range_selection &&
82 lw->selected < playlist_length(playlist)
83 ? playlist_get(playlist, lw->selected)
84 : NULL;
85 }
87 static void
88 screen_queue_save_selection(void)
89 {
90 selected_song_id = screen_queue_selected_song() != NULL
91 ? (int)mpd_song_get_id(screen_queue_selected_song())
92 : -1;
93 }
95 static void
96 screen_queue_restore_selection(void)
97 {
98 const struct mpd_song *song;
99 int pos;
101 list_window_set_length(lw, playlist_length(playlist));
103 if (selected_song_id < 0)
104 /* there was no selection */
105 return;
107 song = screen_queue_selected_song();
108 if (song != NULL &&
109 mpd_song_get_id(song) == (unsigned)selected_song_id)
110 /* selection is still valid */
111 return;
113 pos = playlist_get_index_from_id(playlist, selected_song_id);
114 if (pos >= 0)
115 list_window_set_cursor(lw, pos);
117 screen_queue_save_selection();
118 }
120 static const char *
121 screen_queue_lw_callback(unsigned idx, G_GNUC_UNUSED void *data)
122 {
123 static char songname[MAX_SONG_LENGTH];
124 struct mpd_song *song;
126 assert(playlist != NULL);
127 assert(idx < playlist_length(playlist));
129 song = playlist_get(playlist, idx);
131 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
133 return songname;
134 }
136 static void
137 center_playing_item(struct mpdclient *c, bool center_cursor)
138 {
139 unsigned length = c->playlist.list->len;
140 int idx;
142 /* try to center the song that are playing */
143 idx = playlist_get_index(&c->playlist, c->song);
144 if (idx < 0)
145 return;
147 if (length < lw->rows)
148 {
149 if (center_cursor)
150 list_window_set_cursor(lw, idx);
151 return;
152 }
154 list_window_center(lw, idx);
156 if (center_cursor) {
157 list_window_set_cursor(lw, idx);
158 return;
159 }
161 /* make sure the cursor is in the window */
162 list_window_fetch_cursor(lw);
163 }
165 #ifndef NCMPC_MINI
166 static void
167 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
168 void *data)
169 {
170 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
171 GList **list = tmp->list;
172 struct mpdclient *c = tmp->c;
174 if( *list == NULL ) {
175 /* create completion list */
176 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
177 g_completion_add_items(gcmp, *list);
178 }
179 }
181 static void
182 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
183 G_GNUC_UNUSED gchar *line, GList *items,
184 G_GNUC_UNUSED void *data)
185 {
186 if (g_list_length(items) >= 1)
187 screen_display_completion_list(items);
188 }
189 #endif
191 #ifndef NCMPC_MINI
192 /**
193 * Wrapper for strncmp(). We are not allowed to pass &strncmp to
194 * g_completion_set_compare(), because strncmp() takes size_t where
195 * g_completion_set_compare passes a gsize value.
196 */
197 static gint
198 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
199 {
200 return strncmp(s1, s2, n);
201 }
202 #endif
204 int
205 playlist_save(struct mpdclient *c, char *name, char *defaultname)
206 {
207 struct mpd_connection *connection;
208 gchar *filename, *filename_utf8;
209 #ifndef NCMPC_MINI
210 GCompletion *gcmp;
211 GList *list = NULL;
212 completion_callback_data_t data;
213 #endif
215 #ifdef NCMPC_MINI
216 (void)defaultname;
217 #endif
219 #ifndef NCMPC_MINI
220 if (name == NULL) {
221 /* initialize completion support */
222 gcmp = g_completion_new(NULL);
223 g_completion_set_compare(gcmp, completion_strncmp);
224 data.list = &list;
225 data.dir_list = NULL;
226 data.c = c;
227 wrln_completion_callback_data = &data;
228 wrln_pre_completion_callback = save_pre_completion_cb;
229 wrln_post_completion_callback = save_post_completion_cb;
232 /* query the user for a filename */
233 filename = screen_readln(_("Save playlist as"),
234 defaultname,
235 NULL,
236 gcmp);
238 /* destroy completion support */
239 wrln_completion_callback_data = NULL;
240 wrln_pre_completion_callback = NULL;
241 wrln_post_completion_callback = NULL;
242 g_completion_free(gcmp);
243 list = string_list_free(list);
244 if( filename )
245 filename=g_strstrip(filename);
246 } else
247 #endif
248 filename=g_strdup(name);
250 if (filename == NULL)
251 return -1;
253 /* send save command to mpd */
255 connection = mpdclient_get_connection(c);
256 if (connection == NULL)
257 return -1;
259 filename_utf8 = locale_to_utf8(filename);
260 if (!mpd_run_save(connection, filename_utf8)) {
261 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
262 mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
263 mpd_connection_clear_error(connection)) {
264 char *buf;
265 int key;
267 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
268 filename, YES, NO);
269 key = tolower(screen_getch(buf));
270 g_free(buf);
272 if (key != YES[0]) {
273 g_free(filename_utf8);
274 g_free(filename);
275 screen_status_printf(_("Aborted"));
276 return -1;
277 }
279 if (!mpd_run_rm(connection, filename_utf8) ||
280 !mpd_run_save(connection, filename_utf8)) {
281 mpdclient_handle_error(c);
282 g_free(filename_utf8);
283 g_free(filename);
284 return -1;
285 }
286 } else {
287 mpdclient_handle_error(c);
288 g_free(filename_utf8);
289 g_free(filename);
290 return -1;
291 }
292 }
294 c->events |= MPD_IDLE_STORED_PLAYLIST;
296 g_free(filename_utf8);
298 /* success */
299 screen_status_printf(_("Saved %s"), filename);
300 g_free(filename);
301 return 0;
302 }
304 #ifndef NCMPC_MINI
305 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
306 GList **list, struct mpdclient *c)
307 {
308 g_completion_remove_items(gcmp, *list);
309 *list = string_list_remove(*list, dir);
310 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
311 g_completion_add_items(gcmp, *list);
312 *dir_list = g_list_append(*dir_list, g_strdup(dir));
313 }
315 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
316 {
317 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
318 GList **dir_list = tmp->dir_list;
319 GList **list = tmp->list;
320 struct mpdclient *c = tmp->c;
322 if (*list == NULL) {
323 /* create initial list */
324 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
325 g_completion_add_items(gcmp, *list);
326 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
327 string_list_find(*dir_list, line) == NULL) {
328 /* add directory content to list */
329 add_dir(gcmp, line, dir_list, list, c);
330 }
331 }
333 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
334 GList *items, void *data)
335 {
336 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
337 GList **dir_list = tmp->dir_list;
338 GList **list = tmp->list;
339 struct mpdclient *c = tmp->c;
341 if (g_list_length(items) >= 1)
342 screen_display_completion_list(items);
344 if (line && line[0] && line[strlen(line) - 1] == '/' &&
345 string_list_find(*dir_list, line) == NULL) {
346 /* add directory content to list */
347 add_dir(gcmp, line, dir_list, list, c);
348 }
349 }
350 #endif
352 static int
353 handle_add_to_playlist(struct mpdclient *c)
354 {
355 gchar *path;
356 #ifndef NCMPC_MINI
357 GCompletion *gcmp;
358 GList *list = NULL;
359 GList *dir_list = NULL;
360 completion_callback_data_t data;
362 /* initialize completion support */
363 gcmp = g_completion_new(NULL);
364 g_completion_set_compare(gcmp, completion_strncmp);
365 data.list = &list;
366 data.dir_list = &dir_list;
367 data.c = c;
368 wrln_completion_callback_data = &data;
369 wrln_pre_completion_callback = add_pre_completion_cb;
370 wrln_post_completion_callback = add_post_completion_cb;
371 #endif
373 /* get path */
374 path = screen_readln(_("Add"),
375 NULL,
376 NULL,
377 #ifdef NCMPC_MINI
378 NULL
379 #else
380 gcmp
381 #endif
382 );
384 /* destroy completion data */
385 #ifndef NCMPC_MINI
386 wrln_completion_callback_data = NULL;
387 wrln_pre_completion_callback = NULL;
388 wrln_post_completion_callback = NULL;
389 g_completion_free(gcmp);
390 string_list_free(list);
391 string_list_free(dir_list);
392 #endif
394 /* add the path to the playlist */
395 if (path != NULL) {
396 char *path_utf8 = locale_to_utf8(path);
397 mpdclient_cmd_add_path(c, path_utf8);
398 g_free(path_utf8);
399 }
401 g_free(path);
402 return 0;
403 }
405 static void
406 screen_queue_init(WINDOW *w, int cols, int rows)
407 {
408 lw = list_window_init(w, cols, rows);
410 #ifndef NCMPC_MINI
411 if (options.scroll)
412 hscroll_init(&hscroll, w, options.scroll_sep);
413 #endif
414 }
416 static gboolean
417 timer_hide_cursor(gpointer data)
418 {
419 struct mpdclient *c = data;
421 assert(options.hide_cursor > 0);
422 assert(timer_hide_cursor_id != 0);
424 timer_hide_cursor_id = 0;
426 /* hide the cursor when mpd is playing and the user is inactive */
428 if (c->status != NULL &&
429 mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
430 lw->hide_cursor = true;
431 screen_queue_repaint();
432 } else
433 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
434 timer_hide_cursor, c);
436 return FALSE;
437 }
439 static void
440 screen_queue_open(struct mpdclient *c)
441 {
442 playlist = &c->playlist;
444 assert(timer_hide_cursor_id == 0);
445 if (options.hide_cursor > 0) {
446 lw->hide_cursor = false;
447 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
448 timer_hide_cursor, c);
449 }
451 screen_queue_restore_selection();
452 }
454 static void
455 screen_queue_close(void)
456 {
457 if (timer_hide_cursor_id != 0) {
458 g_source_remove(timer_hide_cursor_id);
459 timer_hide_cursor_id = 0;
460 }
462 #ifndef NCMPC_MINI
463 if (options.scroll)
464 hscroll_clear(&hscroll);
465 #endif
466 }
468 static void
469 screen_queue_resize(int cols, int rows)
470 {
471 list_window_resize(lw, cols, rows);
472 }
475 static void
476 screen_queue_exit(void)
477 {
478 list_window_free(lw);
479 }
481 static const char *
482 screen_queue_title(char *str, size_t size)
483 {
484 if (options.host == NULL)
485 return _("Playlist");
487 g_snprintf(str, size, _("Playlist on %s"), options.host);
488 return str;
489 }
491 static void
492 screen_queue_paint_callback(WINDOW *w, unsigned i,
493 unsigned y, unsigned width,
494 bool selected, G_GNUC_UNUSED void *data)
495 {
496 const struct mpd_song *song;
497 struct hscroll *row_hscroll;
499 assert(playlist != NULL);
500 assert(i < playlist_length(playlist));
502 song = playlist_get(playlist, i);
504 #ifdef NCMPC_MINI
505 row_hscroll = NULL;
506 #else
507 row_hscroll = selected && options.scroll && lw->selected == i
508 ? &hscroll : NULL;
509 #endif
511 paint_song_row(w, y, width, selected,
512 (int)mpd_song_get_id(song) == current_song_id,
513 song, row_hscroll);
514 }
516 static void
517 screen_queue_paint(void)
518 {
519 #ifndef NCMPC_MINI
520 if (options.scroll)
521 hscroll_clear(&hscroll);
522 #endif
524 list_window_paint2(lw, screen_queue_paint_callback, NULL);
525 }
527 G_GNUC_PURE
528 static int
529 get_current_song_id(const struct mpd_status *status)
530 {
531 return status != NULL &&
532 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
533 mpd_status_get_state(status) == MPD_STATE_PAUSE)
534 ? (int)mpd_status_get_song_id(status)
535 : -1;
536 }
538 static void
539 screen_queue_update(struct mpdclient *c)
540 {
541 if (c->events & MPD_IDLE_PLAYLIST)
542 screen_queue_restore_selection();
544 if ((c->events & MPD_IDLE_PLAYER) != 0 &&
545 get_current_song_id(c->status) != current_song_id) {
546 current_song_id = get_current_song_id(c->status);
548 /* center the cursor */
549 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
550 center_playing_item(c, false);
552 screen_queue_repaint();
553 } else if (c->events & MPD_IDLE_PLAYLIST) {
554 /* the playlist has changed, we must paint the new
555 version */
556 screen_queue_repaint();
557 }
558 }
560 #ifdef HAVE_GETMOUSE
561 static bool
562 handle_mouse_event(struct mpdclient *c)
563 {
564 int row;
565 unsigned long bstate;
566 unsigned old_selected;
568 if (screen_get_mouse_event(c, &bstate, &row) ||
569 list_window_mouse(lw, bstate, row)) {
570 screen_queue_repaint();
571 return true;
572 }
574 if (bstate & BUTTON1_DOUBLE_CLICKED) {
575 /* stop */
576 screen_cmd(c, CMD_STOP);
577 return true;
578 }
580 old_selected = lw->selected;
581 list_window_set_cursor(lw, lw->start + row);
583 if (bstate & BUTTON1_CLICKED) {
584 /* play */
585 const struct mpd_song *song = screen_queue_selected_song();
586 if (song != NULL) {
587 struct mpd_connection *connection =
588 mpdclient_get_connection(c);
590 if (connection != NULL &&
591 !mpd_run_play_id(connection,
592 mpd_song_get_id(song)))
593 mpdclient_handle_error(c);
594 }
595 } else if (bstate & BUTTON3_CLICKED) {
596 /* delete */
597 if (lw->selected == old_selected)
598 mpdclient_cmd_delete(c, lw->selected);
600 list_window_set_length(lw, playlist_length(playlist));
601 }
603 screen_queue_save_selection();
604 screen_queue_repaint();
606 return true;
607 }
608 #endif
610 static bool
611 screen_queue_cmd(struct mpdclient *c, command_t cmd)
612 {
613 struct mpd_connection *connection;
614 static command_t cached_cmd = CMD_NONE;
615 command_t prev_cmd = cached_cmd;
616 struct list_window_range range;
617 const struct mpd_song *song;
619 cached_cmd = cmd;
621 lw->hide_cursor = false;
623 if (options.hide_cursor > 0) {
624 if (timer_hide_cursor_id != 0)
625 g_source_remove(timer_hide_cursor_id);
626 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
627 timer_hide_cursor, c);
628 }
630 if (list_window_cmd(lw, cmd)) {
631 screen_queue_save_selection();
632 screen_queue_repaint();
633 return true;
634 }
636 switch(cmd) {
637 case CMD_SCREEN_UPDATE:
638 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
639 screen_queue_repaint();
640 return false;
641 case CMD_SELECT_PLAYING:
642 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
643 c->song));
644 screen_queue_save_selection();
645 screen_queue_repaint();
646 return true;
648 case CMD_LIST_FIND:
649 case CMD_LIST_RFIND:
650 case CMD_LIST_FIND_NEXT:
651 case CMD_LIST_RFIND_NEXT:
652 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
653 screen_queue_save_selection();
654 screen_queue_repaint();
655 return true;
656 case CMD_LIST_JUMP:
657 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
658 screen_queue_save_selection();
659 screen_queue_repaint();
660 return true;
662 #ifdef HAVE_GETMOUSE
663 case CMD_MOUSE_EVENT:
664 return handle_mouse_event(c);
665 #endif
667 #ifdef ENABLE_SONG_SCREEN
668 case CMD_SCREEN_SONG:
669 if (screen_queue_selected_song() != NULL) {
670 screen_song_switch(c, screen_queue_selected_song());
671 return true;
672 }
674 break;
675 #endif
677 #ifdef ENABLE_LYRICS_SCREEN
678 case CMD_SCREEN_LYRICS:
679 if (lw->selected < playlist_length(&c->playlist)) {
680 struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
681 bool follow = false;
683 if (c->song && selected &&
684 !strcmp(mpd_song_get_uri(selected),
685 mpd_song_get_uri(c->song)))
686 follow = true;
688 screen_lyrics_switch(c, selected, follow);
689 return true;
690 }
692 break;
693 #endif
694 case CMD_SCREEN_SWAP:
695 screen_swap(c, playlist_get(&c->playlist, lw->selected));
696 return true;
698 default:
699 break;
700 }
702 if (!mpdclient_is_connected(c))
703 return false;
705 switch(cmd) {
706 case CMD_PLAY:
707 song = screen_queue_selected_song();
708 if (song == NULL)
709 return false;
711 connection = mpdclient_get_connection(c);
712 if (connection != NULL &&
713 !mpd_run_play_id(connection, mpd_song_get_id(song)))
714 mpdclient_handle_error(c);
716 return true;
718 case CMD_DELETE:
719 list_window_get_range(lw, &range);
720 mpdclient_cmd_delete_range(c, range.start, range.end);
722 list_window_set_cursor(lw, range.start);
723 return true;
725 case CMD_SAVE_PLAYLIST:
726 playlist_save(c, NULL, NULL);
727 return true;
729 case CMD_ADD:
730 handle_add_to_playlist(c);
731 return true;
733 case CMD_SHUFFLE:
734 list_window_get_range(lw, &range);
735 if (range.end < range.start + 1)
736 /* No range selection, shuffle all list. */
737 break;
739 connection = mpdclient_get_connection(c);
740 if (connection == NULL)
741 return true;
743 if (mpd_run_shuffle_range(connection, range.start, range.end))
744 screen_status_message(_("Shuffled playlist"));
745 else
746 mpdclient_handle_error(c);
747 return true;
749 case CMD_LIST_MOVE_UP:
750 list_window_get_range(lw, &range);
751 if (range.start == 0 || range.end <= range.start)
752 return false;
754 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
755 return true;
757 lw->selected--;
758 lw->range_base--;
760 screen_queue_save_selection();
761 return true;
763 case CMD_LIST_MOVE_DOWN:
764 list_window_get_range(lw, &range);
765 if (range.end >= playlist_length(&c->playlist))
766 return false;
768 if (!mpdclient_cmd_move(c, range.start, range.end))
769 return true;
771 lw->selected++;
772 lw->range_base++;
774 screen_queue_save_selection();
775 return true;
777 case CMD_LOCATE:
778 if (screen_queue_selected_song() != NULL) {
779 screen_file_goto_song(c, screen_queue_selected_song());
780 return true;
781 }
783 break;
785 default:
786 break;
787 }
789 return false;
790 }
792 const struct screen_functions screen_queue = {
793 .init = screen_queue_init,
794 .exit = screen_queue_exit,
795 .open = screen_queue_open,
796 .close = screen_queue_close,
797 .resize = screen_queue_resize,
798 .paint = screen_queue_paint,
799 .update = screen_queue_update,
800 .cmd = screen_queue_cmd,
801 .get_title = screen_queue_title,
802 };