309b9a54ed656c7ebf7b80b2b6a8bcfddcdaf06e
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 "i18n.h"
22 #include "options.h"
23 #include "support.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"
33 #include "gcc.h"
35 #include <ctype.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <glib.h>
40 #include <ncurses.h>
42 #define MAX_SONG_LENGTH 512
44 typedef struct
45 {
46 GList **list;
47 GList **dir_list;
48 screen_t *screen;
49 mpdclient_t *c;
50 } completion_callback_data_t;
52 static list_window_t *lw = NULL;
53 static guint timer_hide_cursor_id;
55 static void
56 play_paint(struct mpdclient *c);
58 static void
59 playlist_repaint(struct mpdclient *c)
60 {
61 play_paint(c);
62 wrefresh(lw->w);
63 }
65 static void
66 playlist_repaint_if_active(struct mpdclient *c)
67 {
68 if (screen_is_visible(&screen_playlist))
69 playlist_repaint(c);
70 }
72 static void
73 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
74 {
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 if (idx < 0)
125 return;
127 list_window_center(lw, length, idx);
129 /* make sure the cursor is in the window */
130 lw->selected = lw->start+offset;
131 list_window_check_selected(lw, length);
132 }
134 static void
135 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
136 {
137 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
138 GList **list = tmp->list;
139 mpdclient_t *c = tmp->c;
141 if( *list == NULL ) {
142 /* create completion list */
143 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
144 g_completion_add_items(gcmp, *list);
145 }
146 }
148 static void
149 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
150 GList *items, void *data)
151 {
152 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
153 screen_t *screen = tmp->screen;
155 if (g_list_length(items) >= 1)
156 screen_display_completion_list(screen, items);
157 }
159 int
160 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
161 {
162 gchar *filename;
163 gint error;
164 GCompletion *gcmp;
165 GList *list = NULL;
166 completion_callback_data_t data;
168 if (name == NULL) {
169 /* initialize completion support */
170 gcmp = g_completion_new(NULL);
171 g_completion_set_compare(gcmp, strncmp);
172 data.list = &list;
173 data.dir_list = NULL;
174 data.screen = screen;
175 data.c = c;
176 wrln_completion_callback_data = &data;
177 wrln_pre_completion_callback = save_pre_completion_cb;
178 wrln_post_completion_callback = save_post_completion_cb;
181 /* query the user for a filename */
182 filename = screen_readln(screen->status_window.w,
183 _("Save playlist as: "),
184 defaultname,
185 NULL,
186 gcmp);
188 /* destroy completion support */
189 wrln_completion_callback_data = NULL;
190 wrln_pre_completion_callback = NULL;
191 wrln_post_completion_callback = NULL;
192 g_completion_free(gcmp);
193 list = string_list_free(list);
194 if( filename )
195 filename=g_strstrip(filename);
196 } else
197 filename=g_strdup(name);
199 if (filename == NULL || filename[0] == '\0')
200 return -1;
202 /* send save command to mpd */
203 if ((error = mpdclient_cmd_save_playlist(c, filename))) {
204 gint code = GET_ACK_ERROR_CODE(error);
206 if (code == MPD_ACK_ERROR_EXIST) {
207 char *buf;
208 int key;
210 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
211 filename, YES, NO);
212 key = tolower(screen_getch(screen->status_window.w,
213 buf));
214 g_free(buf);
216 if (key == YES[0]) {
217 if (mpdclient_cmd_delete_playlist(c, filename)) {
218 g_free(filename);
219 return -1;
220 }
222 error = playlist_save(screen, c, filename, NULL);
223 g_free(filename);
224 return error;
225 }
227 screen_status_printf(_("Aborted!"));
228 }
230 g_free(filename);
231 return -1;
232 }
234 /* success */
235 screen_status_printf(_("Saved %s"), filename);
236 g_free(filename);
237 return 0;
238 }
240 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
241 GList **list, mpdclient_t *c)
242 {
243 g_completion_remove_items(gcmp, *list);
244 *list = string_list_remove(*list, dir);
245 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
246 g_completion_add_items(gcmp, *list);
247 *dir_list = g_list_append(*dir_list, g_strdup(dir));
248 }
250 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
251 {
252 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
253 GList **dir_list = tmp->dir_list;
254 GList **list = tmp->list;
255 mpdclient_t *c = tmp->c;
257 if (*list == NULL) {
258 /* create initial list */
259 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
260 g_completion_add_items(gcmp, *list);
261 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
262 string_list_find(*dir_list, line) == NULL) {
263 /* add directory content to list */
264 add_dir(gcmp, line, dir_list, list, c);
265 }
266 }
268 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
269 GList *items, void *data)
270 {
271 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
272 GList **dir_list = tmp->dir_list;
273 GList **list = tmp->list;
274 mpdclient_t *c = tmp->c;
275 screen_t *screen = tmp->screen;
277 if (g_list_length(items) >= 1)
278 screen_display_completion_list(screen, items);
280 if (line && line[0] && line[strlen(line) - 1] == '/' &&
281 string_list_find(*dir_list, line) == NULL) {
282 /* add directory content to list */
283 add_dir(gcmp, line, dir_list, list, c);
284 }
285 }
287 static int
288 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
289 {
290 gchar *path;
291 GCompletion *gcmp;
292 GList *list = NULL;
293 GList *dir_list = NULL;
294 completion_callback_data_t data;
296 /* initialize completion support */
297 gcmp = g_completion_new(NULL);
298 g_completion_set_compare(gcmp, strncmp);
299 data.list = &list;
300 data.dir_list = &dir_list;
301 data.screen = screen;
302 data.c = c;
303 wrln_completion_callback_data = &data;
304 wrln_pre_completion_callback = add_pre_completion_cb;
305 wrln_post_completion_callback = add_post_completion_cb;
307 /* get path */
308 path = screen_readln(screen->status_window.w,
309 _("Add: "),
310 NULL,
311 NULL,
312 gcmp);
314 /* destroy completion data */
315 wrln_completion_callback_data = NULL;
316 wrln_pre_completion_callback = NULL;
317 wrln_post_completion_callback = NULL;
318 g_completion_free(gcmp);
319 string_list_free(list);
320 string_list_free(dir_list);
322 /* add the path to the playlist */
323 if (path && path[0])
324 mpdclient_cmd_add_path(c, path);
326 return 0;
327 }
329 static void
330 play_init(WINDOW *w, int cols, int rows)
331 {
332 lw = list_window_init(w, cols, rows);
333 }
335 static gboolean
336 timer_hide_cursor(gpointer data)
337 {
338 struct mpdclient *c = data;
340 assert(options.hide_cursor > 0);
341 assert(timer_hide_cursor_id != 0);
343 timer_hide_cursor_id = 0;
345 /* hide the cursor when mpd is playing and the user is inactive */
347 if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
348 lw->flags |= LW_HIDE_CURSOR;
349 playlist_repaint(c);
350 } else
351 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
352 timer_hide_cursor, c);
354 return FALSE;
355 }
357 static void
358 play_open(mpd_unused screen_t *screen, mpdclient_t *c)
359 {
360 static gboolean install_cb = TRUE;
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(mpdclient_t *c)
410 {
411 list_window_paint(lw, list_callback, (void *) c);
412 }
414 static void
415 play_update(mpdclient_t *c)
416 {
417 static int prev_song_id = -1;
418 int current_song_id = c->song != NULL && c->status != NULL &&
419 !IS_STOPPED(c->status->state) ? c->song->id : -1;
421 if (current_song_id != prev_song_id) {
422 prev_song_id = current_song_id;
424 /* center the cursor */
425 if (options.auto_center && current_song_id != -1)
426 center_playing_item(c);
428 playlist_repaint(c);
429 }
430 }
432 #ifdef HAVE_GETMOUSE
433 static int
434 handle_mouse_event(mpd_unused screen_t *screen, mpdclient_t *c)
435 {
436 int row;
437 unsigned selected;
438 unsigned long bstate;
440 if (screen_get_mouse_event(c, &bstate, &row) ||
441 list_window_mouse(lw, c->playlist.list->len, bstate, row)) {
442 playlist_repaint(c);
443 return 1;
444 }
446 if (bstate & BUTTON1_DOUBLE_CLICKED) {
447 /* stop */
448 screen_cmd(c, CMD_STOP);
449 return 1;
450 }
452 selected = lw->start + row;
454 if (bstate & BUTTON1_CLICKED) {
455 /* play */
456 if (lw->start + row < c->playlist.list->len)
457 mpdclient_cmd_play(c, lw->start + row);
458 } else if (bstate & BUTTON3_CLICKED) {
459 /* delete */
460 if (selected == lw->selected)
461 mpdclient_cmd_delete(c, lw->selected);
462 }
464 lw->selected = selected;
465 list_window_check_selected(lw, c->playlist.list->len);
466 playlist_repaint(c);
468 return 1;
469 }
470 #else
471 #define handle_mouse_event(s,c) (0)
472 #endif
474 static int
475 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
476 {
477 lw->flags &= ~LW_HIDE_CURSOR;
479 if (options.hide_cursor > 0) {
480 if (timer_hide_cursor_id != 0)
481 g_source_remove(timer_hide_cursor_id);
482 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
483 timer_hide_cursor, c);
484 }
486 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
487 playlist_repaint(c);
488 return 1;
489 }
491 switch(cmd) {
492 case CMD_PLAY:
493 mpdclient_cmd_play(c, lw->selected);
494 return 1;
495 case CMD_DELETE:
496 mpdclient_cmd_delete(c, lw->selected);
497 return 1;
498 case CMD_SAVE_PLAYLIST:
499 playlist_save(screen, c, NULL, NULL);
500 return 1;
501 case CMD_ADD:
502 handle_add_to_playlist(screen, c);
503 return 1;
504 case CMD_SCREEN_UPDATE:
505 center_playing_item(c);
506 playlist_repaint(c);
507 return 0;
509 case CMD_LIST_MOVE_UP:
510 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
511 return 1;
512 case CMD_LIST_MOVE_DOWN:
513 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
514 return 1;
515 case CMD_LIST_FIND:
516 case CMD_LIST_RFIND:
517 case CMD_LIST_FIND_NEXT:
518 case CMD_LIST_RFIND_NEXT:
519 screen_find(screen,
520 lw, playlist_length(&c->playlist),
521 cmd, list_callback, c);
522 playlist_repaint(c);
523 return 1;
525 case CMD_MOUSE_EVENT:
526 return handle_mouse_event(screen,c);
528 #ifdef ENABLE_LYRICS_SCREEN
529 case CMD_SCREEN_LYRICS:
530 if (lw->selected < playlist_length(&c->playlist)) {
531 screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
532 return 1;
533 }
535 break;
536 #endif
538 default:
539 break;
540 }
542 return 0;
543 }
545 const struct screen_functions screen_playlist = {
546 .init = play_init,
547 .exit = play_exit,
548 .open = play_open,
549 .close = play_close,
550 .resize = play_resize,
551 .paint = play_paint,
552 .update = play_update,
553 .cmd = play_cmd,
554 .get_title = play_title,
555 };