c89d150cff17c59beeaae70eddb5ff9ae095ca84
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 (idx >= playlist_length(&c->playlist))
84 return NULL;
86 song = playlist_get(&c->playlist, idx);
88 if (c->song != NULL && song->id == c->song->id &&
89 c->status != NULL && !IS_STOPPED(c->status->state))
90 *highlight = 1;
92 strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
93 return songname;
94 }
96 static void
97 center_playing_item(mpdclient_t *c)
98 {
99 unsigned length = c->playlist.list->len;
100 unsigned offset = lw->selected - lw->start;
101 int idx;
103 if (!c->song || length < lw->rows ||
104 c->status == NULL || IS_STOPPED(c->status->state))
105 return;
107 /* try to center the song that are playing */
108 idx = playlist_get_index(c, c->song);
109 D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,idx);
110 if (idx < 0)
111 return;
113 list_window_center(lw, length, idx);
115 /* make sure the cursor is in the window */
116 lw->selected = lw->start+offset;
117 list_window_check_selected(lw, length);
119 return;
120 }
122 static void
123 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
124 {
125 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
126 GList **list = tmp->list;
127 mpdclient_t *c = tmp->c;
129 if( *list == NULL ) {
130 /* create completion list */
131 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
132 g_completion_add_items(gcmp, *list);
133 }
134 }
136 static void
137 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
138 GList *items, void *data)
139 {
140 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
141 screen_t *screen = tmp->screen;
143 if( g_list_length(items)>=1 ) {
144 screen_display_completion_list(screen, items);
145 lw->clear = 1;
146 lw->repaint = 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 lw->repaint = 1;
278 }
280 if( line && line[0] && line[strlen(line)-1]=='/' &&
281 string_list_find(*dir_list, line) == NULL )
282 {
283 /* add directory content to list */
284 add_dir(gcmp, line, dir_list, list, c);
285 }
286 }
288 static int
289 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
290 {
291 gchar *path;
292 GCompletion *gcmp;
293 GList *list = NULL;
294 GList *dir_list = NULL;
295 completion_callback_data_t data;
297 /* initialize completion support */
298 gcmp = g_completion_new(NULL);
299 g_completion_set_compare(gcmp, strncmp);
300 data.list = &list;
301 data.dir_list = &dir_list;
302 data.screen = screen;
303 data.c = c;
304 wrln_completion_callback_data = &data;
305 wrln_pre_completion_callback = add_pre_completion_cb;
306 wrln_post_completion_callback = add_post_completion_cb;
307 /* get path */
308 path = screen_readln(screen->status_window.w,
309 _("Add: "),
310 NULL,
311 NULL,
312 gcmp);
314 /* destroy completion data */
315 wrln_completion_callback_data = NULL;
316 wrln_pre_completion_callback = NULL;
317 wrln_post_completion_callback = NULL;
318 g_completion_free(gcmp);
319 string_list_free(list);
320 string_list_free(dir_list);
322 /* add the path to the playlist */
323 if( path && path[0] )
324 mpdclient_cmd_add_path(c, path);
326 return 0;
327 }
329 static void
330 play_init(WINDOW *w, int cols, int rows)
331 {
332 lw = list_window_init(w, cols, rows);
333 }
335 static void
336 play_open(mpd_unused screen_t *screen, mpdclient_t *c)
337 {
338 static gboolean install_cb = TRUE;
340 if (install_cb) {
341 mpdclient_install_playlist_callback(c, playlist_changed_callback);
342 install_cb = FALSE;
343 }
344 }
346 static void
347 play_resize(int cols, int rows)
348 {
349 lw->cols = cols;
350 lw->rows = rows;
351 }
354 static void
355 play_exit(void)
356 {
357 list_window_free(lw);
358 }
360 static const char *
361 play_title(char *str, size_t size)
362 {
363 if( strcmp(options.host, "localhost") == 0 )
364 return _("Playlist");
366 g_snprintf(str, size, _("Playlist on %s"), options.host);
367 return str;
368 }
370 static void
371 play_paint(mpd_unused screen_t *screen, mpdclient_t *c)
372 {
373 lw->clear = 1;
375 list_window_paint(lw, list_callback, (void *) c);
376 wnoutrefresh(lw->w);
377 }
379 static void
380 play_update(screen_t *screen, mpdclient_t *c)
381 {
382 /* hide the cursor when mpd are playing and the user are inactive */
383 if (options.hide_cursor > 0 &&
384 (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) &&
385 time(NULL)-screen->input_timestamp >= options.hide_cursor ) {
386 lw->flags |= LW_HIDE_CURSOR;
387 } else {
388 lw->flags &= ~LW_HIDE_CURSOR;
389 }
391 /* center the cursor */
392 if( options.auto_center ) {
393 static int prev_song_id = 0;
395 if( c->song && prev_song_id != c->song->id ) {
396 center_playing_item(c);
397 prev_song_id = c->song->id;
398 }
399 }
401 if( c->playlist.updated ) {
402 if (lw->selected >= c->playlist.list->len)
403 lw->selected = c->playlist.list->len - 1;
404 if (lw->start >= c->playlist.list->len)
405 list_window_reset(lw);
407 play_paint(screen, c);
408 c->playlist.updated = FALSE;
409 } else if( lw->repaint || 1) {
410 list_window_paint(lw, list_callback, (void *) c);
411 wnoutrefresh(lw->w);
412 lw->repaint = 0;
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, lw, c->playlist.list->len, &bstate, &row))
425 return 1;
427 if (bstate & BUTTON1_DOUBLE_CLICKED) {
428 /* stop */
429 screen_cmd(c, CMD_STOP);
430 return 1;
431 }
433 selected = lw->start + row;
435 if (bstate & BUTTON1_CLICKED) {
436 /* play */
437 if (lw->start + row < c->playlist.list->len)
438 mpdclient_cmd_play(c, lw->start + row);
439 } else if (bstate & BUTTON3_CLICKED) {
440 /* delete */
441 if (selected == lw->selected)
442 mpdclient_cmd_delete(c, lw->selected);
443 }
445 lw->selected = selected;
446 list_window_check_selected(lw, c->playlist.list->len);
448 return 1;
449 }
450 #else
451 #define handle_mouse_event(s,c) (0)
452 #endif
454 static int
455 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
456 {
457 switch(cmd) {
458 case CMD_PLAY:
459 mpdclient_cmd_play(c, lw->selected);
460 return 1;
461 case CMD_DELETE:
462 mpdclient_cmd_delete(c, lw->selected);
463 return 1;
464 case CMD_SAVE_PLAYLIST:
465 playlist_save(screen, c, NULL, NULL);
466 return 1;
467 case CMD_ADD:
468 handle_add_to_playlist(screen, c);
469 return 1;
470 case CMD_SCREEN_UPDATE:
471 screen->painted = 0;
472 lw->clear = 1;
473 lw->repaint = 1;
474 center_playing_item(c);
475 return 1;
476 case CMD_LIST_MOVE_UP:
477 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
478 return 1;
479 case CMD_LIST_MOVE_DOWN:
480 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
481 return 1;
482 case CMD_LIST_FIND:
483 case CMD_LIST_RFIND:
484 case CMD_LIST_FIND_NEXT:
485 case CMD_LIST_RFIND_NEXT:
486 return screen_find(screen,
487 lw, c->playlist.list->len,
488 cmd, list_callback, (void *) c);
489 case CMD_MOUSE_EVENT:
490 return handle_mouse_event(screen,c);
491 default:
492 break;
493 }
494 return list_window_cmd(lw, c->playlist.list->len, cmd);
495 }
497 const struct screen_functions screen_playlist = {
498 .init = play_init,
499 .exit = play_exit,
500 .open = play_open,
501 .close = NULL,
502 .resize = play_resize,
503 .paint = play_paint,
504 .update = play_update,
505 .cmd = play_cmd,
506 .get_title = play_title,
507 };