1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "config.h"
21 #include "i18n.h"
22 #include "charset.h"
23 #include "options.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 #ifndef NCMPC_MINI
35 #include "hscroll.h"
36 #endif
38 #include <ctype.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 #include <glib.h>
44 #define MAX_SONG_LENGTH 512
46 #ifndef NCMPC_MINI
47 typedef struct
48 {
49 GList **list;
50 GList **dir_list;
51 mpdclient_t *c;
52 } completion_callback_data_t;
53 #endif
55 static struct mpdclient_playlist *playlist;
56 static int current_song_id = -1;
57 static list_window_t *lw = NULL;
58 static guint timer_hide_cursor_id;
60 static void
61 play_paint(void);
63 static void
64 playlist_repaint(void)
65 {
66 play_paint();
67 wrefresh(lw->w);
68 }
70 static void
71 playlist_repaint_if_active(void)
72 {
73 if (screen_is_visible(&screen_playlist))
74 playlist_repaint();
75 }
77 static void
78 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
79 {
80 switch (event) {
81 case PLAYLIST_EVENT_DELETE:
82 break;
83 case PLAYLIST_EVENT_MOVE:
84 if(lw->range_selection < 0)
85 {
86 lw->selected = *((int *) data);
87 if (lw->selected < lw->start)
88 lw->start--;
89 }
90 break;
91 default:
92 break;
93 }
95 list_window_check_selected(lw, c->playlist.list->len);
96 playlist_repaint_if_active();
97 }
99 static const char *
100 list_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED void *data)
101 {
102 static char songname[MAX_SONG_LENGTH];
103 #ifndef NCMPC_MINI
104 static scroll_state_t st;
105 #endif
106 mpd_Song *song;
108 if (playlist == NULL || idx >= playlist_length(playlist))
109 return NULL;
111 song = playlist_get(playlist, idx);
112 if (song->id == current_song_id)
113 *highlight = true;
115 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
117 #ifndef NCMPC_MINI
118 if (options.scroll && (unsigned)song->pos == lw->selected &&
119 utf8_width(songname) > (unsigned)COLS) {
120 static unsigned current_song;
121 char *tmp;
123 if (current_song != lw->selected) {
124 st.offset = 0;
125 current_song = lw->selected;
126 }
128 tmp = strscroll(songname, options.scroll_sep,
129 MAX_SONG_LENGTH, &st);
130 g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
131 g_free(tmp);
132 } else if ((unsigned)song->pos == lw->selected)
133 st.offset = 0;
134 #endif
136 return songname;
137 }
139 static void
140 center_playing_item(mpdclient_t *c, bool center_cursor)
141 {
142 unsigned length = c->playlist.list->len;
143 int idx;
145 if (!c->song || c->status == NULL ||
146 IS_STOPPED(c->status->state))
147 return;
149 /* try to center the song that are playing */
150 idx = playlist_get_index(c, c->song);
151 if (idx < 0)
152 return;
154 if (length < lw->rows)
155 {
156 if (center_cursor)
157 list_window_set_selected(lw, idx);
158 return;
159 }
161 list_window_center(lw, length, idx);
163 if (center_cursor) {
164 list_window_set_selected(lw, idx);
165 return;
166 }
168 /* make sure the cursor is in the window */
169 if (lw->selected < lw->start) {
170 lw->selected = lw->start;
171 if (lw->range_selection) {
172 lw->selected_start = lw->range_base;
173 lw->selected_end = lw->selected;
174 } else {
175 lw->selected_start = lw->selected;
176 lw->selected_end = lw->selected;
177 }
178 } else if (lw->selected > lw->start + lw->rows - 1) {
179 lw->selected = lw->start + lw->rows - 1;
180 if (lw->range_selection) {
181 lw->selected_start = lw->selected;
182 lw->selected_end = lw->range_base;
183 } else {
184 lw->selected_start = lw->selected;
185 lw->selected_end = lw->selected;
186 }
187 }
188 }
190 #ifndef NCMPC_MINI
191 static void
192 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
193 void *data)
194 {
195 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
196 GList **list = tmp->list;
197 mpdclient_t *c = tmp->c;
199 if( *list == NULL ) {
200 /* create completion list */
201 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
202 g_completion_add_items(gcmp, *list);
203 }
204 }
206 static void
207 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
208 G_GNUC_UNUSED gchar *line, GList *items,
209 G_GNUC_UNUSED void *data)
210 {
211 if (g_list_length(items) >= 1)
212 screen_display_completion_list(items);
213 }
214 #endif
216 #ifndef NCMPC_MINI
217 /**
218 * Wrapper for strncmp(). We are not allowed to pass &strncmp to
219 * g_completion_set_compare(), because strncmp() takes size_t where
220 * g_completion_set_compare passes a gsize value.
221 */
222 static gint
223 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
224 {
225 return strncmp(s1, s2, n);
226 }
227 #endif
229 int
230 playlist_save(mpdclient_t *c, char *name, char *defaultname)
231 {
232 gchar *filename, *filename_utf8;
233 gint error;
234 #ifndef NCMPC_MINI
235 GCompletion *gcmp;
236 GList *list = NULL;
237 completion_callback_data_t data;
238 #endif
240 #ifdef NCMPC_MINI
241 (void)defaultname;
242 #endif
244 #ifndef NCMPC_MINI
245 if (name == NULL) {
246 /* initialize completion support */
247 gcmp = g_completion_new(NULL);
248 g_completion_set_compare(gcmp, completion_strncmp);
249 data.list = &list;
250 data.dir_list = NULL;
251 data.c = c;
252 wrln_completion_callback_data = &data;
253 wrln_pre_completion_callback = save_pre_completion_cb;
254 wrln_post_completion_callback = save_post_completion_cb;
257 /* query the user for a filename */
258 filename = screen_readln(screen.status_window.w,
259 _("Save playlist as"),
260 defaultname,
261 NULL,
262 gcmp);
264 /* destroy completion support */
265 wrln_completion_callback_data = NULL;
266 wrln_pre_completion_callback = NULL;
267 wrln_post_completion_callback = NULL;
268 g_completion_free(gcmp);
269 list = string_list_free(list);
270 if( filename )
271 filename=g_strstrip(filename);
272 } else
273 #endif
274 filename=g_strdup(name);
276 if (filename == NULL)
277 return -1;
279 /* send save command to mpd */
281 filename_utf8 = locale_to_utf8(filename);
282 error = mpdclient_cmd_save_playlist(c, filename_utf8);
283 g_free(filename_utf8);
285 if (error) {
286 gint code = GET_ACK_ERROR_CODE(error);
288 if (code == MPD_ACK_ERROR_EXIST) {
289 char *buf;
290 int key;
292 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
293 filename, YES, NO);
294 key = tolower(screen_getch(screen.status_window.w,
295 buf));
296 g_free(buf);
298 if (key == YES[0]) {
299 filename_utf8 = locale_to_utf8(filename);
300 error = mpdclient_cmd_delete_playlist(c, filename_utf8);
301 g_free(filename_utf8);
303 if (error) {
304 g_free(filename);
305 return -1;
306 }
308 error = playlist_save(c, filename, NULL);
309 g_free(filename);
310 return error;
311 }
313 screen_status_printf(_("Aborted"));
314 }
316 g_free(filename);
317 return -1;
318 }
320 /* success */
321 screen_status_printf(_("Saved %s"), filename);
322 g_free(filename);
323 return 0;
324 }
326 #ifndef NCMPC_MINI
327 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
328 GList **list, mpdclient_t *c)
329 {
330 g_completion_remove_items(gcmp, *list);
331 *list = string_list_remove(*list, dir);
332 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
333 g_completion_add_items(gcmp, *list);
334 *dir_list = g_list_append(*dir_list, g_strdup(dir));
335 }
337 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
338 {
339 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
340 GList **dir_list = tmp->dir_list;
341 GList **list = tmp->list;
342 mpdclient_t *c = tmp->c;
344 if (*list == NULL) {
345 /* create initial list */
346 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
347 g_completion_add_items(gcmp, *list);
348 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
349 string_list_find(*dir_list, line) == NULL) {
350 /* add directory content to list */
351 add_dir(gcmp, line, dir_list, list, c);
352 }
353 }
355 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
356 GList *items, void *data)
357 {
358 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
359 GList **dir_list = tmp->dir_list;
360 GList **list = tmp->list;
361 mpdclient_t *c = tmp->c;
363 if (g_list_length(items) >= 1)
364 screen_display_completion_list(items);
366 if (line && line[0] && line[strlen(line) - 1] == '/' &&
367 string_list_find(*dir_list, line) == NULL) {
368 /* add directory content to list */
369 add_dir(gcmp, line, dir_list, list, c);
370 }
371 }
372 #endif
374 static int
375 handle_add_to_playlist(mpdclient_t *c)
376 {
377 gchar *path;
378 #ifndef NCMPC_MINI
379 GCompletion *gcmp;
380 GList *list = NULL;
381 GList *dir_list = NULL;
382 completion_callback_data_t data;
384 /* initialize completion support */
385 gcmp = g_completion_new(NULL);
386 g_completion_set_compare(gcmp, completion_strncmp);
387 data.list = &list;
388 data.dir_list = &dir_list;
389 data.c = c;
390 wrln_completion_callback_data = &data;
391 wrln_pre_completion_callback = add_pre_completion_cb;
392 wrln_post_completion_callback = add_post_completion_cb;
393 #endif
395 /* get path */
396 path = screen_readln(screen.status_window.w,
397 _("Add"),
398 NULL,
399 NULL,
400 #ifdef NCMPC_MINI
401 NULL
402 #else
403 gcmp
404 #endif
405 );
407 /* destroy completion data */
408 #ifndef NCMPC_MINI
409 wrln_completion_callback_data = NULL;
410 wrln_pre_completion_callback = NULL;
411 wrln_post_completion_callback = NULL;
412 g_completion_free(gcmp);
413 string_list_free(list);
414 string_list_free(dir_list);
415 #endif
417 /* add the path to the playlist */
418 if (path != NULL) {
419 char *path_utf8 = locale_to_utf8(path);
420 mpdclient_cmd_add_path(c, path_utf8);
421 g_free(path_utf8);
422 }
424 g_free(path);
425 return 0;
426 }
428 static void
429 play_init(WINDOW *w, int cols, int rows)
430 {
431 lw = list_window_init(w, cols, rows);
432 }
434 static gboolean
435 timer_hide_cursor(gpointer data)
436 {
437 struct mpdclient *c = data;
439 assert(options.hide_cursor > 0);
440 assert(timer_hide_cursor_id != 0);
442 timer_hide_cursor_id = 0;
444 /* hide the cursor when mpd is playing and the user is inactive */
446 if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
447 lw->hide_cursor = true;
448 playlist_repaint();
449 } else
450 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
451 timer_hide_cursor, c);
453 return FALSE;
454 }
456 static void
457 play_open(mpdclient_t *c)
458 {
459 static gboolean install_cb = TRUE;
461 playlist = &c->playlist;
463 assert(timer_hide_cursor_id == 0);
464 if (options.hide_cursor > 0) {
465 lw->hide_cursor = false;
466 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
467 timer_hide_cursor, c);
468 }
470 if (install_cb) {
471 mpdclient_install_playlist_callback(c, playlist_changed_callback);
472 install_cb = FALSE;
473 }
474 }
476 static void
477 play_close(void)
478 {
479 if (timer_hide_cursor_id != 0) {
480 g_source_remove(timer_hide_cursor_id);
481 timer_hide_cursor_id = 0;
482 }
483 }
485 static void
486 play_resize(int cols, int rows)
487 {
488 lw->cols = cols;
489 lw->rows = rows;
490 }
493 static void
494 play_exit(void)
495 {
496 list_window_free(lw);
497 }
499 static const char *
500 play_title(char *str, size_t size)
501 {
502 if( strcmp(options.host, "localhost") == 0 )
503 return _("Playlist");
505 g_snprintf(str, size, _("Playlist on %s"), options.host);
506 return str;
507 }
509 static void
510 play_paint(void)
511 {
512 list_window_paint(lw, list_callback, NULL);
513 }
515 static void
516 play_update(mpdclient_t *c)
517 {
518 static int prev_song_id = -1;
520 current_song_id = c->song != NULL && c->status != NULL &&
521 !IS_STOPPED(c->status->state) ? c->song->id : -1;
523 if (current_song_id != prev_song_id) {
524 prev_song_id = current_song_id;
526 /* center the cursor */
527 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
528 center_playing_item(c, false);
530 playlist_repaint();
531 #ifndef NCMPC_MINI
532 } else if (options.scroll) {
533 /* always repaint if horizontal scrolling is
534 enabled */
535 playlist_repaint();
536 #endif
537 }
538 }
540 #ifdef HAVE_GETMOUSE
541 static bool
542 handle_mouse_event(struct mpdclient *c)
543 {
544 int row;
545 unsigned selected;
546 unsigned long bstate;
548 if (screen_get_mouse_event(c, &bstate, &row) ||
549 list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
550 playlist_repaint();
551 return true;
552 }
554 if (bstate & BUTTON1_DOUBLE_CLICKED) {
555 /* stop */
556 screen_cmd(c, CMD_STOP);
557 return true;
558 }
560 selected = lw->start + row;
562 if (bstate & BUTTON1_CLICKED) {
563 /* play */
564 if (lw->start + row < playlist_length(playlist))
565 mpdclient_cmd_play(c, lw->start + row);
566 } else if (bstate & BUTTON3_CLICKED) {
567 /* delete */
568 if (selected == lw->selected)
569 mpdclient_cmd_delete(c, lw->selected);
570 }
572 lw->selected = selected;
573 list_window_check_selected(lw, playlist_length(playlist));
574 playlist_repaint();
576 return true;
577 }
578 #endif
580 static bool
581 play_cmd(mpdclient_t *c, command_t cmd)
582 {
583 static command_t cached_cmd = CMD_NONE;
584 command_t prev_cmd = cached_cmd;
585 cached_cmd = cmd;
587 lw->hide_cursor = false;
589 if (options.hide_cursor > 0) {
590 if (timer_hide_cursor_id != 0)
591 g_source_remove(timer_hide_cursor_id);
592 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
593 timer_hide_cursor, c);
594 }
596 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
597 playlist_repaint();
598 return true;
599 }
601 switch(cmd) {
602 case CMD_PLAY:
603 mpdclient_cmd_play(c, lw->selected);
604 return true;
605 case CMD_DELETE:
606 {
607 int i = lw->selected_end, start = lw->selected_start;
608 for(; i >= start; --i)
609 mpdclient_cmd_delete(c, i);
611 i++;
612 if(i >= (int)playlist_length(&c->playlist))
613 i--;
614 lw->selected = i;
615 lw->selected_start = i;
616 lw->selected_end = i;
617 lw->range_selection = false;
619 return true;
620 }
621 case CMD_SAVE_PLAYLIST:
622 playlist_save(c, NULL, NULL);
623 return true;
624 case CMD_ADD:
625 handle_add_to_playlist(c);
626 return true;
627 case CMD_SCREEN_UPDATE:
628 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
629 playlist_repaint();
630 return false;
631 case CMD_SHUFFLE:
632 {
633 if(!lw->range_selection)
634 /* No range selection, shuffle all list. */
635 break;
637 if (mpdclient_cmd_shuffle_range(c, lw->selected_start, lw->selected_end+1) == 0)
638 screen_status_message(_("Shuffled playlist"));
640 return true;
641 }
642 case CMD_LIST_MOVE_UP:
643 if(lw->selected_start == 0)
644 return false;
645 if(lw->range_selection)
646 {
647 unsigned i = lw->selected_start;
648 unsigned last_selected = lw->selected;
649 for(; i <= lw->selected_end; ++i)
650 mpdclient_cmd_move(c, i, i-1);
651 lw->selected_start--;
652 lw->selected_end--;
653 lw->selected = last_selected - 1;
654 lw->range_base--;
655 }
656 else
657 {
658 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
659 lw->selected--;
660 lw->selected_start--;
661 lw->selected_end--;
662 }
663 return true;
664 case CMD_LIST_MOVE_DOWN:
665 if(lw->selected_end+1 >= playlist_length(&c->playlist))
666 return false;
667 if(lw->range_selection)
668 {
669 int i = lw->selected_end;
670 unsigned last_selected = lw->selected;
671 for(; i >= (int)lw->selected_start; --i)
672 mpdclient_cmd_move(c, i, i+1);
673 lw->selected_start++;
674 lw->selected_end++;
675 lw->selected = last_selected + 1;
676 lw->range_base++;
677 }
678 else
679 {
680 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
681 lw->selected++;
682 lw->selected_start++;
683 lw->selected_end++;
684 }
685 return true;
686 case CMD_LIST_FIND:
687 case CMD_LIST_RFIND:
688 case CMD_LIST_FIND_NEXT:
689 case CMD_LIST_RFIND_NEXT:
690 screen_find(lw, playlist_length(&c->playlist),
691 cmd, list_callback, NULL);
692 playlist_repaint();
693 return true;
694 case CMD_LIST_JUMP:
695 screen_jump(lw, list_callback, NULL);
696 playlist_repaint();
697 return true;
699 #ifdef HAVE_GETMOUSE
700 case CMD_MOUSE_EVENT:
701 return handle_mouse_event(c);
702 #endif
704 #ifdef ENABLE_SONG_SCREEN
705 case CMD_SCREEN_SONG:
706 if (lw->selected < playlist_length(&c->playlist)) {
707 screen_song_switch(c, playlist_get(&c->playlist, lw->selected));
708 return true;
709 }
711 break;
712 #endif
714 case CMD_LOCATE:
715 if (lw->selected < playlist_length(&c->playlist)) {
716 screen_file_goto_song(c, playlist_get(&c->playlist, lw->selected));
717 return true;
718 }
720 break;
722 #ifdef ENABLE_LYRICS_SCREEN
723 case CMD_SCREEN_LYRICS:
724 if (lw->selected < playlist_length(&c->playlist)) {
725 screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
726 return true;
727 }
729 break;
730 #endif
731 case CMD_SCREEN_SWAP:
732 screen_swap(c, playlist_get(&c->playlist, lw->selected));
733 return true;
735 default:
736 break;
737 }
739 return false;
740 }
742 const struct screen_functions screen_playlist = {
743 .init = play_init,
744 .exit = play_exit,
745 .open = play_open,
746 .close = play_close,
747 .resize = play_resize,
748 .paint = play_paint,
749 .update = play_update,
750 .cmd = play_cmd,
751 .get_title = play_title,
752 };