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