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 list_window_t *lw = NULL;
52 static guint timer_hide_cursor_id;
54 static void
55 play_paint(struct mpdclient *c);
57 static void
58 playlist_repaint(struct mpdclient *c)
59 {
60 play_paint(c);
61 wrefresh(lw->w);
62 }
64 static void
65 playlist_repaint_if_active(struct mpdclient *c)
66 {
67 if (screen_is_visible(&screen_playlist))
68 playlist_repaint(c);
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(c);
88 }
90 static const char *
91 list_callback(unsigned idx, int *highlight, void *data)
92 {
93 static char songname[MAX_SONG_LENGTH];
94 mpdclient_t *c = (mpdclient_t *) data;
95 mpd_Song *song;
97 if (idx >= playlist_length(&c->playlist))
98 return NULL;
100 song = playlist_get(&c->playlist, idx);
102 if (c->song != NULL && song->id == c->song->id &&
103 c->status != NULL && !IS_STOPPED(c->status->state))
104 *highlight = 1;
106 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
107 return songname;
108 }
110 static void
111 center_playing_item(mpdclient_t *c)
112 {
113 unsigned length = c->playlist.list->len;
114 unsigned offset = lw->selected - lw->start;
115 int idx;
117 if (!c->song || length < lw->rows ||
118 c->status == NULL || IS_STOPPED(c->status->state))
119 return;
121 /* try to center the song that are playing */
122 idx = playlist_get_index(c, c->song);
123 if (idx < 0)
124 return;
126 list_window_center(lw, length, idx);
128 /* make sure the cursor is in the window */
129 lw->selected = lw->start+offset;
130 list_window_check_selected(lw, length);
131 }
133 static void
134 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
135 {
136 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
137 GList **list = tmp->list;
138 mpdclient_t *c = tmp->c;
140 if( *list == NULL ) {
141 /* create completion list */
142 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
143 g_completion_add_items(gcmp, *list);
144 }
145 }
147 static void
148 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
149 GList *items, void *data)
150 {
151 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
152 screen_t *screen = tmp->screen;
154 if (g_list_length(items) >= 1)
155 screen_display_completion_list(screen, items);
156 }
158 int
159 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
160 {
161 gchar *filename;
162 gint error;
163 GCompletion *gcmp;
164 GList *list = NULL;
165 completion_callback_data_t data;
167 if (name == NULL) {
168 /* initialize completion support */
169 gcmp = g_completion_new(NULL);
170 g_completion_set_compare(gcmp, strncmp);
171 data.list = &list;
172 data.dir_list = NULL;
173 data.screen = screen;
174 data.c = c;
175 wrln_completion_callback_data = &data;
176 wrln_pre_completion_callback = save_pre_completion_cb;
177 wrln_post_completion_callback = save_post_completion_cb;
180 /* query the user for a filename */
181 filename = screen_readln(screen->status_window.w,
182 _("Save playlist as: "),
183 defaultname,
184 NULL,
185 gcmp);
187 /* destroy completion support */
188 wrln_completion_callback_data = NULL;
189 wrln_pre_completion_callback = NULL;
190 wrln_post_completion_callback = NULL;
191 g_completion_free(gcmp);
192 list = string_list_free(list);
193 if( filename )
194 filename=g_strstrip(filename);
195 } else
196 filename=g_strdup(name);
198 if (filename == NULL || filename[0] == '\0')
199 return -1;
201 /* send save command to mpd */
202 if ((error = mpdclient_cmd_save_playlist(c, filename))) {
203 gint code = GET_ACK_ERROR_CODE(error);
205 if (code == MPD_ACK_ERROR_EXIST) {
206 char *buf;
207 int key;
209 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
210 filename, YES, NO);
211 key = tolower(screen_getch(screen->status_window.w,
212 buf));
213 g_free(buf);
215 if (key == YES[0]) {
216 if (mpdclient_cmd_delete_playlist(c, filename)) {
217 g_free(filename);
218 return -1;
219 }
221 error = playlist_save(screen, c, filename, NULL);
222 g_free(filename);
223 return error;
224 }
226 screen_status_printf(_("Aborted!"));
227 }
229 g_free(filename);
230 return -1;
231 }
233 /* success */
234 screen_status_printf(_("Saved %s"), filename);
235 g_free(filename);
236 return 0;
237 }
239 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
240 GList **list, mpdclient_t *c)
241 {
242 g_completion_remove_items(gcmp, *list);
243 *list = string_list_remove(*list, dir);
244 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
245 g_completion_add_items(gcmp, *list);
246 *dir_list = g_list_append(*dir_list, g_strdup(dir));
247 }
249 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
250 {
251 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
252 GList **dir_list = tmp->dir_list;
253 GList **list = tmp->list;
254 mpdclient_t *c = tmp->c;
256 if (*list == NULL) {
257 /* create initial list */
258 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
259 g_completion_add_items(gcmp, *list);
260 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
261 string_list_find(*dir_list, line) == NULL) {
262 /* add directory content to list */
263 add_dir(gcmp, line, dir_list, list, c);
264 }
265 }
267 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
268 GList *items, void *data)
269 {
270 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
271 GList **dir_list = tmp->dir_list;
272 GList **list = tmp->list;
273 mpdclient_t *c = tmp->c;
274 screen_t *screen = tmp->screen;
276 if (g_list_length(items) >= 1)
277 screen_display_completion_list(screen, items);
279 if (line && line[0] && line[strlen(line) - 1] == '/' &&
280 string_list_find(*dir_list, line) == NULL) {
281 /* add directory content to list */
282 add_dir(gcmp, line, dir_list, list, c);
283 }
284 }
286 static int
287 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
288 {
289 gchar *path;
290 GCompletion *gcmp;
291 GList *list = NULL;
292 GList *dir_list = NULL;
293 completion_callback_data_t data;
295 /* initialize completion support */
296 gcmp = g_completion_new(NULL);
297 g_completion_set_compare(gcmp, strncmp);
298 data.list = &list;
299 data.dir_list = &dir_list;
300 data.screen = screen;
301 data.c = c;
302 wrln_completion_callback_data = &data;
303 wrln_pre_completion_callback = add_pre_completion_cb;
304 wrln_post_completion_callback = add_post_completion_cb;
306 /* get path */
307 path = screen_readln(screen->status_window.w,
308 _("Add: "),
309 NULL,
310 NULL,
311 gcmp);
313 /* destroy completion data */
314 wrln_completion_callback_data = NULL;
315 wrln_pre_completion_callback = NULL;
316 wrln_post_completion_callback = NULL;
317 g_completion_free(gcmp);
318 string_list_free(list);
319 string_list_free(dir_list);
321 /* add the path to the playlist */
322 if (path && path[0])
323 mpdclient_cmd_add_path(c, path);
325 return 0;
326 }
328 static void
329 play_init(WINDOW *w, int cols, int rows)
330 {
331 lw = list_window_init(w, cols, rows);
332 }
334 static gboolean
335 timer_hide_cursor(gpointer data)
336 {
337 struct mpdclient *c = data;
339 assert(options.hide_cursor > 0);
340 assert(timer_hide_cursor_id != 0);
342 timer_hide_cursor_id = 0;
344 /* hide the cursor when mpd is playing and the user is inactive */
346 if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
347 lw->flags |= LW_HIDE_CURSOR;
348 playlist_repaint(c);
349 } else
350 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
351 timer_hide_cursor, c);
353 return FALSE;
354 }
356 static void
357 play_open(mpd_unused screen_t *screen, mpdclient_t *c)
358 {
359 static gboolean install_cb = TRUE;
361 assert(timer_hide_cursor_id == 0);
362 if (options.hide_cursor > 0) {
363 lw->flags &= ~LW_HIDE_CURSOR;
364 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
365 timer_hide_cursor, c);
366 }
368 if (install_cb) {
369 mpdclient_install_playlist_callback(c, playlist_changed_callback);
370 install_cb = FALSE;
371 }
372 }
374 static void
375 play_close(void)
376 {
377 if (timer_hide_cursor_id != 0) {
378 g_source_remove(timer_hide_cursor_id);
379 timer_hide_cursor_id = 0;
380 }
381 }
383 static void
384 play_resize(int cols, int rows)
385 {
386 lw->cols = cols;
387 lw->rows = rows;
388 }
391 static void
392 play_exit(void)
393 {
394 list_window_free(lw);
395 }
397 static const char *
398 play_title(char *str, size_t size)
399 {
400 if( strcmp(options.host, "localhost") == 0 )
401 return _("Playlist");
403 g_snprintf(str, size, _("Playlist on %s"), options.host);
404 return str;
405 }
407 static void
408 play_paint(mpdclient_t *c)
409 {
410 list_window_paint(lw, list_callback, (void *) c);
411 }
413 static void
414 play_update(mpdclient_t *c)
415 {
416 static int prev_song_id = -1;
417 int current_song_id = c->song != NULL && c->status != NULL &&
418 !IS_STOPPED(c->status->state) ? c->song->id : -1;
420 if (current_song_id != prev_song_id) {
421 prev_song_id = current_song_id;
423 /* center the cursor */
424 if (options.auto_center && current_song_id != -1)
425 center_playing_item(c);
427 playlist_repaint(c);
428 }
429 }
431 #ifdef HAVE_GETMOUSE
432 static int
433 handle_mouse_event(mpd_unused screen_t *screen, mpdclient_t *c)
434 {
435 int row;
436 unsigned selected;
437 unsigned long bstate;
439 if (screen_get_mouse_event(c, &bstate, &row) ||
440 list_window_mouse(lw, c->playlist.list->len, bstate, row)) {
441 playlist_repaint(c);
442 return 1;
443 }
445 if (bstate & BUTTON1_DOUBLE_CLICKED) {
446 /* stop */
447 screen_cmd(c, CMD_STOP);
448 return 1;
449 }
451 selected = lw->start + row;
453 if (bstate & BUTTON1_CLICKED) {
454 /* play */
455 if (lw->start + row < c->playlist.list->len)
456 mpdclient_cmd_play(c, lw->start + row);
457 } else if (bstate & BUTTON3_CLICKED) {
458 /* delete */
459 if (selected == lw->selected)
460 mpdclient_cmd_delete(c, lw->selected);
461 }
463 lw->selected = selected;
464 list_window_check_selected(lw, c->playlist.list->len);
465 playlist_repaint(c);
467 return 1;
468 }
469 #else
470 #define handle_mouse_event(s,c) (0)
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(c);
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(c);
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, c);
521 playlist_repaint(c);
522 return 1;
524 case CMD_MOUSE_EVENT:
525 return handle_mouse_event(screen,c);
527 #ifdef ENABLE_LYRICS_SCREEN
528 case CMD_SCREEN_LYRICS:
529 if (lw->selected < playlist_length(&c->playlist)) {
530 screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
531 return 1;
532 }
534 break;
535 #endif
537 default:
538 break;
539 }
541 return 0;
542 }
544 const struct screen_functions screen_playlist = {
545 .init = play_init,
546 .exit = play_exit,
547 .open = play_open,
548 .close = play_close,
549 .resize = play_resize,
550 .paint = play_paint,
551 .update = play_update,
552 .cmd = play_cmd,
553 .get_title = play_title,
554 };