Code

90b06aa0eb730d3ef2ee8749a681e7e6dbd05db5
[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 "options.h"
22 #include "support.h"
23 #include "mpdclient.h"
24 #include "utils.h"
25 #include "strfsong.h"
26 #include "wreadln.h"
27 #include "command.h"
28 #include "colors.h"
29 #include "screen.h"
30 #include "screen_utils.h"
31 #include "screen_play.h"
32 #include "gcc.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 typedef struct
43 {
44         GList **list;
45         GList **dir_list;
46         mpdclient_t *c;
47 } completion_callback_data_t;
49 static struct mpdclient_playlist *playlist;
50 static int current_song_id = -1;
51 static list_window_t *lw = NULL;
52 static guint timer_hide_cursor_id;
54 static void
55 play_paint(void);
57 static void
58 playlist_repaint(void)
59 {
60         play_paint();
61         wrefresh(lw->w);
62 }
64 static void
65 playlist_repaint_if_active(void)
66 {
67         if (screen_is_visible(&screen_playlist))
68                 playlist_repaint();
69 }
71 static void
72 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
73 {
74         switch (event) {
75         case PLAYLIST_EVENT_DELETE:
76                 break;
77         case PLAYLIST_EVENT_MOVE:
78                 lw->selected = *((int *) data);
79                 if (lw->selected < lw->start)
80                         lw->start--;
81                 break;
82         default:
83                 break;
84         }
86         list_window_check_selected(lw, c->playlist.list->len);
87         playlist_repaint_if_active();
88 }
90 static const char *
91 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
92 {
93         static char songname[MAX_SONG_LENGTH];
94         mpd_Song *song;
96         if (playlist == NULL || idx >= playlist_length(playlist))
97                 return NULL;
99         song = playlist_get(playlist, idx);
100         if (song->id == current_song_id)
101                 *highlight = 1;
103         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
104         return songname;
107 static void
108 center_playing_item(mpdclient_t *c)
110         unsigned length = c->playlist.list->len;
111         unsigned offset = lw->selected - lw->start;
112         int idx;
114         if (!c->song || length < lw->rows ||
115             c->status == NULL || IS_STOPPED(c->status->state))
116                 return;
118         /* try to center the song that are playing */
119         idx = playlist_get_index(c, c->song);
120         if (idx < 0)
121                 return;
123         list_window_center(lw, length, idx);
125         /* make sure the cursor is in the window */
126         lw->selected = lw->start+offset;
127         list_window_check_selected(lw, length);
130 static void
131 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
133         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
134         GList **list = tmp->list;
135         mpdclient_t *c = tmp->c;
137         if( *list == NULL ) {
138                 /* create completion list */
139                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
140                 g_completion_add_items(gcmp, *list);
141         }
144 static void
145 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
146                         GList *items, mpd_unused void *data)
148         if (g_list_length(items) >= 1)
149                 screen_display_completion_list(items);
152 int
153 playlist_save(mpdclient_t *c, char *name, char *defaultname)
155         gchar *filename;
156         gint error;
157         GCompletion *gcmp;
158         GList *list = NULL;
159         completion_callback_data_t data;
161         if (name == NULL) {
162                 /* initialize completion support */
163                 gcmp = g_completion_new(NULL);
164                 g_completion_set_compare(gcmp, strncmp);
165                 data.list = &list;
166                 data.dir_list = NULL;
167                 data.c = c;
168                 wrln_completion_callback_data = &data;
169                 wrln_pre_completion_callback = save_pre_completion_cb;
170                 wrln_post_completion_callback = save_post_completion_cb;
173                 /* query the user for a filename */
174                 filename = screen_readln(screen.status_window.w,
175                                          _("Save playlist as: "),
176                                          defaultname,
177                                          NULL,
178                                          gcmp);
180                 /* destroy completion support */
181                 wrln_completion_callback_data = NULL;
182                 wrln_pre_completion_callback = NULL;
183                 wrln_post_completion_callback = NULL;
184                 g_completion_free(gcmp);
185                 list = string_list_free(list);
186                 if( filename )
187                         filename=g_strstrip(filename);
188         } else
189                         filename=g_strdup(name);
191         if (filename == NULL || filename[0] == '\0')
192                 return -1;
194         /* send save command to mpd */
195         if ((error = mpdclient_cmd_save_playlist(c, filename))) {
196                 gint code = GET_ACK_ERROR_CODE(error);
198                 if (code == MPD_ACK_ERROR_EXIST) {
199                         char *buf;
200                         int key;
202                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
203                                               filename, YES, NO);
204                         key = tolower(screen_getch(screen.status_window.w,
205                                                    buf));
206                         g_free(buf);
208                         if (key == YES[0]) {
209                                 if (mpdclient_cmd_delete_playlist(c, filename)) {
210                                         g_free(filename);
211                                         return -1;
212                                 }
214                                 error = playlist_save(c, filename, NULL);
215                                 g_free(filename);
216                                 return error;
217                         }
219                         screen_status_printf(_("Aborted!"));
220                 }
222                 g_free(filename);
223                 return -1;
224         }
226         /* success */
227         screen_status_printf(_("Saved %s"), filename);
228         g_free(filename);
229         return 0;
232 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
233                     GList **list, mpdclient_t *c)
235         g_completion_remove_items(gcmp, *list);
236         *list = string_list_remove(*list, dir);
237         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
238         g_completion_add_items(gcmp, *list);
239         *dir_list = g_list_append(*dir_list, g_strdup(dir));
242 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
244         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
245         GList **dir_list = tmp->dir_list;
246         GList **list = tmp->list;
247         mpdclient_t *c = tmp->c;
249         if (*list == NULL) {
250                 /* create initial list */
251                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
252                 g_completion_add_items(gcmp, *list);
253         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
254                    string_list_find(*dir_list, line) == NULL) {
255                 /* add directory content to list */
256                 add_dir(gcmp, line, dir_list, list, c);
257         }
260 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
261                                    GList *items, void *data)
263         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
264         GList **dir_list = tmp->dir_list;
265         GList **list = tmp->list;
266         mpdclient_t *c = tmp->c;
268         if (g_list_length(items) >= 1)
269                 screen_display_completion_list(items);
271         if (line && line[0] && line[strlen(line) - 1] == '/' &&
272             string_list_find(*dir_list, line) == NULL) {
273                 /* add directory content to list */
274                 add_dir(gcmp, line, dir_list, list, c);
275         }
278 static int
279 handle_add_to_playlist(mpdclient_t *c)
281         gchar *path;
282         GCompletion *gcmp;
283         GList *list = NULL;
284         GList *dir_list = NULL;
285         completion_callback_data_t data;
287         /* initialize completion support */
288         gcmp = g_completion_new(NULL);
289         g_completion_set_compare(gcmp, strncmp);
290         data.list = &list;
291         data.dir_list = &dir_list;
292         data.c = c;
293         wrln_completion_callback_data = &data;
294         wrln_pre_completion_callback = add_pre_completion_cb;
295         wrln_post_completion_callback = add_post_completion_cb;
297         /* get path */
298         path = screen_readln(screen.status_window.w,
299                              _("Add: "),
300                              NULL,
301                              NULL,
302                              gcmp);
304         /* destroy completion data */
305         wrln_completion_callback_data = NULL;
306         wrln_pre_completion_callback = NULL;
307         wrln_post_completion_callback = NULL;
308         g_completion_free(gcmp);
309         string_list_free(list);
310         string_list_free(dir_list);
312         /* add the path to the playlist */
313         if (path && path[0])
314                 mpdclient_cmd_add_path(c, path);
316         return 0;
319 static void
320 play_init(WINDOW *w, int cols, int rows)
322         lw = list_window_init(w, cols, rows);
325 static gboolean
326 timer_hide_cursor(gpointer data)
328         struct mpdclient *c = data;
330         assert(options.hide_cursor > 0);
331         assert(timer_hide_cursor_id != 0);
333         timer_hide_cursor_id = 0;
335         /* hide the cursor when mpd is playing and the user is inactive */
337         if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
338                 lw->flags |= LW_HIDE_CURSOR;
339                 playlist_repaint();
340         } else
341                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
342                                                      timer_hide_cursor, c);
344         return FALSE;
347 static void
348 play_open(mpdclient_t *c)
350         static gboolean install_cb = TRUE;
352         playlist = &c->playlist;
354         assert(timer_hide_cursor_id == 0);
355         if (options.hide_cursor > 0) {
356                 lw->flags &= ~LW_HIDE_CURSOR;
357                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
358                                                      timer_hide_cursor, c);
359         }
361         if (install_cb) {
362                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
363                 install_cb = FALSE;
364         }
367 static void
368 play_close(void)
370         if (timer_hide_cursor_id != 0) {
371                 g_source_remove(timer_hide_cursor_id);
372                 timer_hide_cursor_id = 0;
373         }
376 static void
377 play_resize(int cols, int rows)
379         lw->cols = cols;
380         lw->rows = rows;
384 static void
385 play_exit(void)
387         list_window_free(lw);
390 static const char *
391 play_title(char *str, size_t size)
393         if( strcmp(options.host, "localhost") == 0 )
394                 return _("Playlist");
396         g_snprintf(str, size, _("Playlist on %s"), options.host);
397         return str;
400 static void
401 play_paint(void)
403         list_window_paint(lw, list_callback, NULL);
406 static void
407 play_update(mpdclient_t *c)
409         static int prev_song_id = -1;
411         current_song_id = c->song != NULL && c->status != NULL &&
412                 !IS_STOPPED(c->status->state) ? c->song->id : -1;
414         if (current_song_id != prev_song_id) {
415                 prev_song_id = current_song_id;
417                 /* center the cursor */
418                 if (options.auto_center && current_song_id != -1)
419                         center_playing_item(c);
421                 playlist_repaint();
422         }
425 #ifdef HAVE_GETMOUSE
426 static int
427 handle_mouse_event(struct mpdclient *c)
429         int row;
430         unsigned selected;
431         unsigned long bstate;
433         if (screen_get_mouse_event(c, &bstate, &row) ||
434             list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
435                 playlist_repaint();
436                 return 1;
437         }
439         if (bstate & BUTTON1_DOUBLE_CLICKED) {
440                 /* stop */
441                 screen_cmd(c, CMD_STOP);
442                 return 1;
443         }
445         selected = lw->start + row;
447         if (bstate & BUTTON1_CLICKED) {
448                 /* play */
449                 if (lw->start + row < playlist_length(playlist))
450                         mpdclient_cmd_play(c, lw->start + row);
451         } else if (bstate & BUTTON3_CLICKED) {
452                 /* delete */
453                 if (selected == lw->selected)
454                         mpdclient_cmd_delete(c, lw->selected);
455         }
457         lw->selected = selected;
458         list_window_check_selected(lw, playlist_length(playlist));
459         playlist_repaint();
461         return 1;
463 #endif
465 static int
466 play_cmd(mpdclient_t *c, command_t cmd)
468         lw->flags &= ~LW_HIDE_CURSOR;
470         if (options.hide_cursor > 0) {
471                 if (timer_hide_cursor_id != 0)
472                         g_source_remove(timer_hide_cursor_id);
473                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
474                                                      timer_hide_cursor, c);
475         }
477         if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
478                 playlist_repaint();
479                 return 1;
480         }
482         switch(cmd) {
483         case CMD_PLAY:
484                 mpdclient_cmd_play(c, lw->selected);
485                 return 1;
486         case CMD_DELETE:
487                 mpdclient_cmd_delete(c, lw->selected);
488                 return 1;
489         case CMD_SAVE_PLAYLIST:
490                 playlist_save(c, NULL, NULL);
491                 return 1;
492         case CMD_ADD:
493                 handle_add_to_playlist(c);
494                 return 1;
495         case CMD_SCREEN_UPDATE:
496                 center_playing_item(c);
497                 playlist_repaint();
498                 return 0;
500         case CMD_LIST_MOVE_UP:
501                 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
502                 return 1;
503         case CMD_LIST_MOVE_DOWN:
504                 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
505                 return 1;
506         case CMD_LIST_FIND:
507         case CMD_LIST_RFIND:
508         case CMD_LIST_FIND_NEXT:
509         case CMD_LIST_RFIND_NEXT:
510                 screen_find(lw, playlist_length(&c->playlist),
511                             cmd, list_callback, NULL);
512                 playlist_repaint();
513                 return 1;
515 #ifdef HAVE_GETMOUSE
516         case CMD_MOUSE_EVENT:
517                 return handle_mouse_event(c);
518 #endif
520 #ifdef ENABLE_LYRICS_SCREEN
521         case CMD_SCREEN_LYRICS:
522                 if (lw->selected < playlist_length(&c->playlist)) {
523                         screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
524                         return 1;
525                 }
527                 break;
528 #endif
530         default:
531                 break;
532         }
534         return 0;
537 const struct screen_functions screen_playlist = {
538         .init = play_init,
539         .exit = play_exit,
540         .open = play_open,
541         .close = play_close,
542         .resize = play_resize,
543         .paint = play_paint,
544         .update = play_update,
545         .cmd = play_cmd,
546         .get_title = play_title,
547 };