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