Code

Added basic ncurses mouse support
[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 <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <ncurses.h>
26 #include <panel.h>
28 #include "config.h"
29 #include "ncmpc.h"
30 #include "options.h"
31 #include "support.h"
32 #include "mpdclient.h"
33 #include "utils.h"
34 #include "strfsong.h"
35 #include "wreadln.h"
36 #include "command.h"
37 #include "colors.h"
38 #include "screen.h"
39 #include "screen_utils.h"
41 #define MAX_SONG_LENGTH 512
43 static list_window_t *lw = NULL;
45 static void 
46 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
47 {
48   D("screen_play.c> playlist_callback() [%d]\n", event);
49   switch(event)
50     {
51     case PLAYLIST_EVENT_DELETE:
52       break;
53     case PLAYLIST_EVENT_MOVE:
54       lw->selected = *((int *) data);
55       if( lw->selected<lw->start )
56         lw->start--;
57       break;
58     default:
59       break;
60     }
61   /* make shure the playlist is repainted */
62   lw->clear = 1;
63   lw->repaint = 1;
64   list_window_check_selected(lw, c->playlist.length);
65 }
67 static char *
68 list_callback(int index, int *highlight, void *data)
69 {
70   static char songname[MAX_SONG_LENGTH];
71   mpdclient_t *c = (mpdclient_t *) data;
72   mpd_Song *song;
74   *highlight = 0;
75   if( (song=playlist_get_song(c, index)) == NULL )
76     {
77       return NULL;
78     }
80   if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) )
81     {
82       *highlight = 1;
83     }
84   strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
85   return songname;
86 }
88 static int
89 center_playing_item(screen_t *screen, mpdclient_t *c)
90 {
91   int length = c->playlist.length;
92   int offset = lw->selected-lw->start;
93   int index;
94   
95   if( !lw || !c->song || length<lw->rows || IS_STOPPED(c->status->state) )
96     return 0;
98   /* try to center the song that are playing */
99   index = playlist_get_index(c, c->song);
100   D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,index);
101   lw->start = index-(lw->rows/2);
102   if( lw->start+lw->rows > length )
103     lw->start = length-lw->rows;
104   if( lw->start<0 )
105     lw->start=0;
107   /* make sure the cursor is in the window */
108   lw->selected = lw->start+offset;
109   list_window_check_selected(lw, length);
111   lw->clear = 1;
112   lw->repaint = 1;
114   return 0;
117 static int
118 handle_save_playlist(screen_t *screen, mpdclient_t *c, char *name)
120   gchar *filename;
121   gint error;
122   GCompletion *gcmp;
123   GList *list = NULL;
125   void pre_completion_cb(GCompletion *gcmp, gchar *line)
126     {
127       if( list == NULL )
128         {
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   void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
136     {
137       if( g_list_length(items)>=1 )
138         {
139           screen_display_completion_list(screen, items);
140           lw->clear = 1;
141           lw->repaint = 1;
142         }
143     }
145   if( name==NULL )
146     {
147       /* initialize completion support */
148       gcmp = g_completion_new(NULL);
149       g_completion_set_compare(gcmp, strncmp);
150       wrln_pre_completion_callback = pre_completion_cb;
151       wrln_post_completion_callback = post_completion_cb;
153       /* query the user for a filename */
154       filename = screen_readln(screen->status_window.w,
155                                _("Save playlist as: "),
156                                NULL,
157                                NULL,
158                                gcmp);                                  
159       filename=g_strstrip(filename);
161       /* destroy completion support */
162       wrln_pre_completion_callback = NULL;
163       wrln_post_completion_callback = NULL;
164       g_completion_free(gcmp);
165       list = string_list_free(list);
166     }
167   else
168     {
169       filename=g_strdup(name);
170     }
171   if( filename==NULL || filename[0]=='\0' )
172     return -1;
173   /* send save command to mpd */
174   D("Saving playlist as \'%s \'...\n", filename);
175   if( (error=mpdclient_cmd_save_playlist(c, filename)) )
176     {
177       gint code = GET_ACK_ERROR_CODE(error);
179       if( code == MPD_ACK_ERROR_EXIST )
180         {
181           char buf[256];
182           int key;
184           snprintf(buf, 256, _("Replace %s [%s/%s] ? "), filename, YES, NO);
185           key = tolower(screen_getch(screen->status_window.w, buf));
186           if( key == YES[0] )
187             {
188               if( mpdclient_cmd_delete_playlist(c, filename) )
189                 {
190                   g_free(filename);
191                   return -1;
192                 }
193               error = handle_save_playlist(screen, c, filename);
194               g_free(filename);
195               return error;
196             }     
197           screen_status_printf(_("Aborted!"));
198         }
199       g_free(filename);
200       return -1;
201     }
202   /* success */
203   screen_status_printf(_("Saved %s"), filename);
204   g_free(filename);
205   return 0;
208 static int
209 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
211   gchar *path;
212   GCompletion *gcmp;
213   GList *list = NULL;
214   GList *dir_list = NULL;
216   void add_dir(gchar *dir)
217     {
218       g_completion_remove_items(gcmp, list);
219       list = string_list_remove(list, dir);
220       list = gcmp_list_from_path(c, dir, list, GCMP_TYPE_RFILE);
221       g_completion_add_items(gcmp, list);
222       dir_list = g_list_append(dir_list, g_strdup(dir));
223     }
225   void pre_completion_cb(GCompletion *gcmp, gchar *line)        
226     {
227       D("pre_completion()...\n");
228       if( list == NULL )
229         {
230           /* create initial list */
231           list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
232           g_completion_add_items(gcmp, list);
233         }
234       else if( line && line[0] && line[strlen(line)-1]=='/' &&
235                string_list_find(dir_list, line) == NULL )
236         {         
237           /* add directory content to list */
238           add_dir(line);
239         }
240     }
242   void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
243     {
244       D("post_completion()...\n");
245       if( g_list_length(items)>=1 )
246         {
247           screen_display_completion_list(screen, items);
248           lw->clear = 1;
249           lw->repaint = 1;
250         }
252       if( line && line[0] && line[strlen(line)-1]=='/' &&
253           string_list_find(dir_list, line) == NULL )
254         {         
255           /* add directory content to list */
256           add_dir(line);
257         }
258     }
259     
260   /* initialize completion support */
261   gcmp = g_completion_new(NULL);
262   g_completion_set_compare(gcmp, strncmp);
263   wrln_pre_completion_callback = pre_completion_cb;
264   wrln_post_completion_callback = post_completion_cb;
265   /* get path */
266   path = screen_readln(screen->status_window.w, 
267                        _("Add: "),
268                        NULL, 
269                        NULL, 
270                        gcmp);
272   /* destroy completion data */
273   wrln_pre_completion_callback = NULL;
274   wrln_post_completion_callback = NULL;
275   g_completion_free(gcmp);
276   string_list_free(list);
277   string_list_free(dir_list);
279   /* add the path to the playlist */
280   if( path && path[0] )
281     mpdclient_cmd_add_path(c, path);
283   return 0;
286 static void
287 play_init(WINDOW *w, int cols, int rows)
289   lw = list_window_init(w, cols, rows);
292 static void
293 play_open(screen_t *screen, mpdclient_t *c)
295   static gboolean install_cb = TRUE;
297   if( install_cb )
298     {
299       mpdclient_install_playlist_callback(c, playlist_changed_callback);
300       install_cb = FALSE;
301     }
304 static void
305 play_resize(int cols, int rows)
307   lw->cols = cols;
308   lw->rows = rows;
312 static void
313 play_exit(void)
315   list_window_free(lw);
318 static char *
319 play_title(char *str, size_t size)
321   if( strcmp(options.host, "localhost") == 0 )
322     return _("Playlist");
323   
324   snprintf(str, size, _("Playlist on %s"), options.host);
326   return str;
329 static void
330 play_paint(screen_t *screen, mpdclient_t *c)
331
332   lw->clear = 1;
334   list_window_paint(lw, list_callback, (void *) c);
335   wnoutrefresh(lw->w);
338 static void
339 play_update(screen_t *screen, mpdclient_t *c)
341   if( options.auto_center )
342     {
343       static int prev_song_id = 0;
344       
345       if( c->song && prev_song_id != c->song->id )      
346         {
347           center_playing_item(screen, c);
348           prev_song_id = c->song->id;
349         }
350     }
352   if( c->playlist.updated )
353     {
354       if( lw->selected >= c->playlist.length )
355         lw->selected = c->playlist.length-1;
356       if( lw->start    >= c->playlist.length )
357         list_window_reset(lw);
359       play_paint(screen, c);
360       c->playlist.updated = FALSE;
361     }
362   else if( lw->repaint || 1)
363     {
364       list_window_paint(lw, list_callback, (void *) c);
365       wnoutrefresh(lw->w);
366       lw->repaint = 0;
367     }
370 #ifdef HAVE_GETMOUSE
371 static int
372 handle_mouse_event(screen_t *screen, mpdclient_t *c)
374   int row;
375   int selected;
376   unsigned long bstate;
378   if( screen_get_mouse_event(c, lw, c->playlist.length, &bstate, &row) )
379     return 1;
381   if( bstate & BUTTON1_DOUBLE_CLICKED )
382     {
383       /* stop */
384       screen_cmd(c, CMD_STOP);
385       return 1;
386     }
388   selected = lw->start+row;
390   if( bstate & BUTTON1_CLICKED )
391     {
392       /* play */
393       if( lw->start+row < c->playlist.length )
394         mpdclient_cmd_play(c, lw->start+row);
395     }
396   else if( bstate & BUTTON3_CLICKED )
397     {
398       /* delete */
399       if( selected == lw->selected )
400         mpdclient_cmd_delete(c, lw->selected);
401     }
402   lw->selected = selected;
403   list_window_check_selected(lw, c->playlist.length);
405   return 1;
407 #else
408 #define handle_mouse_event(s,c) (0)
409 #endif
411 static int
412 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
414   switch(cmd)
415     {
416     case CMD_PLAY:
417       mpdclient_cmd_play(c, lw->selected);
418       return 1;
419     case CMD_DELETE:
420       mpdclient_cmd_delete(c, lw->selected);
421       return 1;
422     case CMD_SAVE_PLAYLIST:
423       handle_save_playlist(screen, c, NULL);
424       return 1;
425     case CMD_ADD:
426       handle_add_to_playlist(screen, c);
427       return 1;
428     case CMD_SCREEN_UPDATE:
429       screen->painted = 0;
430       lw->clear = 1;
431       lw->repaint = 1;
432       center_playing_item(screen, c);
433       return 1;
434     case CMD_LIST_MOVE_UP:
435       mpdclient_cmd_move(c, lw->selected, lw->selected-1);
436       return 1;
437     case CMD_LIST_MOVE_DOWN:
438       mpdclient_cmd_move(c, lw->selected, lw->selected+1);
439       return 1;
440     case CMD_LIST_FIND:
441     case CMD_LIST_RFIND:
442     case CMD_LIST_FIND_NEXT:
443     case CMD_LIST_RFIND_NEXT:
444       return screen_find(screen, c, 
445                          lw, c->playlist.length,
446                          cmd, list_callback);
447     case CMD_MOUSE_EVENT:
448       return handle_mouse_event(screen,c);
449     default:
450       break;
451     }
452   return list_window_cmd(lw, c->playlist.length, cmd) ;
457 static list_window_t *
458 play_lw(void)
460   return lw;
464 screen_functions_t *
465 get_screen_playlist(void)
467   static screen_functions_t functions;
469   memset(&functions, 0, sizeof(screen_functions_t));
470   functions.init   = play_init;
471   functions.exit   = play_exit;
472   functions.open   = play_open;
473   functions.close  = NULL;
474   functions.resize = play_resize;
475   functions.paint  = play_paint;
476   functions.update = play_update;
477   functions.cmd    = play_cmd;
478   functions.get_lw = play_lw;
479   functions.get_title = play_title;
481   return &functions;