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