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