307471f2cbdd03840e6feb598503584061690647
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 <glib.h>
25 #include <ncurses.h>
26 #include <panel.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 }
117 static int
118 handle_save_playlist(screen_t *screen, mpdclient_t *c, char *name)
119 {
120 gchar *filename;
121 gint error;
122 GCompletion *gcmp;
123 GList *list = NULL;
125 void pre_completion_cb(GCompletion *gcmp, gchar *line)
126 {
127 if( list == NULL )
128 {
129 /* create completion list */
130 list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
131 g_completion_add_items(gcmp, list);
132 }
133 }
135 void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
136 {
137 if( g_list_length(items)>=1 )
138 {
139 screen_display_completion_list(screen, items);
140 lw->clear = 1;
141 lw->repaint = 1;
142 }
143 }
145 if( name==NULL )
146 {
147 /* initialize completion support */
148 gcmp = g_completion_new(NULL);
149 g_completion_set_compare(gcmp, strncmp);
150 wrln_pre_completion_callback = pre_completion_cb;
151 wrln_post_completion_callback = post_completion_cb;
153 /* query the user for a filename */
154 filename = screen_readln(screen->status_window.w,
155 _("Save playlist as: "),
156 NULL,
157 NULL,
158 gcmp);
159 filename=g_strstrip(filename);
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 }
167 else
168 {
169 filename=g_strdup(name);
170 }
171 if( filename==NULL || filename[0]=='\0' )
172 return -1;
173 /* send save command to mpd */
174 D("Saving playlist as \'%s \'...\n", filename);
175 if( (error=mpdclient_cmd_save_playlist(c, filename)) )
176 {
177 gint code = GET_ACK_ERROR_CODE(error);
179 if( code == MPD_ACK_ERROR_EXIST )
180 {
181 char *buf;
182 int key;
184 buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO);
185 key = tolower(screen_getch(screen->status_window.w, buf));
186 g_free(buf);
187 if( key == YES[0] )
188 {
189 if( mpdclient_cmd_delete_playlist(c, filename) )
190 {
191 g_free(filename);
192 return -1;
193 }
194 error = handle_save_playlist(screen, c, filename);
195 g_free(filename);
196 return error;
197 }
198 screen_status_printf(_("Aborted!"));
199 }
200 g_free(filename);
201 return -1;
202 }
203 /* success */
204 screen_status_printf(_("Saved %s"), filename);
205 g_free(filename);
206 return 0;
207 }
209 static int
210 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
211 {
212 gchar *path;
213 GCompletion *gcmp;
214 GList *list = NULL;
215 GList *dir_list = NULL;
217 void add_dir(gchar *dir)
218 {
219 g_completion_remove_items(gcmp, list);
220 list = string_list_remove(list, dir);
221 list = gcmp_list_from_path(c, dir, list, GCMP_TYPE_RFILE);
222 g_completion_add_items(gcmp, list);
223 dir_list = g_list_append(dir_list, g_strdup(dir));
224 }
226 void pre_completion_cb(GCompletion *gcmp, gchar *line)
227 {
228 D("pre_completion()...\n");
229 if( list == NULL )
230 {
231 /* create initial list */
232 list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
233 g_completion_add_items(gcmp, list);
234 }
235 else if( line && line[0] && line[strlen(line)-1]=='/' &&
236 string_list_find(dir_list, line) == NULL )
237 {
238 /* add directory content to list */
239 add_dir(line);
240 }
241 }
243 void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
244 {
245 D("post_completion()...\n");
246 if( g_list_length(items)>=1 )
247 {
248 screen_display_completion_list(screen, items);
249 lw->clear = 1;
250 lw->repaint = 1;
251 }
253 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(line);
258 }
259 }
261 /* initialize completion support */
262 gcmp = g_completion_new(NULL);
263 g_completion_set_compare(gcmp, strncmp);
264 wrln_pre_completion_callback = pre_completion_cb;
265 wrln_post_completion_callback = post_completion_cb;
266 /* get path */
267 path = screen_readln(screen->status_window.w,
268 _("Add: "),
269 NULL,
270 NULL,
271 gcmp);
273 /* destroy completion data */
274 wrln_pre_completion_callback = NULL;
275 wrln_post_completion_callback = NULL;
276 g_completion_free(gcmp);
277 string_list_free(list);
278 string_list_free(dir_list);
280 /* add the path to the playlist */
281 if( path && path[0] )
282 mpdclient_cmd_add_path(c, path);
284 return 0;
285 }
287 static void
288 play_init(WINDOW *w, int cols, int rows)
289 {
290 lw = list_window_init(w, cols, rows);
291 }
293 static void
294 play_open(screen_t *screen, mpdclient_t *c)
295 {
296 static gboolean install_cb = TRUE;
298 if( install_cb )
299 {
300 mpdclient_install_playlist_callback(c, playlist_changed_callback);
301 install_cb = FALSE;
302 }
303 }
305 static void
306 play_resize(int cols, int rows)
307 {
308 lw->cols = cols;
309 lw->rows = rows;
310 }
313 static void
314 play_exit(void)
315 {
316 list_window_free(lw);
317 }
319 static char *
320 play_title(char *str, size_t size)
321 {
322 if( strcmp(options.host, "localhost") == 0 )
323 return _("Playlist");
325 g_snprintf(str, size, _("Playlist on %s"), options.host);
327 return str;
328 }
330 static void
331 play_paint(screen_t *screen, mpdclient_t *c)
332 {
333 lw->clear = 1;
335 list_window_paint(lw, list_callback, (void *) c);
336 wnoutrefresh(lw->w);
337 }
339 static void
340 play_update(screen_t *screen, mpdclient_t *c)
341 {
342 if( options.auto_center )
343 {
344 static int prev_song_id = 0;
346 if( c->song && prev_song_id != c->song->id )
347 {
348 center_playing_item(screen, c);
349 prev_song_id = c->song->id;
350 }
351 }
353 if( c->playlist.updated )
354 {
355 if( lw->selected >= c->playlist.length )
356 lw->selected = c->playlist.length-1;
357 if( lw->start >= c->playlist.length )
358 list_window_reset(lw);
360 play_paint(screen, c);
361 c->playlist.updated = FALSE;
362 }
363 else if( lw->repaint || 1)
364 {
365 list_window_paint(lw, list_callback, (void *) c);
366 wnoutrefresh(lw->w);
367 lw->repaint = 0;
368 }
369 }
371 #ifdef HAVE_GETMOUSE
372 static int
373 handle_mouse_event(screen_t *screen, mpdclient_t *c)
374 {
375 int row;
376 int selected;
377 unsigned long bstate;
379 if( screen_get_mouse_event(c, lw, c->playlist.length, &bstate, &row) )
380 return 1;
382 if( bstate & BUTTON1_DOUBLE_CLICKED )
383 {
384 /* stop */
385 screen_cmd(c, CMD_STOP);
386 return 1;
387 }
389 selected = lw->start+row;
391 if( bstate & BUTTON1_CLICKED )
392 {
393 /* play */
394 if( lw->start+row < c->playlist.length )
395 mpdclient_cmd_play(c, lw->start+row);
396 }
397 else if( bstate & BUTTON3_CLICKED )
398 {
399 /* delete */
400 if( selected == lw->selected )
401 mpdclient_cmd_delete(c, lw->selected);
402 }
403 lw->selected = selected;
404 list_window_check_selected(lw, c->playlist.length);
406 return 1;
407 }
408 #else
409 #define handle_mouse_event(s,c) (0)
410 #endif
412 static int
413 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
414 {
415 switch(cmd)
416 {
417 case CMD_PLAY:
418 mpdclient_cmd_play(c, lw->selected);
419 return 1;
420 case CMD_DELETE:
421 mpdclient_cmd_delete(c, lw->selected);
422 return 1;
423 case CMD_SAVE_PLAYLIST:
424 handle_save_playlist(screen, c, NULL);
425 return 1;
426 case CMD_ADD:
427 handle_add_to_playlist(screen, c);
428 return 1;
429 case CMD_SCREEN_UPDATE:
430 screen->painted = 0;
431 lw->clear = 1;
432 lw->repaint = 1;
433 center_playing_item(screen, c);
434 return 1;
435 case CMD_LIST_MOVE_UP:
436 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
437 return 1;
438 case CMD_LIST_MOVE_DOWN:
439 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
440 return 1;
441 case CMD_LIST_FIND:
442 case CMD_LIST_RFIND:
443 case CMD_LIST_FIND_NEXT:
444 case CMD_LIST_RFIND_NEXT:
445 return screen_find(screen, c,
446 lw, c->playlist.length,
447 cmd, list_callback);
448 case CMD_MOUSE_EVENT:
449 return handle_mouse_event(screen,c);
450 default:
451 break;
452 }
453 return list_window_cmd(lw, c->playlist.length, cmd) ;
454 }
458 static list_window_t *
459 play_lw(void)
460 {
461 return lw;
462 }
465 screen_functions_t *
466 get_screen_playlist(void)
467 {
468 static screen_functions_t functions;
470 memset(&functions, 0, sizeof(screen_functions_t));
471 functions.init = play_init;
472 functions.exit = play_exit;
473 functions.open = play_open;
474 functions.close = NULL;
475 functions.resize = play_resize;
476 functions.paint = play_paint;
477 functions.update = play_update;
478 functions.cmd = play_cmd;
479 functions.get_lw = play_lw;
480 functions.get_title = play_title;
482 return &functions;
483 }