Code

screen_play: use mpd_cmd_delete_range()
[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_file.h"
21 #include "screen_interface.h"
22 #include "config.h"
23 #include "i18n.h"
24 #include "charset.h"
25 #include "options.h"
26 #include "mpdclient.h"
27 #include "utils.h"
28 #include "strfsong.h"
29 #include "wreadln.h"
30 #include "command.h"
31 #include "colors.h"
32 #include "screen.h"
33 #include "screen_utils.h"
34 #include "screen_play.h"
36 #ifndef NCMPC_MINI
37 #include "hscroll.h"
38 #endif
40 #include <mpd/client.h>
42 #include <ctype.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <glib.h>
48 #define MAX_SONG_LENGTH 512
50 #ifndef NCMPC_MINI
51 typedef struct
52 {
53         GList **list;
54         GList **dir_list;
55         struct mpdclient *c;
56 } completion_callback_data_t;
58 static bool must_scroll;
59 #endif
61 static struct mpdclient_playlist *playlist;
62 static int current_song_id = -1;
63 static int selected_song_id = -1;
64 static list_window_t *lw = NULL;
65 static guint timer_hide_cursor_id;
67 static void
68 play_paint(void);
70 static void
71 playlist_repaint(void)
72 {
73         play_paint();
74         wrefresh(lw->w);
75 }
77 static const struct mpd_song *
78 playlist_selected_song(void)
79 {
80         return !lw->range_selection &&
81                 lw->selected < playlist_length(playlist)
82                 ? playlist_get(playlist, lw->selected)
83                 : NULL;
84 }
86 static void
87 playlist_save_selection(void)
88 {
89         selected_song_id = playlist_selected_song() != NULL
90                 ? (int)mpd_song_get_id(playlist_selected_song())
91                 : -1;
92 }
94 static void
95 playlist_restore_selection(void)
96 {
97         const struct mpd_song *song;
98         int pos;
100         if (selected_song_id < 0)
101                 /* there was no selection */
102                 return;
104         song = playlist_selected_song();
105         if (song != NULL &&
106             mpd_song_get_id(song) == (unsigned)selected_song_id)
107                 /* selection is still valid */
108                 return;
110         pos = playlist_get_index_from_id(playlist, selected_song_id);
111         if (pos >= 0)
112                 lw->selected = pos;
114         list_window_check_selected(lw, playlist_length(playlist));
115         playlist_save_selection();
118 #ifndef NCMPC_MINI
119 static char *
120 format_duration(unsigned duration)
122         if (duration == 0)
123                 return NULL;
125         return g_strdup_printf("%d:%02d", duration / 60, duration % 60);
127 #endif
129 static const char *
130 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
132         static char songname[MAX_SONG_LENGTH];
133 #ifndef NCMPC_MINI
134         static scroll_state_t st;
135 #endif
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)
149                 *second_column = format_duration(mpd_song_get_duration(song));
151         if (idx == lw->selected)
152         {
153                 int second_column_len = 0;
154                 if (second_column != NULL && *second_column != NULL)
155                         second_column_len = strlen(*second_column);
156                 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
157                 {
158                         static unsigned current_song;
159                         char *tmp;
161                         must_scroll = true;
163                         if (current_song != lw->selected) {
164                                 st.offset = 0;
165                                 current_song = lw->selected;
166                         }
168                         tmp = strscroll(songname, options.scroll_sep,
169                                         MAX_SONG_LENGTH, &st);
170                         g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
171                         g_free(tmp);
172                 }
173                 else
174                         st.offset = 0;
175         }
176 #else
177         (void)second_column;
178 #endif
180         return songname;
183 static void
184 center_playing_item(struct mpdclient *c, bool center_cursor)
186         const struct mpd_song *song;
187         unsigned length = c->playlist.list->len;
188         int idx;
190         song = mpdclient_get_current_song(c);
191         if (song == NULL)
192                 return;
194         /* try to center the song that are playing */
195         idx = playlist_get_index(&c->playlist, c->song);
196         if (idx < 0)
197                 return;
199         if (length < lw->rows)
200         {
201                 if (center_cursor)
202                         list_window_set_selected(lw, idx);
203                 return;
204         }
206         list_window_center(lw, length, idx);
208         if (center_cursor) {
209                 list_window_set_selected(lw, idx);
210                 return;
211         }
213         /* make sure the cursor is in the window */
214         if (lw->selected < lw->start + options.scroll_offset) {
215                 if (lw->start > 0)
216                         lw->selected = lw->start + options.scroll_offset;
217                 if (lw->range_selection) {
218                         lw->selected_start = lw->range_base;
219                         lw->selected_end = lw->selected;
220                 } else {
221                         lw->selected_start = lw->selected;
222                         lw->selected_end = lw->selected;
223                 }
224         } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
225                 if (lw->start + lw->rows < length)
226                         lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
227                 if (lw->range_selection) {
228                         lw->selected_start = lw->selected;
229                         lw->selected_end = lw->range_base;
230                 } else {
231                         lw->selected_start = lw->selected;
232                         lw->selected_end = lw->selected;
233                 }
234         }
237 #ifndef NCMPC_MINI
238 static void
239 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
240                        void *data)
242         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
243         GList **list = tmp->list;
244         struct mpdclient *c = tmp->c;
246         if( *list == NULL ) {
247                 /* create completion list */
248                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
249                 g_completion_add_items(gcmp, *list);
250         }
253 static void
254 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
255                         G_GNUC_UNUSED gchar *line, GList *items,
256                         G_GNUC_UNUSED void *data)
258         if (g_list_length(items) >= 1)
259                 screen_display_completion_list(items);
261 #endif
263 #ifndef NCMPC_MINI
264 /**
265  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
266  * g_completion_set_compare(), because strncmp() takes size_t where
267  * g_completion_set_compare passes a gsize value.
268  */
269 static gint
270 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
272         return strncmp(s1, s2, n);
274 #endif
276 int
277 playlist_save(struct mpdclient *c, char *name, char *defaultname)
279         gchar *filename, *filename_utf8;
280         gint error;
281 #ifndef NCMPC_MINI
282         GCompletion *gcmp;
283         GList *list = NULL;
284         completion_callback_data_t data;
285 #endif
287 #ifdef NCMPC_MINI
288         (void)defaultname;
289 #endif
291 #ifndef NCMPC_MINI
292         if (name == NULL) {
293                 /* initialize completion support */
294                 gcmp = g_completion_new(NULL);
295                 g_completion_set_compare(gcmp, completion_strncmp);
296                 data.list = &list;
297                 data.dir_list = NULL;
298                 data.c = c;
299                 wrln_completion_callback_data = &data;
300                 wrln_pre_completion_callback = save_pre_completion_cb;
301                 wrln_post_completion_callback = save_post_completion_cb;
304                 /* query the user for a filename */
305                 filename = screen_readln(_("Save playlist as"),
306                                          defaultname,
307                                          NULL,
308                                          gcmp);
310                 /* destroy completion support */
311                 wrln_completion_callback_data = NULL;
312                 wrln_pre_completion_callback = NULL;
313                 wrln_post_completion_callback = NULL;
314                 g_completion_free(gcmp);
315                 list = string_list_free(list);
316                 if( filename )
317                         filename=g_strstrip(filename);
318         } else
319 #endif
320                         filename=g_strdup(name);
322         if (filename == NULL)
323                 return -1;
325         /* send save command to mpd */
327         filename_utf8 = locale_to_utf8(filename);
328         error = mpdclient_cmd_save_playlist(c, filename_utf8);
330         if (error) {
331                 gint code = GET_ACK_ERROR_CODE(error);
333                 if (code == MPD_SERVER_ERROR_EXIST) {
334                         char *buf;
335                         int key;
337                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
338                                               filename, YES, NO);
339                         key = tolower(screen_getch(buf));
340                         g_free(buf);
342                         if (key != YES[0]) {
343                                 g_free(filename_utf8);
344                                 g_free(filename);
345                                 screen_status_printf(_("Aborted"));
346                                 return -1;
347                         }
349                         error = mpdclient_cmd_delete_playlist(c, filename_utf8);
350                         if (error) {
351                                 g_free(filename_utf8);
352                                 g_free(filename);
353                                 return -1;
354                         }
356                         error = mpdclient_cmd_save_playlist(c, filename_utf8);
357                         if (error) {
358                                 g_free(filename_utf8);
359                                 g_free(filename);
360                                 return error;
361                         }
362                 } else {
363                         g_free(filename_utf8);
364                         g_free(filename);
365                         return -1;
366                 }
367         }
369         g_free(filename_utf8);
371         /* success */
372         screen_status_printf(_("Saved %s"), filename);
373         g_free(filename);
374         return 0;
377 #ifndef NCMPC_MINI
378 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
379                     GList **list, struct mpdclient *c)
381         g_completion_remove_items(gcmp, *list);
382         *list = string_list_remove(*list, dir);
383         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
384         g_completion_add_items(gcmp, *list);
385         *dir_list = g_list_append(*dir_list, g_strdup(dir));
388 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
390         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
391         GList **dir_list = tmp->dir_list;
392         GList **list = tmp->list;
393         struct mpdclient *c = tmp->c;
395         if (*list == NULL) {
396                 /* create initial list */
397                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
398                 g_completion_add_items(gcmp, *list);
399         } else 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         }
406 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
407                                    GList *items, void *data)
409         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
410         GList **dir_list = tmp->dir_list;
411         GList **list = tmp->list;
412         struct mpdclient *c = tmp->c;
414         if (g_list_length(items) >= 1)
415                 screen_display_completion_list(items);
417         if (line && line[0] && line[strlen(line) - 1] == '/' &&
418             string_list_find(*dir_list, line) == NULL) {
419                 /* add directory content to list */
420                 add_dir(gcmp, line, dir_list, list, c);
421         }
423 #endif
425 static int
426 handle_add_to_playlist(struct mpdclient *c)
428         gchar *path;
429 #ifndef NCMPC_MINI
430         GCompletion *gcmp;
431         GList *list = NULL;
432         GList *dir_list = NULL;
433         completion_callback_data_t data;
435         /* initialize completion support */
436         gcmp = g_completion_new(NULL);
437         g_completion_set_compare(gcmp, completion_strncmp);
438         data.list = &list;
439         data.dir_list = &dir_list;
440         data.c = c;
441         wrln_completion_callback_data = &data;
442         wrln_pre_completion_callback = add_pre_completion_cb;
443         wrln_post_completion_callback = add_post_completion_cb;
444 #endif
446         /* get path */
447         path = screen_readln(_("Add"),
448                              NULL,
449                              NULL,
450 #ifdef NCMPC_MINI
451                              NULL
452 #else
453                              gcmp
454 #endif
455                              );
457         /* destroy completion data */
458 #ifndef NCMPC_MINI
459         wrln_completion_callback_data = NULL;
460         wrln_pre_completion_callback = NULL;
461         wrln_post_completion_callback = NULL;
462         g_completion_free(gcmp);
463         string_list_free(list);
464         string_list_free(dir_list);
465 #endif
467         /* add the path to the playlist */
468         if (path != NULL) {
469                 char *path_utf8 = locale_to_utf8(path);
470                 mpdclient_cmd_add_path(c, path_utf8);
471                 g_free(path_utf8);
472         }
474         g_free(path);
475         return 0;
478 static void
479 play_init(WINDOW *w, int cols, int rows)
481         lw = list_window_init(w, cols, rows);
484 static gboolean
485 timer_hide_cursor(gpointer data)
487         struct mpdclient *c = data;
489         assert(options.hide_cursor > 0);
490         assert(timer_hide_cursor_id != 0);
492         timer_hide_cursor_id = 0;
494         /* hide the cursor when mpd is playing and the user is inactive */
496         if (c->status != NULL &&
497             mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
498                 lw->hide_cursor = true;
499                 playlist_repaint();
500         } else
501                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
502                                                      timer_hide_cursor, c);
504         return FALSE;
507 static void
508 play_open(struct mpdclient *c)
510         playlist = &c->playlist;
512         assert(timer_hide_cursor_id == 0);
513         if (options.hide_cursor > 0) {
514                 lw->hide_cursor = false;
515                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
516                                                      timer_hide_cursor, c);
517         }
519         playlist_restore_selection();
522 static void
523 play_close(void)
525         if (timer_hide_cursor_id != 0) {
526                 g_source_remove(timer_hide_cursor_id);
527                 timer_hide_cursor_id = 0;
528         }
531 static void
532 play_resize(int cols, int rows)
534         lw->cols = cols;
535         lw->rows = rows;
539 static void
540 play_exit(void)
542         list_window_free(lw);
545 static const char *
546 play_title(char *str, size_t size)
548         if (options.host == NULL)
549                 return _("Playlist");
551         g_snprintf(str, size, _("Playlist on %s"), options.host);
552         return str;
555 static void
556 play_paint(void)
558 #ifndef NCMPC_MINI
559         must_scroll = false;
560 #endif
562         list_window_paint(lw, list_callback, NULL);
565 static void
566 play_update(struct mpdclient *c)
568         static int prev_song_id = -1;
570         if (c->events & MPD_IDLE_PLAYLIST)
571                 playlist_restore_selection();
573         current_song_id = c->status != NULL &&
574                 !IS_STOPPED(mpd_status_get_state(c->status))
575                 ? (int)mpd_status_get_song_id(c->status) : -1;
577         if (current_song_id != prev_song_id) {
578                 prev_song_id = current_song_id;
580                 /* center the cursor */
581                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
582                         center_playing_item(c, false);
584                 playlist_repaint();
585 #ifndef NCMPC_MINI
586         } else if (options.scroll && must_scroll) {
587                 /* always repaint if horizontal scrolling is
588                    enabled */
589                 playlist_repaint();
590 #endif
591         } else if (c->events & MPD_IDLE_PLAYLIST) {
592                 /* the playlist has changed, we must paint the new
593                    version */
594                 playlist_repaint();
595         }
598 #ifdef HAVE_GETMOUSE
599 static bool
600 handle_mouse_event(struct mpdclient *c)
602         int row;
603         unsigned selected;
604         unsigned long bstate;
606         if (screen_get_mouse_event(c, &bstate, &row) ||
607             list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
608                 playlist_repaint();
609                 return true;
610         }
612         if (bstate & BUTTON1_DOUBLE_CLICKED) {
613                 /* stop */
614                 screen_cmd(c, CMD_STOP);
615                 return true;
616         }
618         selected = lw->start + row;
620         if (bstate & BUTTON1_CLICKED) {
621                 /* play */
622                 if (lw->start + row < playlist_length(playlist))
623                         mpdclient_cmd_play(c, lw->start + row);
624         } else if (bstate & BUTTON3_CLICKED) {
625                 /* delete */
626                 if (selected == lw->selected)
627                         mpdclient_cmd_delete(c, lw->selected);
628         }
630         lw->selected = selected;
631         list_window_check_selected(lw, playlist_length(playlist));
632         playlist_save_selection();
633         playlist_repaint();
635         return true;
637 #endif
639 static bool
640 play_cmd(struct mpdclient *c, command_t cmd)
642         static command_t cached_cmd = CMD_NONE;
643         command_t prev_cmd = cached_cmd;
644         cached_cmd = cmd;
646         lw->hide_cursor = false;
648         if (options.hide_cursor > 0) {
649                 if (timer_hide_cursor_id != 0)
650                         g_source_remove(timer_hide_cursor_id);
651                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
652                                                      timer_hide_cursor, c);
653         }
655         if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
656                 playlist_save_selection();
657                 playlist_repaint();
658                 return true;
659         }
661         switch(cmd) {
662         case CMD_PLAY:
663                 mpdclient_cmd_play(c, lw->selected);
664                 return true;
665         case CMD_DELETE:
666                 if (lw->range_selection) {
667                         mpdclient_cmd_delete_range(c, lw->selected_start,
668                                                    lw->selected_end + 1);
669                 } else {
670                         mpdclient_cmd_delete(c, lw->selected);
671                 }
673                 lw->selected = lw->selected_end = lw->selected_start;
674                 lw->range_selection = false;
675                 return true;
677         case CMD_SAVE_PLAYLIST:
678                 playlist_save(c, NULL, NULL);
679                 return true;
680         case CMD_ADD:
681                 handle_add_to_playlist(c);
682                 return true;
683         case CMD_SCREEN_UPDATE:
684                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
685                 playlist_repaint();
686                 return false;
687         case CMD_SELECT_PLAYING:
688                 list_window_set_selected(lw, playlist_get_index(&c->playlist,
689                                                                 c->song));
690                 playlist_save_selection();
691                 return true;
692         case CMD_SHUFFLE:
693         {
694                 if(!lw->range_selection)
695                         /* No range selection, shuffle all list. */
696                         break;
698                 if (mpdclient_cmd_shuffle_range(c, lw->selected_start, lw->selected_end+1) == 0)
699                         screen_status_message(_("Shuffled playlist"));
701                 return true;
702         }
703         case CMD_LIST_MOVE_UP:
704                 if(lw->selected_start == 0)
705                         return false;
706                 if(lw->range_selection)
707                 {
708                         unsigned i = lw->selected_start;
709                         unsigned last_selected = lw->selected;
710                         for(; i <= lw->selected_end; ++i)
711                                 mpdclient_cmd_move(c, i, i-1);
712                         lw->selected_start--;
713                         lw->selected_end--;
714                         lw->selected = last_selected - 1;
715                         lw->range_base--;
716                 }
717                 else
718                 {
719                         mpdclient_cmd_move(c, lw->selected, lw->selected-1);
720                         lw->selected--;
721                         lw->selected_start--;
722                         lw->selected_end--;
723                 }
725                 playlist_save_selection();
726                 return true;
727         case CMD_LIST_MOVE_DOWN:
728                 if(lw->selected_end+1 >= playlist_length(&c->playlist))
729                         return false;
730                 if(lw->range_selection)
731                 {
732                         int i = lw->selected_end;
733                         unsigned last_selected = lw->selected;
734                         for(; i >= (int)lw->selected_start; --i)
735                                 mpdclient_cmd_move(c, i, i+1);
736                         lw->selected_start++;
737                         lw->selected_end++;
738                         lw->selected = last_selected + 1;
739                         lw->range_base++;
740                 }
741                 else
742                 {
743                         mpdclient_cmd_move(c, lw->selected, lw->selected+1);
744                         lw->selected++;
745                         lw->selected_start++;
746                         lw->selected_end++;
747                 }
749                 playlist_save_selection();
750                 return true;
751         case CMD_LIST_FIND:
752         case CMD_LIST_RFIND:
753         case CMD_LIST_FIND_NEXT:
754         case CMD_LIST_RFIND_NEXT:
755                 screen_find(lw, playlist_length(&c->playlist),
756                             cmd, list_callback, NULL);
757                 playlist_save_selection();
758                 playlist_repaint();
759                 return true;
760         case CMD_LIST_JUMP:
761                 screen_jump(lw, list_callback, NULL);
762                 playlist_save_selection();
763                 playlist_repaint();
764                 return true;
766 #ifdef HAVE_GETMOUSE
767         case CMD_MOUSE_EVENT:
768                 return handle_mouse_event(c);
769 #endif
771 #ifdef ENABLE_SONG_SCREEN
772         case CMD_SCREEN_SONG:
773                 if (playlist_selected_song()) {
774                         screen_song_switch(c, playlist_selected_song());
775                         return true;
776                 }
778                 break;
779 #endif
781         case CMD_LOCATE:
782                 if (playlist_selected_song()) {
783                         screen_file_goto_song(c, playlist_selected_song());
784                         return true;
785                 }
787                 break;
789 #ifdef ENABLE_LYRICS_SCREEN
790         case CMD_SCREEN_LYRICS:
791                 if (lw->selected < playlist_length(&c->playlist)) {
792                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
793                         bool follow = false;
795                         if (c->song && selected &&
796                             !strcmp(mpd_song_get_uri(selected),
797                                     mpd_song_get_uri(c->song)))
798                                 follow = true;
800                         screen_lyrics_switch(c, selected, follow);
801                         return true;
802                 }
804                 break;
805 #endif
806         case CMD_SCREEN_SWAP:
807                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
808                 return true;
810         default:
811                 break;
812         }
814         return false;
817 const struct screen_functions screen_playlist = {
818         .init = play_init,
819         .exit = play_exit,
820         .open = play_open,
821         .close = play_close,
822         .resize = play_resize,
823         .paint = play_paint,
824         .update = play_update,
825         .cmd = play_cmd,
826         .get_title = play_title,
827 };