66649b256ca37d82939945ca276d1ba788cb0469
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 struct list_window *lw;
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 list_window_set_length(lw, playlist_length(playlist));
104 if (selected_song_id < 0)
105 /* there was no selection */
106 return;
108 song = playlist_selected_song();
109 if (song != NULL &&
110 mpd_song_get_id(song) == (unsigned)selected_song_id)
111 /* selection is still valid */
112 return;
114 pos = playlist_get_index_from_id(playlist, selected_song_id);
115 if (pos >= 0)
116 list_window_set_cursor(lw, pos);
118 playlist_save_selection();
119 }
121 #ifndef NCMPC_MINI
122 static gboolean
123 scroll_timer_callback(G_GNUC_UNUSED gpointer data)
124 {
125 scroll_source_id = 0;
127 hscroll_step(&hscroll);
128 playlist_repaint();
129 return false;
130 }
131 #endif
133 static const char *
134 list_callback(unsigned idx, bool *highlight, char **second_column, G_GNUC_UNUSED void *data)
135 {
136 static char songname[MAX_SONG_LENGTH];
137 struct mpd_song *song;
139 assert(playlist != NULL);
140 assert(idx < playlist_length(playlist));
142 song = playlist_get(playlist, idx);
143 if ((int)mpd_song_get_id(song) == current_song_id)
144 *highlight = true;
146 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
148 #ifndef NCMPC_MINI
149 if (second_column != NULL && mpd_song_get_duration(song) > 0) {
150 char duration[32];
151 format_duration_short(duration, sizeof(duration),
152 mpd_song_get_duration(song));
153 *second_column = g_strdup(duration);
154 }
156 if (idx == lw->selected)
157 {
158 int second_column_len = 0;
159 if (second_column != NULL && *second_column != NULL)
160 second_column_len = strlen(*second_column);
161 if (options.scroll && utf8_width(songname) > (unsigned)(COLS - second_column_len - 1) )
162 {
163 static unsigned current_song;
164 char *tmp;
166 if (current_song != lw->selected) {
167 hscroll_reset(&hscroll);
168 current_song = lw->selected;
169 }
171 tmp = strscroll(&hscroll, songname, options.scroll_sep,
172 MAX_SONG_LENGTH);
173 g_strlcpy(songname, tmp, MAX_SONG_LENGTH);
174 g_free(tmp);
176 if (scroll_source_id == 0)
177 scroll_source_id =
178 g_timeout_add(1000,
179 scroll_timer_callback,
180 NULL);
181 } else {
182 hscroll_reset(&hscroll);
184 if (scroll_source_id != 0) {
185 g_source_remove(scroll_source_id);
186 scroll_source_id = 0;
187 }
188 }
189 }
190 #else
191 (void)second_column;
192 #endif
194 return songname;
195 }
197 static void
198 center_playing_item(struct mpdclient *c, bool center_cursor)
199 {
200 const struct mpd_song *song;
201 unsigned length = c->playlist.list->len;
202 int idx;
204 song = mpdclient_get_current_song(c);
205 if (song == NULL)
206 return;
208 /* try to center the song that are playing */
209 idx = playlist_get_index(&c->playlist, c->song);
210 if (idx < 0)
211 return;
213 if (length < lw->rows)
214 {
215 if (center_cursor)
216 list_window_set_cursor(lw, idx);
217 return;
218 }
220 list_window_center(lw, idx);
222 if (center_cursor) {
223 list_window_set_cursor(lw, idx);
224 return;
225 }
227 /* make sure the cursor is in the window */
228 list_window_fetch_cursor(lw);
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 struct mpd_connection *connection;
274 gchar *filename, *filename_utf8;
275 #ifndef NCMPC_MINI
276 GCompletion *gcmp;
277 GList *list = NULL;
278 completion_callback_data_t data;
279 #endif
281 #ifdef NCMPC_MINI
282 (void)defaultname;
283 #endif
285 #ifndef NCMPC_MINI
286 if (name == NULL) {
287 /* initialize completion support */
288 gcmp = g_completion_new(NULL);
289 g_completion_set_compare(gcmp, completion_strncmp);
290 data.list = &list;
291 data.dir_list = NULL;
292 data.c = c;
293 wrln_completion_callback_data = &data;
294 wrln_pre_completion_callback = save_pre_completion_cb;
295 wrln_post_completion_callback = save_post_completion_cb;
298 /* query the user for a filename */
299 filename = screen_readln(_("Save playlist as"),
300 defaultname,
301 NULL,
302 gcmp);
304 /* destroy completion support */
305 wrln_completion_callback_data = NULL;
306 wrln_pre_completion_callback = NULL;
307 wrln_post_completion_callback = NULL;
308 g_completion_free(gcmp);
309 list = string_list_free(list);
310 if( filename )
311 filename=g_strstrip(filename);
312 } else
313 #endif
314 filename=g_strdup(name);
316 if (filename == NULL)
317 return -1;
319 /* send save command to mpd */
321 filename_utf8 = locale_to_utf8(filename);
323 connection = mpdclient_get_connection(c);
324 if (!mpd_run_save(connection, filename_utf8)) {
325 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
326 mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
327 mpd_connection_clear_error(connection)) {
328 char *buf;
329 int key;
331 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
332 filename, YES, NO);
333 key = tolower(screen_getch(buf));
334 g_free(buf);
336 if (key != YES[0]) {
337 g_free(filename_utf8);
338 g_free(filename);
339 screen_status_printf(_("Aborted"));
340 return -1;
341 }
343 if (!mpd_run_rm(connection, filename_utf8) ||
344 !mpd_run_save(connection, filename_utf8)) {
345 mpdclient_handle_error(c);
346 g_free(filename_utf8);
347 g_free(filename);
348 return -1;
349 }
350 } else {
351 mpdclient_handle_error(c);
352 g_free(filename_utf8);
353 g_free(filename);
354 return -1;
355 }
356 }
358 c->events |= MPD_IDLE_STORED_PLAYLIST;
360 g_free(filename_utf8);
362 /* success */
363 screen_status_printf(_("Saved %s"), filename);
364 g_free(filename);
365 return 0;
366 }
368 #ifndef NCMPC_MINI
369 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
370 GList **list, struct mpdclient *c)
371 {
372 g_completion_remove_items(gcmp, *list);
373 *list = string_list_remove(*list, dir);
374 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
375 g_completion_add_items(gcmp, *list);
376 *dir_list = g_list_append(*dir_list, g_strdup(dir));
377 }
379 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
380 {
381 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
382 GList **dir_list = tmp->dir_list;
383 GList **list = tmp->list;
384 struct mpdclient *c = tmp->c;
386 if (*list == NULL) {
387 /* create initial list */
388 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
389 g_completion_add_items(gcmp, *list);
390 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
391 string_list_find(*dir_list, line) == NULL) {
392 /* add directory content to list */
393 add_dir(gcmp, line, dir_list, list, c);
394 }
395 }
397 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
398 GList *items, void *data)
399 {
400 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
401 GList **dir_list = tmp->dir_list;
402 GList **list = tmp->list;
403 struct mpdclient *c = tmp->c;
405 if (g_list_length(items) >= 1)
406 screen_display_completion_list(items);
408 if (line && line[0] && line[strlen(line) - 1] == '/' &&
409 string_list_find(*dir_list, line) == NULL) {
410 /* add directory content to list */
411 add_dir(gcmp, line, dir_list, list, c);
412 }
413 }
414 #endif
416 static int
417 handle_add_to_playlist(struct mpdclient *c)
418 {
419 gchar *path;
420 #ifndef NCMPC_MINI
421 GCompletion *gcmp;
422 GList *list = NULL;
423 GList *dir_list = NULL;
424 completion_callback_data_t data;
426 /* initialize completion support */
427 gcmp = g_completion_new(NULL);
428 g_completion_set_compare(gcmp, completion_strncmp);
429 data.list = &list;
430 data.dir_list = &dir_list;
431 data.c = c;
432 wrln_completion_callback_data = &data;
433 wrln_pre_completion_callback = add_pre_completion_cb;
434 wrln_post_completion_callback = add_post_completion_cb;
435 #endif
437 /* get path */
438 path = screen_readln(_("Add"),
439 NULL,
440 NULL,
441 #ifdef NCMPC_MINI
442 NULL
443 #else
444 gcmp
445 #endif
446 );
448 /* destroy completion data */
449 #ifndef NCMPC_MINI
450 wrln_completion_callback_data = NULL;
451 wrln_pre_completion_callback = NULL;
452 wrln_post_completion_callback = NULL;
453 g_completion_free(gcmp);
454 string_list_free(list);
455 string_list_free(dir_list);
456 #endif
458 /* add the path to the playlist */
459 if (path != NULL) {
460 char *path_utf8 = locale_to_utf8(path);
461 mpdclient_cmd_add_path(c, path_utf8);
462 g_free(path_utf8);
463 }
465 g_free(path);
466 return 0;
467 }
469 static void
470 screen_playlist_init(WINDOW *w, int cols, int rows)
471 {
472 lw = list_window_init(w, cols, rows);
473 }
475 static gboolean
476 timer_hide_cursor(gpointer data)
477 {
478 struct mpdclient *c = data;
480 assert(options.hide_cursor > 0);
481 assert(timer_hide_cursor_id != 0);
483 timer_hide_cursor_id = 0;
485 /* hide the cursor when mpd is playing and the user is inactive */
487 if (c->status != NULL &&
488 mpd_status_get_state(c->status) == MPD_STATE_PLAY) {
489 lw->hide_cursor = true;
490 playlist_repaint();
491 } else
492 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
493 timer_hide_cursor, c);
495 return FALSE;
496 }
498 static void
499 screen_playlist_open(struct mpdclient *c)
500 {
501 playlist = &c->playlist;
503 assert(timer_hide_cursor_id == 0);
504 if (options.hide_cursor > 0) {
505 lw->hide_cursor = false;
506 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
507 timer_hide_cursor, c);
508 }
510 playlist_restore_selection();
511 }
513 static void
514 screen_playlist_close(void)
515 {
516 if (timer_hide_cursor_id != 0) {
517 g_source_remove(timer_hide_cursor_id);
518 timer_hide_cursor_id = 0;
519 }
520 }
522 static void
523 screen_playlist_resize(int cols, int rows)
524 {
525 list_window_resize(lw, cols, rows);
526 }
529 static void
530 screen_playlist_exit(void)
531 {
532 list_window_free(lw);
533 }
535 static const char *
536 screen_playlist_title(char *str, size_t size)
537 {
538 if (options.host == NULL)
539 return _("Playlist");
541 g_snprintf(str, size, _("Playlist on %s"), options.host);
542 return str;
543 }
545 static void
546 screen_playlist_paint(void)
547 {
548 list_window_paint(lw, list_callback, NULL);
549 }
551 static void
552 screen_playlist_update(struct mpdclient *c)
553 {
554 static int prev_song_id = -1;
556 if (c->events & MPD_IDLE_PLAYLIST)
557 playlist_restore_selection();
559 current_song_id = c->status != NULL &&
560 (mpd_status_get_state(c->status) == MPD_STATE_PLAY ||
561 mpd_status_get_state(c->status) == MPD_STATE_PAUSE)
562 ? (int)mpd_status_get_song_id(c->status) : -1;
564 if (current_song_id != prev_song_id) {
565 prev_song_id = current_song_id;
567 /* center the cursor */
568 if (options.auto_center && current_song_id != -1 && ! lw->range_selection)
569 center_playing_item(c, false);
571 playlist_repaint();
572 } else if (c->events & MPD_IDLE_PLAYLIST) {
573 /* the playlist has changed, we must paint the new
574 version */
575 playlist_repaint();
576 }
577 }
579 #ifdef HAVE_GETMOUSE
580 static bool
581 handle_mouse_event(struct mpdclient *c)
582 {
583 int row;
584 unsigned selected;
585 unsigned long bstate;
587 if (screen_get_mouse_event(c, &bstate, &row) ||
588 list_window_mouse(lw, bstate, row)) {
589 playlist_repaint();
590 return true;
591 }
593 if (bstate & BUTTON1_DOUBLE_CLICKED) {
594 /* stop */
595 screen_cmd(c, CMD_STOP);
596 return true;
597 }
599 selected = lw->start + row;
601 if (bstate & BUTTON1_CLICKED) {
602 /* play */
603 if (lw->start + row < playlist_length(playlist))
604 mpdclient_cmd_play(c, lw->start + row);
605 } else if (bstate & BUTTON3_CLICKED) {
606 /* delete */
607 if (selected == lw->selected)
608 mpdclient_cmd_delete(c, lw->selected);
609 }
611 list_window_set_cursor(lw, selected);
612 playlist_save_selection();
613 playlist_repaint();
615 return true;
616 }
617 #endif
619 static bool
620 screen_playlist_cmd(struct mpdclient *c, command_t cmd)
621 {
622 struct mpd_connection *connection;
623 static command_t cached_cmd = CMD_NONE;
624 command_t prev_cmd = cached_cmd;
625 cached_cmd = cmd;
627 lw->hide_cursor = false;
629 if (options.hide_cursor > 0) {
630 if (timer_hide_cursor_id != 0)
631 g_source_remove(timer_hide_cursor_id);
632 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
633 timer_hide_cursor, c);
634 }
636 if (list_window_cmd(lw, cmd)) {
637 playlist_save_selection();
638 playlist_repaint();
639 return true;
640 }
642 switch(cmd) {
643 case CMD_SCREEN_UPDATE:
644 center_playing_item(c, prev_cmd == CMD_SCREEN_UPDATE);
645 playlist_repaint();
646 return false;
647 case CMD_SELECT_PLAYING:
648 list_window_set_cursor(lw, playlist_get_index(&c->playlist,
649 c->song));
650 playlist_save_selection();
651 playlist_repaint();
652 return true;
654 case CMD_LIST_FIND:
655 case CMD_LIST_RFIND:
656 case CMD_LIST_FIND_NEXT:
657 case CMD_LIST_RFIND_NEXT:
658 screen_find(lw, cmd, list_callback, NULL);
659 playlist_save_selection();
660 playlist_repaint();
661 return true;
662 case CMD_LIST_JUMP:
663 screen_jump(lw, list_callback, NULL);
664 playlist_save_selection();
665 playlist_repaint();
666 return true;
668 #ifdef HAVE_GETMOUSE
669 case CMD_MOUSE_EVENT:
670 return handle_mouse_event(c);
671 #endif
673 #ifdef ENABLE_SONG_SCREEN
674 case CMD_SCREEN_SONG:
675 if (playlist_selected_song()) {
676 screen_song_switch(c, playlist_selected_song());
677 return true;
678 }
680 break;
681 #endif
683 #ifdef ENABLE_LYRICS_SCREEN
684 case CMD_SCREEN_LYRICS:
685 if (lw->selected < playlist_length(&c->playlist)) {
686 struct mpd_song *selected = playlist_get(&c->playlist, lw->selected);
687 bool follow = false;
689 if (c->song && selected &&
690 !strcmp(mpd_song_get_uri(selected),
691 mpd_song_get_uri(c->song)))
692 follow = true;
694 screen_lyrics_switch(c, selected, follow);
695 return true;
696 }
698 break;
699 #endif
700 case CMD_SCREEN_SWAP:
701 screen_swap(c, playlist_get(&c->playlist, lw->selected));
702 return true;
704 default:
705 break;
706 }
708 if (!mpdclient_is_connected(c))
709 return false;
711 switch(cmd) {
712 case CMD_PLAY:
713 mpdclient_cmd_play(c, lw->selected);
714 return true;
716 case CMD_DELETE:
717 if (lw->range_selection) {
718 mpdclient_cmd_delete_range(c, lw->selected_start,
719 lw->selected_end + 1);
720 } else {
721 mpdclient_cmd_delete(c, lw->selected);
722 }
724 list_window_set_cursor(lw, lw->selected_start);
725 return true;
727 case CMD_SAVE_PLAYLIST:
728 playlist_save(c, NULL, NULL);
729 return true;
731 case CMD_ADD:
732 handle_add_to_playlist(c);
733 return true;
735 case CMD_SHUFFLE:
736 if(!lw->range_selection)
737 /* No range selection, shuffle all list. */
738 break;
740 connection = mpdclient_get_connection(c);
741 if (mpd_run_shuffle_range(connection, lw->selected_start,
742 lw->selected_end + 1))
743 screen_status_message(_("Shuffled playlist"));
744 else
745 mpdclient_handle_error(c);
746 return true;
748 case CMD_LIST_MOVE_UP:
749 if(lw->selected_start == 0)
750 return false;
751 if(lw->range_selection)
752 {
753 unsigned i = lw->selected_start;
754 unsigned last_selected = lw->selected;
755 for(; i <= lw->selected_end; ++i)
756 mpdclient_cmd_move(c, i, i-1);
757 lw->selected_start--;
758 lw->selected_end--;
759 lw->selected = last_selected - 1;
760 lw->range_base--;
761 }
762 else
763 {
764 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
765 lw->selected--;
766 lw->selected_start--;
767 lw->selected_end--;
768 }
770 playlist_save_selection();
771 return true;
773 case CMD_LIST_MOVE_DOWN:
774 if(lw->selected_end+1 >= playlist_length(&c->playlist))
775 return false;
776 if(lw->range_selection)
777 {
778 int i = lw->selected_end;
779 unsigned last_selected = lw->selected;
780 for(; i >= (int)lw->selected_start; --i)
781 mpdclient_cmd_move(c, i, i+1);
782 lw->selected_start++;
783 lw->selected_end++;
784 lw->selected = last_selected + 1;
785 lw->range_base++;
786 }
787 else
788 {
789 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
790 lw->selected++;
791 lw->selected_start++;
792 lw->selected_end++;
793 }
795 playlist_save_selection();
796 return true;
798 case CMD_LOCATE:
799 if (playlist_selected_song()) {
800 screen_file_goto_song(c, playlist_selected_song());
801 return true;
802 }
804 break;
806 default:
807 break;
808 }
810 return false;
811 }
813 const struct screen_functions screen_playlist = {
814 .init = screen_playlist_init,
815 .exit = screen_playlist_exit,
816 .open = screen_playlist_open,
817 .close = screen_playlist_close,
818 .resize = screen_playlist_resize,
819 .paint = screen_playlist_paint,
820 .update = screen_playlist_update,
821 .cmd = screen_playlist_cmd,
822 .get_title = screen_playlist_title,
823 };