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