37705c559d4a8800df1903d575b93b4c2e19200a
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 mpdclient_t *c;
48 } completion_callback_data_t;
50 static struct mpdclient_playlist *playlist;
51 static int current_song_id = -1;
52 static list_window_t *lw = NULL;
53 static guint timer_hide_cursor_id;
55 static void
56 play_paint(void);
58 static void
59 playlist_repaint(void)
60 {
61 play_paint();
62 wrefresh(lw->w);
63 }
65 static void
66 playlist_repaint_if_active(void)
67 {
68 if (screen_is_visible(&screen_playlist))
69 playlist_repaint();
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();
89 }
91 static const char *
92 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
93 {
94 static char songname[MAX_SONG_LENGTH];
95 mpd_Song *song;
97 if (playlist == NULL || idx >= playlist_length(playlist))
98 return NULL;
100 song = playlist_get(playlist, idx);
101 if (song->id == current_song_id)
102 *highlight = 1;
104 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
105 return songname;
106 }
108 static void
109 center_playing_item(mpdclient_t *c)
110 {
111 unsigned length = c->playlist.list->len;
112 unsigned offset = lw->selected - lw->start;
113 int idx;
115 if (!c->song || length < lw->rows ||
116 c->status == NULL || IS_STOPPED(c->status->state))
117 return;
119 /* try to center the song that are playing */
120 idx = playlist_get_index(c, c->song);
121 if (idx < 0)
122 return;
124 list_window_center(lw, length, idx);
126 /* make sure the cursor is in the window */
127 lw->selected = lw->start+offset;
128 list_window_check_selected(lw, length);
129 }
131 static void
132 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
133 {
134 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
135 GList **list = tmp->list;
136 mpdclient_t *c = tmp->c;
138 if( *list == NULL ) {
139 /* create completion list */
140 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
141 g_completion_add_items(gcmp, *list);
142 }
143 }
145 static void
146 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
147 GList *items, mpd_unused void *data)
148 {
149 if (g_list_length(items) >= 1)
150 screen_display_completion_list(items);
151 }
153 int
154 playlist_save(mpdclient_t *c, char *name, char *defaultname)
155 {
156 gchar *filename;
157 gint error;
158 GCompletion *gcmp;
159 GList *list = NULL;
160 completion_callback_data_t data;
162 if (name == NULL) {
163 /* initialize completion support */
164 gcmp = g_completion_new(NULL);
165 g_completion_set_compare(gcmp, strncmp);
166 data.list = &list;
167 data.dir_list = NULL;
168 data.c = c;
169 wrln_completion_callback_data = &data;
170 wrln_pre_completion_callback = save_pre_completion_cb;
171 wrln_post_completion_callback = save_post_completion_cb;
174 /* query the user for a filename */
175 filename = screen_readln(screen.status_window.w,
176 _("Save playlist as: "),
177 defaultname,
178 NULL,
179 gcmp);
181 /* destroy completion support */
182 wrln_completion_callback_data = NULL;
183 wrln_pre_completion_callback = NULL;
184 wrln_post_completion_callback = NULL;
185 g_completion_free(gcmp);
186 list = string_list_free(list);
187 if( filename )
188 filename=g_strstrip(filename);
189 } else
190 filename=g_strdup(name);
192 if (filename == NULL || filename[0] == '\0')
193 return -1;
195 /* send save command to mpd */
196 if ((error = mpdclient_cmd_save_playlist(c, filename))) {
197 gint code = GET_ACK_ERROR_CODE(error);
199 if (code == MPD_ACK_ERROR_EXIST) {
200 char *buf;
201 int key;
203 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
204 filename, YES, NO);
205 key = tolower(screen_getch(screen.status_window.w,
206 buf));
207 g_free(buf);
209 if (key == YES[0]) {
210 if (mpdclient_cmd_delete_playlist(c, filename)) {
211 g_free(filename);
212 return -1;
213 }
215 error = playlist_save(c, filename, NULL);
216 g_free(filename);
217 return error;
218 }
220 screen_status_printf(_("Aborted!"));
221 }
223 g_free(filename);
224 return -1;
225 }
227 /* success */
228 screen_status_printf(_("Saved %s"), filename);
229 g_free(filename);
230 return 0;
231 }
233 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
234 GList **list, mpdclient_t *c)
235 {
236 g_completion_remove_items(gcmp, *list);
237 *list = string_list_remove(*list, dir);
238 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
239 g_completion_add_items(gcmp, *list);
240 *dir_list = g_list_append(*dir_list, g_strdup(dir));
241 }
243 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
244 {
245 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
246 GList **dir_list = tmp->dir_list;
247 GList **list = tmp->list;
248 mpdclient_t *c = tmp->c;
250 if (*list == NULL) {
251 /* create initial list */
252 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
253 g_completion_add_items(gcmp, *list);
254 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
255 string_list_find(*dir_list, line) == NULL) {
256 /* add directory content to list */
257 add_dir(gcmp, line, dir_list, list, c);
258 }
259 }
261 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
262 GList *items, void *data)
263 {
264 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
265 GList **dir_list = tmp->dir_list;
266 GList **list = tmp->list;
267 mpdclient_t *c = tmp->c;
269 if (g_list_length(items) >= 1)
270 screen_display_completion_list(items);
272 if (line && line[0] && line[strlen(line) - 1] == '/' &&
273 string_list_find(*dir_list, line) == NULL) {
274 /* add directory content to list */
275 add_dir(gcmp, line, dir_list, list, c);
276 }
277 }
279 static int
280 handle_add_to_playlist(mpdclient_t *c)
281 {
282 gchar *path;
283 GCompletion *gcmp;
284 GList *list = NULL;
285 GList *dir_list = NULL;
286 completion_callback_data_t data;
288 /* initialize completion support */
289 gcmp = g_completion_new(NULL);
290 g_completion_set_compare(gcmp, strncmp);
291 data.list = &list;
292 data.dir_list = &dir_list;
293 data.c = c;
294 wrln_completion_callback_data = &data;
295 wrln_pre_completion_callback = add_pre_completion_cb;
296 wrln_post_completion_callback = add_post_completion_cb;
298 /* get path */
299 path = screen_readln(screen.status_window.w,
300 _("Add: "),
301 NULL,
302 NULL,
303 gcmp);
305 /* destroy completion data */
306 wrln_completion_callback_data = NULL;
307 wrln_pre_completion_callback = NULL;
308 wrln_post_completion_callback = NULL;
309 g_completion_free(gcmp);
310 string_list_free(list);
311 string_list_free(dir_list);
313 /* add the path to the playlist */
314 if (path && path[0])
315 mpdclient_cmd_add_path(c, path);
317 return 0;
318 }
320 static void
321 play_init(WINDOW *w, int cols, int rows)
322 {
323 lw = list_window_init(w, cols, rows);
324 }
326 static gboolean
327 timer_hide_cursor(gpointer data)
328 {
329 struct mpdclient *c = data;
331 assert(options.hide_cursor > 0);
332 assert(timer_hide_cursor_id != 0);
334 timer_hide_cursor_id = 0;
336 /* hide the cursor when mpd is playing and the user is inactive */
338 if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
339 lw->flags |= LW_HIDE_CURSOR;
340 playlist_repaint();
341 } else
342 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
343 timer_hide_cursor, c);
345 return FALSE;
346 }
348 static void
349 play_open(mpdclient_t *c)
350 {
351 static gboolean install_cb = TRUE;
353 playlist = &c->playlist;
355 assert(timer_hide_cursor_id == 0);
356 if (options.hide_cursor > 0) {
357 lw->flags &= ~LW_HIDE_CURSOR;
358 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
359 timer_hide_cursor, c);
360 }
362 if (install_cb) {
363 mpdclient_install_playlist_callback(c, playlist_changed_callback);
364 install_cb = FALSE;
365 }
366 }
368 static void
369 play_close(void)
370 {
371 if (timer_hide_cursor_id != 0) {
372 g_source_remove(timer_hide_cursor_id);
373 timer_hide_cursor_id = 0;
374 }
375 }
377 static void
378 play_resize(int cols, int rows)
379 {
380 lw->cols = cols;
381 lw->rows = rows;
382 }
385 static void
386 play_exit(void)
387 {
388 list_window_free(lw);
389 }
391 static const char *
392 play_title(char *str, size_t size)
393 {
394 if( strcmp(options.host, "localhost") == 0 )
395 return _("Playlist");
397 g_snprintf(str, size, _("Playlist on %s"), options.host);
398 return str;
399 }
401 static void
402 play_paint(void)
403 {
404 list_window_paint(lw, list_callback, NULL);
405 }
407 static void
408 play_update(mpdclient_t *c)
409 {
410 static int prev_song_id = -1;
412 current_song_id = c->song != NULL && c->status != NULL &&
413 !IS_STOPPED(c->status->state) ? c->song->id : -1;
415 if (current_song_id != prev_song_id) {
416 prev_song_id = current_song_id;
418 /* center the cursor */
419 if (options.auto_center && current_song_id != -1)
420 center_playing_item(c);
422 playlist_repaint();
423 }
424 }
426 #ifdef HAVE_GETMOUSE
427 static int
428 handle_mouse_event(struct mpdclient *c)
429 {
430 int row;
431 unsigned selected;
432 unsigned long bstate;
434 if (screen_get_mouse_event(c, &bstate, &row) ||
435 list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
436 playlist_repaint();
437 return 1;
438 }
440 if (bstate & BUTTON1_DOUBLE_CLICKED) {
441 /* stop */
442 screen_cmd(c, CMD_STOP);
443 return 1;
444 }
446 selected = lw->start + row;
448 if (bstate & BUTTON1_CLICKED) {
449 /* play */
450 if (lw->start + row < playlist_length(playlist))
451 mpdclient_cmd_play(c, lw->start + row);
452 } else if (bstate & BUTTON3_CLICKED) {
453 /* delete */
454 if (selected == lw->selected)
455 mpdclient_cmd_delete(c, lw->selected);
456 }
458 lw->selected = selected;
459 list_window_check_selected(lw, playlist_length(playlist));
460 playlist_repaint();
462 return 1;
463 }
464 #endif
466 static int
467 play_cmd(mpdclient_t *c, command_t cmd)
468 {
469 lw->flags &= ~LW_HIDE_CURSOR;
471 if (options.hide_cursor > 0) {
472 if (timer_hide_cursor_id != 0)
473 g_source_remove(timer_hide_cursor_id);
474 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
475 timer_hide_cursor, c);
476 }
478 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
479 playlist_repaint();
480 return 1;
481 }
483 switch(cmd) {
484 case CMD_PLAY:
485 mpdclient_cmd_play(c, lw->selected);
486 return 1;
487 case CMD_DELETE:
488 mpdclient_cmd_delete(c, lw->selected);
489 return 1;
490 case CMD_SAVE_PLAYLIST:
491 playlist_save(c, NULL, NULL);
492 return 1;
493 case CMD_ADD:
494 handle_add_to_playlist(c);
495 return 1;
496 case CMD_SCREEN_UPDATE:
497 center_playing_item(c);
498 playlist_repaint();
499 return 0;
501 case CMD_LIST_MOVE_UP:
502 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
503 return 1;
504 case CMD_LIST_MOVE_DOWN:
505 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
506 return 1;
507 case CMD_LIST_FIND:
508 case CMD_LIST_RFIND:
509 case CMD_LIST_FIND_NEXT:
510 case CMD_LIST_RFIND_NEXT:
511 screen_find(lw, playlist_length(&c->playlist),
512 cmd, list_callback, NULL);
513 playlist_repaint();
514 return 1;
516 #ifdef HAVE_GETMOUSE
517 case CMD_MOUSE_EVENT:
518 return handle_mouse_event(c);
519 #endif
521 #ifdef ENABLE_LYRICS_SCREEN
522 case CMD_SCREEN_LYRICS:
523 if (lw->selected < playlist_length(&c->playlist)) {
524 screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
525 return 1;
526 }
528 break;
529 #endif
531 default:
532 break;
533 }
535 return 0;
536 }
538 const struct screen_functions screen_playlist = {
539 .init = play_init,
540 .exit = play_exit,
541 .open = play_open,
542 .close = play_close,
543 .resize = play_resize,
544 .paint = play_paint,
545 .update = play_update,
546 .cmd = play_cmd,
547 .get_title = play_title,
548 };