Code

list_window: added list_window_center()
[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         if (idx < 0)
110                 return 0;
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 0;
121 static void
122 save_pre_completion_cb(GCompletion *gcmp, mpd_unused gchar *line, void *data)
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         }
135 static void
136 save_post_completion_cb(mpd_unused GCompletion *gcmp, mpd_unused gchar *line,
137                         GList *items, void *data)
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                 lw->repaint = 1;
146         }
149 int
150 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
152   gchar *filename;
153   gint error;
154   GCompletion *gcmp;
155   GList *list = NULL;
156   completion_callback_data_t data;
158   if( name==NULL )
159     {
160       /* initialize completion support */
161       gcmp = g_completion_new(NULL);
162       g_completion_set_compare(gcmp, strncmp);
163       data.list = &list;
164       data.dir_list = NULL;
165       data.screen = screen;
166       data.c = c;
167       wrln_completion_callback_data = &data;
168       wrln_pre_completion_callback = save_pre_completion_cb;
169       wrln_post_completion_callback = save_post_completion_cb;
172       /* query the user for a filename */
173       filename = screen_readln(screen->status_window.w,
174                                _("Save playlist as: "),
175                                defaultname,
176                                NULL,
177                                gcmp);                                  
179       /* destroy completion support */
180       wrln_completion_callback_data = NULL;
181       wrln_pre_completion_callback = NULL;
182       wrln_post_completion_callback = NULL;
183       g_completion_free(gcmp);
184       list = string_list_free(list);
185       if( filename )
186         filename=g_strstrip(filename);
187     }
188   else
189     {
190       filename=g_strdup(name);
191     }
192   if( filename==NULL || filename[0]=='\0' )
193     return -1;
194   /* send save command to mpd */
195   D("Saving playlist as \'%s \'...\n", filename);
196   if( (error=mpdclient_cmd_save_playlist(c, filename)) )
197     {
198       gint code = GET_ACK_ERROR_CODE(error);
200       if( code == MPD_ACK_ERROR_EXIST )
201         {
202           char *buf;
203           int key;
205           buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO);
206           key = tolower(screen_getch(screen->status_window.w, buf));
207           g_free(buf);
208           if( key == YES[0] )
209             {
210               if( mpdclient_cmd_delete_playlist(c, filename) )
211                 {
212                   g_free(filename);
213                   return -1;
214                 }
215               error = playlist_save(screen, c, filename, NULL);
216               g_free(filename);
217               return error;
218             }     
219           screen_status_printf(_("Aborted!"));
220         }
221       g_free(filename);
222       return -1;
223     }
224   /* success */
225   screen_status_printf(_("Saved %s"), filename);
226   g_free(filename);
227   return 0;
230 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
231                     GList **list, mpdclient_t *c)
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));
240 static void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
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     {
250       /* create initial list */
251       *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
252       g_completion_add_items(gcmp, *list);
253     }
254   else if( line && line[0] && line[strlen(line)-1]=='/' &&
255            string_list_find(*dir_list, line) == NULL )
256     {     
257       /* add directory content to list */
258       add_dir(gcmp, line, dir_list, list, c);
259     }
262 static void add_post_completion_cb(GCompletion *gcmp, gchar *line,
263                                    GList *items, void *data)
265   completion_callback_data_t *tmp = (completion_callback_data_t *)data;
266   GList **dir_list = tmp->dir_list;
267   GList **list = tmp->list;
268   mpdclient_t *c = tmp->c;
269   screen_t *screen = tmp->screen;
271   D("post_completion()...\n");
272   if( g_list_length(items)>=1 )
273     {
274       screen_display_completion_list(screen, items);
275       lw->clear = 1;
276       lw->repaint = 1;
277     }
279   if( line && line[0] && line[strlen(line)-1]=='/' &&
280       string_list_find(*dir_list, line) == NULL )
281     {     
282       /* add directory content to list */
283       add_dir(gcmp, line, dir_list, list, c);
284     }
287 static int
288 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
290   gchar *path;
291   GCompletion *gcmp;
292   GList *list = NULL;
293   GList *dir_list = NULL;
294   completion_callback_data_t data;
295     
296   /* initialize completion support */
297   gcmp = g_completion_new(NULL);
298   g_completion_set_compare(gcmp, strncmp);
299   data.list = &list;
300   data.dir_list = &dir_list;
301   data.screen = screen;
302   data.c = c;
303   wrln_completion_callback_data = &data;
304   wrln_pre_completion_callback = add_pre_completion_cb;
305   wrln_post_completion_callback = add_post_completion_cb;
306   /* get path */
307   path = screen_readln(screen->status_window.w, 
308                        _("Add: "),
309                        NULL, 
310                        NULL, 
311                        gcmp);
313   /* destroy completion data */
314   wrln_completion_callback_data = NULL;
315   wrln_pre_completion_callback = NULL;
316   wrln_post_completion_callback = NULL;
317   g_completion_free(gcmp);
318   string_list_free(list);
319   string_list_free(dir_list);
321   /* add the path to the playlist */
322   if( path && path[0] )
323     mpdclient_cmd_add_path(c, path);
325   return 0;
328 static void
329 play_init(WINDOW *w, int cols, int rows)
331         lw = list_window_init(w, cols, rows);
334 static void
335 play_open(mpd_unused screen_t *screen, mpdclient_t *c)
337         static gboolean install_cb = TRUE;
339         if (install_cb) {
340                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
341                 install_cb = FALSE;
342         }
345 static void
346 play_resize(int cols, int rows)
348         lw->cols = cols;
349         lw->rows = rows;
353 static void
354 play_exit(void)
356         list_window_free(lw);
359 static const char *
360 play_title(char *str, size_t size)
362         if( strcmp(options.host, "localhost") == 0 )
363                 return _("Playlist");
365         g_snprintf(str, size, _("Playlist on %s"), options.host);
366         return str;
369 static void
370 play_paint(mpd_unused screen_t *screen, mpdclient_t *c)
372         lw->clear = 1;
374         list_window_paint(lw, list_callback, (void *) c);
375         wnoutrefresh(lw->w);
378 static void
379 play_update(screen_t *screen, mpdclient_t *c)
381         /* hide the cursor when mpd are playing and the user are inactive */
382         if( options.hide_cursor>0 && c->status->state == MPD_STATUS_STATE_PLAY &&
383             time(NULL)-screen->input_timestamp >= options.hide_cursor ) {
384                 lw->flags |= LW_HIDE_CURSOR;
385         } else {
386                 lw->flags &= ~LW_HIDE_CURSOR;
387         }
389         /* center the cursor */
390         if( options.auto_center ) {
391                 static int prev_song_id = 0;
393                 if( c->song && prev_song_id != c->song->id ) {
394                         center_playing_item(c);
395                         prev_song_id = c->song->id;
396                 }
397         }
399         if( c->playlist.updated ) {
400                 if (lw->selected >= c->playlist.list->len)
401                         lw->selected = c->playlist.list->len - 1;
402                 if (lw->start >= c->playlist.list->len)
403                         list_window_reset(lw);
405                 play_paint(screen, c);
406                 c->playlist.updated = FALSE;
407         } else if( lw->repaint || 1) {
408                 list_window_paint(lw, list_callback, (void *) c);
409                 wnoutrefresh(lw->w);
410                 lw->repaint = 0;
411         }
414 #ifdef HAVE_GETMOUSE
415 static int
416 handle_mouse_event(mpd_unused screen_t *screen, mpdclient_t *c)
418         int row;
419         unsigned selected;
420         unsigned long bstate;
422         if (screen_get_mouse_event(c, lw, c->playlist.list->len, &bstate, &row))
423                 return 1;
425         if (bstate & BUTTON1_DOUBLE_CLICKED) {
426                 /* stop */
427                 screen_cmd(c, CMD_STOP);
428                 return 1;
429         }
431         selected = lw->start + row;
433         if (bstate & BUTTON1_CLICKED) {
434                 /* play */
435                 if (lw->start + row < c->playlist.list->len)
436                         mpdclient_cmd_play(c, lw->start + row);
437         } else if (bstate & BUTTON3_CLICKED) {
438                 /* delete */
439                 if (selected == lw->selected)
440                         mpdclient_cmd_delete(c, lw->selected);
441         }
443         lw->selected = selected;
444         list_window_check_selected(lw, c->playlist.list->len);
446         return 1;
448 #else
449 #define handle_mouse_event(s,c) (0)
450 #endif
452 static int
453 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
455         switch(cmd) {
456         case CMD_PLAY:
457                 mpdclient_cmd_play(c, lw->selected);
458                 return 1;
459         case CMD_DELETE:
460                 mpdclient_cmd_delete(c, lw->selected);
461                 return 1;
462         case CMD_SAVE_PLAYLIST:
463                 playlist_save(screen, c, NULL, NULL);
464                 return 1;
465         case CMD_ADD:
466                 handle_add_to_playlist(screen, c);
467                 return 1;
468         case CMD_SCREEN_UPDATE:
469                 screen->painted = 0;
470                 lw->clear = 1;
471                 lw->repaint = 1;
472                 center_playing_item(c);
473                 return 1;
474         case CMD_LIST_MOVE_UP:
475                 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
476                 return 1;
477         case CMD_LIST_MOVE_DOWN:
478                 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
479                 return 1;
480         case CMD_LIST_FIND:
481         case CMD_LIST_RFIND:
482         case CMD_LIST_FIND_NEXT:
483         case CMD_LIST_RFIND_NEXT:
484                 return screen_find(screen,
485                                    lw, c->playlist.list->len,
486                                    cmd, list_callback, (void *) c);
487         case CMD_MOUSE_EVENT:
488                 return handle_mouse_event(screen,c);
489         default:
490                 break;
491         }
492         return list_window_cmd(lw, c->playlist.list->len, cmd);
495 const struct screen_functions screen_playlist = {
496         .init = play_init,
497         .exit = play_exit,
498         .open = play_open,
499         .close = NULL,
500         .resize = play_resize,
501         .paint = play_paint,
502         .update = play_update,
503         .cmd = play_cmd,
504         .get_title = play_title,
505 };