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 break;
56 default:
57 break;
58 }
59 /* make shure the playlist is repainted */
60 lw->clear = 1;
61 lw->repaint = 1;
62 list_window_check_selected(lw, c->playlist.length);
63 }
65 static char *
66 list_callback(int index, int *highlight, void *data)
67 {
68 static char songname[MAX_SONG_LENGTH];
69 mpdclient_t *c = (mpdclient_t *) data;
70 mpd_Song *song;
72 *highlight = 0;
73 if( (song=playlist_get_song(c, index)) == NULL )
74 {
75 return NULL;
76 }
78 if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) )
79 {
80 *highlight = 1;
81 }
82 strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
83 return songname;
84 }
86 static int
87 center_playing_item(screen_t *screen, mpdclient_t *c)
88 {
89 int length = c->playlist.length;
90 int offset = lw->selected-lw->start;
91 int index;
93 if( !lw || !c->song || length<lw->rows || IS_STOPPED(c->status->state) )
94 return 0;
96 /* try to center the song that are playing */
97 index = playlist_get_index(c, c->song);
98 D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,index);
99 lw->start = index-(lw->rows/2);
100 if( lw->start+lw->rows > length )
101 lw->start = length-lw->rows;
102 if( lw->start<0 )
103 lw->start=0;
105 /* make sure the cursor is in the window */
106 lw->selected = lw->start+offset;
107 list_window_check_selected(lw, length);
109 lw->clear = 1;
110 lw->repaint = 1;
112 return 0;
113 }
115 static int
116 handle_save_playlist(screen_t *screen, mpdclient_t *c, char *name)
117 {
118 gchar *filename;
119 gint error;
120 GCompletion *gcmp;
121 GList *list = NULL;
123 void pre_completion_cb(GCompletion *gcmp, gchar *line)
124 {
125 if( list == NULL )
126 {
127 /* create completion list */
128 list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
129 g_completion_add_items(gcmp, list);
130 }
131 }
133 void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
134 {
135 if( g_list_length(items)>=1 )
136 screen_display_completion_list(screen, items);
137 }
139 if( name==NULL )
140 {
141 /* initialize completion support */
142 gcmp = g_completion_new(NULL);
143 g_completion_set_compare(gcmp, strncmp);
144 wrln_pre_completion_callback = pre_completion_cb;
145 wrln_post_completion_callback = post_completion_cb;
147 /* query the user for a filename */
148 filename = screen_readln(screen->status_window.w,
149 _("Save playlist as: "),
150 NULL,
151 NULL,
152 gcmp);
153 filename=trim(filename);
155 /* destroy completion support */
156 wrln_pre_completion_callback = NULL;
157 wrln_post_completion_callback = NULL;
158 g_completion_free(gcmp);
159 list = string_list_free(list);
160 }
161 else
162 {
163 filename=g_strdup(name);
164 }
165 if( filename==NULL || filename[0]=='\0' )
166 return -1;
167 /* send save command to mpd */
168 D("Saving playlist as \'%s \'...\n", filename);
169 if( (error=mpdclient_cmd_save_playlist(c, filename)) )
170 {
171 gint code = GET_ACK_ERROR_CODE(error);
173 if( code == MPD_ACK_ERROR_EXIST )
174 {
175 char buf[256];
176 int key;
178 snprintf(buf, 256, _("Replace %s [%s/%s] ? "), filename, YES, NO);
179 key = tolower(screen_getch(screen->status_window.w, buf));
180 if( key == YES[0] )
181 {
182 char *filename_utf8 = locale_to_utf8(filename);
184 if( mpdclient_cmd_delete_playlist(c, filename_utf8) )
185 {
186 g_free(filename);
187 g_free(filename_utf8);
188 return -1;
189 }
190 g_free(filename_utf8);
191 error = handle_save_playlist(screen, c, filename);
192 g_free(filename);
193 return error;
194 }
195 screen_status_printf(_("Aborted!"));
196 }
197 g_free(filename);
198 return -1;
199 }
200 /* success */
201 screen_status_printf(_("Saved %s"), filename);
202 g_free(filename);
203 return 0;
204 }
206 static int
207 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
208 {
209 gchar *path;
210 GCompletion *gcmp;
211 GList *list = NULL;
212 GList *dir_list = NULL;
214 void add_dir(gchar *dir)
215 {
216 g_completion_remove_items(gcmp, list);
217 list = string_list_remove(list, dir);
218 list = gcmp_list_from_path(c, dir, list, GCMP_TYPE_RFILE);
219 g_completion_add_items(gcmp, list);
220 dir_list = g_list_append(dir_list, g_strdup(dir));
221 }
223 void pre_completion_cb(GCompletion *gcmp, gchar *line)
224 {
225 D("pre_completion()...\n");
226 if( list == NULL )
227 {
228 /* create initial list */
229 list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
230 g_completion_add_items(gcmp, list);
231 }
232 else if( line && line[0] && line[strlen(line)-1]=='/' &&
233 string_list_find(dir_list, line) == NULL )
234 {
235 /* add directory content to list */
236 add_dir(line);
237 }
238 }
240 void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
241 {
242 D("post_completion()...\n");
243 if( g_list_length(items)>1 )
244 screen_display_completion_list(screen, items);
246 if( line && line[0] && line[strlen(line)-1]=='/' &&
247 string_list_find(dir_list, line) == NULL )
248 {
249 /* add directory content to list */
250 add_dir(line);
251 }
252 }
254 /* initialize completion support */
255 gcmp = g_completion_new(NULL);
256 g_completion_set_compare(gcmp, strncmp);
257 wrln_pre_completion_callback = pre_completion_cb;
258 wrln_post_completion_callback = post_completion_cb;
259 /* get path */
260 path = screen_readln(screen->status_window.w,
261 _("Add: "),
262 NULL,
263 NULL,
264 gcmp);
266 /* destroy completion data */
267 wrln_pre_completion_callback = NULL;
268 wrln_post_completion_callback = NULL;
269 g_completion_free(gcmp);
270 string_list_free(list);
271 string_list_free(dir_list);
273 /* add the path to the playlist */
274 if( path && path[0] )
275 mpdclient_cmd_add_path(c, path);
277 lw->clear = 1;
278 lw->repaint = 1;
280 return 0;
281 }
283 static void
284 play_init(WINDOW *w, int cols, int rows)
285 {
286 lw = list_window_init(w, cols, rows);
287 }
289 static void
290 play_open(screen_t *screen, mpdclient_t *c)
291 {
292 static gboolean install_cb = TRUE;
294 if( install_cb )
295 {
296 mpdclient_install_playlist_callback(c, playlist_changed_callback);
297 install_cb = FALSE;
298 }
299 }
301 static void
302 play_resize(int cols, int rows)
303 {
304 lw->cols = cols;
305 lw->rows = rows;
306 }
309 static void
310 play_exit(void)
311 {
312 list_window_free(lw);
313 }
315 static char *
316 play_title(char *str, size_t size)
317 {
318 if( strcmp(options.host, "localhost") == 0 )
319 return _("Playlist");
321 snprintf(str, size, _("Playlist on %s"), options.host);
323 return str;
324 }
326 static void
327 play_paint(screen_t *screen, mpdclient_t *c)
328 {
329 lw->clear = 1;
331 list_window_paint(lw, list_callback, (void *) c);
332 wnoutrefresh(lw->w);
333 }
335 static void
336 play_update(screen_t *screen, mpdclient_t *c)
337 {
338 if( options.auto_center )
339 {
340 static int prev_song_id = 0;
342 if( c->song && prev_song_id != c->song->id )
343 {
344 center_playing_item(screen, c);
345 prev_song_id = c->song->id;
346 }
347 }
349 if( c->playlist.updated )
350 {
351 if( lw->selected >= c->playlist.length )
352 lw->selected = c->playlist.length-1;
353 if( lw->start >= c->playlist.length )
354 list_window_reset(lw);
356 play_paint(screen, c);
357 c->playlist.updated = FALSE;
358 }
359 else if( lw->repaint || 1)
360 {
361 list_window_paint(lw, list_callback, (void *) c);
362 wnoutrefresh(lw->w);
363 lw->repaint = 0;
364 }
365 }
367 static int
368 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
369 {
370 switch(cmd)
371 {
372 case CMD_PLAY:
373 mpdclient_cmd_play(c, lw->selected);
374 return 1;
375 case CMD_DELETE:
376 mpdclient_cmd_delete(c, lw->selected);
377 return 1;
378 case CMD_SAVE_PLAYLIST:
379 handle_save_playlist(screen, c, NULL);
380 return 1;
381 case CMD_ADD:
382 handle_add_to_playlist(screen, c);
383 return 1;
384 case CMD_SCREEN_UPDATE:
385 center_playing_item(screen, c);
386 return 1;
387 case CMD_LIST_MOVE_UP:
388 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
389 return 1;
390 case CMD_LIST_MOVE_DOWN:
391 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
392 return 1;
393 case CMD_LIST_FIND:
394 case CMD_LIST_RFIND:
395 case CMD_LIST_FIND_NEXT:
396 case CMD_LIST_RFIND_NEXT:
397 return screen_find(screen, c,
398 lw, c->playlist.length,
399 cmd, list_callback);
400 default:
401 break;
402 }
403 return list_window_cmd(lw, c->playlist.length, cmd) ;
404 }
408 static list_window_t *
409 play_lw(void)
410 {
411 return lw;
412 }
415 screen_functions_t *
416 get_screen_playlist(void)
417 {
418 static screen_functions_t functions;
420 memset(&functions, 0, sizeof(screen_functions_t));
421 functions.init = play_init;
422 functions.exit = play_exit;
423 functions.open = play_open;
424 functions.close = NULL;
425 functions.resize = play_resize;
426 functions.paint = play_paint;
427 functions.update = play_update;
428 functions.cmd = play_cmd;
429 functions.get_lw = play_lw;
430 functions.get_title = play_title;
432 return &functions;
433 }