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"
35 #include <ctype.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <glib.h>
40 #include <ncurses.h>
42 #define MAX_SONG_LENGTH 512
44 typedef struct
45 {
46 GList **list;
47 GList **dir_list;
48 screen_t *screen;
49 mpdclient_t *c;
50 } completion_callback_data_t;
52 static list_window_t *lw = NULL;
54 static void
55 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
56 {
57 D("screen_play.c> playlist_callback() [%d]\n", event);
58 switch(event) {
59 case PLAYLIST_EVENT_DELETE:
60 break;
61 case PLAYLIST_EVENT_MOVE:
62 lw->selected = *((int *) data);
63 if( lw->selected<lw->start )
64 lw->start--;
65 break;
66 default:
67 break;
68 }
69 /* make shure the playlist is repainted */
70 lw->clear = 1;
71 lw->repaint = 1;
72 list_window_check_selected(lw, c->playlist.length);
73 }
75 static const char *
76 list_callback(int index, int *highlight, void *data)
77 {
78 static char songname[MAX_SONG_LENGTH];
79 mpdclient_t *c = (mpdclient_t *) data;
80 mpd_Song *song;
82 *highlight = 0;
83 if( (song=playlist_get_song(c, index)) == NULL ) {
84 return NULL;
85 }
87 if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) ) {
88 *highlight = 1;
89 }
90 strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
91 return songname;
92 }
94 static int
95 center_playing_item(screen_t *screen, mpdclient_t *c)
96 {
97 int length = c->playlist.length;
98 int offset = lw->selected - lw->start;
99 int index;
101 if (!lw || !c->song || length<lw->rows ||
102 IS_STOPPED(c->status->state))
103 return 0;
105 /* try to center the song that are playing */
106 index = playlist_get_index(c, c->song);
107 D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,index);
108 lw->start = index - (lw->rows / 2);
109 if (lw->start + lw->rows > length)
110 lw->start = length - lw->rows;
111 if (lw->start < 0)
112 lw->start = 0;
114 /* make sure the cursor is in the window */
115 lw->selected = lw->start+offset;
116 list_window_check_selected(lw, length);
118 lw->clear = 1;
119 lw->repaint = 1;
121 return 0;
122 }
124 static void
125 save_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
126 {
127 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
128 GList **list = tmp->list;
129 mpdclient_t *c = tmp->c;
131 if( *list == NULL ) {
132 /* create completion list */
133 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
134 g_completion_add_items(gcmp, *list);
135 }
136 }
138 static void
139 save_post_completion_cb(GCompletion *gcmp, gchar *line, GList *items,
140 void *data)
141 {
142 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
143 screen_t *screen = tmp->screen;
145 if( g_list_length(items)>=1 ) {
146 screen_display_completion_list(screen, items);
147 lw->clear = 1;
148 lw->repaint = 1;
149 }
150 }
152 int
153 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
154 {
155 gchar *filename;
156 gint error;
157 GCompletion *gcmp;
158 GList *list = NULL;
159 completion_callback_data_t data;
161 if( name==NULL )
162 {
163 /* initialize completion support */
164 gcmp = g_completion_new(NULL);
165 g_completion_set_compare(gcmp, strncmp);
166 data.list = &list;
167 data.dir_list = NULL;
168 data.screen = screen;
169 data.c = c;
170 wrln_completion_callback_data = &data;
171 wrln_pre_completion_callback = save_pre_completion_cb;
172 wrln_post_completion_callback = save_post_completion_cb;
175 /* query the user for a filename */
176 filename = screen_readln(screen->status_window.w,
177 _("Save playlist as: "),
178 defaultname,
179 NULL,
180 gcmp);
182 /* destroy completion support */
183 wrln_completion_callback_data = NULL;
184 wrln_pre_completion_callback = NULL;
185 wrln_post_completion_callback = NULL;
186 g_completion_free(gcmp);
187 list = string_list_free(list);
188 if( filename )
189 filename=g_strstrip(filename);
190 }
191 else
192 {
193 filename=g_strdup(name);
194 }
195 if( filename==NULL || filename[0]=='\0' )
196 return -1;
197 /* send save command to mpd */
198 D("Saving playlist as \'%s \'...\n", filename);
199 if( (error=mpdclient_cmd_save_playlist(c, filename)) )
200 {
201 gint code = GET_ACK_ERROR_CODE(error);
203 if( code == MPD_ACK_ERROR_EXIST )
204 {
205 char *buf;
206 int key;
208 buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO);
209 key = tolower(screen_getch(screen->status_window.w, buf));
210 g_free(buf);
211 if( key == YES[0] )
212 {
213 if( mpdclient_cmd_delete_playlist(c, filename) )
214 {
215 g_free(filename);
216 return -1;
217 }
218 error = playlist_save(screen, c, filename, NULL);
219 g_free(filename);
220 return error;
221 }
222 screen_status_printf(_("Aborted!"));
223 }
224 g_free(filename);
225 return -1;
226 }
227 /* success */
228 screen_status_printf(_("Saved %s"), filename);
229 g_free(filename);
230 return 0;
231 }
233 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
234 GList **list, mpdclient_t *c)
235 {
236 g_completion_remove_items(gcmp, *list);
237 *list = string_list_remove(*list, dir);
238 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
239 g_completion_add_items(gcmp, *list);
240 *dir_list = g_list_append(*dir_list, g_strdup(dir));
241 }
243 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
244 {
245 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
246 GList **dir_list = tmp->dir_list;
247 GList **list = tmp->list;
248 mpdclient_t *c = tmp->c;
250 D("pre_completion()...\n");
251 if( *list == NULL )
252 {
253 /* create initial list */
254 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
255 g_completion_add_items(gcmp, *list);
256 }
257 else if( line && line[0] && line[strlen(line)-1]=='/' &&
258 string_list_find(*dir_list, line) == NULL )
259 {
260 /* add directory content to list */
261 add_dir(gcmp, line, dir_list, list, c);
262 }
263 }
265 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
266 GList *items, void *data)
267 {
268 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
269 GList **dir_list = tmp->dir_list;
270 GList **list = tmp->list;
271 mpdclient_t *c = tmp->c;
272 screen_t *screen = tmp->screen;
274 D("post_completion()...\n");
275 if( g_list_length(items)>=1 )
276 {
277 screen_display_completion_list(screen, items);
278 lw->clear = 1;
279 lw->repaint = 1;
280 }
282 if( line && line[0] && line[strlen(line)-1]=='/' &&
283 string_list_find(*dir_list, line) == NULL )
284 {
285 /* add directory content to list */
286 add_dir(gcmp, line, dir_list, list, c);
287 }
288 }
290 static int
291 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
292 {
293 gchar *path;
294 GCompletion *gcmp;
295 GList *list = NULL;
296 GList *dir_list = NULL;
297 completion_callback_data_t data;
299 /* initialize completion support */
300 gcmp = g_completion_new(NULL);
301 g_completion_set_compare(gcmp, strncmp);
302 data.list = &list;
303 data.dir_list = &dir_list;
304 data.screen = screen;
305 data.c = c;
306 wrln_completion_callback_data = &data;
307 wrln_pre_completion_callback = add_pre_completion_cb;
308 wrln_post_completion_callback = add_post_completion_cb;
309 /* get path */
310 path = screen_readln(screen->status_window.w,
311 _("Add: "),
312 NULL,
313 NULL,
314 gcmp);
316 /* destroy completion data */
317 wrln_completion_callback_data = NULL;
318 wrln_pre_completion_callback = NULL;
319 wrln_post_completion_callback = NULL;
320 g_completion_free(gcmp);
321 string_list_free(list);
322 string_list_free(dir_list);
324 /* add the path to the playlist */
325 if( path && path[0] )
326 mpdclient_cmd_add_path(c, path);
328 return 0;
329 }
331 static void
332 play_init(WINDOW *w, int cols, int rows)
333 {
334 lw = list_window_init(w, cols, rows);
335 }
337 static void
338 play_open(screen_t *screen, mpdclient_t *c)
339 {
340 static gboolean install_cb = TRUE;
342 if (install_cb) {
343 mpdclient_install_playlist_callback(c, playlist_changed_callback);
344 install_cb = FALSE;
345 }
346 }
348 static void
349 play_resize(int cols, int rows)
350 {
351 lw->cols = cols;
352 lw->rows = rows;
353 }
356 static void
357 play_exit(void)
358 {
359 list_window_free(lw);
360 }
362 static const char *
363 play_title(char *str, size_t size)
364 {
365 if( strcmp(options.host, "localhost") == 0 )
366 return _("Playlist");
368 g_snprintf(str, size, _("Playlist on %s"), options.host);
369 return str;
370 }
372 static void
373 play_paint(screen_t *screen, mpdclient_t *c)
374 {
375 lw->clear = 1;
377 list_window_paint(lw, list_callback, (void *) c);
378 wnoutrefresh(lw->w);
379 }
381 static void
382 play_update(screen_t *screen, mpdclient_t *c)
383 {
384 /* hide the cursor when mpd are playing and the user are inactive */
385 if( options.hide_cursor>0 && c->status->state == MPD_STATUS_STATE_PLAY &&
386 time(NULL)-screen->input_timestamp >= options.hide_cursor ) {
387 lw->flags |= LW_HIDE_CURSOR;
388 } else {
389 lw->flags &= ~LW_HIDE_CURSOR;
390 }
392 /* center the cursor */
393 if( options.auto_center ) {
394 static int prev_song_id = 0;
396 if( c->song && prev_song_id != c->song->id ) {
397 center_playing_item(screen, c);
398 prev_song_id = c->song->id;
399 }
400 }
402 if( c->playlist.updated ) {
403 if( lw->selected >= c->playlist.length )
404 lw->selected = c->playlist.length-1;
405 if( lw->start >= c->playlist.length )
406 list_window_reset(lw);
408 play_paint(screen, c);
409 c->playlist.updated = FALSE;
410 } else if( lw->repaint || 1) {
411 list_window_paint(lw, list_callback, (void *) c);
412 wnoutrefresh(lw->w);
413 lw->repaint = 0;
414 }
415 }
417 #ifdef HAVE_GETMOUSE
418 static int
419 handle_mouse_event(screen_t *screen, mpdclient_t *c)
420 {
421 int row;
422 int selected;
423 unsigned long bstate;
425 if (screen_get_mouse_event(c, lw, c->playlist.length, &bstate, &row))
426 return 1;
428 if (bstate & BUTTON1_DOUBLE_CLICKED) {
429 /* stop */
430 screen_cmd(c, CMD_STOP);
431 return 1;
432 }
434 selected = lw->start + row;
436 if (bstate & BUTTON1_CLICKED) {
437 /* play */
438 if (lw->start + row < c->playlist.length)
439 mpdclient_cmd_play(c, lw->start + row);
440 } else if (bstate & BUTTON3_CLICKED) {
441 /* delete */
442 if (selected == lw->selected)
443 mpdclient_cmd_delete(c, lw->selected);
444 }
446 lw->selected = selected;
447 list_window_check_selected(lw, c->playlist.length);
449 return 1;
450 }
451 #else
452 #define handle_mouse_event(s,c) (0)
453 #endif
455 static int
456 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
457 {
458 switch(cmd) {
459 case CMD_PLAY:
460 mpdclient_cmd_play(c, lw->selected);
461 return 1;
462 case CMD_DELETE:
463 mpdclient_cmd_delete(c, lw->selected);
464 return 1;
465 case CMD_SAVE_PLAYLIST:
466 playlist_save(screen, c, NULL, NULL);
467 return 1;
468 case CMD_ADD:
469 handle_add_to_playlist(screen, c);
470 return 1;
471 case CMD_SCREEN_UPDATE:
472 screen->painted = 0;
473 lw->clear = 1;
474 lw->repaint = 1;
475 center_playing_item(screen, c);
476 return 1;
477 case CMD_LIST_MOVE_UP:
478 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
479 return 1;
480 case CMD_LIST_MOVE_DOWN:
481 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
482 return 1;
483 case CMD_LIST_FIND:
484 case CMD_LIST_RFIND:
485 case CMD_LIST_FIND_NEXT:
486 case CMD_LIST_RFIND_NEXT:
487 return screen_find(screen,
488 lw, c->playlist.length,
489 cmd, list_callback, (void *) c);
490 case CMD_MOUSE_EVENT:
491 return handle_mouse_event(screen,c);
492 default:
493 break;
494 }
495 return list_window_cmd(lw, c->playlist.length, cmd) ;
496 }
498 static list_window_t *
499 play_lw(void)
500 {
501 return lw;
502 }
504 screen_functions_t *
505 get_screen_playlist(void)
506 {
507 static screen_functions_t functions;
509 memset(&functions, 0, sizeof(screen_functions_t));
510 functions.init = play_init;
511 functions.exit = play_exit;
512 functions.open = play_open;
513 functions.close = NULL;
514 functions.resize = play_resize;
515 functions.paint = play_paint;
516 functions.update = play_update;
517 functions.cmd = play_cmd;
518 functions.get_lw = play_lw;
519 functions.get_title = play_title;
521 return &functions;
522 }