main: eliminate local dummy variable
[ncmpc.git] / src / screen_queue.c
blob7c1041cadfe28268e5210b95c97c09c4a4c6c031
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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.
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
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)
75 screen_queue_paint();
76 wrefresh(lw->w);
79 static const struct mpd_song *
80 screen_queue_selected_song(void)
82 return !lw->range_selection &&
83 lw->selected < playlist_length(playlist)
84 ? playlist_get(playlist, lw->selected)
85 : NULL;
88 static void
89 screen_queue_save_selection(void)
91 selected_song_id = screen_queue_selected_song() != NULL
92 ? (int)mpd_song_get_id(screen_queue_selected_song())
93 : -1;
96 static void
97 screen_queue_restore_selection(void)
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();
118 static const char *
119 screen_queue_lw_callback(unsigned idx, gcc_unused void *data)
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;
133 static void
134 center_playing_item(const struct mpd_status *status, bool center_cursor)
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;
153 /* make sure the cursor is in the window */
154 list_window_fetch_cursor(lw);
157 gcc_pure
158 static int
159 get_current_song_id(const struct mpd_status *status)
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;
168 static bool
169 screen_queue_song_change(const struct mpd_status *status)
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;
183 #ifndef NCMPC_MINI
184 static void
185 save_pre_completion_cb(GCompletion *gcmp, gcc_unused gchar *line,
186 void *data)
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);
199 static void
200 save_post_completion_cb(gcc_unused GCompletion *gcmp,
201 gcc_unused gchar *line, GList *items,
202 gcc_unused void *data)
204 if (g_list_length(items) >= 1)
205 screen_display_completion_list(items);
207 #endif
209 #ifndef NCMPC_MINI
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.
215 static gint
216 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
218 return strncmp(s1, s2, n);
220 #endif
223 playlist_save(struct mpdclient *c, char *name, char *defaultname)
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,
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;
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 [%s/%s] ? "),
283 filename, YES, NO);
284 bool replace = screen_get_yesno(buf, false);
285 g_free(buf);
287 if (!replace) {
288 g_free(filename_utf8);
289 g_free(filename);
290 screen_status_printf(_("Aborted"));
291 return -1;
294 if (!mpd_run_rm(connection, filename_utf8) ||
295 !mpd_run_save(connection, filename_utf8)) {
296 mpdclient_handle_error(c);
297 g_free(filename_utf8);
298 g_free(filename);
299 return -1;
301 } else {
302 mpdclient_handle_error(c);
303 g_free(filename_utf8);
304 g_free(filename);
305 return -1;
309 c->events |= MPD_IDLE_STORED_PLAYLIST;
311 g_free(filename_utf8);
313 /* success */
314 screen_status_printf(_("Saved %s"), filename);
315 g_free(filename);
316 return 0;
319 #ifndef NCMPC_MINI
320 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
321 GList **list, struct mpdclient *c)
323 g_completion_remove_items(gcmp, *list);
324 *list = string_list_remove(*list, dir);
325 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
326 g_completion_add_items(gcmp, *list);
327 *dir_list = g_list_append(*dir_list, g_strdup(dir));
330 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
332 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
333 GList **dir_list = tmp->dir_list;
334 GList **list = tmp->list;
335 struct mpdclient *c = tmp->c;
337 if (*list == NULL) {
338 /* create initial list */
339 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
340 g_completion_add_items(gcmp, *list);
341 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
342 string_list_find(*dir_list, line) == NULL) {
343 /* add directory content to list */
344 add_dir(gcmp, line, dir_list, list, c);
348 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
349 GList *items, void *data)
351 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
352 GList **dir_list = tmp->dir_list;
353 GList **list = tmp->list;
354 struct mpdclient *c = tmp->c;
356 if (g_list_length(items) >= 1)
357 screen_display_completion_list(items);
359 if (line && line[0] && line[strlen(line) - 1] == '/' &&
360 string_list_find(*dir_list, line) == NULL) {
361 /* add directory content to list */
362 add_dir(gcmp, line, dir_list, list, c);
365 #endif
367 static int
368 handle_add_to_playlist(struct mpdclient *c)
370 #ifndef NCMPC_MINI
371 /* initialize completion support */
372 GCompletion *gcmp = g_completion_new(NULL);
373 g_completion_set_compare(gcmp, completion_strncmp);
375 GList *list = NULL;
376 GList *dir_list = NULL;
377 completion_callback_data_t data = {
378 .list = &list,
379 .dir_list = &dir_list,
380 .c = c,
383 wrln_completion_callback_data = &data;
384 wrln_pre_completion_callback = add_pre_completion_cb;
385 wrln_post_completion_callback = add_post_completion_cb;
386 #else
387 GCompletion *gcmp = NULL;
388 #endif
390 /* get path */
391 char *path = screen_readln(_("Add"),
392 NULL,
393 NULL,
394 gcmp);
396 /* destroy completion data */
397 #ifndef NCMPC_MINI
398 wrln_completion_callback_data = NULL;
399 wrln_pre_completion_callback = NULL;
400 wrln_post_completion_callback = NULL;
401 g_completion_free(gcmp);
402 string_list_free(list);
403 string_list_free(dir_list);
404 #endif
406 /* add the path to the playlist */
407 if (path != NULL) {
408 char *path_utf8 = locale_to_utf8(path);
409 mpdclient_cmd_add_path(c, path_utf8);
410 g_free(path_utf8);
413 g_free(path);
414 return 0;
417 static void
418 screen_queue_init(WINDOW *w, int cols, int rows)
420 lw = list_window_init(w, cols, rows);
422 #ifndef NCMPC_MINI
423 if (options.scroll)
424 hscroll_init(&hscroll, w, options.scroll_sep);
425 #endif
428 static gboolean
429 timer_hide_cursor(gpointer data)
431 struct mpdclient *c = data;
433 assert(options.hide_cursor > 0);
434 assert(timer_hide_cursor_id != 0);
436 timer_hide_cursor_id = 0;
438 /* hide the cursor when mpd is playing and the user is inactive */
440 if (c->status != NULL &&
441 mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
442 lw->hide_cursor = true;
443 screen_queue_repaint();
444 } else
445 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
446 timer_hide_cursor, c);
448 return FALSE;
451 static void
452 screen_queue_open(struct mpdclient *c)
454 playlist = &c->playlist;
456 assert(timer_hide_cursor_id == 0);
457 if (options.hide_cursor > 0) {
458 lw->hide_cursor = false;
459 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
460 timer_hide_cursor, c);
463 screen_queue_restore_selection();
464 screen_queue_song_change(c->status);
467 static void
468 screen_queue_close(void)
470 if (timer_hide_cursor_id != 0) {
471 g_source_remove(timer_hide_cursor_id);
472 timer_hide_cursor_id = 0;
475 #ifndef NCMPC_MINI
476 if (options.scroll)
477 hscroll_clear(&hscroll);
478 #endif
481 static void
482 screen_queue_resize(int cols, int rows)
484 list_window_resize(lw, cols, rows);
488 static void
489 screen_queue_exit(void)
491 list_window_free(lw);
494 static const char *
495 screen_queue_title(char *str, size_t size)
497 if (options.host == NULL)
498 return _("Queue");
500 g_snprintf(str, size, _("Queue on %s"), options.host);
501 return str;
504 static void
505 screen_queue_paint_callback(WINDOW *w, unsigned i,
506 unsigned y, unsigned width,
507 bool selected, gcc_unused const void *data)
509 assert(playlist != NULL);
510 assert(i < playlist_length(playlist));
512 const struct mpd_song *song = playlist_get(playlist, i);
514 struct hscroll *row_hscroll = NULL;
515 #ifndef NCMPC_MINI
516 row_hscroll = selected && options.scroll && lw->selected == i
517 ? &hscroll : NULL;
518 #endif
520 paint_song_row(w, y, width, selected,
521 (int)mpd_song_get_id(song) == current_song_id,
522 song, row_hscroll, options.list_format);
525 static void
526 screen_queue_paint(void)
528 #ifndef NCMPC_MINI
529 if (options.scroll)
530 hscroll_clear(&hscroll);
531 #endif
533 list_window_paint2(lw, screen_queue_paint_callback, NULL);
536 static void
537 screen_queue_update(struct mpdclient *c)
539 if (c->events & MPD_IDLE_QUEUE)
540 screen_queue_restore_selection();
541 else
542 /* the queue size may have changed, even if we havn't
543 received the QUEUE idle event yet */
544 list_window_set_length(lw, playlist_length(playlist));
546 if (((c->events & MPD_IDLE_PLAYER) != 0 &&
547 screen_queue_song_change(c->status)) ||
548 c->events & MPD_IDLE_QUEUE)
549 /* the queue or the current song has changed, we must
550 paint the new version */
551 screen_queue_repaint();
554 #ifdef HAVE_GETMOUSE
555 static bool
556 handle_mouse_event(struct mpdclient *c)
558 unsigned long bstate;
559 int row;
560 if (screen_get_mouse_event(c, &bstate, &row) ||
561 list_window_mouse(lw, bstate, row)) {
562 screen_queue_repaint();
563 return true;
566 if (bstate & BUTTON1_DOUBLE_CLICKED) {
567 /* stop */
568 screen_cmd(c, CMD_STOP);
569 return true;
572 const unsigned old_selected = lw->selected;
573 list_window_set_cursor(lw, lw->start + row);
575 if (bstate & BUTTON1_CLICKED) {
576 /* play */
577 const struct mpd_song *song = screen_queue_selected_song();
578 if (song != NULL) {
579 struct mpd_connection *connection =
580 mpdclient_get_connection(c);
582 if (connection != NULL &&
583 !mpd_run_play_id(connection,
584 mpd_song_get_id(song)))
585 mpdclient_handle_error(c);
587 } else if (bstate & BUTTON3_CLICKED) {
588 /* delete */
589 if (lw->selected == old_selected)
590 mpdclient_cmd_delete(c, lw->selected);
592 list_window_set_length(lw, playlist_length(playlist));
595 screen_queue_save_selection();
596 screen_queue_repaint();
598 return true;
600 #endif
602 static bool
603 screen_queue_cmd(struct mpdclient *c, command_t cmd)
605 struct mpd_connection *connection;
606 static command_t cached_cmd = CMD_NONE;
608 const command_t prev_cmd = cached_cmd;
609 cached_cmd = cmd;
611 lw->hide_cursor = false;
613 if (options.hide_cursor > 0) {
614 if (timer_hide_cursor_id != 0)
615 g_source_remove(timer_hide_cursor_id);
616 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
617 timer_hide_cursor, c);
620 if (list_window_cmd(lw, cmd)) {
621 screen_queue_save_selection();
622 screen_queue_repaint();
623 return true;
626 switch(cmd) {
627 case CMD_SCREEN_UPDATE:
628 center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
629 screen_queue_repaint();
630 return false;
631 case CMD_SELECT_PLAYING:
632 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
633 c->song));
634 screen_queue_save_selection();
635 screen_queue_repaint();
636 return true;
638 case CMD_LIST_FIND:
639 case CMD_LIST_RFIND:
640 case CMD_LIST_FIND_NEXT:
641 case CMD_LIST_RFIND_NEXT:
642 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
643 screen_queue_save_selection();
644 screen_queue_repaint();
645 return true;
646 case CMD_LIST_JUMP:
647 screen_jump(lw, screen_queue_lw_callback, NULL, NULL, NULL);
648 screen_queue_save_selection();
649 screen_queue_repaint();
650 return true;
652 #ifdef HAVE_GETMOUSE
653 case CMD_MOUSE_EVENT:
654 return handle_mouse_event(c);
655 #endif
657 #ifdef ENABLE_SONG_SCREEN
658 case CMD_SCREEN_SONG:
659 if (screen_queue_selected_song() != NULL) {
660 screen_song_switch(c, screen_queue_selected_song());
661 return true;
664 break;
665 #endif
667 #ifdef ENABLE_LYRICS_SCREEN
668 case CMD_SCREEN_LYRICS:
669 if (lw->selected < playlist_length(&c->playlist)) {
670 struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
671 bool follow = false;
673 if (c->song && selected &&
674 !strcmp(mpd_song_get_uri(selected),
675 mpd_song_get_uri(c->song)))
676 follow = true;
678 screen_lyrics_switch(c, selected, follow);
679 return true;
682 break;
683 #endif
684 case CMD_SCREEN_SWAP:
685 if (playlist_length(&c->playlist) > 0)
686 screen_swap(c, playlist_get(&c->playlist, lw->selected));
687 else
688 screen_swap(c, NULL);
689 return true;
691 default:
692 break;
695 if (!mpdclient_is_connected(c))
696 return false;
698 switch(cmd) {
699 const struct mpd_song *song;
700 struct list_window_range range;
702 case CMD_PLAY:
703 song = screen_queue_selected_song();
704 if (song == NULL)
705 return false;
707 connection = mpdclient_get_connection(c);
708 if (connection != NULL &&
709 !mpd_run_play_id(connection, mpd_song_get_id(song)))
710 mpdclient_handle_error(c);
712 return true;
714 case CMD_DELETE:
715 list_window_get_range(lw, &range);
716 mpdclient_cmd_delete_range(c, range.start, range.end);
718 list_window_set_cursor(lw, range.start);
719 return true;
721 case CMD_SAVE_PLAYLIST:
722 playlist_save(c, NULL, NULL);
723 return true;
725 case CMD_ADD:
726 handle_add_to_playlist(c);
727 return true;
729 case CMD_SHUFFLE:
730 list_window_get_range(lw, &range);
731 if (range.end <= range.start + 1)
732 /* No range selection, shuffle all list. */
733 break;
735 connection = mpdclient_get_connection(c);
736 if (connection == NULL)
737 return true;
739 if (mpd_run_shuffle_range(connection, range.start, range.end))
740 screen_status_message(_("Shuffled queue"));
741 else
742 mpdclient_handle_error(c);
743 return true;
745 case CMD_LIST_MOVE_UP:
746 list_window_get_range(lw, &range);
747 if (range.start == 0 || range.end <= range.start)
748 return false;
750 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
751 return true;
753 lw->selected--;
754 lw->range_base--;
756 if (lw->range_selection)
757 list_window_scroll_to(lw, lw->range_base);
758 list_window_scroll_to(lw, lw->selected);
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 if (lw->range_selection)
775 list_window_scroll_to(lw, lw->range_base);
776 list_window_scroll_to(lw, lw->selected);
778 screen_queue_save_selection();
779 return true;
781 case CMD_LOCATE:
782 if (screen_queue_selected_song() != NULL) {
783 screen_file_goto_song(c, screen_queue_selected_song());
784 return true;
787 break;
789 default:
790 break;
793 return false;
796 const struct screen_functions screen_queue = {
797 .init = screen_queue_init,
798 .exit = screen_queue_exit,
799 .open = screen_queue_open,
800 .close = screen_queue_close,
801 .resize = screen_queue_resize,
802 .paint = screen_queue_paint,
803 .update = screen_queue_update,
804 .cmd = screen_queue_cmd,
805 .get_title = screen_queue_title,