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