Code

screen_queue: check current song only if PLAYER idle event is set
[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 G_GNUC_PURE
553 static int
554 get_current_song_id(const struct mpd_status *status)
556         return status != NULL &&
557                 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
558                  mpd_status_get_state(status) == MPD_STATE_PAUSE)
559                 ? (int)mpd_status_get_song_id(status)
560                 : -1;
563 static void
564 screen_queue_update(struct mpdclient *c)
566         if (c->events & MPD_IDLE_PLAYLIST)
567                 screen_queue_restore_selection();
569         if ((c->events & MPD_IDLE_PLAYER) != 0 &&
570             get_current_song_id(c->status) != current_song_id) {
571                 current_song_id = get_current_song_id(c->status);
573                 /* center the cursor */
574                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
575                         center_playing_item(c, false);
577                 screen_queue_repaint();
578         } else if (c->events & MPD_IDLE_PLAYLIST) {
579                 /* the playlist has changed, we must paint the new
580                    version */
581                 screen_queue_repaint();
582         }
585 #ifdef HAVE_GETMOUSE
586 static bool
587 handle_mouse_event(struct mpdclient *c)
589         int row;
590         unsigned selected;
591         unsigned long bstate;
593         if (screen_get_mouse_event(c, &bstate, &row) ||
594             list_window_mouse(lw, bstate, row)) {
595                 screen_queue_repaint();
596                 return true;
597         }
599         if (bstate & BUTTON1_DOUBLE_CLICKED) {
600                 /* stop */
601                 screen_cmd(c, CMD_STOP);
602                 return true;
603         }
605         selected = lw->start + row;
607         if (bstate & BUTTON1_CLICKED) {
608                 /* play */
609                 if (lw->start + row < playlist_length(playlist))
610                         mpdclient_cmd_play(c, lw->start + row);
611         } else if (bstate & BUTTON3_CLICKED) {
612                 /* delete */
613                 if (selected == lw->selected)
614                         mpdclient_cmd_delete(c, lw->selected);
615         }
617         list_window_set_cursor(lw, selected);
618         screen_queue_save_selection();
619         screen_queue_repaint();
621         return true;
623 #endif
625 static bool
626 screen_queue_cmd(struct mpdclient *c, command_t cmd)
628         struct mpd_connection *connection;
629         static command_t cached_cmd = CMD_NONE;
630         command_t prev_cmd = cached_cmd;
631         struct list_window_range range;
633         cached_cmd = cmd;
635         lw->hide_cursor = false;
637         if (options.hide_cursor > 0) {
638                 if (timer_hide_cursor_id != 0)
639                         g_source_remove(timer_hide_cursor_id);
640                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
641                                                      timer_hide_cursor, c);
642         }
644         if (list_window_cmd(lw, cmd)) {
645                 screen_queue_save_selection();
646                 screen_queue_repaint();
647                 return true;
648         }
650         switch(cmd) {
651         case CMD_SCREEN_UPDATE:
652                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
653                 screen_queue_repaint();
654                 return false;
655         case CMD_SELECT_PLAYING:
656                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
657                                                               c->song));
658                 screen_queue_save_selection();
659                 screen_queue_repaint();
660                 return true;
662         case CMD_LIST_FIND:
663         case CMD_LIST_RFIND:
664         case CMD_LIST_FIND_NEXT:
665         case CMD_LIST_RFIND_NEXT:
666                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
667                 screen_queue_save_selection();
668                 screen_queue_repaint();
669                 return true;
670         case CMD_LIST_JUMP:
671                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
672                 screen_queue_save_selection();
673                 screen_queue_repaint();
674                 return true;
676 #ifdef HAVE_GETMOUSE
677         case CMD_MOUSE_EVENT:
678                 return handle_mouse_event(c);
679 #endif
681 #ifdef ENABLE_SONG_SCREEN
682         case CMD_SCREEN_SONG:
683                 if (screen_queue_selected_song() != NULL) {
684                         screen_song_switch(c, screen_queue_selected_song());
685                         return true;
686                 }
688                 break;
689 #endif
691 #ifdef ENABLE_LYRICS_SCREEN
692         case CMD_SCREEN_LYRICS:
693                 if (lw->selected < playlist_length(&c->playlist)) {
694                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
695                         bool follow = false;
697                         if (c->song && selected &&
698                             !strcmp(mpd_song_get_uri(selected),
699                                     mpd_song_get_uri(c->song)))
700                                 follow = true;
702                         screen_lyrics_switch(c, selected, follow);
703                         return true;
704                 }
706                 break;
707 #endif
708         case CMD_SCREEN_SWAP:
709                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
710                 return true;
712         default:
713                 break;
714         }
716         if (!mpdclient_is_connected(c))
717                 return false;
719         switch(cmd) {
720         case CMD_PLAY:
721                 mpdclient_cmd_play(c, lw->selected);
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 (mpd_run_shuffle_range(connection, range.start, range.end))
747                         screen_status_message(_("Shuffled playlist"));
748                 else
749                         mpdclient_handle_error(c);
750                 return true;
752         case CMD_LIST_MOVE_UP:
753                 list_window_get_range(lw, &range);
754                 if (range.start == 0 || range.end <= range.start)
755                         return false;
757                 for (unsigned i = range.start; i < range.end; ++i)
758                         mpdclient_cmd_move(c, i, i - 1);
760                 lw->selected--;
761                 lw->range_base--;
763                 screen_queue_save_selection();
764                 return true;
766         case CMD_LIST_MOVE_DOWN:
767                 list_window_get_range(lw, &range);
768                 if (range.end >= playlist_length(&c->playlist))
769                         return false;
771                 for (int i = range.end - 1; i >= (int)range.start; --i)
772                         mpdclient_cmd_move(c, i, i + 1);
774                 lw->selected++;
775                 lw->range_base++;
777                 screen_queue_save_selection();
778                 return true;
780         case CMD_LOCATE:
781                 if (screen_queue_selected_song() != NULL) {
782                         screen_file_goto_song(c, screen_queue_selected_song());
783                         return true;
784                 }
786                 break;
788         default:
789                 break;
790         }
792         return false;
795 const struct screen_functions screen_queue = {
796         .init = screen_queue_init,
797         .exit = screen_queue_exit,
798         .open = screen_queue_open,
799         .close = screen_queue_close,
800         .resize = screen_queue_resize,
801         .paint = screen_queue_paint,
802         .update = screen_queue_update,
803         .cmd = screen_queue_cmd,
804         .get_title = screen_queue_title,
805 };