Code

e1389b836f23939d987fcaf1d73bc4d696a3d437
[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 <ctype.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <glib.h>
44 #define MAX_SONG_LENGTH 512
46 #ifndef NCMPC_MINI
47 typedef struct
48 {
49         GList **list;
50         GList **dir_list;
51         mpdclient_t *c;
52 } completion_callback_data_t;
53 #endif
55 static struct mpdclient_playlist *playlist;
56 static int current_song_id = -1;
57 static list_window_t *lw = NULL;
58 static guint timer_hide_cursor_id;
60 static void
61 play_paint(void);
63 static void
64 playlist_repaint(void)
65 {
66         play_paint();
67         wrefresh(lw->w);
68 }
70 static void
71 playlist_repaint_if_active(void)
72 {
73         if (screen_is_visible(&screen_playlist))
74                 playlist_repaint();
75 }
77 static void
78 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
79 {
80         switch (event) {
81         case PLAYLIST_EVENT_DELETE:
82                 break;
83         case PLAYLIST_EVENT_MOVE:
84                 if(lw->range_selection < 0)
85                 {
86                         lw->selected = *((int *) data);
87                         if (lw->selected < lw->start)
88                                 lw->start--;
89                 }
90                 break;
91         default:
92                 break;
93         }
95         list_window_check_selected(lw, c->playlist.list->len);
96         playlist_repaint_if_active();
97 }
99 #ifndef NCMPC_MINI
100 static char *
101 format_duration(int duration)
103         if (duration == MPD_SONG_NO_TIME)
104                 return NULL;
106         return g_strdup_printf("%d:%02d", duration / 60, duration % 60);
108 #endif
110 static const char *
111 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
113         static char songname[MAX_SONG_LENGTH];
114 #ifndef NCMPC_MINI
115         static scroll_state_t st;
116 #endif
117         mpd_Song *song;
119         if (playlist == NULL || idx >= playlist_length(playlist))
120                 return NULL;
122         song = playlist_get(playlist, idx);
123         if (song->id == current_song_id)
124                 *highlight = true;
126         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
128 #ifndef NCMPC_MINI
129         if(second_column)
130                 *second_column = format_duration(song->time);
132         if (idx == lw->selected)
133         {
134                 int second_column_len = 0;
135                 if (second_column != NULL && *second_column != NULL)
136                         second_column_len = strlen(*second_column);
137                 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
138                 {
139                         static unsigned current_song;
140                         char *tmp;
142                         if (current_song != lw->selected) {
143                                 st.offset = 0;
144                                 current_song = lw->selected;
145                         }
147                         tmp = strscroll(songname, options.scroll_sep,
148                                         MAX_SONG_LENGTH, &st);
149                         g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
150                         g_free(tmp);
151                 }
152                 else
153                         st.offset = 0;
154         }
155 #else
156         (void)second_column;
157 #endif
159         return songname;
162 static void
163 center_playing_item(mpdclient_t *c, bool center_cursor)
165         unsigned length = c->playlist.list->len;
166         int idx;
168         if (!c->song || c->status == NULL ||
169                 IS_STOPPED(c->status->state))
170                 return;
172         /* try to center the song that are playing */
173         idx = playlist_get_index(c, c->song);
174         if (idx < 0)
175                 return;
177         if (length < lw->rows)
178         {
179                 if (center_cursor)
180                         list_window_set_selected(lw, idx);
181                 return;
182         }
184         list_window_center(lw, length, idx);
186         if (center_cursor) {
187                 list_window_set_selected(lw, idx);
188                 return;
189         }
191         /* make sure the cursor is in the window */
192         if (lw->selected < lw->start + options.scroll_offset) {
193                 if (lw->start > 0)
194                         lw->selected = lw->start + options.scroll_offset;
195                 if (lw->range_selection) {
196                         lw->selected_start = lw->range_base;
197                         lw->selected_end = lw->selected;
198                 } else {
199                         lw->selected_start = lw->selected;
200                         lw->selected_end = lw->selected;
201                 }
202         } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
203                 if (lw->start + lw->rows < length)
204                         lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
205                 if (lw->range_selection) {
206                         lw->selected_start = lw->selected;
207                         lw->selected_end = lw->range_base;
208                 } else {
209                         lw->selected_start = lw->selected;
210                         lw->selected_end = lw->selected;
211                 }
212         }
215 #ifndef NCMPC_MINI
216 static void
217 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
218                        void *data)
220         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
221         GList **list = tmp->list;
222         mpdclient_t *c = tmp->c;
224         if( *list == NULL ) {
225                 /* create completion list */
226                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
227                 g_completion_add_items(gcmp, *list);
228         }
231 static void
232 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
233                         G_GNUC_UNUSED gchar *line, GList *items,
234                         G_GNUC_UNUSED void *data)
236         if (g_list_length(items) >= 1)
237                 screen_display_completion_list(items);
239 #endif
241 #ifndef NCMPC_MINI
242 /**
243  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
244  * g_completion_set_compare(), because strncmp() takes size_t where
245  * g_completion_set_compare passes a gsize value.
246  */
247 static gint
248 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
250         return strncmp(s1, s2, n);
252 #endif
254 int
255 playlist_save(mpdclient_t *c, char *name, char *defaultname)
257         gchar *filename, *filename_utf8;
258         gint error;
259 #ifndef NCMPC_MINI
260         GCompletion *gcmp;
261         GList *list = NULL;
262         completion_callback_data_t data;
263 #endif
265 #ifdef NCMPC_MINI
266         (void)defaultname;
267 #endif
269 #ifndef NCMPC_MINI
270         if (name == NULL) {
271                 /* initialize completion support */
272                 gcmp = g_completion_new(NULL);
273                 g_completion_set_compare(gcmp, completion_strncmp);
274                 data.list = &list;
275                 data.dir_list = NULL;
276                 data.c = c;
277                 wrln_completion_callback_data = &data;
278                 wrln_pre_completion_callback = save_pre_completion_cb;
279                 wrln_post_completion_callback = save_post_completion_cb;
282                 /* query the user for a filename */
283                 filename = screen_readln(screen.status_window.w,
284                                          _("Save playlist as"),
285                                          defaultname,
286                                          NULL,
287                                          gcmp);
289                 /* destroy completion support */
290                 wrln_completion_callback_data = NULL;
291                 wrln_pre_completion_callback = NULL;
292                 wrln_post_completion_callback = NULL;
293                 g_completion_free(gcmp);
294                 list = string_list_free(list);
295                 if( filename )
296                         filename=g_strstrip(filename);
297         } else
298 #endif
299                         filename=g_strdup(name);
301         if (filename == NULL)
302                 return -1;
304         /* send save command to mpd */
306         filename_utf8 = locale_to_utf8(filename);
307         error = mpdclient_cmd_save_playlist(c, filename_utf8);
308         g_free(filename_utf8);
310         if (error) {
311                 gint code = GET_ACK_ERROR_CODE(error);
313                 if (code == MPD_ACK_ERROR_EXIST) {
314                         char *buf;
315                         int key;
317                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
318                                               filename, YES, NO);
319                         key = tolower(screen_getch(screen.status_window.w,
320                                                    buf));
321                         g_free(buf);
323                         if (key == YES[0]) {
324                                 filename_utf8 = locale_to_utf8(filename);
325                                 error = mpdclient_cmd_delete_playlist(c, filename_utf8);
326                                 g_free(filename_utf8);
328                                 if (error) {
329                                         g_free(filename);
330                                         return -1;
331                                 }
333                                 error = playlist_save(c, filename, NULL);
334                                 g_free(filename);
335                                 return error;
336                         }
338                         screen_status_printf(_("Aborted"));
339                 }
341                 g_free(filename);
342                 return -1;
343         }
345         /* success */
346         screen_status_printf(_("Saved %s"), filename);
347         g_free(filename);
348         return 0;
351 #ifndef NCMPC_MINI
352 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
353                     GList **list, mpdclient_t *c)
355         g_completion_remove_items(gcmp, *list);
356         *list = string_list_remove(*list, dir);
357         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
358         g_completion_add_items(gcmp, *list);
359         *dir_list = g_list_append(*dir_list, g_strdup(dir));
362 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
364         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
365         GList **dir_list = tmp->dir_list;
366         GList **list = tmp->list;
367         mpdclient_t *c = tmp->c;
369         if (*list == NULL) {
370                 /* create initial list */
371                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
372                 g_completion_add_items(gcmp, *list);
373         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
374                    string_list_find(*dir_list, line) == NULL) {
375                 /* add directory content to list */
376                 add_dir(gcmp, line, dir_list, list, c);
377         }
380 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
381                                    GList *items, void *data)
383         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
384         GList **dir_list = tmp->dir_list;
385         GList **list = tmp->list;
386         mpdclient_t *c = tmp->c;
388         if (g_list_length(items) >= 1)
389                 screen_display_completion_list(items);
391         if (line && line[0] && line[strlen(line) - 1] == '/' &&
392             string_list_find(*dir_list, line) == NULL) {
393                 /* add directory content to list */
394                 add_dir(gcmp, line, dir_list, list, c);
395         }
397 #endif
399 static int
400 handle_add_to_playlist(mpdclient_t *c)
402         gchar *path;
403 #ifndef NCMPC_MINI
404         GCompletion *gcmp;
405         GList *list = NULL;
406         GList *dir_list = NULL;
407         completion_callback_data_t data;
409         /* initialize completion support */
410         gcmp = g_completion_new(NULL);
411         g_completion_set_compare(gcmp, completion_strncmp);
412         data.list = &list;
413         data.dir_list = &dir_list;
414         data.c = c;
415         wrln_completion_callback_data = &data;
416         wrln_pre_completion_callback = add_pre_completion_cb;
417         wrln_post_completion_callback = add_post_completion_cb;
418 #endif
420         /* get path */
421         path = screen_readln(screen.status_window.w,
422                              _("Add"),
423                              NULL,
424                              NULL,
425 #ifdef NCMPC_MINI
426                              NULL
427 #else
428                              gcmp
429 #endif
430                              );
432         /* destroy completion data */
433 #ifndef NCMPC_MINI
434         wrln_completion_callback_data = NULL;
435         wrln_pre_completion_callback = NULL;
436         wrln_post_completion_callback = NULL;
437         g_completion_free(gcmp);
438         string_list_free(list);
439         string_list_free(dir_list);
440 #endif
442         /* add the path to the playlist */
443         if (path != NULL) {
444                 char *path_utf8 = locale_to_utf8(path);
445                 mpdclient_cmd_add_path(c, path_utf8);
446                 g_free(path_utf8);
447         }
449         g_free(path);
450         return 0;
453 static void
454 play_init(WINDOW *w, int cols, int rows)
456         lw = list_window_init(w, cols, rows);
459 static gboolean
460 timer_hide_cursor(gpointer data)
462         struct mpdclient *c = data;
464         assert(options.hide_cursor > 0);
465         assert(timer_hide_cursor_id != 0);
467         timer_hide_cursor_id = 0;
469         /* hide the cursor when mpd is playing and the user is inactive */
471         if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
472                 lw->hide_cursor = true;
473                 playlist_repaint();
474         } else
475                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
476                                                      timer_hide_cursor, c);
478         return FALSE;
481 static void
482 play_open(mpdclient_t *c)
484         static gboolean install_cb = TRUE;
486         playlist = &c->playlist;
488         assert(timer_hide_cursor_id == 0);
489         if (options.hide_cursor > 0) {
490                 lw->hide_cursor = false;
491                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
492                                                      timer_hide_cursor, c);
493         }
495         if (install_cb) {
496                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
497                 install_cb = FALSE;
498         }
501 static void
502 play_close(void)
504         if (timer_hide_cursor_id != 0) {
505                 g_source_remove(timer_hide_cursor_id);
506                 timer_hide_cursor_id = 0;
507         }
510 static void
511 play_resize(int cols, int rows)
513         lw->cols = cols;
514         lw->rows = rows;
518 static void
519 play_exit(void)
521         list_window_free(lw);
524 static const char *
525 play_title(char *str, size_t size)
527         if( strcmp(options.host, "localhost") == 0 )
528                 return _("Playlist");
530         g_snprintf(str, size, _("Playlist on %s"), options.host);
531         return str;
534 static void
535 play_paint(void)
537         list_window_paint(lw, list_callback, NULL);
540 static void
541 play_update(mpdclient_t *c)
543         static int prev_song_id = -1;
545         current_song_id = c->song != NULL && c->status != NULL &&
546                 !IS_STOPPED(c->status->state) ? c->song->id : -1;
548         if (current_song_id != prev_song_id) {
549                 prev_song_id = current_song_id;
551                 /* center the cursor */
552                 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
553                         center_playing_item(c, false);
555                 playlist_repaint();
556 #ifndef NCMPC_MINI
557         } else if (options.scroll) {
558                 /* always repaint if horizontal scrolling is
559                    enabled */
560                 playlist_repaint();
561 #endif
562         }
565 #ifdef HAVE_GETMOUSE
566 static bool
567 handle_mouse_event(struct mpdclient *c)
569         int row;
570         unsigned selected;
571         unsigned long bstate;
573         if (screen_get_mouse_event(c, &bstate, &row) ||
574             list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
575                 playlist_repaint();
576                 return true;
577         }
579         if (bstate & BUTTON1_DOUBLE_CLICKED) {
580                 /* stop */
581                 screen_cmd(c, CMD_STOP);
582                 return true;
583         }
585         selected = lw->start + row;
587         if (bstate & BUTTON1_CLICKED) {
588                 /* play */
589                 if (lw->start + row < playlist_length(playlist))
590                         mpdclient_cmd_play(c, lw->start + row);
591         } else if (bstate & BUTTON3_CLICKED) {
592                 /* delete */
593                 if (selected == lw->selected)
594                         mpdclient_cmd_delete(c, lw->selected);
595         }
597         lw->selected = selected;
598         list_window_check_selected(lw, playlist_length(playlist));
599         playlist_repaint();
601         return true;
603 #endif
605 static bool
606 play_cmd(mpdclient_t *c, command_t cmd)
608         static command_t cached_cmd = CMD_NONE;
609         command_t prev_cmd = cached_cmd;
610         cached_cmd = cmd;
612         lw->hide_cursor = false;
614         if (options.hide_cursor > 0) {
615                 if (timer_hide_cursor_id != 0)
616                         g_source_remove(timer_hide_cursor_id);
617                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
618                                                      timer_hide_cursor, c);
619         }
621         if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
622                 playlist_repaint();
623                 return true;
624         }
626         switch(cmd) {
627         case CMD_PLAY:
628                 mpdclient_cmd_play(c, lw->selected);
629                 return true;
630         case CMD_DELETE:
631         {
632                 int i = lw->selected_end, start = lw->selected_start;
633                 for(; i >= start; --i)
634                         mpdclient_cmd_delete(c, i);
636                 i++;
637                 if(i >= (int)playlist_length(&c->playlist))
638                         i--;
639                 lw->selected = i;
640                 lw->selected_start = i;
641                 lw->selected_end = i;
642                 lw->range_selection = false;
644                 return true;
645         }
646         case CMD_SAVE_PLAYLIST:
647                 playlist_save(c, NULL, NULL);
648                 return true;
649         case CMD_ADD:
650                 handle_add_to_playlist(c);
651                 return true;
652         case CMD_SCREEN_UPDATE:
653                 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
654                 playlist_repaint();
655                 return false;
656         case CMD_SELECT_PLAYING:
657                 list_window_set_selected(lw, playlist_get_index(c, c->song));
658                 return true;
659         case CMD_SHUFFLE:
660         {
661                 if(!lw->range_selection)
662                         /* No range selection, shuffle all list. */
663                         break;
665                 if (mpdclient_cmd_shuffle_range(c, lw->selected_start, lw->selected_end+1) == 0)
666                         screen_status_message(_("Shuffled playlist"));
668                 return true;
669         }
670         case CMD_LIST_MOVE_UP:
671                 if(lw->selected_start == 0)
672                         return false;
673                 if(lw->range_selection)
674                 {
675                         unsigned i = lw->selected_start;
676                         unsigned last_selected = lw->selected;
677                         for(; i <= lw->selected_end; ++i)
678                                 mpdclient_cmd_move(c, i, i-1);
679                         lw->selected_start--;
680                         lw->selected_end--;
681                         lw->selected = last_selected - 1;
682                         lw->range_base--;
683                 }
684                 else
685                 {
686                         mpdclient_cmd_move(c, lw->selected, lw->selected-1);
687                         lw->selected--;
688                         lw->selected_start--;
689                         lw->selected_end--;
690                 }
691                 return true;
692         case CMD_LIST_MOVE_DOWN:
693                 if(lw->selected_end+1 >= playlist_length(&c->playlist))
694                         return false;
695                 if(lw->range_selection)
696                 {
697                         int i = lw->selected_end;
698                         unsigned last_selected = lw->selected;
699                         for(; i >= (int)lw->selected_start; --i)
700                                 mpdclient_cmd_move(c, i, i+1);
701                         lw->selected_start++;
702                         lw->selected_end++;
703                         lw->selected = last_selected + 1;
704                         lw->range_base++;
705                 }
706                 else
707                 {
708                         mpdclient_cmd_move(c, lw->selected, lw->selected+1);
709                         lw->selected++;
710                         lw->selected_start++;
711                         lw->selected_end++;
712                 }
713                 return true;
714         case CMD_LIST_FIND:
715         case CMD_LIST_RFIND:
716         case CMD_LIST_FIND_NEXT:
717         case CMD_LIST_RFIND_NEXT:
718                 screen_find(lw, playlist_length(&c->playlist),
719                             cmd, list_callback, NULL);
720                 playlist_repaint();
721                 return true;
722         case CMD_LIST_JUMP:
723                 screen_jump(lw, list_callback, NULL);
724                 playlist_repaint();
725                 return true;
727 #ifdef HAVE_GETMOUSE
728         case CMD_MOUSE_EVENT:
729                 return handle_mouse_event(c);
730 #endif
732 #ifdef ENABLE_SONG_SCREEN
733         case CMD_SCREEN_SONG:
734                 if (lw->selected < playlist_length(&c->playlist)) {
735                         screen_song_switch(c, playlist_get(&c->playlist, lw->selected));
736                         return true;
737                 }
739                 break;
740 #endif
742         case CMD_LOCATE:
743                 if (lw->selected < playlist_length(&c->playlist)) {
744                         screen_file_goto_song(c, playlist_get(&c->playlist, lw->selected));
745                         return true;
746                 }
748                 break;
750 #ifdef ENABLE_LYRICS_SCREEN
751         case CMD_SCREEN_LYRICS:
752                 if (lw->selected < playlist_length(&c->playlist)) {
753                         struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
754                         bool follow = false;
756                         if (c->song && selected &&
757                             !strcmp(selected->file, c->song->file))
758                                 follow = true;
760                         screen_lyrics_switch(c, selected, follow);
761                         return true;
762                 }
764                 break;
765 #endif
766         case CMD_SCREEN_SWAP:
767                 screen_swap(c, playlist_get(&c->playlist, lw->selected));
768                 return true;
770         default:
771                 break;
772         }
774         return false;
777 const struct screen_functions screen_playlist = {
778         .init = play_init,
779         .exit = play_exit,
780         .open = play_open,
781         .close = play_close,
782         .resize = play_resize,
783         .paint = play_paint,
784         .update = play_update,
785         .cmd = play_cmd,
786         .get_title = play_title,
787 };