Code

67df4b53655b7f3603cf174ae48c1a88c724b266
[ncmpc.git] / src / screen_queue.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 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();
120 static const char *
121 screen_queue_lw_callback(unsigned idx, G_GNUC_UNUSED void *data)
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;
136 static void
137 center_playing_item(const struct mpd_status *status, bool center_cursor)
139         int idx;
141         if (status == NULL ||
142             (mpd_status_get_state(status) != MPD_STATE_PLAY &&
143              mpd_status_get_state(status) != MPD_STATE_PAUSE))
144                 return;
146         /* try to center the song that are playing */
147         idx = mpd_status_get_song_pos(status);
148         if (idx < 0)
149                 return;
151         list_window_center(lw, idx);
153         if (center_cursor) {
154                 list_window_set_cursor(lw, idx);
155                 return;
156         }
158         /* make sure the cursor is in the window */
159         list_window_fetch_cursor(lw);
162 G_GNUC_PURE
163 static int
164 get_current_song_id(const struct mpd_status *status)
166         return status != NULL &&
167                 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
168                  mpd_status_get_state(status) == MPD_STATE_PAUSE)
169                 ? (int)mpd_status_get_song_id(status)
170                 : -1;
173 static bool
174 screen_queue_song_change(const struct mpd_status *status)
176         if (get_current_song_id(status) == current_song_id)
177                 return false;
179         current_song_id = get_current_song_id(status);
181         /* center the cursor */
182         if (options.auto_center && !lw->range_selection)
183                 center_playing_item(status, false);
185         return true;
188 #ifndef NCMPC_MINI
189 static void
190 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
191                        void *data)
193         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
194         GList **list = tmp->list;
195         struct mpdclient *c = tmp->c;
197         if( *list == NULL ) {
198                 /* create completion list */
199                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
200                 g_completion_add_items(gcmp, *list);
201         }
204 static void
205 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
206                         G_GNUC_UNUSED gchar *line, GList *items,
207                         G_GNUC_UNUSED void *data)
209         if (g_list_length(items) >= 1)
210                 screen_display_completion_list(items);
212 #endif
214 #ifndef NCMPC_MINI
215 /**
216  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
217  * g_completion_set_compare(), because strncmp() takes size_t where
218  * g_completion_set_compare passes a gsize value.
219  */
220 static gint
221 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
223         return strncmp(s1, s2, n);
225 #endif
227 int
228 playlist_save(struct mpdclient *c, char *name, char *defaultname)
230         struct mpd_connection *connection;
231         gchar *filename, *filename_utf8;
232 #ifndef NCMPC_MINI
233         GCompletion *gcmp;
234         GList *list = NULL;
235         completion_callback_data_t data;
236 #endif
238 #ifdef NCMPC_MINI
239         (void)defaultname;
240 #endif
242 #ifndef NCMPC_MINI
243         if (name == NULL) {
244                 /* initialize completion support */
245                 gcmp = g_completion_new(NULL);
246                 g_completion_set_compare(gcmp, completion_strncmp);
247                 data.list = &list;
248                 data.dir_list = NULL;
249                 data.c = c;
250                 wrln_completion_callback_data = &data;
251                 wrln_pre_completion_callback = save_pre_completion_cb;
252                 wrln_post_completion_callback = save_post_completion_cb;
255                 /* query the user for a filename */
256                 filename = screen_readln(_("Save playlist as"),
257                                          defaultname,
258                                          NULL,
259                                          gcmp);
261                 /* destroy completion support */
262                 wrln_completion_callback_data = NULL;
263                 wrln_pre_completion_callback = NULL;
264                 wrln_post_completion_callback = NULL;
265                 g_completion_free(gcmp);
266                 list = string_list_free(list);
267                 if( filename )
268                         filename=g_strstrip(filename);
269         } else
270 #endif
271                         filename=g_strdup(name);
273         if (filename == NULL)
274                 return -1;
276         /* send save command to mpd */
278         connection = mpdclient_get_connection(c);
279         if (connection == NULL)
280                 return -1;
282         filename_utf8 = locale_to_utf8(filename);
283         if (!mpd_run_save(connection, filename_utf8)) {
284                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
285                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
286                     mpd_connection_clear_error(connection)) {
287                         char *buf;
288                         int key;
290                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
291                                               filename, YES, NO);
292                         key = tolower(screen_getch(buf));
293                         g_free(buf);
295                         if (key != YES[0]) {
296                                 g_free(filename_utf8);
297                                 g_free(filename);
298                                 screen_status_printf(_("Aborted"));
299                                 return -1;
300                         }
302                         if (!mpd_run_rm(connection, filename_utf8) ||
303                             !mpd_run_save(connection, filename_utf8)) {
304                                 mpdclient_handle_error(c);
305                                 g_free(filename_utf8);
306                                 g_free(filename);
307                                 return -1;
308                         }
309                 } else {
310                         mpdclient_handle_error(c);
311                         g_free(filename_utf8);
312                         g_free(filename);
313                         return -1;
314                 }
315         }
317         c->events |= MPD_IDLE_STORED_PLAYLIST;
319         g_free(filename_utf8);
321         /* success */
322         screen_status_printf(_("Saved %s"), filename);
323         g_free(filename);
324         return 0;
327 #ifndef NCMPC_MINI
328 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
329                     GList **list, struct mpdclient *c)
331         g_completion_remove_items(gcmp, *list);
332         *list = string_list_remove(*list, dir);
333         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
334         g_completion_add_items(gcmp, *list);
335         *dir_list = g_list_append(*dir_list, g_strdup(dir));
338 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
340         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
341         GList **dir_list = tmp->dir_list;
342         GList **list = tmp->list;
343         struct mpdclient *c = tmp->c;
345         if (*list == NULL) {
346                 /* create initial list */
347                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
348                 g_completion_add_items(gcmp, *list);
349         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
350                    string_list_find(*dir_list, line) == NULL) {
351                 /* add directory content to list */
352                 add_dir(gcmp, line, dir_list, list, c);
353         }
356 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
357                                    GList *items, void *data)
359         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
360         GList **dir_list = tmp->dir_list;
361         GList **list = tmp->list;
362         struct mpdclient *c = tmp->c;
364         if (g_list_length(items) >= 1)
365                 screen_display_completion_list(items);
367         if (line && line[0] && line[strlen(line) - 1] == '/' &&
368             string_list_find(*dir_list, line) == NULL) {
369                 /* add directory content to list */
370                 add_dir(gcmp, line, dir_list, list, c);
371         }
373 #endif
375 static int
376 handle_add_to_playlist(struct mpdclient *c)
378         gchar *path;
379         GCompletion *gcmp;
380 #ifndef NCMPC_MINI
381         GList *list = NULL;
382         GList *dir_list = NULL;
383         completion_callback_data_t data;
385         /* initialize completion support */
386         gcmp = g_completion_new(NULL);
387         g_completion_set_compare(gcmp, completion_strncmp);
388         data.list = &list;
389         data.dir_list = &dir_list;
390         data.c = c;
391         wrln_completion_callback_data = &data;
392         wrln_pre_completion_callback = add_pre_completion_cb;
393         wrln_post_completion_callback = add_post_completion_cb;
394 #else
395         gcmp = NULL;
396 #endif
398         /* get path */
399         path = screen_readln(_("Add"),
400                              NULL,
401                              NULL,
402                              gcmp);
404         /* destroy completion data */
405 #ifndef NCMPC_MINI
406         wrln_completion_callback_data = NULL;
407         wrln_pre_completion_callback = NULL;
408         wrln_post_completion_callback = NULL;
409         g_completion_free(gcmp);
410         string_list_free(list);
411         string_list_free(dir_list);
412 #endif
414         /* add the path to the playlist */
415         if (path != NULL) {
416                 char *path_utf8 = locale_to_utf8(path);
417                 mpdclient_cmd_add_path(c, path_utf8);
418                 g_free(path_utf8);
419         }
421         g_free(path);
422         return 0;
425 static void
426 screen_queue_init(WINDOW *w, int cols, int rows)
428         lw = list_window_init(w, cols, rows);
430 #ifndef NCMPC_MINI
431         if (options.scroll)
432                 hscroll_init(&hscroll, w, options.scroll_sep);
433 #endif
436 static gboolean
437 timer_hide_cursor(gpointer data)
439         struct mpdclient *c = data;
441         assert(options.hide_cursor > 0);
442         assert(timer_hide_cursor_id != 0);
444         timer_hide_cursor_id = 0;
446         /* hide the cursor when mpd is playing and the user is inactive */
448         if (c->status != NULL &&
449             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
450                 lw->hide_cursor = true;
451                 screen_queue_repaint();
452         } else
453                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
454                                                      timer_hide_cursor, c);
456         return FALSE;
459 static void
460 screen_queue_open(struct mpdclient *c)
462         playlist = &c->playlist;
464         assert(timer_hide_cursor_id == 0);
465         if (options.hide_cursor > 0) {
466                 lw->hide_cursor = false;
467                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
468                                                      timer_hide_cursor, c);
469         }
471         screen_queue_restore_selection();
472         screen_queue_song_change(c->status);
475 static void
476 screen_queue_close(void)
478         if (timer_hide_cursor_id != 0) {
479                 g_source_remove(timer_hide_cursor_id);
480                 timer_hide_cursor_id = 0;
481         }
483 #ifndef NCMPC_MINI
484         if (options.scroll)
485                 hscroll_clear(&hscroll);
486 #endif
489 static void
490 screen_queue_resize(int cols, int rows)
492         list_window_resize(lw, cols, rows);
496 static void
497 screen_queue_exit(void)
499         list_window_free(lw);
502 static const char *
503 screen_queue_title(char *str, size_t size)
505         if (options.host == NULL)
506                 return _("Playlist");
508         g_snprintf(str, size, _("Playlist on %s"), options.host);
509         return str;
512 static void
513 screen_queue_paint_callback(WINDOW *w, unsigned i,
514                             unsigned y, unsigned width,
515                             bool selected, G_GNUC_UNUSED void *data)
517         const struct mpd_song *song;
518         struct hscroll *row_hscroll;
520         assert(playlist != NULL);
521         assert(i < playlist_length(playlist));
523         song = playlist_get(playlist, i);
525 #ifdef NCMPC_MINI
526         row_hscroll = NULL;
527 #else
528         row_hscroll = selected && options.scroll && lw->selected == i
529                 ? &hscroll : NULL;
530 #endif
532         paint_song_row(w, y, width, selected,
533                        (int)mpd_song_get_id(song) == current_song_id,
534                        song, row_hscroll);
537 static void
538 screen_queue_paint(void)
540 #ifndef NCMPC_MINI
541         if (options.scroll)
542                 hscroll_clear(&hscroll);
543 #endif
545         list_window_paint2(lw, screen_queue_paint_callback, NULL);
548 static void
549 screen_queue_update(struct mpdclient *c)
551         if (c->events & MPD_IDLE_QUEUE)
552                 screen_queue_restore_selection();
553         else
554                 /* the queue size may have changed, even if we havn't
555                    revceived the QUEUE idle event yet */
556                 list_window_set_length(lw, playlist_length(playlist));
558         if (((c->events & MPD_IDLE_PLAYER) != 0 &&
559              screen_queue_song_change(c->status)) ||
560             c->events & MPD_IDLE_QUEUE)
561                 /* the queue or the current song has changed, we must
562                    paint the new version */
563                 screen_queue_repaint();
566 #ifdef HAVE_GETMOUSE
567 static bool
568 handle_mouse_event(struct mpdclient *c)
570         int row;
571         unsigned long bstate;
572         unsigned old_selected;
574         if (screen_get_mouse_event(c, &bstate, &row) ||
575             list_window_mouse(lw, bstate, row)) {
576                 screen_queue_repaint();
577                 return true;
578         }
580         if (bstate & BUTTON1_DOUBLE_CLICKED) {
581                 /* stop */
582                 screen_cmd(c, CMD_STOP);
583                 return true;
584         }
586         old_selected = lw->selected;
587         list_window_set_cursor(lw, lw->start + row);
589         if (bstate & BUTTON1_CLICKED) {
590                 /* play */
591                 const struct mpd_song *song = screen_queue_selected_song();
592                 if (song != NULL) {
593                         struct mpd_connection *connection =
594                                 mpdclient_get_connection(c);
596                         if (connection != NULL &&
597                             !mpd_run_play_id(connection,
598                                              mpd_song_get_id(song)))
599                                 mpdclient_handle_error(c);
600                 }
601         } else if (bstate & BUTTON3_CLICKED) {
602                 /* delete */
603                 if (lw->selected == old_selected)
604                         mpdclient_cmd_delete(c, lw->selected);
606                 list_window_set_length(lw, playlist_length(playlist));
607         }
609         screen_queue_save_selection();
610         screen_queue_repaint();
612         return true;
614 #endif
616 static bool
617 screen_queue_cmd(struct mpdclient *c, command_t cmd)
619         struct mpd_connection *connection;
620         static command_t cached_cmd = CMD_NONE;
621         command_t prev_cmd = cached_cmd;
622         struct list_window_range range;
623         const struct mpd_song *song;
625         cached_cmd = cmd;
627         lw->hide_cursor = false;
629         if (options.hide_cursor > 0) {
630                 if (timer_hide_cursor_id != 0)
631                         g_source_remove(timer_hide_cursor_id);
632                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
633                                                      timer_hide_cursor, c);
634         }
636         if (list_window_cmd(lw, cmd)) {
637                 screen_queue_save_selection();
638                 screen_queue_repaint();
639                 return true;
640         }
642         switch(cmd) {
643         case CMD_SCREEN_UPDATE:
644                 center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
645                 screen_queue_repaint();
646                 return false;
647         case CMD_SELECT_PLAYING:
648                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
649                                                               c->song));
650                 screen_queue_save_selection();
651                 screen_queue_repaint();
652                 return true;
654         case CMD_LIST_FIND:
655         case CMD_LIST_RFIND:
656         case CMD_LIST_FIND_NEXT:
657         case CMD_LIST_RFIND_NEXT:
658                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
659                 screen_queue_save_selection();
660                 screen_queue_repaint();
661                 return true;
662         case CMD_LIST_JUMP:
663                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
664                 screen_queue_save_selection();
665                 screen_queue_repaint();
666                 return true;
668 #ifdef HAVE_GETMOUSE
669         case CMD_MOUSE_EVENT:
670                 return handle_mouse_event(c);
671 #endif
673 #ifdef ENABLE_SONG_SCREEN
674         case CMD_SCREEN_SONG:
675                 if (screen_queue_selected_song() != NULL) {
676                         screen_song_switch(c, screen_queue_selected_song());
677                         return true;
678                 }
680                 break;
681 #endif
683 #ifdef ENABLE_LYRICS_SCREEN
684         case CMD_SCREEN_LYRICS:
685                 if (lw->selected < playlist_length(&c->playlist)) {
686                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
687                         bool follow = false;
689                         if (c->song && selected &&
690                             !strcmp(mpd_song_get_uri(selected),
691                                     mpd_song_get_uri(c->song)))
692                                 follow = true;
694                         screen_lyrics_switch(c, selected, follow);
695                         return true;
696                 }
698                 break;
699 #endif
700         case CMD_SCREEN_SWAP:
701                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
702                 return true;
704         default:
705                 break;
706         }
708         if (!mpdclient_is_connected(c))
709                 return false;
711         switch(cmd) {
712         case CMD_PLAY:
713                 song = screen_queue_selected_song();
714                 if (song == NULL)
715                         return false;
717                 connection = mpdclient_get_connection(c);
718                 if (connection != NULL &&
719                     !mpd_run_play_id(connection, mpd_song_get_id(song)))
720                         mpdclient_handle_error(c);
722                 return true;
724         case CMD_DELETE:
725                 list_window_get_range(lw, &range);
726                 mpdclient_cmd_delete_range(c, range.start, range.end);
728                 list_window_set_cursor(lw, range.start);
729                 return true;
731         case CMD_SAVE_PLAYLIST:
732                 playlist_save(c, NULL, NULL);
733                 return true;
735         case CMD_ADD:
736                 handle_add_to_playlist(c);
737                 return true;
739         case CMD_SHUFFLE:
740                 list_window_get_range(lw, &range);
741                 if (range.end <= range.start + 1)
742                         /* No range selection, shuffle all list. */
743                         break;
745                 connection = mpdclient_get_connection(c);
746                 if (connection == NULL)
747                         return true;
749                 if (mpd_run_shuffle_range(connection, range.start, range.end))
750                         screen_status_message(_("Shuffled playlist"));
751                 else
752                         mpdclient_handle_error(c);
753                 return true;
755         case CMD_LIST_MOVE_UP:
756                 list_window_get_range(lw, &range);
757                 if (range.start == 0 || range.end <= range.start)
758                         return false;
760                 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
761                         return true;
763                 lw->selected--;
764                 lw->range_base--;
766                 screen_queue_save_selection();
767                 return true;
769         case CMD_LIST_MOVE_DOWN:
770                 list_window_get_range(lw, &range);
771                 if (range.end >= playlist_length(&c->playlist))
772                         return false;
774                 if (!mpdclient_cmd_move(c, range.start, range.end))
775                         return true;
777                 lw->selected++;
778                 lw->range_base++;
780                 screen_queue_save_selection();
781                 return true;
783         case CMD_LOCATE:
784                 if (screen_queue_selected_song() != NULL) {
785                         screen_file_goto_song(c, screen_queue_selected_song());
786                         return true;
787                 }
789                 break;
791         default:
792                 break;
793         }
795         return false;
798 const struct screen_functions screen_queue = {
799         .init = screen_queue_init,
800         .exit = screen_queue_exit,
801         .open = screen_queue_open,
802         .close = screen_queue_close,
803         .resize = screen_queue_resize,
804         .paint = screen_queue_paint,
805         .update = screen_queue_update,
806         .cmd = screen_queue_cmd,
807         .get_title = screen_queue_title,
808 };