1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include "config.h"
22 #include "ncmpc.h"
23 #include "options.h"
24 #include "support.h"
25 #include "mpdclient.h"
26 #include "utils.h"
27 #include "strfsong.h"
28 #include "wreadln.h"
29 #include "command.h"
30 #include "colors.h"
31 #include "screen.h"
32 #include "screen_utils.h"
33 #include "screen_play.h"
34 #include "gcc.h"
36 #include <ctype.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 #include <glib.h>
41 #include <ncurses.h>
43 #define MAX_SONG_LENGTH 512
45 typedef struct
46 {
47 GList **list;
48 GList **dir_list;
49 screen_t *screen;
50 mpdclient_t *c;
51 } completion_callback_data_t;
53 static list_window_t *lw = NULL;
55 static void
56 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
57 {
58 D("screen_play.c> playlist_callback() [%d]\n", event);
59 switch(event) {
60 case PLAYLIST_EVENT_DELETE:
61 break;
62 case PLAYLIST_EVENT_MOVE:
63 lw->selected = *((int *) data);
64 if( lw->selected<lw->start )
65 lw->start--;
66 break;
67 default:
68 break;
69 }
70 /* make shure the playlist is repainted */
71 lw->clear = 1;
72 lw->repaint = 1;
73 list_window_check_selected(lw, c->playlist.list->len);
74 }
76 static const char *
77 list_callback(unsigned idx, int *highlight, void *data)
78 {
79 static char songname[MAX_SONG_LENGTH];
80 mpdclient_t *c = (mpdclient_t *) data;
81 mpd_Song *song;
83 if( (song=playlist_get_song(c, idx)) == NULL ) {
84 return NULL;
85 }
87 if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) ) {
88 *highlight = 1;
89 }
90 strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
91 return songname;
92 }
94 static int
95 center_playing_item(mpdclient_t *c)
96 {
97 unsigned length = c->playlist.list->len;
98 unsigned offset = lw->selected - lw->start;
99 int idx;
101 if (!lw || !c->song || length<lw->rows ||
102 IS_STOPPED(c->status->state))
103 return 0;
105 /* try to center the song that are playing */
106 idx = playlist_get_index(c, c->song);
107 D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,idx);
108 if (idx < 0)
109 return 0;
111 list_window_center(lw, length, idx);
113 /* make sure the cursor is in the window */
114 lw->selected = lw->start+offset;
115 list_window_check_selected(lw, length);
117 return 0;
118 }
120 static void
121 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
122 {
123 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
124 GList **list = tmp->list;
125 mpdclient_t *c = tmp->c;
127 if( *list == NULL ) {
128 /* create completion list */
129 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
130 g_completion_add_items(gcmp, *list);
131 }
132 }
134 static void
135 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
136 GList *items, void *data)
137 {
138 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
139 screen_t *screen = tmp->screen;
141 if( g_list_length(items)>=1 ) {
142 screen_display_completion_list(screen, items);
143 lw->clear = 1;
144 lw->repaint = 1;
145 }
146 }
148 int
149 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
150 {
151 gchar *filename;
152 gint error;
153 GCompletion *gcmp;
154 GList *list = NULL;
155 completion_callback_data_t data;
157 if( name==NULL )
158 {
159 /* initialize completion support */
160 gcmp = g_completion_new(NULL);
161 g_completion_set_compare(gcmp, strncmp);
162 data.list = &list;
163 data.dir_list = NULL;
164 data.screen = screen;
165 data.c = c;
166 wrln_completion_callback_data = &data;
167 wrln_pre_completion_callback = save_pre_completion_cb;
168 wrln_post_completion_callback = save_post_completion_cb;
171 /* query the user for a filename */
172 filename = screen_readln(screen->status_window.w,
173 _("Save playlist as: "),
174 defaultname,
175 NULL,
176 gcmp);
178 /* destroy completion support */
179 wrln_completion_callback_data = NULL;
180 wrln_pre_completion_callback = NULL;
181 wrln_post_completion_callback = NULL;
182 g_completion_free(gcmp);
183 list = string_list_free(list);
184 if( filename )
185 filename=g_strstrip(filename);
186 }
187 else
188 {
189 filename=g_strdup(name);
190 }
191 if( filename==NULL || filename[0]=='\0' )
192 return -1;
193 /* send save command to mpd */
194 D("Saving playlist as \'%s \'...\n", filename);
195 if( (error=mpdclient_cmd_save_playlist(c, filename)) )
196 {
197 gint code = GET_ACK_ERROR_CODE(error);
199 if( code == MPD_ACK_ERROR_EXIST )
200 {
201 char *buf;
202 int key;
204 buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO);
205 key = tolower(screen_getch(screen->status_window.w, buf));
206 g_free(buf);
207 if( key == YES[0] )
208 {
209 if( mpdclient_cmd_delete_playlist(c, filename) )
210 {
211 g_free(filename);
212 return -1;
213 }
214 error = playlist_save(screen, c, filename, NULL);
215 g_free(filename);
216 return error;
217 }
218 screen_status_printf(_("Aborted!"));
219 }
220 g_free(filename);
221 return -1;
222 }
223 /* success */
224 screen_status_printf(_("Saved %s"), filename);
225 g_free(filename);
226 return 0;
227 }
229 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
230 GList **list, mpdclient_t *c)
231 {
232 g_completion_remove_items(gcmp, *list);
233 *list = string_list_remove(*list, dir);
234 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
235 g_completion_add_items(gcmp, *list);
236 *dir_list = g_list_append(*dir_list, g_strdup(dir));
237 }
239 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
240 {
241 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
242 GList **dir_list = tmp->dir_list;
243 GList **list = tmp->list;
244 mpdclient_t *c = tmp->c;
246 D("pre_completion()...\n");
247 if( *list == NULL )
248 {
249 /* create initial list */
250 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
251 g_completion_add_items(gcmp, *list);
252 }
253 else if( line && line[0] && line[strlen(line)-1]=='/' &&
254 string_list_find(*dir_list, line) == NULL )
255 {
256 /* add directory content to list */
257 add_dir(gcmp, line, dir_list, list, c);
258 }
259 }
261 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
262 GList *items, void *data)
263 {
264 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
265 GList **dir_list = tmp->dir_list;
266 GList **list = tmp->list;
267 mpdclient_t *c = tmp->c;
268 screen_t *screen = tmp->screen;
270 D("post_completion()...\n");
271 if( g_list_length(items)>=1 )
272 {
273 screen_display_completion_list(screen, items);
274 lw->clear = 1;
275 lw->repaint = 1;
276 }
278 if( line && line[0] && line[strlen(line)-1]=='/' &&
279 string_list_find(*dir_list, line) == NULL )
280 {
281 /* add directory content to list */
282 add_dir(gcmp, line, dir_list, list, c);
283 }
284 }
286 static int
287 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
288 {
289 gchar *path;
290 GCompletion *gcmp;
291 GList *list = NULL;
292 GList *dir_list = NULL;
293 completion_callback_data_t data;
295 /* initialize completion support */
296 gcmp = g_completion_new(NULL);
297 g_completion_set_compare(gcmp, strncmp);
298 data.list = &list;
299 data.dir_list = &dir_list;
300 data.screen = screen;
301 data.c = c;
302 wrln_completion_callback_data = &data;
303 wrln_pre_completion_callback = add_pre_completion_cb;
304 wrln_post_completion_callback = add_post_completion_cb;
305 /* get path */
306 path = screen_readln(screen->status_window.w,
307 _("Add: "),
308 NULL,
309 NULL,
310 gcmp);
312 /* destroy completion data */
313 wrln_completion_callback_data = NULL;
314 wrln_pre_completion_callback = NULL;
315 wrln_post_completion_callback = NULL;
316 g_completion_free(gcmp);
317 string_list_free(list);
318 string_list_free(dir_list);
320 /* add the path to the playlist */
321 if( path && path[0] )
322 mpdclient_cmd_add_path(c, path);
324 return 0;
325 }
327 static void
328 play_init(WINDOW *w, int cols, int rows)
329 {
330 lw = list_window_init(w, cols, rows);
331 }
333 static void
334 play_open(mpd_unused screen_t *screen, mpdclient_t *c)
335 {
336 static gboolean install_cb = TRUE;
338 if (install_cb) {
339 mpdclient_install_playlist_callback(c, playlist_changed_callback);
340 install_cb = FALSE;
341 }
342 }
344 static void
345 play_resize(int cols, int rows)
346 {
347 lw->cols = cols;
348 lw->rows = rows;
349 }
352 static void
353 play_exit(void)
354 {
355 list_window_free(lw);
356 }
358 static const char *
359 play_title(char *str, size_t size)
360 {
361 if( strcmp(options.host, "localhost") == 0 )
362 return _("Playlist");
364 g_snprintf(str, size, _("Playlist on %s"), options.host);
365 return str;
366 }
368 static void
369 play_paint(mpd_unused screen_t *screen, mpdclient_t *c)
370 {
371 lw->clear = 1;
373 list_window_paint(lw, list_callback, (void *) c);
374 wnoutrefresh(lw->w);
375 }
377 static void
378 play_update(screen_t *screen, mpdclient_t *c)
379 {
380 /* hide the cursor when mpd are playing and the user are inactive */
381 if( options.hide_cursor>0 && c->status->state == MPD_STATUS_STATE_PLAY &&
382 time(NULL)-screen->input_timestamp >= options.hide_cursor ) {
383 lw->flags |= LW_HIDE_CURSOR;
384 } else {
385 lw->flags &= ~LW_HIDE_CURSOR;
386 }
388 /* center the cursor */
389 if( options.auto_center ) {
390 static int prev_song_id = 0;
392 if( c->song && prev_song_id != c->song->id ) {
393 center_playing_item(c);
394 prev_song_id = c->song->id;
395 }
396 }
398 if( c->playlist.updated ) {
399 if (lw->selected >= c->playlist.list->len)
400 lw->selected = c->playlist.list->len - 1;
401 if (lw->start >= c->playlist.list->len)
402 list_window_reset(lw);
404 play_paint(screen, c);
405 c->playlist.updated = FALSE;
406 } else if( lw->repaint || 1) {
407 list_window_paint(lw, list_callback, (void *) c);
408 wnoutrefresh(lw->w);
409 lw->repaint = 0;
410 }
411 }
413 #ifdef HAVE_GETMOUSE
414 static int
415 handle_mouse_event(mpd_unused screen_t *screen, mpdclient_t *c)
416 {
417 int row;
418 unsigned selected;
419 unsigned long bstate;
421 if (screen_get_mouse_event(c, lw, c->playlist.list->len, &bstate, &row))
422 return 1;
424 if (bstate & BUTTON1_DOUBLE_CLICKED) {
425 /* stop */
426 screen_cmd(c, CMD_STOP);
427 return 1;
428 }
430 selected = lw->start + row;
432 if (bstate & BUTTON1_CLICKED) {
433 /* play */
434 if (lw->start + row < c->playlist.list->len)
435 mpdclient_cmd_play(c, lw->start + row);
436 } else if (bstate & BUTTON3_CLICKED) {
437 /* delete */
438 if (selected == lw->selected)
439 mpdclient_cmd_delete(c, lw->selected);
440 }
442 lw->selected = selected;
443 list_window_check_selected(lw, c->playlist.list->len);
445 return 1;
446 }
447 #else
448 #define handle_mouse_event(s,c) (0)
449 #endif
451 static int
452 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
453 {
454 switch(cmd) {
455 case CMD_PLAY:
456 mpdclient_cmd_play(c, lw->selected);
457 return 1;
458 case CMD_DELETE:
459 mpdclient_cmd_delete(c, lw->selected);
460 return 1;
461 case CMD_SAVE_PLAYLIST:
462 playlist_save(screen, c, NULL, NULL);
463 return 1;
464 case CMD_ADD:
465 handle_add_to_playlist(screen, c);
466 return 1;
467 case CMD_SCREEN_UPDATE:
468 screen->painted = 0;
469 lw->clear = 1;
470 lw->repaint = 1;
471 center_playing_item(c);
472 return 1;
473 case CMD_LIST_MOVE_UP:
474 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
475 return 1;
476 case CMD_LIST_MOVE_DOWN:
477 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
478 return 1;
479 case CMD_LIST_FIND:
480 case CMD_LIST_RFIND:
481 case CMD_LIST_FIND_NEXT:
482 case CMD_LIST_RFIND_NEXT:
483 return screen_find(screen,
484 lw, c->playlist.list->len,
485 cmd, list_callback, (void *) c);
486 case CMD_MOUSE_EVENT:
487 return handle_mouse_event(screen,c);
488 default:
489 break;
490 }
491 return list_window_cmd(lw, c->playlist.list->len, cmd);
492 }
494 const struct screen_functions screen_playlist = {
495 .init = play_init,
496 .exit = play_exit,
497 .open = play_open,
498 .close = NULL,
499 .resize = play_resize,
500 .paint = play_paint,
501 .update = play_update,
502 .cmd = play_cmd,
503 .get_title = play_title,
504 };