Code

disable more features with --enable-mini
[ncmpc.git] / src / screen_play.c
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 "i18n.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>
40 #define MAX_SONG_LENGTH 512
42 #ifndef NCMPC_MINI
43 typedef struct
44 {
45         GList **list;
46         GList **dir_list;
47         mpdclient_t *c;
48 } completion_callback_data_t;
49 #endif
51 static struct mpdclient_playlist *playlist;
52 static int current_song_id = -1;
53 static list_window_t *lw = NULL;
54 static guint timer_hide_cursor_id;
56 static void
57 play_paint(void);
59 static void
60 playlist_repaint(void)
61 {
62         play_paint();
63         wrefresh(lw->w);
64 }
66 static void
67 playlist_repaint_if_active(void)
68 {
69         if (screen_is_visible(&screen_playlist))
70                 playlist_repaint();
71 }
73 static void
74 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
75 {
76         switch (event) {
77         case PLAYLIST_EVENT_DELETE:
78                 break;
79         case PLAYLIST_EVENT_MOVE:
80                 lw->selected = *((int *) data);
81                 if (lw->selected < lw->start)
82                         lw->start--;
83                 break;
84         default:
85                 break;
86         }
88         list_window_check_selected(lw, c->playlist.list->len);
89         playlist_repaint_if_active();
90 }
92 static const char *
93 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
94 {
95         static char songname[MAX_SONG_LENGTH];
96         mpd_Song *song;
98         if (playlist == NULL || idx >= playlist_length(playlist))
99                 return NULL;
101         song = playlist_get(playlist, idx);
102         if (song->id == current_song_id)
103                 *highlight = 1;
105         strfsong(songname, MAX_SONG_LENGTH, options.list_format, song);
106         return songname;
109 static void
110 center_playing_item(mpdclient_t *c)
112         unsigned length = c->playlist.list->len;
113         unsigned offset = lw->selected - lw->start;
114         int idx;
116         if (!c->song || length < lw->rows ||
117             c->status == NULL || IS_STOPPED(c->status->state))
118                 return;
120         /* try to center the song that are playing */
121         idx = playlist_get_index(c, c->song);
122         if (idx < 0)
123                 return;
125         list_window_center(lw, length, idx);
127         /* make sure the cursor is in the window */
128         lw->selected = lw->start+offset;
129         list_window_check_selected(lw, length);
132 #ifndef NCMPC_MINI
133 static void
134 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
136         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
137         GList **list = tmp->list;
138         mpdclient_t *c = tmp->c;
140         if( *list == NULL ) {
141                 /* create completion list */
142                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
143                 g_completion_add_items(gcmp, *list);
144         }
147 static void
148 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
149                         GList *items, mpd_unused void *data)
151         if (g_list_length(items) >= 1)
152                 screen_display_completion_list(items);
154 #endif
156 int
157 playlist_save(mpdclient_t *c, char *name, char *defaultname)
159         gchar *filename;
160         gint error;
161 #ifndef NCMPC_MINI
162         GCompletion *gcmp;
163         GList *list = NULL;
164         completion_callback_data_t data;
165 #endif
167 #ifdef NCMPC_MINI
168         (void)defaultname;
169 #endif
171 #ifndef NCMPC_MINI
172         if (name == NULL) {
173                 /* initialize completion support */
174                 gcmp = g_completion_new(NULL);
175                 g_completion_set_compare(gcmp, strncmp);
176                 data.list = &list;
177                 data.dir_list = NULL;
178                 data.c = c;
179                 wrln_completion_callback_data = &data;
180                 wrln_pre_completion_callback = save_pre_completion_cb;
181                 wrln_post_completion_callback = save_post_completion_cb;
184                 /* query the user for a filename */
185                 filename = screen_readln(screen.status_window.w,
186                                          _("Save playlist as: "),
187                                          defaultname,
188                                          NULL,
189                                          gcmp);
191                 /* destroy completion support */
192                 wrln_completion_callback_data = NULL;
193                 wrln_pre_completion_callback = NULL;
194                 wrln_post_completion_callback = NULL;
195                 g_completion_free(gcmp);
196                 list = string_list_free(list);
197                 if( filename )
198                         filename=g_strstrip(filename);
199         } else
200 #endif
201                         filename=g_strdup(name);
203         if (filename == NULL || filename[0] == '\0')
204                 return -1;
206         /* send save command to mpd */
207         if ((error = mpdclient_cmd_save_playlist(c, filename))) {
208                 gint code = GET_ACK_ERROR_CODE(error);
210                 if (code == MPD_ACK_ERROR_EXIST) {
211                         char *buf;
212                         int key;
214                         buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
215                                               filename, YES, NO);
216                         key = tolower(screen_getch(screen.status_window.w,
217                                                    buf));
218                         g_free(buf);
220                         if (key == YES[0]) {
221                                 if (mpdclient_cmd_delete_playlist(c, filename)) {
222                                         g_free(filename);
223                                         return -1;
224                                 }
226                                 error = playlist_save(c, filename, NULL);
227                                 g_free(filename);
228                                 return error;
229                         }
231                         screen_status_printf(_("Aborted!"));
232                 }
234                 g_free(filename);
235                 return -1;
236         }
238         /* success */
239         screen_status_printf(_("Saved %s"), filename);
240         g_free(filename);
241         return 0;
244 #ifndef NCMPC_MINI
245 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
246                     GList **list, mpdclient_t *c)
248         g_completion_remove_items(gcmp, *list);
249         *list = string_list_remove(*list, dir);
250         *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
251         g_completion_add_items(gcmp, *list);
252         *dir_list = g_list_append(*dir_list, g_strdup(dir));
255 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
257         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
258         GList **dir_list = tmp->dir_list;
259         GList **list = tmp->list;
260         mpdclient_t *c = tmp->c;
262         if (*list == NULL) {
263                 /* create initial list */
264                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
265                 g_completion_add_items(gcmp, *list);
266         } else if (line && line[0] && line[strlen(line)-1]=='/' &&
267                    string_list_find(*dir_list, line) == NULL) {
268                 /* add directory content to list */
269                 add_dir(gcmp, line, dir_list, list, c);
270         }
273 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
274                                    GList *items, void *data)
276         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
277         GList **dir_list = tmp->dir_list;
278         GList **list = tmp->list;
279         mpdclient_t *c = tmp->c;
281         if (g_list_length(items) >= 1)
282                 screen_display_completion_list(items);
284         if (line && line[0] && line[strlen(line) - 1] == '/' &&
285             string_list_find(*dir_list, line) == NULL) {
286                 /* add directory content to list */
287                 add_dir(gcmp, line, dir_list, list, c);
288         }
290 #endif
292 static int
293 handle_add_to_playlist(mpdclient_t *c)
295         gchar *path;
296 #ifndef NCMPC_MINI
297         GCompletion *gcmp;
298         GList *list = NULL;
299         GList *dir_list = NULL;
300         completion_callback_data_t data;
302         /* initialize completion support */
303         gcmp = g_completion_new(NULL);
304         g_completion_set_compare(gcmp, strncmp);
305         data.list = &list;
306         data.dir_list = &dir_list;
307         data.c = c;
308         wrln_completion_callback_data = &data;
309         wrln_pre_completion_callback = add_pre_completion_cb;
310         wrln_post_completion_callback = add_post_completion_cb;
311 #endif
313         /* get path */
314         path = screen_readln(screen.status_window.w,
315                              _("Add: "),
316                              NULL,
317                              NULL,
318 #ifdef NCMPC_MINI
319                              NULL
320 #else
321                              gcmp
322 #endif
323                              );
325         /* destroy completion data */
326 #ifndef NCMPC_MINI
327         wrln_completion_callback_data = NULL;
328         wrln_pre_completion_callback = NULL;
329         wrln_post_completion_callback = NULL;
330         g_completion_free(gcmp);
331         string_list_free(list);
332         string_list_free(dir_list);
333 #endif
335         /* add the path to the playlist */
336         if (path && path[0])
337                 mpdclient_cmd_add_path(c, path);
339         return 0;
342 static void
343 play_init(WINDOW *w, int cols, int rows)
345         lw = list_window_init(w, cols, rows);
348 static gboolean
349 timer_hide_cursor(gpointer data)
351         struct mpdclient *c = data;
353         assert(options.hide_cursor > 0);
354         assert(timer_hide_cursor_id != 0);
356         timer_hide_cursor_id = 0;
358         /* hide the cursor when mpd is playing and the user is inactive */
360         if (c->status != NULL && c->status->state == MPD_STATUS_STATE_PLAY) {
361                 lw->flags |= LW_HIDE_CURSOR;
362                 playlist_repaint();
363         } else
364                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
365                                                      timer_hide_cursor, c);
367         return FALSE;
370 static void
371 play_open(mpdclient_t *c)
373         static gboolean install_cb = TRUE;
375         playlist = &c->playlist;
377         assert(timer_hide_cursor_id == 0);
378         if (options.hide_cursor > 0) {
379                 lw->flags &= ~LW_HIDE_CURSOR;
380                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
381                                                      timer_hide_cursor, c);
382         }
384         if (install_cb) {
385                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
386                 install_cb = FALSE;
387         }
390 static void
391 play_close(void)
393         if (timer_hide_cursor_id != 0) {
394                 g_source_remove(timer_hide_cursor_id);
395                 timer_hide_cursor_id = 0;
396         }
399 static void
400 play_resize(int cols, int rows)
402         lw->cols = cols;
403         lw->rows = rows;
407 static void
408 play_exit(void)
410         list_window_free(lw);
413 static const char *
414 play_title(char *str, size_t size)
416         if( strcmp(options.host, "localhost") == 0 )
417                 return _("Playlist");
419         g_snprintf(str, size, _("Playlist on %s"), options.host);
420         return str;
423 static void
424 play_paint(void)
426         list_window_paint(lw, list_callback, NULL);
429 static void
430 play_update(mpdclient_t *c)
432         static int prev_song_id = -1;
434         current_song_id = c->song != NULL && c->status != NULL &&
435                 !IS_STOPPED(c->status->state) ? c->song->id : -1;
437         if (current_song_id != prev_song_id) {
438                 prev_song_id = current_song_id;
440                 /* center the cursor */
441                 if (options.auto_center && current_song_id != -1)
442                         center_playing_item(c);
444                 playlist_repaint();
445         }
448 #ifdef HAVE_GETMOUSE
449 static int
450 handle_mouse_event(struct mpdclient *c)
452         int row;
453         unsigned selected;
454         unsigned long bstate;
456         if (screen_get_mouse_event(c, &bstate, &row) ||
457             list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
458                 playlist_repaint();
459                 return 1;
460         }
462         if (bstate & BUTTON1_DOUBLE_CLICKED) {
463                 /* stop */
464                 screen_cmd(c, CMD_STOP);
465                 return 1;
466         }
468         selected = lw->start + row;
470         if (bstate & BUTTON1_CLICKED) {
471                 /* play */
472                 if (lw->start + row < playlist_length(playlist))
473                         mpdclient_cmd_play(c, lw->start + row);
474         } else if (bstate & BUTTON3_CLICKED) {
475                 /* delete */
476                 if (selected == lw->selected)
477                         mpdclient_cmd_delete(c, lw->selected);
478         }
480         lw->selected = selected;
481         list_window_check_selected(lw, playlist_length(playlist));
482         playlist_repaint();
484         return 1;
486 #endif
488 static int
489 play_cmd(mpdclient_t *c, command_t cmd)
491         lw->flags &= ~LW_HIDE_CURSOR;
493         if (options.hide_cursor > 0) {
494                 if (timer_hide_cursor_id != 0)
495                         g_source_remove(timer_hide_cursor_id);
496                 timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
497                                                      timer_hide_cursor, c);
498         }
500         if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
501                 playlist_repaint();
502                 return 1;
503         }
505         switch(cmd) {
506         case CMD_PLAY:
507                 mpdclient_cmd_play(c, lw->selected);
508                 return 1;
509         case CMD_DELETE:
510                 mpdclient_cmd_delete(c, lw->selected);
511                 return 1;
512         case CMD_SAVE_PLAYLIST:
513                 playlist_save(c, NULL, NULL);
514                 return 1;
515         case CMD_ADD:
516                 handle_add_to_playlist(c);
517                 return 1;
518         case CMD_SCREEN_UPDATE:
519                 center_playing_item(c);
520                 playlist_repaint();
521                 return 0;
523         case CMD_LIST_MOVE_UP:
524                 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
525                 return 1;
526         case CMD_LIST_MOVE_DOWN:
527                 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
528                 return 1;
529         case CMD_LIST_FIND:
530         case CMD_LIST_RFIND:
531         case CMD_LIST_FIND_NEXT:
532         case CMD_LIST_RFIND_NEXT:
533                 screen_find(lw, playlist_length(&c->playlist),
534                             cmd, list_callback, NULL);
535                 playlist_repaint();
536                 return 1;
538 #ifdef HAVE_GETMOUSE
539         case CMD_MOUSE_EVENT:
540                 return handle_mouse_event(c);
541 #endif
543 #ifdef ENABLE_LYRICS_SCREEN
544         case CMD_SCREEN_LYRICS:
545                 if (lw->selected < playlist_length(&c->playlist)) {
546                         screen_lyrics_switch(c, playlist_get(&c->playlist, lw->selected));
547                         return 1;
548                 }
550                 break;
551 #endif
553         default:
554                 break;
555         }
557         return 0;
560 const struct screen_functions screen_playlist = {
561         .init = play_init,
562         .exit = play_exit,
563         .open = play_open,
564         .close = play_close,
565         .resize = play_resize,
566         .paint = play_paint,
567         .update = play_update,
568         .cmd = play_cmd,
569         .get_title = play_title,
570 };