Code

32c08ea73a21ede201215cf8fd2e8635bc7bdf68
[ncmpc.git] / src / screen_play.c
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"
34 #include "gcc.h"
36 #include <ctype.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 #include <glib.h>
41 #include <ncurses.h>
43 #define MAX_SONG_LENGTH 512
45 typedef struct
46 {
47   GList **list;
48   GList **dir_list;
49   screen_t *screen;
50   mpdclient_t *c;
51 } completion_callback_data_t;
53 static list_window_t *lw = NULL;
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         lw->repaint = 1;
73         list_window_check_selected(lw, c->playlist.list->len);
74 }
76 static const char *
77 list_callback(unsigned idx, int *highlight, void *data)
78 {
79         static char songname[MAX_SONG_LENGTH];
80         mpdclient_t *c = (mpdclient_t *) data;
81         mpd_Song *song;
83         *highlight = 0;
84         if( (song=playlist_get_song(c, idx)) == NULL ) {
85                 return NULL;
86         }
88         if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) ) {
89                 *highlight = 1;
90         }
91         strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
92         return songname;
93 }
95 static int
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 (!lw || !c->song || length<lw->rows ||
103             IS_STOPPED(c->status->state))
104                 return 0;
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         idx -= (lw->rows / 2);
110         if (idx + (int)lw->rows > (int)length)
111                 idx = length - lw->rows;
112         if (idx < 0)
113                 idx = 0;
114         lw->start = idx;
116         /* make sure the cursor is in the window */
117         lw->selected = lw->start+offset;
118         list_window_check_selected(lw, length);
120         lw->clear = 1;
121         lw->repaint = 1;
123         return 0;
126 static void
127 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
129         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
130         GList **list = tmp->list;
131         mpdclient_t *c = tmp->c;
133         if( *list == NULL ) {
134                 /* create completion list */
135                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
136                 g_completion_add_items(gcmp, *list);
137         }
140 static void
141 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
142                         GList *items, void *data)
144         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
145         screen_t *screen = tmp->screen;
147         if( g_list_length(items)>=1 ) {
148                 screen_display_completion_list(screen, items);
149                 lw->clear = 1;
150                 lw->repaint = 1;
151         }
154 int
155 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
157   gchar *filename;
158   gint error;
159   GCompletion *gcmp;
160   GList *list = NULL;
161   completion_callback_data_t data;
163   if( name==NULL )
164     {
165       /* initialize completion support */
166       gcmp = g_completion_new(NULL);
167       g_completion_set_compare(gcmp, strncmp);
168       data.list = &list;
169       data.dir_list = NULL;
170       data.screen = screen;
171       data.c = c;
172       wrln_completion_callback_data = &data;
173       wrln_pre_completion_callback = save_pre_completion_cb;
174       wrln_post_completion_callback = save_post_completion_cb;
177       /* query the user for a filename */
178       filename = screen_readln(screen->status_window.w,
179                                _("Save playlist as: "),
180                                defaultname,
181                                NULL,
182                                gcmp);                                  
184       /* destroy completion support */
185       wrln_completion_callback_data = NULL;
186       wrln_pre_completion_callback = NULL;
187       wrln_post_completion_callback = NULL;
188       g_completion_free(gcmp);
189       list = string_list_free(list);
190       if( filename )
191         filename=g_strstrip(filename);
192     }
193   else
194     {
195       filename=g_strdup(name);
196     }
197   if( filename==NULL || filename[0]=='\0' )
198     return -1;
199   /* send save command to mpd */
200   D("Saving playlist as \'%s \'...\n", filename);
201   if( (error=mpdclient_cmd_save_playlist(c, filename)) )
202     {
203       gint code = GET_ACK_ERROR_CODE(error);
205       if( code == MPD_ACK_ERROR_EXIST )
206         {
207           char *buf;
208           int key;
210           buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO);
211           key = tolower(screen_getch(screen->status_window.w, buf));
212           g_free(buf);
213           if( key == YES[0] )
214             {
215               if( mpdclient_cmd_delete_playlist(c, filename) )
216                 {
217                   g_free(filename);
218                   return -1;
219                 }
220               error = playlist_save(screen, c, filename, NULL);
221               g_free(filename);
222               return error;
223             }     
224           screen_status_printf(_("Aborted!"));
225         }
226       g_free(filename);
227       return -1;
228     }
229   /* success */
230   screen_status_printf(_("Saved %s"), filename);
231   g_free(filename);
232   return 0;
235 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
236                     GList **list, mpdclient_t *c)
238   g_completion_remove_items(gcmp, *list);
239   *list = string_list_remove(*list, dir);
240   *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
241   g_completion_add_items(gcmp, *list);
242   *dir_list = g_list_append(*dir_list, g_strdup(dir));
245 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
247   completion_callback_data_t *tmp = (completion_callback_data_t *)data;
248   GList **dir_list = tmp->dir_list;
249   GList **list = tmp->list;
250   mpdclient_t *c = tmp->c;
252   D("pre_completion()...\n");
253   if( *list == NULL )
254     {
255       /* create initial list */
256       *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
257       g_completion_add_items(gcmp, *list);
258     }
259   else if( line && line[0] && line[strlen(line)-1]=='/' &&
260            string_list_find(*dir_list, line) == NULL )
261     {     
262       /* add directory content to list */
263       add_dir(gcmp, line, dir_list, list, c);
264     }
267 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
268                                    GList *items, void *data)
270   completion_callback_data_t *tmp = (completion_callback_data_t *)data;
271   GList **dir_list = tmp->dir_list;
272   GList **list = tmp->list;
273   mpdclient_t *c = tmp->c;
274   screen_t *screen = tmp->screen;
276   D("post_completion()...\n");
277   if( g_list_length(items)>=1 )
278     {
279       screen_display_completion_list(screen, items);
280       lw->clear = 1;
281       lw->repaint = 1;
282     }
284   if( line && line[0] && line[strlen(line)-1]=='/' &&
285       string_list_find(*dir_list, line) == NULL )
286     {     
287       /* add directory content to list */
288       add_dir(gcmp, line, dir_list, list, c);
289     }
292 static int
293 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
295   gchar *path;
296   GCompletion *gcmp;
297   GList *list = NULL;
298   GList *dir_list = NULL;
299   completion_callback_data_t data;
300     
301   /* initialize completion support */
302   gcmp = g_completion_new(NULL);
303   g_completion_set_compare(gcmp, strncmp);
304   data.list = &list;
305   data.dir_list = &dir_list;
306   data.screen = screen;
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   /* get path */
312   path = screen_readln(screen->status_window.w, 
313                        _("Add: "),
314                        NULL, 
315                        NULL, 
316                        gcmp);
318   /* destroy completion data */
319   wrln_completion_callback_data = NULL;
320   wrln_pre_completion_callback = NULL;
321   wrln_post_completion_callback = NULL;
322   g_completion_free(gcmp);
323   string_list_free(list);
324   string_list_free(dir_list);
326   /* add the path to the playlist */
327   if( path && path[0] )
328     mpdclient_cmd_add_path(c, path);
330   return 0;
333 static void
334 play_init(WINDOW *w, int cols, int rows)
336         lw = list_window_init(w, cols, rows);
339 static void
340 play_open(mpd_unused screen_t *screen, mpdclient_t *c)
342         static gboolean install_cb = TRUE;
344         if (install_cb) {
345                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
346                 install_cb = FALSE;
347         }
350 static void
351 play_resize(int cols, int rows)
353         lw->cols = cols;
354         lw->rows = rows;
358 static void
359 play_exit(void)
361         list_window_free(lw);
364 static const char *
365 play_title(char *str, size_t size)
367         if( strcmp(options.host, "localhost") == 0 )
368                 return _("Playlist");
370         g_snprintf(str, size, _("Playlist on %s"), options.host);
371         return str;
374 static void
375 play_paint(mpd_unused screen_t *screen, mpdclient_t *c)
377         lw->clear = 1;
379         list_window_paint(lw, list_callback, (void *) c);
380         wnoutrefresh(lw->w);
383 static void
384 play_update(screen_t *screen, mpdclient_t *c)
386         /* hide the cursor when mpd are playing and the user are inactive */
387         if( options.hide_cursor>0 && c->status->state == MPD_STATUS_STATE_PLAY &&
388             time(NULL)-screen->input_timestamp >= options.hide_cursor ) {
389                 lw->flags |= LW_HIDE_CURSOR;
390         } else {
391                 lw->flags &= ~LW_HIDE_CURSOR;
392         }
394         /* center the cursor */
395         if( options.auto_center ) {
396                 static int prev_song_id = 0;
398                 if( c->song && prev_song_id != c->song->id ) {
399                         center_playing_item(c);
400                         prev_song_id = c->song->id;
401                 }
402         }
404         if( c->playlist.updated ) {
405                 if (lw->selected >= c->playlist.list->len)
406                         lw->selected = c->playlist.list->len - 1;
407                 if (lw->start >= c->playlist.list->len)
408                         list_window_reset(lw);
410                 play_paint(screen, c);
411                 c->playlist.updated = FALSE;
412         } else if( lw->repaint || 1) {
413                 list_window_paint(lw, list_callback, (void *) c);
414                 wnoutrefresh(lw->w);
415                 lw->repaint = 0;
416         }
419 #ifdef HAVE_GETMOUSE
420 static int
421 handle_mouse_event(mpd_unused screen_t *screen, mpdclient_t *c)
423         int row;
424         unsigned selected;
425         unsigned long bstate;
427         if (screen_get_mouse_event(c, lw, c->playlist.list->len, &bstate, &row))
428                 return 1;
430         if (bstate & BUTTON1_DOUBLE_CLICKED) {
431                 /* stop */
432                 screen_cmd(c, CMD_STOP);
433                 return 1;
434         }
436         selected = lw->start + row;
438         if (bstate & BUTTON1_CLICKED) {
439                 /* play */
440                 if (lw->start + row < c->playlist.list->len)
441                         mpdclient_cmd_play(c, lw->start + row);
442         } else if (bstate & BUTTON3_CLICKED) {
443                 /* delete */
444                 if (selected == lw->selected)
445                         mpdclient_cmd_delete(c, lw->selected);
446         }
448         lw->selected = selected;
449         list_window_check_selected(lw, c->playlist.list->len);
451         return 1;
453 #else
454 #define handle_mouse_event(s,c) (0)
455 #endif
457 static int
458 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
460         switch(cmd) {
461         case CMD_PLAY:
462                 mpdclient_cmd_play(c, lw->selected);
463                 return 1;
464         case CMD_DELETE:
465                 mpdclient_cmd_delete(c, lw->selected);
466                 return 1;
467         case CMD_SAVE_PLAYLIST:
468                 playlist_save(screen, c, NULL, NULL);
469                 return 1;
470         case CMD_ADD:
471                 handle_add_to_playlist(screen, c);
472                 return 1;
473         case CMD_SCREEN_UPDATE:
474                 screen->painted = 0;
475                 lw->clear = 1;
476                 lw->repaint = 1;
477                 center_playing_item(c);
478                 return 1;
479         case CMD_LIST_MOVE_UP:
480                 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
481                 return 1;
482         case CMD_LIST_MOVE_DOWN:
483                 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
484                 return 1;
485         case CMD_LIST_FIND:
486         case CMD_LIST_RFIND:
487         case CMD_LIST_FIND_NEXT:
488         case CMD_LIST_RFIND_NEXT:
489                 return screen_find(screen,
490                                    lw, c->playlist.list->len,
491                                    cmd, list_callback, (void *) c);
492         case CMD_MOUSE_EVENT:
493                 return handle_mouse_event(screen,c);
494         default:
495                 break;
496         }
497         return list_window_cmd(lw, c->playlist.list->len, cmd);
500 static list_window_t *
501 play_lw(void)
503   return lw;
506 screen_functions_t *
507 get_screen_playlist(void)
509   static screen_functions_t functions;
511   memset(&functions, 0, sizeof(screen_functions_t));
512   functions.init   = play_init;
513   functions.exit   = play_exit;
514   functions.open   = play_open;
515   functions.close  = NULL;
516   functions.resize = play_resize;
517   functions.paint  = play_paint;
518   functions.update = play_update;
519   functions.cmd    = play_cmd;
520   functions.get_lw = play_lw;
521   functions.get_title = play_title;
523   return &functions;