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 struct hscroll hscroll;
60 static guint scroll_source_id;
61 #endif
63 static struct mpdclient_playlist *playlist;
64 static int current_song_id = -1;
65 static int selected_song_id = -1;
66 static list_window_t *lw = NULL;
67 static guint timer_hide_cursor_id;
69 static void
70 screen_playlist_paint(void);
72 static void
73 playlist_repaint(void)
74 {
75 screen_playlist_paint();
76 wrefresh(lw->w);
77 }
79 static const struct mpd_song *
80 playlist_selected_song(void)
81 {
82 return !lw->range_selection &&
83 lw->selected < playlist_length(playlist)
84 ? playlist_get(playlist, lw->selected)
85 : NULL;
86 }
88 static void
89 playlist_save_selection(void)
90 {
91 selected_song_id = playlist_selected_song() != NULL
92 ? (int)mpd_song_get_id(playlist_selected_song())
93 : -1;
94 }
96 static void
97 playlist_restore_selection(void)
98 {
99 const struct mpd_song *song;
100 int pos;
102 if (selected_song_id < 0)
103 /* there was no selection */
104 return;
106 song = playlist_selected_song();
107 if (song != NULL &&
108 mpd_song_get_id(song) == (unsigned)selected_song_id)
109 /* selection is still valid */
110 return;
112 pos = playlist_get_index_from_id(playlist, selected_song_id);
113 if (pos >= 0)
114 lw->selected = pos;
116 list_window_check_selected(lw, playlist_length(playlist));
117 playlist_save_selection();
118 }
120 #ifndef NCMPC_MINI
121 static gboolean
122 scroll_timer_callback(G_GNUC_UNUSED gpointer data)
123 {
124 scroll_source_id = 0;
126 hscroll_step(&hscroll);
127 playlist_repaint();
128 return false;
129 }
130 #endif
132 static const char *
133 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
134 {
135 static char songname[MAX_SONG_LENGTH];
136 struct mpd_song *song;
138 if (playlist == NULL || idx >= playlist_length(playlist))
139 return NULL;
141 song = playlist_get(playlist, idx);
142 if ((int)mpd_song_get_id(song) == current_song_id)
143 *highlight = true;
145 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
147 #ifndef NCMPC_MINI
148 if (second_column != NULL && mpd_song_get_duration(song) > 0) {
149 char duration[32];
150 format_duration_short(duration, sizeof(duration),
151 mpd_song_get_duration(song));
152 *second_column = g_strdup(duration);
153 }
155 if (idx == lw->selected)
156 {
157 int second_column_len = 0;
158 if (second_column != NULL && *second_column != NULL)
159 second_column_len = strlen(*second_column);
160 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
161 {
162 static unsigned current_song;
163 char *tmp;
165 if (current_song != lw->selected) {
166 hscroll_reset(&hscroll);
167 current_song = lw->selected;
168 }
170 tmp = strscroll(&hscroll, songname, options.scroll_sep,
171 MAX_SONG_LENGTH);
172 g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
173 g_free(tmp);
175 if (scroll_source_id == 0)
176 scroll_source_id =
177 g_timeout_add(1000,
178 scroll_timer_callback,
179 NULL);
180 } else {
181 hscroll_reset(&hscroll);
183 if (scroll_source_id != 0) {
184 g_source_remove(scroll_source_id);
185 scroll_source_id = 0;
186 }
187 }
188 }
189 #else
190 (void)second_column;
191 #endif
193 return songname;
194 }
196 static void
197 center_playing_item(struct mpdclient *c, bool center_cursor)
198 {
199 const struct mpd_song *song;
200 unsigned length = c->playlist.list->len;
201 int idx;
203 song = mpdclient_get_current_song(c);
204 if (song == NULL)
205 return;
207 /* try to center the song that are playing */
208 idx = playlist_get_index(&c->playlist, c->song);
209 if (idx < 0)
210 return;
212 if (length < lw->rows)
213 {
214 if (center_cursor)
215 list_window_set_selected(lw, idx);
216 return;
217 }
219 list_window_center(lw, length, idx);
221 if (center_cursor) {
222 list_window_set_selected(lw, idx);
223 return;
224 }
226 /* make sure the cursor is in the window */
227 if (lw->selected < lw->start + options.scroll_offset) {
228 if (lw->start > 0)
229 lw->selected = lw->start + options.scroll_offset;
230 if (lw->range_selection) {
231 lw->selected_start = lw->range_base;
232 lw->selected_end = lw->selected;
233 } else {
234 lw->selected_start = lw->selected;
235 lw->selected_end = lw->selected;
236 }
237 } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
238 if (lw->start + lw->rows < length)
239 lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
240 if (lw->range_selection) {
241 lw->selected_start = lw->selected;
242 lw->selected_end = lw->range_base;
243 } else {
244 lw->selected_start = lw->selected;
245 lw->selected_end = lw->selected;
246 }
247 }
248 }
250 #ifndef NCMPC_MINI
251 static void
252 save_pre_completion_cb(GCompletion *gcmp, G_GNUC_UNUSED gchar *line,
253 void *data)
254 {
255 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
256 GList **list = tmp->list;
257 struct mpdclient *c = tmp->c;
259 if( *list == NULL ) {
260 /* create completion list */
261 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
262 g_completion_add_items(gcmp, *list);
263 }
264 }
266 static void
267 save_post_completion_cb(G_GNUC_UNUSED GCompletion *gcmp,
268 G_GNUC_UNUSED gchar *line, GList *items,
269 G_GNUC_UNUSED void *data)
270 {
271 if (g_list_length(items) >= 1)
272 screen_display_completion_list(items);
273 }
274 #endif
276 #ifndef NCMPC_MINI
277 /**
278 * Wrapper for strncmp(). We are not allowed to pass &strncmp to
279 * g_completion_set_compare(), because strncmp() takes size_t where
280 * g_completion_set_compare passes a gsize value.
281 */
282 static gint
283 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
284 {
285 return strncmp(s1, s2, n);
286 }
287 #endif
289 int
290 playlist_save(struct mpdclient *c, char *name, char *defaultname)
291 {
292 gchar *filename, *filename_utf8;
293 #ifndef NCMPC_MINI
294 GCompletion *gcmp;
295 GList *list = NULL;
296 completion_callback_data_t data;
297 #endif
299 #ifdef NCMPC_MINI
300 (void)defaultname;
301 #endif
303 #ifndef NCMPC_MINI
304 if (name == NULL) {
305 /* initialize completion support */
306 gcmp = g_completion_new(NULL);
307 g_completion_set_compare(gcmp, completion_strncmp);
308 data.list = &list;
309 data.dir_list = NULL;
310 data.c = c;
311 wrln_completion_callback_data = &data;
312 wrln_pre_completion_callback = save_pre_completion_cb;
313 wrln_post_completion_callback = save_post_completion_cb;
316 /* query the user for a filename */
317 filename = screen_readln(_("Save playlist as"),
318 defaultname,
319 NULL,
320 gcmp);
322 /* destroy completion support */
323 wrln_completion_callback_data = NULL;
324 wrln_pre_completion_callback = NULL;
325 wrln_post_completion_callback = NULL;
326 g_completion_free(gcmp);
327 list = string_list_free(list);
328 if( filename )
329 filename=g_strstrip(filename);
330 } else
331 #endif
332 filename=g_strdup(name);
334 if (filename == NULL)
335 return -1;
337 /* send save command to mpd */
339 filename_utf8 = locale_to_utf8(filename);
341 if (!mpd_run_save(c->connection, filename_utf8)) {
342 if (mpd_connection_get_error(c->connection) == MPD_ERROR_SERVER &&
343 mpd_connection_get_server_error(c->connection) == MPD_SERVER_ERROR_EXIST &&
344 mpd_connection_clear_error(c->connection)) {
345 char *buf;
346 int key;
348 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
349 filename, YES, NO);
350 key = tolower(screen_getch(buf));
351 g_free(buf);
353 if (key != YES[0]) {
354 g_free(filename_utf8);
355 g_free(filename);
356 screen_status_printf(_("Aborted"));
357 return -1;
358 }
360 if (!mpd_run_rm(c->connection, filename_utf8) ||
361 !mpd_run_save(c->connection, filename_utf8)) {
362 mpdclient_handle_error(c);
363 g_free(filename_utf8);
364 g_free(filename);
365 return -1;
366 }
367 } else {
368 mpdclient_handle_error(c);
369 g_free(filename_utf8);
370 g_free(filename);
371 return -1;
372 }
373 }
375 c->events |= MPD_IDLE_STORED_PLAYLIST;
377 g_free(filename_utf8);
379 /* success */
380 screen_status_printf(_("Saved %s"), filename);
381 g_free(filename);
382 return 0;
383 }
385 #ifndef NCMPC_MINI
386 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
387 GList **list, struct mpdclient *c)
388 {
389 g_completion_remove_items(gcmp, *list);
390 *list = string_list_remove(*list, dir);
391 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
392 g_completion_add_items(gcmp, *list);
393 *dir_list = g_list_append(*dir_list, g_strdup(dir));
394 }
396 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, 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 (*list == NULL) {
404 /* create initial list */
405 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
406 g_completion_add_items(gcmp, *list);
407 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
408 string_list_find(*dir_list, line) == NULL) {
409 /* add directory content to list */
410 add_dir(gcmp, line, dir_list, list, c);
411 }
412 }
414 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
415 GList *items, void *data)
416 {
417 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
418 GList **dir_list = tmp->dir_list;
419 GList **list = tmp->list;
420 struct mpdclient *c = tmp->c;
422 if (g_list_length(items) >= 1)
423 screen_display_completion_list(items);
425 if (line && line[0] && line[strlen(line) - 1] == '/' &&
426 string_list_find(*dir_list, line) == NULL) {
427 /* add directory content to list */
428 add_dir(gcmp, line, dir_list, list, c);
429 }
430 }
431 #endif
433 static int
434 handle_add_to_playlist(struct mpdclient *c)
435 {
436 gchar *path;
437 #ifndef NCMPC_MINI
438 GCompletion *gcmp;
439 GList *list = NULL;
440 GList *dir_list = NULL;
441 completion_callback_data_t data;
443 /* initialize completion support */
444 gcmp = g_completion_new(NULL);
445 g_completion_set_compare(gcmp, completion_strncmp);
446 data.list = &list;
447 data.dir_list = &dir_list;
448 data.c = c;
449 wrln_completion_callback_data = &data;
450 wrln_pre_completion_callback = add_pre_completion_cb;
451 wrln_post_completion_callback = add_post_completion_cb;
452 #endif
454 /* get path */
455 path = screen_readln(_("Add"),
456 NULL,
457 NULL,
458 #ifdef NCMPC_MINI
459 NULL
460 #else
461 gcmp
462 #endif
463 );
465 /* destroy completion data */
466 #ifndef NCMPC_MINI
467 wrln_completion_callback_data = NULL;
468 wrln_pre_completion_callback = NULL;
469 wrln_post_completion_callback = NULL;
470 g_completion_free(gcmp);
471 string_list_free(list);
472 string_list_free(dir_list);
473 #endif
475 /* add the path to the playlist */
476 if (path != NULL) {
477 char *path_utf8 = locale_to_utf8(path);
478 mpdclient_cmd_add_path(c, path_utf8);
479 g_free(path_utf8);
480 }
482 g_free(path);
483 return 0;
484 }
486 static void
487 screen_playlist_init(WINDOW *w, int cols, int rows)
488 {
489 lw = list_window_init(w, cols, rows);
490 }
492 static gboolean
493 timer_hide_cursor(gpointer data)
494 {
495 struct mpdclient *c = data;
497 assert(options.hide_cursor > 0);
498 assert(timer_hide_cursor_id != 0);
500 timer_hide_cursor_id = 0;
502 /* hide the cursor when mpd is playing and the user is inactive */
504 if (c->status != NULL &&
505 mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
506 lw->hide_cursor = true;
507 playlist_repaint();
508 } else
509 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
510 timer_hide_cursor, c);
512 return FALSE;
513 }
515 static void
516 screen_playlist_open(struct mpdclient *c)
517 {
518 playlist = &c->playlist;
520 assert(timer_hide_cursor_id == 0);
521 if (options.hide_cursor > 0) {
522 lw->hide_cursor = false;
523 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
524 timer_hide_cursor, c);
525 }
527 playlist_restore_selection();
528 }
530 static void
531 screen_playlist_close(void)
532 {
533 if (timer_hide_cursor_id != 0) {
534 g_source_remove(timer_hide_cursor_id);
535 timer_hide_cursor_id = 0;
536 }
537 }
539 static void
540 screen_playlist_resize(int cols, int rows)
541 {
542 lw->cols = cols;
543 lw->rows = rows;
544 }
547 static void
548 screen_playlist_exit(void)
549 {
550 list_window_free(lw);
551 }
553 static const char *
554 screen_playlist_title(char *str, size_t size)
555 {
556 if (options.host == NULL)
557 return _("Playlist");
559 g_snprintf(str, size, _("Playlist on %s"), options.host);
560 return str;
561 }
563 static void
564 screen_playlist_paint(void)
565 {
566 list_window_paint(lw, list_callback, NULL);
567 }
569 static void
570 screen_playlist_update(struct mpdclient *c)
571 {
572 static int prev_song_id = -1;
574 if (c->events & MPD_IDLE_PLAYLIST)
575 playlist_restore_selection();
577 current_song_id = c->status != NULL &&
578 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
579 mpd_status_get_state(c->status) == MPD_STATE_PAUSE)
580 ? (int)mpd_status_get_song_id(c->status) : -1;
582 if (current_song_id != prev_song_id) {
583 prev_song_id = current_song_id;
585 /* center the cursor */
586 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
587 center_playing_item(c, false);
589 playlist_repaint();
590 } else if (c->events & MPD_IDLE_PLAYLIST) {
591 /* the playlist has changed, we must paint the new
592 version */
593 playlist_repaint();
594 }
595 }
597 #ifdef HAVE_GETMOUSE
598 static bool
599 handle_mouse_event(struct mpdclient *c)
600 {
601 int row;
602 unsigned selected;
603 unsigned long bstate;
605 if (screen_get_mouse_event(c, &bstate, &row) ||
606 list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
607 playlist_repaint();
608 return true;
609 }
611 if (bstate & BUTTON1_DOUBLE_CLICKED) {
612 /* stop */
613 screen_cmd(c, CMD_STOP);
614 return true;
615 }
617 selected = lw->start + row;
619 if (bstate & BUTTON1_CLICKED) {
620 /* play */
621 if (lw->start + row < playlist_length(playlist))
622 mpdclient_cmd_play(c, lw->start + row);
623 } else if (bstate & BUTTON3_CLICKED) {
624 /* delete */
625 if (selected == lw->selected)
626 mpdclient_cmd_delete(c, lw->selected);
627 }
629 lw->selected = selected;
630 list_window_check_selected(lw, playlist_length(playlist));
631 playlist_save_selection();
632 playlist_repaint();
634 return true;
635 }
636 #endif
638 static bool
639 screen_playlist_cmd(struct mpdclient *c, command_t cmd)
640 {
641 static command_t cached_cmd = CMD_NONE;
642 command_t prev_cmd = cached_cmd;
643 cached_cmd = cmd;
645 lw->hide_cursor = false;
647 if (options.hide_cursor > 0) {
648 if (timer_hide_cursor_id != 0)
649 g_source_remove(timer_hide_cursor_id);
650 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
651 timer_hide_cursor, c);
652 }
654 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
655 playlist_save_selection();
656 playlist_repaint();
657 return true;
658 }
660 switch(cmd) {
661 case CMD_SCREEN_UPDATE:
662 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
663 playlist_repaint();
664 return false;
665 case CMD_SELECT_PLAYING:
666 list_window_set_selected(lw, playlist_get_index(&c->playlist,
667 c->song));
668 playlist_save_selection();
669 return true;
671 case CMD_LIST_FIND:
672 case CMD_LIST_RFIND:
673 case CMD_LIST_FIND_NEXT:
674 case CMD_LIST_RFIND_NEXT:
675 screen_find(lw, playlist_length(&c->playlist),
676 cmd, list_callback, NULL);
677 playlist_save_selection();
678 playlist_repaint();
679 return true;
680 case CMD_LIST_JUMP:
681 screen_jump(lw, list_callback, NULL);
682 playlist_save_selection();
683 playlist_repaint();
684 return true;
686 #ifdef HAVE_GETMOUSE
687 case CMD_MOUSE_EVENT:
688 return handle_mouse_event(c);
689 #endif
691 #ifdef ENABLE_SONG_SCREEN
692 case CMD_SCREEN_SONG:
693 if (playlist_selected_song()) {
694 screen_song_switch(c, playlist_selected_song());
695 return true;
696 }
698 break;
699 #endif
701 #ifdef ENABLE_LYRICS_SCREEN
702 case CMD_SCREEN_LYRICS:
703 if (lw->selected < playlist_length(&c->playlist)) {
704 struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
705 bool follow = false;
707 if (c->song && selected &&
708 !strcmp(mpd_song_get_uri(selected),
709 mpd_song_get_uri(c->song)))
710 follow = true;
712 screen_lyrics_switch(c, selected, follow);
713 return true;
714 }
716 break;
717 #endif
718 case CMD_SCREEN_SWAP:
719 screen_swap(c, playlist_get(&c->playlist, lw->selected));
720 return true;
722 default:
723 break;
724 }
726 if (!mpdclient_is_connected(c))
727 return false;
729 switch(cmd) {
730 case CMD_PLAY:
731 mpdclient_cmd_play(c, lw->selected);
732 return true;
734 case CMD_DELETE:
735 if (lw->range_selection) {
736 mpdclient_cmd_delete_range(c, lw->selected_start,
737 lw->selected_end + 1);
738 } else {
739 mpdclient_cmd_delete(c, lw->selected);
740 }
742 lw->selected = lw->selected_end = lw->selected_start;
743 lw->range_selection = false;
744 return true;
746 case CMD_SAVE_PLAYLIST:
747 playlist_save(c, NULL, NULL);
748 return true;
750 case CMD_ADD:
751 handle_add_to_playlist(c);
752 return true;
754 case CMD_SHUFFLE:
755 {
756 if(!lw->range_selection)
757 /* No range selection, shuffle all list. */
758 break;
760 if (mpd_run_shuffle_range(c->connection, lw->selected_start,
761 lw->selected_end + 1))
762 screen_status_message(_("Shuffled playlist"));
763 else
764 mpdclient_handle_error(c);
765 return true;
766 }
768 case CMD_LIST_MOVE_UP:
769 if(lw->selected_start == 0)
770 return false;
771 if(lw->range_selection)
772 {
773 unsigned i = lw->selected_start;
774 unsigned last_selected = lw->selected;
775 for(; i <= lw->selected_end; ++i)
776 mpdclient_cmd_move(c, i, i-1);
777 lw->selected_start--;
778 lw->selected_end--;
779 lw->selected = last_selected - 1;
780 lw->range_base--;
781 }
782 else
783 {
784 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
785 lw->selected--;
786 lw->selected_start--;
787 lw->selected_end--;
788 }
790 playlist_save_selection();
791 return true;
793 case CMD_LIST_MOVE_DOWN:
794 if(lw->selected_end+1 >= playlist_length(&c->playlist))
795 return false;
796 if(lw->range_selection)
797 {
798 int i = lw->selected_end;
799 unsigned last_selected = lw->selected;
800 for(; i >= (int)lw->selected_start; --i)
801 mpdclient_cmd_move(c, i, i+1);
802 lw->selected_start++;
803 lw->selected_end++;
804 lw->selected = last_selected + 1;
805 lw->range_base++;
806 }
807 else
808 {
809 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
810 lw->selected++;
811 lw->selected_start++;
812 lw->selected_end++;
813 }
815 playlist_save_selection();
816 return true;
818 case CMD_LOCATE:
819 if (playlist_selected_song()) {
820 screen_file_goto_song(c, playlist_selected_song());
821 return true;
822 }
824 break;
826 default:
827 break;
828 }
830 return false;
831 }
833 const struct screen_functions screen_playlist = {
834 .init = screen_playlist_init,
835 .exit = screen_playlist_exit,
836 .open = screen_playlist_open,
837 .close = screen_playlist_close,
838 .resize = screen_playlist_resize,
839 .paint = screen_playlist_paint,
840 .update = screen_playlist_update,
841 .cmd = screen_playlist_cmd,
842 .get_title = screen_playlist_title,
843 };