Code

hscroll: reimplemented the hscroll library
[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 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(struct mpdclient *c, bool center_cursor)
139         const struct mpd_song *song;
140         unsigned length = c->playlist.list->len;
141         int idx;
143         song = mpdclient_get_current_song(c);
144         if (song == NULL)
145                 return;
147         /* try to center the song that are playing */
148         idx = playlist_get_index(&c->playlist, c->song);
149         if (idx < 0)
150                 return;
152         if (length < lw->rows)
153         {
154                 if (center_cursor)
155                         list_window_set_cursor(lw, idx);
156                 return;
157         }
159         list_window_center(lw, idx);
161         if (center_cursor) {
162                 list_window_set_cursor(lw, idx);
163                 return;
164         }
166         /* make sure the cursor is in the window */
167         list_window_fetch_cursor(lw);
170 #ifndef NCMPC_MINI
171 static void
172 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
173                        void *data)
175         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
176         GList **list = tmp->list;
177         struct mpdclient *c = tmp->c;
179         if( *list == NULL ) {
180                 /* create completion list */
181                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
182                 g_completion_add_items(gcmp, *list);
183         }
186 static void
187 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
188                         G_GNUC_UNUSED gchar *line, GList *items,
189                         G_GNUC_UNUSED void *data)
191         if (g_list_length(items) >= 1)
192                 screen_display_completion_list(items);
194 #endif
196 #ifndef NCMPC_MINI
197 /**
198  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
199  * g_completion_set_compare(), because strncmp() takes size_t where
200  * g_completion_set_compare passes a gsize value.
201  */
202 static gint
203 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
205         return strncmp(s1, s2, n);
207 #endif
209 int
210 playlist_save(struct mpdclient *c, char *name, char *defaultname)
212         struct mpd_connection *connection;
213         gchar *filename, *filename_utf8;
214 #ifndef NCMPC_MINI
215         GCompletion *gcmp;
216         GList *list = NULL;
217         completion_callback_data_t data;
218 #endif
220 #ifdef NCMPC_MINI
221         (void)defaultname;
222 #endif
224 #ifndef NCMPC_MINI
225         if (name == NULL) {
226                 /* initialize completion support */
227                 gcmp = g_completion_new(NULL);
228                 g_completion_set_compare(gcmp, completion_strncmp);
229                 data.list = &list;
230                 data.dir_list = NULL;
231                 data.c = c;
232                 wrln_completion_callback_data = &data;
233                 wrln_pre_completion_callback = save_pre_completion_cb;
234                 wrln_post_completion_callback = save_post_completion_cb;
237                 /* query the user for a filename */
238                 filename = screen_readln(_("Save playlist as"),
239                                          defaultname,
240                                          NULL,
241                                          gcmp);
243                 /* destroy completion support */
244                 wrln_completion_callback_data = NULL;
245                 wrln_pre_completion_callback = NULL;
246                 wrln_post_completion_callback = NULL;
247                 g_completion_free(gcmp);
248                 list = string_list_free(list);
249                 if( filename )
250                         filename=g_strstrip(filename);
251         } else
252 #endif
253                         filename=g_strdup(name);
255         if (filename == NULL)
256                 return -1;
258         /* send save command to mpd */
260         connection = mpdclient_get_connection(c);
261         if (connection == NULL)
262                 return -1;
264         filename_utf8 = locale_to_utf8(filename);
265         if (!mpd_run_save(connection, filename_utf8)) {
266                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
267                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
268                     mpd_connection_clear_error(connection)) {
269                         char *buf;
270                         int key;
272                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
273                                               filename, YES, NO);
274                         key = tolower(screen_getch(buf));
275                         g_free(buf);
277                         if (key != YES[0]) {
278                                 g_free(filename_utf8);
279                                 g_free(filename);
280                                 screen_status_printf(_("Aborted"));
281                                 return -1;
282                         }
284                         if (!mpd_run_rm(connection, filename_utf8) ||
285                             !mpd_run_save(connection, filename_utf8)) {
286                                 mpdclient_handle_error(c);
287                                 g_free(filename_utf8);
288                                 g_free(filename);
289                                 return -1;
290                         }
291                 } else {
292                         mpdclient_handle_error(c);
293                         g_free(filename_utf8);
294                         g_free(filename);
295                         return -1;
296                 }
297         }
299         c->events |= MPD_IDLE_STORED_PLAYLIST;
301         g_free(filename_utf8);
303         /* success */
304         screen_status_printf(_("Saved %s"), filename);
305         g_free(filename);
306         return 0;
309 #ifndef NCMPC_MINI
310 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
311                     GList **list, struct mpdclient *c)
313         g_completion_remove_items(gcmp, *list);
314         *list = string_list_remove(*list, dir);
315         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
316         g_completion_add_items(gcmp, *list);
317         *dir_list = g_list_append(*dir_list, g_strdup(dir));
320 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
322         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
323         GList **dir_list = tmp->dir_list;
324         GList **list = tmp->list;
325         struct mpdclient *c = tmp->c;
327         if (*list == NULL) {
328                 /* create initial list */
329                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
330                 g_completion_add_items(gcmp, *list);
331         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
332                    string_list_find(*dir_list, line) == NULL) {
333                 /* add directory content to list */
334                 add_dir(gcmp, line, dir_list, list, c);
335         }
338 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
339                                    GList *items, void *data)
341         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
342         GList **dir_list = tmp->dir_list;
343         GList **list = tmp->list;
344         struct mpdclient *c = tmp->c;
346         if (g_list_length(items) >= 1)
347                 screen_display_completion_list(items);
349         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         }
355 #endif
357 static int
358 handle_add_to_playlist(struct mpdclient *c)
360         gchar *path;
361 #ifndef NCMPC_MINI
362         GCompletion *gcmp;
363         GList *list = NULL;
364         GList *dir_list = NULL;
365         completion_callback_data_t data;
367         /* initialize completion support */
368         gcmp = g_completion_new(NULL);
369         g_completion_set_compare(gcmp, completion_strncmp);
370         data.list = &list;
371         data.dir_list = &dir_list;
372         data.c = c;
373         wrln_completion_callback_data = &data;
374         wrln_pre_completion_callback = add_pre_completion_cb;
375         wrln_post_completion_callback = add_post_completion_cb;
376 #endif
378         /* get path */
379         path = screen_readln(_("Add"),
380                              NULL,
381                              NULL,
382 #ifdef NCMPC_MINI
383                              NULL
384 #else
385                              gcmp
386 #endif
387                              );
389         /* destroy completion data */
390 #ifndef NCMPC_MINI
391         wrln_completion_callback_data = NULL;
392         wrln_pre_completion_callback = NULL;
393         wrln_post_completion_callback = NULL;
394         g_completion_free(gcmp);
395         string_list_free(list);
396         string_list_free(dir_list);
397 #endif
399         /* add the path to the playlist */
400         if (path != NULL) {
401                 char *path_utf8 = locale_to_utf8(path);
402                 mpdclient_cmd_add_path(c, path_utf8);
403                 g_free(path_utf8);
404         }
406         g_free(path);
407         return 0;
410 static void
411 screen_queue_init(WINDOW *w, int cols, int rows)
413         lw = list_window_init(w, cols, rows);
415 #ifndef NCMPC_MINI
416         if (options.scroll)
417                 hscroll_init(&hscroll, w, options.scroll_sep);
418 #endif
421 static gboolean
422 timer_hide_cursor(gpointer data)
424         struct mpdclient *c = data;
426         assert(options.hide_cursor > 0);
427         assert(timer_hide_cursor_id != 0);
429         timer_hide_cursor_id = 0;
431         /* hide the cursor when mpd is playing and the user is inactive */
433         if (c->status != NULL &&
434             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
435                 lw->hide_cursor = true;
436                 screen_queue_repaint();
437         } else
438                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
439                                                      timer_hide_cursor, c);
441         return FALSE;
444 static void
445 screen_queue_open(struct mpdclient *c)
447         playlist = &c->playlist;
449         assert(timer_hide_cursor_id == 0);
450         if (options.hide_cursor > 0) {
451                 lw->hide_cursor = false;
452                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
453                                                      timer_hide_cursor, c);
454         }
456         screen_queue_restore_selection();
459 static void
460 screen_queue_close(void)
462         if (timer_hide_cursor_id != 0) {
463                 g_source_remove(timer_hide_cursor_id);
464                 timer_hide_cursor_id = 0;
465         }
467 #ifndef NCMPC_MINI
468         if (options.scroll)
469                 hscroll_clear(&hscroll);
470 #endif
473 static void
474 screen_queue_resize(int cols, int rows)
476         list_window_resize(lw, cols, rows);
480 static void
481 screen_queue_exit(void)
483         list_window_free(lw);
486 static const char *
487 screen_queue_title(char *str, size_t size)
489         if (options.host == NULL)
490                 return _("Playlist");
492         g_snprintf(str, size, _("Playlist on %s"), options.host);
493         return str;
496 static void
497 screen_queue_paint_callback(WINDOW *w, unsigned i,
498                             unsigned y, unsigned width,
499                             bool selected, G_GNUC_UNUSED void *data)
501         const struct mpd_song *song;
502         struct hscroll *row_hscroll;
504         assert(playlist != NULL);
505         assert(i < playlist_length(playlist));
507         song = playlist_get(playlist, i);
509 #ifdef NCMPC_MINI
510         row_hscroll = NULL;
511 #else
512         row_hscroll = selected && options.scroll && lw->selected == i
513                 ? &hscroll : NULL;
514 #endif
516         paint_song_row(w, y, width, selected,
517                        (int)mpd_song_get_id(song) == current_song_id,
518                        song, row_hscroll);
521 static void
522 screen_queue_paint(void)
524 #ifndef NCMPC_MINI
525         if (options.scroll)
526                 hscroll_clear(&hscroll);
527 #endif
529         list_window_paint2(lw, screen_queue_paint_callback, NULL);
532 G_GNUC_PURE
533 static int
534 get_current_song_id(const struct mpd_status *status)
536         return status != NULL &&
537                 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
538                  mpd_status_get_state(status) == MPD_STATE_PAUSE)
539                 ? (int)mpd_status_get_song_id(status)
540                 : -1;
543 static void
544 screen_queue_update(struct mpdclient *c)
546         if (c->events & MPD_IDLE_PLAYLIST)
547                 screen_queue_restore_selection();
549         if ((c->events & MPD_IDLE_PLAYER) != 0 &&
550             get_current_song_id(c->status) != current_song_id) {
551                 current_song_id = get_current_song_id(c->status);
553                 /* center the cursor */
554                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
555                         center_playing_item(c, false);
557                 screen_queue_repaint();
558         } else if (c->events & MPD_IDLE_PLAYLIST) {
559                 /* the playlist has changed, we must paint the new
560                    version */
561                 screen_queue_repaint();
562         }
565 #ifdef HAVE_GETMOUSE
566 static bool
567 handle_mouse_event(struct mpdclient *c)
569         int row;
570         unsigned long bstate;
571         unsigned old_selected;
573         if (screen_get_mouse_event(c, &bstate, &row) ||
574             list_window_mouse(lw, bstate, row)) {
575                 screen_queue_repaint();
576                 return true;
577         }
579         if (bstate & BUTTON1_DOUBLE_CLICKED) {
580                 /* stop */
581                 screen_cmd(c, CMD_STOP);
582                 return true;
583         }
585         old_selected = lw->selected;
586         list_window_set_cursor(lw, lw->start + row);
588         if (bstate & BUTTON1_CLICKED) {
589                 /* play */
590                 const struct mpd_song *song = screen_queue_selected_song();
591                 if (song != NULL) {
592                         struct mpd_connection *connection =
593                                 mpdclient_get_connection(c);
595                         if (connection != NULL &&
596                             !mpd_run_play_id(connection,
597                                              mpd_song_get_id(song)))
598                                 mpdclient_handle_error(c);
599                 }
600         } else if (bstate & BUTTON3_CLICKED) {
601                 /* delete */
602                 if (lw->selected == old_selected)
603                         mpdclient_cmd_delete(c, lw->selected);
605                 list_window_set_length(lw, playlist_length(playlist));
606         }
608         screen_queue_save_selection();
609         screen_queue_repaint();
611         return true;
613 #endif
615 static bool
616 screen_queue_cmd(struct mpdclient *c, command_t cmd)
618         struct mpd_connection *connection;
619         static command_t cached_cmd = CMD_NONE;
620         command_t prev_cmd = cached_cmd;
621         struct list_window_range range;
622         const struct mpd_song *song;
624         cached_cmd = cmd;
626         lw->hide_cursor = false;
628         if (options.hide_cursor > 0) {
629                 if (timer_hide_cursor_id != 0)
630                         g_source_remove(timer_hide_cursor_id);
631                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
632                                                      timer_hide_cursor, c);
633         }
635         if (list_window_cmd(lw, cmd)) {
636                 screen_queue_save_selection();
637                 screen_queue_repaint();
638                 return true;
639         }
641         switch(cmd) {
642         case CMD_SCREEN_UPDATE:
643                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
644                 screen_queue_repaint();
645                 return false;
646         case CMD_SELECT_PLAYING:
647                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
648                                                               c->song));
649                 screen_queue_save_selection();
650                 screen_queue_repaint();
651                 return true;
653         case CMD_LIST_FIND:
654         case CMD_LIST_RFIND:
655         case CMD_LIST_FIND_NEXT:
656         case CMD_LIST_RFIND_NEXT:
657                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
658                 screen_queue_save_selection();
659                 screen_queue_repaint();
660                 return true;
661         case CMD_LIST_JUMP:
662                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
663                 screen_queue_save_selection();
664                 screen_queue_repaint();
665                 return true;
667 #ifdef HAVE_GETMOUSE
668         case CMD_MOUSE_EVENT:
669                 return handle_mouse_event(c);
670 #endif
672 #ifdef ENABLE_SONG_SCREEN
673         case CMD_SCREEN_SONG:
674                 if (screen_queue_selected_song() != NULL) {
675                         screen_song_switch(c, screen_queue_selected_song());
676                         return true;
677                 }
679                 break;
680 #endif
682 #ifdef ENABLE_LYRICS_SCREEN
683         case CMD_SCREEN_LYRICS:
684                 if (lw->selected < playlist_length(&c->playlist)) {
685                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
686                         bool follow = false;
688                         if (c->song && selected &&
689                             !strcmp(mpd_song_get_uri(selected),
690                                     mpd_song_get_uri(c->song)))
691                                 follow = true;
693                         screen_lyrics_switch(c, selected, follow);
694                         return true;
695                 }
697                 break;
698 #endif
699         case CMD_SCREEN_SWAP:
700                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
701                 return true;
703         default:
704                 break;
705         }
707         if (!mpdclient_is_connected(c))
708                 return false;
710         switch(cmd) {
711         case CMD_PLAY:
712                 song = screen_queue_selected_song();
713                 if (song == NULL)
714                         return false;
716                 connection = mpdclient_get_connection(c);
717                 if (connection != NULL &&
718                     !mpd_run_play_id(connection, mpd_song_get_id(song)))
719                         mpdclient_handle_error(c);
721                 return true;
723         case CMD_DELETE:
724                 list_window_get_range(lw, &range);
725                 mpdclient_cmd_delete_range(c, range.start, range.end);
727                 list_window_set_cursor(lw, range.start);
728                 return true;
730         case CMD_SAVE_PLAYLIST:
731                 playlist_save(c, NULL, NULL);
732                 return true;
734         case CMD_ADD:
735                 handle_add_to_playlist(c);
736                 return true;
738         case CMD_SHUFFLE:
739                 list_window_get_range(lw, &range);
740                 if (range.end < range.start + 1)
741                         /* No range selection, shuffle all list. */
742                         break;
744                 connection = mpdclient_get_connection(c);
745                 if (connection == NULL)
746                         return true;
748                 if (mpd_run_shuffle_range(connection, range.start, range.end))
749                         screen_status_message(_("Shuffled playlist"));
750                 else
751                         mpdclient_handle_error(c);
752                 return true;
754         case CMD_LIST_MOVE_UP:
755                 list_window_get_range(lw, &range);
756                 if (range.start == 0 || range.end <= range.start)
757                         return false;
759                 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
760                         return true;
762                 lw->selected--;
763                 lw->range_base--;
765                 screen_queue_save_selection();
766                 return true;
768         case CMD_LIST_MOVE_DOWN:
769                 list_window_get_range(lw, &range);
770                 if (range.end >= playlist_length(&c->playlist))
771                         return false;
773                 if (!mpdclient_cmd_move(c, range.start, range.end))
774                         return true;
776                 lw->selected++;
777                 lw->range_base++;
779                 screen_queue_save_selection();
780                 return true;
782         case CMD_LOCATE:
783                 if (screen_queue_selected_song() != NULL) {
784                         screen_file_goto_song(c, screen_queue_selected_song());
785                         return true;
786                 }
788                 break;
790         default:
791                 break;
792         }
794         return false;
797 const struct screen_functions screen_queue = {
798         .init = screen_queue_init,
799         .exit = screen_queue_exit,
800         .open = screen_queue_open,
801         .close = screen_queue_close,
802         .resize = screen_queue_resize,
803         .paint = screen_queue_paint,
804         .update = screen_queue_update,
805         .cmd = screen_queue_cmd,
806         .get_title = screen_queue_title,
807 };