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