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