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 "charset.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"
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <glib.h>
40 #define MAX_SONG_LENGTH 512
42 #ifndef NCMPC_MINI
43 typedef struct
44 {
45 GList **list;
46 GList **dir_list;
47 mpdclient_t *c;
48 } completion_callback_data_t;
49 #endif
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, G_GNUC_UNUSED void *data)
94 {
95 static char songname[MAX_SONG_LENGTH];
96 static scroll_state_t st;
97 mpd_Song *song;
99 if (playlist == NULL || idx >= playlist_length(playlist))
100 return NULL;
102 song = playlist_get(playlist, idx);
103 if (song->id == current_song_id)
104 *highlight = 1;
106 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
108 if (options.scroll && (unsigned)song->pos == lw->selected &&
109 utf8_width(songname) > (unsigned)COLS) {
110 static unsigned current_song;
111 char *tmp;
113 if (current_song != lw->selected) {
114 st.offset = 0;
115 current_song = lw->selected;
116 }
118 tmp = strscroll(songname, options.scroll_sep,
119 MAX_SONG_LENGTH, &st);
120 g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
121 g_free(tmp);
122 } else if ((unsigned)song->pos == lw->selected)
123 st.offset = 0;
125 return songname;
126 }
128 static void
129 center_playing_item(mpdclient_t *c)
130 {
131 unsigned length = c->playlist.list->len;
132 unsigned offset = lw->selected - lw->start;
133 int idx;
135 if (!c->song || length < lw->rows ||
136 c->status == NULL || IS_STOPPED(c->status->state))
137 return;
139 /* try to center the song that are playing */
140 idx = playlist_get_index(c, c->song);
141 if (idx < 0)
142 return;
144 list_window_center(lw, length, idx);
146 /* make sure the cursor is in the window */
147 lw->selected = lw->start+offset;
148 list_window_check_selected(lw, length);
149 }
151 #ifndef NCMPC_MINI
152 static void
153 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
154 void *data)
155 {
156 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
157 GList **list = tmp->list;
158 mpdclient_t *c = tmp->c;
160 if( *list == NULL ) {
161 /* create completion list */
162 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
163 g_completion_add_items(gcmp, *list);
164 }
165 }
167 static void
168 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
169 G_GNUC_UNUSED gchar *line, GList *items,
170 G_GNUC_UNUSED void *data)
171 {
172 if (g_list_length(items) >= 1)
173 screen_display_completion_list(items);
174 }
175 #endif
177 int
178 playlist_save(mpdclient_t *c, char *name, char *defaultname)
179 {
180 gchar *filename, *filename_utf8;
181 gint error;
182 #ifndef NCMPC_MINI
183 GCompletion *gcmp;
184 GList *list = NULL;
185 completion_callback_data_t data;
186 #endif
188 #ifdef NCMPC_MINI
189 (void)defaultname;
190 #endif
192 #ifndef NCMPC_MINI
193 if (name == NULL) {
194 /* initialize completion support */
195 gcmp = g_completion_new(NULL);
196 g_completion_set_compare(gcmp, strncmp);
197 data.list = &list;
198 data.dir_list = NULL;
199 data.c = c;
200 wrln_completion_callback_data = &data;
201 wrln_pre_completion_callback = save_pre_completion_cb;
202 wrln_post_completion_callback = save_post_completion_cb;
205 /* query the user for a filename */
206 filename = screen_readln(screen.status_window.w,
207 _("Save playlist as: "),
208 defaultname,
209 NULL,
210 gcmp);
212 /* destroy completion support */
213 wrln_completion_callback_data = NULL;
214 wrln_pre_completion_callback = NULL;
215 wrln_post_completion_callback = NULL;
216 g_completion_free(gcmp);
217 list = string_list_free(list);
218 if( filename )
219 filename=g_strstrip(filename);
220 } else
221 #endif
222 filename=g_strdup(name);
224 if (filename == NULL)
225 return -1;
227 /* send save command to mpd */
229 filename_utf8 = locale_to_utf8(filename);
230 error = mpdclient_cmd_save_playlist(c, filename_utf8);
231 g_free(filename_utf8);
233 if (error) {
234 gint code = GET_ACK_ERROR_CODE(error);
236 if (code == MPD_ACK_ERROR_EXIST) {
237 char *buf;
238 int key;
240 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
241 filename, YES, NO);
242 key = tolower(screen_getch(screen.status_window.w,
243 buf));
244 g_free(buf);
246 if (key == YES[0]) {
247 filename_utf8 = locale_to_utf8(filename);
248 error = mpdclient_cmd_delete_playlist(c, filename_utf8);
249 g_free(filename_utf8);
251 if (error) {
252 g_free(filename);
253 return -1;
254 }
256 error = playlist_save(c, filename, NULL);
257 g_free(filename);
258 return error;
259 }
261 screen_status_printf(_("Aborted"));
262 }
264 g_free(filename);
265 return -1;
266 }
268 /* success */
269 screen_status_printf(_("Saved %s"), filename);
270 g_free(filename);
271 return 0;
272 }
274 #ifndef NCMPC_MINI
275 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
276 GList **list, mpdclient_t *c)
277 {
278 g_completion_remove_items(gcmp, *list);
279 *list = string_list_remove(*list, dir);
280 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
281 g_completion_add_items(gcmp, *list);
282 *dir_list = g_list_append(*dir_list, g_strdup(dir));
283 }
285 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
286 {
287 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
288 GList **dir_list = tmp->dir_list;
289 GList **list = tmp->list;
290 mpdclient_t *c = tmp->c;
292 if (*list == NULL) {
293 /* create initial list */
294 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
295 g_completion_add_items(gcmp, *list);
296 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
297 string_list_find(*dir_list, line) == NULL) {
298 /* add directory content to list */
299 add_dir(gcmp, line, dir_list, list, c);
300 }
301 }
303 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
304 GList *items, void *data)
305 {
306 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
307 GList **dir_list = tmp->dir_list;
308 GList **list = tmp->list;
309 mpdclient_t *c = tmp->c;
311 if (g_list_length(items) >= 1)
312 screen_display_completion_list(items);
314 if (line && line[0] && line[strlen(line) - 1] == '/' &&
315 string_list_find(*dir_list, line) == NULL) {
316 /* add directory content to list */
317 add_dir(gcmp, line, dir_list, list, c);
318 }
319 }
320 #endif
322 static int
323 handle_add_to_playlist(mpdclient_t *c)
324 {
325 gchar *path;
326 #ifndef NCMPC_MINI
327 GCompletion *gcmp;
328 GList *list = NULL;
329 GList *dir_list = NULL;
330 completion_callback_data_t data;
332 /* initialize completion support */
333 gcmp = g_completion_new(NULL);
334 g_completion_set_compare(gcmp, strncmp);
335 data.list = &list;
336 data.dir_list = &dir_list;
337 data.c = c;
338 wrln_completion_callback_data = &data;
339 wrln_pre_completion_callback = add_pre_completion_cb;
340 wrln_post_completion_callback = add_post_completion_cb;
341 #endif
343 /* get path */
344 path = screen_readln(screen.status_window.w,
345 _("Add: "),
346 NULL,
347 NULL,
348 #ifdef NCMPC_MINI
349 NULL
350 #else
351 gcmp
352 #endif
353 );
355 /* destroy completion data */
356 #ifndef NCMPC_MINI
357 wrln_completion_callback_data = NULL;
358 wrln_pre_completion_callback = NULL;
359 wrln_post_completion_callback = NULL;
360 g_completion_free(gcmp);
361 string_list_free(list);
362 string_list_free(dir_list);
363 #endif
365 /* add the path to the playlist */
366 if (path != NULL) {
367 char *path_utf8 = locale_to_utf8(path);
368 mpdclient_cmd_add_path(c, path_utf8);
369 g_free(path_utf8);
370 }
372 g_free(path);
373 return 0;
374 }
376 static void
377 play_init(WINDOW *w, int cols, int rows)
378 {
379 lw = list_window_init(w, cols, rows);
380 }
382 static gboolean
383 timer_hide_cursor(gpointer data)
384 {
385 struct mpdclient *c = data;
387 assert(options.hide_cursor > 0);
388 assert(timer_hide_cursor_id != 0);
390 timer_hide_cursor_id = 0;
392 /* hide the cursor when mpd is playing and the user is inactive */
394 if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
395 lw->flags |= LW_HIDE_CURSOR;
396 playlist_repaint();
397 } else
398 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
399 timer_hide_cursor, c);
401 return FALSE;
402 }
404 static void
405 play_open(mpdclient_t *c)
406 {
407 static gboolean install_cb = TRUE;
409 playlist = &c->playlist;
411 assert(timer_hide_cursor_id == 0);
412 if (options.hide_cursor > 0) {
413 lw->flags &= ~LW_HIDE_CURSOR;
414 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
415 timer_hide_cursor, c);
416 }
418 if (install_cb) {
419 mpdclient_install_playlist_callback(c, playlist_changed_callback);
420 install_cb = FALSE;
421 }
422 }
424 static void
425 play_close(void)
426 {
427 if (timer_hide_cursor_id != 0) {
428 g_source_remove(timer_hide_cursor_id);
429 timer_hide_cursor_id = 0;
430 }
431 }
433 static void
434 play_resize(int cols, int rows)
435 {
436 lw->cols = cols;
437 lw->rows = rows;
438 }
441 static void
442 play_exit(void)
443 {
444 list_window_free(lw);
445 }
447 static const char *
448 play_title(char *str, size_t size)
449 {
450 if( strcmp(options.host, "localhost") == 0 )
451 return _("Playlist");
453 g_snprintf(str, size, _("Playlist on %s"), options.host);
454 return str;
455 }
457 static void
458 play_paint(void)
459 {
460 list_window_paint(lw, list_callback, NULL);
461 }
463 static void
464 play_update(mpdclient_t *c)
465 {
466 static int prev_song_id = -1;
468 current_song_id = c->song != NULL && c->status != NULL &&
469 !IS_STOPPED(c->status->state) ? c->song->id : -1;
471 if (options.scroll || current_song_id != prev_song_id) {
472 prev_song_id = current_song_id;
474 /* center the cursor */
475 if (options.auto_center && current_song_id != -1)
476 center_playing_item(c);
478 playlist_repaint();
479 }
480 }
482 #ifdef HAVE_GETMOUSE
483 static bool
484 handle_mouse_event(struct mpdclient *c)
485 {
486 int row;
487 unsigned selected;
488 unsigned long bstate;
490 if (screen_get_mouse_event(c, &bstate, &row) ||
491 list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
492 playlist_repaint();
493 return true;
494 }
496 if (bstate & BUTTON1_DOUBLE_CLICKED) {
497 /* stop */
498 screen_cmd(c, CMD_STOP);
499 return true;
500 }
502 selected = lw->start + row;
504 if (bstate & BUTTON1_CLICKED) {
505 /* play */
506 if (lw->start + row < playlist_length(playlist))
507 mpdclient_cmd_play(c, lw->start + row);
508 } else if (bstate & BUTTON3_CLICKED) {
509 /* delete */
510 if (selected == lw->selected)
511 mpdclient_cmd_delete(c, lw->selected);
512 }
514 lw->selected = selected;
515 list_window_check_selected(lw, playlist_length(playlist));
516 playlist_repaint();
518 return true;
519 }
520 #endif
522 static bool
523 play_cmd(mpdclient_t *c, command_t cmd)
524 {
525 lw->flags &= ~LW_HIDE_CURSOR;
527 if (options.hide_cursor > 0) {
528 if (timer_hide_cursor_id != 0)
529 g_source_remove(timer_hide_cursor_id);
530 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
531 timer_hide_cursor, c);
532 }
534 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
535 playlist_repaint();
536 return true;
537 }
539 switch(cmd) {
540 case CMD_PLAY:
541 mpdclient_cmd_play(c, lw->selected);
542 return true;
543 case CMD_DELETE:
544 mpdclient_cmd_delete(c, lw->selected);
545 return true;
546 case CMD_SAVE_PLAYLIST:
547 playlist_save(c, NULL, NULL);
548 return true;
549 case CMD_ADD:
550 handle_add_to_playlist(c);
551 return true;
552 case CMD_SCREEN_UPDATE:
553 center_playing_item(c);
554 playlist_repaint();
555 return false;
557 case CMD_LIST_MOVE_UP:
558 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
559 return true;
560 case CMD_LIST_MOVE_DOWN:
561 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
562 return true;
563 case CMD_LIST_FIND:
564 case CMD_LIST_RFIND:
565 case CMD_LIST_FIND_NEXT:
566 case CMD_LIST_RFIND_NEXT:
567 screen_find(lw, playlist_length(&c->playlist),
568 cmd, list_callback, NULL);
569 playlist_repaint();
570 return true;
572 #ifdef HAVE_GETMOUSE
573 case CMD_MOUSE_EVENT:
574 return handle_mouse_event(c);
575 #endif
577 #ifdef ENABLE_SONG_SCREEN
578 case CMD_VIEW:
579 if (lw->selected < playlist_length(&c->playlist)) {
580 screen_song_switch(c, playlist_get(&c->playlist, lw->selected));
581 return true;
582 }
584 break;
585 #endif
587 case CMD_LOCATE:
588 if (lw->selected < playlist_length(&c->playlist)) {
589 screen_file_goto_song(c, playlist_get(&c->playlist, lw->selected));
590 return true;
591 }
593 break;
595 #ifdef ENABLE_LYRICS_SCREEN
596 case CMD_SCREEN_LYRICS:
597 if (lw->selected < playlist_length(&c->playlist)) {
598 screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
599 return true;
600 }
602 break;
603 #endif
605 default:
606 break;
607 }
609 return false;
610 }
612 const struct screen_functions screen_playlist = {
613 .init = play_init,
614 .exit = play_exit,
615 .open = play_open,
616 .close = play_close,
617 .resize = play_resize,
618 .paint = play_paint,
619 .update = play_update,
620 .cmd = play_cmd,
621 .get_title = play_title,
622 };