Code

screen_queue: use mpd_status.song_pos for centering the list
[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(const struct mpd_status *status, bool center_cursor)
139         int idx;
141         if (status == NULL ||
142             (mpd_status_get_state(status) != MPD_STATE_PLAY &&
143              mpd_status_get_state(status) != MPD_STATE_PAUSE))
144                 return;
146         /* try to center the song that are playing */
147         idx = mpd_status_get_song_pos(status);
148         if (idx < 0)
149                 return;
151         list_window_center(lw, idx);
153         if (center_cursor) {
154                 list_window_set_cursor(lw, idx);
155                 return;
156         }
158         /* make sure the cursor is in the window */
159         list_window_fetch_cursor(lw);
162 #ifndef NCMPC_MINI
163 static void
164 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
165                        void *data)
167         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
168         GList **list = tmp->list;
169         struct mpdclient *c = tmp->c;
171         if( *list == NULL ) {
172                 /* create completion list */
173                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
174                 g_completion_add_items(gcmp, *list);
175         }
178 static void
179 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
180                         G_GNUC_UNUSED gchar *line, GList *items,
181                         G_GNUC_UNUSED void *data)
183         if (g_list_length(items) >= 1)
184                 screen_display_completion_list(items);
186 #endif
188 #ifndef NCMPC_MINI
189 /**
190  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
191  * g_completion_set_compare(), because strncmp() takes size_t where
192  * g_completion_set_compare passes a gsize value.
193  */
194 static gint
195 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
197         return strncmp(s1, s2, n);
199 #endif
201 int
202 playlist_save(struct mpdclient *c, char *name, char *defaultname)
204         struct mpd_connection *connection;
205         gchar *filename, *filename_utf8;
206 #ifndef NCMPC_MINI
207         GCompletion *gcmp;
208         GList *list = NULL;
209         completion_callback_data_t data;
210 #endif
212 #ifdef NCMPC_MINI
213         (void)defaultname;
214 #endif
216 #ifndef NCMPC_MINI
217         if (name == NULL) {
218                 /* initialize completion support */
219                 gcmp = g_completion_new(NULL);
220                 g_completion_set_compare(gcmp, completion_strncmp);
221                 data.list = &list;
222                 data.dir_list = NULL;
223                 data.c = c;
224                 wrln_completion_callback_data = &data;
225                 wrln_pre_completion_callback = save_pre_completion_cb;
226                 wrln_post_completion_callback = save_post_completion_cb;
229                 /* query the user for a filename */
230                 filename = screen_readln(_("Save playlist as"),
231                                          defaultname,
232                                          NULL,
233                                          gcmp);
235                 /* destroy completion support */
236                 wrln_completion_callback_data = NULL;
237                 wrln_pre_completion_callback = NULL;
238                 wrln_post_completion_callback = NULL;
239                 g_completion_free(gcmp);
240                 list = string_list_free(list);
241                 if( filename )
242                         filename=g_strstrip(filename);
243         } else
244 #endif
245                         filename=g_strdup(name);
247         if (filename == NULL)
248                 return -1;
250         /* send save command to mpd */
252         connection = mpdclient_get_connection(c);
253         if (connection == NULL)
254                 return -1;
256         filename_utf8 = locale_to_utf8(filename);
257         if (!mpd_run_save(connection, filename_utf8)) {
258                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
259                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
260                     mpd_connection_clear_error(connection)) {
261                         char *buf;
262                         int key;
264                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
265                                               filename, YES, NO);
266                         key = tolower(screen_getch(buf));
267                         g_free(buf);
269                         if (key != YES[0]) {
270                                 g_free(filename_utf8);
271                                 g_free(filename);
272                                 screen_status_printf(_("Aborted"));
273                                 return -1;
274                         }
276                         if (!mpd_run_rm(connection, filename_utf8) ||
277                             !mpd_run_save(connection, filename_utf8)) {
278                                 mpdclient_handle_error(c);
279                                 g_free(filename_utf8);
280                                 g_free(filename);
281                                 return -1;
282                         }
283                 } else {
284                         mpdclient_handle_error(c);
285                         g_free(filename_utf8);
286                         g_free(filename);
287                         return -1;
288                 }
289         }
291         c->events |= MPD_IDLE_STORED_PLAYLIST;
293         g_free(filename_utf8);
295         /* success */
296         screen_status_printf(_("Saved %s"), filename);
297         g_free(filename);
298         return 0;
301 #ifndef NCMPC_MINI
302 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
303                     GList **list, struct mpdclient *c)
305         g_completion_remove_items(gcmp, *list);
306         *list = string_list_remove(*list, dir);
307         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
308         g_completion_add_items(gcmp, *list);
309         *dir_list = g_list_append(*dir_list, g_strdup(dir));
312 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
314         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
315         GList **dir_list = tmp->dir_list;
316         GList **list = tmp->list;
317         struct mpdclient *c = tmp->c;
319         if (*list == NULL) {
320                 /* create initial list */
321                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
322                 g_completion_add_items(gcmp, *list);
323         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
324                    string_list_find(*dir_list, line) == NULL) {
325                 /* add directory content to list */
326                 add_dir(gcmp, line, dir_list, list, c);
327         }
330 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
331                                    GList *items, void *data)
333         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
334         GList **dir_list = tmp->dir_list;
335         GList **list = tmp->list;
336         struct mpdclient *c = tmp->c;
338         if (g_list_length(items) >= 1)
339                 screen_display_completion_list(items);
341         if (line && line[0] && line[strlen(line) - 1] == '/' &&
342             string_list_find(*dir_list, line) == NULL) {
343                 /* add directory content to list */
344                 add_dir(gcmp, line, dir_list, list, c);
345         }
347 #endif
349 static int
350 handle_add_to_playlist(struct mpdclient *c)
352         gchar *path;
353 #ifndef NCMPC_MINI
354         GCompletion *gcmp;
355         GList *list = NULL;
356         GList *dir_list = NULL;
357         completion_callback_data_t data;
359         /* initialize completion support */
360         gcmp = g_completion_new(NULL);
361         g_completion_set_compare(gcmp, completion_strncmp);
362         data.list = &list;
363         data.dir_list = &dir_list;
364         data.c = c;
365         wrln_completion_callback_data = &data;
366         wrln_pre_completion_callback = add_pre_completion_cb;
367         wrln_post_completion_callback = add_post_completion_cb;
368 #endif
370         /* get path */
371         path = screen_readln(_("Add"),
372                              NULL,
373                              NULL,
374 #ifdef NCMPC_MINI
375                              NULL
376 #else
377                              gcmp
378 #endif
379                              );
381         /* destroy completion data */
382 #ifndef NCMPC_MINI
383         wrln_completion_callback_data = NULL;
384         wrln_pre_completion_callback = NULL;
385         wrln_post_completion_callback = NULL;
386         g_completion_free(gcmp);
387         string_list_free(list);
388         string_list_free(dir_list);
389 #endif
391         /* add the path to the playlist */
392         if (path != NULL) {
393                 char *path_utf8 = locale_to_utf8(path);
394                 mpdclient_cmd_add_path(c, path_utf8);
395                 g_free(path_utf8);
396         }
398         g_free(path);
399         return 0;
402 static void
403 screen_queue_init(WINDOW *w, int cols, int rows)
405         lw = list_window_init(w, cols, rows);
407 #ifndef NCMPC_MINI
408         if (options.scroll)
409                 hscroll_init(&hscroll, w, options.scroll_sep);
410 #endif
413 static gboolean
414 timer_hide_cursor(gpointer data)
416         struct mpdclient *c = data;
418         assert(options.hide_cursor > 0);
419         assert(timer_hide_cursor_id != 0);
421         timer_hide_cursor_id = 0;
423         /* hide the cursor when mpd is playing and the user is inactive */
425         if (c->status != NULL &&
426             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
427                 lw->hide_cursor = true;
428                 screen_queue_repaint();
429         } else
430                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
431                                                      timer_hide_cursor, c);
433         return FALSE;
436 static void
437 screen_queue_open(struct mpdclient *c)
439         playlist = &c->playlist;
441         assert(timer_hide_cursor_id == 0);
442         if (options.hide_cursor > 0) {
443                 lw->hide_cursor = false;
444                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
445                                                      timer_hide_cursor, c);
446         }
448         screen_queue_restore_selection();
451 static void
452 screen_queue_close(void)
454         if (timer_hide_cursor_id != 0) {
455                 g_source_remove(timer_hide_cursor_id);
456                 timer_hide_cursor_id = 0;
457         }
459 #ifndef NCMPC_MINI
460         if (options.scroll)
461                 hscroll_clear(&hscroll);
462 #endif
465 static void
466 screen_queue_resize(int cols, int rows)
468         list_window_resize(lw, cols, rows);
472 static void
473 screen_queue_exit(void)
475         list_window_free(lw);
478 static const char *
479 screen_queue_title(char *str, size_t size)
481         if (options.host == NULL)
482                 return _("Playlist");
484         g_snprintf(str, size, _("Playlist on %s"), options.host);
485         return str;
488 static void
489 screen_queue_paint_callback(WINDOW *w, unsigned i,
490                             unsigned y, unsigned width,
491                             bool selected, G_GNUC_UNUSED void *data)
493         const struct mpd_song *song;
494         struct hscroll *row_hscroll;
496         assert(playlist != NULL);
497         assert(i < playlist_length(playlist));
499         song = playlist_get(playlist, i);
501 #ifdef NCMPC_MINI
502         row_hscroll = NULL;
503 #else
504         row_hscroll = selected && options.scroll && lw->selected == i
505                 ? &hscroll : NULL;
506 #endif
508         paint_song_row(w, y, width, selected,
509                        (int)mpd_song_get_id(song) == current_song_id,
510                        song, row_hscroll);
513 static void
514 screen_queue_paint(void)
516 #ifndef NCMPC_MINI
517         if (options.scroll)
518                 hscroll_clear(&hscroll);
519 #endif
521         list_window_paint2(lw, screen_queue_paint_callback, NULL);
524 G_GNUC_PURE
525 static int
526 get_current_song_id(const struct mpd_status *status)
528         return status != NULL &&
529                 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
530                  mpd_status_get_state(status) == MPD_STATE_PAUSE)
531                 ? (int)mpd_status_get_song_id(status)
532                 : -1;
535 static void
536 screen_queue_update(struct mpdclient *c)
538         if (c->events & MPD_IDLE_PLAYLIST)
539                 screen_queue_restore_selection();
541         if ((c->events & MPD_IDLE_PLAYER) != 0 &&
542             get_current_song_id(c->status) != current_song_id) {
543                 current_song_id = get_current_song_id(c->status);
545                 /* center the cursor */
546                 if (options.auto_center && !lw->range_selection)
547                         center_playing_item(c->status, false);
549                 screen_queue_repaint();
550         } else if (c->events & MPD_IDLE_PLAYLIST) {
551                 /* the playlist has changed, we must paint the new
552                    version */
553                 screen_queue_repaint();
554         }
557 #ifdef HAVE_GETMOUSE
558 static bool
559 handle_mouse_event(struct mpdclient *c)
561         int row;
562         unsigned long bstate;
563         unsigned old_selected;
565         if (screen_get_mouse_event(c, &bstate, &row) ||
566             list_window_mouse(lw, bstate, row)) {
567                 screen_queue_repaint();
568                 return true;
569         }
571         if (bstate & BUTTON1_DOUBLE_CLICKED) {
572                 /* stop */
573                 screen_cmd(c, CMD_STOP);
574                 return true;
575         }
577         old_selected = lw->selected;
578         list_window_set_cursor(lw, lw->start + row);
580         if (bstate & BUTTON1_CLICKED) {
581                 /* play */
582                 const struct mpd_song *song = screen_queue_selected_song();
583                 if (song != NULL) {
584                         struct mpd_connection *connection =
585                                 mpdclient_get_connection(c);
587                         if (connection != NULL &&
588                             !mpd_run_play_id(connection,
589                                              mpd_song_get_id(song)))
590                                 mpdclient_handle_error(c);
591                 }
592         } else if (bstate & BUTTON3_CLICKED) {
593                 /* delete */
594                 if (lw->selected == old_selected)
595                         mpdclient_cmd_delete(c, lw->selected);
597                 list_window_set_length(lw, playlist_length(playlist));
598         }
600         screen_queue_save_selection();
601         screen_queue_repaint();
603         return true;
605 #endif
607 static bool
608 screen_queue_cmd(struct mpdclient *c, command_t cmd)
610         struct mpd_connection *connection;
611         static command_t cached_cmd = CMD_NONE;
612         command_t prev_cmd = cached_cmd;
613         struct list_window_range range;
614         const struct mpd_song *song;
616         cached_cmd = cmd;
618         lw->hide_cursor = false;
620         if (options.hide_cursor > 0) {
621                 if (timer_hide_cursor_id != 0)
622                         g_source_remove(timer_hide_cursor_id);
623                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
624                                                      timer_hide_cursor, c);
625         }
627         if (list_window_cmd(lw, cmd)) {
628                 screen_queue_save_selection();
629                 screen_queue_repaint();
630                 return true;
631         }
633         switch(cmd) {
634         case CMD_SCREEN_UPDATE:
635                 center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
636                 screen_queue_repaint();
637                 return false;
638         case CMD_SELECT_PLAYING:
639                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
640                                                               c->song));
641                 screen_queue_save_selection();
642                 screen_queue_repaint();
643                 return true;
645         case CMD_LIST_FIND:
646         case CMD_LIST_RFIND:
647         case CMD_LIST_FIND_NEXT:
648         case CMD_LIST_RFIND_NEXT:
649                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
650                 screen_queue_save_selection();
651                 screen_queue_repaint();
652                 return true;
653         case CMD_LIST_JUMP:
654                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
655                 screen_queue_save_selection();
656                 screen_queue_repaint();
657                 return true;
659 #ifdef HAVE_GETMOUSE
660         case CMD_MOUSE_EVENT:
661                 return handle_mouse_event(c);
662 #endif
664 #ifdef ENABLE_SONG_SCREEN
665         case CMD_SCREEN_SONG:
666                 if (screen_queue_selected_song() != NULL) {
667                         screen_song_switch(c, screen_queue_selected_song());
668                         return true;
669                 }
671                 break;
672 #endif
674 #ifdef ENABLE_LYRICS_SCREEN
675         case CMD_SCREEN_LYRICS:
676                 if (lw->selected < playlist_length(&c->playlist)) {
677                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
678                         bool follow = false;
680                         if (c->song && selected &&
681                             !strcmp(mpd_song_get_uri(selected),
682                                     mpd_song_get_uri(c->song)))
683                                 follow = true;
685                         screen_lyrics_switch(c, selected, follow);
686                         return true;
687                 }
689                 break;
690 #endif
691         case CMD_SCREEN_SWAP:
692                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
693                 return true;
695         default:
696                 break;
697         }
699         if (!mpdclient_is_connected(c))
700                 return false;
702         switch(cmd) {
703         case CMD_PLAY:
704                 song = screen_queue_selected_song();
705                 if (song == NULL)
706                         return false;
708                 connection = mpdclient_get_connection(c);
709                 if (connection != NULL &&
710                     !mpd_run_play_id(connection, mpd_song_get_id(song)))
711                         mpdclient_handle_error(c);
713                 return true;
715         case CMD_DELETE:
716                 list_window_get_range(lw, &range);
717                 mpdclient_cmd_delete_range(c, range.start, range.end);
719                 list_window_set_cursor(lw, range.start);
720                 return true;
722         case CMD_SAVE_PLAYLIST:
723                 playlist_save(c, NULL, NULL);
724                 return true;
726         case CMD_ADD:
727                 handle_add_to_playlist(c);
728                 return true;
730         case CMD_SHUFFLE:
731                 list_window_get_range(lw, &range);
732                 if (range.end < range.start + 1)
733                         /* No range selection, shuffle all list. */
734                         break;
736                 connection = mpdclient_get_connection(c);
737                 if (connection == NULL)
738                         return true;
740                 if (mpd_run_shuffle_range(connection, range.start, range.end))
741                         screen_status_message(_("Shuffled playlist"));
742                 else
743                         mpdclient_handle_error(c);
744                 return true;
746         case CMD_LIST_MOVE_UP:
747                 list_window_get_range(lw, &range);
748                 if (range.start == 0 || range.end <= range.start)
749                         return false;
751                 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
752                         return true;
754                 lw->selected--;
755                 lw->range_base--;
757                 screen_queue_save_selection();
758                 return true;
760         case CMD_LIST_MOVE_DOWN:
761                 list_window_get_range(lw, &range);
762                 if (range.end >= playlist_length(&c->playlist))
763                         return false;
765                 if (!mpdclient_cmd_move(c, range.start, range.end))
766                         return true;
768                 lw->selected++;
769                 lw->range_base++;
771                 screen_queue_save_selection();
772                 return true;
774         case CMD_LOCATE:
775                 if (screen_queue_selected_song() != NULL) {
776                         screen_file_goto_song(c, screen_queue_selected_song());
777                         return true;
778                 }
780                 break;
782         default:
783                 break;
784         }
786         return false;
789 const struct screen_functions screen_queue = {
790         .init = screen_queue_init,
791         .exit = screen_queue_exit,
792         .open = screen_queue_open,
793         .close = screen_queue_close,
794         .resize = screen_queue_resize,
795         .paint = screen_queue_paint,
796         .update = screen_queue_update,
797         .cmd = screen_queue_cmd,
798         .get_title = screen_queue_title,
799 };