cdb8320e8c66e7886c93b1b3482ccec58c0e74e7
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);
160 /* destroy completion support */
161 wrln_pre_completion_callback = NULL;
162 wrln_post_completion_callback = NULL;
163 g_completion_free(gcmp);
164 list = string_list_free(list);
165 if( filename )
166 filename=g_strstrip(filename);
167 }
168 else
169 {
170 filename=g_strdup(name);
171 }
172 if( filename==NULL || filename[0]=='\0' )
173 return -1;
174 /* send save command to mpd */
175 D("Saving playlist as \'%s \'...\n", filename);
176 if( (error=mpdclient_cmd_save_playlist(c, filename)) )
177 {
178 gint code = GET_ACK_ERROR_CODE(error);
180 if( code == MPD_ACK_ERROR_EXIST )
181 {
182 char *buf;
183 int key;
185 buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO);
186 key = tolower(screen_getch(screen->status_window.w, buf));
187 g_free(buf);
188 if( key == YES[0] )
189 {
190 if( mpdclient_cmd_delete_playlist(c, filename) )
191 {
192 g_free(filename);
193 return -1;
194 }
195 error = handle_save_playlist(screen, c, filename);
196 g_free(filename);
197 return error;
198 }
199 screen_status_printf(_("Aborted!"));
200 }
201 g_free(filename);
202 return -1;
203 }
204 /* success */
205 screen_status_printf(_("Saved %s"), filename);
206 g_free(filename);
207 return 0;
208 }
210 static int
211 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
212 {
213 gchar *path;
214 GCompletion *gcmp;
215 GList *list = NULL;
216 GList *dir_list = NULL;
218 void add_dir(gchar *dir)
219 {
220 g_completion_remove_items(gcmp, list);
221 list = string_list_remove(list, dir);
222 list = gcmp_list_from_path(c, dir, list, GCMP_TYPE_RFILE);
223 g_completion_add_items(gcmp, list);
224 dir_list = g_list_append(dir_list, g_strdup(dir));
225 }
227 void pre_completion_cb(GCompletion *gcmp, gchar *line)
228 {
229 D("pre_completion()...\n");
230 if( list == NULL )
231 {
232 /* create initial list */
233 list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
234 g_completion_add_items(gcmp, list);
235 }
236 else if( line && line[0] && line[strlen(line)-1]=='/' &&
237 string_list_find(dir_list, line) == NULL )
238 {
239 /* add directory content to list */
240 add_dir(line);
241 }
242 }
244 void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
245 {
246 D("post_completion()...\n");
247 if( g_list_length(items)>=1 )
248 {
249 screen_display_completion_list(screen, items);
250 lw->clear = 1;
251 lw->repaint = 1;
252 }
254 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(line);
259 }
260 }
262 /* initialize completion support */
263 gcmp = g_completion_new(NULL);
264 g_completion_set_compare(gcmp, strncmp);
265 wrln_pre_completion_callback = pre_completion_cb;
266 wrln_post_completion_callback = post_completion_cb;
267 /* get path */
268 path = screen_readln(screen->status_window.w,
269 _("Add: "),
270 NULL,
271 NULL,
272 gcmp);
274 /* destroy completion data */
275 wrln_pre_completion_callback = NULL;
276 wrln_post_completion_callback = NULL;
277 g_completion_free(gcmp);
278 string_list_free(list);
279 string_list_free(dir_list);
281 /* add the path to the playlist */
282 if( path && path[0] )
283 mpdclient_cmd_add_path(c, path);
285 return 0;
286 }
288 static void
289 play_init(WINDOW *w, int cols, int rows)
290 {
291 lw = list_window_init(w, cols, rows);
292 }
294 static void
295 play_open(screen_t *screen, mpdclient_t *c)
296 {
297 static gboolean install_cb = TRUE;
299 if( install_cb )
300 {
301 mpdclient_install_playlist_callback(c, playlist_changed_callback);
302 install_cb = FALSE;
303 }
304 }
306 static void
307 play_resize(int cols, int rows)
308 {
309 lw->cols = cols;
310 lw->rows = rows;
311 }
314 static void
315 play_exit(void)
316 {
317 list_window_free(lw);
318 }
320 static char *
321 play_title(char *str, size_t size)
322 {
323 if( strcmp(options.host, "localhost") == 0 )
324 return _("Playlist");
326 g_snprintf(str, size, _("Playlist on %s"), options.host);
328 return str;
329 }
331 static void
332 play_paint(screen_t *screen, mpdclient_t *c)
333 {
334 lw->clear = 1;
336 list_window_paint(lw, list_callback, (void *) c);
337 wnoutrefresh(lw->w);
338 }
340 static void
341 play_update(screen_t *screen, mpdclient_t *c)
342 {
343 if( options.auto_center )
344 {
345 static int prev_song_id = 0;
347 if( c->song && prev_song_id != c->song->id )
348 {
349 center_playing_item(screen, c);
350 prev_song_id = c->song->id;
351 }
352 }
354 if( c->playlist.updated )
355 {
356 if( lw->selected >= c->playlist.length )
357 lw->selected = c->playlist.length-1;
358 if( lw->start >= c->playlist.length )
359 list_window_reset(lw);
361 play_paint(screen, c);
362 c->playlist.updated = FALSE;
363 }
364 else if( lw->repaint || 1)
365 {
366 list_window_paint(lw, list_callback, (void *) c);
367 wnoutrefresh(lw->w);
368 lw->repaint = 0;
369 }
370 }
372 #ifdef HAVE_GETMOUSE
373 static int
374 handle_mouse_event(screen_t *screen, mpdclient_t *c)
375 {
376 int row;
377 int selected;
378 unsigned long bstate;
380 if( screen_get_mouse_event(c, lw, c->playlist.length, &bstate, &row) )
381 return 1;
383 if( bstate & BUTTON1_DOUBLE_CLICKED )
384 {
385 /* stop */
386 screen_cmd(c, CMD_STOP);
387 return 1;
388 }
390 selected = lw->start+row;
392 if( bstate & BUTTON1_CLICKED )
393 {
394 /* play */
395 if( lw->start+row < c->playlist.length )
396 mpdclient_cmd_play(c, lw->start+row);
397 }
398 else if( bstate & BUTTON3_CLICKED )
399 {
400 /* delete */
401 if( selected == lw->selected )
402 mpdclient_cmd_delete(c, lw->selected);
403 }
404 lw->selected = selected;
405 list_window_check_selected(lw, c->playlist.length);
407 return 1;
408 }
409 #else
410 #define handle_mouse_event(s,c) (0)
411 #endif
413 static int
414 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
415 {
416 switch(cmd)
417 {
418 case CMD_PLAY:
419 mpdclient_cmd_play(c, lw->selected);
420 return 1;
421 case CMD_DELETE:
422 mpdclient_cmd_delete(c, lw->selected);
423 return 1;
424 case CMD_SAVE_PLAYLIST:
425 handle_save_playlist(screen, c, NULL);
426 return 1;
427 case CMD_ADD:
428 handle_add_to_playlist(screen, c);
429 return 1;
430 case CMD_SCREEN_UPDATE:
431 screen->painted = 0;
432 lw->clear = 1;
433 lw->repaint = 1;
434 center_playing_item(screen, c);
435 return 1;
436 case CMD_LIST_MOVE_UP:
437 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
438 return 1;
439 case CMD_LIST_MOVE_DOWN:
440 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
441 return 1;
442 case CMD_LIST_FIND:
443 case CMD_LIST_RFIND:
444 case CMD_LIST_FIND_NEXT:
445 case CMD_LIST_RFIND_NEXT:
446 return screen_find(screen, c,
447 lw, c->playlist.length,
448 cmd, list_callback, (void *) c);
449 case CMD_MOUSE_EVENT:
450 return handle_mouse_event(screen,c);
451 default:
452 break;
453 }
454 return list_window_cmd(lw, c->playlist.length, cmd) ;
455 }
459 static list_window_t *
460 play_lw(void)
461 {
462 return lw;
463 }
466 screen_functions_t *
467 get_screen_playlist(void)
468 {
469 static screen_functions_t functions;
471 memset(&functions, 0, sizeof(screen_functions_t));
472 functions.init = play_init;
473 functions.exit = play_exit;
474 functions.open = play_open;
475 functions.close = NULL;
476 functions.resize = play_resize;
477 functions.paint = play_paint;
478 functions.update = play_update;
479 functions.cmd = play_cmd;
480 functions.get_lw = play_lw;
481 functions.get_title = play_title;
483 return &functions;
484 }