Code

b86c876de7f0e5959683b2c410f061c5b24ce0f6
[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;
118 int
119 playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
121   gchar *filename;
122   gint error;
123   GCompletion *gcmp;
124   GList *list = NULL;
126   void pre_completion_cb(GCompletion *gcmp, gchar *line)
127     {
128       if( list == NULL )
129         {
130           /* create completion list */
131           list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
132           g_completion_add_items(gcmp, list);
133         }
134     }
136   void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
137     {
138       if( g_list_length(items)>=1 )
139         {
140           screen_display_completion_list(screen, items);
141           lw->clear = 1;
142           lw->repaint = 1;
143         }
144     }
146   if( name==NULL )
147     {
148       /* initialize completion support */
149       gcmp = g_completion_new(NULL);
150       g_completion_set_compare(gcmp, strncmp);
151       wrln_pre_completion_callback = pre_completion_cb;
152       wrln_post_completion_callback = post_completion_cb;
154       /* query the user for a filename */
155       filename = screen_readln(screen->status_window.w,
156                                _("Save playlist as: "),
157                                defaultname,
158                                NULL,
159                                gcmp);                                  
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       if( filename )
167         filename=g_strstrip(filename);
168     }
169   else
170     {
171       filename=g_strdup(name);
172     }
173   if( filename==NULL || filename[0]=='\0' )
174     return -1;
175   /* send save command to mpd */
176   D("Saving playlist as \'%s \'...\n", filename);
177   if( (error=mpdclient_cmd_save_playlist(c, filename)) )
178     {
179       gint code = GET_ACK_ERROR_CODE(error);
181       if( code == MPD_ACK_ERROR_EXIST )
182         {
183           char *buf;
184           int key;
186           buf=g_strdup_printf(_("Replace %s [%s/%s] ? "), filename, YES, NO);
187           key = tolower(screen_getch(screen->status_window.w, buf));
188           g_free(buf);
189           if( key == YES[0] )
190             {
191               if( mpdclient_cmd_delete_playlist(c, filename) )
192                 {
193                   g_free(filename);
194                   return -1;
195                 }
196               error = playlist_save(screen, c, filename, NULL);
197               g_free(filename);
198               return error;
199             }     
200           screen_status_printf(_("Aborted!"));
201         }
202       g_free(filename);
203       return -1;
204     }
205   /* success */
206   screen_status_printf(_("Saved %s"), filename);
207   g_free(filename);
208   return 0;
211 static int
212 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
214   gchar *path;
215   GCompletion *gcmp;
216   GList *list = NULL;
217   GList *dir_list = NULL;
219   void add_dir(gchar *dir)
220     {
221       g_completion_remove_items(gcmp, list);
222       list = string_list_remove(list, dir);
223       list = gcmp_list_from_path(c, dir, list, GCMP_TYPE_RFILE);
224       g_completion_add_items(gcmp, list);
225       dir_list = g_list_append(dir_list, g_strdup(dir));
226     }
228   void pre_completion_cb(GCompletion *gcmp, gchar *line)        
229     {
230       D("pre_completion()...\n");
231       if( list == NULL )
232         {
233           /* create initial list */
234           list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
235           g_completion_add_items(gcmp, list);
236         }
237       else if( line && line[0] && line[strlen(line)-1]=='/' &&
238                string_list_find(dir_list, line) == NULL )
239         {         
240           /* add directory content to list */
241           add_dir(line);
242         }
243     }
245   void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
246     {
247       D("post_completion()...\n");
248       if( g_list_length(items)>=1 )
249         {
250           screen_display_completion_list(screen, items);
251           lw->clear = 1;
252           lw->repaint = 1;
253         }
255       if( line && line[0] && line[strlen(line)-1]=='/' &&
256           string_list_find(dir_list, line) == NULL )
257         {         
258           /* add directory content to list */
259           add_dir(line);
260         }
261     }
262     
263   /* initialize completion support */
264   gcmp = g_completion_new(NULL);
265   g_completion_set_compare(gcmp, strncmp);
266   wrln_pre_completion_callback = pre_completion_cb;
267   wrln_post_completion_callback = post_completion_cb;
268   /* get path */
269   path = screen_readln(screen->status_window.w, 
270                        _("Add: "),
271                        NULL, 
272                        NULL, 
273                        gcmp);
275   /* destroy completion data */
276   wrln_pre_completion_callback = NULL;
277   wrln_post_completion_callback = NULL;
278   g_completion_free(gcmp);
279   string_list_free(list);
280   string_list_free(dir_list);
282   /* add the path to the playlist */
283   if( path && path[0] )
284     mpdclient_cmd_add_path(c, path);
286   return 0;
289 static void
290 play_init(WINDOW *w, int cols, int rows)
292   lw = list_window_init(w, cols, rows);
295 static void
296 play_open(screen_t *screen, mpdclient_t *c)
298   static gboolean install_cb = TRUE;
300   if( install_cb )
301     {
302       mpdclient_install_playlist_callback(c, playlist_changed_callback);
303       install_cb = FALSE;
304     }
307 static void
308 play_resize(int cols, int rows)
310   lw->cols = cols;
311   lw->rows = rows;
315 static void
316 play_exit(void)
318   list_window_free(lw);
321 static char *
322 play_title(char *str, size_t size)
324   if( strcmp(options.host, "localhost") == 0 )
325     return _("Playlist");
326   
327   g_snprintf(str, size, _("Playlist on %s"), options.host);
329   return str;
332 static void
333 play_paint(screen_t *screen, mpdclient_t *c)
334
335   lw->clear = 1;
337   list_window_paint(lw, list_callback, (void *) c);
338   wnoutrefresh(lw->w);
341 static void
342 play_update(screen_t *screen, mpdclient_t *c)
344   if( options.auto_center )
345     {
346       static int prev_song_id = 0;
347       
348       if( c->song && prev_song_id != c->song->id )      
349         {
350           center_playing_item(screen, c);
351           prev_song_id = c->song->id;
352         }
353     }
355   if( c->playlist.updated )
356     {
357       if( lw->selected >= c->playlist.length )
358         lw->selected = c->playlist.length-1;
359       if( lw->start    >= c->playlist.length )
360         list_window_reset(lw);
362       play_paint(screen, c);
363       c->playlist.updated = FALSE;
364     }
365   else if( lw->repaint || 1)
366     {
367       list_window_paint(lw, list_callback, (void *) c);
368       wnoutrefresh(lw->w);
369       lw->repaint = 0;
370     }
373 #ifdef HAVE_GETMOUSE
374 static int
375 handle_mouse_event(screen_t *screen, mpdclient_t *c)
377   int row;
378   int selected;
379   unsigned long bstate;
381   if( screen_get_mouse_event(c, lw, c->playlist.length, &bstate, &row) )
382     return 1;
384   if( bstate & BUTTON1_DOUBLE_CLICKED )
385     {
386       /* stop */
387       screen_cmd(c, CMD_STOP);
388       return 1;
389     }
391   selected = lw->start+row;
393   if( bstate & BUTTON1_CLICKED )
394     {
395       /* play */
396       if( lw->start+row < c->playlist.length )
397         mpdclient_cmd_play(c, lw->start+row);
398     }
399   else if( bstate & BUTTON3_CLICKED )
400     {
401       /* delete */
402       if( selected == lw->selected )
403         mpdclient_cmd_delete(c, lw->selected);
404     }
405   lw->selected = selected;
406   list_window_check_selected(lw, c->playlist.length);
408   return 1;
410 #else
411 #define handle_mouse_event(s,c) (0)
412 #endif
414 static int
415 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
417   switch(cmd)
418     {
419     case CMD_PLAY:
420       mpdclient_cmd_play(c, lw->selected);
421       return 1;
422     case CMD_DELETE:
423       mpdclient_cmd_delete(c, lw->selected);
424       return 1;
425     case CMD_SAVE_PLAYLIST:
426       playlist_save(screen, c, NULL, NULL);
427       return 1;
428     case CMD_ADD:
429       handle_add_to_playlist(screen, c);
430       return 1;
431     case CMD_SCREEN_UPDATE:
432       screen->painted = 0;
433       lw->clear = 1;
434       lw->repaint = 1;
435       center_playing_item(screen, c);
436       return 1;
437     case CMD_LIST_MOVE_UP:
438       mpdclient_cmd_move(c, lw->selected, lw->selected-1);
439       return 1;
440     case CMD_LIST_MOVE_DOWN:
441       mpdclient_cmd_move(c, lw->selected, lw->selected+1);
442       return 1;
443     case CMD_LIST_FIND:
444     case CMD_LIST_RFIND:
445     case CMD_LIST_FIND_NEXT:
446     case CMD_LIST_RFIND_NEXT:
447       return screen_find(screen, c, 
448                          lw, c->playlist.length,
449                          cmd, list_callback, (void *) c);
450     case CMD_MOUSE_EVENT:
451       return handle_mouse_event(screen,c);
452     default:
453       break;
454     }
455   return list_window_cmd(lw, c->playlist.length, cmd) ;
460 static list_window_t *
461 play_lw(void)
463   return lw;
467 screen_functions_t *
468 get_screen_playlist(void)
470   static screen_functions_t functions;
472   memset(&functions, 0, sizeof(screen_functions_t));
473   functions.init   = play_init;
474   functions.exit   = play_exit;
475   functions.open   = play_open;
476   functions.close  = NULL;
477   functions.resize = play_resize;
478   functions.paint  = play_paint;
479   functions.update = play_update;
480   functions.cmd    = play_cmd;
481   functions.get_lw = play_lw;
482   functions.get_title = play_title;
484   return &functions;