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