Code

0381d52a0dbdeefb9fb0b2cbe0619feb2cc97360
[ncmpc.git] / src / screen_queue.c
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 "colors.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 static guint scroll_source_id;
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)
74 {
75         screen_queue_paint();
76         wrefresh(lw->w);
77 }
79 static const struct mpd_song *
80 screen_queue_selected_song(void)
81 {
82         return !lw->range_selection &&
83                 lw->selected < playlist_length(playlist)
84                 ? playlist_get(playlist, lw->selected)
85                 : NULL;
86 }
88 static void
89 screen_queue_save_selection(void)
90 {
91         selected_song_id = screen_queue_selected_song() != NULL
92                 ? (int)mpd_song_get_id(screen_queue_selected_song())
93                 : -1;
94 }
96 static void
97 screen_queue_restore_selection(void)
98 {
99         const struct mpd_song *song;
100         int pos;
102         list_window_set_length(lw, playlist_length(playlist));
104         if (selected_song_id < 0)
105                 /* there was no selection */
106                 return;
108         song = screen_queue_selected_song();
109         if (song != NULL &&
110             mpd_song_get_id(song) == (unsigned)selected_song_id)
111                 /* selection is still valid */
112                 return;
114         pos = playlist_get_index_from_id(playlist, selected_song_id);
115         if (pos >= 0)
116                 list_window_set_cursor(lw, pos);
118         screen_queue_save_selection();
121 #ifndef NCMPC_MINI
122 static gboolean
123 scroll_timer_callback(G_GNUC_UNUSED gpointer data)
125         scroll_source_id = 0;
127         hscroll_step(&hscroll);
128         screen_queue_repaint();
129         return false;
131 #endif
133 static const char *
134 screen_queue_lw_callback(unsigned idx, bool *highlight, char **second_column,
135                          G_GNUC_UNUSED void *data)
137         static char songname[MAX_SONG_LENGTH];
138         struct mpd_song *song;
140         assert(playlist != NULL);
141         assert(idx < playlist_length(playlist));
143         song = playlist_get(playlist, idx);
144         if ((int)mpd_song_get_id(song) == current_song_id)
145                 *highlight = true;
147         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
149 #ifndef NCMPC_MINI
150         if (second_column != NULL && mpd_song_get_duration(song) > 0) {
151                 char duration[32];
152                 format_duration_short(duration, sizeof(duration),
153                                       mpd_song_get_duration(song));
154                 *second_column = g_strdup(duration);
155         }
157         if (idx == lw->selected)
158         {
159                 int second_column_len = 0;
160                 if (second_column != NULL && *second_column != NULL)
161                         second_column_len = strlen(*second_column);
162                 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
163                 {
164                         static unsigned current_song;
165                         char *tmp;
167                         if (current_song != lw->selected) {
168                                 hscroll_reset(&hscroll);
169                                 current_song = lw->selected;
170                         }
172                         tmp = strscroll(&hscroll, songname, options.scroll_sep,
173                                         MAX_SONG_LENGTH);
174                         g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
175                         g_free(tmp);
177                         if (scroll_source_id == 0)
178                                 scroll_source_id =
179                                         g_timeout_add(1000,
180                                                       scroll_timer_callback,
181                                                       NULL);
182                 } else {
183                         hscroll_reset(&hscroll);
185                         if (scroll_source_id != 0) {
186                                 g_source_remove(scroll_source_id);
187                                 scroll_source_id = 0;
188                         }
189                 }
190         }
191 #else
192         (void)second_column;
193 #endif
195         return songname;
198 static void
199 center_playing_item(struct mpdclient *c, bool center_cursor)
201         const struct mpd_song *song;
202         unsigned length = c->playlist.list->len;
203         int idx;
205         song = mpdclient_get_current_song(c);
206         if (song == NULL)
207                 return;
209         /* try to center the song that are playing */
210         idx = playlist_get_index(&c->playlist, c->song);
211         if (idx < 0)
212                 return;
214         if (length < lw->rows)
215         {
216                 if (center_cursor)
217                         list_window_set_cursor(lw, idx);
218                 return;
219         }
221         list_window_center(lw, idx);
223         if (center_cursor) {
224                 list_window_set_cursor(lw, idx);
225                 return;
226         }
228         /* make sure the cursor is in the window */
229         list_window_fetch_cursor(lw);
232 #ifndef NCMPC_MINI
233 static void
234 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
235                        void *data)
237         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
238         GList **list = tmp->list;
239         struct mpdclient *c = tmp->c;
241         if( *list == NULL ) {
242                 /* create completion list */
243                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
244                 g_completion_add_items(gcmp, *list);
245         }
248 static void
249 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
250                         G_GNUC_UNUSED gchar *line, GList *items,
251                         G_GNUC_UNUSED void *data)
253         if (g_list_length(items) >= 1)
254                 screen_display_completion_list(items);
256 #endif
258 #ifndef NCMPC_MINI
259 /**
260  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
261  * g_completion_set_compare(), because strncmp() takes size_t where
262  * g_completion_set_compare passes a gsize value.
263  */
264 static gint
265 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
267         return strncmp(s1, s2, n);
269 #endif
271 int
272 playlist_save(struct mpdclient *c, char *name, char *defaultname)
274         struct mpd_connection *connection;
275         gchar *filename, *filename_utf8;
276 #ifndef NCMPC_MINI
277         GCompletion *gcmp;
278         GList *list = NULL;
279         completion_callback_data_t data;
280 #endif
282 #ifdef NCMPC_MINI
283         (void)defaultname;
284 #endif
286 #ifndef NCMPC_MINI
287         if (name == NULL) {
288                 /* initialize completion support */
289                 gcmp = g_completion_new(NULL);
290                 g_completion_set_compare(gcmp, completion_strncmp);
291                 data.list = &list;
292                 data.dir_list = NULL;
293                 data.c = c;
294                 wrln_completion_callback_data = &data;
295                 wrln_pre_completion_callback = save_pre_completion_cb;
296                 wrln_post_completion_callback = save_post_completion_cb;
299                 /* query the user for a filename */
300                 filename = screen_readln(_("Save playlist as"),
301                                          defaultname,
302                                          NULL,
303                                          gcmp);
305                 /* destroy completion support */
306                 wrln_completion_callback_data = NULL;
307                 wrln_pre_completion_callback = NULL;
308                 wrln_post_completion_callback = NULL;
309                 g_completion_free(gcmp);
310                 list = string_list_free(list);
311                 if( filename )
312                         filename=g_strstrip(filename);
313         } else
314 #endif
315                         filename=g_strdup(name);
317         if (filename == NULL)
318                 return -1;
320         /* send save command to mpd */
322         filename_utf8 = locale_to_utf8(filename);
324         connection = mpdclient_get_connection(c);
325         if (!mpd_run_save(connection, filename_utf8)) {
326                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
327                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
328                     mpd_connection_clear_error(connection)) {
329                         char *buf;
330                         int key;
332                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
333                                               filename, YES, NO);
334                         key = tolower(screen_getch(buf));
335                         g_free(buf);
337                         if (key != YES[0]) {
338                                 g_free(filename_utf8);
339                                 g_free(filename);
340                                 screen_status_printf(_("Aborted"));
341                                 return -1;
342                         }
344                         if (!mpd_run_rm(connection, filename_utf8) ||
345                             !mpd_run_save(connection, filename_utf8)) {
346                                 mpdclient_handle_error(c);
347                                 g_free(filename_utf8);
348                                 g_free(filename);
349                                 return -1;
350                         }
351                 } else {
352                         mpdclient_handle_error(c);
353                         g_free(filename_utf8);
354                         g_free(filename);
355                         return -1;
356                 }
357         }
359         c->events |= MPD_IDLE_STORED_PLAYLIST;
361         g_free(filename_utf8);
363         /* success */
364         screen_status_printf(_("Saved %s"), filename);
365         g_free(filename);
366         return 0;
369 #ifndef NCMPC_MINI
370 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
371                     GList **list, struct mpdclient *c)
373         g_completion_remove_items(gcmp, *list);
374         *list = string_list_remove(*list, dir);
375         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
376         g_completion_add_items(gcmp, *list);
377         *dir_list = g_list_append(*dir_list, g_strdup(dir));
380 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
382         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
383         GList **dir_list = tmp->dir_list;
384         GList **list = tmp->list;
385         struct mpdclient *c = tmp->c;
387         if (*list == NULL) {
388                 /* create initial list */
389                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
390                 g_completion_add_items(gcmp, *list);
391         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
392                    string_list_find(*dir_list, line) == NULL) {
393                 /* add directory content to list */
394                 add_dir(gcmp, line, dir_list, list, c);
395         }
398 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
399                                    GList *items, void *data)
401         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
402         GList **dir_list = tmp->dir_list;
403         GList **list = tmp->list;
404         struct mpdclient *c = tmp->c;
406         if (g_list_length(items) >= 1)
407                 screen_display_completion_list(items);
409         if (line && line[0] && line[strlen(line) - 1] == '/' &&
410             string_list_find(*dir_list, line) == NULL) {
411                 /* add directory content to list */
412                 add_dir(gcmp, line, dir_list, list, c);
413         }
415 #endif
417 static int
418 handle_add_to_playlist(struct mpdclient *c)
420         gchar *path;
421 #ifndef NCMPC_MINI
422         GCompletion *gcmp;
423         GList *list = NULL;
424         GList *dir_list = NULL;
425         completion_callback_data_t data;
427         /* initialize completion support */
428         gcmp = g_completion_new(NULL);
429         g_completion_set_compare(gcmp, completion_strncmp);
430         data.list = &list;
431         data.dir_list = &dir_list;
432         data.c = c;
433         wrln_completion_callback_data = &data;
434         wrln_pre_completion_callback = add_pre_completion_cb;
435         wrln_post_completion_callback = add_post_completion_cb;
436 #endif
438         /* get path */
439         path = screen_readln(_("Add"),
440                              NULL,
441                              NULL,
442 #ifdef NCMPC_MINI
443                              NULL
444 #else
445                              gcmp
446 #endif
447                              );
449         /* destroy completion data */
450 #ifndef NCMPC_MINI
451         wrln_completion_callback_data = NULL;
452         wrln_pre_completion_callback = NULL;
453         wrln_post_completion_callback = NULL;
454         g_completion_free(gcmp);
455         string_list_free(list);
456         string_list_free(dir_list);
457 #endif
459         /* add the path to the playlist */
460         if (path != NULL) {
461                 char *path_utf8 = locale_to_utf8(path);
462                 mpdclient_cmd_add_path(c, path_utf8);
463                 g_free(path_utf8);
464         }
466         g_free(path);
467         return 0;
470 static void
471 screen_queue_init(WINDOW *w, int cols, int rows)
473         lw = list_window_init(w, cols, rows);
476 static gboolean
477 timer_hide_cursor(gpointer data)
479         struct mpdclient *c = data;
481         assert(options.hide_cursor > 0);
482         assert(timer_hide_cursor_id != 0);
484         timer_hide_cursor_id = 0;
486         /* hide the cursor when mpd is playing and the user is inactive */
488         if (c->status != NULL &&
489             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
490                 lw->hide_cursor = true;
491                 screen_queue_repaint();
492         } else
493                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
494                                                      timer_hide_cursor, c);
496         return FALSE;
499 static void
500 screen_queue_open(struct mpdclient *c)
502         playlist = &c->playlist;
504         assert(timer_hide_cursor_id == 0);
505         if (options.hide_cursor > 0) {
506                 lw->hide_cursor = false;
507                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
508                                                      timer_hide_cursor, c);
509         }
511         screen_queue_restore_selection();
514 static void
515 screen_queue_close(void)
517         if (timer_hide_cursor_id != 0) {
518                 g_source_remove(timer_hide_cursor_id);
519                 timer_hide_cursor_id = 0;
520         }
523 static void
524 screen_queue_resize(int cols, int rows)
526         list_window_resize(lw, cols, rows);
530 static void
531 screen_queue_exit(void)
533         list_window_free(lw);
536 static const char *
537 screen_queue_title(char *str, size_t size)
539         if (options.host == NULL)
540                 return _("Playlist");
542         g_snprintf(str, size, _("Playlist on %s"), options.host);
543         return str;
546 static void
547 screen_queue_paint(void)
549         list_window_paint(lw, screen_queue_lw_callback, NULL);
552 static void
553 screen_queue_update(struct mpdclient *c)
555         static int prev_song_id = -1;
557         if (c->events & MPD_IDLE_PLAYLIST)
558                 screen_queue_restore_selection();
560         current_song_id = c->status != NULL &&
561                 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
562                  mpd_status_get_state(c->status) == MPD_STATE_PAUSE)
563                 ? (int)mpd_status_get_song_id(c->status) : -1;
565         if (current_song_id != prev_song_id) {
566                 prev_song_id = current_song_id;
568                 /* center the cursor */
569                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
570                         center_playing_item(c, false);
572                 screen_queue_repaint();
573         } else if (c->events & MPD_IDLE_PLAYLIST) {
574                 /* the playlist has changed, we must paint the new
575                    version */
576                 screen_queue_repaint();
577         }
580 #ifdef HAVE_GETMOUSE
581 static bool
582 handle_mouse_event(struct mpdclient *c)
584         int row;
585         unsigned selected;
586         unsigned long bstate;
588         if (screen_get_mouse_event(c, &bstate, &row) ||
589             list_window_mouse(lw, bstate, row)) {
590                 screen_queue_repaint();
591                 return true;
592         }
594         if (bstate & BUTTON1_DOUBLE_CLICKED) {
595                 /* stop */
596                 screen_cmd(c, CMD_STOP);
597                 return true;
598         }
600         selected = lw->start + row;
602         if (bstate & BUTTON1_CLICKED) {
603                 /* play */
604                 if (lw->start + row < playlist_length(playlist))
605                         mpdclient_cmd_play(c, lw->start + row);
606         } else if (bstate & BUTTON3_CLICKED) {
607                 /* delete */
608                 if (selected == lw->selected)
609                         mpdclient_cmd_delete(c, lw->selected);
610         }
612         list_window_set_cursor(lw, selected);
613         screen_queue_save_selection();
614         screen_queue_repaint();
616         return true;
618 #endif
620 static bool
621 screen_queue_cmd(struct mpdclient *c, command_t cmd)
623         struct mpd_connection *connection;
624         static command_t cached_cmd = CMD_NONE;
625         command_t prev_cmd = cached_cmd;
626         struct list_window_range range;
628         cached_cmd = cmd;
630         lw->hide_cursor = false;
632         if (options.hide_cursor > 0) {
633                 if (timer_hide_cursor_id != 0)
634                         g_source_remove(timer_hide_cursor_id);
635                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
636                                                      timer_hide_cursor, c);
637         }
639         if (list_window_cmd(lw, cmd)) {
640                 screen_queue_save_selection();
641                 screen_queue_repaint();
642                 return true;
643         }
645         switch(cmd) {
646         case CMD_SCREEN_UPDATE:
647                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
648                 screen_queue_repaint();
649                 return false;
650         case CMD_SELECT_PLAYING:
651                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
652                                                               c->song));
653                 screen_queue_save_selection();
654                 screen_queue_repaint();
655                 return true;
657         case CMD_LIST_FIND:
658         case CMD_LIST_RFIND:
659         case CMD_LIST_FIND_NEXT:
660         case CMD_LIST_RFIND_NEXT:
661                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
662                 screen_queue_save_selection();
663                 screen_queue_repaint();
664                 return true;
665         case CMD_LIST_JUMP:
666                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
667                 screen_queue_save_selection();
668                 screen_queue_repaint();
669                 return true;
671 #ifdef HAVE_GETMOUSE
672         case CMD_MOUSE_EVENT:
673                 return handle_mouse_event(c);
674 #endif
676 #ifdef ENABLE_SONG_SCREEN
677         case CMD_SCREEN_SONG:
678                 if (screen_queue_selected_song() != NULL) {
679                         screen_song_switch(c, screen_queue_selected_song());
680                         return true;
681                 }
683                 break;
684 #endif
686 #ifdef ENABLE_LYRICS_SCREEN
687         case CMD_SCREEN_LYRICS:
688                 if (lw->selected < playlist_length(&c->playlist)) {
689                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
690                         bool follow = false;
692                         if (c->song && selected &&
693                             !strcmp(mpd_song_get_uri(selected),
694                                     mpd_song_get_uri(c->song)))
695                                 follow = true;
697                         screen_lyrics_switch(c, selected, follow);
698                         return true;
699                 }
701                 break;
702 #endif
703         case CMD_SCREEN_SWAP:
704                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
705                 return true;
707         default:
708                 break;
709         }
711         if (!mpdclient_is_connected(c))
712                 return false;
714         switch(cmd) {
715         case CMD_PLAY:
716                 mpdclient_cmd_play(c, lw->selected);
717                 return true;
719         case CMD_DELETE:
720                 list_window_get_range(lw, &range);
721                 mpdclient_cmd_delete_range(c, range.start, range.end);
723                 list_window_set_cursor(lw, range.start);
724                 return true;
726         case CMD_SAVE_PLAYLIST:
727                 playlist_save(c, NULL, NULL);
728                 return true;
730         case CMD_ADD:
731                 handle_add_to_playlist(c);
732                 return true;
734         case CMD_SHUFFLE:
735                 list_window_get_range(lw, &range);
736                 if (range.end < range.start + 1)
737                         /* No range selection, shuffle all list. */
738                         break;
740                 connection = mpdclient_get_connection(c);
741                 if (mpd_run_shuffle_range(connection, range.start, range.end))
742                         screen_status_message(_("Shuffled playlist"));
743                 else
744                         mpdclient_handle_error(c);
745                 return true;
747         case CMD_LIST_MOVE_UP:
748                 list_window_get_range(lw, &range);
749                 if (range.start == 0 || range.end <= range.start)
750                         return false;
752                 for (unsigned i = range.start; i < range.end; ++i)
753                         mpdclient_cmd_move(c, i, i - 1);
755                 lw->selected--;
756                 lw->range_base--;
758                 screen_queue_save_selection();
759                 return true;
761         case CMD_LIST_MOVE_DOWN:
762                 list_window_get_range(lw, &range);
763                 if (range.end >= playlist_length(&c->playlist))
764                         return false;
766                 for (int i = range.end - 1; i >= (int)range.start; --i)
767                         mpdclient_cmd_move(c, i, i + 1);
769                 lw->selected++;
770                 lw->range_base++;
772                 screen_queue_save_selection();
773                 return true;
775         case CMD_LOCATE:
776                 if (screen_queue_selected_song() != NULL) {
777                         screen_file_goto_song(c, screen_queue_selected_song());
778                         return true;
779                 }
781                 break;
783         default:
784                 break;
785         }
787         return false;
790 const struct screen_functions screen_queue = {
791         .init = screen_queue_init,
792         .exit = screen_queue_exit,
793         .open = screen_queue_open,
794         .close = screen_queue_close,
795         .resize = screen_queue_resize,
796         .paint = screen_queue_paint,
797         .update = screen_queue_update,
798         .cmd = screen_queue_cmd,
799         .get_title = screen_queue_title,
800 };