Code

screen_{play,keydef,text}: use list_window_set_cursor()
[ncmpc.git] / src / screen_play.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
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.
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.
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_play.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 "colors.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 static guint scroll_source_id;
61 #endif
63 static struct mpdclient_playlist *playlist;
64 static int current_song_id = -1;
65 static int selected_song_id = -1;
66 static struct list_window *lw;
67 static guint timer_hide_cursor_id;
69 static void
70 screen_playlist_paint(void);
72 static void
73 playlist_repaint(void)
74 {
75         screen_playlist_paint();
76         wrefresh(lw->w);
77 }
79 static const struct mpd_song *
80 playlist_selected_song(void)
81 {
82         return !lw->range_selection &&
83                 lw->selected < playlist_length(playlist)
84                 ? playlist_get(playlist, lw->selected)
85                 : NULL;
86 }
88 static void
89 playlist_save_selection(void)
90 {
91         selected_song_id = playlist_selected_song() != NULL
92                 ? (int)mpd_song_get_id(playlist_selected_song())
93                 : -1;
94 }
96 static void
97 playlist_restore_selection(void)
98 {
99         const struct mpd_song *song;
100         int pos;
102         if (selected_song_id < 0)
103                 /* there was no selection */
104                 return;
106         song = playlist_selected_song();
107         if (song != NULL &&
108             mpd_song_get_id(song) == (unsigned)selected_song_id)
109                 /* selection is still valid */
110                 return;
112         pos = playlist_get_index_from_id(playlist, selected_song_id);
113         if (pos >= 0)
114                 list_window_set_cursor(lw, pos);
116         list_window_check_selected(lw, playlist_length(playlist));
117         playlist_save_selection();
120 #ifndef NCMPC_MINI
121 static gboolean
122 scroll_timer_callback(G_GNUC_UNUSED gpointer data)
124         scroll_source_id = 0;
126         hscroll_step(&hscroll);
127         playlist_repaint();
128         return false;
130 #endif
132 static const char *
133 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
135         static char songname[MAX_SONG_LENGTH];
136         struct mpd_song *song;
138         if (playlist == NULL || idx >= playlist_length(playlist))
139                 return NULL;
141         song = playlist_get(playlist, idx);
142         if ((int)mpd_song_get_id(song) == current_song_id)
143                 *highlight = true;
145         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
147 #ifndef NCMPC_MINI
148         if (second_column != NULL && mpd_song_get_duration(song) > 0) {
149                 char duration[32];
150                 format_duration_short(duration, sizeof(duration),
151                                       mpd_song_get_duration(song));
152                 *second_column = g_strdup(duration);
153         }
155         if (idx == lw->selected)
156         {
157                 int second_column_len = 0;
158                 if (second_column != NULL && *second_column != NULL)
159                         second_column_len = strlen(*second_column);
160                 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
161                 {
162                         static unsigned current_song;
163                         char *tmp;
165                         if (current_song != lw->selected) {
166                                 hscroll_reset(&hscroll);
167                                 current_song = lw->selected;
168                         }
170                         tmp = strscroll(&hscroll, songname, options.scroll_sep,
171                                         MAX_SONG_LENGTH);
172                         g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
173                         g_free(tmp);
175                         if (scroll_source_id == 0)
176                                 scroll_source_id =
177                                         g_timeout_add(1000,
178                                                       scroll_timer_callback,
179                                                       NULL);
180                 } else {
181                         hscroll_reset(&hscroll);
183                         if (scroll_source_id != 0) {
184                                 g_source_remove(scroll_source_id);
185                                 scroll_source_id = 0;
186                         }
187                 }
188         }
189 #else
190         (void)second_column;
191 #endif
193         return songname;
196 static void
197 center_playing_item(struct mpdclient *c, bool center_cursor)
199         const struct mpd_song *song;
200         unsigned length = c->playlist.list->len;
201         int idx;
203         song = mpdclient_get_current_song(c);
204         if (song == NULL)
205                 return;
207         /* try to center the song that are playing */
208         idx = playlist_get_index(&c->playlist, c->song);
209         if (idx < 0)
210                 return;
212         if (length < lw->rows)
213         {
214                 if (center_cursor)
215                         list_window_set_cursor(lw, idx);
216                 return;
217         }
219         list_window_center(lw, length, idx);
221         if (center_cursor) {
222                 list_window_set_cursor(lw, idx);
223                 return;
224         }
226         /* make sure the cursor is in the window */
227         list_window_fetch_cursor(lw, length);
230 #ifndef NCMPC_MINI
231 static void
232 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
233                        void *data)
235         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
236         GList **list = tmp->list;
237         struct mpdclient *c = tmp->c;
239         if( *list == NULL ) {
240                 /* create completion list */
241                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
242                 g_completion_add_items(gcmp, *list);
243         }
246 static void
247 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
248                         G_GNUC_UNUSED gchar *line, GList *items,
249                         G_GNUC_UNUSED void *data)
251         if (g_list_length(items) >= 1)
252                 screen_display_completion_list(items);
254 #endif
256 #ifndef NCMPC_MINI
257 /**
258  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
259  * g_completion_set_compare(), because strncmp() takes size_t where
260  * g_completion_set_compare passes a gsize value.
261  */
262 static gint
263 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
265         return strncmp(s1, s2, n);
267 #endif
269 int
270 playlist_save(struct mpdclient *c, char *name, char *defaultname)
272         struct mpd_connection *connection;
273         gchar *filename, *filename_utf8;
274 #ifndef NCMPC_MINI
275         GCompletion *gcmp;
276         GList *list = NULL;
277         completion_callback_data_t data;
278 #endif
280 #ifdef NCMPC_MINI
281         (void)defaultname;
282 #endif
284 #ifndef NCMPC_MINI
285         if (name == NULL) {
286                 /* initialize completion support */
287                 gcmp = g_completion_new(NULL);
288                 g_completion_set_compare(gcmp, completion_strncmp);
289                 data.list = &list;
290                 data.dir_list = NULL;
291                 data.c = c;
292                 wrln_completion_callback_data = &data;
293                 wrln_pre_completion_callback = save_pre_completion_cb;
294                 wrln_post_completion_callback = save_post_completion_cb;
297                 /* query the user for a filename */
298                 filename = screen_readln(_("Save playlist as"),
299                                          defaultname,
300                                          NULL,
301                                          gcmp);
303                 /* destroy completion support */
304                 wrln_completion_callback_data = NULL;
305                 wrln_pre_completion_callback = NULL;
306                 wrln_post_completion_callback = NULL;
307                 g_completion_free(gcmp);
308                 list = string_list_free(list);
309                 if( filename )
310                         filename=g_strstrip(filename);
311         } else
312 #endif
313                         filename=g_strdup(name);
315         if (filename == NULL)
316                 return -1;
318         /* send save command to mpd */
320         filename_utf8 = locale_to_utf8(filename);
322         connection = mpdclient_get_connection(c);
323         if (!mpd_run_save(connection, filename_utf8)) {
324                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
325                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
326                     mpd_connection_clear_error(connection)) {
327                         char *buf;
328                         int key;
330                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
331                                               filename, YES, NO);
332                         key = tolower(screen_getch(buf));
333                         g_free(buf);
335                         if (key != YES[0]) {
336                                 g_free(filename_utf8);
337                                 g_free(filename);
338                                 screen_status_printf(_("Aborted"));
339                                 return -1;
340                         }
342                         if (!mpd_run_rm(connection, filename_utf8) ||
343                             !mpd_run_save(connection, filename_utf8)) {
344                                 mpdclient_handle_error(c);
345                                 g_free(filename_utf8);
346                                 g_free(filename);
347                                 return -1;
348                         }
349                 } else {
350                         mpdclient_handle_error(c);
351                         g_free(filename_utf8);
352                         g_free(filename);
353                         return -1;
354                 }
355         }
357         c->events |= MPD_IDLE_STORED_PLAYLIST;
359         g_free(filename_utf8);
361         /* success */
362         screen_status_printf(_("Saved %s"), filename);
363         g_free(filename);
364         return 0;
367 #ifndef NCMPC_MINI
368 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
369                     GList **list, struct mpdclient *c)
371         g_completion_remove_items(gcmp, *list);
372         *list = string_list_remove(*list, dir);
373         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
374         g_completion_add_items(gcmp, *list);
375         *dir_list = g_list_append(*dir_list, g_strdup(dir));
378 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
380         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
381         GList **dir_list = tmp->dir_list;
382         GList **list = tmp->list;
383         struct mpdclient *c = tmp->c;
385         if (*list == NULL) {
386                 /* create initial list */
387                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
388                 g_completion_add_items(gcmp, *list);
389         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
390                    string_list_find(*dir_list, line) == NULL) {
391                 /* add directory content to list */
392                 add_dir(gcmp, line, dir_list, list, c);
393         }
396 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
397                                    GList *items, void *data)
399         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
400         GList **dir_list = tmp->dir_list;
401         GList **list = tmp->list;
402         struct mpdclient *c = tmp->c;
404         if (g_list_length(items) >= 1)
405                 screen_display_completion_list(items);
407         if (line && line[0] && line[strlen(line) - 1] == '/' &&
408             string_list_find(*dir_list, line) == NULL) {
409                 /* add directory content to list */
410                 add_dir(gcmp, line, dir_list, list, c);
411         }
413 #endif
415 static int
416 handle_add_to_playlist(struct mpdclient *c)
418         gchar *path;
419 #ifndef NCMPC_MINI
420         GCompletion *gcmp;
421         GList *list = NULL;
422         GList *dir_list = NULL;
423         completion_callback_data_t data;
425         /* initialize completion support */
426         gcmp = g_completion_new(NULL);
427         g_completion_set_compare(gcmp, completion_strncmp);
428         data.list = &list;
429         data.dir_list = &dir_list;
430         data.c = c;
431         wrln_completion_callback_data = &data;
432         wrln_pre_completion_callback = add_pre_completion_cb;
433         wrln_post_completion_callback = add_post_completion_cb;
434 #endif
436         /* get path */
437         path = screen_readln(_("Add"),
438                              NULL,
439                              NULL,
440 #ifdef NCMPC_MINI
441                              NULL
442 #else
443                              gcmp
444 #endif
445                              );
447         /* destroy completion data */
448 #ifndef NCMPC_MINI
449         wrln_completion_callback_data = NULL;
450         wrln_pre_completion_callback = NULL;
451         wrln_post_completion_callback = NULL;
452         g_completion_free(gcmp);
453         string_list_free(list);
454         string_list_free(dir_list);
455 #endif
457         /* add the path to the playlist */
458         if (path != NULL) {
459                 char *path_utf8 = locale_to_utf8(path);
460                 mpdclient_cmd_add_path(c, path_utf8);
461                 g_free(path_utf8);
462         }
464         g_free(path);
465         return 0;
468 static void
469 screen_playlist_init(WINDOW *w, int cols, int rows)
471         lw = list_window_init(w, cols, rows);
474 static gboolean
475 timer_hide_cursor(gpointer data)
477         struct mpdclient *c = data;
479         assert(options.hide_cursor > 0);
480         assert(timer_hide_cursor_id != 0);
482         timer_hide_cursor_id = 0;
484         /* hide the cursor when mpd is playing and the user is inactive */
486         if (c->status != NULL &&
487             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
488                 lw->hide_cursor = true;
489                 playlist_repaint();
490         } else
491                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
492                                                      timer_hide_cursor, c);
494         return FALSE;
497 static void
498 screen_playlist_open(struct mpdclient *c)
500         playlist = &c->playlist;
502         assert(timer_hide_cursor_id == 0);
503         if (options.hide_cursor > 0) {
504                 lw->hide_cursor = false;
505                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
506                                                      timer_hide_cursor, c);
507         }
509         playlist_restore_selection();
512 static void
513 screen_playlist_close(void)
515         if (timer_hide_cursor_id != 0) {
516                 g_source_remove(timer_hide_cursor_id);
517                 timer_hide_cursor_id = 0;
518         }
521 static void
522 screen_playlist_resize(int cols, int rows)
524         lw->cols = cols;
525         lw->rows = rows;
529 static void
530 screen_playlist_exit(void)
532         list_window_free(lw);
535 static const char *
536 screen_playlist_title(char *str, size_t size)
538         if (options.host == NULL)
539                 return _("Playlist");
541         g_snprintf(str, size, _("Playlist on %s"), options.host);
542         return str;
545 static void
546 screen_playlist_paint(void)
548         list_window_paint(lw, list_callback, NULL);
551 static void
552 screen_playlist_update(struct mpdclient *c)
554         static int prev_song_id = -1;
556         if (c->events & MPD_IDLE_PLAYLIST)
557                 playlist_restore_selection();
559         current_song_id = c->status != NULL &&
560                 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
561                  mpd_status_get_state(c->status) == MPD_STATE_PAUSE)
562                 ? (int)mpd_status_get_song_id(c->status) : -1;
564         if (current_song_id != prev_song_id) {
565                 prev_song_id = current_song_id;
567                 /* center the cursor */
568                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
569                         center_playing_item(c, false);
571                 playlist_repaint();
572         } else if (c->events & MPD_IDLE_PLAYLIST) {
573                 /* the playlist has changed, we must paint the new
574                    version */
575                 playlist_repaint();
576         }
579 #ifdef HAVE_GETMOUSE
580 static bool
581 handle_mouse_event(struct mpdclient *c)
583         int row;
584         unsigned selected;
585         unsigned long bstate;
587         if (screen_get_mouse_event(c, &bstate, &row) ||
588             list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
589                 playlist_repaint();
590                 return true;
591         }
593         if (bstate & BUTTON1_DOUBLE_CLICKED) {
594                 /* stop */
595                 screen_cmd(c, CMD_STOP);
596                 return true;
597         }
599         selected = lw->start + row;
601         if (bstate & BUTTON1_CLICKED) {
602                 /* play */
603                 if (lw->start + row < playlist_length(playlist))
604                         mpdclient_cmd_play(c, lw->start + row);
605         } else if (bstate & BUTTON3_CLICKED) {
606                 /* delete */
607                 if (selected == lw->selected)
608                         mpdclient_cmd_delete(c, lw->selected);
609         }
611         list_window_set_cursor(lw, selected);
612         list_window_check_selected(lw, playlist_length(playlist));
613         playlist_save_selection();
614         playlist_repaint();
616         return true;
618 #endif
620 static bool
621 screen_playlist_cmd(struct mpdclient *c, command_t cmd)
623         struct mpd_connection *connection;
624         static command_t cached_cmd = CMD_NONE;
625         command_t prev_cmd = cached_cmd;
626         cached_cmd = cmd;
628         lw->hide_cursor = false;
630         if (options.hide_cursor > 0) {
631                 if (timer_hide_cursor_id != 0)
632                         g_source_remove(timer_hide_cursor_id);
633                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
634                                                      timer_hide_cursor, c);
635         }
637         if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
638                 playlist_save_selection();
639                 playlist_repaint();
640                 return true;
641         }
643         switch(cmd) {
644         case CMD_SCREEN_UPDATE:
645                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
646                 playlist_repaint();
647                 return false;
648         case CMD_SELECT_PLAYING:
649                 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
650                                                               c->song));
651                 playlist_save_selection();
652                 playlist_repaint();
653                 return true;
655         case CMD_LIST_FIND:
656         case CMD_LIST_RFIND:
657         case CMD_LIST_FIND_NEXT:
658         case CMD_LIST_RFIND_NEXT:
659                 screen_find(lw, playlist_length(&c->playlist),
660                             cmd, list_callback, NULL);
661                 playlist_save_selection();
662                 playlist_repaint();
663                 return true;
664         case CMD_LIST_JUMP:
665                 screen_jump(lw, list_callback, NULL);
666                 playlist_save_selection();
667                 playlist_repaint();
668                 return true;
670 #ifdef HAVE_GETMOUSE
671         case CMD_MOUSE_EVENT:
672                 return handle_mouse_event(c);
673 #endif
675 #ifdef ENABLE_SONG_SCREEN
676         case CMD_SCREEN_SONG:
677                 if (playlist_selected_song()) {
678                         screen_song_switch(c, playlist_selected_song());
679                         return true;
680                 }
682                 break;
683 #endif
685 #ifdef ENABLE_LYRICS_SCREEN
686         case CMD_SCREEN_LYRICS:
687                 if (lw->selected < playlist_length(&c->playlist)) {
688                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
689                         bool follow = false;
691                         if (c->song && selected &&
692                             !strcmp(mpd_song_get_uri(selected),
693                                     mpd_song_get_uri(c->song)))
694                                 follow = true;
696                         screen_lyrics_switch(c, selected, follow);
697                         return true;
698                 }
700                 break;
701 #endif
702         case CMD_SCREEN_SWAP:
703                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
704                 return true;
706         default:
707                 break;
708         }
710         if (!mpdclient_is_connected(c))
711                 return false;
713         switch(cmd) {
714         case CMD_PLAY:
715                 mpdclient_cmd_play(c, lw->selected);
716                 return true;
718         case CMD_DELETE:
719                 if (lw->range_selection) {
720                         mpdclient_cmd_delete_range(c, lw->selected_start,
721                                                    lw->selected_end + 1);
722                 } else {
723                         mpdclient_cmd_delete(c, lw->selected);
724                 }
726                 list_window_set_cursor(lw, lw->selected_start);
727                 return true;
729         case CMD_SAVE_PLAYLIST:
730                 playlist_save(c, NULL, NULL);
731                 return true;
733         case CMD_ADD:
734                 handle_add_to_playlist(c);
735                 return true;
737         case CMD_SHUFFLE:
738                 if(!lw->range_selection)
739                         /* No range selection, shuffle all list. */
740                         break;
742                 connection = mpdclient_get_connection(c);
743                 if (mpd_run_shuffle_range(connection, lw->selected_start,
744                                           lw->selected_end + 1))
745                         screen_status_message(_("Shuffled playlist"));
746                 else
747                         mpdclient_handle_error(c);
748                 return true;
750         case CMD_LIST_MOVE_UP:
751                 if(lw->selected_start == 0)
752                         return false;
753                 if(lw->range_selection)
754                 {
755                         unsigned i = lw->selected_start;
756                         unsigned last_selected = lw->selected;
757                         for(; i <= lw->selected_end; ++i)
758                                 mpdclient_cmd_move(c, i, i-1);
759                         lw->selected_start--;
760                         lw->selected_end--;
761                         lw->selected = last_selected - 1;
762                         lw->range_base--;
763                 }
764                 else
765                 {
766                         mpdclient_cmd_move(c, lw->selected, lw->selected-1);
767                         lw->selected--;
768                         lw->selected_start--;
769                         lw->selected_end--;
770                 }
772                 playlist_save_selection();
773                 return true;
775         case CMD_LIST_MOVE_DOWN:
776                 if(lw->selected_end+1 >= playlist_length(&c->playlist))
777                         return false;
778                 if(lw->range_selection)
779                 {
780                         int i = lw->selected_end;
781                         unsigned last_selected = lw->selected;
782                         for(; i >= (int)lw->selected_start; --i)
783                                 mpdclient_cmd_move(c, i, i+1);
784                         lw->selected_start++;
785                         lw->selected_end++;
786                         lw->selected = last_selected + 1;
787                         lw->range_base++;
788                 }
789                 else
790                 {
791                         mpdclient_cmd_move(c, lw->selected, lw->selected+1);
792                         lw->selected++;
793                         lw->selected_start++;
794                         lw->selected_end++;
795                 }
797                 playlist_save_selection();
798                 return true;
800         case CMD_LOCATE:
801                 if (playlist_selected_song()) {
802                         screen_file_goto_song(c, playlist_selected_song());
803                         return true;
804                 }
806                 break;
808         default:
809                 break;
810         }
812         return false;
815 const struct screen_functions screen_playlist = {
816         .init = screen_playlist_init,
817         .exit = screen_playlist_exit,
818         .open = screen_playlist_open,
819         .close = screen_playlist_close,
820         .resize = screen_playlist_resize,
821         .paint = screen_playlist_paint,
822         .update = screen_playlist_update,
823         .cmd = screen_playlist_cmd,
824         .get_title = screen_playlist_title,
825 };