1 /*
2 * (c) 2004 by Kalle Wallin (kaw@linux.se)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include <stdlib.h>
20 #include <string.h>
21 #include <glib.h>
22 #include <ncurses.h>
24 #include "config.h"
25 #include "ncmpc.h"
26 #include "options.h"
27 #include "support.h"
28 #include "libmpdclient.h"
29 #include "mpc.h"
30 #include "command.h"
31 #include "screen.h"
32 #include "screen_utils.h"
33 #include "screen_file.h"
34 #include "screen_play.h"
36 #define BUFSIZE 256
38 static list_window_t *lw = NULL;
40 static char *
41 list_callback(int index, int *highlight, void *data)
42 {
43 mpd_client_t *c = (mpd_client_t *) data;
44 mpd_Song *song;
46 *highlight = 0;
47 if( (song=mpc_playlist_get_song(c, index)) == NULL )
48 {
49 return NULL;
50 }
52 if( index==c->song_id && !IS_STOPPED(c->status->state) )
53 {
54 *highlight = 1;
55 }
57 return mpc_get_song_name(song);
58 }
60 static int
61 center_playing_item(screen_t *screen, mpd_client_t *c)
62 {
63 int length = c->playlist_length;
64 int offset = lw->selected-lw->start;
66 if( !lw || length<lw->rows || IS_STOPPED(c->status->state) )
67 return 0;
69 /* try to center the song that are playing */
70 lw->start = c->song_id-(lw->rows/2);
71 if( lw->start+lw->rows > length )
72 lw->start = length-lw->rows;
73 if( lw->start<0 )
74 lw->start=0;
76 /* make sure the cursor is in the window */
77 lw->selected = lw->start+offset;
78 list_window_check_selected(lw, length);
80 lw->clear = 1;
81 lw->repaint = 1;
83 return 0;
84 }
86 static int
87 handle_save_playlist(screen_t *screen, mpd_client_t *c)
88 {
89 char *filename, *filename_utf8;
91 filename=screen_getstr(screen->status_window.w, _("Save playlist as: "));
92 filename=trim(filename);
93 if( filename==NULL || filename[0]=='\0' )
94 return -1;
95 /* convert filename to utf-8 */
96 filename_utf8 = locale_to_utf8(filename);
97 /* send save command to mpd */
98 mpd_sendSaveCommand(c->connection, filename_utf8);
99 mpd_finishCommand(c->connection);
100 g_free(filename_utf8);
101 /* handle errors */
102 if( mpc_error(c))
103 {
104 if( mpc_error_str(c) )
105 {
106 char *str = utf8_to_locale(mpc_error_str(c));
107 screen_status_message(str);
108 g_free(str);
109 }
110 else
111 screen_status_printf(_("Error: Unable to save playlist as %s"),
112 filename);
113 mpd_clearError(c->connection);
114 beep();
115 return -1;
116 }
117 /* success */
118 screen_status_printf(_("Saved %s"), filename);
119 g_free(filename);
120 /* update the file list if it has been initalized */
121 if( c->filelist )
122 {
123 list_window_t *file_lw = get_filelist_window();
125 mpc_update_filelist(c);
126 list_window_check_selected(file_lw, c->filelist_length);
127 }
128 return 0;
129 }
131 static void
132 play_init(WINDOW *w, int cols, int rows)
133 {
134 lw = list_window_init(w, cols, rows);
135 }
137 static void
138 play_resize(int cols, int rows)
139 {
140 lw->cols = cols;
141 lw->rows = rows;
142 }
145 static void
146 play_exit(void)
147 {
148 list_window_free(lw);
149 }
151 static char *
152 play_title(void)
153 {
154 return _(TOP_HEADER_PREFIX "Playlist");
155 }
157 static void
158 play_paint(screen_t *screen, mpd_client_t *c)
159 {
160 lw->clear = 1;
162 list_window_paint(lw, list_callback, (void *) c);
163 wnoutrefresh(lw->w);
164 }
166 static void
167 play_update(screen_t *screen, mpd_client_t *c)
168 {
169 if( options.auto_center )
170 {
171 static int prev_song_id = 0;
173 if( prev_song_id != c->song_id )
174 {
175 center_playing_item(screen, c);
176 prev_song_id = c->song_id;
177 }
178 }
180 if( c->playlist_updated )
181 {
182 if( lw->selected >= c->playlist_length )
183 lw->selected = c->playlist_length-1;
184 if( lw->start >= c->playlist_length )
185 list_window_reset(lw);
187 play_paint(screen, c);
188 c->playlist_updated = 0;
189 }
190 else if( lw->repaint || 1)
191 {
192 list_window_paint(lw, list_callback, (void *) c);
193 wnoutrefresh(lw->w);
194 lw->repaint = 0;
195 }
196 }
198 static int
199 play_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
200 {
201 switch(cmd)
202 {
203 case CMD_DELETE:
204 playlist_delete_song(c, lw->selected);
205 return 1;
206 case CMD_SAVE_PLAYLIST:
207 handle_save_playlist(screen, c);
208 return 1;
209 case CMD_SCREEN_UPDATE:
210 center_playing_item(screen, c);
211 return 1;
212 case CMD_LIST_MOVE_UP:
213 playlist_move_song(c, lw->selected, lw->selected-1);
214 break;
215 case CMD_LIST_MOVE_DOWN:
216 playlist_move_song(c, lw->selected, lw->selected+1);
217 break;
218 case CMD_LIST_FIND:
219 case CMD_LIST_RFIND:
220 case CMD_LIST_FIND_NEXT:
221 case CMD_LIST_RFIND_NEXT:
222 return screen_find(screen, c,
223 lw, c->playlist_length,
224 cmd, list_callback);
225 default:
226 break;
227 }
228 return list_window_cmd(lw, c->playlist_length, cmd) ;
229 }
233 static list_window_t *
234 play_lw(void)
235 {
236 return lw;
237 }
239 int
240 play_get_selected(void)
241 {
242 return lw->selected;
243 }
245 int
246 playlist_move_song(mpd_client_t *c, int old_index, int new_index)
247 {
248 int index1, index2;
249 GList *item1, *item2;
250 gpointer data1, data2;
252 if( old_index==new_index || new_index<0 || new_index>=c->playlist_length )
253 return -1;
255 /* send the move command to mpd */
256 mpd_sendMoveCommand(c->connection, old_index, new_index);
257 mpd_finishCommand(c->connection);
258 if( mpc_error(c) )
259 return -1;
261 index1 = MIN(old_index, new_index);
262 index2 = MAX(old_index, new_index);
263 item1 = g_list_nth(c->playlist, index1);
264 item2 = g_list_nth(c->playlist, index2);
265 data1 = item1->data;
266 data2 = item2->data;
268 /* move the second item */
269 D(fprintf(stderr, "move second item [%d->%d]...\n", index2, index1));
270 c->playlist = g_list_remove(c->playlist, data2);
271 c->playlist = g_list_insert_before(c->playlist, item1, data2);
273 /* move the first item */
274 if( index2-index1 >1 )
275 {
276 D(fprintf(stderr, "move first item [%d->%d]...\n", index1, index2));
277 item2 = g_list_nth(c->playlist, index2);
278 c->playlist = g_list_remove(c->playlist, data1);
279 c->playlist = g_list_insert_before(c->playlist, item2, data1);
280 }
282 /* increment the playlist id, so we dont retrives a new playlist */
283 c->playlist_id++;
285 /* make shure the playlist is repainted */
286 lw->clear = 1;
287 lw->repaint = 1;
289 /* keep song selected */
290 lw->selected = new_index;
292 return 0;
293 }
295 int
296 playlist_add_song(mpd_client_t *c, mpd_Song *song)
297 {
298 if( !song || !song->file )
299 return -1;
301 /* send the add command to mpd */
302 mpd_sendAddCommand(c->connection, song->file);
303 mpd_finishCommand(c->connection);
304 if( mpc_error(c) )
305 return -1;
307 /* add the song to playlist */
308 c->playlist = g_list_append(c->playlist, (gpointer) mpd_songDup(song));
309 c->playlist_length++;
311 /* increment the playlist id, so we dont retrives a new playlist */
312 c->playlist_id++;
314 /* make shure the playlist is repainted */
315 lw->clear = 1;
316 lw->repaint = 1;
318 /* set selected highlight in the browse screen */
319 file_set_highlight(c, song, 1);
321 return 0;
322 }
324 int
325 playlist_delete_song(mpd_client_t *c, int index)
326 {
327 mpd_Song *song = mpc_playlist_get_song(c, index);
329 if( !song )
330 return -1;
332 /* send the delete command to mpd */
333 mpd_sendDeleteCommand(c->connection, index);
334 mpd_finishCommand(c->connection);
335 if( mpc_error(c) )
336 return -1;
338 /* print a status message */
339 screen_status_printf(_("Removed \'%s\' from playlist!"),
340 mpc_get_song_name(song));
341 /* clear selected highlight in the browse screen */
342 file_set_highlight(c, song, 0);
344 /* increment the playlist id, so we dont retrives a new playlist */
345 c->playlist_id++;
347 /* remove references to the song */
348 if( c->song == song )
349 {
350 c->song = NULL;
351 c->song_id = -1;
352 }
354 /* remove the song from the playlist */
355 c->playlist = g_list_remove(c->playlist, (gpointer) song);
356 c->playlist_length = g_list_length(c->playlist);
357 mpd_freeSong(song);
359 /* make shure the playlist is repainted */
360 lw->clear = 1;
361 lw->repaint = 1;
362 list_window_check_selected(lw, c->playlist_length);
364 return 0;
365 }
368 screen_functions_t *
369 get_screen_playlist(void)
370 {
371 static screen_functions_t functions;
373 memset(&functions, 0, sizeof(screen_functions_t));
374 functions.init = play_init;
375 functions.exit = play_exit;
376 functions.open = NULL;
377 functions.close = NULL;
378 functions.resize = play_resize;
379 functions.paint = play_paint;
380 functions.update = play_update;
381 functions.cmd = play_cmd;
382 functions.get_lw = play_lw;
383 functions.get_title = play_title;
385 return &functions;
386 }