4b50f7459916481650c5735c6732f2ce6b055c39
1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "config.h"
20 #include "i18n.h"
21 #include "charset.h"
22 #include "options.h"
23 #include "support.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"
33 #include "gcc.h"
35 #include <ctype.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <glib.h>
41 #define MAX_SONG_LENGTH 512
43 #ifndef NCMPC_MINI
44 typedef struct
45 {
46 GList **list;
47 GList **dir_list;
48 mpdclient_t *c;
49 } completion_callback_data_t;
50 #endif
52 static struct mpdclient_playlist *playlist;
53 static int current_song_id = -1;
54 static list_window_t *lw = NULL;
55 static guint timer_hide_cursor_id;
57 static void
58 play_paint(void);
60 static void
61 playlist_repaint(void)
62 {
63 play_paint();
64 wrefresh(lw->w);
65 }
67 static void
68 playlist_repaint_if_active(void)
69 {
70 if (screen_is_visible(&screen_playlist))
71 playlist_repaint();
72 }
74 static void
75 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
76 {
77 switch (event) {
78 case PLAYLIST_EVENT_DELETE:
79 break;
80 case PLAYLIST_EVENT_MOVE:
81 lw->selected = *((int *) data);
82 if (lw->selected < lw->start)
83 lw->start--;
84 break;
85 default:
86 break;
87 }
89 list_window_check_selected(lw, c->playlist.list->len);
90 playlist_repaint_if_active();
91 }
93 static const char *
94 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
95 {
96 static char songname[MAX_SONG_LENGTH];
97 mpd_Song *song;
99 if (playlist == NULL || idx >= playlist_length(playlist))
100 return NULL;
102 song = playlist_get(playlist, idx);
103 if (song->id == current_song_id)
104 *highlight = 1;
106 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
107 return songname;
108 }
110 static void
111 center_playing_item(mpdclient_t *c)
112 {
113 unsigned length = c->playlist.list->len;
114 unsigned offset = lw->selected - lw->start;
115 int idx;
117 if (!c->song || length < lw->rows ||
118 c->status == NULL || IS_STOPPED(c->status->state))
119 return;
121 /* try to center the song that are playing */
122 idx = playlist_get_index(c, c->song);
123 if (idx < 0)
124 return;
126 list_window_center(lw, length, idx);
128 /* make sure the cursor is in the window */
129 lw->selected = lw->start+offset;
130 list_window_check_selected(lw, length);
131 }
133 #ifndef NCMPC_MINI
134 static void
135 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
136 {
137 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
138 GList **list = tmp->list;
139 mpdclient_t *c = tmp->c;
141 if( *list == NULL ) {
142 /* create completion list */
143 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
144 g_completion_add_items(gcmp, *list);
145 }
146 }
148 static void
149 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
150 GList *items, mpd_unused void *data)
151 {
152 if (g_list_length(items) >= 1)
153 screen_display_completion_list(items);
154 }
155 #endif
157 int
158 playlist_save(mpdclient_t *c, char *name, char *defaultname)
159 {
160 gchar *filename, *filename_utf8;
161 gint error;
162 #ifndef NCMPC_MINI
163 GCompletion *gcmp;
164 GList *list = NULL;
165 completion_callback_data_t data;
166 #endif
168 #ifdef NCMPC_MINI
169 (void)defaultname;
170 #endif
172 #ifndef NCMPC_MINI
173 if (name == NULL) {
174 /* initialize completion support */
175 gcmp = g_completion_new(NULL);
176 g_completion_set_compare(gcmp, strncmp);
177 data.list = &list;
178 data.dir_list = NULL;
179 data.c = c;
180 wrln_completion_callback_data = &data;
181 wrln_pre_completion_callback = save_pre_completion_cb;
182 wrln_post_completion_callback = save_post_completion_cb;
185 /* query the user for a filename */
186 filename = screen_readln(screen.status_window.w,
187 _("Save playlist as: "),
188 defaultname,
189 NULL,
190 gcmp);
192 /* destroy completion support */
193 wrln_completion_callback_data = NULL;
194 wrln_pre_completion_callback = NULL;
195 wrln_post_completion_callback = NULL;
196 g_completion_free(gcmp);
197 list = string_list_free(list);
198 if( filename )
199 filename=g_strstrip(filename);
200 } else
201 #endif
202 filename=g_strdup(name);
204 if (filename == NULL)
205 return -1;
207 /* send save command to mpd */
209 filename_utf8 = locale_to_utf8(filename);
210 error = mpdclient_cmd_save_playlist(c, filename_utf8);
211 g_free(filename_utf8);
213 if (error) {
214 gint code = GET_ACK_ERROR_CODE(error);
216 if (code == MPD_ACK_ERROR_EXIST) {
217 char *buf;
218 int key;
220 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
221 filename, YES, NO);
222 key = tolower(screen_getch(screen.status_window.w,
223 buf));
224 g_free(buf);
226 if (key == YES[0]) {
227 filename_utf8 = locale_to_utf8(filename);
228 error = mpdclient_cmd_delete_playlist(c, filename_utf8);
229 g_free(filename_utf8);
231 if (error) {
232 g_free(filename);
233 return -1;
234 }
236 error = playlist_save(c, filename, NULL);
237 g_free(filename);
238 return error;
239 }
241 screen_status_printf(_("Aborted!"));
242 }
244 g_free(filename);
245 return -1;
246 }
248 /* success */
249 screen_status_printf(_("Saved %s"), filename);
250 g_free(filename);
251 return 0;
252 }
254 #ifndef NCMPC_MINI
255 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
256 GList **list, mpdclient_t *c)
257 {
258 g_completion_remove_items(gcmp, *list);
259 *list = string_list_remove(*list, dir);
260 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
261 g_completion_add_items(gcmp, *list);
262 *dir_list = g_list_append(*dir_list, g_strdup(dir));
263 }
265 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
266 {
267 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
268 GList **dir_list = tmp->dir_list;
269 GList **list = tmp->list;
270 mpdclient_t *c = tmp->c;
272 if (*list == NULL) {
273 /* create initial list */
274 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
275 g_completion_add_items(gcmp, *list);
276 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
277 string_list_find(*dir_list, line) == NULL) {
278 /* add directory content to list */
279 add_dir(gcmp, line, dir_list, list, c);
280 }
281 }
283 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
284 GList *items, void *data)
285 {
286 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
287 GList **dir_list = tmp->dir_list;
288 GList **list = tmp->list;
289 mpdclient_t *c = tmp->c;
291 if (g_list_length(items) >= 1)
292 screen_display_completion_list(items);
294 if (line && line[0] && line[strlen(line) - 1] == '/' &&
295 string_list_find(*dir_list, line) == NULL) {
296 /* add directory content to list */
297 add_dir(gcmp, line, dir_list, list, c);
298 }
299 }
300 #endif
302 static int
303 handle_add_to_playlist(mpdclient_t *c)
304 {
305 gchar *path;
306 #ifndef NCMPC_MINI
307 GCompletion *gcmp;
308 GList *list = NULL;
309 GList *dir_list = NULL;
310 completion_callback_data_t data;
312 /* initialize completion support */
313 gcmp = g_completion_new(NULL);
314 g_completion_set_compare(gcmp, strncmp);
315 data.list = &list;
316 data.dir_list = &dir_list;
317 data.c = c;
318 wrln_completion_callback_data = &data;
319 wrln_pre_completion_callback = add_pre_completion_cb;
320 wrln_post_completion_callback = add_post_completion_cb;
321 #endif
323 /* get path */
324 path = screen_readln(screen.status_window.w,
325 _("Add: "),
326 NULL,
327 NULL,
328 #ifdef NCMPC_MINI
329 NULL
330 #else
331 gcmp
332 #endif
333 );
335 /* destroy completion data */
336 #ifndef NCMPC_MINI
337 wrln_completion_callback_data = NULL;
338 wrln_pre_completion_callback = NULL;
339 wrln_post_completion_callback = NULL;
340 g_completion_free(gcmp);
341 string_list_free(list);
342 string_list_free(dir_list);
343 #endif
345 /* add the path to the playlist */
346 if (path != NULL) {
347 char *path_utf8 = locale_to_utf8(path);
348 mpdclient_cmd_add_path(c, path_utf8);
349 g_free(path_utf8);
350 }
352 g_free(path);
353 return 0;
354 }
356 static void
357 play_init(WINDOW *w, int cols, int rows)
358 {
359 lw = list_window_init(w, cols, rows);
360 }
362 static gboolean
363 timer_hide_cursor(gpointer data)
364 {
365 struct mpdclient *c = data;
367 assert(options.hide_cursor > 0);
368 assert(timer_hide_cursor_id != 0);
370 timer_hide_cursor_id = 0;
372 /* hide the cursor when mpd is playing and the user is inactive */
374 if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
375 lw->flags |= LW_HIDE_CURSOR;
376 playlist_repaint();
377 } else
378 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
379 timer_hide_cursor, c);
381 return FALSE;
382 }
384 static void
385 play_open(mpdclient_t *c)
386 {
387 static gboolean install_cb = TRUE;
389 playlist = &c->playlist;
391 assert(timer_hide_cursor_id == 0);
392 if (options.hide_cursor > 0) {
393 lw->flags &= ~LW_HIDE_CURSOR;
394 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
395 timer_hide_cursor, c);
396 }
398 if (install_cb) {
399 mpdclient_install_playlist_callback(c, playlist_changed_callback);
400 install_cb = FALSE;
401 }
402 }
404 static void
405 play_close(void)
406 {
407 if (timer_hide_cursor_id != 0) {
408 g_source_remove(timer_hide_cursor_id);
409 timer_hide_cursor_id = 0;
410 }
411 }
413 static void
414 play_resize(int cols, int rows)
415 {
416 lw->cols = cols;
417 lw->rows = rows;
418 }
421 static void
422 play_exit(void)
423 {
424 list_window_free(lw);
425 }
427 static const char *
428 play_title(char *str, size_t size)
429 {
430 if( strcmp(options.host, "localhost") == 0 )
431 return _("Playlist");
433 g_snprintf(str, size, _("Playlist on %s"), options.host);
434 return str;
435 }
437 static void
438 play_paint(void)
439 {
440 list_window_paint(lw, list_callback, NULL);
441 }
443 static void
444 play_update(mpdclient_t *c)
445 {
446 static int prev_song_id = -1;
448 current_song_id = c->song != NULL && c->status != NULL &&
449 !IS_STOPPED(c->status->state) ? c->song->id : -1;
451 if (current_song_id != prev_song_id) {
452 prev_song_id = current_song_id;
454 /* center the cursor */
455 if (options.auto_center && current_song_id != -1)
456 center_playing_item(c);
458 playlist_repaint();
459 }
460 }
462 #ifdef HAVE_GETMOUSE
463 static bool
464 handle_mouse_event(struct mpdclient *c)
465 {
466 int row;
467 unsigned selected;
468 unsigned long bstate;
470 if (screen_get_mouse_event(c, &bstate, &row) ||
471 list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
472 playlist_repaint();
473 return true;
474 }
476 if (bstate & BUTTON1_DOUBLE_CLICKED) {
477 /* stop */
478 screen_cmd(c, CMD_STOP);
479 return true;
480 }
482 selected = lw->start + row;
484 if (bstate & BUTTON1_CLICKED) {
485 /* play */
486 if (lw->start + row < playlist_length(playlist))
487 mpdclient_cmd_play(c, lw->start + row);
488 } else if (bstate & BUTTON3_CLICKED) {
489 /* delete */
490 if (selected == lw->selected)
491 mpdclient_cmd_delete(c, lw->selected);
492 }
494 lw->selected = selected;
495 list_window_check_selected(lw, playlist_length(playlist));
496 playlist_repaint();
498 return true;
499 }
500 #endif
502 static bool
503 play_cmd(mpdclient_t *c, command_t cmd)
504 {
505 lw->flags &= ~LW_HIDE_CURSOR;
507 if (options.hide_cursor > 0) {
508 if (timer_hide_cursor_id != 0)
509 g_source_remove(timer_hide_cursor_id);
510 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
511 timer_hide_cursor, c);
512 }
514 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
515 playlist_repaint();
516 return true;
517 }
519 switch(cmd) {
520 case CMD_PLAY:
521 mpdclient_cmd_play(c, lw->selected);
522 return true;
523 case CMD_DELETE:
524 mpdclient_cmd_delete(c, lw->selected);
525 return true;
526 case CMD_SAVE_PLAYLIST:
527 playlist_save(c, NULL, NULL);
528 return true;
529 case CMD_ADD:
530 handle_add_to_playlist(c);
531 return true;
532 case CMD_SCREEN_UPDATE:
533 center_playing_item(c);
534 playlist_repaint();
535 return false;
537 case CMD_LIST_MOVE_UP:
538 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
539 return true;
540 case CMD_LIST_MOVE_DOWN:
541 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
542 return true;
543 case CMD_LIST_FIND:
544 case CMD_LIST_RFIND:
545 case CMD_LIST_FIND_NEXT:
546 case CMD_LIST_RFIND_NEXT:
547 screen_find(lw, playlist_length(&c->playlist),
548 cmd, list_callback, NULL);
549 playlist_repaint();
550 return true;
552 #ifdef HAVE_GETMOUSE
553 case CMD_MOUSE_EVENT:
554 return handle_mouse_event(c);
555 #endif
557 case CMD_LOCATE:
558 if (lw->selected < playlist_length(&c->playlist)) {
559 screen_file_goto_song(c, playlist_get(&c->playlist, lw->selected));
560 return true;
561 }
563 break;
565 #ifdef ENABLE_LYRICS_SCREEN
566 case CMD_SCREEN_LYRICS:
567 if (lw->selected < playlist_length(&c->playlist)) {
568 screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
569 return true;
570 }
572 break;
573 #endif
575 default:
576 break;
577 }
579 return false;
580 }
582 const struct screen_functions screen_playlist = {
583 .init = play_init,
584 .exit = play_exit,
585 .open = play_open,
586 .close = play_close,
587 .resize = play_resize,
588 .paint = play_paint,
589 .update = play_update,
590 .cmd = play_cmd,
591 .get_title = play_title,
592 };