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