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 <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <glib.h>
26 #include <ncurses.h>
28 #include "config.h"
29 #include "ncmpc.h"
30 #include "options.h"
31 #include "support.h"
32 #include "mpdclient.h"
33 #include "utils.h"
34 #include "strfsong.h"
35 #include "wreadln.h"
36 #include "command.h"
37 #include "colors.h"
38 #include "screen.h"
39 #include "screen_utils.h"
41 #define MAX_SONG_LENGTH 512
43 typedef struct
44 {
45 GList **list;
46 GList **dir_list;
47 screen_t *screen;
48 mpdclient_t *c;
49 } completion_callback_data_t;
51 static list_window_t *lw = NULL;
53 static void
54 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
55 {
56 D("screen_play.c> playlist_callback() [%d]\n", event);
57 switch(event) {
58 case PLAYLIST_EVENT_DELETE:
59 break;
60 case PLAYLIST_EVENT_MOVE:
61 lw->selected = *((int *) data);
62 if( lw->selected<lw->start )
63 lw->start--;
64 break;
65 default:
66 break;
67 }
68 /* make shure the playlist is repainted */
69 lw->clear = 1;
70 lw->repaint = 1;
71 list_window_check_selected(lw, c->playlist.length);
72 }
74 static char *
75 list_callback(int index, int *highlight, void *data)
76 {
77 static char songname[MAX_SONG_LENGTH];
78 mpdclient_t *c = (mpdclient_t *) data;
79 mpd_Song *song;
81 *highlight = 0;
82 if( (song=playlist_get_song(c, index)) == NULL ) {
83 return NULL;
84 }
86 if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) ) {
87 *highlight = 1;
88 }
89 strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
90 return songname;
91 }
93 static int
94 center_playing_item(screen_t *screen, mpdclient_t *c)
95 {
96 int length = c->playlist.length;
97 int offset = lw->selected - lw->start;
98 int index;
100 if (!lw || !c->song || length<lw->rows ||
101 IS_STOPPED(c->status->state))
102 return 0;
104 /* try to center the song that are playing */
105 index = playlist_get_index(c, c->song);
106 D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,index);
107 lw->start = index - (lw->rows / 2);
108 if (lw->start + lw->rows > length)
109 lw->start = length - lw->rows;
110 if (lw->start < 0)
111 lw->start = 0;
113 /* make sure the cursor is in the window */
114 lw->selected = lw->start+offset;
115 list_window_check_selected(lw, length);
117 lw->clear = 1;
118 lw->repaint = 1;
120 return 0;
121 }
123 void save_pre_completion_cb(GCompletion *gcmp, 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 void save_post_completion_cb(GCompletion *gcmp, gchar *line, GList *items,
137 void *data)
138 {
139 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
140 screen_t *screen = tmp->screen;
142 if( g_list_length(items)>=1 ) {
143 screen_display_completion_list(screen, items);
144 lw->clear = 1;
145 lw->repaint = 1;
146 }
147 }
149 int
150 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
151 {
152 gchar *filename;
153 gint error;
154 GCompletion *gcmp;
155 GList *list = NULL;
156 completion_callback_data_t data;
158 if( name==NULL )
159 {
160 /* initialize completion support */
161 gcmp = g_completion_new(NULL);
162 g_completion_set_compare(gcmp, strncmp);
163 data.list = &list;
164 data.dir_list = NULL;
165 data.screen = screen;
166 data.c = c;
167 wrln_completion_callback_data = &data;
168 wrln_pre_completion_callback = save_pre_completion_cb;
169 wrln_post_completion_callback = save_post_completion_cb;
172 /* query the user for a filename */
173 filename = screen_readln(screen->status_window.w,
174 _("Save playlist as: "),
175 defaultname,
176 NULL,
177 gcmp);
179 /* destroy completion support */
180 wrln_completion_callback_data = NULL;
181 wrln_pre_completion_callback = NULL;
182 wrln_post_completion_callback = NULL;
183 g_completion_free(gcmp);
184 list = string_list_free(list);
185 if( filename )
186 filename=g_strstrip(filename);
187 }
188 else
189 {
190 filename=g_strdup(name);
191 }
192 if( filename==NULL || filename[0]=='\0' )
193 return -1;
194 /* send save command to mpd */
195 D("Saving playlist as \'%s \'...\n", filename);
196 if( (error=mpdclient_cmd_save_playlist(c, filename)) )
197 {
198 gint code = GET_ACK_ERROR_CODE(error);
200 if( code == MPD_ACK_ERROR_EXIST )
201 {
202 char *buf;
203 int key;
205 buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO);
206 key = tolower(screen_getch(screen->status_window.w, buf));
207 g_free(buf);
208 if( key == YES[0] )
209 {
210 if( mpdclient_cmd_delete_playlist(c, filename) )
211 {
212 g_free(filename);
213 return -1;
214 }
215 error = playlist_save(screen, c, filename, NULL);
216 g_free(filename);
217 return error;
218 }
219 screen_status_printf(_("Aborted!"));
220 }
221 g_free(filename);
222 return -1;
223 }
224 /* success */
225 screen_status_printf(_("Saved %s"), filename);
226 g_free(filename);
227 return 0;
228 }
230 void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list, GList **list,
231 mpdclient_t *c)
232 {
233 g_completion_remove_items(gcmp, *list);
234 *list = string_list_remove(*list, dir);
235 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
236 g_completion_add_items(gcmp, *list);
237 *dir_list = g_list_append(*dir_list, g_strdup(dir));
238 }
240 void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
241 {
242 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
243 GList **dir_list = tmp->dir_list;
244 GList **list = tmp->list;
245 mpdclient_t *c = tmp->c;
247 D("pre_completion()...\n");
248 if( *list == NULL )
249 {
250 /* create initial list */
251 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
252 g_completion_add_items(gcmp, *list);
253 }
254 else if( line && line[0] && line[strlen(line)-1]=='/' &&
255 string_list_find(*dir_list, line) == NULL )
256 {
257 /* add directory content to list */
258 add_dir(gcmp, line, dir_list, list, c);
259 }
260 }
262 void add_post_completion_cb(GCompletion *gcmp, gchar *line, GList *items,
263 void *data)
264 {
265 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
266 GList **dir_list = tmp->dir_list;
267 GList **list = tmp->list;
268 mpdclient_t *c = tmp->c;
269 screen_t *screen = tmp->screen;
271 D("post_completion()...\n");
272 if( g_list_length(items)>=1 )
273 {
274 screen_display_completion_list(screen, items);
275 lw->clear = 1;
276 lw->repaint = 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(screen_t *screen, mpdclient_t *c)
336 {
337 static gboolean install_cb = TRUE;
339 if (install_cb) {
340 mpdclient_install_playlist_callback(c, playlist_changed_callback);
341 install_cb = FALSE;
342 }
343 }
345 static void
346 play_resize(int cols, int rows)
347 {
348 lw->cols = cols;
349 lw->rows = rows;
350 }
353 static void
354 play_exit(void)
355 {
356 list_window_free(lw);
357 }
359 static char *
360 play_title(char *str, size_t size)
361 {
362 if( strcmp(options.host, "localhost") == 0 )
363 return _("Playlist");
365 g_snprintf(str, size, _("Playlist on %s"), options.host);
366 return str;
367 }
369 static void
370 play_paint(screen_t *screen, mpdclient_t *c)
371 {
372 lw->clear = 1;
374 list_window_paint(lw, list_callback, (void *) c);
375 wnoutrefresh(lw->w);
376 }
378 static void
379 play_update(screen_t *screen, mpdclient_t *c)
380 {
381 /* hide the cursor when mpd are playing and the user are inactive */
382 if( options.hide_cursor>0 && c->status->state == MPD_STATUS_STATE_PLAY &&
383 time(NULL)-screen->input_timestamp >= options.hide_cursor ) {
384 lw->flags |= LW_HIDE_CURSOR;
385 } else {
386 lw->flags &= ~LW_HIDE_CURSOR;
387 }
389 /* center the cursor */
390 if( options.auto_center ) {
391 static int prev_song_id = 0;
393 if( c->song && prev_song_id != c->song->id ) {
394 center_playing_item(screen, c);
395 prev_song_id = c->song->id;
396 }
397 }
399 if( c->playlist.updated ) {
400 if( lw->selected >= c->playlist.length )
401 lw->selected = c->playlist.length-1;
402 if( lw->start >= c->playlist.length )
403 list_window_reset(lw);
405 play_paint(screen, c);
406 c->playlist.updated = FALSE;
407 } else if( lw->repaint || 1) {
408 list_window_paint(lw, list_callback, (void *) c);
409 wnoutrefresh(lw->w);
410 lw->repaint = 0;
411 }
412 }
414 #ifdef HAVE_GETMOUSE
415 static int
416 handle_mouse_event(screen_t *screen, mpdclient_t *c)
417 {
418 int row;
419 int selected;
420 unsigned long bstate;
422 if (screen_get_mouse_event(c, lw, c->playlist.length, &bstate, &row))
423 return 1;
425 if (bstate & BUTTON1_DOUBLE_CLICKED) {
426 /* stop */
427 screen_cmd(c, CMD_STOP);
428 return 1;
429 }
431 selected = lw->start + row;
433 if (bstate & BUTTON1_CLICKED) {
434 /* play */
435 if (lw->start + row < c->playlist.length)
436 mpdclient_cmd_play(c, lw->start + row);
437 } else if (bstate & BUTTON3_CLICKED) {
438 /* delete */
439 if (selected == lw->selected)
440 mpdclient_cmd_delete(c, lw->selected);
441 }
443 lw->selected = selected;
444 list_window_check_selected(lw, c->playlist.length);
446 return 1;
447 }
448 #else
449 #define handle_mouse_event(s,c) (0)
450 #endif
452 static int
453 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
454 {
455 switch(cmd) {
456 case CMD_PLAY:
457 mpdclient_cmd_play(c, lw->selected);
458 return 1;
459 case CMD_DELETE:
460 mpdclient_cmd_delete(c, lw->selected);
461 return 1;
462 case CMD_SAVE_PLAYLIST:
463 playlist_save(screen, c, NULL, NULL);
464 return 1;
465 case CMD_ADD:
466 handle_add_to_playlist(screen, c);
467 return 1;
468 case CMD_SCREEN_UPDATE:
469 screen->painted = 0;
470 lw->clear = 1;
471 lw->repaint = 1;
472 center_playing_item(screen, c);
473 return 1;
474 case CMD_LIST_MOVE_UP:
475 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
476 return 1;
477 case CMD_LIST_MOVE_DOWN:
478 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
479 return 1;
480 case CMD_LIST_FIND:
481 case CMD_LIST_RFIND:
482 case CMD_LIST_FIND_NEXT:
483 case CMD_LIST_RFIND_NEXT:
484 return screen_find(screen,
485 lw, c->playlist.length,
486 cmd, list_callback, (void *) c);
487 case CMD_MOUSE_EVENT:
488 return handle_mouse_event(screen,c);
489 default:
490 break;
491 }
492 return list_window_cmd(lw, c->playlist.length, cmd) ;
493 }
495 static list_window_t *
496 play_lw(void)
497 {
498 return lw;
499 }
501 screen_functions_t *
502 get_screen_playlist(void)
503 {
504 static screen_functions_t functions;
506 memset(&functions, 0, sizeof(screen_functions_t));
507 functions.init = play_init;
508 functions.exit = play_exit;
509 functions.open = play_open;
510 functions.close = NULL;
511 functions.resize = play_resize;
512 functions.paint = play_paint;
513 functions.update = play_update;
514 functions.cmd = play_cmd;
515 functions.get_lw = play_lw;
516 functions.get_title = play_title;
518 return &functions;
519 }