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 static list_window_t *lw = NULL;
45 static void
46 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
47 {
48 D("screen_play.c> playlist_callback() [%d]\n", event);
49 switch(event)
50 {
51 case PLAYLIST_EVENT_DELETE:
52 break;
53 case PLAYLIST_EVENT_MOVE:
54 lw->selected = *((int *) data);
55 if( lw->selected<lw->start )
56 lw->start--;
57 break;
58 default:
59 break;
60 }
61 /* make shure the playlist is repainted */
62 lw->clear = 1;
63 lw->repaint = 1;
64 list_window_check_selected(lw, c->playlist.length);
65 }
67 static char *
68 list_callback(int index, int *highlight, void *data)
69 {
70 static char songname[MAX_SONG_LENGTH];
71 mpdclient_t *c = (mpdclient_t *) data;
72 mpd_Song *song;
74 *highlight = 0;
75 if( (song=playlist_get_song(c, index)) == NULL )
76 {
77 return NULL;
78 }
80 if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) )
81 {
82 *highlight = 1;
83 }
84 strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
85 return songname;
86 }
88 static int
89 center_playing_item(screen_t *screen, mpdclient_t *c)
90 {
91 int length = c->playlist.length;
92 int offset = lw->selected-lw->start;
93 int index;
95 if( !lw || !c->song || length<lw->rows || IS_STOPPED(c->status->state) )
96 return 0;
98 /* try to center the song that are playing */
99 index = playlist_get_index(c, c->song);
100 D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,index);
101 lw->start = index-(lw->rows/2);
102 if( lw->start+lw->rows > length )
103 lw->start = length-lw->rows;
104 if( lw->start<0 )
105 lw->start=0;
107 /* make sure the cursor is in the window */
108 lw->selected = lw->start+offset;
109 list_window_check_selected(lw, length);
111 lw->clear = 1;
112 lw->repaint = 1;
114 return 0;
115 }
118 int
119 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
120 {
121 gchar *filename;
122 gint error;
123 GCompletion *gcmp;
124 GList *list = NULL;
126 void pre_completion_cb(GCompletion *gcmp, gchar *line)
127 {
128 if( list == NULL )
129 {
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 post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
137 {
138 if( g_list_length(items)>=1 )
139 {
140 screen_display_completion_list(screen, items);
141 lw->clear = 1;
142 lw->repaint = 1;
143 }
144 }
146 if( name==NULL )
147 {
148 /* initialize completion support */
149 gcmp = g_completion_new(NULL);
150 g_completion_set_compare(gcmp, strncmp);
151 wrln_pre_completion_callback = pre_completion_cb;
152 wrln_post_completion_callback = post_completion_cb;
154 /* query the user for a filename */
155 filename = screen_readln(screen->status_window.w,
156 _("Save playlist as: "),
157 defaultname,
158 NULL,
159 gcmp);
161 /* destroy completion support */
162 wrln_pre_completion_callback = NULL;
163 wrln_post_completion_callback = NULL;
164 g_completion_free(gcmp);
165 list = string_list_free(list);
166 if( filename )
167 filename=g_strstrip(filename);
168 }
169 else
170 {
171 filename=g_strdup(name);
172 }
173 if( filename==NULL || filename[0]=='\0' )
174 return -1;
175 /* send save command to mpd */
176 D("Saving playlist as \'%s \'...\n", filename);
177 if( (error=mpdclient_cmd_save_playlist(c, filename)) )
178 {
179 gint code = GET_ACK_ERROR_CODE(error);
181 if( code == MPD_ACK_ERROR_EXIST )
182 {
183 char *buf;
184 int key;
186 buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO);
187 key = tolower(screen_getch(screen->status_window.w, buf));
188 g_free(buf);
189 if( key == YES[0] )
190 {
191 if( mpdclient_cmd_delete_playlist(c, filename) )
192 {
193 g_free(filename);
194 return -1;
195 }
196 error = playlist_save(screen, c, filename, NULL);
197 g_free(filename);
198 return error;
199 }
200 screen_status_printf(_("Aborted!"));
201 }
202 g_free(filename);
203 return -1;
204 }
205 /* success */
206 screen_status_printf(_("Saved %s"), filename);
207 g_free(filename);
208 return 0;
209 }
211 static int
212 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
213 {
214 gchar *path;
215 GCompletion *gcmp;
216 GList *list = NULL;
217 GList *dir_list = NULL;
219 void add_dir(gchar *dir)
220 {
221 g_completion_remove_items(gcmp, list);
222 list = string_list_remove(list, dir);
223 list = gcmp_list_from_path(c, dir, list, GCMP_TYPE_RFILE);
224 g_completion_add_items(gcmp, list);
225 dir_list = g_list_append(dir_list, g_strdup(dir));
226 }
228 void pre_completion_cb(GCompletion *gcmp, gchar *line)
229 {
230 D("pre_completion()...\n");
231 if( list == NULL )
232 {
233 /* create initial list */
234 list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
235 g_completion_add_items(gcmp, list);
236 }
237 else if( line && line[0] && line[strlen(line)-1]=='/' &&
238 string_list_find(dir_list, line) == NULL )
239 {
240 /* add directory content to list */
241 add_dir(line);
242 }
243 }
245 void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
246 {
247 D("post_completion()...\n");
248 if( g_list_length(items)>=1 )
249 {
250 screen_display_completion_list(screen, items);
251 lw->clear = 1;
252 lw->repaint = 1;
253 }
255 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(line);
260 }
261 }
263 /* initialize completion support */
264 gcmp = g_completion_new(NULL);
265 g_completion_set_compare(gcmp, strncmp);
266 wrln_pre_completion_callback = pre_completion_cb;
267 wrln_post_completion_callback = post_completion_cb;
268 /* get path */
269 path = screen_readln(screen->status_window.w,
270 _("Add: "),
271 NULL,
272 NULL,
273 gcmp);
275 /* destroy completion data */
276 wrln_pre_completion_callback = NULL;
277 wrln_post_completion_callback = NULL;
278 g_completion_free(gcmp);
279 string_list_free(list);
280 string_list_free(dir_list);
282 /* add the path to the playlist */
283 if( path && path[0] )
284 mpdclient_cmd_add_path(c, path);
286 return 0;
287 }
289 static void
290 play_init(WINDOW *w, int cols, int rows)
291 {
292 lw = list_window_init(w, cols, rows);
293 }
295 static void
296 play_open(screen_t *screen, mpdclient_t *c)
297 {
298 static gboolean install_cb = TRUE;
300 if( install_cb )
301 {
302 mpdclient_install_playlist_callback(c, playlist_changed_callback);
303 install_cb = FALSE;
304 }
305 }
307 static void
308 play_resize(int cols, int rows)
309 {
310 lw->cols = cols;
311 lw->rows = rows;
312 }
315 static void
316 play_exit(void)
317 {
318 list_window_free(lw);
319 }
321 static char *
322 play_title(char *str, size_t size)
323 {
324 if( strcmp(options.host, "localhost") == 0 )
325 return _("Playlist");
327 g_snprintf(str, size, _("Playlist on %s"), options.host);
329 return str;
330 }
332 static void
333 play_paint(screen_t *screen, mpdclient_t *c)
334 {
335 lw->clear = 1;
337 list_window_paint(lw, list_callback, (void *) c);
338 wnoutrefresh(lw->w);
339 }
341 static void
342 play_update(screen_t *screen, mpdclient_t *c)
343 {
344 /* hide the cursor when mpd are playing and the user are inactive */
345 if( options.hide_cursor>0 && c->status->state == MPD_STATUS_STATE_PLAY &&
346 time(NULL)-screen->input_timestamp >= options.hide_cursor )
347 {
348 lw->flags |= LW_HIDE_CURSOR;
349 }
350 else
351 {
352 lw->flags &= ~LW_HIDE_CURSOR;
353 }
355 /* center the cursor */
356 if( options.auto_center )
357 {
358 static int prev_song_id = 0;
360 if( c->song && prev_song_id != c->song->id )
361 {
362 center_playing_item(screen, c);
363 prev_song_id = c->song->id;
364 }
365 }
367 if( c->playlist.updated )
368 {
369 if( lw->selected >= c->playlist.length )
370 lw->selected = c->playlist.length-1;
371 if( lw->start >= c->playlist.length )
372 list_window_reset(lw);
374 play_paint(screen, c);
375 c->playlist.updated = FALSE;
376 }
377 else if( lw->repaint || 1)
378 {
379 list_window_paint(lw, list_callback, (void *) c);
380 wnoutrefresh(lw->w);
381 lw->repaint = 0;
382 }
383 }
385 #ifdef HAVE_GETMOUSE
386 static int
387 handle_mouse_event(screen_t *screen, mpdclient_t *c)
388 {
389 int row;
390 int selected;
391 unsigned long bstate;
393 if( screen_get_mouse_event(c, lw, c->playlist.length, &bstate, &row) )
394 return 1;
396 if( bstate & BUTTON1_DOUBLE_CLICKED )
397 {
398 /* stop */
399 screen_cmd(c, CMD_STOP);
400 return 1;
401 }
403 selected = lw->start+row;
405 if( bstate & BUTTON1_CLICKED )
406 {
407 /* play */
408 if( lw->start+row < c->playlist.length )
409 mpdclient_cmd_play(c, lw->start+row);
410 }
411 else if( bstate & BUTTON3_CLICKED )
412 {
413 /* delete */
414 if( selected == lw->selected )
415 mpdclient_cmd_delete(c, lw->selected);
416 }
417 lw->selected = selected;
418 list_window_check_selected(lw, c->playlist.length);
420 return 1;
421 }
422 #else
423 #define handle_mouse_event(s,c) (0)
424 #endif
426 static int
427 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
428 {
429 switch(cmd)
430 {
431 case CMD_PLAY:
432 mpdclient_cmd_play(c, lw->selected);
433 return 1;
434 case CMD_DELETE:
435 mpdclient_cmd_delete(c, lw->selected);
436 return 1;
437 case CMD_SAVE_PLAYLIST:
438 playlist_save(screen, c, NULL, NULL);
439 return 1;
440 case CMD_ADD:
441 handle_add_to_playlist(screen, c);
442 return 1;
443 case CMD_SCREEN_UPDATE:
444 screen->painted = 0;
445 lw->clear = 1;
446 lw->repaint = 1;
447 center_playing_item(screen, c);
448 return 1;
449 case CMD_LIST_MOVE_UP:
450 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
451 return 1;
452 case CMD_LIST_MOVE_DOWN:
453 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
454 return 1;
455 case CMD_LIST_FIND:
456 case CMD_LIST_RFIND:
457 case CMD_LIST_FIND_NEXT:
458 case CMD_LIST_RFIND_NEXT:
459 return screen_find(screen, c,
460 lw, c->playlist.length,
461 cmd, list_callback, (void *) c);
462 case CMD_MOUSE_EVENT:
463 return handle_mouse_event(screen,c);
464 default:
465 break;
466 }
467 return list_window_cmd(lw, c->playlist.length, cmd) ;
468 }
472 static list_window_t *
473 play_lw(void)
474 {
475 return lw;
476 }
479 screen_functions_t *
480 get_screen_playlist(void)
481 {
482 static screen_functions_t functions;
484 memset(&functions, 0, sizeof(screen_functions_t));
485 functions.init = play_init;
486 functions.exit = play_exit;
487 functions.open = play_open;
488 functions.close = NULL;
489 functions.resize = play_resize;
490 functions.paint = play_paint;
491 functions.update = play_update;
492 functions.cmd = play_cmd;
493 functions.get_lw = play_lw;
494 functions.get_title = play_title;
496 return &functions;
497 }