Code

screen_queue: set cursor before button handlers
[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 "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 /*
60 static struct hscroll hscroll;
61 static guint scroll_source_id;
62 */
63 #endif
65 static struct mpdclient_playlist *playlist;
66 static int current_song_id = -1;
67 static int selected_song_id = -1;
68 static struct list_window *lw;
69 static guint timer_hide_cursor_id;
71 static void
72 screen_queue_paint(void);
74 static void
75 screen_queue_repaint(void)
76 {
77         screen_queue_paint();
78         wrefresh(lw->w);
79 }
81 static const struct mpd_song *
82 screen_queue_selected_song(void)
83 {
84         return !lw->range_selection &&
85                 lw->selected < playlist_length(playlist)
86                 ? playlist_get(playlist, lw->selected)
87                 : NULL;
88 }
90 static void
91 screen_queue_save_selection(void)
92 {
93         selected_song_id = screen_queue_selected_song() != NULL
94                 ? (int)mpd_song_get_id(screen_queue_selected_song())
95                 : -1;
96 }
98 static void
99 screen_queue_restore_selection(void)
101         const struct mpd_song *song;
102         int pos;
104         list_window_set_length(lw, playlist_length(playlist));
106         if (selected_song_id < 0)
107                 /* there was no selection */
108                 return;
110         song = screen_queue_selected_song();
111         if (song != NULL &&
112             mpd_song_get_id(song) == (unsigned)selected_song_id)
113                 /* selection is still valid */
114                 return;
116         pos = playlist_get_index_from_id(playlist, selected_song_id);
117         if (pos >= 0)
118                 list_window_set_cursor(lw, pos);
120         screen_queue_save_selection();
123 /*
124 #ifndef NCMPC_MINI
125 static gboolean
126 scroll_timer_callback(G_GNUC_UNUSED gpointer data)
128         scroll_source_id = 0;
130         hscroll_step(&hscroll);
131         screen_queue_repaint();
132         return false;
134 #endif
135 */
137 static const char *
138 screen_queue_lw_callback(unsigned idx, G_GNUC_UNUSED void *data)
140         static char songname[MAX_SONG_LENGTH];
141         struct mpd_song *song;
143         assert(playlist != NULL);
144         assert(idx < playlist_length(playlist));
146         song = playlist_get(playlist, idx);
148         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
150         /*
151 #ifndef NCMPC_MINI
152         if (idx == lw->selected)
153         {
154                 if (options.scroll && utf8_width(songname) > (unsigned)COLS) {
155                         static unsigned current_song;
156                         char *tmp;
158                         if (current_song != lw->selected) {
159                                 hscroll_reset(&hscroll);
160                                 current_song = lw->selected;
161                         }
163                         tmp = strscroll(&hscroll, songname, options.scroll_sep,
164                                         MAX_SONG_LENGTH);
165                         g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
166                         g_free(tmp);
168                         if (scroll_source_id == 0)
169                                 scroll_source_id =
170                                         g_timeout_add(1000,
171                                                       scroll_timer_callback,
172                                                       NULL);
173                 } else {
174                         hscroll_reset(&hscroll);
176                         if (scroll_source_id != 0) {
177                                 g_source_remove(scroll_source_id);
178                                 scroll_source_id = 0;
179                         }
180                 }
181         }
182 #endif
183         */
185         return songname;
188 static void
189 center_playing_item(struct mpdclient *c, bool center_cursor)
191         const struct mpd_song *song;
192         unsigned length = c->playlist.list->len;
193         int idx;
195         song = mpdclient_get_current_song(c);
196         if (song == NULL)
197                 return;
199         /* try to center the song that are playing */
200         idx = playlist_get_index(&c->playlist, c->song);
201         if (idx < 0)
202                 return;
204         if (length < lw->rows)
205         {
206                 if (center_cursor)
207                         list_window_set_cursor(lw, idx);
208                 return;
209         }
211         list_window_center(lw, idx);
213         if (center_cursor) {
214                 list_window_set_cursor(lw, idx);
215                 return;
216         }
218         /* make sure the cursor is in the window */
219         list_window_fetch_cursor(lw);
222 #ifndef NCMPC_MINI
223 static void
224 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
225                        void *data)
227         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
228         GList **list = tmp->list;
229         struct mpdclient *c = tmp->c;
231         if( *list == NULL ) {
232                 /* create completion list */
233                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
234                 g_completion_add_items(gcmp, *list);
235         }
238 static void
239 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
240                         G_GNUC_UNUSED gchar *line, GList *items,
241                         G_GNUC_UNUSED void *data)
243         if (g_list_length(items) >= 1)
244                 screen_display_completion_list(items);
246 #endif
248 #ifndef NCMPC_MINI
249 /**
250  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
251  * g_completion_set_compare(), because strncmp() takes size_t where
252  * g_completion_set_compare passes a gsize value.
253  */
254 static gint
255 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
257         return strncmp(s1, s2, n);
259 #endif
261 int
262 playlist_save(struct mpdclient *c, char *name, char *defaultname)
264         struct mpd_connection *connection;
265         gchar *filename, *filename_utf8;
266 #ifndef NCMPC_MINI
267         GCompletion *gcmp;
268         GList *list = NULL;
269         completion_callback_data_t data;
270 #endif
272 #ifdef NCMPC_MINI
273         (void)defaultname;
274 #endif
276 #ifndef NCMPC_MINI
277         if (name == NULL) {
278                 /* initialize completion support */
279                 gcmp = g_completion_new(NULL);
280                 g_completion_set_compare(gcmp, completion_strncmp);
281                 data.list = &list;
282                 data.dir_list = NULL;
283                 data.c = c;
284                 wrln_completion_callback_data = &data;
285                 wrln_pre_completion_callback = save_pre_completion_cb;
286                 wrln_post_completion_callback = save_post_completion_cb;
289                 /* query the user for a filename */
290                 filename = screen_readln(_("Save playlist as"),
291                                          defaultname,
292                                          NULL,
293                                          gcmp);
295                 /* destroy completion support */
296                 wrln_completion_callback_data = NULL;
297                 wrln_pre_completion_callback = NULL;
298                 wrln_post_completion_callback = NULL;
299                 g_completion_free(gcmp);
300                 list = string_list_free(list);
301                 if( filename )
302                         filename=g_strstrip(filename);
303         } else
304 #endif
305                         filename=g_strdup(name);
307         if (filename == NULL)
308                 return -1;
310         /* send save command to mpd */
312         connection = mpdclient_get_connection(c);
313         if (connection == NULL)
314                 return -1;
316         filename_utf8 = locale_to_utf8(filename);
317         if (!mpd_run_save(connection, filename_utf8)) {
318                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
319                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
320                     mpd_connection_clear_error(connection)) {
321                         char *buf;
322                         int key;
324                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
325                                               filename, YES, NO);
326                         key = tolower(screen_getch(buf));
327                         g_free(buf);
329                         if (key != YES[0]) {
330                                 g_free(filename_utf8);
331                                 g_free(filename);
332                                 screen_status_printf(_("Aborted"));
333                                 return -1;
334                         }
336                         if (!mpd_run_rm(connection, filename_utf8) ||
337                             !mpd_run_save(connection, filename_utf8)) {
338                                 mpdclient_handle_error(c);
339                                 g_free(filename_utf8);
340                                 g_free(filename);
341                                 return -1;
342                         }
343                 } else {
344                         mpdclient_handle_error(c);
345                         g_free(filename_utf8);
346                         g_free(filename);
347                         return -1;
348                 }
349         }
351         c->events |= MPD_IDLE_STORED_PLAYLIST;
353         g_free(filename_utf8);
355         /* success */
356         screen_status_printf(_("Saved %s"), filename);
357         g_free(filename);
358         return 0;
361 #ifndef NCMPC_MINI
362 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
363                     GList **list, struct mpdclient *c)
365         g_completion_remove_items(gcmp, *list);
366         *list = string_list_remove(*list, dir);
367         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
368         g_completion_add_items(gcmp, *list);
369         *dir_list = g_list_append(*dir_list, g_strdup(dir));
372 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
374         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
375         GList **dir_list = tmp->dir_list;
376         GList **list = tmp->list;
377         struct mpdclient *c = tmp->c;
379         if (*list == NULL) {
380                 /* create initial list */
381                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
382                 g_completion_add_items(gcmp, *list);
383         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
384                    string_list_find(*dir_list, line) == NULL) {
385                 /* add directory content to list */
386                 add_dir(gcmp, line, dir_list, list, c);
387         }
390 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
391                                    GList *items, void *data)
393         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
394         GList **dir_list = tmp->dir_list;
395         GList **list = tmp->list;
396         struct mpdclient *c = tmp->c;
398         if (g_list_length(items) >= 1)
399                 screen_display_completion_list(items);
401         if (line && line[0] && line[strlen(line) - 1] == '/' &&
402             string_list_find(*dir_list, line) == NULL) {
403                 /* add directory content to list */
404                 add_dir(gcmp, line, dir_list, list, c);
405         }
407 #endif
409 static int
410 handle_add_to_playlist(struct mpdclient *c)
412         gchar *path;
413 #ifndef NCMPC_MINI
414         GCompletion *gcmp;
415         GList *list = NULL;
416         GList *dir_list = NULL;
417         completion_callback_data_t data;
419         /* initialize completion support */
420         gcmp = g_completion_new(NULL);
421         g_completion_set_compare(gcmp, completion_strncmp);
422         data.list = &list;
423         data.dir_list = &dir_list;
424         data.c = c;
425         wrln_completion_callback_data = &data;
426         wrln_pre_completion_callback = add_pre_completion_cb;
427         wrln_post_completion_callback = add_post_completion_cb;
428 #endif
430         /* get path */
431         path = screen_readln(_("Add"),
432                              NULL,
433                              NULL,
434 #ifdef NCMPC_MINI
435                              NULL
436 #else
437                              gcmp
438 #endif
439                              );
441         /* destroy completion data */
442 #ifndef NCMPC_MINI
443         wrln_completion_callback_data = NULL;
444         wrln_pre_completion_callback = NULL;
445         wrln_post_completion_callback = NULL;
446         g_completion_free(gcmp);
447         string_list_free(list);
448         string_list_free(dir_list);
449 #endif
451         /* add the path to the playlist */
452         if (path != NULL) {
453                 char *path_utf8 = locale_to_utf8(path);
454                 mpdclient_cmd_add_path(c, path_utf8);
455                 g_free(path_utf8);
456         }
458         g_free(path);
459         return 0;
462 static void
463 screen_queue_init(WINDOW *w, int cols, int rows)
465         lw = list_window_init(w, cols, rows);
468 static gboolean
469 timer_hide_cursor(gpointer data)
471         struct mpdclient *c = data;
473         assert(options.hide_cursor > 0);
474         assert(timer_hide_cursor_id != 0);
476         timer_hide_cursor_id = 0;
478         /* hide the cursor when mpd is playing and the user is inactive */
480         if (c->status != NULL &&
481             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
482                 lw->hide_cursor = true;
483                 screen_queue_repaint();
484         } else
485                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
486                                                      timer_hide_cursor, c);
488         return FALSE;
491 static void
492 screen_queue_open(struct mpdclient *c)
494         playlist = &c->playlist;
496         assert(timer_hide_cursor_id == 0);
497         if (options.hide_cursor > 0) {
498                 lw->hide_cursor = false;
499                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
500                                                      timer_hide_cursor, c);
501         }
503         screen_queue_restore_selection();
506 static void
507 screen_queue_close(void)
509         if (timer_hide_cursor_id != 0) {
510                 g_source_remove(timer_hide_cursor_id);
511                 timer_hide_cursor_id = 0;
512         }
515 static void
516 screen_queue_resize(int cols, int rows)
518         list_window_resize(lw, cols, rows);
522 static void
523 screen_queue_exit(void)
525         list_window_free(lw);
528 static const char *
529 screen_queue_title(char *str, size_t size)
531         if (options.host == NULL)
532                 return _("Playlist");
534         g_snprintf(str, size, _("Playlist on %s"), options.host);
535         return str;
538 static void
539 screen_queue_paint_callback(WINDOW *w, unsigned i,
540                             unsigned y, unsigned width,
541                             bool selected, G_GNUC_UNUSED void *data)
543         const struct mpd_song *song;
545         assert(playlist != NULL);
546         assert(i < playlist_length(playlist));
548         song = playlist_get(playlist, i);
550         paint_song_row(w, y, width, selected,
551                        (int)mpd_song_get_id(song) == current_song_id,
552                        song);
555 static void
556 screen_queue_paint(void)
558         list_window_paint2(lw, screen_queue_paint_callback, NULL);
561 G_GNUC_PURE
562 static int
563 get_current_song_id(const struct mpd_status *status)
565         return status != NULL &&
566                 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
567                  mpd_status_get_state(status) == MPD_STATE_PAUSE)
568                 ? (int)mpd_status_get_song_id(status)
569                 : -1;
572 static void
573 screen_queue_update(struct mpdclient *c)
575         if (c->events & MPD_IDLE_PLAYLIST)
576                 screen_queue_restore_selection();
578         if ((c->events & MPD_IDLE_PLAYER) != 0 &&
579             get_current_song_id(c->status) != current_song_id) {
580                 current_song_id = get_current_song_id(c->status);
582                 /* center the cursor */
583                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
584                         center_playing_item(c, false);
586                 screen_queue_repaint();
587         } else if (c->events & MPD_IDLE_PLAYLIST) {
588                 /* the playlist has changed, we must paint the new
589                    version */
590                 screen_queue_repaint();
591         }
594 #ifdef HAVE_GETMOUSE
595 static bool
596 handle_mouse_event(struct mpdclient *c)
598         int row;
599         unsigned long bstate;
600         unsigned old_selected;
602         if (screen_get_mouse_event(c, &bstate, &row) ||
603             list_window_mouse(lw, bstate, row)) {
604                 screen_queue_repaint();
605                 return true;
606         }
608         if (bstate & BUTTON1_DOUBLE_CLICKED) {
609                 /* stop */
610                 screen_cmd(c, CMD_STOP);
611                 return true;
612         }
614         old_selected = lw->selected;
615         list_window_set_cursor(lw, lw->start + row);
617         if (bstate & BUTTON1_CLICKED) {
618                 /* play */
619                 if (lw->selected < playlist_length(playlist))
620                         mpdclient_cmd_play(c, lw->start + row);
621         } else if (bstate & BUTTON3_CLICKED) {
622                 /* delete */
623                 if (lw->selected == old_selected)
624                         mpdclient_cmd_delete(c, lw->selected);
626                 list_window_set_length(lw, playlist_length(playlist));
627         }
629         screen_queue_save_selection();
630         screen_queue_repaint();
632         return true;
634 #endif
636 static bool
637 screen_queue_cmd(struct mpdclient *c, command_t cmd)
639         struct mpd_connection *connection;
640         static command_t cached_cmd = CMD_NONE;
641         command_t prev_cmd = cached_cmd;
642         struct list_window_range range;
644         cached_cmd = cmd;
646         lw->hide_cursor = false;
648         if (options.hide_cursor > 0) {
649                 if (timer_hide_cursor_id != 0)
650                         g_source_remove(timer_hide_cursor_id);
651                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
652                                                      timer_hide_cursor, c);
653         }
655         if (list_window_cmd(lw, cmd)) {
656                 screen_queue_save_selection();
657                 screen_queue_repaint();
658                 return true;
659         }
661         switch(cmd) {
662         case CMD_SCREEN_UPDATE:
663                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
664                 screen_queue_repaint();
665                 return false;
666         case CMD_SELECT_PLAYING:
667                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
668                                                               c->song));
669                 screen_queue_save_selection();
670                 screen_queue_repaint();
671                 return true;
673         case CMD_LIST_FIND:
674         case CMD_LIST_RFIND:
675         case CMD_LIST_FIND_NEXT:
676         case CMD_LIST_RFIND_NEXT:
677                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
678                 screen_queue_save_selection();
679                 screen_queue_repaint();
680                 return true;
681         case CMD_LIST_JUMP:
682                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
683                 screen_queue_save_selection();
684                 screen_queue_repaint();
685                 return true;
687 #ifdef HAVE_GETMOUSE
688         case CMD_MOUSE_EVENT:
689                 return handle_mouse_event(c);
690 #endif
692 #ifdef ENABLE_SONG_SCREEN
693         case CMD_SCREEN_SONG:
694                 if (screen_queue_selected_song() != NULL) {
695                         screen_song_switch(c, screen_queue_selected_song());
696                         return true;
697                 }
699                 break;
700 #endif
702 #ifdef ENABLE_LYRICS_SCREEN
703         case CMD_SCREEN_LYRICS:
704                 if (lw->selected < playlist_length(&c->playlist)) {
705                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
706                         bool follow = false;
708                         if (c->song && selected &&
709                             !strcmp(mpd_song_get_uri(selected),
710                                     mpd_song_get_uri(c->song)))
711                                 follow = true;
713                         screen_lyrics_switch(c, selected, follow);
714                         return true;
715                 }
717                 break;
718 #endif
719         case CMD_SCREEN_SWAP:
720                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
721                 return true;
723         default:
724                 break;
725         }
727         if (!mpdclient_is_connected(c))
728                 return false;
730         switch(cmd) {
731         case CMD_PLAY:
732                 mpdclient_cmd_play(c, lw->selected);
733                 return true;
735         case CMD_DELETE:
736                 list_window_get_range(lw, &range);
737                 mpdclient_cmd_delete_range(c, range.start, range.end);
739                 list_window_set_cursor(lw, range.start);
740                 return true;
742         case CMD_SAVE_PLAYLIST:
743                 playlist_save(c, NULL, NULL);
744                 return true;
746         case CMD_ADD:
747                 handle_add_to_playlist(c);
748                 return true;
750         case CMD_SHUFFLE:
751                 list_window_get_range(lw, &range);
752                 if (range.end < range.start + 1)
753                         /* No range selection, shuffle all list. */
754                         break;
756                 connection = mpdclient_get_connection(c);
757                 if (connection == NULL)
758                         return true;
760                 if (mpd_run_shuffle_range(connection, range.start, range.end))
761                         screen_status_message(_("Shuffled playlist"));
762                 else
763                         mpdclient_handle_error(c);
764                 return true;
766         case CMD_LIST_MOVE_UP:
767                 list_window_get_range(lw, &range);
768                 if (range.start == 0 || range.end <= range.start)
769                         return false;
771                 for (unsigned i = range.start; i < range.end; ++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_LIST_MOVE_DOWN:
781                 list_window_get_range(lw, &range);
782                 if (range.end >= playlist_length(&c->playlist))
783                         return false;
785                 for (int i = range.end - 1; i >= (int)range.start; --i)
786                         mpdclient_cmd_move(c, i, i + 1);
788                 lw->selected++;
789                 lw->range_base++;
791                 screen_queue_save_selection();
792                 return true;
794         case CMD_LOCATE:
795                 if (screen_queue_selected_song() != NULL) {
796                         screen_file_goto_song(c, screen_queue_selected_song());
797                         return true;
798                 }
800                 break;
802         default:
803                 break;
804         }
806         return false;
809 const struct screen_functions screen_queue = {
810         .init = screen_queue_init,
811         .exit = screen_queue_exit,
812         .open = screen_queue_open,
813         .close = screen_queue_close,
814         .resize = screen_queue_resize,
815         .paint = screen_queue_paint,
816         .update = screen_queue_update,
817         .cmd = screen_queue_cmd,
818         .get_title = screen_queue_title,
819 };