Code

screen_play: use idle events instead of playlist_callback
[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 "config.h"
21 #include "i18n.h"
22 #include "charset.h"
23 #include "options.h"
24 #include "mpdclient.h"
25 #include "utils.h"
26 #include "strfsong.h"
27 #include "wreadln.h"
28 #include "command.h"
29 #include "colors.h"
30 #include "screen.h"
31 #include "screen_utils.h"
32 #include "screen_play.h"
34 #ifndef NCMPC_MINI
35 #include "hscroll.h"
36 #endif
38 #include <mpd/client.h>
40 #include <ctype.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <glib.h>
46 #define MAX_SONG_LENGTH 512
48 #ifndef NCMPC_MINI
49 typedef struct
50 {
51         GList **list;
52         GList **dir_list;
53         struct mpdclient *c;
54 } completion_callback_data_t;
56 static bool must_scroll;
57 #endif
59 static struct mpdclient_playlist *playlist;
60 static int current_song_id = -1;
61 static int selected_song_id = -1;
62 static list_window_t *lw = NULL;
63 static guint timer_hide_cursor_id;
65 static void
66 play_paint(void);
68 static void
69 playlist_repaint(void)
70 {
71         play_paint();
72         wrefresh(lw->w);
73 }
75 static const struct mpd_song *
76 playlist_selected_song(void)
77 {
78         return !lw->range_selection &&
79                 lw->selected < playlist_length(playlist)
80                 ? playlist_get(playlist, lw->selected)
81                 : NULL;
82 }
84 static void
85 playlist_save_selection(void)
86 {
87         selected_song_id = playlist_selected_song() != NULL
88                 ? (int)mpd_song_get_id(playlist_selected_song())
89                 : -1;
90 }
92 static void
93 playlist_restore_selection(void)
94 {
95         const struct mpd_song *song;
96         int pos;
98         if (selected_song_id < 0)
99                 /* there was no selection */
100                 return;
102         song = playlist_selected_song();
103         if (song != NULL &&
104             mpd_song_get_id(song) == (unsigned)selected_song_id)
105                 /* selection is still valid */
106                 return;
108         pos = playlist_get_index_from_id(playlist, selected_song_id);
109         if (pos >= 0)
110                 lw->selected = pos;
112         list_window_check_selected(lw, playlist_length(playlist));
113         playlist_save_selection();
116 #ifndef NCMPC_MINI
117 static char *
118 format_duration(unsigned duration)
120         if (duration == 0)
121                 return NULL;
123         return g_strdup_printf("%d:%02d", duration / 60, duration % 60);
125 #endif
127 static const char *
128 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
130         static char songname[MAX_SONG_LENGTH];
131 #ifndef NCMPC_MINI
132         static scroll_state_t st;
133 #endif
134         struct mpd_song *song;
136         if (playlist == NULL || idx >= playlist_length(playlist))
137                 return NULL;
139         song = playlist_get(playlist, idx);
140         if ((int)mpd_song_get_id(song) == current_song_id)
141                 *highlight = true;
143         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
145 #ifndef NCMPC_MINI
146         if(second_column)
147                 *second_column = format_duration(mpd_song_get_duration(song));
149         if (idx == lw->selected)
150         {
151                 int second_column_len = 0;
152                 if (second_column != NULL && *second_column != NULL)
153                         second_column_len = strlen(*second_column);
154                 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
155                 {
156                         static unsigned current_song;
157                         char *tmp;
159                         must_scroll = true;
161                         if (current_song != lw->selected) {
162                                 st.offset = 0;
163                                 current_song = lw->selected;
164                         }
166                         tmp = strscroll(songname, options.scroll_sep,
167                                         MAX_SONG_LENGTH, &st);
168                         g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
169                         g_free(tmp);
170                 }
171                 else
172                         st.offset = 0;
173         }
174 #else
175         (void)second_column;
176 #endif
178         return songname;
181 static void
182 center_playing_item(struct mpdclient *c, bool center_cursor)
184         unsigned length = c->playlist.list->len;
185         int idx;
187         if (!c->song || c->status == NULL ||
188             IS_STOPPED(mpd_status_get_state(c->status)))
189                 return;
191         /* try to center the song that are playing */
192         idx = playlist_get_index(&c->playlist, c->song);
193         if (idx < 0)
194                 return;
196         if (length < lw->rows)
197         {
198                 if (center_cursor)
199                         list_window_set_selected(lw, idx);
200                 return;
201         }
203         list_window_center(lw, length, idx);
205         if (center_cursor) {
206                 list_window_set_selected(lw, idx);
207                 return;
208         }
210         /* make sure the cursor is in the window */
211         if (lw->selected < lw->start + options.scroll_offset) {
212                 if (lw->start > 0)
213                         lw->selected = lw->start + options.scroll_offset;
214                 if (lw->range_selection) {
215                         lw->selected_start = lw->range_base;
216                         lw->selected_end = lw->selected;
217                 } else {
218                         lw->selected_start = lw->selected;
219                         lw->selected_end = lw->selected;
220                 }
221         } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
222                 if (lw->start + lw->rows < length)
223                         lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
224                 if (lw->range_selection) {
225                         lw->selected_start = lw->selected;
226                         lw->selected_end = lw->range_base;
227                 } else {
228                         lw->selected_start = lw->selected;
229                         lw->selected_end = lw->selected;
230                 }
231         }
234 #ifndef NCMPC_MINI
235 static void
236 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
237                        void *data)
239         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
240         GList **list = tmp->list;
241         struct mpdclient *c = tmp->c;
243         if( *list == NULL ) {
244                 /* create completion list */
245                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
246                 g_completion_add_items(gcmp, *list);
247         }
250 static void
251 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
252                         G_GNUC_UNUSED gchar *line, GList *items,
253                         G_GNUC_UNUSED void *data)
255         if (g_list_length(items) >= 1)
256                 screen_display_completion_list(items);
258 #endif
260 #ifndef NCMPC_MINI
261 /**
262  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
263  * g_completion_set_compare(), because strncmp() takes size_t where
264  * g_completion_set_compare passes a gsize value.
265  */
266 static gint
267 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
269         return strncmp(s1, s2, n);
271 #endif
273 int
274 playlist_save(struct mpdclient *c, char *name, char *defaultname)
276         gchar *filename, *filename_utf8;
277         gint error;
278 #ifndef NCMPC_MINI
279         GCompletion *gcmp;
280         GList *list = NULL;
281         completion_callback_data_t data;
282 #endif
284 #ifdef NCMPC_MINI
285         (void)defaultname;
286 #endif
288 #ifndef NCMPC_MINI
289         if (name == NULL) {
290                 /* initialize completion support */
291                 gcmp = g_completion_new(NULL);
292                 g_completion_set_compare(gcmp, completion_strncmp);
293                 data.list = &list;
294                 data.dir_list = NULL;
295                 data.c = c;
296                 wrln_completion_callback_data = &data;
297                 wrln_pre_completion_callback = save_pre_completion_cb;
298                 wrln_post_completion_callback = save_post_completion_cb;
301                 /* query the user for a filename */
302                 filename = screen_readln(_("Save playlist as"),
303                                          defaultname,
304                                          NULL,
305                                          gcmp);
307                 /* destroy completion support */
308                 wrln_completion_callback_data = NULL;
309                 wrln_pre_completion_callback = NULL;
310                 wrln_post_completion_callback = NULL;
311                 g_completion_free(gcmp);
312                 list = string_list_free(list);
313                 if( filename )
314                         filename=g_strstrip(filename);
315         } else
316 #endif
317                         filename=g_strdup(name);
319         if (filename == NULL)
320                 return -1;
322         /* send save command to mpd */
324         filename_utf8 = locale_to_utf8(filename);
325         error = mpdclient_cmd_save_playlist(c, filename_utf8);
327         if (error) {
328                 gint code = GET_ACK_ERROR_CODE(error);
330                 if (code == MPD_SERVER_ERROR_EXIST) {
331                         char *buf;
332                         int key;
334                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
335                                               filename, YES, NO);
336                         key = tolower(screen_getch(buf));
337                         g_free(buf);
339                         if (key != YES[0]) {
340                                 g_free(filename_utf8);
341                                 g_free(filename);
342                                 screen_status_printf(_("Aborted"));
343                                 return -1;
344                         }
346                         error = mpdclient_cmd_delete_playlist(c, filename_utf8);
347                         if (error) {
348                                 g_free(filename_utf8);
349                                 g_free(filename);
350                                 return -1;
351                         }
353                         error = mpdclient_cmd_save_playlist(c, filename_utf8);
354                         if (error) {
355                                 g_free(filename_utf8);
356                                 g_free(filename);
357                                 return error;
358                         }
359                 } else {
360                         g_free(filename_utf8);
361                         g_free(filename);
362                         return -1;
363                 }
364         }
366         g_free(filename_utf8);
368         /* success */
369         screen_status_printf(_("Saved %s"), filename);
370         g_free(filename);
371         return 0;
374 #ifndef NCMPC_MINI
375 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
376                     GList **list, struct mpdclient *c)
378         g_completion_remove_items(gcmp, *list);
379         *list = string_list_remove(*list, dir);
380         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
381         g_completion_add_items(gcmp, *list);
382         *dir_list = g_list_append(*dir_list, g_strdup(dir));
385 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
387         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
388         GList **dir_list = tmp->dir_list;
389         GList **list = tmp->list;
390         struct mpdclient *c = tmp->c;
392         if (*list == NULL) {
393                 /* create initial list */
394                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
395                 g_completion_add_items(gcmp, *list);
396         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
397                    string_list_find(*dir_list, line) == NULL) {
398                 /* add directory content to list */
399                 add_dir(gcmp, line, dir_list, list, c);
400         }
403 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
404                                    GList *items, void *data)
406         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
407         GList **dir_list = tmp->dir_list;
408         GList **list = tmp->list;
409         struct mpdclient *c = tmp->c;
411         if (g_list_length(items) >= 1)
412                 screen_display_completion_list(items);
414         if (line && line[0] && line[strlen(line) - 1] == '/' &&
415             string_list_find(*dir_list, line) == NULL) {
416                 /* add directory content to list */
417                 add_dir(gcmp, line, dir_list, list, c);
418         }
420 #endif
422 static int
423 handle_add_to_playlist(struct mpdclient *c)
425         gchar *path;
426 #ifndef NCMPC_MINI
427         GCompletion *gcmp;
428         GList *list = NULL;
429         GList *dir_list = NULL;
430         completion_callback_data_t data;
432         /* initialize completion support */
433         gcmp = g_completion_new(NULL);
434         g_completion_set_compare(gcmp, completion_strncmp);
435         data.list = &list;
436         data.dir_list = &dir_list;
437         data.c = c;
438         wrln_completion_callback_data = &data;
439         wrln_pre_completion_callback = add_pre_completion_cb;
440         wrln_post_completion_callback = add_post_completion_cb;
441 #endif
443         /* get path */
444         path = screen_readln(_("Add"),
445                              NULL,
446                              NULL,
447 #ifdef NCMPC_MINI
448                              NULL
449 #else
450                              gcmp
451 #endif
452                              );
454         /* destroy completion data */
455 #ifndef NCMPC_MINI
456         wrln_completion_callback_data = NULL;
457         wrln_pre_completion_callback = NULL;
458         wrln_post_completion_callback = NULL;
459         g_completion_free(gcmp);
460         string_list_free(list);
461         string_list_free(dir_list);
462 #endif
464         /* add the path to the playlist */
465         if (path != NULL) {
466                 char *path_utf8 = locale_to_utf8(path);
467                 mpdclient_cmd_add_path(c, path_utf8);
468                 g_free(path_utf8);
469         }
471         g_free(path);
472         return 0;
475 static void
476 play_init(WINDOW *w, int cols, int rows)
478         lw = list_window_init(w, cols, rows);
481 static gboolean
482 timer_hide_cursor(gpointer data)
484         struct mpdclient *c = data;
486         assert(options.hide_cursor > 0);
487         assert(timer_hide_cursor_id != 0);
489         timer_hide_cursor_id = 0;
491         /* hide the cursor when mpd is playing and the user is inactive */
493         if (c->status != NULL &&
494             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
495                 lw->hide_cursor = true;
496                 playlist_repaint();
497         } else
498                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
499                                                      timer_hide_cursor, c);
501         return FALSE;
504 static void
505 play_open(struct mpdclient *c)
507         playlist = &c->playlist;
509         assert(timer_hide_cursor_id == 0);
510         if (options.hide_cursor > 0) {
511                 lw->hide_cursor = false;
512                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
513                                                      timer_hide_cursor, c);
514         }
516         playlist_restore_selection();
519 static void
520 play_close(void)
522         if (timer_hide_cursor_id != 0) {
523                 g_source_remove(timer_hide_cursor_id);
524                 timer_hide_cursor_id = 0;
525         }
528 static void
529 play_resize(int cols, int rows)
531         lw->cols = cols;
532         lw->rows = rows;
536 static void
537 play_exit(void)
539         list_window_free(lw);
542 static const char *
543 play_title(char *str, size_t size)
545         if (options.host == NULL)
546                 return _("Playlist");
548         g_snprintf(str, size, _("Playlist on %s"), options.host);
549         return str;
552 static void
553 play_paint(void)
555 #ifndef NCMPC_MINI
556         must_scroll = false;
557 #endif
559         list_window_paint(lw, list_callback, NULL);
562 static void
563 play_update(struct mpdclient *c)
565         static int prev_song_id = -1;
567         if (c->events & MPD_IDLE_PLAYLIST)
568                 playlist_restore_selection();
570         current_song_id = c->song != NULL && c->status != NULL &&
571                 !IS_STOPPED(mpd_status_get_state(c->status))
572                 ? (int)mpd_song_get_id(c->song) : -1;
574         if (current_song_id != prev_song_id) {
575                 prev_song_id = current_song_id;
577                 /* center the cursor */
578                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
579                         center_playing_item(c, false);
581                 playlist_repaint();
582 #ifndef NCMPC_MINI
583         } else if (options.scroll && must_scroll) {
584                 /* always repaint if horizontal scrolling is
585                    enabled */
586                 playlist_repaint();
587 #endif
588         } else if (c->events & MPD_IDLE_PLAYLIST) {
589                 /* the playlist has changed, we must paint the new
590                    version */
591                 playlist_repaint();
592         }
595 #ifdef HAVE_GETMOUSE
596 static bool
597 handle_mouse_event(struct mpdclient *c)
599         int row;
600         unsigned selected;
601         unsigned long bstate;
603         if (screen_get_mouse_event(c, &bstate, &row) ||
604             list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
605                 playlist_repaint();
606                 return true;
607         }
609         if (bstate & BUTTON1_DOUBLE_CLICKED) {
610                 /* stop */
611                 screen_cmd(c, CMD_STOP);
612                 return true;
613         }
615         selected = lw->start + row;
617         if (bstate & BUTTON1_CLICKED) {
618                 /* play */
619                 if (lw->start + row < playlist_length(playlist))
620                         mpdclient_cmd_play(c, lw->start + row);
621         } else if (bstate & BUTTON3_CLICKED) {
622                 /* delete */
623                 if (selected == lw->selected)
624                         mpdclient_cmd_delete(c, lw->selected);
625         }
627         lw->selected = selected;
628         list_window_check_selected(lw, playlist_length(playlist));
629         playlist_save_selection();
630         playlist_repaint();
632         return true;
634 #endif
636 static bool
637 play_cmd(struct mpdclient *c, command_t cmd)
639         static command_t cached_cmd = CMD_NONE;
640         command_t prev_cmd = cached_cmd;
641         cached_cmd = cmd;
643         lw->hide_cursor = false;
645         if (options.hide_cursor > 0) {
646                 if (timer_hide_cursor_id != 0)
647                         g_source_remove(timer_hide_cursor_id);
648                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
649                                                      timer_hide_cursor, c);
650         }
652         if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
653                 playlist_save_selection();
654                 playlist_repaint();
655                 return true;
656         }
658         switch(cmd) {
659         case CMD_PLAY:
660                 mpdclient_cmd_play(c, lw->selected);
661                 return true;
662         case CMD_DELETE:
663         {
664                 int i = lw->selected_end, start = lw->selected_start;
665                 for(; i >= start; --i)
666                         mpdclient_cmd_delete(c, i);
668                 i++;
669                 if(i >= (int)playlist_length(&c->playlist))
670                         i--;
671                 lw->selected = i;
672                 lw->selected_start = i;
673                 lw->selected_end = i;
674                 lw->range_selection = false;
676                 playlist_save_selection();
677                 return true;
678         }
679         case CMD_SAVE_PLAYLIST:
680                 playlist_save(c, NULL, NULL);
681                 return true;
682         case CMD_ADD:
683                 handle_add_to_playlist(c);
684                 return true;
685         case CMD_SCREEN_UPDATE:
686                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
687                 playlist_repaint();
688                 return false;
689         case CMD_SELECT_PLAYING:
690                 list_window_set_selected(lw, playlist_get_index(&c->playlist,
691                                                                 c->song));
692                 playlist_save_selection();
693                 return true;
694         case CMD_SHUFFLE:
695         {
696                 if(!lw->range_selection)
697                         /* No range selection, shuffle all list. */
698                         break;
700                 if (mpdclient_cmd_shuffle_range(c, lw->selected_start, lw->selected_end+1) == 0)
701                         screen_status_message(_("Shuffled playlist"));
703                 return true;
704         }
705         case CMD_LIST_MOVE_UP:
706                 if(lw->selected_start == 0)
707                         return false;
708                 if(lw->range_selection)
709                 {
710                         unsigned i = lw->selected_start;
711                         unsigned last_selected = lw->selected;
712                         for(; i <= lw->selected_end; ++i)
713                                 mpdclient_cmd_move(c, i, i-1);
714                         lw->selected_start--;
715                         lw->selected_end--;
716                         lw->selected = last_selected - 1;
717                         lw->range_base--;
718                 }
719                 else
720                 {
721                         mpdclient_cmd_move(c, lw->selected, lw->selected-1);
722                         lw->selected--;
723                         lw->selected_start--;
724                         lw->selected_end--;
725                 }
727                 playlist_save_selection();
728                 return true;
729         case CMD_LIST_MOVE_DOWN:
730                 if(lw->selected_end+1 >= playlist_length(&c->playlist))
731                         return false;
732                 if(lw->range_selection)
733                 {
734                         int i = lw->selected_end;
735                         unsigned last_selected = lw->selected;
736                         for(; i >= (int)lw->selected_start; --i)
737                                 mpdclient_cmd_move(c, i, i+1);
738                         lw->selected_start++;
739                         lw->selected_end++;
740                         lw->selected = last_selected + 1;
741                         lw->range_base++;
742                 }
743                 else
744                 {
745                         mpdclient_cmd_move(c, lw->selected, lw->selected+1);
746                         lw->selected++;
747                         lw->selected_start++;
748                         lw->selected_end++;
749                 }
751                 playlist_save_selection();
752                 return true;
753         case CMD_LIST_FIND:
754         case CMD_LIST_RFIND:
755         case CMD_LIST_FIND_NEXT:
756         case CMD_LIST_RFIND_NEXT:
757                 screen_find(lw, playlist_length(&c->playlist),
758                             cmd, list_callback, NULL);
759                 playlist_save_selection();
760                 playlist_repaint();
761                 return true;
762         case CMD_LIST_JUMP:
763                 screen_jump(lw, list_callback, NULL);
764                 playlist_save_selection();
765                 playlist_repaint();
766                 return true;
768 #ifdef HAVE_GETMOUSE
769         case CMD_MOUSE_EVENT:
770                 return handle_mouse_event(c);
771 #endif
773 #ifdef ENABLE_SONG_SCREEN
774         case CMD_SCREEN_SONG:
775                 if (playlist_selected_song()) {
776                         screen_song_switch(c, playlist_selected_song());
777                         return true;
778                 }
780                 break;
781 #endif
783         case CMD_LOCATE:
784                 if (playlist_selected_song()) {
785                         screen_file_goto_song(c, playlist_selected_song());
786                         return true;
787                 }
789                 break;
791 #ifdef ENABLE_LYRICS_SCREEN
792         case CMD_SCREEN_LYRICS:
793                 if (lw->selected < playlist_length(&c->playlist)) {
794                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
795                         bool follow = false;
797                         if (c->song && selected &&
798                             !strcmp(mpd_song_get_uri(selected),
799                                     mpd_song_get_uri(c->song)))
800                                 follow = true;
802                         screen_lyrics_switch(c, selected, follow);
803                         return true;
804                 }
806                 break;
807 #endif
808         case CMD_SCREEN_SWAP:
809                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
810                 return true;
812         default:
813                 break;
814         }
816         return false;
819 const struct screen_functions screen_playlist = {
820         .init = play_init,
821         .exit = play_exit,
822         .open = play_open,
823         .close = play_close,
824         .resize = play_resize,
825         .paint = play_paint,
826         .update = play_update,
827         .cmd = play_cmd,
828         .get_title = play_title,
829 };