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