Code

f8f65ba6255a9b1a8e1c4b446e5eb15a4e66ad9c
[ncmpc.git] / src / screen_play.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "config.h"
20 #include "i18n.h"
21 #include "charset.h"
22 #include "options.h"
23 #include "hscroll.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 #include <ctype.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <glib.h>
40 #define MAX_SONG_LENGTH 512
42 #ifndef NCMPC_MINI
43 typedef struct
44 {
45         GList **list;
46         GList **dir_list;
47         mpdclient_t *c;
48 } completion_callback_data_t;
49 #endif
51 static struct mpdclient_playlist *playlist;
52 static int current_song_id = -1;
53 static list_window_t *lw = NULL;
54 static guint timer_hide_cursor_id;
56 static void
57 play_paint(void);
59 static void
60 playlist_repaint(void)
61 {
62         play_paint();
63         wrefresh(lw->w);
64 }
66 static void
67 playlist_repaint_if_active(void)
68 {
69         if (screen_is_visible(&screen_playlist))
70                 playlist_repaint();
71 }
73 static void
74 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
75 {
76         switch (event) {
77         case PLAYLIST_EVENT_DELETE:
78                 break;
79         case PLAYLIST_EVENT_MOVE:
80                 lw->selected = *((int *) data);
81                 if (lw->selected < lw->start)
82                         lw->start--;
83                 break;
84         default:
85                 break;
86         }
88         list_window_check_selected(lw, c->playlist.list->len);
89         playlist_repaint_if_active();
90 }
92 static const char *
93 list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED void *data)
94 {
95         static char songname[MAX_SONG_LENGTH];
96 #ifndef NCMPC_MINI
97         static scroll_state_t st;
98 #endif
99         mpd_Song *song;
101         if (playlist == NULL || idx >= playlist_length(playlist))
102                 return NULL;
104         song = playlist_get(playlist, idx);
105         if (song->id == current_song_id)
106                 *highlight = true;
108         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
110 #ifndef NCMPC_MINI
111         if (options.scroll && (unsigned)song->pos == lw->selected &&
112             utf8_width(songname) > (unsigned)COLS) {
113                 static unsigned current_song;
114                 char *tmp;
116                 if (current_song != lw->selected) {
117                         st.offset = 0;
118                         current_song = lw->selected;
119                 }
121                 tmp = strscroll(songname, options.scroll_sep,
122                                 MAX_SONG_LENGTH, &st);
123                 g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
124                 g_free(tmp);
125         } else if ((unsigned)song->pos == lw->selected)
126                 st.offset = 0;
127 #endif
129         return songname;
132 static void
133 center_playing_item(mpdclient_t *c)
135         unsigned length = c->playlist.list->len;
136         unsigned offset = lw->selected - lw->start;
137         int idx;
139         if (!c->song || length < lw->rows ||
140             c->status == NULL || IS_STOPPED(c->status->state))
141                 return;
143         /* try to center the song that are playing */
144         idx = playlist_get_index(c, c->song);
145         if (idx < 0)
146                 return;
148         list_window_center(lw, length, idx);
150         /* make sure the cursor is in the window */
151         lw->selected = lw->start+offset;
152         list_window_check_selected(lw, length);
155 #ifndef NCMPC_MINI
156 static void
157 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
158                        void *data)
160         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
161         GList **list = tmp->list;
162         mpdclient_t *c = tmp->c;
164         if( *list == NULL ) {
165                 /* create completion list */
166                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
167                 g_completion_add_items(gcmp, *list);
168         }
171 static void
172 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
173                         G_GNUC_UNUSED gchar *line, GList *items,
174                         G_GNUC_UNUSED void *data)
176         if (g_list_length(items) >= 1)
177                 screen_display_completion_list(items);
179 #endif
181 #ifndef NCMPC_MINI
182 /**
183  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
184  * g_completion_set_compare(), because strncmp() takes size_t where
185  * g_completion_set_compare passes a gsize value.
186  */
187 static gint
188 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
190         return strncmp(s1, s2, n);
192 #endif
194 int
195 playlist_save(mpdclient_t *c, char *name, char *defaultname)
197         gchar *filename, *filename_utf8;
198         gint error;
199 #ifndef NCMPC_MINI
200         GCompletion *gcmp;
201         GList *list = NULL;
202         completion_callback_data_t data;
203 #endif
205 #ifdef NCMPC_MINI
206         (void)defaultname;
207 #endif
209 #ifndef NCMPC_MINI
210         if (name == NULL) {
211                 /* initialize completion support */
212                 gcmp = g_completion_new(NULL);
213                 g_completion_set_compare(gcmp, completion_strncmp);
214                 data.list = &list;
215                 data.dir_list = NULL;
216                 data.c = c;
217                 wrln_completion_callback_data = &data;
218                 wrln_pre_completion_callback = save_pre_completion_cb;
219                 wrln_post_completion_callback = save_post_completion_cb;
222                 /* query the user for a filename */
223                 filename = screen_readln(screen.status_window.w,
224                                          _("Save playlist as: "),
225                                          defaultname,
226                                          NULL,
227                                          gcmp);
229                 /* destroy completion support */
230                 wrln_completion_callback_data = NULL;
231                 wrln_pre_completion_callback = NULL;
232                 wrln_post_completion_callback = NULL;
233                 g_completion_free(gcmp);
234                 list = string_list_free(list);
235                 if( filename )
236                         filename=g_strstrip(filename);
237         } else
238 #endif
239                         filename=g_strdup(name);
241         if (filename == NULL)
242                 return -1;
244         /* send save command to mpd */
246         filename_utf8 = locale_to_utf8(filename);
247         error = mpdclient_cmd_save_playlist(c, filename_utf8);
248         g_free(filename_utf8);
250         if (error) {
251                 gint code = GET_ACK_ERROR_CODE(error);
253                 if (code == MPD_ACK_ERROR_EXIST) {
254                         char *buf;
255                         int key;
257                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
258                                               filename, YES, NO);
259                         key = tolower(screen_getch(screen.status_window.w,
260                                                    buf));
261                         g_free(buf);
263                         if (key == YES[0]) {
264                                 filename_utf8 = locale_to_utf8(filename);
265                                 error = mpdclient_cmd_delete_playlist(c, filename_utf8);
266                                 g_free(filename_utf8);
268                                 if (error) {
269                                         g_free(filename);
270                                         return -1;
271                                 }
273                                 error = playlist_save(c, filename, NULL);
274                                 g_free(filename);
275                                 return error;
276                         }
278                         screen_status_printf(_("Aborted"));
279                 }
281                 g_free(filename);
282                 return -1;
283         }
285         /* success */
286         screen_status_printf(_("Saved %s"), filename);
287         g_free(filename);
288         return 0;
291 #ifndef NCMPC_MINI
292 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
293                     GList **list, mpdclient_t *c)
295         g_completion_remove_items(gcmp, *list);
296         *list = string_list_remove(*list, dir);
297         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
298         g_completion_add_items(gcmp, *list);
299         *dir_list = g_list_append(*dir_list, g_strdup(dir));
302 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
304         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
305         GList **dir_list = tmp->dir_list;
306         GList **list = tmp->list;
307         mpdclient_t *c = tmp->c;
309         if (*list == NULL) {
310                 /* create initial list */
311                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
312                 g_completion_add_items(gcmp, *list);
313         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
314                    string_list_find(*dir_list, line) == NULL) {
315                 /* add directory content to list */
316                 add_dir(gcmp, line, dir_list, list, c);
317         }
320 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
321                                    GList *items, void *data)
323         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
324         GList **dir_list = tmp->dir_list;
325         GList **list = tmp->list;
326         mpdclient_t *c = tmp->c;
328         if (g_list_length(items) >= 1)
329                 screen_display_completion_list(items);
331         if (line && line[0] && line[strlen(line) - 1] == '/' &&
332             string_list_find(*dir_list, line) == NULL) {
333                 /* add directory content to list */
334                 add_dir(gcmp, line, dir_list, list, c);
335         }
337 #endif
339 static int
340 handle_add_to_playlist(mpdclient_t *c)
342         gchar *path;
343 #ifndef NCMPC_MINI
344         GCompletion *gcmp;
345         GList *list = NULL;
346         GList *dir_list = NULL;
347         completion_callback_data_t data;
349         /* initialize completion support */
350         gcmp = g_completion_new(NULL);
351         g_completion_set_compare(gcmp, completion_strncmp);
352         data.list = &list;
353         data.dir_list = &dir_list;
354         data.c = c;
355         wrln_completion_callback_data = &data;
356         wrln_pre_completion_callback = add_pre_completion_cb;
357         wrln_post_completion_callback = add_post_completion_cb;
358 #endif
360         /* get path */
361         path = screen_readln(screen.status_window.w,
362                              _("Add: "),
363                              NULL,
364                              NULL,
365 #ifdef NCMPC_MINI
366                              NULL
367 #else
368                              gcmp
369 #endif
370                              );
372         /* destroy completion data */
373 #ifndef NCMPC_MINI
374         wrln_completion_callback_data = NULL;
375         wrln_pre_completion_callback = NULL;
376         wrln_post_completion_callback = NULL;
377         g_completion_free(gcmp);
378         string_list_free(list);
379         string_list_free(dir_list);
380 #endif
382         /* add the path to the playlist */
383         if (path != NULL) {
384                 char *path_utf8 = locale_to_utf8(path);
385                 mpdclient_cmd_add_path(c, path_utf8);
386                 g_free(path_utf8);
387         }
389         g_free(path);
390         return 0;
393 static void
394 play_init(WINDOW *w, int cols, int rows)
396         lw = list_window_init(w, cols, rows);
399 static gboolean
400 timer_hide_cursor(gpointer data)
402         struct mpdclient *c = data;
404         assert(options.hide_cursor > 0);
405         assert(timer_hide_cursor_id != 0);
407         timer_hide_cursor_id = 0;
409         /* hide the cursor when mpd is playing and the user is inactive */
411         if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
412                 lw->hide_cursor = true;
413                 playlist_repaint();
414         } else
415                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
416                                                      timer_hide_cursor, c);
418         return FALSE;
421 static void
422 play_open(mpdclient_t *c)
424         static gboolean install_cb = TRUE;
426         playlist = &c->playlist;
428         assert(timer_hide_cursor_id == 0);
429         if (options.hide_cursor > 0) {
430                 lw->hide_cursor = false;
431                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
432                                                      timer_hide_cursor, c);
433         }
435         if (install_cb) {
436                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
437                 install_cb = FALSE;
438         }
441 static void
442 play_close(void)
444         if (timer_hide_cursor_id != 0) {
445                 g_source_remove(timer_hide_cursor_id);
446                 timer_hide_cursor_id = 0;
447         }
450 static void
451 play_resize(int cols, int rows)
453         lw->cols = cols;
454         lw->rows = rows;
458 static void
459 play_exit(void)
461         list_window_free(lw);
464 static const char *
465 play_title(char *str, size_t size)
467         if( strcmp(options.host, "localhost") == 0 )
468                 return _("Playlist");
470         g_snprintf(str, size, _("Playlist on %s"), options.host);
471         return str;
474 static void
475 play_paint(void)
477         list_window_paint(lw, list_callback, NULL);
480 static void
481 play_update(mpdclient_t *c)
483         static int prev_song_id = -1;
485         current_song_id = c->song != NULL && c->status != NULL &&
486                 !IS_STOPPED(c->status->state) ? c->song->id : -1;
488         if (current_song_id != prev_song_id) {
489                 prev_song_id = current_song_id;
491                 /* center the cursor */
492                 if (options.auto_center && current_song_id != -1)
493                         center_playing_item(c);
495                 playlist_repaint();
496 #ifndef NCMPC_MINI
497         } else if (options.scroll) {
498                 /* always repaint if horizontal scrolling is
499                    enabled */
500                 playlist_repaint();
501 #endif
502         }
505 #ifdef HAVE_GETMOUSE
506 static bool
507 handle_mouse_event(struct mpdclient *c)
509         int row;
510         unsigned selected;
511         unsigned long bstate;
513         if (screen_get_mouse_event(c, &bstate, &row) ||
514             list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
515                 playlist_repaint();
516                 return true;
517         }
519         if (bstate & BUTTON1_DOUBLE_CLICKED) {
520                 /* stop */
521                 screen_cmd(c, CMD_STOP);
522                 return true;
523         }
525         selected = lw->start + row;
527         if (bstate & BUTTON1_CLICKED) {
528                 /* play */
529                 if (lw->start + row < playlist_length(playlist))
530                         mpdclient_cmd_play(c, lw->start + row);
531         } else if (bstate & BUTTON3_CLICKED) {
532                 /* delete */
533                 if (selected == lw->selected)
534                         mpdclient_cmd_delete(c, lw->selected);
535         }
537         lw->selected = selected;
538         list_window_check_selected(lw, playlist_length(playlist));
539         playlist_repaint();
541         return true;
543 #endif
545 static bool
546 play_cmd(mpdclient_t *c, command_t cmd)
548         lw->hide_cursor = false;
550         if (options.hide_cursor > 0) {
551                 if (timer_hide_cursor_id != 0)
552                         g_source_remove(timer_hide_cursor_id);
553                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
554                                                      timer_hide_cursor, c);
555         }
557         if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
558                 playlist_repaint();
559                 return true;
560         }
562         switch(cmd) {
563         case CMD_PLAY:
564                 mpdclient_cmd_play(c, lw->selected);
565                 return true;
566         case CMD_DELETE:
567                 mpdclient_cmd_delete(c, lw->selected);
568                 return true;
569         case CMD_SAVE_PLAYLIST:
570                 playlist_save(c, NULL, NULL);
571                 return true;
572         case CMD_ADD:
573                 handle_add_to_playlist(c);
574                 return true;
575         case CMD_SCREEN_UPDATE:
576                 center_playing_item(c);
577                 playlist_repaint();
578                 return false;
580         case CMD_LIST_MOVE_UP:
581                 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
582                 return true;
583         case CMD_LIST_MOVE_DOWN:
584                 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
585                 return true;
586         case CMD_LIST_FIND:
587         case CMD_LIST_RFIND:
588         case CMD_LIST_FIND_NEXT:
589         case CMD_LIST_RFIND_NEXT:
590                 screen_find(lw, playlist_length(&c->playlist),
591                             cmd, list_callback, NULL);
592                 playlist_repaint();
593                 return true;
595 #ifdef HAVE_GETMOUSE
596         case CMD_MOUSE_EVENT:
597                 return handle_mouse_event(c);
598 #endif
600 #ifdef ENABLE_SONG_SCREEN
601         case CMD_VIEW:
602                 if (lw->selected < playlist_length(&c->playlist)) {
603                         screen_song_switch(c, playlist_get(&c->playlist, lw->selected));
604                         return true;
605                 }
607                 break;
608 #endif
610         case CMD_LOCATE:
611                 if (lw->selected < playlist_length(&c->playlist)) {
612                         screen_file_goto_song(c, playlist_get(&c->playlist, lw->selected));
613                         return true;
614                 }
616                 break;
618 #ifdef ENABLE_LYRICS_SCREEN
619         case CMD_SCREEN_LYRICS:
620                 if (lw->selected < playlist_length(&c->playlist)) {
621                         screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
622                         return true;
623                 }
625                 break;
626 #endif
628         default:
629                 break;
630         }
632         return false;
635 const struct screen_functions screen_playlist = {
636         .init = play_init,
637         .exit = play_exit,
638         .open = play_open,
639         .close = play_close,
640         .resize = play_resize,
641         .paint = play_paint,
642         .update = play_update,
643         .cmd = play_cmd,
644         .get_title = play_title,
645 };