Code

list_window: removed second column support
[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         filename_utf8 = locale_to_utf8(filename);
314         connection = mpdclient_get_connection(c);
315         if (!mpd_run_save(connection, filename_utf8)) {
316                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
317                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
318                     mpd_connection_clear_error(connection)) {
319                         char *buf;
320                         int key;
322                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
323                                               filename, YES, NO);
324                         key = tolower(screen_getch(buf));
325                         g_free(buf);
327                         if (key != YES[0]) {
328                                 g_free(filename_utf8);
329                                 g_free(filename);
330                                 screen_status_printf(_("Aborted"));
331                                 return -1;
332                         }
334                         if (!mpd_run_rm(connection, filename_utf8) ||
335                             !mpd_run_save(connection, filename_utf8)) {
336                                 mpdclient_handle_error(c);
337                                 g_free(filename_utf8);
338                                 g_free(filename);
339                                 return -1;
340                         }
341                 } else {
342                         mpdclient_handle_error(c);
343                         g_free(filename_utf8);
344                         g_free(filename);
345                         return -1;
346                 }
347         }
349         c->events |= MPD_IDLE_STORED_PLAYLIST;
351         g_free(filename_utf8);
353         /* success */
354         screen_status_printf(_("Saved %s"), filename);
355         g_free(filename);
356         return 0;
359 #ifndef NCMPC_MINI
360 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
361                     GList **list, struct mpdclient *c)
363         g_completion_remove_items(gcmp, *list);
364         *list = string_list_remove(*list, dir);
365         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
366         g_completion_add_items(gcmp, *list);
367         *dir_list = g_list_append(*dir_list, g_strdup(dir));
370 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
372         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
373         GList **dir_list = tmp->dir_list;
374         GList **list = tmp->list;
375         struct mpdclient *c = tmp->c;
377         if (*list == NULL) {
378                 /* create initial list */
379                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
380                 g_completion_add_items(gcmp, *list);
381         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
382                    string_list_find(*dir_list, line) == NULL) {
383                 /* add directory content to list */
384                 add_dir(gcmp, line, dir_list, list, c);
385         }
388 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
389                                    GList *items, void *data)
391         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
392         GList **dir_list = tmp->dir_list;
393         GList **list = tmp->list;
394         struct mpdclient *c = tmp->c;
396         if (g_list_length(items) >= 1)
397                 screen_display_completion_list(items);
399         if (line && line[0] && line[strlen(line) - 1] == '/' &&
400             string_list_find(*dir_list, line) == NULL) {
401                 /* add directory content to list */
402                 add_dir(gcmp, line, dir_list, list, c);
403         }
405 #endif
407 static int
408 handle_add_to_playlist(struct mpdclient *c)
410         gchar *path;
411 #ifndef NCMPC_MINI
412         GCompletion *gcmp;
413         GList *list = NULL;
414         GList *dir_list = NULL;
415         completion_callback_data_t data;
417         /* initialize completion support */
418         gcmp = g_completion_new(NULL);
419         g_completion_set_compare(gcmp, completion_strncmp);
420         data.list = &list;
421         data.dir_list = &dir_list;
422         data.c = c;
423         wrln_completion_callback_data = &data;
424         wrln_pre_completion_callback = add_pre_completion_cb;
425         wrln_post_completion_callback = add_post_completion_cb;
426 #endif
428         /* get path */
429         path = screen_readln(_("Add"),
430                              NULL,
431                              NULL,
432 #ifdef NCMPC_MINI
433                              NULL
434 #else
435                              gcmp
436 #endif
437                              );
439         /* destroy completion data */
440 #ifndef NCMPC_MINI
441         wrln_completion_callback_data = NULL;
442         wrln_pre_completion_callback = NULL;
443         wrln_post_completion_callback = NULL;
444         g_completion_free(gcmp);
445         string_list_free(list);
446         string_list_free(dir_list);
447 #endif
449         /* add the path to the playlist */
450         if (path != NULL) {
451                 char *path_utf8 = locale_to_utf8(path);
452                 mpdclient_cmd_add_path(c, path_utf8);
453                 g_free(path_utf8);
454         }
456         g_free(path);
457         return 0;
460 static void
461 screen_queue_init(WINDOW *w, int cols, int rows)
463         lw = list_window_init(w, cols, rows);
466 static gboolean
467 timer_hide_cursor(gpointer data)
469         struct mpdclient *c = data;
471         assert(options.hide_cursor > 0);
472         assert(timer_hide_cursor_id != 0);
474         timer_hide_cursor_id = 0;
476         /* hide the cursor when mpd is playing and the user is inactive */
478         if (c->status != NULL &&
479             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
480                 lw->hide_cursor = true;
481                 screen_queue_repaint();
482         } else
483                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
484                                                      timer_hide_cursor, c);
486         return FALSE;
489 static void
490 screen_queue_open(struct mpdclient *c)
492         playlist = &c->playlist;
494         assert(timer_hide_cursor_id == 0);
495         if (options.hide_cursor > 0) {
496                 lw->hide_cursor = false;
497                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
498                                                      timer_hide_cursor, c);
499         }
501         screen_queue_restore_selection();
504 static void
505 screen_queue_close(void)
507         if (timer_hide_cursor_id != 0) {
508                 g_source_remove(timer_hide_cursor_id);
509                 timer_hide_cursor_id = 0;
510         }
513 static void
514 screen_queue_resize(int cols, int rows)
516         list_window_resize(lw, cols, rows);
520 static void
521 screen_queue_exit(void)
523         list_window_free(lw);
526 static const char *
527 screen_queue_title(char *str, size_t size)
529         if (options.host == NULL)
530                 return _("Playlist");
532         g_snprintf(str, size, _("Playlist on %s"), options.host);
533         return str;
536 static void
537 screen_queue_paint_callback(WINDOW *w, unsigned i,
538                             unsigned y, unsigned width,
539                             bool selected, G_GNUC_UNUSED void *data)
541         const struct mpd_song *song;
543         assert(playlist != NULL);
544         assert(i < playlist_length(playlist));
546         song = playlist_get(playlist, i);
548         paint_song_row(w, y, width, selected,
549                        (int)mpd_song_get_id(song) == current_song_id,
550                        song);
553 static void
554 screen_queue_paint(void)
556         list_window_paint2(lw, screen_queue_paint_callback, NULL);
559 G_GNUC_PURE
560 static int
561 get_current_song_id(const struct mpd_status *status)
563         return status != NULL &&
564                 (mpd_status_get_state(status) == MPD_STATE_PLAY ||
565                  mpd_status_get_state(status) == MPD_STATE_PAUSE)
566                 ? (int)mpd_status_get_song_id(status)
567                 : -1;
570 static void
571 screen_queue_update(struct mpdclient *c)
573         if (c->events & MPD_IDLE_PLAYLIST)
574                 screen_queue_restore_selection();
576         if ((c->events & MPD_IDLE_PLAYER) != 0 &&
577             get_current_song_id(c->status) != current_song_id) {
578                 current_song_id = get_current_song_id(c->status);
580                 /* center the cursor */
581                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
582                         center_playing_item(c, false);
584                 screen_queue_repaint();
585         } else if (c->events & MPD_IDLE_PLAYLIST) {
586                 /* the playlist has changed, we must paint the new
587                    version */
588                 screen_queue_repaint();
589         }
592 #ifdef HAVE_GETMOUSE
593 static bool
594 handle_mouse_event(struct mpdclient *c)
596         int row;
597         unsigned selected;
598         unsigned long bstate;
600         if (screen_get_mouse_event(c, &bstate, &row) ||
601             list_window_mouse(lw, bstate, row)) {
602                 screen_queue_repaint();
603                 return true;
604         }
606         if (bstate & BUTTON1_DOUBLE_CLICKED) {
607                 /* stop */
608                 screen_cmd(c, CMD_STOP);
609                 return true;
610         }
612         selected = lw->start + row;
614         if (bstate & BUTTON1_CLICKED) {
615                 /* play */
616                 if (lw->start + row < playlist_length(playlist))
617                         mpdclient_cmd_play(c, lw->start + row);
618         } else if (bstate & BUTTON3_CLICKED) {
619                 /* delete */
620                 if (selected == lw->selected)
621                         mpdclient_cmd_delete(c, lw->selected);
622         }
624         list_window_set_cursor(lw, selected);
625         screen_queue_save_selection();
626         screen_queue_repaint();
628         return true;
630 #endif
632 static bool
633 screen_queue_cmd(struct mpdclient *c, command_t cmd)
635         struct mpd_connection *connection;
636         static command_t cached_cmd = CMD_NONE;
637         command_t prev_cmd = cached_cmd;
638         struct list_window_range range;
640         cached_cmd = cmd;
642         lw->hide_cursor = false;
644         if (options.hide_cursor > 0) {
645                 if (timer_hide_cursor_id != 0)
646                         g_source_remove(timer_hide_cursor_id);
647                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
648                                                      timer_hide_cursor, c);
649         }
651         if (list_window_cmd(lw, cmd)) {
652                 screen_queue_save_selection();
653                 screen_queue_repaint();
654                 return true;
655         }
657         switch(cmd) {
658         case CMD_SCREEN_UPDATE:
659                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
660                 screen_queue_repaint();
661                 return false;
662         case CMD_SELECT_PLAYING:
663                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
664                                                               c->song));
665                 screen_queue_save_selection();
666                 screen_queue_repaint();
667                 return true;
669         case CMD_LIST_FIND:
670         case CMD_LIST_RFIND:
671         case CMD_LIST_FIND_NEXT:
672         case CMD_LIST_RFIND_NEXT:
673                 screen_find(lw, cmd, screen_queue_lw_callback, NULL);
674                 screen_queue_save_selection();
675                 screen_queue_repaint();
676                 return true;
677         case CMD_LIST_JUMP:
678                 screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
679                 screen_queue_save_selection();
680                 screen_queue_repaint();
681                 return true;
683 #ifdef HAVE_GETMOUSE
684         case CMD_MOUSE_EVENT:
685                 return handle_mouse_event(c);
686 #endif
688 #ifdef ENABLE_SONG_SCREEN
689         case CMD_SCREEN_SONG:
690                 if (screen_queue_selected_song() != NULL) {
691                         screen_song_switch(c, screen_queue_selected_song());
692                         return true;
693                 }
695                 break;
696 #endif
698 #ifdef ENABLE_LYRICS_SCREEN
699         case CMD_SCREEN_LYRICS:
700                 if (lw->selected < playlist_length(&c->playlist)) {
701                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
702                         bool follow = false;
704                         if (c->song && selected &&
705                             !strcmp(mpd_song_get_uri(selected),
706                                     mpd_song_get_uri(c->song)))
707                                 follow = true;
709                         screen_lyrics_switch(c, selected, follow);
710                         return true;
711                 }
713                 break;
714 #endif
715         case CMD_SCREEN_SWAP:
716                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
717                 return true;
719         default:
720                 break;
721         }
723         if (!mpdclient_is_connected(c))
724                 return false;
726         switch(cmd) {
727         case CMD_PLAY:
728                 mpdclient_cmd_play(c, lw->selected);
729                 return true;
731         case CMD_DELETE:
732                 list_window_get_range(lw, &range);
733                 mpdclient_cmd_delete_range(c, range.start, range.end);
735                 list_window_set_cursor(lw, range.start);
736                 return true;
738         case CMD_SAVE_PLAYLIST:
739                 playlist_save(c, NULL, NULL);
740                 return true;
742         case CMD_ADD:
743                 handle_add_to_playlist(c);
744                 return true;
746         case CMD_SHUFFLE:
747                 list_window_get_range(lw, &range);
748                 if (range.end < range.start + 1)
749                         /* No range selection, shuffle all list. */
750                         break;
752                 connection = mpdclient_get_connection(c);
753                 if (mpd_run_shuffle_range(connection, range.start, range.end))
754                         screen_status_message(_("Shuffled playlist"));
755                 else
756                         mpdclient_handle_error(c);
757                 return true;
759         case CMD_LIST_MOVE_UP:
760                 list_window_get_range(lw, &range);
761                 if (range.start == 0 || range.end <= range.start)
762                         return false;
764                 for (unsigned i = range.start; i < range.end; ++i)
765                         mpdclient_cmd_move(c, i, i - 1);
767                 lw->selected--;
768                 lw->range_base--;
770                 screen_queue_save_selection();
771                 return true;
773         case CMD_LIST_MOVE_DOWN:
774                 list_window_get_range(lw, &range);
775                 if (range.end >= playlist_length(&c->playlist))
776                         return false;
778                 for (int i = range.end - 1; i >= (int)range.start; --i)
779                         mpdclient_cmd_move(c, i, i + 1);
781                 lw->selected++;
782                 lw->range_base++;
784                 screen_queue_save_selection();
785                 return true;
787         case CMD_LOCATE:
788                 if (screen_queue_selected_song() != NULL) {
789                         screen_file_goto_song(c, screen_queue_selected_song());
790                         return true;
791                 }
793                 break;
795         default:
796                 break;
797         }
799         return false;
802 const struct screen_functions screen_queue = {
803         .init = screen_queue_init,
804         .exit = screen_queue_exit,
805         .open = screen_queue_open,
806         .close = screen_queue_close,
807         .resize = screen_queue_resize,
808         .paint = screen_queue_paint,
809         .update = screen_queue_update,
810         .cmd = screen_queue_cmd,
811         .get_title = screen_queue_title,
812 };