6df8c9f81a7f8408924070408117576178626a5e
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 "config.h"
20 #include "ncmpc.h"
21 #include "options.h"
22 #include "support.h"
23 #include "mpdclient.h"
24 #include "utils.h"
25 #include "strfsong.h"
26 #include "wreadln.h"
27 #include "command.h"
28 #include "colors.h"
29 #include "screen.h"
30 #include "screen_utils.h"
31 #include "screen_play.h"
32 #include "gcc.h"
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <glib.h>
39 #include <ncurses.h>
41 #define MAX_SONG_LENGTH 512
43 typedef struct
44 {
45 GList **list;
46 GList **dir_list;
47 screen_t *screen;
48 mpdclient_t *c;
49 } completion_callback_data_t;
51 static GTime input_timestamp;
52 static list_window_t *lw = NULL;
53 static long long playlist_id;
55 static void
56 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
57 {
58 D("screen_play.c> playlist_callback() [%d]\n", event);
59 switch (event) {
60 case PLAYLIST_EVENT_DELETE:
61 break;
62 case PLAYLIST_EVENT_MOVE:
63 lw->selected = *((int *) data);
64 if (lw->selected < lw->start)
65 lw->start--;
66 break;
67 default:
68 break;
69 }
70 /* make shure the playlist is repainted */
71 lw->clear = 1;
72 list_window_check_selected(lw, c->playlist.list->len);
73 }
75 static const char *
76 list_callback(unsigned idx, int *highlight, void *data)
77 {
78 static char songname[MAX_SONG_LENGTH];
79 mpdclient_t *c = (mpdclient_t *) data;
80 mpd_Song *song;
82 if (idx >= playlist_length(&c->playlist))
83 return NULL;
85 song = playlist_get(&c->playlist, idx);
87 if (c->song != NULL && song->id == c->song->id &&
88 c->status != NULL && !IS_STOPPED(c->status->state))
89 *highlight = 1;
91 strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
92 return songname;
93 }
95 static void
96 center_playing_item(mpdclient_t *c)
97 {
98 unsigned length = c->playlist.list->len;
99 unsigned offset = lw->selected - lw->start;
100 int idx;
102 if (!c->song || length < lw->rows ||
103 c->status == NULL || IS_STOPPED(c->status->state))
104 return;
106 /* try to center the song that are playing */
107 idx = playlist_get_index(c, c->song);
108 D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,idx);
109 if (idx < 0)
110 return;
112 list_window_center(lw, length, idx);
114 /* make sure the cursor is in the window */
115 lw->selected = lw->start+offset;
116 list_window_check_selected(lw, length);
118 return;
119 }
121 static void
122 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
123 {
124 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
125 GList **list = tmp->list;
126 mpdclient_t *c = tmp->c;
128 if( *list == NULL ) {
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 static void
136 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
137 GList *items, void *data)
138 {
139 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
140 screen_t *screen = tmp->screen;
142 if (g_list_length(items) >= 1) {
143 screen_display_completion_list(screen, items);
144 lw->clear = 1;
145 }
146 }
148 int
149 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
150 {
151 gchar *filename;
152 gint error;
153 GCompletion *gcmp;
154 GList *list = NULL;
155 completion_callback_data_t data;
157 if (name == NULL) {
158 /* initialize completion support */
159 gcmp = g_completion_new(NULL);
160 g_completion_set_compare(gcmp, strncmp);
161 data.list = &list;
162 data.dir_list = NULL;
163 data.screen = screen;
164 data.c = c;
165 wrln_completion_callback_data = &data;
166 wrln_pre_completion_callback = save_pre_completion_cb;
167 wrln_post_completion_callback = save_post_completion_cb;
170 /* query the user for a filename */
171 filename = screen_readln(screen->status_window.w,
172 _("Save playlist as: "),
173 defaultname,
174 NULL,
175 gcmp);
177 /* destroy completion support */
178 wrln_completion_callback_data = NULL;
179 wrln_pre_completion_callback = NULL;
180 wrln_post_completion_callback = NULL;
181 g_completion_free(gcmp);
182 list = string_list_free(list);
183 if( filename )
184 filename=g_strstrip(filename);
185 } else
186 filename=g_strdup(name);
188 if (filename == NULL || filename[0] == '\0')
189 return -1;
191 /* send save command to mpd */
192 D("Saving playlist as \'%s \'...\n", filename);
193 if ((error = mpdclient_cmd_save_playlist(c, filename))) {
194 gint code = GET_ACK_ERROR_CODE(error);
196 if (code == MPD_ACK_ERROR_EXIST) {
197 char *buf;
198 int key;
200 buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
201 filename, YES, NO);
202 key = tolower(screen_getch(screen->status_window.w,
203 buf));
204 g_free(buf);
206 if (key == YES[0]) {
207 if (mpdclient_cmd_delete_playlist(c, filename)) {
208 g_free(filename);
209 return -1;
210 }
212 error = playlist_save(screen, c, filename, NULL);
213 g_free(filename);
214 return error;
215 }
217 screen_status_printf(_("Aborted!"));
218 }
220 g_free(filename);
221 return -1;
222 }
224 /* success */
225 screen_status_printf(_("Saved %s"), filename);
226 g_free(filename);
227 return 0;
228 }
230 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
231 GList **list, mpdclient_t *c)
232 {
233 g_completion_remove_items(gcmp, *list);
234 *list = string_list_remove(*list, dir);
235 *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
236 g_completion_add_items(gcmp, *list);
237 *dir_list = g_list_append(*dir_list, g_strdup(dir));
238 }
240 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
241 {
242 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
243 GList **dir_list = tmp->dir_list;
244 GList **list = tmp->list;
245 mpdclient_t *c = tmp->c;
247 D("pre_completion()...\n");
248 if (*list == NULL) {
249 /* create initial list */
250 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
251 g_completion_add_items(gcmp, *list);
252 } else if (line && line[0] && line[strlen(line)-1]=='/' &&
253 string_list_find(*dir_list, line) == NULL) {
254 /* add directory content to list */
255 add_dir(gcmp, line, dir_list, list, c);
256 }
257 }
259 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
260 GList *items, void *data)
261 {
262 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
263 GList **dir_list = tmp->dir_list;
264 GList **list = tmp->list;
265 mpdclient_t *c = tmp->c;
266 screen_t *screen = tmp->screen;
268 D("post_completion()...\n");
269 if (g_list_length(items) >= 1) {
270 screen_display_completion_list(screen, items);
271 lw->clear = 1;
272 }
274 if (line && line[0] && line[strlen(line) - 1] == '/' &&
275 string_list_find(*dir_list, line) == NULL) {
276 /* add directory content to list */
277 add_dir(gcmp, line, dir_list, list, c);
278 }
279 }
281 static int
282 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
283 {
284 gchar *path;
285 GCompletion *gcmp;
286 GList *list = NULL;
287 GList *dir_list = NULL;
288 completion_callback_data_t data;
290 /* initialize completion support */
291 gcmp = g_completion_new(NULL);
292 g_completion_set_compare(gcmp, strncmp);
293 data.list = &list;
294 data.dir_list = &dir_list;
295 data.screen = screen;
296 data.c = c;
297 wrln_completion_callback_data = &data;
298 wrln_pre_completion_callback = add_pre_completion_cb;
299 wrln_post_completion_callback = add_post_completion_cb;
301 /* get path */
302 path = screen_readln(screen->status_window.w,
303 _("Add: "),
304 NULL,
305 NULL,
306 gcmp);
308 /* destroy completion data */
309 wrln_completion_callback_data = NULL;
310 wrln_pre_completion_callback = NULL;
311 wrln_post_completion_callback = NULL;
312 g_completion_free(gcmp);
313 string_list_free(list);
314 string_list_free(dir_list);
316 /* add the path to the playlist */
317 if (path && path[0])
318 mpdclient_cmd_add_path(c, path);
320 return 0;
321 }
323 static void
324 play_init(WINDOW *w, int cols, int rows)
325 {
326 lw = list_window_init(w, cols, rows);
327 }
329 static void
330 play_open(mpd_unused screen_t *screen, mpdclient_t *c)
331 {
332 static gboolean install_cb = TRUE;
334 input_timestamp = time(NULL);
336 if (install_cb) {
337 mpdclient_install_playlist_callback(c, playlist_changed_callback);
338 install_cb = FALSE;
339 }
340 }
342 static void
343 play_resize(int cols, int rows)
344 {
345 lw->cols = cols;
346 lw->rows = rows;
347 }
350 static void
351 play_exit(void)
352 {
353 list_window_free(lw);
354 }
356 static const char *
357 play_title(char *str, size_t size)
358 {
359 if( strcmp(options.host, "localhost") == 0 )
360 return _("Playlist");
362 g_snprintf(str, size, _("Playlist on %s"), options.host);
363 return str;
364 }
366 static void
367 play_paint(mpd_unused screen_t *screen, mpdclient_t *c)
368 {
369 lw->clear = 1;
371 list_window_paint(lw, list_callback, (void *) c);
372 wnoutrefresh(lw->w);
373 }
375 static void
376 play_update(screen_t *screen, mpdclient_t *c)
377 {
378 /* hide the cursor when mpd are playing and the user are inactive */
379 if (options.hide_cursor > 0 &&
380 (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) &&
381 time(NULL) - input_timestamp >= options.hide_cursor ) {
382 lw->flags |= LW_HIDE_CURSOR;
383 } else {
384 lw->flags &= ~LW_HIDE_CURSOR;
385 }
387 /* center the cursor */
388 if (options.auto_center) {
389 static int prev_song_id = 0;
391 if (c->song && prev_song_id != c->song->id) {
392 center_playing_item(c);
393 prev_song_id = c->song->id;
394 }
395 }
397 if (c->playlist.id != playlist_id) {
398 list_window_check_selected(lw, playlist_length(&c->playlist));
399 play_paint(screen, c);
400 playlist_id = c->playlist.id;
401 } else {
402 list_window_paint(lw, list_callback, (void *) c);
403 wnoutrefresh(lw->w);
404 }
405 }
407 #ifdef HAVE_GETMOUSE
408 static int
409 handle_mouse_event(mpd_unused screen_t *screen, mpdclient_t *c)
410 {
411 int row;
412 unsigned selected;
413 unsigned long bstate;
415 if (screen_get_mouse_event(c, &bstate, &row) ||
416 list_window_mouse(lw, c->playlist.list->len, bstate, row))
417 return 1;
419 if (bstate & BUTTON1_DOUBLE_CLICKED) {
420 /* stop */
421 screen_cmd(c, CMD_STOP);
422 return 1;
423 }
425 selected = lw->start + row;
427 if (bstate & BUTTON1_CLICKED) {
428 /* play */
429 if (lw->start + row < c->playlist.list->len)
430 mpdclient_cmd_play(c, lw->start + row);
431 } else if (bstate & BUTTON3_CLICKED) {
432 /* delete */
433 if (selected == lw->selected)
434 mpdclient_cmd_delete(c, lw->selected);
435 }
437 lw->selected = selected;
438 list_window_check_selected(lw, c->playlist.list->len);
440 return 1;
441 }
442 #else
443 #define handle_mouse_event(s,c) (0)
444 #endif
446 static int
447 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
448 {
449 input_timestamp = time(NULL);
451 switch(cmd) {
452 case CMD_PLAY:
453 mpdclient_cmd_play(c, lw->selected);
454 return 1;
455 case CMD_DELETE:
456 mpdclient_cmd_delete(c, lw->selected);
457 return 1;
458 case CMD_SAVE_PLAYLIST:
459 playlist_save(screen, c, NULL, NULL);
460 return 1;
461 case CMD_ADD:
462 handle_add_to_playlist(screen, c);
463 return 1;
464 case CMD_SCREEN_UPDATE:
465 center_playing_item(c);
466 return 0;
468 case CMD_LIST_MOVE_UP:
469 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
470 return 1;
471 case CMD_LIST_MOVE_DOWN:
472 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
473 return 1;
474 case CMD_LIST_FIND:
475 case CMD_LIST_RFIND:
476 case CMD_LIST_FIND_NEXT:
477 case CMD_LIST_RFIND_NEXT:
478 return screen_find(screen,
479 lw, c->playlist.list->len,
480 cmd, list_callback, (void *) c);
481 case CMD_MOUSE_EVENT:
482 return handle_mouse_event(screen,c);
483 default:
484 break;
485 }
486 return list_window_cmd(lw, c->playlist.list->len, cmd);
487 }
489 const struct screen_functions screen_playlist = {
490 .init = play_init,
491 .exit = play_exit,
492 .open = play_open,
493 .close = NULL,
494 .resize = play_resize,
495 .paint = play_paint,
496 .update = play_update,
497 .cmd = play_cmd,
498 .get_title = play_title,
499 };