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