]> git.sesse.net Git - vlc/blob - modules/gui/ncurses.c
ncurses: use directory:// URI when adding a directory to playlist
[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( "directory://" ) +
671                                        strlen( p_sys->psz_current_dir ) +
672                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
673                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
674
675                     sprintf( psz_uri, "directory://%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
676
677                     playlist_item_t *p_parent = p_sys->p_node;
678                     if( !p_parent )
679                     p_parent = p_playlist->status.p_node;
680                     if( !p_parent )
681                         p_parent = p_playlist->p_local_onelevel;
682
683                     while( p_parent->p_parent && p_parent->p_parent->p_parent )
684                         p_parent = p_parent->p_parent;
685
686                     playlist_Add( p_playlist, psz_uri, NULL, PLAYLIST_APPEND,
687                                   PLAYLIST_END,
688                                   p_parent->p_input == 
689                                     p_playlist->p_local_onelevel->p_input
690                                   , VLC_FALSE );
691
692                     p_sys->i_box_type = BOX_PLAYLIST;
693                     free( psz_uri );
694                 }
695                 else
696                 {
697                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
698                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
699                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
700
701                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
702
703                     p_sys->psz_current_dir = strdup( psz_uri );
704                     ReadDir( p_intf );
705                     free( psz_uri );
706                 }
707                 break;
708             default:
709                 b_ret = VLC_FALSE;
710                 break;
711         }
712         if( b_ret )
713         {
714             if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
715             if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
716             return 1;
717         }
718     }
719     else if( p_sys->i_box_type == BOX_HELP || p_sys->i_box_type == BOX_INFO ||
720              p_sys->i_box_type == BOX_META )
721     {
722         switch( i_key )
723         {
724             case KEY_HOME:
725                 p_sys->i_box_start = 0;
726                 return 1;
727 #ifdef __FreeBSD__
728             case KEY_SELECT:
729 #endif
730             case KEY_END:
731                 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
732                 return 1;
733             case KEY_UP:
734                 if( p_sys->i_box_start > 0 ) p_sys->i_box_start--;
735                 return 1;
736             case KEY_DOWN:
737                 if( p_sys->i_box_start < p_sys->i_box_lines_total - 1 )
738                 {
739                     p_sys->i_box_start++;
740                 }
741                 return 1;
742             case KEY_PPAGE:
743                 p_sys->i_box_start -= p_sys->i_box_lines;
744                 if( p_sys->i_box_start < 0 ) p_sys->i_box_start = 0;
745                 return 1;
746             case KEY_NPAGE:
747                 p_sys->i_box_start += p_sys->i_box_lines;
748                 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
749                 {
750                     p_sys->i_box_start = p_sys->i_box_lines_total - 1;
751                 }
752                 return 1;
753             default:
754                 break;
755         }
756     }
757     else if( p_sys->i_box_type == BOX_NONE )
758     {
759         switch( i_key )
760         {
761             case KEY_HOME:
762                 p_sys->f_slider = 0;
763                 ManageSlider( p_intf );
764                 return 1;
765 #ifdef __FreeBSD__
766             case KEY_SELECT:
767 #endif
768             case KEY_END:
769                 p_sys->f_slider = 99.9;
770                 ManageSlider( p_intf );
771                 return 1;
772             case KEY_UP:
773                 p_sys->f_slider += 5.0;
774                 if( p_sys->f_slider >= 99.0 ) p_sys->f_slider = 99.0;
775                 ManageSlider( p_intf );
776                 return 1;
777             case KEY_DOWN:
778                 p_sys->f_slider -= 5.0;
779                 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
780                 ManageSlider( p_intf );
781                 return 1;
782
783             default:
784                 break;
785         }
786     }
787     else if( p_sys->i_box_type == BOX_SEARCH && p_sys->psz_search_chain )
788     {
789         int i_chain_len;
790         i_chain_len = strlen( p_sys->psz_search_chain );
791         switch( i_key )
792         {
793             case 0x0c:      /* ^l */
794                 clear();
795                 return 1;
796             case KEY_ENTER:
797             case 0x0d:
798                 if( i_chain_len > 0 )
799                 {
800                     p_sys->psz_old_search = strdup( p_sys->psz_search_chain );
801                 }
802                 else if( p_sys->psz_old_search )
803                 {
804                     SearchPlaylist( p_intf, p_sys->psz_old_search );
805                 }
806                 p_sys->i_box_type = BOX_PLAYLIST;
807                 return 1;
808             case 0x1b:      /* Esc. */
809                 p_sys->i_box_plidx = p_sys->i_before_search;
810                 p_sys->i_box_type = BOX_PLAYLIST;
811                 return 1;
812             case KEY_BACKSPACE:
813                 RemoveLastUTF8Entity( p_sys->psz_search_chain, i_chain_len );
814                 break;
815             default:
816             {
817                 char *psz_utf8 = KeyToUTF8( i_key, p_sys->psz_partial_keys );
818
819                 if( psz_utf8 != NULL )
820                 {
821                     if( i_chain_len + strlen( psz_utf8 ) < SEARCH_CHAIN_SIZE )
822                     {
823                         strcpy( p_sys->psz_search_chain + i_chain_len,
824                                 psz_utf8 );
825                     }
826                     free( psz_utf8 );
827                 }
828                 break;
829             }
830         }
831         if( p_sys->psz_old_search )
832         {
833             free( p_sys->psz_old_search );
834             p_sys->psz_old_search = NULL;
835         }
836         SearchPlaylist( p_intf, p_sys->psz_search_chain );
837         return 1;
838     }
839     else if( p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain )
840     {
841         int i_chain_len = strlen( p_sys->psz_open_chain );
842
843         switch( i_key )
844         {
845             case 0x0c:      /* ^l */
846                 clear();
847                 return 1;
848             case KEY_ENTER:
849             case 0x0d:
850                 if( i_chain_len > 0 )
851                 {
852                     playlist_item_t *p_parent = p_sys->p_node;
853                    
854                     if( !p_parent )
855                     p_parent = p_playlist->status.p_node;
856                     if( !p_parent )
857                         p_parent = p_playlist->p_local_onelevel;
858
859                     while( p_parent->p_parent && p_parent->p_parent->p_parent )
860                         p_parent = p_parent->p_parent;
861
862                     playlist_Add( p_playlist, p_sys->psz_open_chain, NULL,
863                                   PLAYLIST_APPEND|PLAYLIST_GO, PLAYLIST_END,
864                                   p_parent->p_input == 
865                                     p_playlist->p_local_onelevel->p_input
866                                   , VLC_FALSE );
867
868                     p_sys->b_box_plidx_follow = VLC_TRUE;
869                 }
870                 p_sys->i_box_type = BOX_PLAYLIST;
871                 return 1;
872             case 0x1b:      /* Esc. */
873                 p_sys->i_box_type = BOX_PLAYLIST;
874                 return 1;
875             case KEY_BACKSPACE:
876                 RemoveLastUTF8Entity( p_sys->psz_open_chain, i_chain_len );
877                 break;
878             default:
879             {
880                 char *psz_utf8 = KeyToUTF8( i_key, p_sys->psz_partial_keys );
881
882                 if( psz_utf8 != NULL )
883                 {
884                     if( i_chain_len + strlen( psz_utf8 ) < OPEN_CHAIN_SIZE )
885                     {
886                         strcpy( p_sys->psz_open_chain + i_chain_len,
887                                 psz_utf8 );
888                     }
889                     free( psz_utf8 );
890                 }
891                 break;
892             }
893         }
894         return 1;
895     }
896
897
898     /* Common keys */
899     switch( i_key )
900     {
901         case 'q':
902         case 'Q':
903         case 0x1b:  /* Esc */
904             vlc_object_kill( p_intf->p_libvlc );
905             return 0;
906
907         /* Box switching */
908         case 'i':
909             if( p_sys->i_box_type == BOX_INFO )
910                 p_sys->i_box_type = BOX_NONE;
911             else
912                 p_sys->i_box_type = BOX_INFO;
913             p_sys->i_box_lines_total = 0;
914             return 1;
915         case 'm':
916             if( p_sys->i_box_type == BOX_META )
917                 p_sys->i_box_type = BOX_NONE;
918             else
919                 p_sys->i_box_type = BOX_META;
920             p_sys->i_box_lines_total = 0;
921             return 1;
922         case 'L':
923             if( p_sys->i_box_type == BOX_LOG )
924                 p_sys->i_box_type = BOX_NONE;
925             else
926                 p_sys->i_box_type = BOX_LOG;
927             return 1;
928         case 'P':
929             if( p_sys->i_box_type == BOX_PLAYLIST )
930                 p_sys->i_box_type = BOX_NONE;
931             else
932                 p_sys->i_box_type = BOX_PLAYLIST;
933             return 1;
934         case 'B':
935             if( p_sys->i_box_type == BOX_BROWSE )
936                 p_sys->i_box_type = BOX_NONE;
937             else
938                 p_sys->i_box_type = BOX_BROWSE;
939             return 1;
940         case 'h':
941         case 'H':
942             if( p_sys->i_box_type == BOX_HELP )
943                 p_sys->i_box_type = BOX_NONE;
944             else
945                 p_sys->i_box_type = BOX_HELP;
946             p_sys->i_box_lines_total = 0;
947             return 1;
948         case '/':
949             if( p_sys->i_box_type != BOX_SEARCH )
950             {
951                 if( p_sys->psz_search_chain == NULL )
952                 {
953                     return 1;
954                 }
955                 p_sys->psz_search_chain[0] = '\0';
956                 p_sys->b_box_plidx_follow = VLC_FALSE;
957                 p_sys->i_before_search = p_sys->i_box_plidx;
958                 p_sys->i_box_type = BOX_SEARCH;
959             }
960             return 1;
961         case 'A': /* Open */
962             if( p_sys->i_box_type != BOX_OPEN )
963             {
964                 if( p_sys->psz_open_chain == NULL )
965                 {
966                     return 1;
967                 }
968                 p_sys->psz_open_chain[0] = '\0';
969                 p_sys->i_box_type = BOX_OPEN;
970             }
971             return 1;
972
973         /* Navigation */
974         case KEY_RIGHT:
975             p_sys->f_slider += 1.0;
976             if( p_sys->f_slider > 99.9 ) p_sys->f_slider = 99.9;
977             ManageSlider( p_intf );
978             return 1;
979
980         case KEY_LEFT:
981             p_sys->f_slider -= 1.0;
982             if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
983             ManageSlider( p_intf );
984             return 1;
985
986         /* Common control */
987         case 'f':
988         {
989             if( p_intf->p_sys->p_input )
990             {
991                 vout_thread_t *p_vout;
992                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
993                                           VLC_OBJECT_VOUT, FIND_CHILD );
994                 if( p_vout )
995                 {
996                     var_Get( p_vout, "fullscreen", &val );
997                     val.b_bool = !val.b_bool;
998                     var_Set( p_vout, "fullscreen", val );
999                     vlc_object_release( p_vout );
1000                 }
1001                 else
1002                 {
1003                     var_Get( p_playlist, "fullscreen", &val );
1004                     val.b_bool = !val.b_bool;
1005                     var_Set( p_playlist, "fullscreen", val );
1006                 }
1007             }
1008             return 0;
1009         }
1010
1011         case ' ':
1012             PlayPause( p_intf );
1013             return 1;
1014
1015         case 's':
1016             playlist_Stop( p_playlist );
1017             return 1;
1018
1019         case 'e':
1020             Eject( p_intf );
1021             return 1;
1022
1023         case '[':
1024             if( p_sys->p_input )
1025             {
1026                 val.b_bool = VLC_TRUE;
1027                 var_Set( p_sys->p_input, "prev-title", val );
1028             }
1029             return 1;
1030
1031         case ']':
1032             if( p_sys->p_input )
1033             {
1034                 val.b_bool = VLC_TRUE;
1035                 var_Set( p_sys->p_input, "next-title", val );
1036             }
1037             return 1;
1038
1039         case '<':
1040             if( p_sys->p_input )
1041             {
1042                 val.b_bool = VLC_TRUE;
1043                 var_Set( p_sys->p_input, "prev-chapter", val );
1044             }
1045             return 1;
1046
1047         case '>':
1048             if( p_sys->p_input )
1049             {
1050                 val.b_bool = VLC_TRUE;
1051                 var_Set( p_sys->p_input, "next-chapter", val );
1052             }
1053             return 1;
1054
1055         case 'p':
1056             playlist_Prev( p_playlist );
1057             clear();
1058             return 1;
1059
1060         case 'n':
1061             playlist_Next( p_playlist );
1062             clear();
1063             return 1;
1064
1065         case 'a':
1066             aout_VolumeUp( p_intf, 1, NULL );
1067             clear();
1068             return 1;
1069
1070         case 'z':
1071             aout_VolumeDown( p_intf, 1, NULL );
1072             clear();
1073             return 1;
1074
1075         /*
1076          * ^l should clear and redraw the screen
1077          */
1078         case 0x0c:
1079             clear();
1080             return 1;
1081
1082         default:
1083             return 0;
1084     }
1085 }
1086
1087 static void ManageSlider( intf_thread_t *p_intf )
1088 {
1089     intf_sys_t     *p_sys = p_intf->p_sys;
1090     input_thread_t *p_input = p_sys->p_input;
1091     vlc_value_t     val;
1092
1093     if( p_input == NULL )
1094     {
1095         return;
1096     }
1097     var_Get( p_input, "state", &val );
1098     if( val.i_int != PLAYING_S )
1099     {
1100         return;
1101     }
1102
1103     var_Get( p_input, "position", &val );
1104     if( p_sys->f_slider == p_sys->f_slider_old )
1105     {
1106         p_sys->f_slider =
1107         p_sys->f_slider_old = 100 * val.f_float;
1108     }
1109     else
1110     {
1111         p_sys->f_slider_old = p_sys->f_slider;
1112
1113         val.f_float = p_sys->f_slider / 100.0;
1114         var_Set( p_input, "position", val );
1115     }
1116 }
1117
1118 static void SearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring )
1119 {
1120     int i_max;
1121     int i_first = 0 ;
1122     int i_item = -1;
1123     intf_sys_t *p_sys = p_intf->p_sys;
1124
1125     if( p_sys->i_before_search >= 0 )
1126     {
1127         i_first = p_sys->i_before_search;
1128     }
1129
1130     if( ( ! psz_searchstring ) ||  strlen( psz_searchstring ) <= 0 )
1131     {
1132         p_sys->i_box_plidx = p_sys->i_before_search;
1133         return;
1134     }
1135
1136     i_max = p_sys->i_plist_entries;
1137
1138     i_item = SubSearchPlaylist( p_intf, psz_searchstring, i_first + 1, i_max );
1139     if( i_item < 0 )
1140     {
1141         i_item = SubSearchPlaylist( p_intf, psz_searchstring, 0, i_first );
1142     }
1143
1144     if( i_item < 0 || i_item >= i_max ) return;
1145
1146     p_sys->i_box_plidx = i_item;
1147 }
1148
1149 static int SubSearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring,
1150                               int i_start, int i_stop )
1151 {
1152     intf_sys_t *p_sys = p_intf->p_sys;
1153     int i, i_item = -1;
1154
1155     for( i = i_start + 1; i < i_stop; i++ )
1156     {
1157         if( strcasestr( p_sys->pp_plist[i]->psz_display,
1158                         psz_searchstring ) != NULL )
1159         {
1160             i_item = i;
1161             break;
1162         }
1163     }
1164
1165     return i_item;
1166 }
1167
1168
1169 static void mvnprintw( int y, int x, int w, const char *p_fmt, ... )
1170 {
1171     va_list  vl_args;
1172     char    *p_buf = NULL;
1173     int      i_len;
1174
1175     va_start( vl_args, p_fmt );
1176     if( vasprintf( &p_buf, p_fmt, vl_args ) == -1 )
1177         return;
1178     va_end( vl_args );
1179
1180     if( ( p_buf == NULL ) || ( w <= 0 ) )
1181         return;
1182
1183     i_len = strlen( p_buf );
1184
1185 #ifdef HAVE_NCURSESW
1186     wchar_t psz_wide[i_len + 1];
1187     
1188     EnsureUTF8( p_buf );
1189     size_t i_char_len = mbstowcs( psz_wide, p_buf, i_len );
1190
1191     size_t i_width; /* number of columns */
1192
1193     if( i_char_len == (size_t)-1 )
1194         /* an invalid character was encountered */
1195         abort();
1196     else
1197     {
1198         i_width = wcswidth( psz_wide, i_char_len );
1199         if( i_width == (size_t)-1 )
1200         {
1201             /* a non printable character was encountered */
1202             unsigned int i;
1203             int i_cwidth;
1204             i_width = 0;
1205             for( i = 0 ; i < i_char_len ; i++ )
1206             {
1207                 i_cwidth = wcwidth( psz_wide[i] );
1208                 if( i_cwidth != -1 )
1209                     i_width += i_cwidth;
1210             }
1211         }
1212     }
1213     if( i_width > (size_t)w )
1214     {
1215         int i_total_width = 0;
1216         int i = 0;
1217         while( i_total_width < w )
1218         {
1219             i_total_width += wcwidth( psz_wide[i] );
1220             if( w > 7 && i_total_width >= w/2 )
1221             {
1222                 psz_wide[i  ] = '.';
1223                 psz_wide[i+1] = '.';
1224                 i_total_width -= wcwidth( psz_wide[i] ) - 2;
1225                 if( i > 0 )
1226                 {
1227                     /* we require this check only if at least one character
1228                      * 4 or more columns wide exists (which i doubt) */
1229                     psz_wide[i-1] = '.';
1230                     i_total_width -= wcwidth( psz_wide[i-1] ) - 1;
1231                 }
1232
1233                 /* find the widest string */
1234                 int j, i_2nd_width = 0;
1235                 for( j = i_char_len - 1; i_2nd_width < w - i_total_width; j-- )
1236                     i_2nd_width += wcwidth( psz_wide[j] );
1237
1238                 /* we already have i_total_width columns filled, and we can't
1239                  * have more than w columns */
1240                 if( i_2nd_width > w - i_total_width )
1241                     j++;
1242
1243                 wmemmove( &psz_wide[i+2], &psz_wide[j+1], i_char_len - j - 1 );
1244                 psz_wide[i + 2 + i_char_len - j - 1] = '\0';
1245                 break;
1246             }
1247             i++;
1248         }
1249         if( w <= 7 ) /* we don't add the '...' else we lose too much chars */
1250             psz_wide[i] = '\0';
1251
1252         size_t i_wlen = wcslen( psz_wide ) * 6 + 1; /* worst case */
1253         char psz_ellipsized[i_wlen];
1254         wcstombs( psz_ellipsized, psz_wide, i_wlen );
1255         mvprintw( y, x, "%s", psz_ellipsized );
1256     }
1257     else
1258     {
1259         mvprintw( y, x, "%s", p_buf );
1260         mvhline( y, x + i_width, ' ', w - i_width );
1261     }
1262 #else
1263     if( i_len > w )
1264     {
1265         int i_cut = i_len - w;
1266         int x1 = i_len/2 - i_cut/2;
1267         int x2 = x1 + i_cut;
1268
1269         if( i_len > x2 )
1270         {
1271             memmove( &p_buf[x1], &p_buf[x2], i_len - x2 );
1272         }
1273         p_buf[w] = '\0';
1274         if( w > 7 )
1275         {
1276             p_buf[w/2-1] = '.';
1277             p_buf[w/2  ] = '.';
1278             p_buf[w/2+1] = '.';
1279         }
1280         char *psz_local = ToLocale( p_buf );
1281         mvprintw( y, x, "%s", psz_local );
1282         LocaleFree( p_buf );
1283     }
1284     else
1285     {
1286         char *psz_local = ToLocale( p_buf );
1287         mvprintw( y, x, "%s", psz_local );
1288         LocaleFree( p_buf );
1289         mvhline( y, x + i_len, ' ', w - i_len );
1290     }
1291 #endif
1292 }
1293 static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt, ... )
1294 {
1295     intf_sys_t     *p_sys = p_intf->p_sys;
1296
1297     va_list  vl_args;
1298     char    *p_buf = NULL;
1299
1300     if( l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_lines )
1301     {
1302         return;
1303     }
1304
1305     va_start( vl_args, p_fmt );
1306     if( vasprintf( &p_buf, p_fmt, vl_args ) == -1 )
1307         return;
1308     va_end( vl_args );
1309
1310     if( p_buf == NULL )
1311     {
1312         return;
1313     }
1314
1315     mvnprintw( p_sys->i_box_y + l - p_sys->i_box_start, x, COLS - x - 1, "%s", p_buf );
1316 }
1317
1318 static void Redraw( intf_thread_t *p_intf, time_t *t_last_refresh )
1319 {
1320     intf_sys_t     *p_sys = p_intf->p_sys;
1321     input_thread_t *p_input = p_sys->p_input;
1322     playlist_t     *p_playlist = pl_Get( p_intf );
1323     int y = 0;
1324     int h;
1325     int y_end;
1326
1327     clear();
1328
1329     /* Title */
1330     attrset( A_REVERSE );
1331     int i_len = strlen( "VLC media player [ h for help ]" );
1332     int mid = ( COLS - i_len ) / 2;
1333     if( mid < 0 )
1334         mid = 0;
1335     int i_size = ( COLS > i_len + 1 ) ? COLS : i_len + 1;
1336     char psz_title[i_size];
1337     memset( psz_title, ' ', mid );
1338     snprintf( &psz_title[mid], i_size, "VLC media player [ h for help ]" );
1339     mvnprintw( y, 0, COLS, "%s", psz_title );
1340     attroff( A_REVERSE );
1341     y += 2;
1342
1343     /* Infos */
1344
1345     char psz_state[25];
1346     /* strlen( "[Repeat] [Random] [Loop]" ) == 24, + '\0' */
1347     psz_state[0] = '\0';
1348     if( var_GetBool( p_playlist, "repeat" ) )
1349         strcat( psz_state, "[Repeat] " );
1350     if( var_GetBool( p_playlist, "random" ) )
1351         strcat( psz_state, "[Random] " );
1352     if( var_GetBool( p_playlist, "loop" ) )
1353         strcat( psz_state, "[Loop]" );
1354
1355     if( p_input && !p_input->b_dead )
1356     {
1357         char buf1[MSTRTIME_MAX_SIZE];
1358         char buf2[MSTRTIME_MAX_SIZE];
1359         vlc_value_t val;
1360         vlc_value_t val_list;
1361
1362         /* Source */
1363         char *psz_uri = input_item_GetURI( input_GetItem( p_input ) );
1364         mvnprintw( y++, 0, COLS, " Source   : %s", psz_uri );
1365         free( psz_uri );
1366
1367         /* State */
1368         var_Get( p_input, "state", &val );
1369         if( val.i_int == PLAYING_S )
1370         {
1371             mvnprintw( y++, 0, COLS, " State    : Playing %s", psz_state );
1372         }
1373         else if( val.i_int == OPENING_S )
1374         {
1375             mvnprintw( y++, 0, COLS, " State    : Opening/Connecting %s", psz_state );
1376         }
1377         else if( val.i_int == BUFFERING_S )
1378         {
1379             mvnprintw( y++, 0, COLS, " State    : Buffering %s", psz_state );
1380         }
1381         else if( val.i_int == PAUSE_S )
1382         {
1383             mvnprintw( y++, 0, COLS, " State    : Paused %s", psz_state );
1384         }
1385
1386         if( val.i_int != INIT_S && val.i_int != END_S )
1387         {
1388             audio_volume_t i_volume;
1389
1390             /* Position */
1391             var_Get( p_input, "time", &val );
1392             msecstotimestr( buf1, val.i_time / 1000 );
1393
1394             var_Get( p_input, "length", &val );
1395             msecstotimestr( buf2, val.i_time / 1000 );
1396
1397             mvnprintw( y++, 0, COLS, " Position : %s/%s (%.2f%%)", buf1, buf2, p_sys->f_slider );
1398
1399             /* Volume */
1400             aout_VolumeGet( p_intf, &i_volume );
1401             mvnprintw( y++, 0, COLS, " Volume   : %i%%", i_volume*200/AOUT_VOLUME_MAX );
1402
1403             /* Title */
1404             if( !var_Get( p_input, "title", &val ) )
1405             {
1406                 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
1407                 if( val_list.p_list->i_count > 0 )
1408                 {
1409                     mvnprintw( y++, 0, COLS, " Title    : %d/%d", val.i_int, val_list.p_list->i_count );
1410                 }
1411                 var_Change( p_input, "title", VLC_VAR_FREELIST, &val_list, NULL );
1412             }
1413
1414             /* Chapter */
1415             if( !var_Get( p_input, "chapter", &val ) )
1416             {
1417                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
1418                 if( val_list.p_list->i_count > 0 )
1419                 {
1420                     mvnprintw( y++, 0, COLS, " Chapter  : %d/%d", val.i_int, val_list.p_list->i_count );
1421                 }
1422                 var_Change( p_input, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
1423             }
1424         }
1425         else
1426         {
1427             y += 2;
1428         }
1429     }
1430     else
1431     {
1432         mvnprintw( y++, 0, COLS, "Source: <no current item> %s", psz_state );
1433         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1434         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1435         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1436     }
1437
1438     DrawBox( p_sys->w, y, 0, 3, COLS, "" );
1439     DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
1440     DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
1441     y += 3;
1442
1443     p_sys->i_box_y = y + 1;
1444     p_sys->i_box_lines = LINES - y - 2;
1445
1446     h = LINES - y;
1447     y_end = y + h - 1;
1448
1449     if( p_sys->i_box_type == BOX_HELP )
1450     {
1451         /* Help box */
1452         int l = 0;
1453         DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
1454
1455         MainBoxWrite( p_intf, l++, 1, "[Display]" );
1456         MainBoxWrite( p_intf, l++, 1, "     h,H         Show/Hide help box" );
1457         MainBoxWrite( p_intf, l++, 1, "     i           Show/Hide info box" );
1458         MainBoxWrite( p_intf, l++, 1, "     m           Show/Hide metadata box" );
1459         MainBoxWrite( p_intf, l++, 1, "     L           Show/Hide messages box" );
1460         MainBoxWrite( p_intf, l++, 1, "     P           Show/Hide playlist box" );
1461         MainBoxWrite( p_intf, l++, 1, "     B           Show/Hide filebrowser" );
1462         MainBoxWrite( p_intf, l++, 1, "" );
1463
1464         MainBoxWrite( p_intf, l++, 1, "[Global]" );
1465         MainBoxWrite( p_intf, l++, 1, "     q, Q        Quit" );
1466         MainBoxWrite( p_intf, l++, 1, "     s           Stop" );
1467         MainBoxWrite( p_intf, l++, 1, "     <space>     Pause/Play" );
1468         MainBoxWrite( p_intf, l++, 1, "     f           Toggle Fullscreen" );
1469         MainBoxWrite( p_intf, l++, 1, "     n, p        Next/Previous playlist item" );
1470         MainBoxWrite( p_intf, l++, 1, "     [, ]        Next/Previous title" );
1471         MainBoxWrite( p_intf, l++, 1, "     <, >        Next/Previous chapter" );
1472         MainBoxWrite( p_intf, l++, 1, "     <right>     Seek +1%%" );
1473         MainBoxWrite( p_intf, l++, 1, "     <left>      Seek -1%%" );
1474         MainBoxWrite( p_intf, l++, 1, "     a           Volume Up" );
1475         MainBoxWrite( p_intf, l++, 1, "     z           Volume Down" );
1476         MainBoxWrite( p_intf, l++, 1, "" );
1477
1478         MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
1479         MainBoxWrite( p_intf, l++, 1, "     r           Toggle Random playing" );
1480         MainBoxWrite( p_intf, l++, 1, "     l           Toggle Loop Playlist" );
1481         MainBoxWrite( p_intf, l++, 1, "     R           Toggle Repeat item" );
1482         MainBoxWrite( p_intf, l++, 1, "     o           Order Playlist by title" );
1483         MainBoxWrite( p_intf, l++, 1, "     O           Reverse order Playlist by title" );
1484         MainBoxWrite( p_intf, l++, 1, "     g           Go to the current playing item" );
1485         MainBoxWrite( p_intf, l++, 1, "     /           Look for an item" );
1486         MainBoxWrite( p_intf, l++, 1, "     A           Add an entry" );
1487         MainBoxWrite( p_intf, l++, 1, "     D, <del>    Delete an entry" );
1488         MainBoxWrite( p_intf, l++, 1, "     <backspace> Delete an entry" );
1489         MainBoxWrite( p_intf, l++, 1, "" );
1490
1491         MainBoxWrite( p_intf, l++, 1, "[Filebrowser]" );
1492         MainBoxWrite( p_intf, l++, 1, "     <enter>     Add the selected file to the playlist" );
1493         MainBoxWrite( p_intf, l++, 1, "     <space>     Add the selected directory to the playlist" );
1494         MainBoxWrite( p_intf, l++, 1, "     .           Show/Hide hidden files" );
1495         MainBoxWrite( p_intf, l++, 1, "" );
1496
1497         MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
1498         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Navigate through the box line by line" );
1499         MainBoxWrite( p_intf, l++, 1, "     <pgup>,<pgdown> Navigate through the box page by page" );
1500         MainBoxWrite( p_intf, l++, 1, "" );
1501
1502         MainBoxWrite( p_intf, l++, 1, "[Player]" );
1503         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Seek +/-5%%" );
1504         MainBoxWrite( p_intf, l++, 1, "" );
1505
1506         MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
1507         MainBoxWrite( p_intf, l++, 1, "     Ctrl-l          Refresh the screen" );
1508
1509         p_sys->i_box_lines_total = l;
1510         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1511         {
1512             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1513         }
1514
1515         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1516         {
1517             y += l - p_sys->i_box_start;
1518         }
1519         else
1520         {
1521             y += p_sys->i_box_lines;
1522         }
1523     }
1524     else if( p_sys->i_box_type == BOX_INFO )
1525     {
1526         /* Info box */
1527         int l = 0;
1528         DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1529
1530         if( p_input )
1531         {
1532             int i,j;
1533             vlc_mutex_lock( &input_GetItem(p_input)->lock );
1534             for( i = 0; i < input_GetItem(p_input)->i_categories; i++ )
1535             {
1536                 info_category_t *p_category = input_GetItem(p_input)->pp_categories[i];
1537                 if( y >= y_end ) break;
1538                 MainBoxWrite( p_intf, l++, 1, "  [%s]", p_category->psz_name );
1539                 for( j = 0; j < p_category->i_infos; j++ )
1540                 {
1541                     info_t *p_info = p_category->pp_infos[j];
1542                     if( y >= y_end ) break;
1543                     MainBoxWrite( p_intf, l++, 1, "      %s: %s", p_info->psz_name, p_info->psz_value );
1544                 }
1545             }
1546             vlc_mutex_unlock( &input_GetItem(p_input)->lock );
1547         }
1548         else
1549         {
1550             MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1551         }
1552         p_sys->i_box_lines_total = l;
1553         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1554         {
1555             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1556         }
1557
1558         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1559         {
1560             y += l - p_sys->i_box_start;
1561         }
1562         else
1563         {
1564             y += p_sys->i_box_lines;
1565         }
1566     }
1567     else if( p_sys->i_box_type == BOX_META )
1568     {
1569         /* Meta data box */
1570         int l = 0;
1571
1572         int i_len = strlen( VLC_META_INFO_CAT );
1573         char psz_title[i_len + 3];
1574         psz_title[0] = ' ';
1575         psz_title[1] = '\0';
1576         strcat( &psz_title[1], VLC_META_INFO_CAT );
1577         psz_title[i_len + 1] = ' ';
1578         psz_title[i_len + 2] = '\0';
1579         DrawBox( p_sys->w, y++, 0, h, COLS, psz_title );
1580
1581         if( p_input )
1582         {
1583             int i;
1584             input_item_t *p_item = input_GetItem( p_input );
1585             vlc_mutex_lock( &p_item->lock );
1586             for( i=0; i<VLC_META_TYPE_COUNT; i++ )
1587             {
1588                 if( y >= y_end ) break;
1589                 char *psz_meta = p_item->p_meta->ppsz_meta[i];
1590                 if( psz_meta && *psz_meta )
1591                 {
1592                     const char *psz_meta_title;
1593                     switch( i )
1594                     {
1595                         case 0:
1596                             psz_meta_title = VLC_META_TITLE; break;
1597                         case 1:
1598                             psz_meta_title = VLC_META_ARTIST; break;
1599                         case 2:
1600                             psz_meta_title = VLC_META_GENRE ; break;
1601                         case 3:
1602                             psz_meta_title = VLC_META_COPYRIGHT; break;
1603                         case 4:
1604                             psz_meta_title = VLC_META_COLLECTION; break;
1605                         case 5:
1606                             psz_meta_title = VLC_META_SEQ_NUM; break;
1607                         case 6:
1608                             psz_meta_title = VLC_META_DESCRIPTION; break;
1609                         case 7:
1610                             psz_meta_title = VLC_META_RATING; break;
1611                         case 8:
1612                             psz_meta_title = VLC_META_DATE; break;
1613                         case 9:
1614                             psz_meta_title = VLC_META_SETTING; break;
1615                         case 10:
1616                             psz_meta_title = VLC_META_URL; break;
1617                         case 11:
1618                             psz_meta_title = VLC_META_LANGUAGE; break;
1619                         case 12:
1620                             psz_meta_title = VLC_META_NOW_PLAYING; break;
1621                         case 13:
1622                             psz_meta_title = VLC_META_PUBLISHER; break;
1623                         case 14:
1624                             psz_meta_title = VLC_META_ENCODED_BY; break;
1625                         case 15:
1626                             psz_meta_title = VLC_META_ART_URL; break;
1627                         case 16:
1628                             psz_meta_title = VLC_META_TRACKID; break;
1629                         default:
1630                             psz_meta_title = ""; break;
1631                     }
1632                     MainBoxWrite( p_intf, l++, 1, "  [%s]", psz_meta_title );
1633                     MainBoxWrite( p_intf, l++, 1, "      %s", psz_meta );
1634                 }
1635             }
1636             vlc_mutex_unlock( &p_item->lock );
1637         }
1638         else
1639         {
1640             MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1641         }
1642         p_sys->i_box_lines_total = l;
1643         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1644         {
1645             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1646         }
1647
1648         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1649         {
1650             y += l - p_sys->i_box_start;
1651         }
1652         else
1653         {
1654             y += p_sys->i_box_lines;
1655         }
1656     }
1657     else if( p_sys->i_box_type == BOX_LOG )
1658     {
1659         int i_line = 0;
1660         int i_stop;
1661         int i_start;
1662
1663         DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1664
1665         i_start = p_intf->p_sys->p_sub->i_start;
1666
1667         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1668         i_stop = *p_intf->p_sys->p_sub->pi_stop;
1669         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1670
1671         for( ;; )
1672         {
1673             static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1674             if( i_line >= h - 2 )
1675             {
1676                 break;
1677             }
1678             i_stop--;
1679             i_line++;
1680             if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1681             if( i_stop == i_start )
1682             {
1683                 break;
1684             }
1685             mvnprintw( y + h-2-i_line, 1, COLS - 2, "   [%s] %s",
1686                       ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1687                       p_sys->p_sub->p_msg[i_stop].psz_msg );
1688         }
1689
1690         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1691         p_intf->p_sys->p_sub->i_start = i_stop;
1692         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1693         y = y_end;
1694     }
1695     else if( p_sys->i_box_type == BOX_BROWSE )
1696     {
1697         /* Filebrowser box */
1698         int        i_start, i_stop;
1699         int        i_item;
1700         DrawBox( p_sys->w, y++, 0, h, COLS, " Browse " );
1701
1702         if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
1703         if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
1704
1705         if( p_sys->i_box_bidx < (h - 2)/2 )
1706         {
1707             i_start = 0;
1708             i_stop = h - 2;
1709         }
1710         else if( p_sys->i_dir_entries - p_sys->i_box_bidx > (h - 2)/2 )
1711         {
1712             i_start = p_sys->i_box_bidx - (h - 2)/2;
1713             i_stop = i_start + h - 2;
1714         }
1715         else
1716         {
1717             i_stop = p_sys->i_dir_entries;
1718             i_start = p_sys->i_dir_entries - (h - 2);
1719         }
1720         if( i_start < 0 )
1721         {
1722             i_start = 0;
1723         }
1724         if( i_stop > p_sys->i_dir_entries )
1725         {
1726             i_stop = p_sys->i_dir_entries;
1727         }
1728
1729         for( i_item = i_start; i_item < i_stop; i_item++ )
1730         {
1731             vlc_bool_t b_selected = ( p_sys->i_box_bidx == i_item );
1732
1733             if( y >= y_end ) break;
1734             if( b_selected )
1735             {
1736                 attrset( A_REVERSE );
1737             }
1738             mvnprintw( y++, 1, COLS - 2, " %c %s", p_sys->pp_dir_entries[i_item]->b_file == VLC_TRUE ? ' ' : '+',
1739                             p_sys->pp_dir_entries[i_item]->psz_path );
1740             if( b_selected )
1741             {
1742                 attroff( A_REVERSE );
1743             }
1744         }
1745
1746     }
1747     else if( p_sys->i_box_type == BOX_PLAYLIST ||
1748                p_sys->i_box_type == BOX_SEARCH ||
1749                p_sys->i_box_type == BOX_OPEN   )
1750     {
1751         /* Playlist box */
1752         int        i_start, i_stop, i_max = p_sys->i_plist_entries;
1753         int        i_item;
1754         char       *psz_title;
1755
1756         switch( p_sys->i_current_view )
1757         {
1758             case VIEW_ONELEVEL:
1759                 psz_title = strdup( " Playlist (All, one level) " );
1760                 break;
1761             case VIEW_CATEGORY:
1762                 psz_title = strdup( " Playlist (By category) " );
1763                 break;
1764             default:
1765                 psz_title = strdup( " Playlist (Manually added) " );
1766         }
1767
1768         DrawBox( p_sys->w, y++, 0, h, COLS, psz_title );
1769
1770         if( p_sys->b_need_update || p_sys->pp_plist == NULL )
1771         {
1772             PlaylistRebuild( p_intf );
1773         }
1774         if( p_sys->b_box_plidx_follow )
1775         {
1776             FindIndex( p_intf );
1777         }
1778
1779         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1780         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1781         if( p_sys->i_box_plidx >= i_max ) p_sys->i_box_plidx = i_max - 1;
1782
1783         if( p_sys->i_box_plidx < (h - 2)/2 )
1784         {
1785             i_start = 0;
1786             i_stop = h - 2;
1787         }
1788         else if( i_max - p_sys->i_box_plidx > (h - 2)/2 )
1789         {
1790             i_start = p_sys->i_box_plidx - (h - 2)/2;
1791             i_stop = i_start + h - 2;
1792         }
1793         else
1794         {
1795             i_stop = i_max;
1796             i_start = i_max - (h - 2);
1797         }
1798         if( i_start < 0 )
1799         {
1800             i_start = 0;
1801         }
1802         if( i_stop > i_max )
1803         {
1804             i_stop = i_max;
1805         }
1806
1807         for( i_item = i_start; i_item < i_stop; i_item++ )
1808         {
1809             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1810             playlist_item_t *p_item = p_sys->pp_plist[i_item]->p_item;
1811             playlist_item_t *p_node = p_sys->p_node;
1812             int c = ' ';
1813             if( ( p_node && p_item->p_input == p_node->p_input ) ||
1814                         ( !p_node && p_item->p_input ==
1815                         p_playlist->status.p_node->p_input ) )
1816                 c = '*';
1817             else if( p_item == p_node || ( p_item != p_node &&
1818                         PlaylistIsPlaying( p_intf, p_item ) ) )
1819                 c = '>';
1820
1821             if( y >= y_end ) break;
1822             if( b_selected )
1823             {
1824                 attrset( A_REVERSE );
1825             }
1826             mvnprintw( y++, 1, COLS - 2, "%c%s", c,
1827                        p_sys->pp_plist[i_item]->psz_display );
1828             if( b_selected )
1829             {
1830                 attroff( A_REVERSE );
1831             }
1832         }
1833
1834     }
1835     else
1836     {
1837         y++;
1838     }
1839     if( p_sys->i_box_type == BOX_SEARCH )
1840     {
1841         DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1842         if( p_sys->psz_search_chain )
1843         {
1844             if( strlen( p_sys->psz_search_chain ) == 0 &&
1845                 p_sys->psz_old_search != NULL )
1846             {
1847                 /* Searching next entry */
1848                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1849             }
1850             else
1851             {
1852                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1853             }
1854         }
1855     }
1856     if( p_sys->i_box_type == BOX_OPEN )
1857     {
1858         if( p_sys->psz_open_chain )
1859         {
1860             DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1861             mvnprintw( 7, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1862         }
1863     }
1864
1865     while( y < y_end )
1866     {
1867         DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1868     }
1869
1870     refresh();
1871
1872     *t_last_refresh = time( 0 );
1873 }
1874
1875 static playlist_item_t *PlaylistGetRoot( intf_thread_t *p_intf )
1876 {
1877     intf_sys_t *p_sys = p_intf->p_sys;
1878     playlist_t *p_playlist = pl_Get( p_intf );
1879
1880     switch( p_sys->i_current_view )
1881     {
1882         case VIEW_CATEGORY:
1883             return p_playlist->p_root_category;
1884         default:
1885             return p_playlist->p_root_onelevel;
1886     }
1887 }
1888
1889 static void PlaylistRebuild( intf_thread_t *p_intf )
1890 {
1891     intf_sys_t *p_sys = p_intf->p_sys;
1892     playlist_t *p_playlist = pl_Get( p_intf );
1893
1894     PL_LOCK;
1895
1896     /* First clear the old one */
1897     PlaylistDestroy( p_intf );
1898
1899     /* Build the new one */
1900     PlaylistAddNode( p_intf, PlaylistGetRoot( p_intf ), 0, "" );
1901
1902     p_sys->b_need_update = VLC_FALSE;
1903
1904     PL_UNLOCK;
1905 }
1906
1907 static void PlaylistAddNode( intf_thread_t *p_intf, playlist_item_t *p_node,
1908                              int i, const char *c )
1909 {
1910     intf_sys_t *p_sys = p_intf->p_sys;
1911     playlist_item_t *p_child;
1912     int k;
1913
1914     for( k = 0; k < p_node->i_children; k++ )
1915     {
1916         char *psz_display;
1917         p_child = p_node->pp_children[k];
1918         char *psz_name = input_item_GetTitle( p_child->p_input );
1919         if( !psz_name || !*psz_name )
1920         {
1921             free( psz_name );
1922             psz_name = input_item_GetName( p_child->p_input );
1923         }
1924
1925         if( c && *c )
1926         {
1927             if( asprintf( &psz_display, "%s%c-%s", c,
1928                     k == p_node->i_children - 1 ?  '`' : '|', psz_name ) == -1 )
1929                 return;
1930         }
1931         else
1932         {
1933             if( asprintf( &psz_display, " %s", psz_name ) == -1 )
1934                 return;
1935         }
1936         free( psz_name );
1937         struct pl_item_t *p_pl_item = malloc( sizeof( struct pl_item_t ) );
1938         if( !p_pl_item )
1939             return;
1940         p_pl_item->psz_display = psz_display;
1941         p_pl_item->p_item = p_child;
1942         INSERT_ELEM( p_sys->pp_plist, p_sys->i_plist_entries,
1943                      p_sys->i_plist_entries, p_pl_item );
1944         i++;
1945
1946         if( p_child->i_children > 0 )
1947         {
1948             char *psz_tmp;
1949             if( asprintf( &psz_tmp, "%s%c ", c,
1950                      k == p_node->i_children - 1 ? ' ' : '|' ) == -1 )
1951                 return; 
1952             PlaylistAddNode( p_intf, p_child, i,
1953                              strlen( c ) ? psz_tmp : " " );
1954             free( psz_tmp );
1955         }
1956     }
1957 }
1958
1959 static int PlaylistChanged( vlc_object_t *p_this, const char *psz_variable,
1960                             vlc_value_t oval, vlc_value_t nval, void *param )
1961 {
1962     intf_thread_t *p_intf = (intf_thread_t *)param;
1963     playlist_t *p_playlist = pl_Get( p_intf );
1964     p_intf->p_sys->b_need_update = VLC_TRUE;
1965     p_intf->p_sys->p_node = p_playlist->status.p_node;
1966     return VLC_SUCCESS;
1967 }
1968
1969 /* Playlist suxx */
1970 static inline vlc_bool_t PlaylistIsPlaying( intf_thread_t *p_intf,
1971                                             playlist_item_t *p_item )
1972 {
1973     playlist_t *p_playlist = pl_Get( p_intf );
1974     playlist_item_t *p_played_item = p_playlist->status.p_item;
1975     return( p_item != NULL && p_played_item != NULL &&
1976             p_item->p_input != NULL && p_played_item->p_input != NULL &&
1977             p_item->p_input->i_id == p_played_item->p_input->i_id );
1978 }
1979
1980 static void FindIndex( intf_thread_t *p_intf )
1981 {
1982     intf_sys_t *p_sys = p_intf->p_sys;
1983     int i;
1984
1985     if( p_sys->i_box_plidx < p_sys->i_plist_entries && p_sys->i_box_plidx >= 0 )
1986     {
1987         playlist_item_t *p_item = p_sys->pp_plist[p_sys->i_box_plidx]->p_item;
1988         if( ( p_item->i_children == 0 && p_item == p_sys->p_node ) ||
1989                 PlaylistIsPlaying( p_intf, p_item ) )
1990             return;
1991     }
1992
1993     for( i = 0; i < p_sys->i_plist_entries; i++ )
1994     {
1995         playlist_item_t *p_item = p_sys->pp_plist[i]->p_item;
1996         if( ( p_item->i_children == 0 && p_item == p_sys->p_node ) ||
1997                 PlaylistIsPlaying( p_intf, p_sys->pp_plist[i]->p_item ) )
1998         {
1999             p_sys->i_box_plidx = i;
2000             break;
2001         }
2002     }
2003 }
2004
2005 static void PlaylistDestroy( intf_thread_t *p_intf )
2006 {
2007     intf_sys_t *p_sys = p_intf->p_sys;
2008     int i;
2009
2010     for( i = 0; i < p_sys->i_plist_entries; i++ )
2011     {
2012         struct pl_item_t *p_pl_item = p_sys->pp_plist[i];
2013         free( p_pl_item->psz_display );
2014         REMOVE_ELEM( p_sys->pp_plist, p_sys->i_plist_entries, i );
2015         free( p_pl_item );
2016     }
2017     p_sys->pp_plist = NULL;
2018     p_sys->i_plist_entries = 0;
2019 }
2020
2021 static void Eject( intf_thread_t *p_intf )
2022 {
2023     char *psz_device = NULL, *psz_parser, *psz_name;
2024
2025     /*
2026      * Get the active input
2027      * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
2028      * If it's neither of these, then return
2029      */
2030
2031     playlist_t * p_playlist = pl_Get( p_intf );
2032     PL_LOCK;
2033
2034     if( p_playlist->status.p_item == NULL )
2035     {
2036         PL_UNLOCK;
2037         return;
2038     }
2039
2040     psz_name = p_playlist->status.p_item->p_input->psz_name;
2041
2042     if( psz_name )
2043     {
2044         if( !strncmp(psz_name, "dvd://", 4) )
2045         {
2046             switch( psz_name[strlen("dvd://")] )
2047             {
2048             case '\0':
2049             case '@':
2050                 psz_device = config_GetPsz( p_intf, "dvd" );
2051                 break;
2052             default:
2053                 /* Omit the first MRL-selector characters */
2054                 psz_device = strdup( psz_name + strlen("dvd://" ) );
2055                 break;
2056             }
2057         }
2058         else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
2059         {
2060             switch( psz_name[strlen(VCD_MRL)] )
2061             {
2062             case '\0':
2063             case '@':
2064                 psz_device = config_GetPsz( p_intf, VCD_MRL );
2065                 break;
2066             default:
2067                 /* Omit the beginning MRL-selector characters */
2068                 psz_device = strdup( psz_name + strlen(VCD_MRL) );
2069                 break;
2070             }
2071         }
2072         else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
2073         {
2074             switch( psz_name[strlen(CDDA_MRL)] )
2075             {
2076             case '\0':
2077             case '@':
2078                 psz_device = config_GetPsz( p_intf, "cd-audio" );
2079                 break;
2080             default:
2081                 /* Omit the beginning MRL-selector characters */
2082                 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
2083                 break;
2084             }
2085         }
2086         else
2087         {
2088             psz_device = strdup( psz_name );
2089         }
2090     }
2091
2092     PL_UNLOCK;
2093
2094     if( psz_device == NULL )
2095     {
2096         return;
2097     }
2098
2099     /* Remove what we have after @ */
2100     psz_parser = psz_device;
2101     for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
2102     {
2103         if( *psz_parser == '@' )
2104         {
2105             *psz_parser = '\0';
2106             break;
2107         }
2108     }
2109
2110     /* If there's a stream playing, we aren't allowed to eject ! */
2111     if( p_intf->p_sys->p_input == NULL )
2112     {
2113         msg_Dbg( p_intf, "ejecting %s", psz_device );
2114
2115         intf_Eject( p_intf, psz_device );
2116     }
2117
2118     free(psz_device);
2119     return;
2120 }
2121
2122 static int comp_dir_entries( const void *pp_dir_entry1,
2123                              const void *pp_dir_entry2 )
2124 {
2125     struct dir_entry_t *p_dir_entry1 = *(struct dir_entry_t**)pp_dir_entry1;
2126     struct dir_entry_t *p_dir_entry2 = *(struct dir_entry_t**)pp_dir_entry2;
2127     if ( p_dir_entry1->b_file == p_dir_entry2->b_file ) {
2128         return strcasecmp( p_dir_entry1->psz_path, p_dir_entry2->psz_path );
2129     }
2130     else
2131     {
2132         return ( p_dir_entry1->b_file ? 1 : -1 );
2133     }
2134 }
2135
2136 static void ReadDir( intf_thread_t *p_intf )
2137 {
2138     intf_sys_t *p_sys = p_intf->p_sys;
2139     DIR *p_current_dir;
2140     int i;
2141
2142     if( p_sys->psz_current_dir && *p_sys->psz_current_dir )
2143     {
2144         char *psz_entry;
2145
2146         /* Open the dir */
2147         p_current_dir = utf8_opendir( p_sys->psz_current_dir );
2148
2149         if( p_current_dir == NULL )
2150         {
2151             /* something went bad, get out of here ! */
2152             msg_Warn( p_intf, "cannot open directory `%s' (%m)",
2153                       p_sys->psz_current_dir );
2154             return;
2155         }
2156
2157         /* Clean the old shit */
2158         for( i = 0; i < p_sys->i_dir_entries; i++ )
2159         {
2160             struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
2161             free( p_dir_entry->psz_path );
2162             REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
2163             free( p_dir_entry );
2164         }
2165         p_sys->pp_dir_entries = NULL;
2166         p_sys->i_dir_entries = 0;
2167
2168         /* while we still have entries in the directory */
2169         while( ( psz_entry = utf8_readdir( p_current_dir ) ) != NULL )
2170         {
2171 #if defined( S_ISDIR )
2172             struct stat stat_data;
2173 #endif
2174             struct dir_entry_t *p_dir_entry;
2175             int i_size_entry = strlen( p_sys->psz_current_dir ) +
2176                                strlen( psz_entry ) + 2;
2177             char *psz_uri;
2178
2179             if( p_sys->b_show_hidden_files == VLC_FALSE &&
2180                 ( strlen( psz_entry ) && psz_entry[0] == '.' ) &&
2181                 strcmp( psz_entry, ".." ) )
2182             {
2183                 free( psz_entry );
2184                 continue;
2185             }
2186
2187             psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
2188             sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, psz_entry );
2189
2190             if( !( p_dir_entry = malloc( sizeof( struct dir_entry_t) ) ) )
2191             {
2192                 free( psz_uri );
2193                 free( psz_entry );
2194                 continue;
2195             }
2196
2197 #if defined( S_ISDIR )
2198             if( !utf8_stat( psz_uri, &stat_data )
2199              && S_ISDIR(stat_data.st_mode) )
2200 /*#elif defined( DT_DIR )
2201             if( p_dir_content->d_type & DT_DIR )*/
2202 #else
2203             if( 0 )
2204 #endif
2205             {
2206                 p_dir_entry->psz_path = strdup( psz_entry );
2207                 p_dir_entry->b_file = VLC_FALSE;
2208                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2209                      p_sys->i_dir_entries, p_dir_entry );
2210             }
2211             else
2212             {
2213                 p_dir_entry->psz_path = strdup( psz_entry );
2214                 p_dir_entry->b_file = VLC_TRUE;
2215                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2216                      p_sys->i_dir_entries, p_dir_entry );
2217             }
2218
2219             free( psz_uri );
2220             free( psz_entry );
2221         }
2222
2223         /* Sort */
2224         qsort( p_sys->pp_dir_entries, p_sys->i_dir_entries,
2225                sizeof(struct dir_entry_t*), &comp_dir_entries );
2226
2227         closedir( p_current_dir );
2228         return;
2229     }
2230     else
2231     {
2232         msg_Dbg( p_intf, "no current dir set" );
2233         return;
2234     }
2235 }
2236
2237 static void PlayPause( intf_thread_t *p_intf )
2238 {
2239     input_thread_t *p_input = p_intf->p_sys->p_input;
2240     playlist_t *p_playlist = pl_Get( p_intf );
2241     vlc_value_t val;
2242
2243     if( p_input )
2244     {
2245         var_Get( p_input, "state", &val );
2246         if( val.i_int != PAUSE_S )
2247         {
2248             val.i_int = PAUSE_S;
2249         }
2250         else
2251         {
2252             val.i_int = PLAYING_S;
2253         }
2254         var_Set( p_input, "state", val );
2255     }
2256     else
2257         playlist_Play( p_playlist );
2258 }
2259
2260 /****************************************************************************
2261  *
2262  ****************************************************************************/
2263 static void DrawBox( WINDOW *win, int y, int x, int h, int w, const char *title )
2264 {
2265     int i;
2266     int i_len;
2267
2268     if( w > 3 && h > 2 )
2269     {
2270         if( title == NULL ) title = "";
2271         i_len = strlen( title );
2272
2273         if( i_len > w - 2 ) i_len = w - 2;
2274
2275         mvwaddch( win, y, x,    ACS_ULCORNER );
2276         mvwhline( win, y, x+1,  ACS_HLINE, ( w-i_len-2)/2 );
2277         mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
2278         mvwhline( win, y, x+(w-i_len)/2+i_len,  ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
2279         mvwaddch( win, y, x+w-1,ACS_URCORNER );
2280
2281         for( i = 0; i < h-2; i++ )
2282         {
2283             mvwaddch( win, y+i+1, x,     ACS_VLINE );
2284             mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
2285         }
2286
2287         mvwaddch( win, y+h-1, x,     ACS_LLCORNER );
2288         mvwhline( win, y+h-1, x+1,   ACS_HLINE, w - 2 );
2289         mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
2290     }
2291 }
2292
2293 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
2294 {
2295     if( w > 0 )
2296     {
2297         mvhline( y, x, ' ', w );
2298     }
2299 }
2300
2301 static void DrawLine( WINDOW *win, int y, int x, int w )
2302 {
2303     if( w > 0 )
2304     {
2305         attrset( A_REVERSE );
2306         mvhline( y, x, ' ', w );
2307         attroff( A_REVERSE );
2308     }
2309 }