7ee0a9ff487817967d9957e100eab7111a553bbc
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, bool *highlight, G_GNUC_UNUSED void *data)
94 {
95 static char songname[MAX_SONG_LENGTH];
96 #ifndef NCMPC_MINI
97 static scroll_state_t st;
98 #endif
99 mpd_Song *song;
101 if (playlist == NULL || idx >= playlist_length(playlist))
102 return NULL;
104 song = playlist_get(playlist, idx);
105 if (song->id == current_song_id)
106 *highlight = true;
108 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
110 #ifndef NCMPC_MINI
111 if (options.scroll && (unsigned)song->pos == lw->selected &&
112 utf8_width(songname) > (unsigned)COLS) {
113 static unsigned current_song;
114 char *tmp;
116 if (current_song != lw->selected) {
117 st.offset = 0;
118 current_song = lw->selected;
119 }
121 tmp = strscroll(songname, options.scroll_sep,
122 MAX_SONG_LENGTH, &st);
123 g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
124 g_free(tmp);
125 } else if ((unsigned)song->pos == lw->selected)
126 st.offset = 0;
127 #endif
129 return songname;
130 }
132 static void
133 center_playing_item(mpdclient_t *c)
134 {
135 unsigned length = c->playlist.list->len;
136 unsigned offset = lw->selected - lw->start;
137 int idx;
139 if (!c->song || length < lw->rows ||
140 c->status == NULL || IS_STOPPED(c->status->state))
141 return;
143 /* try to center the song that are playing */
144 idx = playlist_get_index(c, c->song);
145 if (idx < 0)
146 return;
148 list_window_center(lw, length, idx);
150 /* make sure the cursor is in the window */
151 lw->selected = lw->start+offset;
152 list_window_check_selected(lw, length);
153 }
155 #ifndef NCMPC_MINI
156 static void
157 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
158 void *data)
159 {
160 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
161 GList **list = tmp->list;
162 mpdclient_t *c = tmp->c;
164 if( *list == NULL ) {
165 /* create completion list */
166 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
167 g_completion_add_items(gcmp, *list);
168 }
169 }
171 static void
172 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
173 G_GNUC_UNUSED gchar *line, GList *items,
174 G_GNUC_UNUSED void *data)
175 {
176 if (g_list_length(items) >= 1)
177 screen_display_completion_list(items);
178 }
179 #endif
181 int
182 playlist_save(mpdclient_t *c, char *name, char *defaultname)
183 {
184 gchar *filename, *filename_utf8;
185 gint error;
186 #ifndef NCMPC_MINI
187 GCompletion *gcmp;
188 GList *list = NULL;
189 completion_callback_data_t data;
190 #endif
192 #ifdef NCMPC_MINI
193 (void)defaultname;
194 #endif
196 #ifndef NCMPC_MINI
197 if (name == NULL) {
198 /* initialize completion support */
199 gcmp = g_completion_new(NULL);
200 g_completion_set_compare(gcmp, strncmp);
201 data.list = &list;
202 data.dir_list = NULL;
203 data.c = c;
204 wrln_completion_callback_data = &data;
205 wrln_pre_completion_callback = save_pre_completion_cb;
206 wrln_post_completion_callback = save_post_completion_cb;
209 /* query the user for a filename */
210 filename = screen_readln(screen.status_window.w,
211 _("Save playlist as: "),
212 defaultname,
213 NULL,
214 gcmp);
216 /* destroy completion support */
217 wrln_completion_callback_data = NULL;
218 wrln_pre_completion_callback = NULL;
219 wrln_post_completion_callback = NULL;
220 g_completion_free(gcmp);
221 list = string_list_free(list);
222 if( filename )
223 filename=g_strstrip(filename);
224 } else
225 #endif
226 filename=g_strdup(name);
228 if (filename == NULL)
229 return -1;
231 /* send save command to mpd */
233 filename_utf8 = locale_to_utf8(filename);
234 error = mpdclient_cmd_save_playlist(c, filename_utf8);
235 g_free(filename_utf8);
237 if (error) {
238 gint code = GET_ACK_ERROR_CODE(error);
240 if (code == MPD_ACK_ERROR_EXIST) {
241 char *buf;
242 int key;
244 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
245 filename, YES, NO);
246 key = tolower(screen_getch(screen.status_window.w,
247 buf));
248 g_free(buf);
250 if (key == YES[0]) {
251 filename_utf8 = locale_to_utf8(filename);
252 error = mpdclient_cmd_delete_playlist(c, filename_utf8);
253 g_free(filename_utf8);
255 if (error) {
256 g_free(filename);
257 return -1;
258 }
260 error = playlist_save(c, filename, NULL);
261 g_free(filename);
262 return error;
263 }
265 screen_status_printf(_("Aborted"));
266 }
268 g_free(filename);
269 return -1;
270 }
272 /* success */
273 screen_status_printf(_("Saved %s"), filename);
274 g_free(filename);
275 return 0;
276 }
278 #ifndef NCMPC_MINI
279 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
280 GList **list, mpdclient_t *c)
281 {
282 g_completion_remove_items(gcmp, *list);
283 *list = string_list_remove(*list, dir);
284 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
285 g_completion_add_items(gcmp, *list);
286 *dir_list = g_list_append(*dir_list, g_strdup(dir));
287 }
289 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
290 {
291 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
292 GList **dir_list = tmp->dir_list;
293 GList **list = tmp->list;
294 mpdclient_t *c = tmp->c;
296 if (*list == NULL) {
297 /* create initial list */
298 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
299 g_completion_add_items(gcmp, *list);
300 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
301 string_list_find(*dir_list, line) == NULL) {
302 /* add directory content to list */
303 add_dir(gcmp, line, dir_list, list, c);
304 }
305 }
307 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
308 GList *items, void *data)
309 {
310 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
311 GList **dir_list = tmp->dir_list;
312 GList **list = tmp->list;
313 mpdclient_t *c = tmp->c;
315 if (g_list_length(items) >= 1)
316 screen_display_completion_list(items);
318 if (line && line[0] && line[strlen(line) - 1] == '/' &&
319 string_list_find(*dir_list, line) == NULL) {
320 /* add directory content to list */
321 add_dir(gcmp, line, dir_list, list, c);
322 }
323 }
324 #endif
326 static int
327 handle_add_to_playlist(mpdclient_t *c)
328 {
329 gchar *path;
330 #ifndef NCMPC_MINI
331 GCompletion *gcmp;
332 GList *list = NULL;
333 GList *dir_list = NULL;
334 completion_callback_data_t data;
336 /* initialize completion support */
337 gcmp = g_completion_new(NULL);
338 g_completion_set_compare(gcmp, strncmp);
339 data.list = &list;
340 data.dir_list = &dir_list;
341 data.c = c;
342 wrln_completion_callback_data = &data;
343 wrln_pre_completion_callback = add_pre_completion_cb;
344 wrln_post_completion_callback = add_post_completion_cb;
345 #endif
347 /* get path */
348 path = screen_readln(screen.status_window.w,
349 _("Add: "),
350 NULL,
351 NULL,
352 #ifdef NCMPC_MINI
353 NULL
354 #else
355 gcmp
356 #endif
357 );
359 /* destroy completion data */
360 #ifndef NCMPC_MINI
361 wrln_completion_callback_data = NULL;
362 wrln_pre_completion_callback = NULL;
363 wrln_post_completion_callback = NULL;
364 g_completion_free(gcmp);
365 string_list_free(list);
366 string_list_free(dir_list);
367 #endif
369 /* add the path to the playlist */
370 if (path != NULL) {
371 char *path_utf8 = locale_to_utf8(path);
372 mpdclient_cmd_add_path(c, path_utf8);
373 g_free(path_utf8);
374 }
376 g_free(path);
377 return 0;
378 }
380 static void
381 play_init(WINDOW *w, int cols, int rows)
382 {
383 lw = list_window_init(w, cols, rows);
384 }
386 static gboolean
387 timer_hide_cursor(gpointer data)
388 {
389 struct mpdclient *c = data;
391 assert(options.hide_cursor > 0);
392 assert(timer_hide_cursor_id != 0);
394 timer_hide_cursor_id = 0;
396 /* hide the cursor when mpd is playing and the user is inactive */
398 if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
399 lw->flags |= LW_HIDE_CURSOR;
400 playlist_repaint();
401 } else
402 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
403 timer_hide_cursor, c);
405 return FALSE;
406 }
408 static void
409 play_open(mpdclient_t *c)
410 {
411 static gboolean install_cb = TRUE;
413 playlist = &c->playlist;
415 assert(timer_hide_cursor_id == 0);
416 if (options.hide_cursor > 0) {
417 lw->flags &= ~LW_HIDE_CURSOR;
418 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
419 timer_hide_cursor, c);
420 }
422 if (install_cb) {
423 mpdclient_install_playlist_callback(c, playlist_changed_callback);
424 install_cb = FALSE;
425 }
426 }
428 static void
429 play_close(void)
430 {
431 if (timer_hide_cursor_id != 0) {
432 g_source_remove(timer_hide_cursor_id);
433 timer_hide_cursor_id = 0;
434 }
435 }
437 static void
438 play_resize(int cols, int rows)
439 {
440 lw->cols = cols;
441 lw->rows = rows;
442 }
445 static void
446 play_exit(void)
447 {
448 list_window_free(lw);
449 }
451 static const char *
452 play_title(char *str, size_t size)
453 {
454 if( strcmp(options.host, "localhost") == 0 )
455 return _("Playlist");
457 g_snprintf(str, size, _("Playlist on %s"), options.host);
458 return str;
459 }
461 static void
462 play_paint(void)
463 {
464 list_window_paint(lw, list_callback, NULL);
465 }
467 static void
468 play_update(mpdclient_t *c)
469 {
470 static int prev_song_id = -1;
472 current_song_id = c->song != NULL && c->status != NULL &&
473 !IS_STOPPED(c->status->state) ? c->song->id : -1;
475 if (current_song_id != prev_song_id
476 #ifndef NCMPC_MINI
477 || options.scroll
478 #endif
479 ) {
480 prev_song_id = current_song_id;
482 /* center the cursor */
483 if (options.auto_center && current_song_id != -1)
484 center_playing_item(c);
486 playlist_repaint();
487 }
488 }
490 #ifdef HAVE_GETMOUSE
491 static bool
492 handle_mouse_event(struct mpdclient *c)
493 {
494 int row;
495 unsigned selected;
496 unsigned long bstate;
498 if (screen_get_mouse_event(c, &bstate, &row) ||
499 list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
500 playlist_repaint();
501 return true;
502 }
504 if (bstate & BUTTON1_DOUBLE_CLICKED) {
505 /* stop */
506 screen_cmd(c, CMD_STOP);
507 return true;
508 }
510 selected = lw->start + row;
512 if (bstate & BUTTON1_CLICKED) {
513 /* play */
514 if (lw->start + row < playlist_length(playlist))
515 mpdclient_cmd_play(c, lw->start + row);
516 } else if (bstate & BUTTON3_CLICKED) {
517 /* delete */
518 if (selected == lw->selected)
519 mpdclient_cmd_delete(c, lw->selected);
520 }
522 lw->selected = selected;
523 list_window_check_selected(lw, playlist_length(playlist));
524 playlist_repaint();
526 return true;
527 }
528 #endif
530 static bool
531 play_cmd(mpdclient_t *c, command_t cmd)
532 {
533 lw->flags &= ~LW_HIDE_CURSOR;
535 if (options.hide_cursor > 0) {
536 if (timer_hide_cursor_id != 0)
537 g_source_remove(timer_hide_cursor_id);
538 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
539 timer_hide_cursor, c);
540 }
542 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
543 playlist_repaint();
544 return true;
545 }
547 switch(cmd) {
548 case CMD_PLAY:
549 mpdclient_cmd_play(c, lw->selected);
550 return true;
551 case CMD_DELETE:
552 mpdclient_cmd_delete(c, lw->selected);
553 return true;
554 case CMD_SAVE_PLAYLIST:
555 playlist_save(c, NULL, NULL);
556 return true;
557 case CMD_ADD:
558 handle_add_to_playlist(c);
559 return true;
560 case CMD_SCREEN_UPDATE:
561 center_playing_item(c);
562 playlist_repaint();
563 return false;
565 case CMD_LIST_MOVE_UP:
566 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
567 return true;
568 case CMD_LIST_MOVE_DOWN:
569 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
570 return true;
571 case CMD_LIST_FIND:
572 case CMD_LIST_RFIND:
573 case CMD_LIST_FIND_NEXT:
574 case CMD_LIST_RFIND_NEXT:
575 screen_find(lw, playlist_length(&c->playlist),
576 cmd, list_callback, NULL);
577 playlist_repaint();
578 return true;
580 #ifdef HAVE_GETMOUSE
581 case CMD_MOUSE_EVENT:
582 return handle_mouse_event(c);
583 #endif
585 #ifdef ENABLE_SONG_SCREEN
586 case CMD_VIEW:
587 if (lw->selected < playlist_length(&c->playlist)) {
588 screen_song_switch(c, playlist_get(&c->playlist, lw->selected));
589 return true;
590 }
592 break;
593 #endif
595 case CMD_LOCATE:
596 if (lw->selected < playlist_length(&c->playlist)) {
597 screen_file_goto_song(c, playlist_get(&c->playlist, lw->selected));
598 return true;
599 }
601 break;
603 #ifdef ENABLE_LYRICS_SCREEN
604 case CMD_SCREEN_LYRICS:
605 if (lw->selected < playlist_length(&c->playlist)) {
606 screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
607 return true;
608 }
610 break;
611 #endif
613 default:
614 break;
615 }
617 return false;
618 }
620 const struct screen_functions screen_playlist = {
621 .init = play_init,
622 .exit = play_exit,
623 .open = play_open,
624 .close = play_close,
625 .resize = play_resize,
626 .paint = play_paint,
627 .update = play_update,
628 .cmd = play_cmd,
629 .get_title = play_title,
630 };