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 "screen_play.h"
21 #include "screen_interface.h"
22 #include "screen_file.h"
23 #include "screen_message.h"
24 #include "screen_find.h"
25 #include "config.h"
26 #include "i18n.h"
27 #include "charset.h"
28 #include "options.h"
29 #include "mpdclient.h"
30 #include "utils.h"
31 #include "strfsong.h"
32 #include "wreadln.h"
33 #include "colors.h"
34 #include "screen.h"
35 #include "screen_utils.h"
36 #include "screen_song.h"
37 #include "screen_lyrics.h"
39 #ifndef NCMPC_MINI
40 #include "hscroll.h"
41 #endif
43 #include <mpd/client.h>
45 #include <ctype.h>
46 #include <string.h>
47 #include <glib.h>
49 #define MAX_SONG_LENGTH 512
51 #ifndef NCMPC_MINI
52 typedef struct
53 {
54 GList **list;
55 GList **dir_list;
56 struct mpdclient *c;
57 } completion_callback_data_t;
59 static bool must_scroll;
60 #endif
62 static struct mpdclient_playlist *playlist;
63 static int current_song_id = -1;
64 static int selected_song_id = -1;
65 static list_window_t *lw = NULL;
66 static guint timer_hide_cursor_id;
68 static void
69 screen_playlist_paint(void);
71 static void
72 playlist_repaint(void)
73 {
74 screen_playlist_paint();
75 wrefresh(lw->w);
76 }
78 static const struct mpd_song *
79 playlist_selected_song(void)
80 {
81 return !lw->range_selection &&
82 lw->selected < playlist_length(playlist)
83 ? playlist_get(playlist, lw->selected)
84 : NULL;
85 }
87 static void
88 playlist_save_selection(void)
89 {
90 selected_song_id = playlist_selected_song() != NULL
91 ? (int)mpd_song_get_id(playlist_selected_song())
92 : -1;
93 }
95 static void
96 playlist_restore_selection(void)
97 {
98 const struct mpd_song *song;
99 int pos;
101 if (selected_song_id < 0)
102 /* there was no selection */
103 return;
105 song = playlist_selected_song();
106 if (song != NULL &&
107 mpd_song_get_id(song) == (unsigned)selected_song_id)
108 /* selection is still valid */
109 return;
111 pos = playlist_get_index_from_id(playlist, selected_song_id);
112 if (pos >= 0)
113 lw->selected = pos;
115 list_window_check_selected(lw, playlist_length(playlist));
116 playlist_save_selection();
117 }
119 static const char *
120 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
121 {
122 static char songname[MAX_SONG_LENGTH];
123 #ifndef NCMPC_MINI
124 static scroll_state_t st;
125 #endif
126 struct mpd_song *song;
128 if (playlist == NULL || idx >= playlist_length(playlist))
129 return NULL;
131 song = playlist_get(playlist, idx);
132 if ((int)mpd_song_get_id(song) == current_song_id)
133 *highlight = true;
135 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
137 #ifndef NCMPC_MINI
138 if (second_column != NULL && mpd_song_get_duration(song) > 0) {
139 char duration[32];
140 format_duration_short(duration, sizeof(duration),
141 mpd_song_get_duration(song));
142 *second_column = g_strdup(duration);
143 }
145 if (idx == lw->selected)
146 {
147 int second_column_len = 0;
148 if (second_column != NULL && *second_column != NULL)
149 second_column_len = strlen(*second_column);
150 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
151 {
152 static unsigned current_song;
153 char *tmp;
155 must_scroll = true;
157 if (current_song != lw->selected) {
158 st.offset = 0;
159 current_song = lw->selected;
160 }
162 tmp = strscroll(songname, options.scroll_sep,
163 MAX_SONG_LENGTH, &st);
164 g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
165 g_free(tmp);
166 }
167 else
168 st.offset = 0;
169 }
170 #else
171 (void)second_column;
172 #endif
174 return songname;
175 }
177 static void
178 center_playing_item(struct mpdclient *c, bool center_cursor)
179 {
180 const struct mpd_song *song;
181 unsigned length = c->playlist.list->len;
182 int idx;
184 song = mpdclient_get_current_song(c);
185 if (song == NULL)
186 return;
188 /* try to center the song that are playing */
189 idx = playlist_get_index(&c->playlist, c->song);
190 if (idx < 0)
191 return;
193 if (length < lw->rows)
194 {
195 if (center_cursor)
196 list_window_set_selected(lw, idx);
197 return;
198 }
200 list_window_center(lw, length, idx);
202 if (center_cursor) {
203 list_window_set_selected(lw, idx);
204 return;
205 }
207 /* make sure the cursor is in the window */
208 if (lw->selected < lw->start + options.scroll_offset) {
209 if (lw->start > 0)
210 lw->selected = lw->start + options.scroll_offset;
211 if (lw->range_selection) {
212 lw->selected_start = lw->range_base;
213 lw->selected_end = lw->selected;
214 } else {
215 lw->selected_start = lw->selected;
216 lw->selected_end = lw->selected;
217 }
218 } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
219 if (lw->start + lw->rows < length)
220 lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
221 if (lw->range_selection) {
222 lw->selected_start = lw->selected;
223 lw->selected_end = lw->range_base;
224 } else {
225 lw->selected_start = lw->selected;
226 lw->selected_end = lw->selected;
227 }
228 }
229 }
231 #ifndef NCMPC_MINI
232 static void
233 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
234 void *data)
235 {
236 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
237 GList **list = tmp->list;
238 struct mpdclient *c = tmp->c;
240 if( *list == NULL ) {
241 /* create completion list */
242 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
243 g_completion_add_items(gcmp, *list);
244 }
245 }
247 static void
248 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
249 G_GNUC_UNUSED gchar *line, GList *items,
250 G_GNUC_UNUSED void *data)
251 {
252 if (g_list_length(items) >= 1)
253 screen_display_completion_list(items);
254 }
255 #endif
257 #ifndef NCMPC_MINI
258 /**
259 * Wrapper for strncmp(). We are not allowed to pass &strncmp to
260 * g_completion_set_compare(), because strncmp() takes size_t where
261 * g_completion_set_compare passes a gsize value.
262 */
263 static gint
264 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
265 {
266 return strncmp(s1, s2, n);
267 }
268 #endif
270 int
271 playlist_save(struct mpdclient *c, char *name, char *defaultname)
272 {
273 gchar *filename, *filename_utf8;
274 #ifndef NCMPC_MINI
275 GCompletion *gcmp;
276 GList *list = NULL;
277 completion_callback_data_t data;
278 #endif
280 #ifdef NCMPC_MINI
281 (void)defaultname;
282 #endif
284 #ifndef NCMPC_MINI
285 if (name == NULL) {
286 /* initialize completion support */
287 gcmp = g_completion_new(NULL);
288 g_completion_set_compare(gcmp, completion_strncmp);
289 data.list = &list;
290 data.dir_list = NULL;
291 data.c = c;
292 wrln_completion_callback_data = &data;
293 wrln_pre_completion_callback = save_pre_completion_cb;
294 wrln_post_completion_callback = save_post_completion_cb;
297 /* query the user for a filename */
298 filename = screen_readln(_("Save playlist as"),
299 defaultname,
300 NULL,
301 gcmp);
303 /* destroy completion support */
304 wrln_completion_callback_data = NULL;
305 wrln_pre_completion_callback = NULL;
306 wrln_post_completion_callback = NULL;
307 g_completion_free(gcmp);
308 list = string_list_free(list);
309 if( filename )
310 filename=g_strstrip(filename);
311 } else
312 #endif
313 filename=g_strdup(name);
315 if (filename == NULL)
316 return -1;
318 /* send save command to mpd */
320 filename_utf8 = locale_to_utf8(filename);
322 if (!mpd_run_save(c->connection, filename_utf8)) {
323 if (mpd_connection_get_error(c->connection) == MPD_ERROR_SERVER &&
324 mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_EXIST &&
325 mpd_connection_clear_error(c->connection)) {
326 char *buf;
327 int key;
329 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
330 filename, YES, NO);
331 key = tolower(screen_getch(buf));
332 g_free(buf);
334 if (key != YES[0]) {
335 g_free(filename_utf8);
336 g_free(filename);
337 screen_status_printf(_("Aborted"));
338 return -1;
339 }
341 if (!mpd_run_rm(c->connection, filename_utf8) ||
342 !mpd_run_save(c->connection, filename_utf8)) {
343 mpdclient_handle_error(c);
344 g_free(filename_utf8);
345 g_free(filename);
346 return -1;
347 }
348 } else {
349 mpdclient_handle_error(c);
350 g_free(filename_utf8);
351 g_free(filename);
352 return -1;
353 }
354 }
356 c->events |= MPD_IDLE_STORED_PLAYLIST;
358 g_free(filename_utf8);
360 /* success */
361 screen_status_printf(_("Saved %s"), filename);
362 g_free(filename);
363 return 0;
364 }
366 #ifndef NCMPC_MINI
367 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
368 GList **list, struct mpdclient *c)
369 {
370 g_completion_remove_items(gcmp, *list);
371 *list = string_list_remove(*list, dir);
372 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
373 g_completion_add_items(gcmp, *list);
374 *dir_list = g_list_append(*dir_list, g_strdup(dir));
375 }
377 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
378 {
379 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
380 GList **dir_list = tmp->dir_list;
381 GList **list = tmp->list;
382 struct mpdclient *c = tmp->c;
384 if (*list == NULL) {
385 /* create initial list */
386 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
387 g_completion_add_items(gcmp, *list);
388 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
389 string_list_find(*dir_list, line) == NULL) {
390 /* add directory content to list */
391 add_dir(gcmp, line, dir_list, list, c);
392 }
393 }
395 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
396 GList *items, void *data)
397 {
398 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
399 GList **dir_list = tmp->dir_list;
400 GList **list = tmp->list;
401 struct mpdclient *c = tmp->c;
403 if (g_list_length(items) >= 1)
404 screen_display_completion_list(items);
406 if (line && line[0] && line[strlen(line) - 1] == '/' &&
407 string_list_find(*dir_list, line) == NULL) {
408 /* add directory content to list */
409 add_dir(gcmp, line, dir_list, list, c);
410 }
411 }
412 #endif
414 static int
415 handle_add_to_playlist(struct mpdclient *c)
416 {
417 gchar *path;
418 #ifndef NCMPC_MINI
419 GCompletion *gcmp;
420 GList *list = NULL;
421 GList *dir_list = NULL;
422 completion_callback_data_t data;
424 /* initialize completion support */
425 gcmp = g_completion_new(NULL);
426 g_completion_set_compare(gcmp, completion_strncmp);
427 data.list = &list;
428 data.dir_list = &dir_list;
429 data.c = c;
430 wrln_completion_callback_data = &data;
431 wrln_pre_completion_callback = add_pre_completion_cb;
432 wrln_post_completion_callback = add_post_completion_cb;
433 #endif
435 /* get path */
436 path = screen_readln(_("Add"),
437 NULL,
438 NULL,
439 #ifdef NCMPC_MINI
440 NULL
441 #else
442 gcmp
443 #endif
444 );
446 /* destroy completion data */
447 #ifndef NCMPC_MINI
448 wrln_completion_callback_data = NULL;
449 wrln_pre_completion_callback = NULL;
450 wrln_post_completion_callback = NULL;
451 g_completion_free(gcmp);
452 string_list_free(list);
453 string_list_free(dir_list);
454 #endif
456 /* add the path to the playlist */
457 if (path != NULL) {
458 char *path_utf8 = locale_to_utf8(path);
459 mpdclient_cmd_add_path(c, path_utf8);
460 g_free(path_utf8);
461 }
463 g_free(path);
464 return 0;
465 }
467 static void
468 screen_playlist_init(WINDOW *w, int cols, int rows)
469 {
470 lw = list_window_init(w, cols, rows);
471 }
473 static gboolean
474 timer_hide_cursor(gpointer data)
475 {
476 struct mpdclient *c = data;
478 assert(options.hide_cursor > 0);
479 assert(timer_hide_cursor_id != 0);
481 timer_hide_cursor_id = 0;
483 /* hide the cursor when mpd is playing and the user is inactive */
485 if (c->status != NULL &&
486 mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
487 lw->hide_cursor = true;
488 playlist_repaint();
489 } else
490 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
491 timer_hide_cursor, c);
493 return FALSE;
494 }
496 static void
497 screen_playlist_open(struct mpdclient *c)
498 {
499 playlist = &c->playlist;
501 assert(timer_hide_cursor_id == 0);
502 if (options.hide_cursor > 0) {
503 lw->hide_cursor = false;
504 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
505 timer_hide_cursor, c);
506 }
508 playlist_restore_selection();
509 }
511 static void
512 screen_playlist_close(void)
513 {
514 if (timer_hide_cursor_id != 0) {
515 g_source_remove(timer_hide_cursor_id);
516 timer_hide_cursor_id = 0;
517 }
518 }
520 static void
521 screen_playlist_resize(int cols, int rows)
522 {
523 lw->cols = cols;
524 lw->rows = rows;
525 }
528 static void
529 screen_playlist_exit(void)
530 {
531 list_window_free(lw);
532 }
534 static const char *
535 screen_playlist_title(char *str, size_t size)
536 {
537 if (options.host == NULL)
538 return _("Playlist");
540 g_snprintf(str, size, _("Playlist on %s"), options.host);
541 return str;
542 }
544 static void
545 screen_playlist_paint(void)
546 {
547 #ifndef NCMPC_MINI
548 must_scroll = false;
549 #endif
551 list_window_paint(lw, list_callback, NULL);
552 }
554 static void
555 screen_playlist_update(struct mpdclient *c)
556 {
557 static int prev_song_id = -1;
559 if (c->events & MPD_IDLE_PLAYLIST)
560 playlist_restore_selection();
562 current_song_id = c->status != NULL &&
563 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
564 mpd_status_get_state(c->status) == MPD_STATE_PAUSE)
565 ? (int)mpd_status_get_song_id(c->status) : -1;
567 if (current_song_id != prev_song_id) {
568 prev_song_id = current_song_id;
570 /* center the cursor */
571 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
572 center_playing_item(c, false);
574 playlist_repaint();
575 #ifndef NCMPC_MINI
576 } else if (options.scroll && must_scroll) {
577 /* always repaint if horizontal scrolling is
578 enabled */
579 playlist_repaint();
580 #endif
581 } else if (c->events & MPD_IDLE_PLAYLIST) {
582 /* the playlist has changed, we must paint the new
583 version */
584 playlist_repaint();
585 }
586 }
588 #ifdef HAVE_GETMOUSE
589 static bool
590 handle_mouse_event(struct mpdclient *c)
591 {
592 int row;
593 unsigned selected;
594 unsigned long bstate;
596 if (screen_get_mouse_event(c, &bstate, &row) ||
597 list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
598 playlist_repaint();
599 return true;
600 }
602 if (bstate & BUTTON1_DOUBLE_CLICKED) {
603 /* stop */
604 screen_cmd(c, CMD_STOP);
605 return true;
606 }
608 selected = lw->start + row;
610 if (bstate & BUTTON1_CLICKED) {
611 /* play */
612 if (lw->start + row < playlist_length(playlist))
613 mpdclient_cmd_play(c, lw->start + row);
614 } else if (bstate & BUTTON3_CLICKED) {
615 /* delete */
616 if (selected == lw->selected)
617 mpdclient_cmd_delete(c, lw->selected);
618 }
620 lw->selected = selected;
621 list_window_check_selected(lw, playlist_length(playlist));
622 playlist_save_selection();
623 playlist_repaint();
625 return true;
626 }
627 #endif
629 static bool
630 screen_playlist_cmd(struct mpdclient *c, command_t cmd)
631 {
632 static command_t cached_cmd = CMD_NONE;
633 command_t prev_cmd = cached_cmd;
634 cached_cmd = cmd;
636 lw->hide_cursor = false;
638 if (options.hide_cursor > 0) {
639 if (timer_hide_cursor_id != 0)
640 g_source_remove(timer_hide_cursor_id);
641 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
642 timer_hide_cursor, c);
643 }
645 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
646 playlist_save_selection();
647 playlist_repaint();
648 return true;
649 }
651 switch(cmd) {
652 case CMD_SCREEN_UPDATE:
653 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
654 playlist_repaint();
655 return false;
656 case CMD_SELECT_PLAYING:
657 list_window_set_selected(lw, playlist_get_index(&c->playlist,
658 c->song));
659 playlist_save_selection();
660 return true;
662 case CMD_LIST_FIND:
663 case CMD_LIST_RFIND:
664 case CMD_LIST_FIND_NEXT:
665 case CMD_LIST_RFIND_NEXT:
666 screen_find(lw, playlist_length(&c->playlist),
667 cmd, list_callback, NULL);
668 playlist_save_selection();
669 playlist_repaint();
670 return true;
671 case CMD_LIST_JUMP:
672 screen_jump(lw, list_callback, NULL);
673 playlist_save_selection();
674 playlist_repaint();
675 return true;
677 #ifdef HAVE_GETMOUSE
678 case CMD_MOUSE_EVENT:
679 return handle_mouse_event(c);
680 #endif
682 #ifdef ENABLE_SONG_SCREEN
683 case CMD_SCREEN_SONG:
684 if (playlist_selected_song()) {
685 screen_song_switch(c, playlist_selected_song());
686 return true;
687 }
689 break;
690 #endif
692 #ifdef ENABLE_LYRICS_SCREEN
693 case CMD_SCREEN_LYRICS:
694 if (lw->selected < playlist_length(&c->playlist)) {
695 struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
696 bool follow = false;
698 if (c->song && selected &&
699 !strcmp(mpd_song_get_uri(selected),
700 mpd_song_get_uri(c->song)))
701 follow = true;
703 screen_lyrics_switch(c, selected, follow);
704 return true;
705 }
707 break;
708 #endif
709 case CMD_SCREEN_SWAP:
710 screen_swap(c, playlist_get(&c->playlist, lw->selected));
711 return true;
713 default:
714 break;
715 }
717 if (!mpdclient_is_connected(c))
718 return false;
720 switch(cmd) {
721 case CMD_PLAY:
722 mpdclient_cmd_play(c, lw->selected);
723 return true;
725 case CMD_DELETE:
726 if (lw->range_selection) {
727 mpdclient_cmd_delete_range(c, lw->selected_start,
728 lw->selected_end + 1);
729 } else {
730 mpdclient_cmd_delete(c, lw->selected);
731 }
733 lw->selected = lw->selected_end = lw->selected_start;
734 lw->range_selection = false;
735 return true;
737 case CMD_SAVE_PLAYLIST:
738 playlist_save(c, NULL, NULL);
739 return true;
741 case CMD_ADD:
742 handle_add_to_playlist(c);
743 return true;
745 case CMD_SHUFFLE:
746 {
747 if(!lw->range_selection)
748 /* No range selection, shuffle all list. */
749 break;
751 if (mpd_run_shuffle_range(c->connection, lw->selected_start,
752 lw->selected_end + 1))
753 screen_status_message(_("Shuffled playlist"));
754 else
755 mpdclient_handle_error(c);
756 return true;
757 }
759 case CMD_LIST_MOVE_UP:
760 if(lw->selected_start == 0)
761 return false;
762 if(lw->range_selection)
763 {
764 unsigned i = lw->selected_start;
765 unsigned last_selected = lw->selected;
766 for(; i <= lw->selected_end; ++i)
767 mpdclient_cmd_move(c, i, i-1);
768 lw->selected_start--;
769 lw->selected_end--;
770 lw->selected = last_selected - 1;
771 lw->range_base--;
772 }
773 else
774 {
775 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
776 lw->selected--;
777 lw->selected_start--;
778 lw->selected_end--;
779 }
781 playlist_save_selection();
782 return true;
784 case CMD_LIST_MOVE_DOWN:
785 if(lw->selected_end+1 >= playlist_length(&c->playlist))
786 return false;
787 if(lw->range_selection)
788 {
789 int i = lw->selected_end;
790 unsigned last_selected = lw->selected;
791 for(; i >= (int)lw->selected_start; --i)
792 mpdclient_cmd_move(c, i, i+1);
793 lw->selected_start++;
794 lw->selected_end++;
795 lw->selected = last_selected + 1;
796 lw->range_base++;
797 }
798 else
799 {
800 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
801 lw->selected++;
802 lw->selected_start++;
803 lw->selected_end++;
804 }
806 playlist_save_selection();
807 return true;
809 case CMD_LOCATE:
810 if (playlist_selected_song()) {
811 screen_file_goto_song(c, playlist_selected_song());
812 return true;
813 }
815 break;
817 default:
818 break;
819 }
821 return false;
822 }
824 const struct screen_functions screen_playlist = {
825 .init = screen_playlist_init,
826 .exit = screen_playlist_exit,
827 .open = screen_playlist_open,
828 .close = screen_playlist_close,
829 .resize = screen_playlist_resize,
830 .paint = screen_playlist_paint,
831 .update = screen_playlist_update,
832 .cmd = screen_playlist_cmd,
833 .get_title = screen_playlist_title,
834 };