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>
39 #include <ncurses.h>
41 #define MAX_SONG_LENGTH 512
43 typedef struct
44 {
45 GList **list;
46 GList **dir_list;
47 screen_t *screen;
48 mpdclient_t *c;
49 } completion_callback_data_t;
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, int *highlight, mpd_unused void *data)
94 {
95 static char songname[MAX_SONG_LENGTH];
96 mpd_Song *song;
98 if (playlist == NULL || idx >= playlist_length(playlist))
99 return NULL;
101 song = playlist_get(playlist, idx);
102 if (song->id == current_song_id)
103 *highlight = 1;
105 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
106 return songname;
107 }
109 static void
110 center_playing_item(mpdclient_t *c)
111 {
112 unsigned length = c->playlist.list->len;
113 unsigned offset = lw->selected - lw->start;
114 int idx;
116 if (!c->song || length < lw->rows ||
117 c->status == NULL || IS_STOPPED(c->status->state))
118 return;
120 /* try to center the song that are playing */
121 idx = playlist_get_index(c, c->song);
122 if (idx < 0)
123 return;
125 list_window_center(lw, length, idx);
127 /* make sure the cursor is in the window */
128 lw->selected = lw->start+offset;
129 list_window_check_selected(lw, length);
130 }
132 static void
133 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
134 {
135 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
136 GList **list = tmp->list;
137 mpdclient_t *c = tmp->c;
139 if( *list == NULL ) {
140 /* create completion list */
141 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
142 g_completion_add_items(gcmp, *list);
143 }
144 }
146 static void
147 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
148 GList *items, void *data)
149 {
150 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
151 screen_t *screen = tmp->screen;
153 if (g_list_length(items) >= 1)
154 screen_display_completion_list(screen, items);
155 }
157 int
158 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
159 {
160 gchar *filename;
161 gint error;
162 GCompletion *gcmp;
163 GList *list = NULL;
164 completion_callback_data_t data;
166 if (name == NULL) {
167 /* initialize completion support */
168 gcmp = g_completion_new(NULL);
169 g_completion_set_compare(gcmp, strncmp);
170 data.list = &list;
171 data.dir_list = NULL;
172 data.screen = screen;
173 data.c = c;
174 wrln_completion_callback_data = &data;
175 wrln_pre_completion_callback = save_pre_completion_cb;
176 wrln_post_completion_callback = save_post_completion_cb;
179 /* query the user for a filename */
180 filename = screen_readln(screen->status_window.w,
181 _("Save playlist as: "),
182 defaultname,
183 NULL,
184 gcmp);
186 /* destroy completion support */
187 wrln_completion_callback_data = NULL;
188 wrln_pre_completion_callback = NULL;
189 wrln_post_completion_callback = NULL;
190 g_completion_free(gcmp);
191 list = string_list_free(list);
192 if( filename )
193 filename=g_strstrip(filename);
194 } else
195 filename=g_strdup(name);
197 if (filename == NULL || filename[0] == '\0')
198 return -1;
200 /* send save command to mpd */
201 if ((error = mpdclient_cmd_save_playlist(c, filename))) {
202 gint code = GET_ACK_ERROR_CODE(error);
204 if (code == MPD_ACK_ERROR_EXIST) {
205 char *buf;
206 int key;
208 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
209 filename, YES, NO);
210 key = tolower(screen_getch(screen->status_window.w,
211 buf));
212 g_free(buf);
214 if (key == YES[0]) {
215 if (mpdclient_cmd_delete_playlist(c, filename)) {
216 g_free(filename);
217 return -1;
218 }
220 error = playlist_save(screen, c, filename, NULL);
221 g_free(filename);
222 return error;
223 }
225 screen_status_printf(_("Aborted!"));
226 }
228 g_free(filename);
229 return -1;
230 }
232 /* success */
233 screen_status_printf(_("Saved %s"), filename);
234 g_free(filename);
235 return 0;
236 }
238 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
239 GList **list, mpdclient_t *c)
240 {
241 g_completion_remove_items(gcmp, *list);
242 *list = string_list_remove(*list, dir);
243 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
244 g_completion_add_items(gcmp, *list);
245 *dir_list = g_list_append(*dir_list, g_strdup(dir));
246 }
248 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
249 {
250 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
251 GList **dir_list = tmp->dir_list;
252 GList **list = tmp->list;
253 mpdclient_t *c = tmp->c;
255 if (*list == NULL) {
256 /* create initial list */
257 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
258 g_completion_add_items(gcmp, *list);
259 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
260 string_list_find(*dir_list, line) == NULL) {
261 /* add directory content to list */
262 add_dir(gcmp, line, dir_list, list, c);
263 }
264 }
266 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
267 GList *items, void *data)
268 {
269 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
270 GList **dir_list = tmp->dir_list;
271 GList **list = tmp->list;
272 mpdclient_t *c = tmp->c;
273 screen_t *screen = tmp->screen;
275 if (g_list_length(items) >= 1)
276 screen_display_completion_list(screen, items);
278 if (line && line[0] && line[strlen(line) - 1] == '/' &&
279 string_list_find(*dir_list, line) == NULL) {
280 /* add directory content to list */
281 add_dir(gcmp, line, dir_list, list, c);
282 }
283 }
285 static int
286 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
287 {
288 gchar *path;
289 GCompletion *gcmp;
290 GList *list = NULL;
291 GList *dir_list = NULL;
292 completion_callback_data_t data;
294 /* initialize completion support */
295 gcmp = g_completion_new(NULL);
296 g_completion_set_compare(gcmp, strncmp);
297 data.list = &list;
298 data.dir_list = &dir_list;
299 data.screen = screen;
300 data.c = c;
301 wrln_completion_callback_data = &data;
302 wrln_pre_completion_callback = add_pre_completion_cb;
303 wrln_post_completion_callback = add_post_completion_cb;
305 /* get path */
306 path = screen_readln(screen->status_window.w,
307 _("Add: "),
308 NULL,
309 NULL,
310 gcmp);
312 /* destroy completion data */
313 wrln_completion_callback_data = NULL;
314 wrln_pre_completion_callback = NULL;
315 wrln_post_completion_callback = NULL;
316 g_completion_free(gcmp);
317 string_list_free(list);
318 string_list_free(dir_list);
320 /* add the path to the playlist */
321 if (path && path[0])
322 mpdclient_cmd_add_path(c, path);
324 return 0;
325 }
327 static void
328 play_init(WINDOW *w, int cols, int rows)
329 {
330 lw = list_window_init(w, cols, rows);
331 }
333 static gboolean
334 timer_hide_cursor(gpointer data)
335 {
336 struct mpdclient *c = data;
338 assert(options.hide_cursor > 0);
339 assert(timer_hide_cursor_id != 0);
341 timer_hide_cursor_id = 0;
343 /* hide the cursor when mpd is playing and the user is inactive */
345 if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
346 lw->flags |= LW_HIDE_CURSOR;
347 playlist_repaint();
348 } else
349 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
350 timer_hide_cursor, c);
352 return FALSE;
353 }
355 static void
356 play_open(mpd_unused screen_t *screen, mpdclient_t *c)
357 {
358 static gboolean install_cb = TRUE;
360 playlist = &c->playlist;
362 assert(timer_hide_cursor_id == 0);
363 if (options.hide_cursor > 0) {
364 lw->flags &= ~LW_HIDE_CURSOR;
365 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
366 timer_hide_cursor, c);
367 }
369 if (install_cb) {
370 mpdclient_install_playlist_callback(c, playlist_changed_callback);
371 install_cb = FALSE;
372 }
373 }
375 static void
376 play_close(void)
377 {
378 if (timer_hide_cursor_id != 0) {
379 g_source_remove(timer_hide_cursor_id);
380 timer_hide_cursor_id = 0;
381 }
382 }
384 static void
385 play_resize(int cols, int rows)
386 {
387 lw->cols = cols;
388 lw->rows = rows;
389 }
392 static void
393 play_exit(void)
394 {
395 list_window_free(lw);
396 }
398 static const char *
399 play_title(char *str, size_t size)
400 {
401 if( strcmp(options.host, "localhost") == 0 )
402 return _("Playlist");
404 g_snprintf(str, size, _("Playlist on %s"), options.host);
405 return str;
406 }
408 static void
409 play_paint(void)
410 {
411 list_window_paint(lw, list_callback, NULL);
412 }
414 static void
415 play_update(mpdclient_t *c)
416 {
417 static int prev_song_id = -1;
419 current_song_id = c->song != NULL && c->status != NULL &&
420 !IS_STOPPED(c->status->state) ? c->song->id : -1;
422 if (current_song_id != prev_song_id) {
423 prev_song_id = current_song_id;
425 /* center the cursor */
426 if (options.auto_center && current_song_id != -1)
427 center_playing_item(c);
429 playlist_repaint();
430 }
431 }
433 #ifdef HAVE_GETMOUSE
434 static int
435 handle_mouse_event(struct mpdclient *c)
436 {
437 int row;
438 unsigned selected;
439 unsigned long bstate;
441 if (screen_get_mouse_event(c, &bstate, &row) ||
442 list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
443 playlist_repaint();
444 return 1;
445 }
447 if (bstate & BUTTON1_DOUBLE_CLICKED) {
448 /* stop */
449 screen_cmd(c, CMD_STOP);
450 return 1;
451 }
453 selected = lw->start + row;
455 if (bstate & BUTTON1_CLICKED) {
456 /* play */
457 if (lw->start + row < playlist_length(playlist))
458 mpdclient_cmd_play(c, lw->start + row);
459 } else if (bstate & BUTTON3_CLICKED) {
460 /* delete */
461 if (selected == lw->selected)
462 mpdclient_cmd_delete(c, lw->selected);
463 }
465 lw->selected = selected;
466 list_window_check_selected(lw, playlist_length(playlist));
467 playlist_repaint();
469 return 1;
470 }
471 #endif
473 static int
474 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
475 {
476 lw->flags &= ~LW_HIDE_CURSOR;
478 if (options.hide_cursor > 0) {
479 if (timer_hide_cursor_id != 0)
480 g_source_remove(timer_hide_cursor_id);
481 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
482 timer_hide_cursor, c);
483 }
485 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
486 playlist_repaint();
487 return 1;
488 }
490 switch(cmd) {
491 case CMD_PLAY:
492 mpdclient_cmd_play(c, lw->selected);
493 return 1;
494 case CMD_DELETE:
495 mpdclient_cmd_delete(c, lw->selected);
496 return 1;
497 case CMD_SAVE_PLAYLIST:
498 playlist_save(screen, c, NULL, NULL);
499 return 1;
500 case CMD_ADD:
501 handle_add_to_playlist(screen, c);
502 return 1;
503 case CMD_SCREEN_UPDATE:
504 center_playing_item(c);
505 playlist_repaint();
506 return 0;
508 case CMD_LIST_MOVE_UP:
509 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
510 return 1;
511 case CMD_LIST_MOVE_DOWN:
512 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
513 return 1;
514 case CMD_LIST_FIND:
515 case CMD_LIST_RFIND:
516 case CMD_LIST_FIND_NEXT:
517 case CMD_LIST_RFIND_NEXT:
518 screen_find(screen,
519 lw, playlist_length(&c->playlist),
520 cmd, list_callback, NULL);
521 playlist_repaint();
522 return 1;
524 #ifdef HAVE_GETMOUSE
525 case CMD_MOUSE_EVENT:
526 return handle_mouse_event(c);
527 #endif
529 #ifdef ENABLE_LYRICS_SCREEN
530 case CMD_SCREEN_LYRICS:
531 if (lw->selected < playlist_length(&c->playlist)) {
532 screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
533 return 1;
534 }
536 break;
537 #endif
539 default:
540 break;
541 }
543 return 0;
544 }
546 const struct screen_functions screen_playlist = {
547 .init = play_init,
548 .exit = play_exit,
549 .open = play_open,
550 .close = play_close,
551 .resize = play_resize,
552 .paint = play_paint,
553 .update = play_update,
554 .cmd = play_cmd,
555 .get_title = play_title,
556 };