c9b4535aac67f47f2eee2c12d9a4228b458dc252
[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 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         /* Browser navigation */
451         switch( i_key )
452         {
453             case KEY_HOME:
454                 p_sys->i_box_bidx = 0;
455                 break;
456             case KEY_END:
457                 p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
458                 break;
459             case KEY_UP:
460                 p_sys->i_box_bidx--;
461                 break;
462             case KEY_DOWN:
463                 p_sys->i_box_bidx++;
464                 break;
465             case KEY_PPAGE:
466                 p_sys->i_box_bidx -= p_sys->i_box_lines;
467                 break;
468             case KEY_NPAGE:
469                 p_sys->i_box_bidx += p_sys->i_box_lines;
470                 break;
471
472             case KEY_ENTER:
473             case 0x0d:
474                 if( p_sys->pp_dir_entries[p_sys->i_box_bidx]->b_file )
475                 {
476                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
477                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
478                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
479
480                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
481                     playlist_Add( p_sys->p_playlist, psz_uri,
482                                   psz_uri,
483                                   PLAYLIST_APPEND, PLAYLIST_END );
484                     p_sys->i_box_type = BOX_PLAYLIST;
485                     free( psz_uri );
486                 }
487                 else
488                 {
489                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
490                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path ) + 2;
491                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
492
493                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_bidx]->psz_path );
494                     
495                     p_sys->psz_current_dir = strdup( psz_uri );
496                     ReadDir( p_intf );
497                     free( psz_uri );
498                 }
499                 break;
500             default:
501                 return 1;
502                 break;
503
504             if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_bidx = p_sys->i_dir_entries - 1;
505             if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
506             return 1;
507         }
508     }
509     else if( p_sys->i_box_type == BOX_HELP || p_sys->i_box_type == BOX_INFO )
510     {
511         switch( i_key )
512         {
513             case KEY_HOME:
514                 p_sys->i_box_start = 0;
515                 return 1;
516             case KEY_END:
517                 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
518                 return 1;
519             case KEY_UP:
520                 if( p_sys->i_box_start > 0 ) p_sys->i_box_start--;
521                 return 1;
522             case KEY_DOWN:
523                 if( p_sys->i_box_start < p_sys->i_box_lines_total - 1 )
524                 {
525                     p_sys->i_box_start++;
526                 }
527                 return 1;
528             case KEY_PPAGE:
529                 p_sys->i_box_start -= p_sys->i_box_lines;
530                 if( p_sys->i_box_start < 0 ) p_sys->i_box_start = 0;
531                 return 1;
532             case KEY_NPAGE:
533                 p_sys->i_box_start += p_sys->i_box_lines;
534                 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
535                 {
536                     p_sys->i_box_start = p_sys->i_box_lines_total - 1;
537                 }
538                 return 1;
539             default:
540                 break;
541         }
542     }
543     else if( p_sys->i_box_type == BOX_NONE )
544     {
545         switch( i_key )
546         {
547             case KEY_HOME:
548                 p_sys->f_slider = 0;
549                 ManageSlider ( p_intf );
550                 return 1;
551             case KEY_END:
552                 p_sys->f_slider = 99.9;
553                 ManageSlider ( p_intf );
554                 return 1;
555             case KEY_UP:
556                 p_sys->f_slider += 5.0;
557                 if( p_sys->f_slider >= 99.0 ) p_sys->f_slider = 99.0;
558                 ManageSlider ( p_intf );
559                 return 1;
560             case KEY_DOWN:
561                 p_sys->f_slider -= 5.0;
562                 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
563                 ManageSlider ( p_intf );
564                 return 1;
565
566             default:
567                 break;
568         }
569     }
570     else if( p_sys->i_box_type == BOX_SEARCH && p_sys->psz_search_chain )
571     {
572         int i_chain_len;
573         i_chain_len = strlen( p_sys->psz_search_chain );
574         switch( i_key )
575         {
576             case 0x0c:      /* ^l */
577                 clear();
578                 return 1;
579             case KEY_ENTER:
580             case 0x0d:
581                 if( i_chain_len > 0 )
582                 {
583                     p_sys->psz_old_search = strdup( p_sys->psz_search_chain );
584                 }
585                 else if( p_sys->psz_old_search )
586                 {
587                     SearchPlaylist( p_intf, p_sys->psz_old_search );
588                 }
589                 p_sys->i_box_type = BOX_PLAYLIST;
590                 return 1;
591             case 0x1b:      /* Esc. */
592                 p_sys->i_box_plidx = p_sys->i_before_search;
593                 p_sys->i_box_type = BOX_PLAYLIST;
594                 return 1;
595             case KEY_BACKSPACE:
596                 if( i_chain_len > 0 )
597                 {
598                     p_sys->psz_search_chain[ i_chain_len - 1 ] = '\0';
599                 }
600                 break;
601             default:
602                 if( i_chain_len < SEARCH_CHAIN_SIZE )
603                 {
604                     p_sys->psz_search_chain[ i_chain_len++ ] = i_key;
605                     p_sys->psz_search_chain[ i_chain_len ] = 0;
606                 }
607                 break;
608         }
609         if( p_sys->psz_old_search )
610         {
611             free( p_sys->psz_old_search );
612             p_sys->psz_old_search = NULL;
613         }
614         SearchPlaylist( p_intf, p_sys->psz_search_chain );
615         return 1;
616     }
617     else if( p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain )
618     {
619         int i_chain_len;
620         i_chain_len = strlen( p_sys->psz_open_chain );
621         playlist_t *p_playlist = p_sys->p_playlist;
622
623         switch( i_key )
624         {
625             case 0x0c:      /* ^l */
626                 clear();
627                 return 1;
628             case KEY_ENTER:
629             case 0x0d:
630                 if( p_playlist && i_chain_len > 0 )
631                 {
632                     playlist_Add( p_playlist, p_sys->psz_open_chain,
633                                   p_sys->psz_open_chain,
634                                   PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
635                     p_sys->b_box_plidx_follow = VLC_TRUE;
636                 }
637                 p_sys->i_box_type = BOX_PLAYLIST;
638                 return 1;
639             case 0x1b:      /* Esc. */
640                 p_sys->i_box_type = BOX_PLAYLIST;
641                 return 1;
642             case KEY_BACKSPACE:
643                 if( i_chain_len > 0 )
644                 {
645                     p_sys->psz_open_chain[ i_chain_len - 1 ] = '\0';
646                 }
647                 break;
648             default:
649                 if( i_chain_len < OPEN_CHAIN_SIZE )
650                 {
651                     p_sys->psz_open_chain[ i_chain_len++ ] = i_key;
652                     p_sys->psz_open_chain[ i_chain_len ] = 0;
653                 }
654                 break;
655         }
656         return 1;
657     }
658
659
660     /* Common keys */
661     switch( i_key )
662     {
663         case 'q':
664         case 'Q':
665         case 0x1b:  /* Esc */
666             p_intf->p_vlc->b_die = VLC_TRUE;
667             return 0;
668
669         /* Box switching */
670         case 'i':
671             if( p_sys->i_box_type == BOX_INFO )
672                 p_sys->i_box_type = BOX_NONE;
673             else
674                 p_sys->i_box_type = BOX_INFO;
675             p_sys->i_box_lines_total = 0;
676             return 1;
677         case 'L':
678             if( p_sys->i_box_type == BOX_LOG )
679                 p_sys->i_box_type = BOX_NONE;
680             else
681                 p_sys->i_box_type = BOX_LOG;
682             return 1;
683         case 'P':
684             if( p_sys->i_box_type == BOX_PLAYLIST )
685                 p_sys->i_box_type = BOX_NONE;
686             else
687                 p_sys->i_box_type = BOX_PLAYLIST;
688             return 1;
689         case 'B':
690             if( p_sys->i_box_type == BOX_BROWSE )
691                 p_sys->i_box_type = BOX_NONE;
692             else
693                 p_sys->i_box_type = BOX_BROWSE;
694             return 1;
695         case 'h':
696         case 'H':
697             if( p_sys->i_box_type == BOX_HELP )
698                 p_sys->i_box_type = BOX_NONE;
699             else
700                 p_sys->i_box_type = BOX_HELP;
701             p_sys->i_box_lines_total = 0;
702             return 1;
703         case '/':
704             if( p_sys->i_box_type != BOX_SEARCH )
705             {
706                 if( p_sys->psz_search_chain == NULL )
707                 {
708                     return 1;
709                 }
710                 p_sys->psz_search_chain[0] = '\0';
711                 p_sys->b_box_plidx_follow = VLC_FALSE;
712                 p_sys->i_before_search = p_sys->i_box_plidx;
713                 p_sys->i_box_type = BOX_SEARCH;
714             }
715             return 1;
716         case 'A': /* Open */
717             if( p_sys->i_box_type != BOX_OPEN )
718             {
719                 if( p_sys->psz_open_chain == NULL )
720                 {
721                     return 1;
722                 }
723                 p_sys->psz_open_chain[0] = '\0';
724                 p_sys->i_box_type = BOX_OPEN;
725             }
726             return 1;
727
728         /* Navigation */
729         case KEY_RIGHT:
730             p_sys->f_slider += 1.0;
731             if( p_sys->f_slider > 99.9 ) p_sys->f_slider = 99.9;
732             ManageSlider ( p_intf );
733             return 1;
734
735         case KEY_LEFT:
736             p_sys->f_slider -= 1.0;
737             if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
738             ManageSlider ( p_intf );
739             return 1;
740
741         /* Common control */
742         case 'f':
743         {
744             vout_thread_t *p_vout;
745             if( p_intf->p_sys->p_input )
746             {
747                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
748                                           VLC_OBJECT_VOUT, FIND_CHILD );
749                 if( p_vout )
750                 {
751                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
752                     vlc_object_release( p_vout );
753                 }
754             }
755             return 0;
756         }
757
758         case ' ':
759             PlayPause( p_intf );
760             return 1;
761
762         case 's':
763             if( p_intf->p_sys->p_playlist )
764             {
765                 playlist_Stop( p_intf->p_sys->p_playlist );
766             }
767             return 1;
768
769         case 'e':
770             Eject( p_intf );
771             return 1;
772
773         case '[':
774             if( p_sys->p_input )
775             {
776                 val.b_bool = VLC_TRUE;
777                 var_Set( p_sys->p_input, "prev-title", val );
778             }
779             return 1;
780
781         case ']':
782             if( p_sys->p_input )
783             {
784                 val.b_bool = VLC_TRUE;
785                 var_Set( p_sys->p_input, "next-title", val );
786             }
787             return 1;
788
789         case '<':
790             if( p_sys->p_input )
791             {
792                 val.b_bool = VLC_TRUE;
793                 var_Set( p_sys->p_input, "prev-chapter", val );
794             }
795             return 1;
796
797         case '>':
798             if( p_sys->p_input )
799             {
800                 val.b_bool = VLC_TRUE;
801                 var_Set( p_sys->p_input, "next-chapter", val );
802             }
803             return 1;
804
805         case 'p':
806             if( p_intf->p_sys->p_playlist )
807             {
808                 playlist_Prev( p_intf->p_sys->p_playlist );
809             }
810             clear();
811             return 1;
812
813         case 'n':
814             if( p_intf->p_sys->p_playlist )
815             {
816                 playlist_Next( p_intf->p_sys->p_playlist );
817             }
818             clear();
819             return 1;
820
821         case 'a':
822             aout_VolumeUp( p_intf, 1, NULL );
823             clear();
824             return 1;
825
826         case 'z':
827             aout_VolumeDown( p_intf, 1, NULL );
828             clear();
829             return 1;
830
831         /*
832          * ^l should clear and redraw the screen
833          */
834         case 0x0c:
835             clear();
836             return 1;
837
838         default:
839             return 0;
840     }
841 }
842
843 static void ManageSlider ( intf_thread_t *p_intf )
844 {
845     intf_sys_t     *p_sys = p_intf->p_sys;
846     input_thread_t *p_input = p_sys->p_input;
847     vlc_value_t     val;
848
849     if( p_input == NULL )
850     {
851         return;
852     }
853     var_Get( p_input, "state", &val );
854     if( val.i_int != PLAYING_S )
855     {
856         return;
857     }
858
859     var_Get( p_input, "position", &val );
860     if( p_sys->f_slider == p_sys->f_slider_old )
861     {
862         p_sys->f_slider =
863         p_sys->f_slider_old = 100 * val.f_float;
864     }
865     else
866     {
867         p_sys->f_slider_old = p_sys->f_slider;
868
869         val.f_float = p_sys->f_slider / 100.0;
870         var_Set( p_input, "position", val );
871     }
872 }
873
874 static void SearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring )
875 {
876     bool b_ok = false;
877     int i_current;
878     int i_first = 0 ;
879     int i_item = -1;
880     intf_sys_t *p_sys = p_intf->p_sys;
881     playlist_t *p_playlist = p_sys->p_playlist;
882
883     if( p_sys->i_before_search >= 0 )
884         i_first = p_sys->i_before_search;
885
886     if( ( ! psz_searchstring ) ||  strlen( psz_searchstring ) <= 0 )
887     {
888         p_sys->i_box_plidx = p_sys->i_before_search;
889         return;
890     }
891
892     for( i_current = i_first + 1; i_current < p_playlist->i_size;
893          i_current++ )
894     {
895         if( strcasestr( p_playlist->pp_items[i_current]->input.psz_name,
896                         psz_searchstring ) != NULL
897             || strcasestr( p_playlist->pp_items[i_current]->input.psz_uri,
898                            psz_searchstring ) != NULL )
899         {
900             i_item = i_current;
901             b_ok = true;
902             break;
903         }
904     }
905     if( !b_ok )
906     {
907         for( i_current = 0; i_current < i_first; i_current++ )
908         {
909             if( strcasestr( p_playlist->pp_items[i_current]->input.psz_name,
910                             psz_searchstring ) != NULL
911                 || strcasestr( p_playlist->pp_items[i_current]->input.psz_uri,
912                                psz_searchstring ) != NULL )
913             {
914                 i_item = i_current;
915                 b_ok = true;
916                 break;
917             }
918         }
919     }
920
921     if( i_item < 0 || i_item >= p_playlist->i_size ) return;
922
923     p_sys->i_box_plidx = i_item;
924 }
925
926
927 static void mvnprintw( int y, int x, int w, const char *p_fmt, ... )
928 {
929     va_list  vl_args;
930     char    *p_buf = NULL;
931     int      i_len;
932
933     va_start ( vl_args, p_fmt );
934     vasprintf ( &p_buf, p_fmt, vl_args );
935     va_end ( vl_args );
936
937     if ( p_buf == NULL )
938     {
939         return;
940     }
941     if(  w > 0 )
942     {
943         if( ( i_len = strlen( p_buf ) ) > w )
944         {
945             int i_cut = i_len - w;
946             int x1 = i_len/2 - i_cut/2;
947             int x2 = x1 + i_cut;
948
949             if( i_len > x2 )
950             {
951                 memmove( &p_buf[x1], &p_buf[x2], i_len - x2 );
952             }
953             p_buf[w] = '\0';
954             if( w > 7 )
955             {
956                 p_buf[w/2-1] = '.';
957                 p_buf[w/2  ] = '.';
958                 p_buf[w/2+1] = '.';
959             }
960             mvprintw( y, x, "%s", p_buf );
961         }
962         else
963         {
964             mvprintw( y, x, "%s", p_buf );
965             mvhline( y, x + i_len, ' ', w - i_len );
966         }
967     }
968 }
969 static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt, ... )
970 {
971     intf_sys_t     *p_sys = p_intf->p_sys;
972
973     va_list  vl_args;
974     char    *p_buf = NULL;
975
976     if( l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_lines )
977     {
978         return;
979     }
980
981     va_start ( vl_args, p_fmt );
982     vasprintf ( &p_buf, p_fmt, vl_args );
983     va_end ( vl_args );
984
985     if( p_buf == NULL )
986     {
987         return;
988     }
989
990     mvnprintw( p_sys->i_box_y + l - p_sys->i_box_start, x, COLS - x - 1, "%s", p_buf );
991 }
992
993 static void Redraw ( intf_thread_t *p_intf, time_t *t_last_refresh )
994 {
995     intf_sys_t     *p_sys = p_intf->p_sys;
996     input_thread_t *p_input = p_sys->p_input;
997     int y = 0;
998     int h;
999     int y_end;
1000
1001     //clear();
1002
1003     /* Title */
1004     attrset ( A_REVERSE );
1005     mvnprintw( y, 0, COLS, "VLC media player" " (ncurses interface) [ h for help ]" );
1006     attroff ( A_REVERSE );
1007     y += 2;
1008
1009     /* Infos */
1010     if( p_input && !p_input->b_dead )
1011     {
1012         char buf1[MSTRTIME_MAX_SIZE];
1013         char buf2[MSTRTIME_MAX_SIZE];
1014         vlc_value_t val;
1015         vlc_value_t val_list;
1016
1017         /* Source */
1018         mvnprintw( y++, 0, COLS, " Source   : %s", p_input->psz_source );
1019
1020         /* State */
1021         var_Get( p_input, "state", &val );
1022         if( val.i_int == PLAYING_S )
1023         {
1024             mvnprintw( y++, 0, COLS, " State    : Playing" );
1025         }
1026         else if( val.i_int == PAUSE_S )
1027         {
1028             mvnprintw( y++, 0, COLS, " State    : Paused" );
1029         }
1030         else
1031         {
1032             y++;
1033         }
1034         if( val.i_int != INIT_S && val.i_int != END_S )
1035         {
1036             audio_volume_t i_volume;
1037
1038             /* Position */
1039             var_Get( p_input, "time", &val );
1040             msecstotimestr( buf1, val.i_time / 1000 );
1041
1042             var_Get( p_input, "length", &val );
1043             msecstotimestr( buf2, val.i_time / 1000 );
1044
1045             mvnprintw( y++, 0, COLS, " Position : %s/%s (%.2f%%)", buf1, buf2, p_sys->f_slider );
1046
1047             /* Volume */
1048             aout_VolumeGet( p_intf, &i_volume );
1049             mvnprintw( y++, 0, COLS, " Volume   : %i%%", i_volume*200/AOUT_VOLUME_MAX );
1050
1051             /* Title */
1052             if( !var_Get( p_input, "title", &val ) )
1053             {
1054                 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
1055                 if( val_list.p_list->i_count > 0 )
1056                 {
1057                     mvnprintw( y++, 0, COLS, " Title    : %d/%d", val.i_int, val_list.p_list->i_count );
1058                 }
1059                 var_Change( p_input, "title", VLC_VAR_FREELIST, &val_list, NULL );
1060             }
1061
1062             /* Chapter */
1063             if( !var_Get( p_input, "chapter", &val ) )
1064             {
1065                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
1066                 if( val_list.p_list->i_count > 0 )
1067                 {
1068                     mvnprintw( y++, 0, COLS, " Chapter  : %d/%d", val.i_int, val_list.p_list->i_count );
1069                 }
1070                 var_Change( p_input, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
1071             }
1072         }
1073         else
1074         {
1075             y++;
1076         }
1077     }
1078     else
1079     {
1080         mvnprintw( y++, 0, COLS, "Source: <no current item>" );
1081         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1082         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1083         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1084     }
1085
1086     DrawBox( p_sys->w, y, 0, 3, COLS, "" );
1087     DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
1088     DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
1089     y += 3;
1090
1091     p_sys->i_box_y = y + 1;
1092     p_sys->i_box_lines = LINES - y - 2;
1093
1094     h = LINES - y;
1095     y_end = y + h - 1;
1096
1097     if( p_sys->i_box_type == BOX_HELP )
1098     {
1099         /* Help box */
1100         int l = 0;
1101         DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
1102
1103         MainBoxWrite( p_intf, l++, 1, "[Display]" );
1104         MainBoxWrite( p_intf, l++, 1, "     h,H         Show/Hide help box" );
1105         MainBoxWrite( p_intf, l++, 1, "     i           Show/Hide info box" );
1106         MainBoxWrite( p_intf, l++, 1, "     L           Show/Hide messages box" );
1107         MainBoxWrite( p_intf, l++, 1, "     P           Show/Hide playlist box" );
1108         MainBoxWrite( p_intf, l++, 1, "     B           Show/Hide filebrowser" );
1109         MainBoxWrite( p_intf, l++, 1, "" );
1110
1111         MainBoxWrite( p_intf, l++, 1, "[Global]" );
1112         MainBoxWrite( p_intf, l++, 1, "     q, Q        Quit" );
1113         MainBoxWrite( p_intf, l++, 1, "     s           Stop" );
1114         MainBoxWrite( p_intf, l++, 1, "     <space>     Pause/Play" );
1115         MainBoxWrite( p_intf, l++, 1, "     f           Toggle Fullscreen" );
1116         MainBoxWrite( p_intf, l++, 1, "     n, p        Next/Previous playlist item" );
1117         MainBoxWrite( p_intf, l++, 1, "     [, ]        Next/Previous title" );
1118         MainBoxWrite( p_intf, l++, 1, "     <, >        Next/Previous chapter" );
1119         MainBoxWrite( p_intf, l++, 1, "     <right>     Seek +1%%" );
1120         MainBoxWrite( p_intf, l++, 1, "     <left>      Seek -1%%" );
1121         MainBoxWrite( p_intf, l++, 1, "     a           Volume Up" );
1122         MainBoxWrite( p_intf, l++, 1, "     z           Volume Down" );
1123         MainBoxWrite( p_intf, l++, 1, "" );
1124
1125         MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
1126         MainBoxWrite( p_intf, l++, 1, "     r           Random" );
1127         MainBoxWrite( p_intf, l++, 1, "     l           Loop Playlist" );
1128         MainBoxWrite( p_intf, l++, 1, "     R           Repeat item" );
1129         MainBoxWrite( p_intf, l++, 1, "     o           Order Playlist by title" );
1130         MainBoxWrite( p_intf, l++, 1, "     O           Reverse order Playlist by title" );
1131         MainBoxWrite( p_intf, l++, 1, "     /           Look for an item" );
1132         MainBoxWrite( p_intf, l++, 1, "     A           Add an entry" );
1133         MainBoxWrite( p_intf, l++, 1, "     D, <del>    Delete an entry" );
1134         MainBoxWrite( p_intf, l++, 1, "     <backspace> Delete an entry" );
1135         MainBoxWrite( p_intf, l++, 1, "" );
1136
1137         MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
1138         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Navigate through the box line by line" );
1139         MainBoxWrite( p_intf, l++, 1, "     <pgup>,<pgdown> Navigate through the box page by page" );
1140         MainBoxWrite( p_intf, l++, 1, "" );
1141
1142         MainBoxWrite( p_intf, l++, 1, "[Player]" );
1143         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Seek +/-5%%" );
1144         MainBoxWrite( p_intf, l++, 1, "" );
1145
1146         MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
1147         MainBoxWrite( p_intf, l++, 1, "     Ctrl-l          Refresh the screen" );
1148
1149         p_sys->i_box_lines_total = l;
1150         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1151         {
1152             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1153         }
1154
1155         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1156         {
1157             y += l - p_sys->i_box_start;
1158         }
1159         else
1160         {
1161             y += p_sys->i_box_lines;
1162         }
1163     }
1164     else if( p_sys->i_box_type == BOX_INFO )
1165     {
1166         /* Info box */
1167         int l = 0;
1168         DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1169
1170         if( p_input )
1171         {
1172             int i,j;
1173             vlc_mutex_lock( &p_input->p_item->lock );
1174             for ( i = 0; i < p_input->p_item->i_categories; i++ )
1175             {
1176                 info_category_t *p_category = p_input->p_item->pp_categories[i];
1177                 if( y >= y_end ) break;
1178                 MainBoxWrite( p_intf, l++, 1, "  [%s]", p_category->psz_name );
1179                 for ( j = 0; j < p_category->i_infos; j++ )
1180                 {
1181                     info_t *p_info = p_category->pp_infos[j];
1182                     if( y >= y_end ) break;
1183                     MainBoxWrite( p_intf, l++, 1, "      %s: %s", p_info->psz_name, p_info->psz_value );
1184                 }
1185             }
1186             vlc_mutex_unlock( &p_input->p_item->lock );
1187         }
1188         else
1189         {
1190             MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1191         }
1192         p_sys->i_box_lines_total = l;
1193         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1194         {
1195             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1196         }
1197
1198         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1199         {
1200             y += l - p_sys->i_box_start;
1201         }
1202         else
1203         {
1204             y += p_sys->i_box_lines;
1205         }
1206     }
1207     else if( p_sys->i_box_type == BOX_LOG )
1208     {
1209         int i_line = 0;
1210         int i_stop;
1211         int i_start;
1212
1213         DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1214
1215         i_start = p_intf->p_sys->p_sub->i_start;
1216
1217         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1218         i_stop = *p_intf->p_sys->p_sub->pi_stop;
1219         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1220
1221         for( ;; )
1222         {
1223             static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1224             if( i_line >= h - 2 )
1225             {
1226                 break;
1227             }
1228             i_stop--;
1229             i_line++;
1230             if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1231             if( i_stop == i_start )
1232             {
1233                 break;
1234             }
1235             mvnprintw( y + h-2-i_line, 1, COLS - 2, "   [%s] %s",
1236                       ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1237                       p_sys->p_sub->p_msg[i_stop].psz_msg );
1238         }
1239
1240         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1241         p_intf->p_sys->p_sub->i_start = i_stop;
1242         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1243         y = y_end;
1244     }
1245     else if( p_sys->i_box_type == BOX_BROWSE )
1246     {
1247         /* Playlist box */
1248         int        i_start, i_stop;
1249         int        i_item;
1250         DrawBox( p_sys->w, y++, 0, h, COLS, " Browse " );
1251
1252         if( p_sys->i_box_bidx >= p_sys->i_dir_entries ) p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
1253         if( p_sys->i_box_bidx < 0 ) p_sys->i_box_bidx = 0;
1254
1255         if( p_sys->i_box_bidx < (h - 2)/2 )
1256         {
1257             i_start = 0;
1258             i_stop = h - 2;
1259         }
1260         else if( p_sys->i_dir_entries - p_sys->i_box_bidx > (h - 2)/2 )
1261         {
1262             i_start = p_sys->i_box_bidx - (h - 2)/2;
1263             i_stop = i_start + h - 2;
1264         }
1265         else
1266         {
1267             i_stop = p_sys->i_dir_entries;
1268             i_start = p_sys->i_dir_entries - (h - 2);
1269         }
1270         if( i_start < 0 )
1271         {
1272             i_start = 0;
1273         }
1274         if( i_stop > p_sys->i_dir_entries )
1275         {
1276             i_stop = p_sys->i_dir_entries;
1277         }
1278
1279         for( i_item = i_start; i_item < i_stop; i_item++ )
1280         {
1281             vlc_bool_t b_selected = ( p_sys->i_box_bidx == i_item );
1282
1283             if( y >= y_end ) break;
1284             if( b_selected )
1285             {
1286                 attrset( A_REVERSE );
1287             }
1288             mvnprintw( y++, 1, COLS - 2, "%c %s", p_sys->pp_dir_entries[i_item]->b_file == VLC_TRUE ? '-' : '+',
1289                             p_sys->pp_dir_entries[i_item]->psz_path );
1290             if( b_selected )
1291             {
1292                 attroff ( A_REVERSE );
1293             }
1294         }
1295
1296     }
1297     else if( ( p_sys->i_box_type == BOX_PLAYLIST ||
1298                p_sys->i_box_type == BOX_SEARCH ||
1299                p_sys->i_box_type == BOX_OPEN  ) && p_sys->p_playlist )
1300     {
1301         /* Playlist box */
1302         playlist_t *p_playlist = p_sys->p_playlist;
1303         int        i_start, i_stop;
1304         int        i_item;
1305         DrawBox( p_sys->w, y++, 0, h, COLS, " Playlist " );
1306
1307         if( p_sys->i_box_plidx >= p_playlist->i_size ) p_sys->i_box_plidx = p_playlist->i_size - 1;
1308         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1309
1310         if( p_sys->i_box_plidx < (h - 2)/2 )
1311         {
1312             i_start = 0;
1313             i_stop = h - 2;
1314         }
1315         else if( p_playlist->i_size - p_sys->i_box_plidx > (h - 2)/2 )
1316         {
1317             i_start = p_sys->i_box_plidx - (h - 2)/2;
1318             i_stop = i_start + h - 2;
1319         }
1320         else
1321         {
1322             i_stop = p_playlist->i_size;
1323             i_start = p_playlist->i_size - (h - 2);
1324         }
1325         if( i_start < 0 )
1326         {
1327             i_start = 0;
1328         }
1329         if( i_stop > p_playlist->i_size )
1330         {
1331             i_stop = p_playlist->i_size;
1332         }
1333
1334         for( i_item = i_start; i_item < i_stop; i_item++ )
1335         {
1336             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1337             int c = p_playlist->i_index == i_item ? '>' : ' ';
1338
1339             if( y >= y_end ) break;
1340             if( b_selected )
1341             {
1342                 attrset( A_REVERSE );
1343             }
1344             if( !strcmp( p_playlist->pp_items[i_item]->input.psz_name,
1345                          p_playlist->pp_items[i_item]->input.psz_uri ) )
1346             {
1347                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s'",
1348                            c,
1349                            i_item,
1350                            p_playlist->pp_items[i_item]->input.psz_uri );
1351             }
1352             else
1353             {
1354                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s' (%s)",
1355                           c,
1356                           i_item,
1357                           p_playlist->pp_items[i_item]->input.psz_uri,
1358                           p_playlist->pp_items[i_item]->input.psz_name );
1359             }
1360             if( b_selected )
1361             {
1362                 attroff ( A_REVERSE );
1363             }
1364         }
1365     }
1366     else
1367     {
1368         y++;
1369     }
1370     if( p_sys->i_box_type == BOX_SEARCH )
1371     {
1372         DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1373         if( p_sys->psz_search_chain )
1374         {
1375             if( strlen( p_sys->psz_search_chain ) == 0 &&
1376                 p_sys->psz_old_search != NULL )
1377             {
1378                 /* Searching next entry */
1379                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1380             }
1381             else
1382             {
1383                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1384             }
1385         }
1386     }
1387     if( p_sys->i_box_type == BOX_OPEN )
1388     {
1389         if( p_sys->psz_open_chain )
1390         {
1391             DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1392             mvnprintw( 7, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1393         }
1394     }
1395
1396     while( y < y_end )
1397     {
1398         DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1399     }
1400
1401     refresh();
1402
1403     *t_last_refresh = time( 0 );
1404 }
1405
1406 static void Eject ( intf_thread_t *p_intf )
1407 {
1408     char *psz_device = NULL, *psz_parser, *psz_name;
1409
1410     /*
1411      * Get the active input
1412      * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
1413      * If it's neither of these, then return
1414      */
1415
1416     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1417                                                        FIND_ANYWHERE );
1418
1419     if( p_playlist == NULL )
1420     {
1421         return;
1422     }
1423
1424     vlc_mutex_lock( &p_playlist->object_lock );
1425
1426     if( p_playlist->i_index < 0 )
1427     {
1428         vlc_mutex_unlock( &p_playlist->object_lock );
1429         vlc_object_release( p_playlist );
1430         return;
1431     }
1432
1433     psz_name = p_playlist->pp_items[ p_playlist->i_index ]->input.psz_name;
1434
1435     if( psz_name )
1436     {
1437         if( !strncmp(psz_name, "dvd://", 4) )
1438         {
1439             switch( psz_name[strlen("dvd://")] )
1440             {
1441             case '\0':
1442             case '@':
1443                 psz_device = config_GetPsz( p_intf, "dvd" );
1444                 break;
1445             default:
1446                 /* Omit the first MRL-selector characters */
1447                 psz_device = strdup( psz_name + strlen("dvd://" ) );
1448                 break;
1449             }
1450         }
1451         else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
1452         {
1453             switch( psz_name[strlen(VCD_MRL)] )
1454             {
1455             case '\0':
1456             case '@':
1457                 psz_device = config_GetPsz( p_intf, VCD_MRL );
1458                 break;
1459             default:
1460                 /* Omit the beginning MRL-selector characters */
1461                 psz_device = strdup( psz_name + strlen(VCD_MRL) );
1462                 break;
1463             }
1464         }
1465         else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
1466         {
1467             switch( psz_name[strlen(CDDA_MRL)] )
1468             {
1469             case '\0':
1470             case '@':
1471                 psz_device = config_GetPsz( p_intf, "cd-audio" );
1472                 break;
1473             default:
1474                 /* Omit the beginning MRL-selector characters */
1475                 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
1476                 break;
1477             }
1478         }
1479         else
1480         {
1481             psz_device = strdup( psz_name );
1482         }
1483     }
1484
1485     vlc_mutex_unlock( &p_playlist->object_lock );
1486     vlc_object_release( p_playlist );
1487
1488     if( psz_device == NULL )
1489     {
1490         return;
1491     }
1492
1493     /* Remove what we have after @ */
1494     psz_parser = psz_device;
1495     for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
1496     {
1497         if( *psz_parser == '@' )
1498         {
1499             *psz_parser = '\0';
1500             break;
1501         }
1502     }
1503
1504     /* If there's a stream playing, we aren't allowed to eject ! */
1505     if( p_intf->p_sys->p_input == NULL )
1506     {
1507         msg_Dbg( p_intf, "ejecting %s", psz_device );
1508
1509         intf_Eject( p_intf, psz_device );
1510     }
1511
1512     free(psz_device);
1513     return;
1514 }
1515
1516 static void ReadDir( intf_thread_t *p_intf )
1517 {
1518     intf_sys_t     *p_sys = p_intf->p_sys;
1519     DIR *                       p_current_dir;
1520     struct dirent *             p_dir_content;
1521     int i;
1522
1523     if( p_sys->psz_current_dir && *p_sys->psz_current_dir )
1524     {
1525         /* Open the dir */
1526         p_current_dir = opendir( p_sys->psz_current_dir );
1527
1528         if( p_current_dir == NULL )
1529         {
1530             /* something went bad, get out of here ! */
1531 #ifdef HAVE_ERRNO_H
1532             msg_Warn( p_intf, "cannot open directory `%s' (%s)",
1533                       p_sys->psz_current_dir, strerror(errno));
1534 #else
1535             msg_Warn( p_intf, "cannot open directory `%s'", p_sys->psz_current_dir );
1536 #endif
1537             return;
1538         }
1539         
1540         /* Clean the old shit */
1541         for( i = 0; i < p_sys->i_dir_entries; i++ )
1542         {
1543             struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
1544             free( p_dir_entry->psz_path );
1545             REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
1546             free( p_dir_entry );
1547         }
1548         p_sys->pp_dir_entries = NULL;
1549         p_sys->i_dir_entries = 0;
1550
1551         /* get the first directory entry */
1552         p_dir_content = readdir( p_current_dir );
1553
1554         /* while we still have entries in the directory */
1555         while( p_dir_content != NULL )
1556         {
1557             struct dir_entry_t *p_dir_entry;
1558             int i_size_entry = strlen( p_sys->psz_current_dir ) +
1559                                strlen( p_dir_content->d_name ) + 2;
1560             char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
1561
1562             sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_dir_content->d_name );
1563
1564             if( ( p_dir_entry = malloc( sizeof( struct dir_entry_t) ) ) == NULL )
1565             {
1566                 free( psz_uri);
1567                 return;
1568             }
1569
1570 #if defined( S_ISDIR )
1571             struct stat stat_data;
1572             stat( psz_uri, &stat_data );
1573             if( S_ISDIR(stat_data.st_mode) )
1574 #elif defined( DT_DIR )
1575             if( p_dir_content->d_type & DT_DIR )
1576 #else
1577             if( 0 )
1578 #endif
1579             {
1580                 p_dir_entry->psz_path = strdup( p_dir_content->d_name );
1581                 p_dir_entry->b_file = VLC_FALSE;
1582                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1583                      p_sys->i_dir_entries, p_dir_entry );
1584             }
1585             else
1586             {
1587                 p_dir_entry->psz_path = strdup( p_dir_content->d_name );
1588                 p_dir_entry->b_file = VLC_TRUE;
1589                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1590                      p_sys->i_dir_entries, p_dir_entry );
1591             }
1592             free( psz_uri );
1593             /* Read next entry */
1594             p_dir_content = readdir( p_current_dir );
1595         }
1596         closedir( p_current_dir );
1597         return;
1598     }
1599     else
1600     {
1601         msg_Dbg( p_intf, "no current dir set" );
1602         return;
1603     }
1604 }
1605
1606 static void PlayPause( intf_thread_t *p_intf )
1607 {
1608     input_thread_t *p_input = p_intf->p_sys->p_input;
1609     vlc_value_t val;
1610
1611     if( p_input )
1612     {
1613         var_Get( p_input, "state", &val );
1614         if( val.i_int != PAUSE_S )
1615         {
1616             val.i_int = PAUSE_S;
1617         }
1618         else
1619         {
1620             val.i_int = PLAYING_S;
1621         }
1622         var_Set( p_input, "state", val );
1623     }
1624     else if( p_intf->p_sys->p_playlist )
1625     {
1626         playlist_Play( p_intf->p_sys->p_playlist );
1627     }
1628 }
1629
1630 /****************************************************************************
1631  *
1632  ****************************************************************************/
1633 static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title )
1634 {
1635     int i;
1636     int i_len;
1637
1638     if(  w > 3 && h > 2 )
1639     {
1640         if( title == NULL ) title = "";
1641         i_len = strlen( title );
1642
1643         if( i_len > w - 2 ) i_len = w - 2;
1644
1645         mvwaddch( win, y, x,    ACS_ULCORNER );
1646         mvwhline( win, y, x+1,  ACS_HLINE, ( w-i_len-2)/2 );
1647         mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
1648         mvwhline( win, y, x+(w-i_len)/2+i_len,  ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
1649         mvwaddch( win, y, x+w-1,ACS_URCORNER );
1650
1651         for( i = 0; i < h-2; i++ )
1652         {
1653             mvwaddch( win, y+i+1, x,     ACS_VLINE );
1654             mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
1655         }
1656
1657         mvwaddch( win, y+h-1, x,     ACS_LLCORNER );
1658         mvwhline( win, y+h-1, x+1,   ACS_HLINE, w - 2 );
1659         mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
1660     }
1661 }
1662
1663 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
1664 {
1665     if( w > 0 )
1666     {
1667         mvhline( y, x, ' ', w );
1668     }
1669 }
1670
1671 static void DrawLine( WINDOW *win, int y, int x, int w )
1672 {
1673     if( w > 0 )
1674     {
1675         attrset( A_REVERSE );
1676         mvhline( y, x, ' ', w );
1677         attroff ( A_REVERSE );
1678     }
1679 }