d4fb7cee239f3076f6202f889379c4270fd98300
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 "options.h"
22 #include "support.h"
23 #include "mpdclient.h"
24 #include "utils.h"
25 #include "strfsong.h"
26 #include "wreadln.h"
27 #include "command.h"
28 #include "colors.h"
29 #include "screen.h"
30 #include "screen_utils.h"
31 #include "screen_play.h"
32 #include "gcc.h"
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <glib.h>
40 #define MAX_SONG_LENGTH 512
42 #ifndef NCMPC_MINI
43 typedef struct
44 {
45 GList **list;
46 GList **dir_list;
47 mpdclient_t *c;
48 } completion_callback_data_t;
49 #endif
51 static struct mpdclient_playlist *playlist;
52 static int current_song_id = -1;
53 static list_window_t *lw = NULL;
54 static guint timer_hide_cursor_id;
56 static void
57 play_paint(void);
59 static void
60 playlist_repaint(void)
61 {
62 play_paint();
63 wrefresh(lw->w);
64 }
66 static void
67 playlist_repaint_if_active(void)
68 {
69 if (screen_is_visible(&screen_playlist))
70 playlist_repaint();
71 }
73 static void
74 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
75 {
76 switch (event) {
77 case PLAYLIST_EVENT_DELETE:
78 break;
79 case PLAYLIST_EVENT_MOVE:
80 lw->selected = *((int *) data);
81 if (lw->selected < lw->start)
82 lw->start--;
83 break;
84 default:
85 break;
86 }
88 list_window_check_selected(lw, c->playlist.list->len);
89 playlist_repaint_if_active();
90 }
92 static const char *
93 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
94 {
95 static char songname[MAX_SONG_LENGTH];
96 mpd_Song *song;
98 if (playlist == NULL || idx >= playlist_length(playlist))
99 return NULL;
101 song = playlist_get(playlist, idx);
102 if (song->id == current_song_id)
103 *highlight = 1;
105 strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
106 return songname;
107 }
109 static void
110 center_playing_item(mpdclient_t *c)
111 {
112 unsigned length = c->playlist.list->len;
113 unsigned offset = lw->selected - lw->start;
114 int idx;
116 if (!c->song || length < lw->rows ||
117 c->status == NULL || IS_STOPPED(c->status->state))
118 return;
120 /* try to center the song that are playing */
121 idx = playlist_get_index(c, c->song);
122 if (idx < 0)
123 return;
125 list_window_center(lw, length, idx);
127 /* make sure the cursor is in the window */
128 lw->selected = lw->start+offset;
129 list_window_check_selected(lw, length);
130 }
132 #ifndef NCMPC_MINI
133 static void
134 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
135 {
136 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
137 GList **list = tmp->list;
138 mpdclient_t *c = tmp->c;
140 if( *list == NULL ) {
141 /* create completion list */
142 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
143 g_completion_add_items(gcmp, *list);
144 }
145 }
147 static void
148 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
149 GList *items, mpd_unused void *data)
150 {
151 if (g_list_length(items) >= 1)
152 screen_display_completion_list(items);
153 }
154 #endif
156 int
157 playlist_save(mpdclient_t *c, char *name, char *defaultname)
158 {
159 gchar *filename;
160 gint error;
161 #ifndef NCMPC_MINI
162 GCompletion *gcmp;
163 GList *list = NULL;
164 completion_callback_data_t data;
165 #endif
167 #ifdef NCMPC_MINI
168 (void)defaultname;
169 #endif
171 #ifndef NCMPC_MINI
172 if (name == NULL) {
173 /* initialize completion support */
174 gcmp = g_completion_new(NULL);
175 g_completion_set_compare(gcmp, strncmp);
176 data.list = &list;
177 data.dir_list = NULL;
178 data.c = c;
179 wrln_completion_callback_data = &data;
180 wrln_pre_completion_callback = save_pre_completion_cb;
181 wrln_post_completion_callback = save_post_completion_cb;
184 /* query the user for a filename */
185 filename = screen_readln(screen.status_window.w,
186 _("Save playlist as: "),
187 defaultname,
188 NULL,
189 gcmp);
191 /* destroy completion support */
192 wrln_completion_callback_data = NULL;
193 wrln_pre_completion_callback = NULL;
194 wrln_post_completion_callback = NULL;
195 g_completion_free(gcmp);
196 list = string_list_free(list);
197 if( filename )
198 filename=g_strstrip(filename);
199 } else
200 #endif
201 filename=g_strdup(name);
203 if (filename == NULL || filename[0] == '\0')
204 return -1;
206 /* send save command to mpd */
207 if ((error = mpdclient_cmd_save_playlist(c, filename))) {
208 gint code = GET_ACK_ERROR_CODE(error);
210 if (code == MPD_ACK_ERROR_EXIST) {
211 char *buf;
212 int key;
214 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
215 filename, YES, NO);
216 key = tolower(screen_getch(screen.status_window.w,
217 buf));
218 g_free(buf);
220 if (key == YES[0]) {
221 if (mpdclient_cmd_delete_playlist(c, filename)) {
222 g_free(filename);
223 return -1;
224 }
226 error = playlist_save(c, filename, NULL);
227 g_free(filename);
228 return error;
229 }
231 screen_status_printf(_("Aborted!"));
232 }
234 g_free(filename);
235 return -1;
236 }
238 /* success */
239 screen_status_printf(_("Saved %s"), filename);
240 g_free(filename);
241 return 0;
242 }
244 #ifndef NCMPC_MINI
245 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
246 GList **list, mpdclient_t *c)
247 {
248 g_completion_remove_items(gcmp, *list);
249 *list = string_list_remove(*list, dir);
250 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
251 g_completion_add_items(gcmp, *list);
252 *dir_list = g_list_append(*dir_list, g_strdup(dir));
253 }
255 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
256 {
257 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
258 GList **dir_list = tmp->dir_list;
259 GList **list = tmp->list;
260 mpdclient_t *c = tmp->c;
262 if (*list == NULL) {
263 /* create initial list */
264 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
265 g_completion_add_items(gcmp, *list);
266 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
267 string_list_find(*dir_list, line) == NULL) {
268 /* add directory content to list */
269 add_dir(gcmp, line, dir_list, list, c);
270 }
271 }
273 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
274 GList *items, void *data)
275 {
276 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
277 GList **dir_list = tmp->dir_list;
278 GList **list = tmp->list;
279 mpdclient_t *c = tmp->c;
281 if (g_list_length(items) >= 1)
282 screen_display_completion_list(items);
284 if (line && line[0] && line[strlen(line) - 1] == '/' &&
285 string_list_find(*dir_list, line) == NULL) {
286 /* add directory content to list */
287 add_dir(gcmp, line, dir_list, list, c);
288 }
289 }
290 #endif
292 static int
293 handle_add_to_playlist(mpdclient_t *c)
294 {
295 gchar *path;
296 #ifndef NCMPC_MINI
297 GCompletion *gcmp;
298 GList *list = NULL;
299 GList *dir_list = NULL;
300 completion_callback_data_t data;
302 /* initialize completion support */
303 gcmp = g_completion_new(NULL);
304 g_completion_set_compare(gcmp, strncmp);
305 data.list = &list;
306 data.dir_list = &dir_list;
307 data.c = c;
308 wrln_completion_callback_data = &data;
309 wrln_pre_completion_callback = add_pre_completion_cb;
310 wrln_post_completion_callback = add_post_completion_cb;
311 #endif
313 /* get path */
314 path = screen_readln(screen.status_window.w,
315 _("Add: "),
316 NULL,
317 NULL,
318 #ifdef NCMPC_MINI
319 NULL
320 #else
321 gcmp
322 #endif
323 );
325 /* destroy completion data */
326 #ifndef NCMPC_MINI
327 wrln_completion_callback_data = NULL;
328 wrln_pre_completion_callback = NULL;
329 wrln_post_completion_callback = NULL;
330 g_completion_free(gcmp);
331 string_list_free(list);
332 string_list_free(dir_list);
333 #endif
335 /* add the path to the playlist */
336 if (path && path[0])
337 mpdclient_cmd_add_path(c, path);
339 g_free(path);
340 return 0;
341 }
343 static void
344 play_init(WINDOW *w, int cols, int rows)
345 {
346 lw = list_window_init(w, cols, rows);
347 }
349 static gboolean
350 timer_hide_cursor(gpointer data)
351 {
352 struct mpdclient *c = data;
354 assert(options.hide_cursor > 0);
355 assert(timer_hide_cursor_id != 0);
357 timer_hide_cursor_id = 0;
359 /* hide the cursor when mpd is playing and the user is inactive */
361 if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
362 lw->flags |= LW_HIDE_CURSOR;
363 playlist_repaint();
364 } else
365 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
366 timer_hide_cursor, c);
368 return FALSE;
369 }
371 static void
372 play_open(mpdclient_t *c)
373 {
374 static gboolean install_cb = TRUE;
376 playlist = &c->playlist;
378 assert(timer_hide_cursor_id == 0);
379 if (options.hide_cursor > 0) {
380 lw->flags &= ~LW_HIDE_CURSOR;
381 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
382 timer_hide_cursor, c);
383 }
385 if (install_cb) {
386 mpdclient_install_playlist_callback(c, playlist_changed_callback);
387 install_cb = FALSE;
388 }
389 }
391 static void
392 play_close(void)
393 {
394 if (timer_hide_cursor_id != 0) {
395 g_source_remove(timer_hide_cursor_id);
396 timer_hide_cursor_id = 0;
397 }
398 }
400 static void
401 play_resize(int cols, int rows)
402 {
403 lw->cols = cols;
404 lw->rows = rows;
405 }
408 static void
409 play_exit(void)
410 {
411 list_window_free(lw);
412 }
414 static const char *
415 play_title(char *str, size_t size)
416 {
417 if( strcmp(options.host, "localhost") == 0 )
418 return _("Playlist");
420 g_snprintf(str, size, _("Playlist on %s"), options.host);
421 return str;
422 }
424 static void
425 play_paint(void)
426 {
427 list_window_paint(lw, list_callback, NULL);
428 }
430 static void
431 play_update(mpdclient_t *c)
432 {
433 static int prev_song_id = -1;
435 current_song_id = c->song != NULL && c->status != NULL &&
436 !IS_STOPPED(c->status->state) ? c->song->id : -1;
438 if (current_song_id != prev_song_id) {
439 prev_song_id = current_song_id;
441 /* center the cursor */
442 if (options.auto_center && current_song_id != -1)
443 center_playing_item(c);
445 playlist_repaint();
446 }
447 }
449 #ifdef HAVE_GETMOUSE
450 static int
451 handle_mouse_event(struct mpdclient *c)
452 {
453 int row;
454 unsigned selected;
455 unsigned long bstate;
457 if (screen_get_mouse_event(c, &bstate, &row) ||
458 list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
459 playlist_repaint();
460 return 1;
461 }
463 if (bstate & BUTTON1_DOUBLE_CLICKED) {
464 /* stop */
465 screen_cmd(c, CMD_STOP);
466 return 1;
467 }
469 selected = lw->start + row;
471 if (bstate & BUTTON1_CLICKED) {
472 /* play */
473 if (lw->start + row < playlist_length(playlist))
474 mpdclient_cmd_play(c, lw->start + row);
475 } else if (bstate & BUTTON3_CLICKED) {
476 /* delete */
477 if (selected == lw->selected)
478 mpdclient_cmd_delete(c, lw->selected);
479 }
481 lw->selected = selected;
482 list_window_check_selected(lw, playlist_length(playlist));
483 playlist_repaint();
485 return 1;
486 }
487 #endif
489 static int
490 play_cmd(mpdclient_t *c, command_t cmd)
491 {
492 lw->flags &= ~LW_HIDE_CURSOR;
494 if (options.hide_cursor > 0) {
495 if (timer_hide_cursor_id != 0)
496 g_source_remove(timer_hide_cursor_id);
497 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
498 timer_hide_cursor, c);
499 }
501 if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
502 playlist_repaint();
503 return 1;
504 }
506 switch(cmd) {
507 case CMD_PLAY:
508 mpdclient_cmd_play(c, lw->selected);
509 return 1;
510 case CMD_DELETE:
511 mpdclient_cmd_delete(c, lw->selected);
512 return 1;
513 case CMD_SAVE_PLAYLIST:
514 playlist_save(c, NULL, NULL);
515 return 1;
516 case CMD_ADD:
517 handle_add_to_playlist(c);
518 return 1;
519 case CMD_SCREEN_UPDATE:
520 center_playing_item(c);
521 playlist_repaint();
522 return 0;
524 case CMD_LIST_MOVE_UP:
525 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
526 return 1;
527 case CMD_LIST_MOVE_DOWN:
528 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
529 return 1;
530 case CMD_LIST_FIND:
531 case CMD_LIST_RFIND:
532 case CMD_LIST_FIND_NEXT:
533 case CMD_LIST_RFIND_NEXT:
534 screen_find(lw, playlist_length(&c->playlist),
535 cmd, list_callback, NULL);
536 playlist_repaint();
537 return 1;
539 #ifdef HAVE_GETMOUSE
540 case CMD_MOUSE_EVENT:
541 return handle_mouse_event(c);
542 #endif
544 #ifdef ENABLE_LYRICS_SCREEN
545 case CMD_SCREEN_LYRICS:
546 if (lw->selected < playlist_length(&c->playlist)) {
547 screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
548 return 1;
549 }
551 break;
552 #endif
554 default:
555 break;
556 }
558 return 0;
559 }
561 const struct screen_functions screen_playlist = {
562 .init = play_init,
563 .exit = play_exit,
564 .open = play_open,
565 .close = play_close,
566 .resize = play_resize,
567 .paint = play_paint,
568 .update = play_update,
569 .cmd = play_cmd,
570 .get_title = play_title,
571 };