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 GTime input_timestamp;
54 static list_window_t *lw = NULL;
55 static long long playlist_id;
57 static void
58 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
59 {
60 D("screen_play.c> playlist_callback() [%d]\n", event);
61 switch(event) {
62 case PLAYLIST_EVENT_DELETE:
63 break;
64 case PLAYLIST_EVENT_MOVE:
65 lw->selected = *((int *) data);
66 if( lw->selected<lw->start )
67 lw->start--;
68 break;
69 default:
70 break;
71 }
72 /* make shure the playlist is repainted */
73 lw->clear = 1;
74 list_window_check_selected(lw, c->playlist.list->len);
75 }
77 static const char *
78 list_callback(unsigned idx, int *highlight, void *data)
79 {
80 static char songname[MAX_SONG_LENGTH];
81 mpdclient_t *c = (mpdclient_t *) data;
82 mpd_Song *song;
84 if (idx >= playlist_length(&c->playlist))
85 return NULL;
87 song = playlist_get(&c->playlist, idx);
89 if (c->song != NULL && song->id == c->song->id &&
90 c->status != NULL && !IS_STOPPED(c->status->state))
91 *highlight = 1;
93 strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
94 return songname;
95 }
97 static void
98 center_playing_item(mpdclient_t *c)
99 {
100 unsigned length = c->playlist.list->len;
101 unsigned offset = lw->selected - lw->start;
102 int idx;
104 if (!c->song || length < lw->rows ||
105 c->status == NULL || IS_STOPPED(c->status->state))
106 return;
108 /* try to center the song that are playing */
109 idx = playlist_get_index(c, c->song);
110 D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,idx);
111 if (idx < 0)
112 return;
114 list_window_center(lw, length, idx);
116 /* make sure the cursor is in the window */
117 lw->selected = lw->start+offset;
118 list_window_check_selected(lw, length);
120 return;
121 }
123 static void
124 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
125 {
126 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
127 GList **list = tmp->list;
128 mpdclient_t *c = tmp->c;
130 if( *list == NULL ) {
131 /* create completion list */
132 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
133 g_completion_add_items(gcmp, *list);
134 }
135 }
137 static void
138 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
139 GList *items, void *data)
140 {
141 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
142 screen_t *screen = tmp->screen;
144 if( g_list_length(items)>=1 ) {
145 screen_display_completion_list(screen, items);
146 lw->clear = 1;
147 }
148 }
150 int
151 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
152 {
153 gchar *filename;
154 gint error;
155 GCompletion *gcmp;
156 GList *list = NULL;
157 completion_callback_data_t data;
159 if( name==NULL )
160 {
161 /* initialize completion support */
162 gcmp = g_completion_new(NULL);
163 g_completion_set_compare(gcmp, strncmp);
164 data.list = &list;
165 data.dir_list = NULL;
166 data.screen = screen;
167 data.c = c;
168 wrln_completion_callback_data = &data;
169 wrln_pre_completion_callback = save_pre_completion_cb;
170 wrln_post_completion_callback = save_post_completion_cb;
173 /* query the user for a filename */
174 filename = screen_readln(screen->status_window.w,
175 _("Save playlist as: "),
176 defaultname,
177 NULL,
178 gcmp);
180 /* destroy completion support */
181 wrln_completion_callback_data = NULL;
182 wrln_pre_completion_callback = NULL;
183 wrln_post_completion_callback = NULL;
184 g_completion_free(gcmp);
185 list = string_list_free(list);
186 if( filename )
187 filename=g_strstrip(filename);
188 }
189 else
190 {
191 filename=g_strdup(name);
192 }
193 if( filename==NULL || filename[0]=='\0' )
194 return -1;
195 /* send save command to mpd */
196 D("Saving playlist as \'%s \'...\n", filename);
197 if( (error=mpdclient_cmd_save_playlist(c, filename)) )
198 {
199 gint code = GET_ACK_ERROR_CODE(error);
201 if( code == MPD_ACK_ERROR_EXIST )
202 {
203 char *buf;
204 int key;
206 buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO);
207 key = tolower(screen_getch(screen->status_window.w, buf));
208 g_free(buf);
209 if( key == YES[0] )
210 {
211 if( mpdclient_cmd_delete_playlist(c, filename) )
212 {
213 g_free(filename);
214 return -1;
215 }
216 error = playlist_save(screen, c, filename, NULL);
217 g_free(filename);
218 return error;
219 }
220 screen_status_printf(_("Aborted!"));
221 }
222 g_free(filename);
223 return -1;
224 }
225 /* success */
226 screen_status_printf(_("Saved %s"), filename);
227 g_free(filename);
228 return 0;
229 }
231 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
232 GList **list, mpdclient_t *c)
233 {
234 g_completion_remove_items(gcmp, *list);
235 *list = string_list_remove(*list, dir);
236 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
237 g_completion_add_items(gcmp, *list);
238 *dir_list = g_list_append(*dir_list, g_strdup(dir));
239 }
241 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
242 {
243 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
244 GList **dir_list = tmp->dir_list;
245 GList **list = tmp->list;
246 mpdclient_t *c = tmp->c;
248 D("pre_completion()...\n");
249 if( *list == NULL )
250 {
251 /* create initial list */
252 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
253 g_completion_add_items(gcmp, *list);
254 }
255 else if( line && line[0] && line[strlen(line)-1]=='/' &&
256 string_list_find(*dir_list, line) == NULL )
257 {
258 /* add directory content to list */
259 add_dir(gcmp, line, dir_list, list, c);
260 }
261 }
263 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
264 GList *items, void *data)
265 {
266 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
267 GList **dir_list = tmp->dir_list;
268 GList **list = tmp->list;
269 mpdclient_t *c = tmp->c;
270 screen_t *screen = tmp->screen;
272 D("post_completion()...\n");
273 if( g_list_length(items)>=1 )
274 {
275 screen_display_completion_list(screen, items);
276 lw->clear = 1;
277 }
279 if( line && line[0] && line[strlen(line)-1]=='/' &&
280 string_list_find(*dir_list, line) == NULL )
281 {
282 /* add directory content to list */
283 add_dir(gcmp, line, dir_list, list, c);
284 }
285 }
287 static int
288 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
289 {
290 gchar *path;
291 GCompletion *gcmp;
292 GList *list = NULL;
293 GList *dir_list = NULL;
294 completion_callback_data_t data;
296 /* initialize completion support */
297 gcmp = g_completion_new(NULL);
298 g_completion_set_compare(gcmp, strncmp);
299 data.list = &list;
300 data.dir_list = &dir_list;
301 data.screen = screen;
302 data.c = c;
303 wrln_completion_callback_data = &data;
304 wrln_pre_completion_callback = add_pre_completion_cb;
305 wrln_post_completion_callback = add_post_completion_cb;
306 /* get path */
307 path = screen_readln(screen->status_window.w,
308 _("Add: "),
309 NULL,
310 NULL,
311 gcmp);
313 /* destroy completion data */
314 wrln_completion_callback_data = NULL;
315 wrln_pre_completion_callback = NULL;
316 wrln_post_completion_callback = NULL;
317 g_completion_free(gcmp);
318 string_list_free(list);
319 string_list_free(dir_list);
321 /* add the path to the playlist */
322 if( path && path[0] )
323 mpdclient_cmd_add_path(c, path);
325 return 0;
326 }
328 static void
329 play_init(WINDOW *w, int cols, int rows)
330 {
331 lw = list_window_init(w, cols, rows);
332 }
334 static void
335 play_open(mpd_unused screen_t *screen, mpdclient_t *c)
336 {
337 static gboolean install_cb = TRUE;
339 input_timestamp = time(NULL);
341 if (install_cb) {
342 mpdclient_install_playlist_callback(c, playlist_changed_callback);
343 install_cb = FALSE;
344 }
345 }
347 static void
348 play_resize(int cols, int rows)
349 {
350 lw->cols = cols;
351 lw->rows = rows;
352 }
355 static void
356 play_exit(void)
357 {
358 list_window_free(lw);
359 }
361 static const char *
362 play_title(char *str, size_t size)
363 {
364 if( strcmp(options.host, "localhost") == 0 )
365 return _("Playlist");
367 g_snprintf(str, size, _("Playlist on %s"), options.host);
368 return str;
369 }
371 static void
372 play_paint(mpd_unused screen_t *screen, mpdclient_t *c)
373 {
374 lw->clear = 1;
376 list_window_paint(lw, list_callback, (void *) c);
377 wnoutrefresh(lw->w);
378 }
380 static void
381 play_update(screen_t *screen, mpdclient_t *c)
382 {
383 /* hide the cursor when mpd are playing and the user are inactive */
384 if (options.hide_cursor > 0 &&
385 (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) &&
386 time(NULL) - input_timestamp >= options.hide_cursor ) {
387 lw->flags |= LW_HIDE_CURSOR;
388 } else {
389 lw->flags &= ~LW_HIDE_CURSOR;
390 }
392 /* center the cursor */
393 if( options.auto_center ) {
394 static int prev_song_id = 0;
396 if( c->song && prev_song_id != c->song->id ) {
397 center_playing_item(c);
398 prev_song_id = c->song->id;
399 }
400 }
402 if (c->playlist.id != playlist_id) {
403 if (lw->selected >= c->playlist.list->len)
404 lw->selected = c->playlist.list->len - 1;
405 if (lw->start >= c->playlist.list->len)
406 list_window_reset(lw);
408 play_paint(screen, c);
409 playlist_id = c->playlist.id;
410 } else {
411 list_window_paint(lw, list_callback, (void *) c);
412 wnoutrefresh(lw->w);
413 }
414 }
416 #ifdef HAVE_GETMOUSE
417 static int
418 handle_mouse_event(mpd_unused screen_t *screen, mpdclient_t *c)
419 {
420 int row;
421 unsigned selected;
422 unsigned long bstate;
424 if (screen_get_mouse_event(c, &bstate, &row) ||
425 list_window_mouse(lw, c->playlist.list->len, bstate, row))
426 return 1;
428 if (bstate & BUTTON1_DOUBLE_CLICKED) {
429 /* stop */
430 screen_cmd(c, CMD_STOP);
431 return 1;
432 }
434 selected = lw->start + row;
436 if (bstate & BUTTON1_CLICKED) {
437 /* play */
438 if (lw->start + row < c->playlist.list->len)
439 mpdclient_cmd_play(c, lw->start + row);
440 } else if (bstate & BUTTON3_CLICKED) {
441 /* delete */
442 if (selected == lw->selected)
443 mpdclient_cmd_delete(c, lw->selected);
444 }
446 lw->selected = selected;
447 list_window_check_selected(lw, c->playlist.list->len);
449 return 1;
450 }
451 #else
452 #define handle_mouse_event(s,c) (0)
453 #endif
455 static int
456 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
457 {
458 input_timestamp = time(NULL);
460 switch(cmd) {
461 case CMD_PLAY:
462 mpdclient_cmd_play(c, lw->selected);
463 return 1;
464 case CMD_DELETE:
465 mpdclient_cmd_delete(c, lw->selected);
466 return 1;
467 case CMD_SAVE_PLAYLIST:
468 playlist_save(screen, c, NULL, NULL);
469 return 1;
470 case CMD_ADD:
471 handle_add_to_playlist(screen, c);
472 return 1;
473 case CMD_SCREEN_UPDATE:
474 center_playing_item(c);
475 return 0;
477 case CMD_LIST_MOVE_UP:
478 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
479 return 1;
480 case CMD_LIST_MOVE_DOWN:
481 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
482 return 1;
483 case CMD_LIST_FIND:
484 case CMD_LIST_RFIND:
485 case CMD_LIST_FIND_NEXT:
486 case CMD_LIST_RFIND_NEXT:
487 return screen_find(screen,
488 lw, c->playlist.list->len,
489 cmd, list_callback, (void *) c);
490 case CMD_MOUSE_EVENT:
491 return handle_mouse_event(screen,c);
492 default:
493 break;
494 }
495 return list_window_cmd(lw, c->playlist.list->len, cmd);
496 }
498 const struct screen_functions screen_playlist = {
499 .init = play_init,
500 .exit = play_exit,
501 .open = play_open,
502 .close = NULL,
503 .resize = play_resize,
504 .paint = play_paint,
505 .update = play_update,
506 .cmd = play_cmd,
507 .get_title = play_title,
508 };