67de5fa3e62d3d6ce73ec045b6613668a7351e3e
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 "ncmpc.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 D("screen_play.c> playlist_callback() [%d]\n", event);
75 switch (event) {
76 case PLAYLIST_EVENT_DELETE:
77 break;
78 case PLAYLIST_EVENT_MOVE:
79 lw->selected = *((int *) data);
80 if (lw->selected < lw->start)
81 lw->start--;
82 break;
83 default:
84 break;
85 }
87 list_window_check_selected(lw, c->playlist.list->len);
88 playlist_repaint_if_active(c);
89 }
91 static const char *
92 list_callback(unsigned idx, int *highlight, void *data)
93 {
94 static char songname[MAX_SONG_LENGTH];
95 mpdclient_t *c = (mpdclient_t *) data;
96 mpd_Song *song;
98 if (idx >= playlist_length(&c->playlist))
99 return NULL;
101 song = playlist_get(&c->playlist, idx);
103 if (c->song != NULL && song->id == c->song->id &&
104 c->status != NULL && !IS_STOPPED(c->status->state))
105 *highlight = 1;
107 strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
108 return songname;
109 }
111 static void
112 center_playing_item(mpdclient_t *c)
113 {
114 unsigned length = c->playlist.list->len;
115 unsigned offset = lw->selected - lw->start;
116 int idx;
118 if (!c->song || length < lw->rows ||
119 c->status == NULL || IS_STOPPED(c->status->state))
120 return;
122 /* try to center the song that are playing */
123 idx = playlist_get_index(c, c->song);
124 D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,idx);
125 if (idx < 0)
126 return;
128 list_window_center(lw, length, idx);
130 /* make sure the cursor is in the window */
131 lw->selected = lw->start+offset;
132 list_window_check_selected(lw, length);
133 }
135 static void
136 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
137 {
138 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
139 GList **list = tmp->list;
140 mpdclient_t *c = tmp->c;
142 if( *list == NULL ) {
143 /* create completion list */
144 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
145 g_completion_add_items(gcmp, *list);
146 }
147 }
149 static void
150 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
151 GList *items, void *data)
152 {
153 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
154 screen_t *screen = tmp->screen;
156 if (g_list_length(items) >= 1)
157 screen_display_completion_list(screen, items);
158 }
160 int
161 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
162 {
163 gchar *filename;
164 gint error;
165 GCompletion *gcmp;
166 GList *list = NULL;
167 completion_callback_data_t data;
169 if (name == NULL) {
170 /* initialize completion support */
171 gcmp = g_completion_new(NULL);
172 g_completion_set_compare(gcmp, strncmp);
173 data.list = &list;
174 data.dir_list = NULL;
175 data.screen = screen;
176 data.c = c;
177 wrln_completion_callback_data = &data;
178 wrln_pre_completion_callback = save_pre_completion_cb;
179 wrln_post_completion_callback = save_post_completion_cb;
182 /* query the user for a filename */
183 filename = screen_readln(screen->status_window.w,
184 _("Save playlist as: "),
185 defaultname,
186 NULL,
187 gcmp);
189 /* destroy completion support */
190 wrln_completion_callback_data = NULL;
191 wrln_pre_completion_callback = NULL;
192 wrln_post_completion_callback = NULL;
193 g_completion_free(gcmp);
194 list = string_list_free(list);
195 if( filename )
196 filename=g_strstrip(filename);
197 } else
198 filename=g_strdup(name);
200 if (filename == NULL || filename[0] == '\0')
201 return -1;
203 /* send save command to mpd */
204 D("Saving playlist as \'%s \'...\n", filename);
205 if ((error = mpdclient_cmd_save_playlist(c, filename))) {
206 gint code = GET_ACK_ERROR_CODE(error);
208 if (code == MPD_ACK_ERROR_EXIST) {
209 char *buf;
210 int key;
212 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
213 filename, YES, NO);
214 key = tolower(screen_getch(screen->status_window.w,
215 buf));
216 g_free(buf);
218 if (key == YES[0]) {
219 if (mpdclient_cmd_delete_playlist(c, filename)) {
220 g_free(filename);
221 return -1;
222 }
224 error = playlist_save(screen, c, filename, NULL);
225 g_free(filename);
226 return error;
227 }
229 screen_status_printf(_("Aborted!"));
230 }
232 g_free(filename);
233 return -1;
234 }
236 /* success */
237 screen_status_printf(_("Saved %s"), filename);
238 g_free(filename);
239 return 0;
240 }
242 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
243 GList **list, mpdclient_t *c)
244 {
245 g_completion_remove_items(gcmp, *list);
246 *list = string_list_remove(*list, dir);
247 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
248 g_completion_add_items(gcmp, *list);
249 *dir_list = g_list_append(*dir_list, g_strdup(dir));
250 }
252 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
253 {
254 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
255 GList **dir_list = tmp->dir_list;
256 GList **list = tmp->list;
257 mpdclient_t *c = tmp->c;
259 D("pre_completion()...\n");
260 if (*list == NULL) {
261 /* create initial list */
262 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
263 g_completion_add_items(gcmp, *list);
264 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
265 string_list_find(*dir_list, line) == NULL) {
266 /* add directory content to list */
267 add_dir(gcmp, line, dir_list, list, c);
268 }
269 }
271 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
272 GList *items, void *data)
273 {
274 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
275 GList **dir_list = tmp->dir_list;
276 GList **list = tmp->list;
277 mpdclient_t *c = tmp->c;
278 screen_t *screen = tmp->screen;
280 D("post_completion()...\n");
281 if (g_list_length(items) >= 1)
282 screen_display_completion_list(screen, items);
284 if (line && line[0] && line[strlen(line) - 1] == '/' &&
285 string_list_find(*dir_list, line) == NULL) {
286 /* add directory content to list */
287 add_dir(gcmp, line, dir_list, list, c);
288 }
289 }
291 static int
292 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
293 {
294 gchar *path;
295 GCompletion *gcmp;
296 GList *list = NULL;
297 GList *dir_list = NULL;
298 completion_callback_data_t data;
300 /* initialize completion support */
301 gcmp = g_completion_new(NULL);
302 g_completion_set_compare(gcmp, strncmp);
303 data.list = &list;
304 data.dir_list = &dir_list;
305 data.screen = screen;
306 data.c = c;
307 wrln_completion_callback_data = &data;
308 wrln_pre_completion_callback = add_pre_completion_cb;
309 wrln_post_completion_callback = add_post_completion_cb;
311 /* get path */
312 path = screen_readln(screen->status_window.w,
313 _("Add: "),
314 NULL,
315 NULL,
316 gcmp);
318 /* destroy completion data */
319 wrln_completion_callback_data = NULL;
320 wrln_pre_completion_callback = NULL;
321 wrln_post_completion_callback = NULL;
322 g_completion_free(gcmp);
323 string_list_free(list);
324 string_list_free(dir_list);
326 /* add the path to the playlist */
327 if (path && path[0])
328 mpdclient_cmd_add_path(c, path);
330 return 0;
331 }
333 static void
334 play_init(WINDOW *w, int cols, int rows)
335 {
336 lw = list_window_init(w, cols, rows);
337 }
339 static gboolean
340 timer_hide_cursor(gpointer data)
341 {
342 struct mpdclient *c = data;
344 assert(options.hide_cursor > 0);
345 assert(timer_hide_cursor_id != 0);
347 timer_hide_cursor_id = 0;
349 /* hide the cursor when mpd is playing and the user is inactive */
351 if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
352 lw->flags |= LW_HIDE_CURSOR;
353 playlist_repaint(c);
354 } else
355 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
356 timer_hide_cursor, c);
358 return FALSE;
359 }
361 static void
362 play_open(mpd_unused screen_t *screen, mpdclient_t *c)
363 {
364 static gboolean install_cb = TRUE;
366 assert(timer_hide_cursor_id == 0);
367 if (options.hide_cursor > 0) {
368 lw->flags &= ~LW_HIDE_CURSOR;
369 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
370 timer_hide_cursor, c);
371 }
373 if (install_cb) {
374 mpdclient_install_playlist_callback(c, playlist_changed_callback);
375 install_cb = FALSE;
376 }
377 }
379 static void
380 play_close(void)
381 {
382 if (timer_hide_cursor_id != 0) {
383 g_source_remove(timer_hide_cursor_id);
384 timer_hide_cursor_id = 0;
385 }
386 }
388 static void
389 play_resize(int cols, int rows)
390 {
391 lw->cols = cols;
392 lw->rows = rows;
393 }
396 static void
397 play_exit(void)
398 {
399 list_window_free(lw);
400 }
402 static const char *
403 play_title(char *str, size_t size)
404 {
405 if( strcmp(options.host, "localhost") == 0 )
406 return _("Playlist");
408 g_snprintf(str, size, _("Playlist on %s"), options.host);
409 return str;
410 }
412 static void
413 play_paint(mpdclient_t *c)
414 {
415 list_window_paint(lw, list_callback, (void *) c);
416 }
418 static void
419 play_update(mpdclient_t *c)
420 {
421 static int prev_song_id = -1;
422 int current_song_id = c->song != NULL && c->status != NULL &&
423 !IS_STOPPED(c->status->state) ? c->song->id : -1;
425 if (current_song_id != prev_song_id) {
426 prev_song_id = current_song_id;
428 /* center the cursor */
429 if (options.auto_center && current_song_id != -1)
430 center_playing_item(c);
432 playlist_repaint(c);
433 }
434 }
436 #ifdef HAVE_GETMOUSE
437 static int
438 handle_mouse_event(mpd_unused screen_t *screen, mpdclient_t *c)
439 {
440 int row;
441 unsigned selected;
442 unsigned long bstate;
444 if (screen_get_mouse_event(c, &bstate, &row) ||
445 list_window_mouse(lw, c->playlist.list->len, bstate, row)) {
446 playlist_repaint(c);
447 return 1;
448 }
450 if (bstate & BUTTON1_DOUBLE_CLICKED) {
451 /* stop */
452 screen_cmd(c, CMD_STOP);
453 return 1;
454 }
456 selected = lw->start + row;
458 if (bstate & BUTTON1_CLICKED) {
459 /* play */
460 if (lw->start + row < c->playlist.list->len)
461 mpdclient_cmd_play(c, lw->start + row);
462 } else if (bstate & BUTTON3_CLICKED) {
463 /* delete */
464 if (selected == lw->selected)
465 mpdclient_cmd_delete(c, lw->selected);
466 }
468 lw->selected = selected;
469 list_window_check_selected(lw, c->playlist.list->len);
470 playlist_repaint(c);
472 return 1;
473 }
474 #else
475 #define handle_mouse_event(s,c) (0)
476 #endif
478 static int
479 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
480 {
481 lw->flags &= ~LW_HIDE_CURSOR;
483 if (options.hide_cursor > 0) {
484 if (timer_hide_cursor_id != 0)
485 g_source_remove(timer_hide_cursor_id);
486 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
487 timer_hide_cursor, c);
488 }
490 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
491 playlist_repaint(c);
492 return 1;
493 }
495 switch(cmd) {
496 case CMD_PLAY:
497 mpdclient_cmd_play(c, lw->selected);
498 return 1;
499 case CMD_DELETE:
500 mpdclient_cmd_delete(c, lw->selected);
501 return 1;
502 case CMD_SAVE_PLAYLIST:
503 playlist_save(screen, c, NULL, NULL);
504 return 1;
505 case CMD_ADD:
506 handle_add_to_playlist(screen, c);
507 return 1;
508 case CMD_SCREEN_UPDATE:
509 center_playing_item(c);
510 playlist_repaint(c);
511 return 0;
513 case CMD_LIST_MOVE_UP:
514 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
515 return 1;
516 case CMD_LIST_MOVE_DOWN:
517 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
518 return 1;
519 case CMD_LIST_FIND:
520 case CMD_LIST_RFIND:
521 case CMD_LIST_FIND_NEXT:
522 case CMD_LIST_RFIND_NEXT:
523 screen_find(screen,
524 lw, playlist_length(&c->playlist),
525 cmd, list_callback, c);
526 playlist_repaint(c);
527 return 1;
529 case CMD_MOUSE_EVENT:
530 return handle_mouse_event(screen,c);
532 #ifdef ENABLE_LYRICS_SCREEN
533 case CMD_SCREEN_LYRICS:
534 if (lw->selected < playlist_length(&c->playlist)) {
535 screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
536 return 1;
537 }
539 break;
540 #endif
542 default:
543 break;
544 }
546 return 0;
547 }
549 const struct screen_functions screen_playlist = {
550 .init = play_init,
551 .exit = play_exit,
552 .open = play_open,
553 .close = play_close,
554 .resize = play_resize,
555 .paint = play_paint,
556 .update = play_update,
557 .cmd = play_cmd,
558 .get_title = play_title,
559 };