]> git.sesse.net Git - vlc/blob - modules/gui/ncurses.c
Improvements to preferences
[vlc] / modules / gui / ncurses.c
1 /*****************************************************************************
2  * ncurses.c : NCurses plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                      /* malloc(), free() */
31 #include <string.h>
32 #include <errno.h>                                                 /* ENOMEM */
33 #include <stdio.h>
34 #include <time.h>
35
36 #include <curses.h>
37
38 #include <vlc/vlc.h>
39 #include <vlc/intf.h>
40 #include <vlc/vout.h>
41 #include <vlc/aout.h>
42
43 #ifdef HAVE_SYS_STAT_H
44 #   include <sys/stat.h>
45 #endif
46 #if (!defined( WIN32 ) || defined(__MINGW32__))
47 /* Mingw has its own version of dirent */
48 #   include <dirent.h>
49 #endif
50
51 #ifdef HAVE_CDDAX
52 #define CDDA_MRL "cddax://"
53 #else
54 #define CDDA_MRL "cdda://"
55 #endif
56
57 #ifdef HAVE_VCDX
58 #define VCD_MRL "vcdx://"
59 #else
60 #define VCD_MRL "vcdx://"
61 #endif
62
63 #define SEARCH_CHAIN_SIZE 20
64 #define OPEN_CHAIN_SIZE 50
65
66 /*****************************************************************************
67  * Local prototypes.
68  *****************************************************************************/
69 static int  Open           ( vlc_object_t * );
70 static void Close          ( vlc_object_t * );
71
72 static void Run            ( intf_thread_t * );
73 static void PlayPause      ( intf_thread_t * );
74 static void Eject          ( intf_thread_t * );
75
76 static int  HandleKey      ( intf_thread_t *, int );
77 static void Redraw         ( intf_thread_t *, time_t * );
78 static void SearchPlaylist ( intf_thread_t *, char * );
79 static void ManageSlider   ( intf_thread_t * );
80 static void ReadDir        ( intf_thread_t * );
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85
86 #define BROWSE_TEXT N_("Filebrowser starting point")
87 #define BROWSE_LONGTEXT N_( \
88     "This option allows you to specify the directory the ncurses filebrowser " \
89     "will show you initially.")
90
91 vlc_module_begin();
92     set_description( _("ncurses interface") );
93     set_capability( "interface", 10 );
94     set_category( CAT_INTERFACE );
95     set_subcategory( SUBCAT_INTERFACE_GENERAL );
96     set_callbacks( Open, Close );
97     add_shortcut( "curses" );
98     add_directory( "browse-dir", NULL, NULL, BROWSE_TEXT, BROWSE_LONGTEXT, VLC_FALSE );
99 vlc_module_end();
100
101 /*****************************************************************************
102  * intf_sys_t: description and status of ncurses interface
103  *****************************************************************************/
104 enum
105 {
106     BOX_NONE,
107     BOX_HELP,
108     BOX_INFO,
109     BOX_LOG,
110     BOX_PLAYLIST,
111     BOX_SEARCH,
112     BOX_OPEN,
113     BOX_BROWSE
114 };
115 struct dir_entry_t
116 {
117     vlc_bool_t  b_file;
118     char        *psz_path;
119 };
120 struct intf_sys_t
121 {
122     playlist_t     *p_playlist;
123     input_thread_t *p_input;
124
125     float           f_slider;
126     float           f_slider_old;
127
128     WINDOW          *w;
129
130     int             i_box_type;
131     int             i_box_y;
132     int             i_box_lines;
133     int             i_box_lines_total;
134     int             i_box_start;
135
136     int             i_box_plidx;    /* Playlist index */
137     int             b_box_plidx_follow;
138     int             i_box_bidx;    /* browser index */
139
140     int             b_box_cleared;
141
142     msg_subscription_t* p_sub;                  /* message bank subscription */
143
144     char            *psz_search_chain;          /* for playlist searching    */
145     char            *psz_old_search;            /* for searching next        */
146     int             i_before_search;
147
148     char            *psz_open_chain;
149     
150     char            *psz_current_dir;
151     int             i_dir_entries;
152     struct dir_entry_t  **pp_dir_entries;
153 };
154
155 static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title );
156 static void DrawLine( WINDOW *win, int y, int x, int w );
157 static void DrawEmptyLine( WINDOW *win, int y, int x, int w );
158
159 /*****************************************************************************
160  * Open: initialize and create window
161  *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
163 {
164     intf_thread_t *p_intf = (intf_thread_t *)p_this;
165     intf_sys_t    *p_sys;
166     vlc_value_t    val;
167
168     /* Allocate instance and initialize some members */
169     p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
170     p_sys->p_playlist = NULL;
171     p_sys->p_input = NULL;
172     p_sys->f_slider = 0.0;
173     p_sys->f_slider_old = 0.0;
174     p_sys->i_box_type = BOX_PLAYLIST;
175     p_sys->i_box_lines = 0;
176     p_sys->i_box_start= 0;
177     p_sys->i_box_lines_total = 0;
178     p_sys->b_box_plidx_follow = VLC_TRUE;
179     p_sys->b_box_cleared = VLC_FALSE;
180     p_sys->i_box_plidx = 0;
181     p_sys->i_box_bidx = 0;
182     p_sys->p_sub = msg_Subscribe( p_intf );
183
184     /* Initialize the curses library */
185     p_sys->w = initscr();
186     keypad( p_sys->w, TRUE );
187     /* Don't do NL -> CR/NL */
188     nonl();
189     /* Take input chars one at a time */
190     cbreak();
191     /* Don't echo */
192     noecho();
193
194     curs_set(0);
195     timeout(0);
196
197     clear();
198
199     /* exported function */
200     p_intf->pf_run = Run;
201
202     /* Set quiet mode */
203     val.i_int = -1;
204     var_Set( p_intf->p_vlc, "verbose", val );
205
206     /* Initialize search chain */
207     p_sys->psz_search_chain = (char *)malloc( SEARCH_CHAIN_SIZE + 1 );
208     p_sys->psz_old_search = NULL;
209     p_sys->i_before_search = 0;
210
211     /* Initialize open chain */
212     p_sys->psz_open_chain = (char *)malloc( OPEN_CHAIN_SIZE + 1 );
213     
214     /* Initialize browser options */
215     var_Create( p_intf, "browse-dir", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
216     var_Get( p_intf, "browse-dir", &val);
217     
218     if( val.psz_string && *val.psz_string )
219     {
220         p_sys->psz_current_dir = strdup( val.psz_string);
221         free( val.psz_string );
222     }
223     else
224     {
225         p_sys->psz_current_dir = strdup( p_intf->p_vlc->psz_homedir );
226     }
227     
228     p_sys->i_dir_entries = 0;
229     p_sys->pp_dir_entries  = NULL;
230     ReadDir( p_intf );
231
232     return VLC_SUCCESS;
233 }
234
235 /*****************************************************************************
236  * Close: destroy interface window
237  *****************************************************************************/
238 static void Close( vlc_object_t *p_this )
239 {
240     intf_thread_t *p_intf = (intf_thread_t *)p_this;
241     intf_sys_t    *p_sys = p_intf->p_sys;
242     int i;
243
244     for( i = 0; i < p_sys->i_dir_entries; i++ )
245     {
246         struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
247         if( p_dir_entry->psz_path ) free( p_dir_entry->psz_path );
248         REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
249         if( p_dir_entry ) free( p_dir_entry );
250     }
251     p_sys->pp_dir_entries = NULL;
252     
253     if( p_sys->psz_current_dir ) free( p_sys->psz_current_dir );
254     if( p_sys->psz_search_chain ) free( p_sys->psz_search_chain );
255     if( p_sys->psz_old_search ) free( p_sys->psz_old_search );
256     if( p_sys->psz_open_chain ) free( p_sys->psz_open_chain );
257
258     if( p_sys->p_input )
259     {
260         vlc_object_release( p_sys->p_input );
261     }
262     if( p_sys->p_playlist )
263     {
264         vlc_object_release( p_sys->p_playlist );
265     }
266
267     /* Close the ncurses interface */
268     endwin();
269
270     msg_Unsubscribe( p_intf, p_sys->p_sub );
271
272     /* Destroy structure */
273     free( p_sys );
274 }
275
276 /*****************************************************************************
277  * Run: ncurses thread
278  *****************************************************************************/
279 static void Run( intf_thread_t *p_intf )
280 {
281     intf_sys_t    *p_sys = p_intf->p_sys;
282
283     int i_key;
284     time_t t_last_refresh;
285
286     /*
287      * force drawing the interface for the first time
288      */
289     t_last_refresh = ( time( 0 ) - 1);
290
291     while( !p_intf->b_die )
292     {
293         msleep( INTF_IDLE_SLEEP );
294
295         /* Update the input */
296         if( p_sys->p_playlist == NULL )
297         {
298             p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
299         }
300         if( p_sys->p_playlist )
301         {
302             vlc_mutex_lock( &p_sys->p_playlist->object_lock );
303             if( p_sys->p_input == NULL )
304             {
305                 p_sys->p_input = p_sys->p_playlist->p_input;
306                 if( p_sys->p_input )
307                 {
308                     if( !p_sys->p_input->b_dead )
309                     {
310                         vlc_object_yield( p_sys->p_input );
311                     }
312                 }
313             }
314             else if( p_sys->p_input->b_dead )
315             {
316                 vlc_object_release( p_sys->p_input );
317                 p_sys->p_input = NULL;
318                 p_sys->f_slider = p_sys->f_slider_old = 0.0;
319                 p_sys->b_box_cleared = VLC_FALSE;
320             }
321             vlc_mutex_unlock( &p_sys->p_playlist->object_lock );
322         }
323
324         if( p_sys->b_box_plidx_follow && p_sys->p_playlist->i_index >= 0 )
325         {
326             p_sys->i_box_plidx = p_sys->p_playlist->i_index;
327         }
328
329         while( ( i_key = getch()) != -1 )
330         {
331             /*
332              * HandleKey returns 1 if the screen needs to be redrawn
333              */
334             if ( HandleKey( p_intf, i_key ) )
335             {
336                 Redraw( p_intf, &t_last_refresh );
337             }
338         }
339         /* Hack */
340         if( p_sys->f_slider > 0.0001 && !p_sys->b_box_cleared )
341         {
342             clear();
343             Redraw( p_intf, &t_last_refresh );
344             p_sys->b_box_cleared = VLC_TRUE;
345         }
346
347         /*
348          * redraw the screen every second
349          */
350         if ( (time(0) - t_last_refresh) >= 1 )
351         {
352             ManageSlider ( p_intf );
353             Redraw( p_intf, &t_last_refresh );
354         }
355     }
356 }
357
358 /* following functions are local */
359
360 static int HandleKey( intf_thread_t *p_intf, int i_key )
361 {
362     intf_sys_t *p_sys = p_intf->p_sys;
363     vlc_value_t val;
364
365     if( p_sys->i_box_type == BOX_PLAYLIST && p_sys->p_playlist )
366     {
367         int b_ret = VLC_TRUE;
368
369         switch( i_key )
370         {
371             vlc_value_t val;
372             /* Playlist Settings */
373             case 'r':
374                 var_Get( p_sys->p_playlist, "random", &val );
375                 val.b_bool = !val.b_bool;
376                 var_Set( p_sys->p_playlist, "random", val );
377                 return 1;
378             case 'l':
379                 var_Get( p_sys->p_playlist, "loop", &val );
380                 val.b_bool = !val.b_bool;
381                 var_Set( p_sys->p_playlist, "loop", val );
382                 return 1;
383             case 'R':
384                 var_Get( p_sys->p_playlist, "repeat", &val );
385                 val.b_bool = !val.b_bool;
386                 var_Set( p_sys->p_playlist, "repeat", val );
387                 return 1;
388
389             /* Playlist sort */
390             case 'o':
391                 playlist_Sort( p_sys->p_playlist, SORT_TITLE, ORDER_NORMAL );
392                 return 1;
393             case 'O':
394                 playlist_Sort( p_sys->p_playlist, SORT_TITLE, ORDER_REVERSE );
395                 return 1;
396
397             /* Playlist navigation */
398             case KEY_HOME:
399                 p_sys->i_box_plidx = 0;
400                 break;
401             case KEY_END:
402                 p_sys->i_box_plidx = p_sys->p_playlist->i_size - 1;
403                 break;
404             case KEY_UP:
405                 p_sys->i_box_plidx--;
406                 break;
407             case KEY_DOWN:
408                 p_sys->i_box_plidx++;
409                 break;
410             case KEY_PPAGE:
411                 p_sys->i_box_plidx -= p_sys->i_box_lines;
412                 break;
413             case KEY_NPAGE:
414                 p_sys->i_box_plidx += p_sys->i_box_lines;
415                 break;
416             case 'D':
417             case KEY_BACKSPACE:
418             case KEY_DC:
419             {
420                 int i_item = p_sys->p_playlist->i_index;
421
422                 playlist_Delete( p_sys->p_playlist, p_sys->i_box_plidx );
423                 if( i_item < p_sys->p_playlist->i_size && i_item != p_sys->p_playlist->i_index )
424                 {
425                     playlist_Goto( p_sys->p_playlist, i_item );
426                 }
427                 break;
428             }
429
430             case KEY_ENTER:
431             case 0x0d:
432                 playlist_Goto( p_sys->p_playlist, p_sys->i_box_plidx );
433                 break;
434             default:
435                 b_ret = VLC_FALSE;
436                 break;
437         }
438
439         if( b_ret )
440         {
441             if( p_sys->i_box_plidx >= p_sys->p_playlist->i_size ) p_sys->i_box_plidx = p_sys->p_playlist->i_size - 1;
442             if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
443             if( p_sys->i_box_plidx == p_sys->p_playlist->i_index )
444                 p_sys->b_box_plidx_follow = VLC_TRUE;
445             else
446                 p_sys->b_box_plidx_follow = VLC_FALSE;
447             return 1;
448         }
449     }
450     if( p_sys->i_box_type == BOX_BROWSE )
451     {
452         vlc_bool_t b_ret = VLC_TRUE;
453         /* Browser navigation */
454         switch( i_key )
455         {
456             case KEY_HOME:
457                 p_sys->i_box_bidx = 0;
458                 break;
459             case KEY_END:
460                 p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
461                 break;
462             case KEY_UP:
463                 p_sys->i_box_bidx--;
464                 break;
465             case KEY_DOWN:
466                 p_sys->i_box_bidx++;
467                 break;
468             case KEY_PPAGE:
469                 p_sys->i_box_bidx -= p_sys->i_box_lines;
470                 break;
471             case KEY_NPAGE:
472                 p_sys->i_box_bidx += p_sys->i_box_lines;
473                 break;
474
475             case KEY_ENTER:
476             case 0x0d:
477                 if( p_sys->pp_dir_entries[p_sys->i_box_bidx]->b_file )
478                 {
479                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
480                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
481                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
482
483                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
484                     playlist_Add( p_sys->p_playlist, psz_uri,
485                                   psz_uri,
486                                   PLAYLIST_APPEND, PLAYLIST_END );
487                     p_sys->i_box_type = BOX_PLAYLIST;
488                     free( psz_uri );
489                 }
490                 else
491                 {
492                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
493                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
494                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
495
496                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
497                     
498                     p_sys->psz_current_dir = strdup( psz_uri );
499                     ReadDir( p_intf );
500                     free( psz_uri );
501                 }
502                 break;
503             default:
504                 b_ret = VLC_FALSE;
505                 break;
506         }
507         if( b_ret )
508         {
509             if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
510             if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
511             return 1;
512         }
513     }
514     else if( p_sys->i_box_type == BOX_HELP || p_sys->i_box_type == BOX_INFO )
515     {
516         switch( i_key )
517         {
518             case KEY_HOME:
519                 p_sys->i_box_start = 0;
520                 return 1;
521             case KEY_END:
522                 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
523                 return 1;
524             case KEY_UP:
525                 if( p_sys->i_box_start > 0 ) p_sys->i_box_start--;
526                 return 1;
527             case KEY_DOWN:
528                 if( p_sys->i_box_start < p_sys->i_box_lines_total - 1 )
529                 {
530                     p_sys->i_box_start++;
531                 }
532                 return 1;
533             case KEY_PPAGE:
534                 p_sys->i_box_start -= p_sys->i_box_lines;
535                 if( p_sys->i_box_start < 0 ) p_sys->i_box_start = 0;
536                 return 1;
537             case KEY_NPAGE:
538                 p_sys->i_box_start += p_sys->i_box_lines;
539                 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
540                 {
541                     p_sys->i_box_start = p_sys->i_box_lines_total - 1;
542                 }
543                 return 1;
544             default:
545                 break;
546         }
547     }
548     else if( p_sys->i_box_type == BOX_NONE )
549     {
550         switch( i_key )
551         {
552             case KEY_HOME:
553                 p_sys->f_slider = 0;
554                 ManageSlider ( p_intf );
555                 return 1;
556             case KEY_END:
557                 p_sys->f_slider = 99.9;
558                 ManageSlider ( p_intf );
559                 return 1;
560             case KEY_UP:
561                 p_sys->f_slider += 5.0;
562                 if( p_sys->f_slider >= 99.0 ) p_sys->f_slider = 99.0;
563                 ManageSlider ( p_intf );
564                 return 1;
565             case KEY_DOWN:
566                 p_sys->f_slider -= 5.0;
567                 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
568                 ManageSlider ( p_intf );
569                 return 1;
570
571             default:
572                 break;
573         }
574     }
575     else if( p_sys->i_box_type == BOX_SEARCH && p_sys->psz_search_chain )
576     {
577         int i_chain_len;
578         i_chain_len = strlen( p_sys->psz_search_chain );
579         switch( i_key )
580         {
581             case 0x0c:      /* ^l */
582                 clear();
583                 return 1;
584             case KEY_ENTER:
585             case 0x0d:
586                 if( i_chain_len > 0 )
587                 {
588                     p_sys->psz_old_search = strdup( p_sys->psz_search_chain );
589                 }
590                 else if( p_sys->psz_old_search )
591                 {
592                     SearchPlaylist( p_intf, p_sys->psz_old_search );
593                 }
594                 p_sys->i_box_type = BOX_PLAYLIST;
595                 return 1;
596             case 0x1b:      /* Esc. */
597                 p_sys->i_box_plidx = p_sys->i_before_search;
598                 p_sys->i_box_type = BOX_PLAYLIST;
599                 return 1;
600             case KEY_BACKSPACE:
601                 if( i_chain_len > 0 )
602                 {
603                     p_sys->psz_search_chain[ i_chain_len - 1 ] = '\0';
604                 }
605                 break;
606             default:
607                 if( i_chain_len < SEARCH_CHAIN_SIZE )
608                 {
609                     p_sys->psz_search_chain[ i_chain_len++ ] = i_key;
610                     p_sys->psz_search_chain[ i_chain_len ] = 0;
611                 }
612                 break;
613         }
614         if( p_sys->psz_old_search )
615         {
616             free( p_sys->psz_old_search );
617             p_sys->psz_old_search = NULL;
618         }
619         SearchPlaylist( p_intf, p_sys->psz_search_chain );
620         return 1;
621     }
622     else if( p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain )
623     {
624         int i_chain_len = strlen( p_sys->psz_open_chain );
625         playlist_t *p_playlist = p_sys->p_playlist;
626
627         switch( i_key )
628         {
629             case 0x0c:      /* ^l */
630                 clear();
631                 return 1;
632             case KEY_ENTER:
633             case 0x0d:
634                 if( p_playlist && i_chain_len > 0 )
635                 {
636                     playlist_Add( p_playlist, p_sys->psz_open_chain,
637                                   p_sys->psz_open_chain,
638                                   PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
639                     p_sys->b_box_plidx_follow = VLC_TRUE;
640                 }
641                 p_sys->i_box_type = BOX_PLAYLIST;
642                 return 1;
643             case 0x1b:      /* Esc. */
644                 p_sys->i_box_type = BOX_PLAYLIST;
645                 return 1;
646             case KEY_BACKSPACE:
647                 if( i_chain_len > 0 )
648                 {
649                     p_sys->psz_open_chain[ i_chain_len - 1 ] = '\0';
650                 }
651                 break;
652             default:
653                 if( i_chain_len < OPEN_CHAIN_SIZE )
654                 {
655                     p_sys->psz_open_chain[ i_chain_len++ ] = i_key;
656                     p_sys->psz_open_chain[ i_chain_len ] = 0;
657                 }
658                 break;
659         }
660         return 1;
661     }
662
663
664     /* Common keys */
665     switch( i_key )
666     {
667         case 'q':
668         case 'Q':
669         case 0x1b:  /* Esc */
670             p_intf->p_vlc->b_die = VLC_TRUE;
671             return 0;
672
673         /* Box switching */
674         case 'i':
675             if( p_sys->i_box_type == BOX_INFO )
676                 p_sys->i_box_type = BOX_NONE;
677             else
678                 p_sys->i_box_type = BOX_INFO;
679             p_sys->i_box_lines_total = 0;
680             return 1;
681         case 'L':
682             if( p_sys->i_box_type == BOX_LOG )
683                 p_sys->i_box_type = BOX_NONE;
684             else
685                 p_sys->i_box_type = BOX_LOG;
686             return 1;
687         case 'P':
688             if( p_sys->i_box_type == BOX_PLAYLIST )
689                 p_sys->i_box_type = BOX_NONE;
690             else
691                 p_sys->i_box_type = BOX_PLAYLIST;
692             return 1;
693         case 'B':
694             if( p_sys->i_box_type == BOX_BROWSE )
695                 p_sys->i_box_type = BOX_NONE;
696             else
697                 p_sys->i_box_type = BOX_BROWSE;
698             return 1;
699         case 'h':
700         case 'H':
701             if( p_sys->i_box_type == BOX_HELP )
702                 p_sys->i_box_type = BOX_NONE;
703             else
704                 p_sys->i_box_type = BOX_HELP;
705             p_sys->i_box_lines_total = 0;
706             return 1;
707         case '/':
708             if( p_sys->i_box_type != BOX_SEARCH )
709             {
710                 if( p_sys->psz_search_chain == NULL )
711                 {
712                     return 1;
713                 }
714                 p_sys->psz_search_chain[0] = '\0';
715                 p_sys->b_box_plidx_follow = VLC_FALSE;
716                 p_sys->i_before_search = p_sys->i_box_plidx;
717                 p_sys->i_box_type = BOX_SEARCH;
718             }
719             return 1;
720         case 'A': /* Open */
721             if( p_sys->i_box_type != BOX_OPEN )
722             {
723                 if( p_sys->psz_open_chain == NULL )
724                 {
725                     return 1;
726                 }
727                 p_sys->psz_open_chain[0] = '\0';
728                 p_sys->i_box_type = BOX_OPEN;
729             }
730             return 1;
731
732         /* Navigation */
733         case KEY_RIGHT:
734             p_sys->f_slider += 1.0;
735             if( p_sys->f_slider > 99.9 ) p_sys->f_slider = 99.9;
736             ManageSlider ( p_intf );
737             return 1;
738
739         case KEY_LEFT:
740             p_sys->f_slider -= 1.0;
741             if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
742             ManageSlider ( p_intf );
743             return 1;
744
745         /* Common control */
746         case 'f':
747         {
748             vout_thread_t *p_vout;
749             if( p_intf->p_sys->p_input )
750             {
751                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
752                                           VLC_OBJECT_VOUT, FIND_CHILD );
753                 if( p_vout )
754                 {
755                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
756                     vlc_object_release( p_vout );
757                 }
758             }
759             return 0;
760         }
761
762         case ' ':
763             PlayPause( p_intf );
764             return 1;
765
766         case 's':
767             if( p_intf->p_sys->p_playlist )
768             {
769                 playlist_Stop( p_intf->p_sys->p_playlist );
770             }
771             return 1;
772
773         case 'e':
774             Eject( p_intf );
775             return 1;
776
777         case '[':
778             if( p_sys->p_input )
779             {
780                 val.b_bool = VLC_TRUE;
781                 var_Set( p_sys->p_input, "prev-title", val );
782             }
783             return 1;
784
785         case ']':
786             if( p_sys->p_input )
787             {
788                 val.b_bool = VLC_TRUE;
789                 var_Set( p_sys->p_input, "next-title", val );
790             }
791             return 1;
792
793         case '<':
794             if( p_sys->p_input )
795             {
796                 val.b_bool = VLC_TRUE;
797                 var_Set( p_sys->p_input, "prev-chapter", val );
798             }
799             return 1;
800
801         case '>':
802             if( p_sys->p_input )
803             {
804                 val.b_bool = VLC_TRUE;
805                 var_Set( p_sys->p_input, "next-chapter", val );
806             }
807             return 1;
808
809         case 'p':
810             if( p_intf->p_sys->p_playlist )
811             {
812                 playlist_Prev( p_intf->p_sys->p_playlist );
813             }
814             clear();
815             return 1;
816
817         case 'n':
818             if( p_intf->p_sys->p_playlist )
819             {
820                 playlist_Next( p_intf->p_sys->p_playlist );
821             }
822             clear();
823             return 1;
824
825         case 'a':
826             aout_VolumeUp( p_intf, 1, NULL );
827             clear();
828             return 1;
829
830         case 'z':
831             aout_VolumeDown( p_intf, 1, NULL );
832             clear();
833             return 1;
834
835         /*
836          * ^l should clear and redraw the screen
837          */
838         case 0x0c:
839             clear();
840             return 1;
841
842         default:
843             return 0;
844     }
845 }
846
847 static void ManageSlider ( intf_thread_t *p_intf )
848 {
849     intf_sys_t     *p_sys = p_intf->p_sys;
850     input_thread_t *p_input = p_sys->p_input;
851     vlc_value_t     val;
852
853     if( p_input == NULL )
854     {
855         return;
856     }
857     var_Get( p_input, "state", &val );
858     if( val.i_int != PLAYING_S )
859     {
860         return;
861     }
862
863     var_Get( p_input, "position", &val );
864     if( p_sys->f_slider == p_sys->f_slider_old )
865     {
866         p_sys->f_slider =
867         p_sys->f_slider_old = 100 * val.f_float;
868     }
869     else
870     {
871         p_sys->f_slider_old = p_sys->f_slider;
872
873         val.f_float = p_sys->f_slider / 100.0;
874         var_Set( p_input, "position", val );
875     }
876 }
877
878 static void SearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring )
879 {
880     bool b_ok = false;
881     int i_current;
882     int i_first = 0 ;
883     int i_item = -1;
884     intf_sys_t *p_sys = p_intf->p_sys;
885     playlist_t *p_playlist = p_sys->p_playlist;
886
887     if( p_sys->i_before_search >= 0 )
888         i_first = p_sys->i_before_search;
889
890     if( ( ! psz_searchstring ) ||  strlen( psz_searchstring ) <= 0 )
891     {
892         p_sys->i_box_plidx = p_sys->i_before_search;
893         return;
894     }
895
896     for( i_current = i_first + 1; i_current < p_playlist->i_size;
897          i_current++ )
898     {
899         if( strcasestr( p_playlist->pp_items[i_current]->input.psz_name,
900                         psz_searchstring ) != NULL
901             || strcasestr( p_playlist->pp_items[i_current]->input.psz_uri,
902                            psz_searchstring ) != NULL )
903         {
904             i_item = i_current;
905             b_ok = true;
906             break;
907         }
908     }
909     if( !b_ok )
910     {
911         for( i_current = 0; i_current < i_first; i_current++ )
912         {
913             if( strcasestr( p_playlist->pp_items[i_current]->input.psz_name,
914                             psz_searchstring ) != NULL
915                 || strcasestr( p_playlist->pp_items[i_current]->input.psz_uri,
916                                psz_searchstring ) != NULL )
917             {
918                 i_item = i_current;
919                 b_ok = true;
920                 break;
921             }
922         }
923     }
924
925     if( i_item < 0 || i_item >= p_playlist->i_size ) return;
926
927     p_sys->i_box_plidx = i_item;
928 }
929
930
931 static void mvnprintw( int y, int x, int w, const char *p_fmt, ... )
932 {
933     va_list  vl_args;
934     char    *p_buf = NULL;
935     int      i_len;
936
937     va_start ( vl_args, p_fmt );
938     vasprintf ( &p_buf, p_fmt, vl_args );
939     va_end ( vl_args );
940
941     if ( p_buf == NULL )
942     {
943         return;
944     }
945     if(  w > 0 )
946     {
947         if( ( i_len = strlen( p_buf ) ) > w )
948         {
949             int i_cut = i_len - w;
950             int x1 = i_len/2 - i_cut/2;
951             int x2 = x1 + i_cut;
952
953             if( i_len > x2 )
954             {
955                 memmove( &p_buf[x1], &p_buf[x2], i_len - x2 );
956             }
957             p_buf[w] = '\0';
958             if( w > 7 )
959             {
960                 p_buf[w/2-1] = '.';
961                 p_buf[w/2  ] = '.';
962                 p_buf[w/2+1] = '.';
963             }
964             mvprintw( y, x, "%s", p_buf );
965         }
966         else
967         {
968             mvprintw( y, x, "%s", p_buf );
969             mvhline( y, x + i_len, ' ', w - i_len );
970         }
971     }
972 }
973 static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt, ... )
974 {
975     intf_sys_t     *p_sys = p_intf->p_sys;
976
977     va_list  vl_args;
978     char    *p_buf = NULL;
979
980     if( l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_lines )
981     {
982         return;
983     }
984
985     va_start ( vl_args, p_fmt );
986     vasprintf ( &p_buf, p_fmt, vl_args );
987     va_end ( vl_args );
988
989     if( p_buf == NULL )
990     {
991         return;
992     }
993
994     mvnprintw( p_sys->i_box_y + l - p_sys->i_box_start, x, COLS - x - 1, "%s", p_buf );
995 }
996
997 static void Redraw ( intf_thread_t *p_intf, time_t *t_last_refresh )
998 {
999     intf_sys_t     *p_sys = p_intf->p_sys;
1000     input_thread_t *p_input = p_sys->p_input;
1001     int y = 0;
1002     int h;
1003     int y_end;
1004
1005     //clear();
1006
1007     /* Title */
1008     attrset ( A_REVERSE );
1009     mvnprintw( y, 0, COLS, "VLC media player" " (ncurses interface) [ h for help ]" );
1010     attroff ( A_REVERSE );
1011     y += 2;
1012
1013     /* Infos */
1014     if( p_input && !p_input->b_dead )
1015     {
1016         char buf1[MSTRTIME_MAX_SIZE];
1017         char buf2[MSTRTIME_MAX_SIZE];
1018         vlc_value_t val;
1019         vlc_value_t val_list;
1020
1021         /* Source */
1022         mvnprintw( y++, 0, COLS, " Source   : %s",
1023                    p_input->input.p_item->psz_uri );
1024
1025         /* State */
1026         var_Get( p_input, "state", &val );
1027         if( val.i_int == PLAYING_S )
1028         {
1029             mvnprintw( y++, 0, COLS, " State    : Playing" );
1030         }
1031         else if( val.i_int == PAUSE_S )
1032         {
1033             mvnprintw( y++, 0, COLS, " State    : Paused" );
1034         }
1035         else
1036         {
1037             y++;
1038         }
1039         if( val.i_int != INIT_S && val.i_int != END_S )
1040         {
1041             audio_volume_t i_volume;
1042
1043             /* Position */
1044             var_Get( p_input, "time", &val );
1045             msecstotimestr( buf1, val.i_time / 1000 );
1046
1047             var_Get( p_input, "length", &val );
1048             msecstotimestr( buf2, val.i_time / 1000 );
1049
1050             mvnprintw( y++, 0, COLS, " Position : %s/%s (%.2f%%)", buf1, buf2, p_sys->f_slider );
1051
1052             /* Volume */
1053             aout_VolumeGet( p_intf, &i_volume );
1054             mvnprintw( y++, 0, COLS, " Volume   : %i%%", i_volume*200/AOUT_VOLUME_MAX );
1055
1056             /* Title */
1057             if( !var_Get( p_input, "title", &val ) )
1058             {
1059                 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
1060                 if( val_list.p_list->i_count > 0 )
1061                 {
1062                     mvnprintw( y++, 0, COLS, " Title    : %d/%d", val.i_int, val_list.p_list->i_count );
1063                 }
1064                 var_Change( p_input, "title", VLC_VAR_FREELIST, &val_list, NULL );
1065             }
1066
1067             /* Chapter */
1068             if( !var_Get( p_input, "chapter", &val ) )
1069             {
1070                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
1071                 if( val_list.p_list->i_count > 0 )
1072                 {
1073                     mvnprintw( y++, 0, COLS, " Chapter  : %d/%d", val.i_int, val_list.p_list->i_count );
1074                 }
1075                 var_Change( p_input, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
1076             }
1077         }
1078         else
1079         {
1080             y++;
1081         }
1082     }
1083     else
1084     {
1085         mvnprintw( y++, 0, COLS, "Source: <no current item>" );
1086         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1087         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1088         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1089     }
1090
1091     DrawBox( p_sys->w, y, 0, 3, COLS, "" );
1092     DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
1093     DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
1094     y += 3;
1095
1096     p_sys->i_box_y = y + 1;
1097     p_sys->i_box_lines = LINES - y - 2;
1098
1099     h = LINES - y;
1100     y_end = y + h - 1;
1101
1102     if( p_sys->i_box_type == BOX_HELP )
1103     {
1104         /* Help box */
1105         int l = 0;
1106         DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
1107
1108         MainBoxWrite( p_intf, l++, 1, "[Display]" );
1109         MainBoxWrite( p_intf, l++, 1, "     h,H         Show/Hide help box" );
1110         MainBoxWrite( p_intf, l++, 1, "     i           Show/Hide info box" );
1111         MainBoxWrite( p_intf, l++, 1, "     L           Show/Hide messages box" );
1112         MainBoxWrite( p_intf, l++, 1, "     P           Show/Hide playlist box" );
1113         MainBoxWrite( p_intf, l++, 1, "     B           Show/Hide filebrowser" );
1114         MainBoxWrite( p_intf, l++, 1, "" );
1115
1116         MainBoxWrite( p_intf, l++, 1, "[Global]" );
1117         MainBoxWrite( p_intf, l++, 1, "     q, Q        Quit" );
1118         MainBoxWrite( p_intf, l++, 1, "     s           Stop" );
1119         MainBoxWrite( p_intf, l++, 1, "     <space>     Pause/Play" );
1120         MainBoxWrite( p_intf, l++, 1, "     f           Toggle Fullscreen" );
1121         MainBoxWrite( p_intf, l++, 1, "     n, p        Next/Previous playlist item" );
1122         MainBoxWrite( p_intf, l++, 1, "     [, ]        Next/Previous title" );
1123         MainBoxWrite( p_intf, l++, 1, "     <, >        Next/Previous chapter" );
1124         MainBoxWrite( p_intf, l++, 1, "     <right>     Seek +1%%" );
1125         MainBoxWrite( p_intf, l++, 1, "     <left>      Seek -1%%" );
1126         MainBoxWrite( p_intf, l++, 1, "     a           Volume Up" );
1127         MainBoxWrite( p_intf, l++, 1, "     z           Volume Down" );
1128         MainBoxWrite( p_intf, l++, 1, "" );
1129
1130         MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
1131         MainBoxWrite( p_intf, l++, 1, "     r           Random" );
1132         MainBoxWrite( p_intf, l++, 1, "     l           Loop Playlist" );
1133         MainBoxWrite( p_intf, l++, 1, "     R           Repeat item" );
1134         MainBoxWrite( p_intf, l++, 1, "     o           Order Playlist by title" );
1135         MainBoxWrite( p_intf, l++, 1, "     O           Reverse order Playlist by title" );
1136         MainBoxWrite( p_intf, l++, 1, "     /           Look for an item" );
1137         MainBoxWrite( p_intf, l++, 1, "     A           Add an entry" );
1138         MainBoxWrite( p_intf, l++, 1, "     D, <del>    Delete an entry" );
1139         MainBoxWrite( p_intf, l++, 1, "     <backspace> Delete an entry" );
1140         MainBoxWrite( p_intf, l++, 1, "" );
1141
1142         MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
1143         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Navigate through the box line by line" );
1144         MainBoxWrite( p_intf, l++, 1, "     <pgup>,<pgdown> Navigate through the box page by page" );
1145         MainBoxWrite( p_intf, l++, 1, "" );
1146
1147         MainBoxWrite( p_intf, l++, 1, "[Player]" );
1148         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Seek +/-5%%" );
1149         MainBoxWrite( p_intf, l++, 1, "" );
1150
1151         MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
1152         MainBoxWrite( p_intf, l++, 1, "     Ctrl-l          Refresh the screen" );
1153
1154         p_sys->i_box_lines_total = l;
1155         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1156         {
1157             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1158         }
1159
1160         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1161         {
1162             y += l - p_sys->i_box_start;
1163         }
1164         else
1165         {
1166             y += p_sys->i_box_lines;
1167         }
1168     }
1169     else if( p_sys->i_box_type == BOX_INFO )
1170     {
1171         /* Info box */
1172         int l = 0;
1173         DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1174
1175         if( p_input )
1176         {
1177             int i,j;
1178             vlc_mutex_lock( &p_input->input.p_item->lock );
1179             for ( i = 0; i < p_input->input.p_item->i_categories; i++ )
1180             {
1181                 info_category_t *p_category = p_input->input.p_item->pp_categories[i];
1182                 if( y >= y_end ) break;
1183                 MainBoxWrite( p_intf, l++, 1, "  [%s]", p_category->psz_name );
1184                 for ( j = 0; j < p_category->i_infos; j++ )
1185                 {
1186                     info_t *p_info = p_category->pp_infos[j];
1187                     if( y >= y_end ) break;
1188                     MainBoxWrite( p_intf, l++, 1, "      %s: %s", p_info->psz_name, p_info->psz_value );
1189                 }
1190             }
1191             vlc_mutex_unlock( &p_input->input.p_item->lock );
1192         }
1193         else
1194         {
1195             MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1196         }
1197         p_sys->i_box_lines_total = l;
1198         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1199         {
1200             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1201         }
1202
1203         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1204         {
1205             y += l - p_sys->i_box_start;
1206         }
1207         else
1208         {
1209             y += p_sys->i_box_lines;
1210         }
1211     }
1212     else if( p_sys->i_box_type == BOX_LOG )
1213     {
1214         int i_line = 0;
1215         int i_stop;
1216         int i_start;
1217
1218         DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1219
1220         i_start = p_intf->p_sys->p_sub->i_start;
1221
1222         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1223         i_stop = *p_intf->p_sys->p_sub->pi_stop;
1224         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1225
1226         for( ;; )
1227         {
1228             static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1229             if( i_line >= h - 2 )
1230             {
1231                 break;
1232             }
1233             i_stop--;
1234             i_line++;
1235             if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1236             if( i_stop == i_start )
1237             {
1238                 break;
1239             }
1240             mvnprintw( y + h-2-i_line, 1, COLS - 2, "   [%s] %s",
1241                       ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1242                       p_sys->p_sub->p_msg[i_stop].psz_msg );
1243         }
1244
1245         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1246         p_intf->p_sys->p_sub->i_start = i_stop;
1247         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1248         y = y_end;
1249     }
1250     else if( p_sys->i_box_type == BOX_BROWSE )
1251     {
1252         /* Playlist box */
1253         int        i_start, i_stop;
1254         int        i_item;
1255         DrawBox( p_sys->w, y++, 0, h, COLS, " Browse " );
1256
1257         if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
1258         if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
1259
1260         if( p_sys->i_box_bidx < (h - 2)/2 )
1261         {
1262             i_start = 0;
1263             i_stop = h - 2;
1264         }
1265         else if( p_sys->i_dir_entries - p_sys->i_box_bidx > (h - 2)/2 )
1266         {
1267             i_start = p_sys->i_box_bidx - (h - 2)/2;
1268             i_stop = i_start + h - 2;
1269         }
1270         else
1271         {
1272             i_stop = p_sys->i_dir_entries;
1273             i_start = p_sys->i_dir_entries - (h - 2);
1274         }
1275         if( i_start < 0 )
1276         {
1277             i_start = 0;
1278         }
1279         if( i_stop > p_sys->i_dir_entries )
1280         {
1281             i_stop = p_sys->i_dir_entries;
1282         }
1283
1284         for( i_item = i_start; i_item < i_stop; i_item++ )
1285         {
1286             vlc_bool_t b_selected = ( p_sys->i_box_bidx == i_item );
1287
1288             if( y >= y_end ) break;
1289             if( b_selected )
1290             {
1291                 attrset( A_REVERSE );
1292             }
1293             mvnprintw( y++, 1, COLS - 2, "%c %s", p_sys->pp_dir_entries[i_item]->b_file == VLC_TRUE ? '-' : '+',
1294                             p_sys->pp_dir_entries[i_item]->psz_path );
1295             if( b_selected )
1296             {
1297                 attroff ( A_REVERSE );
1298             }
1299         }
1300
1301     }
1302     else if( ( p_sys->i_box_type == BOX_PLAYLIST ||
1303                p_sys->i_box_type == BOX_SEARCH ||
1304                p_sys->i_box_type == BOX_OPEN  ) && p_sys->p_playlist )
1305     {
1306         /* Playlist box */
1307         playlist_t *p_playlist = p_sys->p_playlist;
1308         int        i_start, i_stop;
1309         int        i_item;
1310         DrawBox( p_sys->w, y++, 0, h, COLS, " Playlist " );
1311
1312         if( p_sys->i_box_plidx >= p_playlist->i_size ) p_sys->i_box_plidx = p_playlist->i_size - 1;
1313         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1314
1315         if( p_sys->i_box_plidx < (h - 2)/2 )
1316         {
1317             i_start = 0;
1318             i_stop = h - 2;
1319         }
1320         else if( p_playlist->i_size - p_sys->i_box_plidx > (h - 2)/2 )
1321         {
1322             i_start = p_sys->i_box_plidx - (h - 2)/2;
1323             i_stop = i_start + h - 2;
1324         }
1325         else
1326         {
1327             i_stop = p_playlist->i_size;
1328             i_start = p_playlist->i_size - (h - 2);
1329         }
1330         if( i_start < 0 )
1331         {
1332             i_start = 0;
1333         }
1334         if( i_stop > p_playlist->i_size )
1335         {
1336             i_stop = p_playlist->i_size;
1337         }
1338
1339         for( i_item = i_start; i_item < i_stop; i_item++ )
1340         {
1341             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1342             int c = p_playlist->i_index == i_item ? '>' : ' ';
1343
1344             if( y >= y_end ) break;
1345             if( b_selected )
1346             {
1347                 attrset( A_REVERSE );
1348             }
1349             if( !strcmp( p_playlist->pp_items[i_item]->input.psz_name,
1350                          p_playlist->pp_items[i_item]->input.psz_uri ) )
1351             {
1352                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s'",
1353                            c,
1354                            i_item,
1355                            p_playlist->pp_items[i_item]->input.psz_uri );
1356             }
1357             else
1358             {
1359                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s' (%s)",
1360                           c,
1361                           i_item,
1362                           p_playlist->pp_items[i_item]->input.psz_uri,
1363                           p_playlist->pp_items[i_item]->input.psz_name );
1364             }
1365             if( b_selected )
1366             {
1367                 attroff ( A_REVERSE );
1368             }
1369         }
1370     }
1371     else
1372     {
1373         y++;
1374     }
1375     if( p_sys->i_box_type == BOX_SEARCH )
1376     {
1377         DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1378         if( p_sys->psz_search_chain )
1379         {
1380             if( strlen( p_sys->psz_search_chain ) == 0 &&
1381                 p_sys->psz_old_search != NULL )
1382             {
1383                 /* Searching next entry */
1384                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1385             }
1386             else
1387             {
1388                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1389             }
1390         }
1391     }
1392     if( p_sys->i_box_type == BOX_OPEN )
1393     {
1394         if( p_sys->psz_open_chain )
1395         {
1396             DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1397             mvnprintw( 7, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1398         }
1399     }
1400
1401     while( y < y_end )
1402     {
1403         DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1404     }
1405
1406     refresh();
1407
1408     *t_last_refresh = time( 0 );
1409 }
1410
1411 static void Eject ( intf_thread_t *p_intf )
1412 {
1413     char *psz_device = NULL, *psz_parser, *psz_name;
1414
1415     /*
1416      * Get the active input
1417      * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
1418      * If it's neither of these, then return
1419      */
1420
1421     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1422                                                        FIND_ANYWHERE );
1423
1424     if( p_playlist == NULL )
1425     {
1426         return;
1427     }
1428
1429     vlc_mutex_lock( &p_playlist->object_lock );
1430
1431     if( p_playlist->i_index < 0 )
1432     {
1433         vlc_mutex_unlock( &p_playlist->object_lock );
1434         vlc_object_release( p_playlist );
1435         return;
1436     }
1437
1438     psz_name = p_playlist->pp_items[ p_playlist->i_index ]->input.psz_name;
1439
1440     if( psz_name )
1441     {
1442         if( !strncmp(psz_name, "dvd://", 4) )
1443         {
1444             switch( psz_name[strlen("dvd://")] )
1445             {
1446             case '\0':
1447             case '@':
1448                 psz_device = config_GetPsz( p_intf, "dvd" );
1449                 break;
1450             default:
1451                 /* Omit the first MRL-selector characters */
1452                 psz_device = strdup( psz_name + strlen("dvd://" ) );
1453                 break;
1454             }
1455         }
1456         else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
1457         {
1458             switch( psz_name[strlen(VCD_MRL)] )
1459             {
1460             case '\0':
1461             case '@':
1462                 psz_device = config_GetPsz( p_intf, VCD_MRL );
1463                 break;
1464             default:
1465                 /* Omit the beginning MRL-selector characters */
1466                 psz_device = strdup( psz_name + strlen(VCD_MRL) );
1467                 break;
1468             }
1469         }
1470         else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
1471         {
1472             switch( psz_name[strlen(CDDA_MRL)] )
1473             {
1474             case '\0':
1475             case '@':
1476                 psz_device = config_GetPsz( p_intf, "cd-audio" );
1477                 break;
1478             default:
1479                 /* Omit the beginning MRL-selector characters */
1480                 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
1481                 break;
1482             }
1483         }
1484         else
1485         {
1486             psz_device = strdup( psz_name );
1487         }
1488     }
1489
1490     vlc_mutex_unlock( &p_playlist->object_lock );
1491     vlc_object_release( p_playlist );
1492
1493     if( psz_device == NULL )
1494     {
1495         return;
1496     }
1497
1498     /* Remove what we have after @ */
1499     psz_parser = psz_device;
1500     for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
1501     {
1502         if( *psz_parser == '@' )
1503         {
1504             *psz_parser = '\0';
1505             break;
1506         }
1507     }
1508
1509     /* If there's a stream playing, we aren't allowed to eject ! */
1510     if( p_intf->p_sys->p_input == NULL )
1511     {
1512         msg_Dbg( p_intf, "ejecting %s", psz_device );
1513
1514         intf_Eject( p_intf, psz_device );
1515     }
1516
1517     free(psz_device);
1518     return;
1519 }
1520
1521 static void ReadDir( intf_thread_t *p_intf )
1522 {
1523     intf_sys_t     *p_sys = p_intf->p_sys;
1524     DIR *                       p_current_dir;
1525     struct dirent *             p_dir_content;
1526     int i;
1527
1528     if( p_sys->psz_current_dir && *p_sys->psz_current_dir )
1529     {
1530         /* Open the dir */
1531         p_current_dir = opendir( p_sys->psz_current_dir );
1532
1533         if( p_current_dir == NULL )
1534         {
1535             /* something went bad, get out of here ! */
1536 #ifdef HAVE_ERRNO_H
1537             msg_Warn( p_intf, "cannot open directory `%s' (%s)",
1538                       p_sys->psz_current_dir, strerror(errno));
1539 #else
1540             msg_Warn( p_intf, "cannot open directory `%s'", p_sys->psz_current_dir );
1541 #endif
1542             return;
1543         }
1544         
1545         /* Clean the old shit */
1546         for( i = 0; i < p_sys->i_dir_entries; i++ )
1547         {
1548             struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
1549             free( p_dir_entry->psz_path );
1550             REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
1551             free( p_dir_entry );
1552         }
1553         p_sys->pp_dir_entries = NULL;
1554         p_sys->i_dir_entries = 0;
1555
1556         /* get the first directory entry */
1557         p_dir_content = readdir( p_current_dir );
1558
1559         /* while we still have entries in the directory */
1560         while( p_dir_content != NULL )
1561         {
1562 #if defined( S_ISDIR )
1563             struct stat stat_data;
1564 #endif
1565             struct dir_entry_t *p_dir_entry;
1566             int i_size_entry = strlen( p_sys->psz_current_dir ) +
1567                                strlen( p_dir_content->d_name ) + 2;
1568             char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
1569
1570             sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir,
1571                      p_dir_content->d_name );
1572
1573             if( !( p_dir_entry = malloc( sizeof( struct dir_entry_t) ) ) )
1574             {
1575                 free( psz_uri);
1576                 return;
1577             }
1578
1579 #if defined( S_ISDIR )
1580             stat( psz_uri, &stat_data );
1581             if( S_ISDIR(stat_data.st_mode) )
1582 #elif defined( DT_DIR )
1583             if( p_dir_content->d_type & DT_DIR )
1584 #else
1585             if( 0 )
1586 #endif
1587             {
1588                 p_dir_entry->psz_path = strdup( p_dir_content->d_name );
1589                 p_dir_entry->b_file = VLC_FALSE;
1590                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1591                      p_sys->i_dir_entries, p_dir_entry );
1592             }
1593             else
1594             {
1595                 p_dir_entry->psz_path = strdup( p_dir_content->d_name );
1596                 p_dir_entry->b_file = VLC_TRUE;
1597                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1598                      p_sys->i_dir_entries, p_dir_entry );
1599             }
1600
1601             free( psz_uri );
1602             /* Read next entry */
1603             p_dir_content = readdir( p_current_dir );
1604         }
1605         closedir( p_current_dir );
1606         return;
1607     }
1608     else
1609     {
1610         msg_Dbg( p_intf, "no current dir set" );
1611         return;
1612     }
1613 }
1614
1615 static void PlayPause( intf_thread_t *p_intf )
1616 {
1617     input_thread_t *p_input = p_intf->p_sys->p_input;
1618     vlc_value_t val;
1619
1620     if( p_input )
1621     {
1622         var_Get( p_input, "state", &val );
1623         if( val.i_int != PAUSE_S )
1624         {
1625             val.i_int = PAUSE_S;
1626         }
1627         else
1628         {
1629             val.i_int = PLAYING_S;
1630         }
1631         var_Set( p_input, "state", val );
1632     }
1633     else if( p_intf->p_sys->p_playlist )
1634     {
1635         playlist_Play( p_intf->p_sys->p_playlist );
1636     }
1637 }
1638
1639 /****************************************************************************
1640  *
1641  ****************************************************************************/
1642 static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title )
1643 {
1644     int i;
1645     int i_len;
1646
1647     if(  w > 3 && h > 2 )
1648     {
1649         if( title == NULL ) title = "";
1650         i_len = strlen( title );
1651
1652         if( i_len > w - 2 ) i_len = w - 2;
1653
1654         mvwaddch( win, y, x,    ACS_ULCORNER );
1655         mvwhline( win, y, x+1,  ACS_HLINE, ( w-i_len-2)/2 );
1656         mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
1657         mvwhline( win, y, x+(w-i_len)/2+i_len,  ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
1658         mvwaddch( win, y, x+w-1,ACS_URCORNER );
1659
1660         for( i = 0; i < h-2; i++ )
1661         {
1662             mvwaddch( win, y+i+1, x,     ACS_VLINE );
1663             mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
1664         }
1665
1666         mvwaddch( win, y+h-1, x,     ACS_LLCORNER );
1667         mvwhline( win, y+h-1, x+1,   ACS_HLINE, w - 2 );
1668         mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
1669     }
1670 }
1671
1672 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
1673 {
1674     if( w > 0 )
1675     {
1676         mvhline( y, x, ' ', w );
1677     }
1678 }
1679
1680 static void DrawLine( WINDOW *win, int y, int x, int w )
1681 {
1682     if( w > 0 )
1683     {
1684         attrset( A_REVERSE );
1685         mvhline( y, x, ' ', w );
1686         attroff ( A_REVERSE );
1687     }
1688 }