Code

screen_queue: eliminated length check in center()
[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         int idx;
141         /* try to center the song that are playing */
142         idx = playlist_get_index(&c->playlist, c->song);
143         if (idx < 0)
144                 return;
146         list_window_center(lw, idx);
148         if (center_cursor) {
149                 list_window_set_cursor(lw, idx);
150                 return;
151         }
153         /* make sure the cursor is in the window */
154         list_window_fetch_cursor(lw);
157 #ifndef NCMPC_MINI
158 static void
159 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
160                        void *data)
162         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
163         GList **list = tmp->list;
164         struct mpdclient *c = tmp->c;
166         if( *list == NULL ) {
167                 /* create completion list */
168                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
169                 g_completion_add_items(gcmp, *list);
170         }
173 static void
174 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
175                         G_GNUC_UNUSED gchar *line, GList *items,
176                         G_GNUC_UNUSED void *data)
178         if (g_list_length(items) >= 1)
179                 screen_display_completion_list(items);
181 #endif
183 #ifndef NCMPC_MINI
184 /**
185  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
186  * g_completion_set_compare(), because strncmp() takes size_t where
187  * g_completion_set_compare passes a gsize value.
188  */
189 static gint
190 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
192         return strncmp(s1, s2, n);
194 #endif
196 int
197 playlist_save(struct mpdclient *c, char *name, char *defaultname)
199         struct mpd_connection *connection;
200         gchar *filename, *filename_utf8;
201 #ifndef NCMPC_MINI
202         GCompletion *gcmp;
203         GList *list = NULL;
204         completion_callback_data_t data;
205 #endif
207 #ifdef NCMPC_MINI
208         (void)defaultname;
209 #endif
211 #ifndef NCMPC_MINI
212         if (name == NULL) {
213                 /* initialize completion support */
214                 gcmp = g_completion_new(NULL);
215                 g_completion_set_compare(gcmp, completion_strncmp);
216                 data.list = &list;
217                 data.dir_list = NULL;
218                 data.c = c;
219                 wrln_completion_callback_data = &data;
220                 wrln_pre_completion_callback = save_pre_completion_cb;
221                 wrln_post_completion_callback = save_post_completion_cb;
224                 /* query the user for a filename */
225                 filename = screen_readln(_("Save playlist as"),
226                                          defaultname,
227                                          NULL,
228                                          gcmp);
230                 /* destroy completion support */
231                 wrln_completion_callback_data = NULL;
232                 wrln_pre_completion_callback = NULL;
233                 wrln_post_completion_callback = NULL;
234                 g_completion_free(gcmp);
235                 list = string_list_free(list);
236                 if( filename )
237                         filename=g_strstrip(filename);
238         } else
239 #endif
240                         filename=g_strdup(name);
242         if (filename == NULL)
243                 return -1;
245         /* send save command to mpd */
247         connection = mpdclient_get_connection(c);
248         if (connection == NULL)
249                 return -1;
251         filename_utf8 = locale_to_utf8(filename);
252         if (!mpd_run_save(connection, filename_utf8)) {
253                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
254                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
255                     mpd_connection_clear_error(connection)) {
256                         char *buf;
257                         int key;
259                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
260                                               filename, YES, NO);
261                         key = tolower(screen_getch(buf));
262                         g_free(buf);
264                         if (key != YES[0]) {
265                                 g_free(filename_utf8);
266                                 g_free(filename);
267                                 screen_status_printf(_("Aborted"));
268                                 return -1;
269                         }
271                         if (!mpd_run_rm(connection, filename_utf8) ||
272                             !mpd_run_save(connection, filename_utf8)) {
273                                 mpdclient_handle_error(c);
274                                 g_free(filename_utf8);
275                                 g_free(filename);
276                                 return -1;
277                         }
278                 } else {
279                         mpdclient_handle_error(c);
280                         g_free(filename_utf8);
281                         g_free(filename);
282                         return -1;
283                 }
284         }
286         c->events |= MPD_IDLE_STORED_PLAYLIST;
288         g_free(filename_utf8);
290         /* success */
291         screen_status_printf(_("Saved %s"), filename);
292         g_free(filename);
293         return 0;
296 #ifndef NCMPC_MINI
297 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
298                     GList **list, struct mpdclient *c)
300         g_completion_remove_items(gcmp, *list);
301         *list = string_list_remove(*list, dir);
302         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
303         g_completion_add_items(gcmp, *list);
304         *dir_list = g_list_append(*dir_list, g_strdup(dir));
307 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
309         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
310         GList **dir_list = tmp->dir_list;
311         GList **list = tmp->list;
312         struct mpdclient *c = tmp->c;
314         if (*list == NULL) {
315                 /* create initial list */
316                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
317                 g_completion_add_items(gcmp, *list);
318         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
319                    string_list_find(*dir_list, line) == NULL) {
320                 /* add directory content to list */
321                 add_dir(gcmp, line, dir_list, list, c);
322         }
325 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
326                                    GList *items, void *data)
328         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
329         GList **dir_list = tmp->dir_list;
330         GList **list = tmp->list;
331         struct mpdclient *c = tmp->c;
333         if (g_list_length(items) >= 1)
334                 screen_display_completion_list(items);
336         if (line && line[0] && line[strlen(line) - 1] == '/' &&
337             string_list_find(*dir_list, line) == NULL) {
338                 /* add directory content to list */
339                 add_dir(gcmp, line, dir_list, list, c);
340         }
342 #endif
344 static int
345 handle_add_to_playlist(struct mpdclient *c)
347         gchar *path;
348 #ifndef NCMPC_MINI
349         GCompletion *gcmp;
350         GList *list = NULL;
351         GList *dir_list = NULL;
352         completion_callback_data_t data;
354         /* initialize completion support */
355         gcmp = g_completion_new(NULL);
356         g_completion_set_compare(gcmp, completion_strncmp);
357         data.list = &list;
358         data.dir_list = &dir_list;
359         data.c = c;
360         wrln_completion_callback_data = &data;
361         wrln_pre_completion_callback = add_pre_completion_cb;
362         wrln_post_completion_callback = add_post_completion_cb;
363 #endif
365         /* get path */
366         path = screen_readln(_("Add"),
367                              NULL,
368                              NULL,
369 #ifdef NCMPC_MINI
370                              NULL
371 #else
372                              gcmp
373 #endif
374                              );
376         /* destroy completion data */
377 #ifndef NCMPC_MINI
378         wrln_completion_callback_data = NULL;
379         wrln_pre_completion_callback = NULL;
380         wrln_post_completion_callback = NULL;
381         g_completion_free(gcmp);
382         string_list_free(list);
383         string_list_free(dir_list);
384 #endif
386         /* add the path to the playlist */
387         if (path != NULL) {
388                 char *path_utf8 = locale_to_utf8(path);
389                 mpdclient_cmd_add_path(c, path_utf8);
390                 g_free(path_utf8);
391         }
393         g_free(path);
394         return 0;
397 static void
398 screen_queue_init(WINDOW *w, int cols, int rows)
400         lw = list_window_init(w, cols, rows);
402 #ifndef NCMPC_MINI
403         if (options.scroll)
404                 hscroll_init(&hscroll, w, options.scroll_sep);
405 #endif
408 static gboolean
409 timer_hide_cursor(gpointer data)
411         struct mpdclient *c = data;
413         assert(options.hide_cursor > 0);
414         assert(timer_hide_cursor_id != 0);
416         timer_hide_cursor_id = 0;
418         /* hide the cursor when mpd is playing and the user is inactive */
420         if (c->status != NULL &&
421             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
422                 lw->hide_cursor = true;
423                 screen_queue_repaint();
424         } else
425                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
426                                                      timer_hide_cursor, c);
428         return FALSE;
431 static void
432 screen_queue_open(struct mpdclient *c)
434         playlist = &c->playlist;
436         assert(timer_hide_cursor_id == 0);
437         if (options.hide_cursor > 0) {
438                 lw->hide_cursor = false;
439                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
440                                                      timer_hide_cursor, c);
441         }
443         screen_queue_restore_selection();
446 static void
447 screen_queue_close(void)
449         if (timer_hide_cursor_id != 0) {
450                 g_source_remove(timer_hide_cursor_id);
451                 timer_hide_cursor_id = 0;
452         }
454 #ifndef NCMPC_MINI
455         if (options.scroll)
456                 hscroll_clear(&hscroll);
457 #endif
460 static void
461 screen_queue_resize(int cols, int rows)
463         list_window_resize(lw, cols, rows);
467 static void
468 screen_queue_exit(void)
470         list_window_free(lw);
473 static const char *
474 screen_queue_title(char *str, size_t size)
476         if (options.host == NULL)
477                 return _("Playlist");
479         g_snprintf(str, size, _("Playlist on %s"), options.host);
480         return str;
483 static void
484 screen_queue_paint_callback(WINDOW *w, unsigned i,
485                             unsigned y, unsigned width,
486                             bool selected, G_GNUC_UNUSED void *data)
488         const struct mpd_song *song;
489         struct hscroll *row_hscroll;
491         assert(playlist != NULL);
492         assert(i < playlist_length(playlist));
494         song = playlist_get(playlist, i);
496 #ifdef NCMPC_MINI
497         row_hscroll = NULL;
498 #else
499         row_hscroll = selected && options.scroll && lw->selected == i
500                 ? &hscroll : NULL;
501 #endif
503         paint_song_row(w, y, width, selected,
504                        (int)mpd_song_get_id(song) == current_song_id,
505                        song, row_hscroll);
508 static void
509 screen_queue_paint(void)
511 #ifndef NCMPC_MINI
512         if (options.scroll)
513                 hscroll_clear(&hscroll);
514 #endif
516         list_window_paint2(lw, screen_queue_paint_callback, NULL);
519 G_GNUC_PURE
520 static int
521 get_current_song_id(const struct mpd_status *status)
523         return status != NULL &&
524                 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
525                  mpd_status_get_state(status) == MPD_STATE_PAUSE)
526                 ? (int)mpd_status_get_song_id(status)
527                 : -1;
530 static void
531 screen_queue_update(struct mpdclient *c)
533         if (c->events & MPD_IDLE_PLAYLIST)
534                 screen_queue_restore_selection();
536         if ((c->events & MPD_IDLE_PLAYER) != 0 &&
537             get_current_song_id(c->status) != current_song_id) {
538                 current_song_id = get_current_song_id(c->status);
540                 /* center the cursor */
541                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
542                         center_playing_item(c, false);
544                 screen_queue_repaint();
545         } else if (c->events & MPD_IDLE_PLAYLIST) {
546                 /* the playlist has changed, we must paint the new
547                    version */
548                 screen_queue_repaint();
549         }
552 #ifdef HAVE_GETMOUSE
553 static bool
554 handle_mouse_event(struct mpdclient *c)
556         int row;
557         unsigned long bstate;
558         unsigned old_selected;
560         if (screen_get_mouse_event(c, &bstate, &row) ||
561             list_window_mouse(lw, bstate, row)) {
562                 screen_queue_repaint();
563                 return true;
564         }
566         if (bstate & BUTTON1_DOUBLE_CLICKED) {
567                 /* stop */
568                 screen_cmd(c, CMD_STOP);
569                 return true;
570         }
572         old_selected = lw->selected;
573         list_window_set_cursor(lw, lw->start + row);
575         if (bstate & BUTTON1_CLICKED) {
576                 /* play */
577                 const struct mpd_song *song = screen_queue_selected_song();
578                 if (song != NULL) {
579                         struct mpd_connection *connection =
580                                 mpdclient_get_connection(c);
582                         if (connection != NULL &&
583                             !mpd_run_play_id(connection,
584                                              mpd_song_get_id(song)))
585                                 mpdclient_handle_error(c);
586                 }
587         } else if (bstate & BUTTON3_CLICKED) {
588                 /* delete */
589                 if (lw->selected == old_selected)
590                         mpdclient_cmd_delete(c, lw->selected);
592                 list_window_set_length(lw, playlist_length(playlist));
593         }
595         screen_queue_save_selection();
596         screen_queue_repaint();
598         return true;
600 #endif
602 static bool
603 screen_queue_cmd(struct mpdclient *c, command_t cmd)
605         struct mpd_connection *connection;
606         static command_t cached_cmd = CMD_NONE;
607         command_t prev_cmd = cached_cmd;
608         struct list_window_range range;
609         const struct mpd_song *song;
611         cached_cmd = cmd;
613         lw->hide_cursor = false;
615         if (options.hide_cursor > 0) {
616                 if (timer_hide_cursor_id != 0)
617                         g_source_remove(timer_hide_cursor_id);
618                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
619                                                      timer_hide_cursor, c);
620         }
622         if (list_window_cmd(lw, cmd)) {
623                 screen_queue_save_selection();
624                 screen_queue_repaint();
625                 return true;
626         }
628         switch(cmd) {
629         case CMD_SCREEN_UPDATE:
630                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
631                 screen_queue_repaint();
632                 return false;
633         case CMD_SELECT_PLAYING:
634                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
635                                                               c->song));
636                 screen_queue_save_selection();
637                 screen_queue_repaint();
638                 return true;
640         case CMD_LIST_FIND:
641         case CMD_LIST_RFIND:
642         case CMD_LIST_FIND_NEXT:
643         case CMD_LIST_RFIND_NEXT:
644                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
645                 screen_queue_save_selection();
646                 screen_queue_repaint();
647                 return true;
648         case CMD_LIST_JUMP:
649                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
650                 screen_queue_save_selection();
651                 screen_queue_repaint();
652                 return true;
654 #ifdef HAVE_GETMOUSE
655         case CMD_MOUSE_EVENT:
656                 return handle_mouse_event(c);
657 #endif
659 #ifdef ENABLE_SONG_SCREEN
660         case CMD_SCREEN_SONG:
661                 if (screen_queue_selected_song() != NULL) {
662                         screen_song_switch(c, screen_queue_selected_song());
663                         return true;
664                 }
666                 break;
667 #endif
669 #ifdef ENABLE_LYRICS_SCREEN
670         case CMD_SCREEN_LYRICS:
671                 if (lw->selected < playlist_length(&c->playlist)) {
672                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
673                         bool follow = false;
675                         if (c->song && selected &&
676                             !strcmp(mpd_song_get_uri(selected),
677                                     mpd_song_get_uri(c->song)))
678                                 follow = true;
680                         screen_lyrics_switch(c, selected, follow);
681                         return true;
682                 }
684                 break;
685 #endif
686         case CMD_SCREEN_SWAP:
687                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
688                 return true;
690         default:
691                 break;
692         }
694         if (!mpdclient_is_connected(c))
695                 return false;
697         switch(cmd) {
698         case CMD_PLAY:
699                 song = screen_queue_selected_song();
700                 if (song == NULL)
701                         return false;
703                 connection = mpdclient_get_connection(c);
704                 if (connection != NULL &&
705                     !mpd_run_play_id(connection, mpd_song_get_id(song)))
706                         mpdclient_handle_error(c);
708                 return true;
710         case CMD_DELETE:
711                 list_window_get_range(lw, &range);
712                 mpdclient_cmd_delete_range(c, range.start, range.end);
714                 list_window_set_cursor(lw, range.start);
715                 return true;
717         case CMD_SAVE_PLAYLIST:
718                 playlist_save(c, NULL, NULL);
719                 return true;
721         case CMD_ADD:
722                 handle_add_to_playlist(c);
723                 return true;
725         case CMD_SHUFFLE:
726                 list_window_get_range(lw, &range);
727                 if (range.end < range.start + 1)
728                         /* No range selection, shuffle all list. */
729                         break;
731                 connection = mpdclient_get_connection(c);
732                 if (connection == NULL)
733                         return true;
735                 if (mpd_run_shuffle_range(connection, range.start, range.end))
736                         screen_status_message(_("Shuffled playlist"));
737                 else
738                         mpdclient_handle_error(c);
739                 return true;
741         case CMD_LIST_MOVE_UP:
742                 list_window_get_range(lw, &range);
743                 if (range.start == 0 || range.end <= range.start)
744                         return false;
746                 if (!mpdclient_cmd_move(c, range.end - 1, range.start - 1))
747                         return true;
749                 lw->selected--;
750                 lw->range_base--;
752                 screen_queue_save_selection();
753                 return true;
755         case CMD_LIST_MOVE_DOWN:
756                 list_window_get_range(lw, &range);
757                 if (range.end >= playlist_length(&c->playlist))
758                         return false;
760                 if (!mpdclient_cmd_move(c, range.start, range.end))
761                         return true;
763                 lw->selected++;
764                 lw->range_base++;
766                 screen_queue_save_selection();
767                 return true;
769         case CMD_LOCATE:
770                 if (screen_queue_selected_song() != NULL) {
771                         screen_file_goto_song(c, screen_queue_selected_song());
772                         return true;
773                 }
775                 break;
777         default:
778                 break;
779         }
781         return false;
784 const struct screen_functions screen_queue = {
785         .init = screen_queue_init,
786         .exit = screen_queue_exit,
787         .open = screen_queue_open,
788         .close = screen_queue_close,
789         .resize = screen_queue_resize,
790         .paint = screen_queue_paint,
791         .update = screen_queue_update,
792         .cmd = screen_queue_cmd,
793         .get_title = screen_queue_title,
794 };