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 <mpd/client.h>
40 #include <ctype.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <glib.h>
46 #define MAX_SONG_LENGTH 512
48 #ifndef NCMPC_MINI
49 typedef struct
50 {
51 GList **list;
52 GList **dir_list;
53 struct mpdclient *c;
54 } completion_callback_data_t;
56 static bool must_scroll;
57 #endif
59 static struct mpdclient_playlist *playlist;
60 static int current_song_id = -1;
61 static list_window_t *lw = NULL;
62 static guint timer_hide_cursor_id;
64 static void
65 play_paint(void);
67 static void
68 playlist_repaint(void)
69 {
70 play_paint();
71 wrefresh(lw->w);
72 }
74 static void
75 playlist_repaint_if_active(void)
76 {
77 if (screen_is_visible(&screen_playlist))
78 playlist_repaint();
79 }
81 static void
82 playlist_changed_callback(struct mpdclient *c, int event, gpointer data)
83 {
84 switch (event) {
85 case PLAYLIST_EVENT_DELETE:
86 break;
87 case PLAYLIST_EVENT_MOVE:
88 if(lw->range_selection < 0)
89 {
90 lw->selected = *((int *) data);
91 if (lw->selected < lw->start)
92 lw->start--;
93 }
94 break;
95 default:
96 break;
97 }
99 list_window_check_selected(lw, c->playlist.list->len);
100 playlist_repaint_if_active();
101 }
103 #ifndef NCMPC_MINI
104 static char *
105 format_duration(unsigned duration)
106 {
107 if (duration == 0)
108 return NULL;
110 return g_strdup_printf("%d:%02d", duration / 60, duration % 60);
111 }
112 #endif
114 static const char *
115 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
116 {
117 static char songname[MAX_SONG_LENGTH];
118 #ifndef NCMPC_MINI
119 static scroll_state_t st;
120 #endif
121 struct mpd_song *song;
123 if (playlist == NULL || idx >= playlist_length(playlist))
124 return NULL;
126 song = playlist_get(playlist, idx);
127 if ((int)mpd_song_get_id(song) == current_song_id)
128 *highlight = true;
130 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
132 #ifndef NCMPC_MINI
133 if(second_column)
134 *second_column = format_duration(mpd_song_get_duration(song));
136 if (idx == lw->selected)
137 {
138 int second_column_len = 0;
139 if (second_column != NULL && *second_column != NULL)
140 second_column_len = strlen(*second_column);
141 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
142 {
143 static unsigned current_song;
144 char *tmp;
146 must_scroll = true;
148 if (current_song != lw->selected) {
149 st.offset = 0;
150 current_song = lw->selected;
151 }
153 tmp = strscroll(songname, options.scroll_sep,
154 MAX_SONG_LENGTH, &st);
155 g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
156 g_free(tmp);
157 }
158 else
159 st.offset = 0;
160 }
161 #else
162 (void)second_column;
163 #endif
165 return songname;
166 }
168 static void
169 center_playing_item(struct mpdclient *c, bool center_cursor)
170 {
171 unsigned length = c->playlist.list->len;
172 int idx;
174 if (!c->song || c->status == NULL ||
175 IS_STOPPED(mpd_status_get_state(c->status)))
176 return;
178 /* try to center the song that are playing */
179 idx = playlist_get_index(c, c->song);
180 if (idx < 0)
181 return;
183 if (length < lw->rows)
184 {
185 if (center_cursor)
186 list_window_set_selected(lw, idx);
187 return;
188 }
190 list_window_center(lw, length, idx);
192 if (center_cursor) {
193 list_window_set_selected(lw, idx);
194 return;
195 }
197 /* make sure the cursor is in the window */
198 if (lw->selected < lw->start + options.scroll_offset) {
199 if (lw->start > 0)
200 lw->selected = lw->start + options.scroll_offset;
201 if (lw->range_selection) {
202 lw->selected_start = lw->range_base;
203 lw->selected_end = lw->selected;
204 } else {
205 lw->selected_start = lw->selected;
206 lw->selected_end = lw->selected;
207 }
208 } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
209 if (lw->start + lw->rows < length)
210 lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
211 if (lw->range_selection) {
212 lw->selected_start = lw->selected;
213 lw->selected_end = lw->range_base;
214 } else {
215 lw->selected_start = lw->selected;
216 lw->selected_end = lw->selected;
217 }
218 }
219 }
221 #ifndef NCMPC_MINI
222 static void
223 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
224 void *data)
225 {
226 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
227 GList **list = tmp->list;
228 struct mpdclient *c = tmp->c;
230 if( *list == NULL ) {
231 /* create completion list */
232 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
233 g_completion_add_items(gcmp, *list);
234 }
235 }
237 static void
238 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
239 G_GNUC_UNUSED gchar *line, GList *items,
240 G_GNUC_UNUSED void *data)
241 {
242 if (g_list_length(items) >= 1)
243 screen_display_completion_list(items);
244 }
245 #endif
247 #ifndef NCMPC_MINI
248 /**
249 * Wrapper for strncmp(). We are not allowed to pass &strncmp to
250 * g_completion_set_compare(), because strncmp() takes size_t where
251 * g_completion_set_compare passes a gsize value.
252 */
253 static gint
254 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
255 {
256 return strncmp(s1, s2, n);
257 }
258 #endif
260 int
261 playlist_save(struct mpdclient *c, char *name, char *defaultname)
262 {
263 gchar *filename, *filename_utf8;
264 gint error;
265 #ifndef NCMPC_MINI
266 GCompletion *gcmp;
267 GList *list = NULL;
268 completion_callback_data_t data;
269 #endif
271 #ifdef NCMPC_MINI
272 (void)defaultname;
273 #endif
275 #ifndef NCMPC_MINI
276 if (name == NULL) {
277 /* initialize completion support */
278 gcmp = g_completion_new(NULL);
279 g_completion_set_compare(gcmp, completion_strncmp);
280 data.list = &list;
281 data.dir_list = NULL;
282 data.c = c;
283 wrln_completion_callback_data = &data;
284 wrln_pre_completion_callback = save_pre_completion_cb;
285 wrln_post_completion_callback = save_post_completion_cb;
288 /* query the user for a filename */
289 filename = screen_readln(screen.status_window.w,
290 _("Save playlist as"),
291 defaultname,
292 NULL,
293 gcmp);
295 /* destroy completion support */
296 wrln_completion_callback_data = NULL;
297 wrln_pre_completion_callback = NULL;
298 wrln_post_completion_callback = NULL;
299 g_completion_free(gcmp);
300 list = string_list_free(list);
301 if( filename )
302 filename=g_strstrip(filename);
303 } else
304 #endif
305 filename=g_strdup(name);
307 if (filename == NULL)
308 return -1;
310 /* send save command to mpd */
312 filename_utf8 = locale_to_utf8(filename);
313 error = mpdclient_cmd_save_playlist(c, filename_utf8);
314 g_free(filename_utf8);
316 if (error) {
317 gint code = GET_ACK_ERROR_CODE(error);
319 if (code == MPD_SERVER_ERROR_EXIST) {
320 char *buf;
321 int key;
323 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
324 filename, YES, NO);
325 key = tolower(screen_getch(screen.status_window.w,
326 buf));
327 g_free(buf);
329 if (key == YES[0]) {
330 filename_utf8 = locale_to_utf8(filename);
331 error = mpdclient_cmd_delete_playlist(c, filename_utf8);
332 g_free(filename_utf8);
334 if (error) {
335 g_free(filename);
336 return -1;
337 }
339 error = playlist_save(c, filename, NULL);
340 g_free(filename);
341 return error;
342 }
344 screen_status_printf(_("Aborted"));
345 }
347 g_free(filename);
348 return -1;
349 }
351 /* success */
352 screen_status_printf(_("Saved %s"), filename);
353 g_free(filename);
354 return 0;
355 }
357 #ifndef NCMPC_MINI
358 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
359 GList **list, struct mpdclient *c)
360 {
361 g_completion_remove_items(gcmp, *list);
362 *list = string_list_remove(*list, dir);
363 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
364 g_completion_add_items(gcmp, *list);
365 *dir_list = g_list_append(*dir_list, g_strdup(dir));
366 }
368 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
369 {
370 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
371 GList **dir_list = tmp->dir_list;
372 GList **list = tmp->list;
373 struct mpdclient *c = tmp->c;
375 if (*list == NULL) {
376 /* create initial list */
377 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
378 g_completion_add_items(gcmp, *list);
379 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
380 string_list_find(*dir_list, line) == NULL) {
381 /* add directory content to list */
382 add_dir(gcmp, line, dir_list, list, c);
383 }
384 }
386 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
387 GList *items, void *data)
388 {
389 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
390 GList **dir_list = tmp->dir_list;
391 GList **list = tmp->list;
392 struct mpdclient *c = tmp->c;
394 if (g_list_length(items) >= 1)
395 screen_display_completion_list(items);
397 if (line && line[0] && line[strlen(line) - 1] == '/' &&
398 string_list_find(*dir_list, line) == NULL) {
399 /* add directory content to list */
400 add_dir(gcmp, line, dir_list, list, c);
401 }
402 }
403 #endif
405 static int
406 handle_add_to_playlist(struct mpdclient *c)
407 {
408 gchar *path;
409 #ifndef NCMPC_MINI
410 GCompletion *gcmp;
411 GList *list = NULL;
412 GList *dir_list = NULL;
413 completion_callback_data_t data;
415 /* initialize completion support */
416 gcmp = g_completion_new(NULL);
417 g_completion_set_compare(gcmp, completion_strncmp);
418 data.list = &list;
419 data.dir_list = &dir_list;
420 data.c = c;
421 wrln_completion_callback_data = &data;
422 wrln_pre_completion_callback = add_pre_completion_cb;
423 wrln_post_completion_callback = add_post_completion_cb;
424 #endif
426 /* get path */
427 path = screen_readln(screen.status_window.w,
428 _("Add"),
429 NULL,
430 NULL,
431 #ifdef NCMPC_MINI
432 NULL
433 #else
434 gcmp
435 #endif
436 );
438 /* destroy completion data */
439 #ifndef NCMPC_MINI
440 wrln_completion_callback_data = NULL;
441 wrln_pre_completion_callback = NULL;
442 wrln_post_completion_callback = NULL;
443 g_completion_free(gcmp);
444 string_list_free(list);
445 string_list_free(dir_list);
446 #endif
448 /* add the path to the playlist */
449 if (path != NULL) {
450 char *path_utf8 = locale_to_utf8(path);
451 mpdclient_cmd_add_path(c, path_utf8);
452 g_free(path_utf8);
453 }
455 g_free(path);
456 return 0;
457 }
459 static void
460 play_init(WINDOW *w, int cols, int rows)
461 {
462 lw = list_window_init(w, cols, rows);
463 }
465 static gboolean
466 timer_hide_cursor(gpointer data)
467 {
468 struct mpdclient *c = data;
470 assert(options.hide_cursor > 0);
471 assert(timer_hide_cursor_id != 0);
473 timer_hide_cursor_id = 0;
475 /* hide the cursor when mpd is playing and the user is inactive */
477 if (c->status != NULL &&
478 mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
479 lw->hide_cursor = true;
480 playlist_repaint();
481 } else
482 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
483 timer_hide_cursor, c);
485 return FALSE;
486 }
488 static void
489 play_open(struct mpdclient *c)
490 {
491 static gboolean install_cb = TRUE;
493 playlist = &c->playlist;
495 assert(timer_hide_cursor_id == 0);
496 if (options.hide_cursor > 0) {
497 lw->hide_cursor = false;
498 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
499 timer_hide_cursor, c);
500 }
502 if (install_cb) {
503 mpdclient_install_playlist_callback(c, playlist_changed_callback);
504 install_cb = FALSE;
505 }
506 }
508 static void
509 play_close(void)
510 {
511 if (timer_hide_cursor_id != 0) {
512 g_source_remove(timer_hide_cursor_id);
513 timer_hide_cursor_id = 0;
514 }
515 }
517 static void
518 play_resize(int cols, int rows)
519 {
520 lw->cols = cols;
521 lw->rows = rows;
522 }
525 static void
526 play_exit(void)
527 {
528 list_window_free(lw);
529 }
531 static const char *
532 play_title(char *str, size_t size)
533 {
534 if (options.host == NULL)
535 return _("Playlist");
537 g_snprintf(str, size, _("Playlist on %s"), options.host);
538 return str;
539 }
541 static void
542 play_paint(void)
543 {
544 #ifndef NCMPC_MINI
545 must_scroll = false;
546 #endif
548 list_window_paint(lw, list_callback, NULL);
549 }
551 static void
552 play_update(struct mpdclient *c)
553 {
554 static int prev_song_id = -1;
556 current_song_id = c->song != NULL && c->status != NULL &&
557 !IS_STOPPED(mpd_status_get_state(c->status))
558 ? (int)mpd_song_get_id(c->song) : -1;
560 if (current_song_id != prev_song_id) {
561 prev_song_id = current_song_id;
563 /* center the cursor */
564 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
565 center_playing_item(c, false);
567 playlist_repaint();
568 #ifndef NCMPC_MINI
569 } else if (options.scroll && must_scroll) {
570 /* always repaint if horizontal scrolling is
571 enabled */
572 playlist_repaint();
573 #endif
574 }
575 }
577 #ifdef HAVE_GETMOUSE
578 static bool
579 handle_mouse_event(struct mpdclient *c)
580 {
581 int row;
582 unsigned selected;
583 unsigned long bstate;
585 if (screen_get_mouse_event(c, &bstate, &row) ||
586 list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
587 playlist_repaint();
588 return true;
589 }
591 if (bstate & BUTTON1_DOUBLE_CLICKED) {
592 /* stop */
593 screen_cmd(c, CMD_STOP);
594 return true;
595 }
597 selected = lw->start + row;
599 if (bstate & BUTTON1_CLICKED) {
600 /* play */
601 if (lw->start + row < playlist_length(playlist))
602 mpdclient_cmd_play(c, lw->start + row);
603 } else if (bstate & BUTTON3_CLICKED) {
604 /* delete */
605 if (selected == lw->selected)
606 mpdclient_cmd_delete(c, lw->selected);
607 }
609 lw->selected = selected;
610 list_window_check_selected(lw, playlist_length(playlist));
611 playlist_repaint();
613 return true;
614 }
615 #endif
617 static bool
618 play_cmd(struct mpdclient *c, command_t cmd)
619 {
620 static command_t cached_cmd = CMD_NONE;
621 command_t prev_cmd = cached_cmd;
622 cached_cmd = cmd;
624 lw->hide_cursor = false;
626 if (options.hide_cursor > 0) {
627 if (timer_hide_cursor_id != 0)
628 g_source_remove(timer_hide_cursor_id);
629 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
630 timer_hide_cursor, c);
631 }
633 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
634 playlist_repaint();
635 return true;
636 }
638 switch(cmd) {
639 case CMD_PLAY:
640 mpdclient_cmd_play(c, lw->selected);
641 return true;
642 case CMD_DELETE:
643 {
644 int i = lw->selected_end, start = lw->selected_start;
645 for(; i >= start; --i)
646 mpdclient_cmd_delete(c, i);
648 i++;
649 if(i >= (int)playlist_length(&c->playlist))
650 i--;
651 lw->selected = i;
652 lw->selected_start = i;
653 lw->selected_end = i;
654 lw->range_selection = false;
656 return true;
657 }
658 case CMD_SAVE_PLAYLIST:
659 playlist_save(c, NULL, NULL);
660 return true;
661 case CMD_ADD:
662 handle_add_to_playlist(c);
663 return true;
664 case CMD_SCREEN_UPDATE:
665 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
666 playlist_repaint();
667 return false;
668 case CMD_SELECT_PLAYING:
669 list_window_set_selected(lw, playlist_get_index(c, c->song));
670 return true;
671 case CMD_SHUFFLE:
672 {
673 if(!lw->range_selection)
674 /* No range selection, shuffle all list. */
675 break;
677 if (mpdclient_cmd_shuffle_range(c, lw->selected_start, lw->selected_end+1) == 0)
678 screen_status_message(_("Shuffled playlist"));
680 return true;
681 }
682 case CMD_LIST_MOVE_UP:
683 if(lw->selected_start == 0)
684 return false;
685 if(lw->range_selection)
686 {
687 unsigned i = lw->selected_start;
688 unsigned last_selected = lw->selected;
689 for(; i <= lw->selected_end; ++i)
690 mpdclient_cmd_move(c, i, i-1);
691 lw->selected_start--;
692 lw->selected_end--;
693 lw->selected = last_selected - 1;
694 lw->range_base--;
695 }
696 else
697 {
698 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
699 lw->selected--;
700 lw->selected_start--;
701 lw->selected_end--;
702 }
703 return true;
704 case CMD_LIST_MOVE_DOWN:
705 if(lw->selected_end+1 >= playlist_length(&c->playlist))
706 return false;
707 if(lw->range_selection)
708 {
709 int i = lw->selected_end;
710 unsigned last_selected = lw->selected;
711 for(; i >= (int)lw->selected_start; --i)
712 mpdclient_cmd_move(c, i, i+1);
713 lw->selected_start++;
714 lw->selected_end++;
715 lw->selected = last_selected + 1;
716 lw->range_base++;
717 }
718 else
719 {
720 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
721 lw->selected++;
722 lw->selected_start++;
723 lw->selected_end++;
724 }
725 return true;
726 case CMD_LIST_FIND:
727 case CMD_LIST_RFIND:
728 case CMD_LIST_FIND_NEXT:
729 case CMD_LIST_RFIND_NEXT:
730 screen_find(lw, playlist_length(&c->playlist),
731 cmd, list_callback, NULL);
732 playlist_repaint();
733 return true;
734 case CMD_LIST_JUMP:
735 screen_jump(lw, list_callback, NULL);
736 playlist_repaint();
737 return true;
739 #ifdef HAVE_GETMOUSE
740 case CMD_MOUSE_EVENT:
741 return handle_mouse_event(c);
742 #endif
744 #ifdef ENABLE_SONG_SCREEN
745 case CMD_SCREEN_SONG:
746 if (lw->selected < playlist_length(&c->playlist)) {
747 screen_song_switch(c, playlist_get(&c->playlist, lw->selected));
748 return true;
749 }
751 break;
752 #endif
754 case CMD_LOCATE:
755 if (lw->selected < playlist_length(&c->playlist)) {
756 screen_file_goto_song(c, playlist_get(&c->playlist, lw->selected));
757 return true;
758 }
760 break;
762 #ifdef ENABLE_LYRICS_SCREEN
763 case CMD_SCREEN_LYRICS:
764 if (lw->selected < playlist_length(&c->playlist)) {
765 struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
766 bool follow = false;
768 if (c->song && selected &&
769 !strcmp(mpd_song_get_uri(selected),
770 mpd_song_get_uri(c->song)))
771 follow = true;
773 screen_lyrics_switch(c, selected, follow);
774 return true;
775 }
777 break;
778 #endif
779 case CMD_SCREEN_SWAP:
780 screen_swap(c, playlist_get(&c->playlist, lw->selected));
781 return true;
783 default:
784 break;
785 }
787 return false;
788 }
790 const struct screen_functions screen_playlist = {
791 .init = play_init,
792 .exit = play_exit,
793 .open = play_open,
794 .close = play_close,
795 .resize = play_resize,
796 .paint = play_paint,
797 .update = play_update,
798 .cmd = play_cmd,
799 .get_title = play_title,
800 };