]> git.sesse.net Git - vlc/blob - modules/gui/ncurses.c
* modules/gui/ncurses.c: use <space> in the filebrowser to add the selected director...
[vlc] / modules / gui / ncurses.c
1 /*****************************************************************************
2  * ncurses.c : NCurses plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Yoann Peronneau <yoann@videolan.org>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                      /* malloc(), free() */
31 #include <string.h>
32 #include <errno.h>                                                 /* ENOMEM */
33 #include <stdio.h>
34 #include <time.h>
35
36 #include <curses.h>
37
38 #include <vlc/vlc.h>
39 #include <vlc/intf.h>
40 #include <vlc/vout.h>
41 #include <vlc/aout.h>
42
43 #ifdef HAVE_SYS_STAT_H
44 #   include <sys/stat.h>
45 #endif
46 #if (!defined( WIN32 ) || defined(__MINGW32__))
47 /* Mingw has its own version of dirent */
48 #   include <dirent.h>
49 #endif
50
51 #ifdef HAVE_CDDAX
52 #define CDDA_MRL "cddax://"
53 #else
54 #define CDDA_MRL "cdda://"
55 #endif
56
57 #ifdef HAVE_VCDX
58 #define VCD_MRL "vcdx://"
59 #else
60 #define VCD_MRL "vcdx://"
61 #endif
62
63 #define SEARCH_CHAIN_SIZE 20
64 #define OPEN_CHAIN_SIZE 50
65
66 /*****************************************************************************
67  * Local prototypes.
68  *****************************************************************************/
69 static int  Open           ( vlc_object_t * );
70 static void Close          ( vlc_object_t * );
71
72 static void Run            ( intf_thread_t * );
73 static void PlayPause      ( intf_thread_t * );
74 static void Eject          ( intf_thread_t * );
75
76 static int  HandleKey      ( intf_thread_t *, int );
77 static void Redraw         ( intf_thread_t *, time_t * );
78 static void PlaylistRebuild( intf_thread_t * );
79 static void PlaylistAddNode( intf_thread_t *, playlist_item_t *, int, char *);
80 static void PlaylistDestroy( intf_thread_t * );
81 static int  PlaylistChanged( vlc_object_t *, const char *, vlc_value_t,
82                              vlc_value_t, void * );
83 static void FindIndex      ( intf_thread_t * );
84 static void SearchPlaylist ( intf_thread_t *, char * );
85 static int  SubSearchPlaylist( intf_thread_t *, char *, int, int );
86 static void ManageSlider   ( intf_thread_t * );
87 static void ReadDir        ( intf_thread_t * );
88
89 /*****************************************************************************
90  * Module descriptor
91  *****************************************************************************/
92
93 #define BROWSE_TEXT N_("Filebrowser starting point")
94 #define BROWSE_LONGTEXT N_( \
95     "This option allows you to specify the directory the ncurses filebrowser " \
96     "will show you initially.")
97
98 vlc_module_begin();
99     set_shortname( "Ncurses" );
100     set_description( _("Ncurses interface") );
101     set_capability( "interface", 10 );
102     set_category( CAT_INTERFACE );
103     set_subcategory( SUBCAT_INTERFACE_GENERAL );
104     set_callbacks( Open, Close );
105     add_shortcut( "curses" );
106     add_directory( "browse-dir", NULL, NULL, BROWSE_TEXT, BROWSE_LONGTEXT, VLC_FALSE );
107 vlc_module_end();
108
109 /*****************************************************************************
110  * intf_sys_t: description and status of ncurses interface
111  *****************************************************************************/
112 enum
113 {
114     BOX_NONE,
115     BOX_HELP,
116     BOX_INFO,
117     BOX_LOG,
118     BOX_PLAYLIST,
119     BOX_SEARCH,
120     BOX_OPEN,
121     BOX_BROWSE
122 };
123 struct dir_entry_t
124 {
125     vlc_bool_t  b_file;
126     char        *psz_path;
127 };
128 struct pl_item_t
129 {
130     playlist_item_t *p_item;
131     char            *psz_display;
132 };
133 struct intf_sys_t
134 {
135     playlist_t     *p_playlist;
136     input_thread_t *p_input;
137
138     float           f_slider;
139     float           f_slider_old;
140
141     WINDOW          *w;
142
143     int             i_box_type;
144     int             i_box_y;
145     int             i_box_lines;
146     int             i_box_lines_total;
147     int             i_box_start;
148
149     int             i_box_plidx;    /* Playlist index */
150     int             b_box_plidx_follow;
151     playlist_item_t *p_plnode;      /* Playlist node */
152     int             i_box_bidx;     /* browser index */
153
154     int             b_box_cleared;
155
156     msg_subscription_t* p_sub;                  /* message bank subscription */
157
158     char            *psz_search_chain;          /* for playlist searching    */
159     char            *psz_old_search;            /* for searching next        */
160     int             i_before_search;
161
162     char            *psz_open_chain;
163
164     char            *psz_current_dir;
165     int             i_dir_entries;
166     struct dir_entry_t  **pp_dir_entries;
167
168     int             i_current_view;             /* playlist view             */
169     struct pl_item_t    **pp_plist;
170     int             i_plist_entries;
171     vlc_bool_t      b_need_update;              /* for playlist view */
172 };
173
174 static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title );
175 static void DrawLine( WINDOW *win, int y, int x, int w );
176 static void DrawEmptyLine( WINDOW *win, int y, int x, int w );
177
178 /*****************************************************************************
179  * Open: initialize and create window
180  *****************************************************************************/
181 static int Open( vlc_object_t *p_this )
182 {
183     intf_thread_t *p_intf = (intf_thread_t *)p_this;
184     intf_sys_t    *p_sys;
185     vlc_value_t    val;
186
187     /* Allocate instance and initialize some members */
188     p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
189     p_sys->p_playlist = NULL;
190     p_sys->p_input = NULL;
191     p_sys->f_slider = 0.0;
192     p_sys->f_slider_old = 0.0;
193     p_sys->i_box_type = BOX_PLAYLIST;
194     p_sys->i_box_lines = 0;
195     p_sys->i_box_start= 0;
196     p_sys->i_box_lines_total = 0;
197     p_sys->b_box_plidx_follow = VLC_TRUE;
198     p_sys->b_box_cleared = VLC_FALSE;
199     p_sys->i_box_plidx = 0;
200     p_sys->p_plnode = NULL;
201     p_sys->i_box_bidx = 0;
202     p_sys->p_sub = msg_Subscribe( p_intf );
203
204     /* Initialize the curses library */
205     p_sys->w = initscr();
206     keypad( p_sys->w, TRUE );
207     /* Don't do NL -> CR/NL */
208     nonl();
209     /* Take input chars one at a time */
210     cbreak();
211     /* Don't echo */
212     noecho();
213
214     curs_set(0);
215     timeout(0);
216
217     clear();
218
219     /* exported function */
220     p_intf->pf_run = Run;
221
222     /* Set quiet mode */
223     val.i_int = -1;
224     var_Set( p_intf->p_vlc, "verbose", val );
225
226     /* Set defaul playlist view */
227     p_sys->i_current_view = VIEW_CATEGORY;
228     p_sys->pp_plist = NULL;
229     p_sys->i_plist_entries = 0;
230     p_sys->b_need_update = VLC_FALSE;
231
232     /* Initialize search chain */
233     p_sys->psz_search_chain = (char *)malloc( SEARCH_CHAIN_SIZE + 1 );
234     p_sys->psz_old_search = NULL;
235     p_sys->i_before_search = 0;
236
237     /* Initialize open chain */
238     p_sys->psz_open_chain = (char *)malloc( OPEN_CHAIN_SIZE + 1 );
239
240     /* Initialize browser options */
241     var_Create( p_intf, "browse-dir", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
242     var_Get( p_intf, "browse-dir", &val);
243
244     if( val.psz_string && *val.psz_string )
245     {
246         p_sys->psz_current_dir = strdup( val.psz_string);
247         free( val.psz_string );
248     }
249     else
250     {
251         p_sys->psz_current_dir = strdup( p_intf->p_vlc->psz_homedir );
252     }
253
254     p_sys->i_dir_entries = 0;
255     p_sys->pp_dir_entries = NULL;
256     ReadDir( p_intf );
257
258     return VLC_SUCCESS;
259 }
260
261 /*****************************************************************************
262  * Close: destroy interface window
263  *****************************************************************************/
264 static void Close( vlc_object_t *p_this )
265 {
266     intf_thread_t *p_intf = (intf_thread_t *)p_this;
267     intf_sys_t    *p_sys = p_intf->p_sys;
268     int i;
269
270     var_DelCallback( p_sys->p_playlist, "intf-change", PlaylistChanged,
271                      p_intf );
272     var_DelCallback( p_sys->p_playlist, "item-append", PlaylistChanged,
273                      p_intf );
274
275     PlaylistDestroy( p_intf );
276
277     for( i = 0; i < p_sys->i_dir_entries; i++ )
278     {
279         struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
280         if( p_dir_entry->psz_path ) free( p_dir_entry->psz_path );
281         REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
282         if( p_dir_entry ) free( p_dir_entry );
283     }
284     p_sys->pp_dir_entries = NULL;
285
286     if( p_sys->psz_current_dir ) free( p_sys->psz_current_dir );
287     if( p_sys->psz_search_chain ) free( p_sys->psz_search_chain );
288     if( p_sys->psz_old_search ) free( p_sys->psz_old_search );
289     if( p_sys->psz_open_chain ) free( p_sys->psz_open_chain );
290
291     if( p_sys->p_input )
292     {
293         vlc_object_release( p_sys->p_input );
294     }
295     if( p_sys->p_playlist )
296     {
297         vlc_object_release( p_sys->p_playlist );
298     }
299
300     /* Close the ncurses interface */
301     endwin();
302
303     msg_Unsubscribe( p_intf, p_sys->p_sub );
304
305     /* Destroy structure */
306     free( p_sys );
307 }
308
309 /*****************************************************************************
310  * Run: ncurses thread
311  *****************************************************************************/
312 static void Run( intf_thread_t *p_intf )
313 {
314     intf_sys_t    *p_sys = p_intf->p_sys;
315
316     int i_key;
317     time_t t_last_refresh;
318
319     /*
320      * force drawing the interface for the first time
321      */
322     t_last_refresh = ( time( 0 ) - 1);
323
324     while( !p_intf->b_die )
325     {
326         msleep( INTF_IDLE_SLEEP );
327
328         /* Update the input */
329         if( p_sys->p_playlist == NULL )
330         {
331             p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
332                                                  FIND_ANYWHERE );
333             if( p_sys->p_playlist )
334             {
335                 var_AddCallback( p_sys->p_playlist, "intf-change",
336                                  PlaylistChanged, p_intf );
337                 var_AddCallback( p_sys->p_playlist, "item-append",
338                                  PlaylistChanged, p_intf );
339             }
340         }
341         if( p_sys->p_playlist )
342         {
343             vlc_mutex_lock( &p_sys->p_playlist->object_lock );
344             if( p_sys->p_input == NULL )
345             {
346                 p_sys->p_input = p_sys->p_playlist->p_input;
347                 if( p_sys->p_input )
348                 {
349                     if( !p_sys->p_input->b_dead )
350                     {
351                         vlc_object_yield( p_sys->p_input );
352                     }
353                 }
354             }
355             else if( p_sys->p_input->b_dead )
356             {
357                 vlc_object_release( p_sys->p_input );
358                 p_sys->p_input = NULL;
359                 p_sys->f_slider = p_sys->f_slider_old = 0.0;
360                 p_sys->b_box_cleared = VLC_FALSE;
361             }
362             vlc_mutex_unlock( &p_sys->p_playlist->object_lock );
363         }
364
365         if( p_sys->b_box_plidx_follow && p_sys->p_playlist->i_index >= 0 )
366         {
367             FindIndex( p_intf );
368         }
369
370         while( ( i_key = getch()) != -1 )
371         {
372             /*
373              * HandleKey returns 1 if the screen needs to be redrawn
374              */
375             if( HandleKey( p_intf, i_key ) )
376             {
377                 Redraw( p_intf, &t_last_refresh );
378             }
379         }
380         /* Hack */
381         if( p_sys->f_slider > 0.0001 && !p_sys->b_box_cleared )
382         {
383             clear();
384             Redraw( p_intf, &t_last_refresh );
385             p_sys->b_box_cleared = VLC_TRUE;
386         }
387
388         /*
389          * redraw the screen every second
390          */
391         if( (time(0) - t_last_refresh) >= 1 )
392         {
393             ManageSlider( p_intf );
394             Redraw( p_intf, &t_last_refresh );
395         }
396     }
397 }
398
399 /* following functions are local */
400
401 static int HandleKey( intf_thread_t *p_intf, int i_key )
402 {
403     intf_sys_t *p_sys = p_intf->p_sys;
404     vlc_value_t val;
405
406     if( p_sys->i_box_type == BOX_PLAYLIST && p_sys->p_playlist )
407     {
408         int b_ret = VLC_TRUE;
409
410         switch( i_key )
411         {
412             vlc_value_t val;
413             /* Playlist Settings */
414             case 'r':
415                 var_Get( p_sys->p_playlist, "random", &val );
416                 val.b_bool = !val.b_bool;
417                 var_Set( p_sys->p_playlist, "random", val );
418                 return 1;
419             case 'l':
420                 var_Get( p_sys->p_playlist, "loop", &val );
421                 val.b_bool = !val.b_bool;
422                 var_Set( p_sys->p_playlist, "loop", val );
423                 return 1;
424             case 'R':
425                 var_Get( p_sys->p_playlist, "repeat", &val );
426                 val.b_bool = !val.b_bool;
427                 var_Set( p_sys->p_playlist, "repeat", val );
428                 return 1;
429
430             /* Playlist sort */
431             case 'o':
432                 playlist_Sort( p_sys->p_playlist, SORT_TITLE, ORDER_NORMAL );
433                 return 1;
434             case 'O':
435                 playlist_Sort( p_sys->p_playlist, SORT_TITLE, ORDER_REVERSE );
436                 return 1;
437
438             /* Playlist view */
439             case 'v':
440                 switch( p_sys->i_current_view )
441                 {
442                     case VIEW_CATEGORY:
443                         p_sys->i_current_view = VIEW_ALL;
444                         break;
445                     default:
446                         p_sys->i_current_view = VIEW_CATEGORY;
447                 }
448                 PlaylistRebuild( p_intf );
449                 FindIndex( p_intf );
450                 return 1;
451
452             /* Playlist navigation */
453             case KEY_HOME:
454                 p_sys->i_box_plidx = 0;
455                 break;
456             case KEY_END:
457                 p_sys->i_box_plidx = p_sys->p_playlist->i_size - 1;
458                 break;
459             case KEY_UP:
460                 p_sys->i_box_plidx--;
461                 break;
462             case KEY_DOWN:
463                 p_sys->i_box_plidx++;
464                 break;
465             case KEY_PPAGE:
466                 p_sys->i_box_plidx -= p_sys->i_box_lines;
467                 break;
468             case KEY_NPAGE:
469                 p_sys->i_box_plidx += p_sys->i_box_lines;
470                 break;
471             case 'D':
472             case KEY_BACKSPACE:
473             case KEY_DC:
474             {
475                 int i_item = p_sys->p_playlist->i_index;
476
477                 playlist_LockDelete( p_sys->p_playlist, p_sys->i_box_plidx );
478                 if( i_item < p_sys->p_playlist->i_size &&
479                     i_item != p_sys->p_playlist->i_index )
480                 {
481                     playlist_Goto( p_sys->p_playlist, i_item );
482                 }
483                 break;
484             }
485
486             case KEY_ENTER:
487             case 0x0d:
488                 if( p_sys->i_current_view == VIEW_ALL )
489                 {
490                     playlist_Goto( p_sys->p_playlist, p_sys->i_box_plidx );
491                 }
492                 else
493                 {
494                     if( p_sys->pp_plist[p_sys->i_box_plidx]->p_item->i_children
495                                 == -1 )
496                     {
497                         playlist_Control( p_sys->p_playlist, PLAYLIST_ITEMPLAY,
498                             p_sys->pp_plist[p_sys->i_box_plidx]->p_item );
499                     }
500                     else
501                     {
502                         playlist_Control( p_sys->p_playlist, PLAYLIST_VIEWPLAY,
503                             p_sys->i_current_view,
504                             p_sys->pp_plist[p_sys->i_box_plidx]->p_item,
505                             NULL );
506                     }
507                 }
508                 p_sys->b_box_plidx_follow = VLC_TRUE;
509                 break;
510             default:
511                 b_ret = VLC_FALSE;
512                 break;
513         }
514
515         if( b_ret )
516         {
517             int i_max = p_sys->i_plist_entries;
518             if( p_sys->i_current_view == VIEW_ALL )
519                 i_max = p_sys->p_playlist->i_size;
520             if( p_sys->i_box_plidx >= i_max ) p_sys->i_box_plidx = i_max - 1;
521             if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
522             if( p_sys->i_current_view == VIEW_ALL )
523             {
524                 if( p_sys->i_box_plidx == p_sys->p_playlist->i_index )
525                     p_sys->b_box_plidx_follow = VLC_TRUE;
526                 else
527                     p_sys->b_box_plidx_follow = VLC_FALSE;
528             }
529             else
530             {
531                 if( p_sys->pp_plist[p_sys->i_box_plidx]->p_item ==
532                         p_sys->p_playlist->status.p_item )
533                     p_sys->b_box_plidx_follow = VLC_TRUE;
534                 else
535                     p_sys->b_box_plidx_follow = VLC_FALSE;
536             }
537             return 1;
538         }
539     }
540     if( p_sys->i_box_type == BOX_BROWSE )
541     {
542         vlc_bool_t b_ret = VLC_TRUE;
543         /* Browser navigation */
544         switch( i_key )
545         {
546             case KEY_HOME:
547                 p_sys->i_box_bidx = 0;
548                 break;
549             case KEY_END:
550                 p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
551                 break;
552             case KEY_UP:
553                 p_sys->i_box_bidx--;
554                 break;
555             case KEY_DOWN:
556                 p_sys->i_box_bidx++;
557                 break;
558             case KEY_PPAGE:
559                 p_sys->i_box_bidx -= p_sys->i_box_lines;
560                 break;
561             case KEY_NPAGE:
562                 p_sys->i_box_bidx += p_sys->i_box_lines;
563                 break;
564
565             case KEY_ENTER:
566             case 0x0d:
567             case ' ':
568                 if( p_sys->pp_dir_entries[p_sys->i_box_bidx]->b_file || i_key == ' ' )
569                 {
570                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
571                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
572                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
573
574                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
575                     playlist_Add( p_sys->p_playlist, psz_uri,
576                                   psz_uri,
577                                   PLAYLIST_APPEND, PLAYLIST_END );
578                     p_sys->i_box_type = BOX_PLAYLIST;
579                     free( psz_uri );
580                 }
581                 else
582                 {
583                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
584                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
585                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
586
587                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
588
589                     p_sys->psz_current_dir = strdup( psz_uri );
590                     ReadDir( p_intf );
591                     free( psz_uri );
592                 }
593                 break;
594             default:
595                 b_ret = VLC_FALSE;
596                 break;
597         }
598         if( b_ret )
599         {
600             if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
601             if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
602             return 1;
603         }
604     }
605     else if( p_sys->i_box_type == BOX_HELP || p_sys->i_box_type == BOX_INFO )
606     {
607         switch( i_key )
608         {
609             case KEY_HOME:
610                 p_sys->i_box_start = 0;
611                 return 1;
612             case KEY_END:
613                 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
614                 return 1;
615             case KEY_UP:
616                 if( p_sys->i_box_start > 0 ) p_sys->i_box_start--;
617                 return 1;
618             case KEY_DOWN:
619                 if( p_sys->i_box_start < p_sys->i_box_lines_total - 1 )
620                 {
621                     p_sys->i_box_start++;
622                 }
623                 return 1;
624             case KEY_PPAGE:
625                 p_sys->i_box_start -= p_sys->i_box_lines;
626                 if( p_sys->i_box_start < 0 ) p_sys->i_box_start = 0;
627                 return 1;
628             case KEY_NPAGE:
629                 p_sys->i_box_start += p_sys->i_box_lines;
630                 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
631                 {
632                     p_sys->i_box_start = p_sys->i_box_lines_total - 1;
633                 }
634                 return 1;
635             default:
636                 break;
637         }
638     }
639     else if( p_sys->i_box_type == BOX_NONE )
640     {
641         switch( i_key )
642         {
643             case KEY_HOME:
644                 p_sys->f_slider = 0;
645                 ManageSlider( p_intf );
646                 return 1;
647             case KEY_END:
648                 p_sys->f_slider = 99.9;
649                 ManageSlider( p_intf );
650                 return 1;
651             case KEY_UP:
652                 p_sys->f_slider += 5.0;
653                 if( p_sys->f_slider >= 99.0 ) p_sys->f_slider = 99.0;
654                 ManageSlider( p_intf );
655                 return 1;
656             case KEY_DOWN:
657                 p_sys->f_slider -= 5.0;
658                 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
659                 ManageSlider( p_intf );
660                 return 1;
661
662             default:
663                 break;
664         }
665     }
666     else if( p_sys->i_box_type == BOX_SEARCH && p_sys->psz_search_chain )
667     {
668         int i_chain_len;
669         i_chain_len = strlen( p_sys->psz_search_chain );
670         switch( i_key )
671         {
672             case 0x0c:      /* ^l */
673                 clear();
674                 return 1;
675             case KEY_ENTER:
676             case 0x0d:
677                 if( i_chain_len > 0 )
678                 {
679                     p_sys->psz_old_search = strdup( p_sys->psz_search_chain );
680                 }
681                 else if( p_sys->psz_old_search )
682                 {
683                     SearchPlaylist( p_intf, p_sys->psz_old_search );
684                 }
685                 p_sys->i_box_type = BOX_PLAYLIST;
686                 return 1;
687             case 0x1b:      /* Esc. */
688                 p_sys->i_box_plidx = p_sys->i_before_search;
689                 p_sys->i_box_type = BOX_PLAYLIST;
690                 return 1;
691             case KEY_BACKSPACE:
692                 if( i_chain_len > 0 )
693                 {
694                     p_sys->psz_search_chain[ i_chain_len - 1 ] = '\0';
695                 }
696                 break;
697             default:
698                 if( i_chain_len < SEARCH_CHAIN_SIZE )
699                 {
700                     p_sys->psz_search_chain[ i_chain_len++ ] = i_key;
701                     p_sys->psz_search_chain[ i_chain_len ] = 0;
702                 }
703                 break;
704         }
705         if( p_sys->psz_old_search )
706         {
707             free( p_sys->psz_old_search );
708             p_sys->psz_old_search = NULL;
709         }
710         SearchPlaylist( p_intf, p_sys->psz_search_chain );
711         return 1;
712     }
713     else if( p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain )
714     {
715         int i_chain_len = strlen( p_sys->psz_open_chain );
716         playlist_t *p_playlist = p_sys->p_playlist;
717
718         switch( i_key )
719         {
720             case 0x0c:      /* ^l */
721                 clear();
722                 return 1;
723             case KEY_ENTER:
724             case 0x0d:
725                 if( p_playlist && i_chain_len > 0 )
726                 {
727                     playlist_Add( p_playlist, p_sys->psz_open_chain,
728                                   p_sys->psz_open_chain,
729                                   PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
730                     p_sys->b_box_plidx_follow = VLC_TRUE;
731                 }
732                 p_sys->i_box_type = BOX_PLAYLIST;
733                 return 1;
734             case 0x1b:      /* Esc. */
735                 p_sys->i_box_type = BOX_PLAYLIST;
736                 return 1;
737             case KEY_BACKSPACE:
738                 if( i_chain_len > 0 )
739                 {
740                     p_sys->psz_open_chain[ i_chain_len - 1 ] = '\0';
741                 }
742                 break;
743             default:
744                 if( i_chain_len < OPEN_CHAIN_SIZE )
745                 {
746                     p_sys->psz_open_chain[ i_chain_len++ ] = i_key;
747                     p_sys->psz_open_chain[ i_chain_len ] = 0;
748                 }
749                 break;
750         }
751         return 1;
752     }
753
754
755     /* Common keys */
756     switch( i_key )
757     {
758         case 'q':
759         case 'Q':
760         case 0x1b:  /* Esc */
761             p_intf->p_vlc->b_die = VLC_TRUE;
762             return 0;
763
764         /* Box switching */
765         case 'i':
766             if( p_sys->i_box_type == BOX_INFO )
767                 p_sys->i_box_type = BOX_NONE;
768             else
769                 p_sys->i_box_type = BOX_INFO;
770             p_sys->i_box_lines_total = 0;
771             return 1;
772         case 'L':
773             if( p_sys->i_box_type == BOX_LOG )
774                 p_sys->i_box_type = BOX_NONE;
775             else
776                 p_sys->i_box_type = BOX_LOG;
777             return 1;
778         case 'P':
779             if( p_sys->i_box_type == BOX_PLAYLIST )
780                 p_sys->i_box_type = BOX_NONE;
781             else
782                 p_sys->i_box_type = BOX_PLAYLIST;
783             return 1;
784         case 'B':
785             if( p_sys->i_box_type == BOX_BROWSE )
786                 p_sys->i_box_type = BOX_NONE;
787             else
788                 p_sys->i_box_type = BOX_BROWSE;
789             return 1;
790         case 'h':
791         case 'H':
792             if( p_sys->i_box_type == BOX_HELP )
793                 p_sys->i_box_type = BOX_NONE;
794             else
795                 p_sys->i_box_type = BOX_HELP;
796             p_sys->i_box_lines_total = 0;
797             return 1;
798         case '/':
799             if( p_sys->i_box_type != BOX_SEARCH )
800             {
801                 if( p_sys->psz_search_chain == NULL )
802                 {
803                     return 1;
804                 }
805                 p_sys->psz_search_chain[0] = '\0';
806                 p_sys->b_box_plidx_follow = VLC_FALSE;
807                 p_sys->i_before_search = p_sys->i_box_plidx;
808                 p_sys->i_box_type = BOX_SEARCH;
809             }
810             return 1;
811         case 'A': /* Open */
812             if( p_sys->i_box_type != BOX_OPEN )
813             {
814                 if( p_sys->psz_open_chain == NULL )
815                 {
816                     return 1;
817                 }
818                 p_sys->psz_open_chain[0] = '\0';
819                 p_sys->i_box_type = BOX_OPEN;
820             }
821             return 1;
822
823         /* Navigation */
824         case KEY_RIGHT:
825             p_sys->f_slider += 1.0;
826             if( p_sys->f_slider > 99.9 ) p_sys->f_slider = 99.9;
827             ManageSlider( p_intf );
828             return 1;
829
830         case KEY_LEFT:
831             p_sys->f_slider -= 1.0;
832             if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
833             ManageSlider( p_intf );
834             return 1;
835
836         /* Common control */
837         case 'f':
838         {
839             if( p_intf->p_sys->p_input )
840             {
841                 vout_thread_t *p_vout;
842                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
843                                           VLC_OBJECT_VOUT, FIND_CHILD );
844                 if( p_vout )
845                 {
846                     var_Get( p_vout, "fullscreen", &val );
847                     val.b_bool = !val.b_bool;
848                     var_Set( p_vout, "fullscreen", val );
849                     vlc_object_release( p_vout );
850                 }
851                 else
852                 {
853                     playlist_t *p_playlist;
854                     p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
855                                                   FIND_ANYWHERE );
856                     if( p_playlist )
857                     {
858                         var_Get( p_playlist, "fullscreen", &val );
859                         val.b_bool = !val.b_bool;
860                         var_Set( p_playlist, "fullscreen", val );
861                         vlc_object_release( p_playlist );
862                     }
863                 }
864             }
865             return 0;
866         }
867
868         case ' ':
869             PlayPause( p_intf );
870             return 1;
871
872         case 's':
873             if( p_intf->p_sys->p_playlist )
874             {
875                 playlist_Stop( p_intf->p_sys->p_playlist );
876             }
877             return 1;
878
879         case 'e':
880             Eject( p_intf );
881             return 1;
882
883         case '[':
884             if( p_sys->p_input )
885             {
886                 val.b_bool = VLC_TRUE;
887                 var_Set( p_sys->p_input, "prev-title", val );
888             }
889             return 1;
890
891         case ']':
892             if( p_sys->p_input )
893             {
894                 val.b_bool = VLC_TRUE;
895                 var_Set( p_sys->p_input, "next-title", val );
896             }
897             return 1;
898
899         case '<':
900             if( p_sys->p_input )
901             {
902                 val.b_bool = VLC_TRUE;
903                 var_Set( p_sys->p_input, "prev-chapter", val );
904             }
905             return 1;
906
907         case '>':
908             if( p_sys->p_input )
909             {
910                 val.b_bool = VLC_TRUE;
911                 var_Set( p_sys->p_input, "next-chapter", val );
912             }
913             return 1;
914
915         case 'p':
916             if( p_intf->p_sys->p_playlist )
917             {
918                 playlist_Prev( p_intf->p_sys->p_playlist );
919             }
920             clear();
921             return 1;
922
923         case 'n':
924             if( p_intf->p_sys->p_playlist )
925             {
926                 playlist_Next( p_intf->p_sys->p_playlist );
927             }
928             clear();
929             return 1;
930
931         case 'a':
932             aout_VolumeUp( p_intf, 1, NULL );
933             clear();
934             return 1;
935
936         case 'z':
937             aout_VolumeDown( p_intf, 1, NULL );
938             clear();
939             return 1;
940
941         /*
942          * ^l should clear and redraw the screen
943          */
944         case 0x0c:
945             clear();
946             return 1;
947
948         default:
949             return 0;
950     }
951 }
952
953 static void ManageSlider( intf_thread_t *p_intf )
954 {
955     intf_sys_t     *p_sys = p_intf->p_sys;
956     input_thread_t *p_input = p_sys->p_input;
957     vlc_value_t     val;
958
959     if( p_input == NULL )
960     {
961         return;
962     }
963     var_Get( p_input, "state", &val );
964     if( val.i_int != PLAYING_S )
965     {
966         return;
967     }
968
969     var_Get( p_input, "position", &val );
970     if( p_sys->f_slider == p_sys->f_slider_old )
971     {
972         p_sys->f_slider =
973         p_sys->f_slider_old = 100 * val.f_float;
974     }
975     else
976     {
977         p_sys->f_slider_old = p_sys->f_slider;
978
979         val.f_float = p_sys->f_slider / 100.0;
980         var_Set( p_input, "position", val );
981     }
982 }
983
984 static void SearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring )
985 {
986     int i_max;
987     int i_first = 0 ;
988     int i_item = -1;
989     intf_sys_t *p_sys = p_intf->p_sys;
990     playlist_t *p_playlist = p_sys->p_playlist;
991
992     if( p_sys->i_before_search >= 0 )
993     {
994         i_first = p_sys->i_before_search;
995     }
996
997     if( ( ! psz_searchstring ) ||  strlen( psz_searchstring ) <= 0 )
998     {
999         p_sys->i_box_plidx = p_sys->i_before_search;
1000         return;
1001     }
1002
1003     i_max = p_sys->i_current_view == VIEW_ALL ?
1004                 p_playlist->i_size : p_sys->i_plist_entries;
1005
1006     i_item = SubSearchPlaylist( p_intf, psz_searchstring, i_first + 1, i_max );
1007     if( i_item < 0 )
1008     {
1009         i_item = SubSearchPlaylist( p_intf, psz_searchstring, 0, i_first );
1010     }
1011
1012     if( i_item < 0 || i_item >= i_max ) return;
1013
1014     p_sys->i_box_plidx = i_item;
1015 }
1016
1017 static int SubSearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring,
1018                               int i_start, int i_stop )
1019 {
1020     intf_sys_t *p_sys = p_intf->p_sys;
1021     playlist_t *p_playlist = p_sys->p_playlist;
1022     int i, i_item = -1;
1023
1024     if( p_sys->i_current_view == VIEW_ALL )
1025     {
1026         for( i = i_start + 1; i < i_stop; i++ )
1027         {
1028             if( strcasestr( p_playlist->pp_items[i]->input.psz_name,
1029                             psz_searchstring ) != NULL
1030                 || strcasestr( p_playlist->pp_items[i]->input.psz_uri,
1031                                psz_searchstring ) != NULL )
1032             {
1033                 i_item = i;
1034                 break;
1035             }
1036         }
1037     }
1038     else
1039     {
1040         for( i = i_start + 1; i < i_stop; i++ )
1041         {
1042             if( strcasestr( p_sys->pp_plist[i]->psz_display,
1043                             psz_searchstring ) != NULL )
1044             {
1045                 i_item = i;
1046                 break;
1047             }
1048         }
1049     }
1050
1051     return i_item;
1052 }
1053
1054
1055 static void mvnprintw( int y, int x, int w, const char *p_fmt, ... )
1056 {
1057     va_list  vl_args;
1058     char    *p_buf = NULL;
1059     int      i_len;
1060
1061     va_start( vl_args, p_fmt );
1062     vasprintf( &p_buf, p_fmt, vl_args );
1063     va_end( vl_args );
1064
1065     if( p_buf == NULL )
1066     {
1067         return;
1068     }
1069     if(  w > 0 )
1070     {
1071         if( ( i_len = strlen( p_buf ) ) > w )
1072         {
1073             int i_cut = i_len - w;
1074             int x1 = i_len/2 - i_cut/2;
1075             int x2 = x1 + i_cut;
1076
1077             if( i_len > x2 )
1078             {
1079                 memmove( &p_buf[x1], &p_buf[x2], i_len - x2 );
1080             }
1081             p_buf[w] = '\0';
1082             if( w > 7 )
1083             {
1084                 p_buf[w/2-1] = '.';
1085                 p_buf[w/2  ] = '.';
1086                 p_buf[w/2+1] = '.';
1087             }
1088             mvprintw( y, x, "%s", p_buf );
1089         }
1090         else
1091         {
1092             mvprintw( y, x, "%s", p_buf );
1093             mvhline( y, x + i_len, ' ', w - i_len );
1094         }
1095     }
1096 }
1097 static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt, ... )
1098 {
1099     intf_sys_t     *p_sys = p_intf->p_sys;
1100
1101     va_list  vl_args;
1102     char    *p_buf = NULL;
1103
1104     if( l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_lines )
1105     {
1106         return;
1107     }
1108
1109     va_start( vl_args, p_fmt );
1110     vasprintf( &p_buf, p_fmt, vl_args );
1111     va_end( vl_args );
1112
1113     if( p_buf == NULL )
1114     {
1115         return;
1116     }
1117
1118     mvnprintw( p_sys->i_box_y + l - p_sys->i_box_start, x, COLS - x - 1, "%s", p_buf );
1119 }
1120
1121 static void Redraw( intf_thread_t *p_intf, time_t *t_last_refresh )
1122 {
1123     intf_sys_t     *p_sys = p_intf->p_sys;
1124     input_thread_t *p_input = p_sys->p_input;
1125     int y = 0;
1126     int h;
1127     int y_end;
1128
1129     //clear();
1130
1131     /* Title */
1132     attrset( A_REVERSE );
1133     mvnprintw( y, 0, COLS, "VLC media player" " (ncurses interface) [ h for help ]" );
1134     attroff( A_REVERSE );
1135     y += 2;
1136
1137     /* Infos */
1138     if( p_input && !p_input->b_dead )
1139     {
1140         char buf1[MSTRTIME_MAX_SIZE];
1141         char buf2[MSTRTIME_MAX_SIZE];
1142         vlc_value_t val;
1143         vlc_value_t val_list;
1144
1145         /* Source */
1146         mvnprintw( y++, 0, COLS, " Source   : %s",
1147                    p_input->input.p_item->psz_uri );
1148
1149         /* State */
1150         var_Get( p_input, "state", &val );
1151         if( val.i_int == PLAYING_S )
1152         {
1153             mvnprintw( y++, 0, COLS, " State    : Playing" );
1154         }
1155         else if( val.i_int == PAUSE_S )
1156         {
1157             mvnprintw( y++, 0, COLS, " State    : Paused" );
1158         }
1159         else
1160         {
1161             y++;
1162         }
1163         if( val.i_int != INIT_S && val.i_int != END_S )
1164         {
1165             audio_volume_t i_volume;
1166
1167             /* Position */
1168             var_Get( p_input, "time", &val );
1169             msecstotimestr( buf1, val.i_time / 1000 );
1170
1171             var_Get( p_input, "length", &val );
1172             msecstotimestr( buf2, val.i_time / 1000 );
1173
1174             mvnprintw( y++, 0, COLS, " Position : %s/%s (%.2f%%)", buf1, buf2, p_sys->f_slider );
1175
1176             /* Volume */
1177             aout_VolumeGet( p_intf, &i_volume );
1178             mvnprintw( y++, 0, COLS, " Volume   : %i%%", i_volume*200/AOUT_VOLUME_MAX );
1179
1180             /* Title */
1181             if( !var_Get( p_input, "title", &val ) )
1182             {
1183                 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
1184                 if( val_list.p_list->i_count > 0 )
1185                 {
1186                     mvnprintw( y++, 0, COLS, " Title    : %d/%d", val.i_int, val_list.p_list->i_count );
1187                 }
1188                 var_Change( p_input, "title", VLC_VAR_FREELIST, &val_list, NULL );
1189             }
1190
1191             /* Chapter */
1192             if( !var_Get( p_input, "chapter", &val ) )
1193             {
1194                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
1195                 if( val_list.p_list->i_count > 0 )
1196                 {
1197                     mvnprintw( y++, 0, COLS, " Chapter  : %d/%d", val.i_int, val_list.p_list->i_count );
1198                 }
1199                 var_Change( p_input, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
1200             }
1201         }
1202         else
1203         {
1204             y += 2;
1205         }
1206     }
1207     else
1208     {
1209         mvnprintw( y++, 0, COLS, "Source: <no current item>" );
1210         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1211         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1212         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1213     }
1214
1215     DrawBox( p_sys->w, y, 0, 3, COLS, "" );
1216     DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
1217     DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
1218     y += 3;
1219
1220     p_sys->i_box_y = y + 1;
1221     p_sys->i_box_lines = LINES - y - 2;
1222
1223     h = LINES - y;
1224     y_end = y + h - 1;
1225
1226     if( p_sys->i_box_type == BOX_HELP )
1227     {
1228         /* Help box */
1229         int l = 0;
1230         DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
1231
1232         MainBoxWrite( p_intf, l++, 1, "[Display]" );
1233         MainBoxWrite( p_intf, l++, 1, "     h,H         Show/Hide help box" );
1234         MainBoxWrite( p_intf, l++, 1, "     i           Show/Hide info box" );
1235         MainBoxWrite( p_intf, l++, 1, "     L           Show/Hide messages box" );
1236         MainBoxWrite( p_intf, l++, 1, "     P           Show/Hide playlist box" );
1237         MainBoxWrite( p_intf, l++, 1, "     B           Show/Hide filebrowser" );
1238         MainBoxWrite( p_intf, l++, 1, "" );
1239
1240         MainBoxWrite( p_intf, l++, 1, "[Global]" );
1241         MainBoxWrite( p_intf, l++, 1, "     q, Q        Quit" );
1242         MainBoxWrite( p_intf, l++, 1, "     s           Stop" );
1243         MainBoxWrite( p_intf, l++, 1, "     <space>     Pause/Play" );
1244         MainBoxWrite( p_intf, l++, 1, "     f           Toggle Fullscreen" );
1245         MainBoxWrite( p_intf, l++, 1, "     n, p        Next/Previous playlist item" );
1246         MainBoxWrite( p_intf, l++, 1, "     [, ]        Next/Previous title" );
1247         MainBoxWrite( p_intf, l++, 1, "     <, >        Next/Previous chapter" );
1248         MainBoxWrite( p_intf, l++, 1, "     <right>     Seek +1%%" );
1249         MainBoxWrite( p_intf, l++, 1, "     <left>      Seek -1%%" );
1250         MainBoxWrite( p_intf, l++, 1, "     a           Volume Up" );
1251         MainBoxWrite( p_intf, l++, 1, "     z           Volume Down" );
1252         MainBoxWrite( p_intf, l++, 1, "" );
1253
1254         MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
1255         MainBoxWrite( p_intf, l++, 1, "     r           Random" );
1256         MainBoxWrite( p_intf, l++, 1, "     l           Loop Playlist" );
1257         MainBoxWrite( p_intf, l++, 1, "     R           Repeat item" );
1258         MainBoxWrite( p_intf, l++, 1, "     o           Order Playlist by title" );
1259         MainBoxWrite( p_intf, l++, 1, "     O           Reverse order Playlist by title" );
1260         MainBoxWrite( p_intf, l++, 1, "     /           Look for an item" );
1261         MainBoxWrite( p_intf, l++, 1, "     A           Add an entry" );
1262         MainBoxWrite( p_intf, l++, 1, "     D, <del>    Delete an entry" );
1263         MainBoxWrite( p_intf, l++, 1, "     <backspace> Delete an entry" );
1264         MainBoxWrite( p_intf, l++, 1, "" );
1265
1266         MainBoxWrite( p_intf, l++, 1, "[Filebrowser]" );
1267         MainBoxWrite( p_intf, l++, 1, "     <enter>     Add the selected file to the playlist" );
1268         MainBoxWrite( p_intf, l++, 1, "     <space>     Add the selected directory to the playlist" );
1269         MainBoxWrite( p_intf, l++, 1, "" );
1270
1271         MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
1272         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Navigate through the box line by line" );
1273         MainBoxWrite( p_intf, l++, 1, "     <pgup>,<pgdown> Navigate through the box page by page" );
1274         MainBoxWrite( p_intf, l++, 1, "" );
1275
1276         MainBoxWrite( p_intf, l++, 1, "[Player]" );
1277         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Seek +/-5%%" );
1278         MainBoxWrite( p_intf, l++, 1, "" );
1279
1280         MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
1281         MainBoxWrite( p_intf, l++, 1, "     Ctrl-l          Refresh the screen" );
1282
1283         p_sys->i_box_lines_total = l;
1284         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1285         {
1286             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1287         }
1288
1289         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1290         {
1291             y += l - p_sys->i_box_start;
1292         }
1293         else
1294         {
1295             y += p_sys->i_box_lines;
1296         }
1297     }
1298     else if( p_sys->i_box_type == BOX_INFO )
1299     {
1300         /* Info box */
1301         int l = 0;
1302         DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1303
1304         if( p_input )
1305         {
1306             int i,j;
1307             vlc_mutex_lock( &p_input->input.p_item->lock );
1308             for( i = 0; i < p_input->input.p_item->i_categories; i++ )
1309             {
1310                 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
1311                 if( y >= y_end ) break;
1312                 MainBoxWrite( p_intf, l++, 1, "  [%s]", p_category->psz_name );
1313                 for( j = 0; j < p_category->i_infos; j++ )
1314                 {
1315                     info_t *p_info = p_category->pp_infos[j];
1316                     if( y >= y_end ) break;
1317                     MainBoxWrite( p_intf, l++, 1, "      %s: %s", p_info->psz_name, p_info->psz_value );
1318                 }
1319             }
1320             vlc_mutex_unlock( &p_input->input.p_item->lock );
1321         }
1322         else
1323         {
1324             MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1325         }
1326         p_sys->i_box_lines_total = l;
1327         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1328         {
1329             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1330         }
1331
1332         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1333         {
1334             y += l - p_sys->i_box_start;
1335         }
1336         else
1337         {
1338             y += p_sys->i_box_lines;
1339         }
1340     }
1341     else if( p_sys->i_box_type == BOX_LOG )
1342     {
1343         int i_line = 0;
1344         int i_stop;
1345         int i_start;
1346
1347         DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1348
1349         i_start = p_intf->p_sys->p_sub->i_start;
1350
1351         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1352         i_stop = *p_intf->p_sys->p_sub->pi_stop;
1353         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1354
1355         for( ;; )
1356         {
1357             static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1358             if( i_line >= h - 2 )
1359             {
1360                 break;
1361             }
1362             i_stop--;
1363             i_line++;
1364             if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1365             if( i_stop == i_start )
1366             {
1367                 break;
1368             }
1369             mvnprintw( y + h-2-i_line, 1, COLS - 2, "   [%s] %s",
1370                       ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1371                       p_sys->p_sub->p_msg[i_stop].psz_msg );
1372         }
1373
1374         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1375         p_intf->p_sys->p_sub->i_start = i_stop;
1376         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1377         y = y_end;
1378     }
1379     else if( p_sys->i_box_type == BOX_BROWSE )
1380     {
1381         /* Filebrowser box */
1382         int        i_start, i_stop;
1383         int        i_item;
1384         DrawBox( p_sys->w, y++, 0, h, COLS, " Browse " );
1385
1386         if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
1387         if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
1388
1389         if( p_sys->i_box_bidx < (h - 2)/2 )
1390         {
1391             i_start = 0;
1392             i_stop = h - 2;
1393         }
1394         else if( p_sys->i_dir_entries - p_sys->i_box_bidx > (h - 2)/2 )
1395         {
1396             i_start = p_sys->i_box_bidx - (h - 2)/2;
1397             i_stop = i_start + h - 2;
1398         }
1399         else
1400         {
1401             i_stop = p_sys->i_dir_entries;
1402             i_start = p_sys->i_dir_entries - (h - 2);
1403         }
1404         if( i_start < 0 )
1405         {
1406             i_start = 0;
1407         }
1408         if( i_stop > p_sys->i_dir_entries )
1409         {
1410             i_stop = p_sys->i_dir_entries;
1411         }
1412
1413         for( i_item = i_start; i_item < i_stop; i_item++ )
1414         {
1415             vlc_bool_t b_selected = ( p_sys->i_box_bidx == i_item );
1416
1417             if( y >= y_end ) break;
1418             if( b_selected )
1419             {
1420                 attrset( A_REVERSE );
1421             }
1422             mvnprintw( y++, 1, COLS - 2, "%c %s", p_sys->pp_dir_entries[i_item]->b_file == VLC_TRUE ? '-' : '+',
1423                             p_sys->pp_dir_entries[i_item]->psz_path );
1424             if( b_selected )
1425             {
1426                 attroff( A_REVERSE );
1427             }
1428         }
1429
1430     }
1431     else if( ( p_sys->i_box_type == BOX_PLAYLIST ||
1432                p_sys->i_box_type == BOX_SEARCH ||
1433                p_sys->i_box_type == BOX_OPEN  ) && p_sys->p_playlist )
1434     {
1435         /* Playlist box */
1436         playlist_t *p_playlist = p_sys->p_playlist;
1437         int        i_start, i_stop, i_max = p_sys->i_plist_entries;
1438         int        i_item;
1439         char       *psz_title;
1440
1441         switch( p_sys->i_current_view )
1442         {
1443             case VIEW_ALL:
1444                 psz_title = strdup( " Playlist (All, unsorted) " );
1445                 i_max = p_playlist->i_size;
1446                 break;
1447             case VIEW_CATEGORY:
1448                 psz_title = strdup( " Playlist (By category) " );
1449                 break;
1450             default:
1451                 psz_title = strdup( " Playlist (Manually added) " );
1452         }
1453
1454         DrawBox( p_sys->w, y++, 0, h, COLS, psz_title );
1455
1456         if( p_sys->i_current_view != VIEW_ALL &&
1457                 ( p_sys->b_need_update || p_sys->pp_plist == NULL ) )
1458         {
1459             PlaylistRebuild( p_intf );
1460         }
1461         if( p_sys->b_box_plidx_follow )
1462         {
1463             FindIndex( p_intf );
1464         }
1465
1466         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1467
1468         if( p_sys->i_box_plidx < (h - 2)/2 )
1469         {
1470             i_start = 0;
1471             i_stop = h - 2;
1472         }
1473         else if( i_max - p_sys->i_box_plidx > (h - 2)/2 )
1474         {
1475             i_start = p_sys->i_box_plidx - (h - 2)/2;
1476             i_stop = i_start + h - 2;
1477         }
1478         else
1479         {
1480             i_stop = i_max;
1481             i_start = i_max - (h - 2);
1482         }
1483         if( i_start < 0 )
1484         {
1485             i_start = 0;
1486         }
1487         if( i_stop > i_max )
1488         {
1489             i_stop = i_max;
1490         }
1491
1492        if( p_sys->i_current_view == VIEW_ALL )
1493        {
1494
1495         for( i_item = i_start; i_item < i_stop; i_item++ )
1496         {
1497             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1498             int c = p_playlist->i_index == i_item ? '>' : ' ';
1499
1500             if( y >= y_end ) break;
1501             if( b_selected )
1502             {
1503                 attrset( A_REVERSE );
1504             }
1505             if( !strcmp( p_playlist->pp_items[i_item]->input.psz_name,
1506                          p_playlist->pp_items[i_item]->input.psz_uri ) )
1507             {
1508                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s'",
1509                            c,
1510                            i_item,
1511                            p_playlist->pp_items[i_item]->input.psz_uri );
1512             }
1513             else
1514             {
1515                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s' (%s)",
1516                           c,
1517                           i_item,
1518                           p_playlist->pp_items[i_item]->input.psz_uri,
1519                           p_playlist->pp_items[i_item]->input.psz_name );
1520             }
1521             if( b_selected )
1522             {
1523                 attroff( A_REVERSE );
1524             }
1525         }
1526
1527        }
1528        else
1529        {
1530
1531         for( i_item = i_start; i_item < i_stop; i_item++ )
1532         {
1533             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1534             int c = ( p_playlist->status.p_item ==
1535                           p_sys->pp_plist[i_item]->p_item ) ? '>' : ' ';
1536
1537             if( y >= y_end ) break;
1538             if( b_selected )
1539             {
1540                 attrset( A_REVERSE );
1541             }
1542             mvnprintw( y++, 1, COLS - 2, "%c%s", c,
1543                        p_sys->pp_plist[i_item]->psz_display );
1544             if( b_selected )
1545             {
1546                 attroff( A_REVERSE );
1547             }
1548         }
1549
1550        }
1551     }
1552     else
1553     {
1554         y++;
1555     }
1556     if( p_sys->i_box_type == BOX_SEARCH )
1557     {
1558         DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1559         if( p_sys->psz_search_chain )
1560         {
1561             if( strlen( p_sys->psz_search_chain ) == 0 &&
1562                 p_sys->psz_old_search != NULL )
1563             {
1564                 /* Searching next entry */
1565                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1566             }
1567             else
1568             {
1569                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1570             }
1571         }
1572     }
1573     if( p_sys->i_box_type == BOX_OPEN )
1574     {
1575         if( p_sys->psz_open_chain )
1576         {
1577             DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1578             mvnprintw( 7, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1579         }
1580     }
1581
1582     while( y < y_end )
1583     {
1584         DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1585     }
1586
1587     refresh();
1588
1589     *t_last_refresh = time( 0 );
1590 }
1591
1592 static void PlaylistRebuild( intf_thread_t *p_intf )
1593 {
1594     intf_sys_t *p_sys = p_intf->p_sys;
1595     playlist_view_t *p_view;
1596     playlist_t *p_playlist = p_sys->p_playlist;
1597
1598     if( p_playlist == NULL )
1599     {
1600         return;
1601     }
1602
1603     vlc_mutex_lock( &p_playlist->object_lock );
1604
1605     p_view = playlist_ViewFind( p_playlist, p_intf->p_sys->i_current_view );
1606
1607     /* First clear the old one */
1608     PlaylistDestroy( p_intf );
1609
1610     /* Build the new one */
1611     PlaylistAddNode( p_intf, p_view->p_root, 0, "" );
1612
1613     p_sys->b_need_update = VLC_FALSE;
1614
1615     vlc_mutex_unlock( &p_playlist->object_lock );
1616 }
1617
1618 static void PlaylistAddNode( intf_thread_t *p_intf, playlist_item_t *p_node,
1619                              int i, char *c )
1620 {
1621     intf_sys_t *p_sys = p_intf->p_sys;
1622     playlist_item_t *p_child;
1623     char *psz_tmp;
1624     int k;
1625
1626     psz_tmp = (char *)malloc( strlen( c ) + 4 );
1627     if( psz_tmp == NULL ) return;
1628     for( k = 0; k < p_node->i_children; k++ )
1629     {
1630         struct pl_item_t *p_pl_item;
1631         char *buff;
1632         int i_size;
1633
1634         p_child = p_node->pp_children[k];
1635         i_size = strlen( c ) + strlen( p_child->input.psz_name ) + 4;
1636         buff = (char *)malloc( sizeof( char ) * i_size );
1637         p_pl_item = (struct pl_item_t *)malloc( sizeof( struct pl_item_t ) );
1638         if(  p_pl_item == NULL || buff == NULL ) return;
1639
1640         if( strlen( c ) )
1641         {
1642             sprintf( buff, "%s%c-%s", c, k == p_node->i_children - 1 ?
1643                      '`' : '|', p_child->input.psz_name );
1644         }
1645         else
1646         {
1647             sprintf( buff, " %s", p_child->input.psz_name );
1648         }
1649         p_pl_item->psz_display = strdup( buff );
1650         p_pl_item->p_item = p_child;
1651         INSERT_ELEM( p_sys->pp_plist, p_sys->i_plist_entries,
1652                      p_sys->i_plist_entries, p_pl_item );
1653         free( buff );
1654         i++;
1655
1656         if( p_child->i_children > 0 )
1657         {
1658             sprintf( psz_tmp, "%s%c ", c,
1659                      k == p_node->i_children - 1 ? ' ' : '|' );
1660             PlaylistAddNode( p_intf, p_child, i,
1661                              strlen( c ) ? psz_tmp : " " );
1662         }
1663     }
1664     free( psz_tmp );
1665 }
1666
1667 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1668                             vlc_value_t oval, vlc_value_t nval, void *param )
1669 {
1670     intf_thread_t *p_intf = (intf_thread_t *)param;
1671     p_intf->p_sys->b_need_update = VLC_TRUE;
1672     return VLC_SUCCESS;
1673 }
1674
1675 static void FindIndex( intf_thread_t *p_intf )
1676 {
1677     intf_sys_t *p_sys = p_intf->p_sys;
1678     int i;
1679
1680     if( p_sys->i_current_view == VIEW_ALL )
1681     {
1682          p_sys->i_box_plidx = p_sys->p_playlist->i_index;
1683     }
1684     else if( ( p_sys->i_box_plidx < p_sys->i_plist_entries &&
1685                p_sys->pp_plist[p_sys->i_box_plidx]->p_item !=
1686                p_sys->p_playlist->status.p_item ) )
1687     {
1688         for( i = 0; i < p_sys->i_plist_entries; i++ )
1689         {
1690             if( p_sys->pp_plist[i]->p_item ==
1691                 p_sys->p_playlist->status.p_item )
1692             {
1693                 p_sys->i_box_plidx = i;
1694                 break;
1695             }
1696         }
1697     }
1698 }
1699
1700 static void PlaylistDestroy( intf_thread_t *p_intf )
1701 {
1702     intf_sys_t *p_sys = p_intf->p_sys;
1703     int i;
1704
1705     for( i = 0; i < p_sys->i_plist_entries; i++ )
1706     {
1707         struct pl_item_t *p_pl_item = p_sys->pp_plist[i];
1708         free( p_pl_item->psz_display );
1709         REMOVE_ELEM( p_sys->pp_plist, p_sys->i_plist_entries, i );
1710         free( p_pl_item );
1711     }
1712     p_sys->pp_plist = NULL;
1713     p_sys->i_plist_entries = 0;
1714 }
1715
1716 static void Eject( intf_thread_t *p_intf )
1717 {
1718     char *psz_device = NULL, *psz_parser, *psz_name;
1719
1720     /*
1721      * Get the active input
1722      * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
1723      * If it's neither of these, then return
1724      */
1725
1726     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1727                                                        FIND_ANYWHERE );
1728
1729     if( p_playlist == NULL )
1730     {
1731         return;
1732     }
1733
1734     vlc_mutex_lock( &p_playlist->object_lock );
1735
1736     if( p_playlist->i_index < 0 )
1737     {
1738         vlc_mutex_unlock( &p_playlist->object_lock );
1739         vlc_object_release( p_playlist );
1740         return;
1741     }
1742
1743     psz_name = p_playlist->pp_items[ p_playlist->i_index ]->input.psz_name;
1744
1745     if( psz_name )
1746     {
1747         if( !strncmp(psz_name, "dvd://", 4) )
1748         {
1749             switch( psz_name[strlen("dvd://")] )
1750             {
1751             case '\0':
1752             case '@':
1753                 psz_device = config_GetPsz( p_intf, "dvd" );
1754                 break;
1755             default:
1756                 /* Omit the first MRL-selector characters */
1757                 psz_device = strdup( psz_name + strlen("dvd://" ) );
1758                 break;
1759             }
1760         }
1761         else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
1762         {
1763             switch( psz_name[strlen(VCD_MRL)] )
1764             {
1765             case '\0':
1766             case '@':
1767                 psz_device = config_GetPsz( p_intf, VCD_MRL );
1768                 break;
1769             default:
1770                 /* Omit the beginning MRL-selector characters */
1771                 psz_device = strdup( psz_name + strlen(VCD_MRL) );
1772                 break;
1773             }
1774         }
1775         else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
1776         {
1777             switch( psz_name[strlen(CDDA_MRL)] )
1778             {
1779             case '\0':
1780             case '@':
1781                 psz_device = config_GetPsz( p_intf, "cd-audio" );
1782                 break;
1783             default:
1784                 /* Omit the beginning MRL-selector characters */
1785                 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
1786                 break;
1787             }
1788         }
1789         else
1790         {
1791             psz_device = strdup( psz_name );
1792         }
1793     }
1794
1795     vlc_mutex_unlock( &p_playlist->object_lock );
1796     vlc_object_release( p_playlist );
1797
1798     if( psz_device == NULL )
1799     {
1800         return;
1801     }
1802
1803     /* Remove what we have after @ */
1804     psz_parser = psz_device;
1805     for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
1806     {
1807         if( *psz_parser == '@' )
1808         {
1809             *psz_parser = '\0';
1810             break;
1811         }
1812     }
1813
1814     /* If there's a stream playing, we aren't allowed to eject ! */
1815     if( p_intf->p_sys->p_input == NULL )
1816     {
1817         msg_Dbg( p_intf, "ejecting %s", psz_device );
1818
1819         intf_Eject( p_intf, psz_device );
1820     }
1821
1822     free(psz_device);
1823     return;
1824 }
1825
1826 static void ReadDir( intf_thread_t *p_intf )
1827 {
1828     intf_sys_t     *p_sys = p_intf->p_sys;
1829     DIR *                       p_current_dir;
1830     struct dirent *             p_dir_content;
1831     int i;
1832
1833     if( p_sys->psz_current_dir && *p_sys->psz_current_dir )
1834     {
1835         /* Open the dir */
1836         p_current_dir = opendir( p_sys->psz_current_dir );
1837
1838         if( p_current_dir == NULL )
1839         {
1840             /* something went bad, get out of here ! */
1841 #ifdef HAVE_ERRNO_H
1842             msg_Warn( p_intf, "cannot open directory `%s' (%s)",
1843                       p_sys->psz_current_dir, strerror(errno));
1844 #else
1845             msg_Warn( p_intf, "cannot open directory `%s'", p_sys->psz_current_dir );
1846 #endif
1847             return;
1848         }
1849
1850         /* Clean the old shit */
1851         for( i = 0; i < p_sys->i_dir_entries; i++ )
1852         {
1853             struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
1854             free( p_dir_entry->psz_path );
1855             REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
1856             free( p_dir_entry );
1857         }
1858         p_sys->pp_dir_entries = NULL;
1859         p_sys->i_dir_entries = 0;
1860
1861         /* get the first directory entry */
1862         p_dir_content = readdir( p_current_dir );
1863
1864         /* while we still have entries in the directory */
1865         while( p_dir_content != NULL )
1866         {
1867 #if defined( S_ISDIR )
1868             struct stat stat_data;
1869 #endif
1870             struct dir_entry_t *p_dir_entry;
1871             int i_size_entry = strlen( p_sys->psz_current_dir ) +
1872                                strlen( p_dir_content->d_name ) + 2;
1873             char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
1874
1875             sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir,
1876                      p_dir_content->d_name );
1877
1878             if( !( p_dir_entry = malloc( sizeof( struct dir_entry_t) ) ) )
1879             {
1880                 free( psz_uri);
1881                 return;
1882             }
1883
1884 #if defined( S_ISDIR )
1885             stat( psz_uri, &stat_data );
1886             if( S_ISDIR(stat_data.st_mode) )
1887 #elif defined( DT_DIR )
1888             if( p_dir_content->d_type & DT_DIR )
1889 #else
1890             if( 0 )
1891 #endif
1892             {
1893                 p_dir_entry->psz_path = strdup( p_dir_content->d_name );
1894                 p_dir_entry->b_file = VLC_FALSE;
1895                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1896                      p_sys->i_dir_entries, p_dir_entry );
1897             }
1898             else
1899             {
1900                 p_dir_entry->psz_path = strdup( p_dir_content->d_name );
1901                 p_dir_entry->b_file = VLC_TRUE;
1902                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1903                      p_sys->i_dir_entries, p_dir_entry );
1904             }
1905
1906             free( psz_uri );
1907             /* Read next entry */
1908             p_dir_content = readdir( p_current_dir );
1909         }
1910         closedir( p_current_dir );
1911         return;
1912     }
1913     else
1914     {
1915         msg_Dbg( p_intf, "no current dir set" );
1916         return;
1917     }
1918 }
1919
1920 static void PlayPause( intf_thread_t *p_intf )
1921 {
1922     input_thread_t *p_input = p_intf->p_sys->p_input;
1923     vlc_value_t val;
1924
1925     if( p_input )
1926     {
1927         var_Get( p_input, "state", &val );
1928         if( val.i_int != PAUSE_S )
1929         {
1930             val.i_int = PAUSE_S;
1931         }
1932         else
1933         {
1934             val.i_int = PLAYING_S;
1935         }
1936         var_Set( p_input, "state", val );
1937     }
1938     else if( p_intf->p_sys->p_playlist )
1939     {
1940         playlist_Play( p_intf->p_sys->p_playlist );
1941     }
1942 }
1943
1944 /****************************************************************************
1945  *
1946  ****************************************************************************/
1947 static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title )
1948 {
1949     int i;
1950     int i_len;
1951
1952     if( w > 3 && h > 2 )
1953     {
1954         if( title == NULL ) title = "";
1955         i_len = strlen( title );
1956
1957         if( i_len > w - 2 ) i_len = w - 2;
1958
1959         mvwaddch( win, y, x,    ACS_ULCORNER );
1960         mvwhline( win, y, x+1,  ACS_HLINE, ( w-i_len-2)/2 );
1961         mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
1962         mvwhline( win, y, x+(w-i_len)/2+i_len,  ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
1963         mvwaddch( win, y, x+w-1,ACS_URCORNER );
1964
1965         for( i = 0; i < h-2; i++ )
1966         {
1967             mvwaddch( win, y+i+1, x,     ACS_VLINE );
1968             mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
1969         }
1970
1971         mvwaddch( win, y+h-1, x,     ACS_LLCORNER );
1972         mvwhline( win, y+h-1, x+1,   ACS_HLINE, w - 2 );
1973         mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
1974     }
1975 }
1976
1977 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
1978 {
1979     if( w > 0 )
1980     {
1981         mvhline( y, x, ' ', w );
1982     }
1983 }
1984
1985 static void DrawLine( WINDOW *win, int y, int x, int w )
1986 {
1987     if( w > 0 )
1988     {
1989         attrset( A_REVERSE );
1990         mvhline( y, x, ' ', w );
1991         attroff( A_REVERSE );
1992     }
1993 }