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