40e0f97665ffd1fcbb42ee2f7c7fee820ef693b2
[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
137     int             b_box_cleared;
138
139     msg_subscription_t* p_sub;                  /* message bank subscription */
140
141     char            *psz_search_chain;          /* for playlist searching    */
142     char            *psz_old_search;            /* for searching next        */
143     int             i_before_search;
144
145     char            *psz_open_chain;
146     
147     char            *psz_current_dir;
148     int             i_dir_entries;
149     struct dir_entry_t  **pp_dir_entries;
150 };
151
152 static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title );
153 static void DrawLine( WINDOW *win, int y, int x, int w );
154 static void DrawEmptyLine( WINDOW *win, int y, int x, int w );
155
156 /*****************************************************************************
157  * Open: initialize and create window
158  *****************************************************************************/
159 static int Open( vlc_object_t *p_this )
160 {
161     intf_thread_t *p_intf = (intf_thread_t *)p_this;
162     intf_sys_t    *p_sys;
163     vlc_value_t    val;
164
165     /* Allocate instance and initialize some members */
166     p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
167     p_sys->p_playlist = NULL;
168     p_sys->p_input = NULL;
169     p_sys->f_slider = 0.0;
170     p_sys->f_slider_old = 0.0;
171     p_sys->i_box_type = BOX_PLAYLIST;
172     p_sys->i_box_lines = 0;
173     p_sys->i_box_start= 0;
174     p_sys->i_box_lines_total = 0;
175     p_sys->b_box_plidx_follow = VLC_TRUE;
176     p_sys->b_box_cleared = VLC_FALSE;
177     p_sys->i_box_plidx = 0;
178     p_sys->p_sub = msg_Subscribe( p_intf );
179
180     /* Initialize the curses library */
181     p_sys->w = initscr();
182     keypad( p_sys->w, TRUE );
183     /* Don't do NL -> CR/NL */
184     nonl();
185     /* Take input chars one at a time */
186     cbreak();
187     /* Don't echo */
188     noecho();
189
190     curs_set(0);
191     timeout(0);
192
193     clear();
194
195     /* exported function */
196     p_intf->pf_run = Run;
197
198     /* Set quiet mode */
199     val.i_int = -1;
200     var_Set( p_intf->p_vlc, "verbose", val );
201
202     /* Initialize search chain */
203     p_sys->psz_search_chain = (char *)malloc( SEARCH_CHAIN_SIZE + 1 );
204     p_sys->psz_old_search = NULL;
205     p_sys->i_before_search = 0;
206
207     /* Initialize open chain */
208     p_sys->psz_open_chain = (char *)malloc( OPEN_CHAIN_SIZE + 1 );
209     
210     /* Initialize browser options */
211     var_Create( p_intf, "browse-dir", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
212     var_Get( p_intf, "browse-dir", &val);
213     
214     if( val.psz_string && *val.psz_string )
215     {
216         p_sys->psz_current_dir = strdup( val.psz_string);
217         free( val.psz_string );
218     }
219     else
220     {
221         p_sys->psz_current_dir = strdup( p_intf->p_vlc->psz_homedir );
222     }
223     
224     p_sys->i_dir_entries = 0;
225     p_sys->pp_dir_entries  = NULL;
226     ReadDir( p_intf );
227
228     return VLC_SUCCESS;
229 }
230
231 /*****************************************************************************
232  * Close: destroy interface window
233  *****************************************************************************/
234 static void Close( vlc_object_t *p_this )
235 {
236     intf_thread_t *p_intf = (intf_thread_t *)p_this;
237     intf_sys_t    *p_sys = p_intf->p_sys;
238     int i;
239
240     for( i = 0; i < p_sys->i_dir_entries; i++ )
241     {
242         struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
243         if( p_dir_entry->psz_path ) free( p_dir_entry->psz_path );
244         REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
245         if( p_dir_entry ) free( p_dir_entry );
246     }
247     p_sys->pp_dir_entries = NULL;
248     
249     if( p_sys->psz_current_dir ) free( p_sys->psz_current_dir );
250     if( p_sys->psz_search_chain ) free( p_sys->psz_search_chain );
251     if( p_sys->psz_old_search ) free( p_sys->psz_old_search );
252     if( p_sys->psz_open_chain ) free( p_sys->psz_open_chain );
253
254     if( p_sys->p_input )
255     {
256         vlc_object_release( p_sys->p_input );
257     }
258     if( p_sys->p_playlist )
259     {
260         vlc_object_release( p_sys->p_playlist );
261     }
262
263     /* Close the ncurses interface */
264     endwin();
265
266     msg_Unsubscribe( p_intf, p_sys->p_sub );
267
268     /* Destroy structure */
269     free( p_sys );
270 }
271
272 /*****************************************************************************
273  * Run: ncurses thread
274  *****************************************************************************/
275 static void Run( intf_thread_t *p_intf )
276 {
277     intf_sys_t    *p_sys = p_intf->p_sys;
278
279     int i_key;
280     time_t t_last_refresh;
281
282     /*
283      * force drawing the interface for the first time
284      */
285     t_last_refresh = ( time( 0 ) - 1);
286
287     while( !p_intf->b_die )
288     {
289         msleep( INTF_IDLE_SLEEP );
290
291         /* Update the input */
292         if( p_sys->p_playlist == NULL )
293         {
294             p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
295         }
296         if( p_sys->p_playlist )
297         {
298             vlc_mutex_lock( &p_sys->p_playlist->object_lock );
299             if( p_sys->p_input == NULL )
300             {
301                 p_sys->p_input = p_sys->p_playlist->p_input;
302                 if( p_sys->p_input )
303                 {
304                     if( !p_sys->p_input->b_dead )
305                     {
306                         vlc_object_yield( p_sys->p_input );
307                     }
308                 }
309             }
310             else if( p_sys->p_input->b_dead )
311             {
312                 vlc_object_release( p_sys->p_input );
313                 p_sys->p_input = NULL;
314                 p_sys->f_slider = p_sys->f_slider_old = 0.0;
315                 p_sys->b_box_cleared = VLC_FALSE;
316             }
317             vlc_mutex_unlock( &p_sys->p_playlist->object_lock );
318         }
319
320         if( p_sys->b_box_plidx_follow && p_sys->p_playlist->i_index >= 0 )
321         {
322             p_sys->i_box_plidx = p_sys->p_playlist->i_index;
323         }
324
325
326         while( ( i_key = getch()) != -1 )
327         {
328             /*
329              * HandleKey returns 1 if the screen needs to be redrawn
330              */
331             if ( HandleKey( p_intf, i_key ) )
332             {
333                 Redraw( p_intf, &t_last_refresh );
334             }
335         }
336         /* Hack */
337         if( p_sys->f_slider > 0.0001 && !p_sys->b_box_cleared )
338         {
339             clear();
340             Redraw( p_intf, &t_last_refresh );
341             p_sys->b_box_cleared = VLC_TRUE;
342         }
343
344         /*
345          * redraw the screen every second
346          */
347         if ( (time(0) - t_last_refresh) >= 1 )
348         {
349             ManageSlider ( p_intf );
350             Redraw( p_intf, &t_last_refresh );
351         }
352     }
353 }
354
355 /* following functions are local */
356
357 static int HandleKey( intf_thread_t *p_intf, int i_key )
358 {
359     intf_sys_t *p_sys = p_intf->p_sys;
360     vlc_value_t val;
361
362     if( p_sys->i_box_type == BOX_PLAYLIST && p_sys->p_playlist )
363     {
364         int b_ret = VLC_TRUE;
365
366         switch( i_key )
367         {
368             vlc_value_t val;
369             /* Playlist Settings */
370             case 'r':
371                 var_Get( p_sys->p_playlist, "random", &val );
372                 val.b_bool = !val.b_bool;
373                 var_Set( p_sys->p_playlist, "random", val );
374                 return 1;
375             case 'l':
376                 var_Get( p_sys->p_playlist, "loop", &val );
377                 val.b_bool = !val.b_bool;
378                 var_Set( p_sys->p_playlist, "loop", val );
379                 return 1;
380             case 'R':
381                 var_Get( p_sys->p_playlist, "repeat", &val );
382                 val.b_bool = !val.b_bool;
383                 var_Set( p_sys->p_playlist, "repeat", val );
384                 return 1;
385
386             /* Playlist sort */
387             case 'o':
388                 playlist_Sort( p_sys->p_playlist, SORT_TITLE, ORDER_NORMAL );
389                 return 1;
390             case 'O':
391                 playlist_Sort( p_sys->p_playlist, SORT_TITLE, ORDER_REVERSE );
392                 return 1;
393
394             /* Playlist navigation */
395             case KEY_HOME:
396                 p_sys->i_box_plidx = 0;
397                 break;
398             case KEY_END:
399                 p_sys->i_box_plidx = p_sys->p_playlist->i_size - 1;
400                 break;
401             case KEY_UP:
402                 p_sys->i_box_plidx--;
403                 break;
404             case KEY_DOWN:
405                 p_sys->i_box_plidx++;
406                 break;
407             case KEY_PPAGE:
408                 p_sys->i_box_plidx -= p_sys->i_box_lines;
409                 break;
410             case KEY_NPAGE:
411                 p_sys->i_box_plidx += p_sys->i_box_lines;
412                 break;
413             case 'D':
414             case KEY_BACKSPACE:
415             case KEY_DC:
416             {
417                 int i_item = p_sys->p_playlist->i_index;
418
419                 playlist_Delete( p_sys->p_playlist, p_sys->i_box_plidx );
420                 if( i_item < p_sys->p_playlist->i_size && i_item != p_sys->p_playlist->i_index )
421                 {
422                     playlist_Goto( p_sys->p_playlist, i_item );
423                 }
424                 break;
425             }
426
427             case KEY_ENTER:
428             case 0x0d:
429                 playlist_Goto( p_sys->p_playlist, p_sys->i_box_plidx );
430                 break;
431             default:
432                 b_ret = VLC_FALSE;
433                 break;
434         }
435
436         if( b_ret )
437         {
438             if( p_sys->i_box_plidx >= p_sys->p_playlist->i_size ) p_sys->i_box_plidx = p_sys->p_playlist->i_size - 1;
439             if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
440             if( p_sys->i_box_plidx == p_sys->p_playlist->i_index )
441                 p_sys->b_box_plidx_follow = VLC_TRUE;
442             else
443                 p_sys->b_box_plidx_follow = VLC_FALSE;
444             return 1;
445         }
446     }
447     if( p_sys->i_box_type == BOX_BROWSE )
448     {
449         /* Browser navigation */
450         switch( i_key )
451         {
452             case KEY_HOME:
453                 p_sys->i_box_plidx = 0;
454                 break;
455             case KEY_END:
456                 p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
457                 break;
458             case KEY_UP:
459                 p_sys->i_box_plidx--;
460                 break;
461             case KEY_DOWN:
462                 p_sys->i_box_plidx++;
463                 break;
464             case KEY_PPAGE:
465                 p_sys->i_box_plidx -= p_sys->i_box_lines;
466                 break;
467             case KEY_NPAGE:
468                 p_sys->i_box_plidx += p_sys->i_box_lines;
469                 break;
470
471             case KEY_ENTER:
472             case 0x0d:
473                 if( p_sys->pp_dir_entries[p_sys->i_box_plidx]->b_file )
474                 {
475                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
476                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_plidx]->psz_path ) + 2;
477                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
478
479                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_plidx]->psz_path );
480                     playlist_Add( p_sys->p_playlist, psz_uri,
481                                   psz_uri,
482                                   PLAYLIST_APPEND, PLAYLIST_END );
483                     p_sys->i_box_type = BOX_PLAYLIST;
484                     free( psz_uri );
485                 }
486                 else
487                 {
488                     int i_size_entry = strlen( p_sys->psz_current_dir ) +
489                                        strlen( p_sys->pp_dir_entries[p_sys->i_box_plidx]->psz_path ) + 2;
490                     char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
491
492                     sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_sys->pp_dir_entries[p_sys->i_box_plidx]->psz_path );
493                     
494                     p_sys->psz_current_dir = strdup( psz_uri );
495                     ReadDir( p_intf );
496                     free( psz_uri );
497                 }
498                 break;
499             default:
500                 break;
501
502             if( p_sys->i_box_plidx >= p_sys->i_dir_entries ) p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
503             if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
504             if( p_sys->i_box_plidx == p_sys->i_dir_entries )
505                 p_sys->b_box_plidx_follow = VLC_TRUE;
506             else
507                 p_sys->b_box_plidx_follow = VLC_FALSE;
508             return 1;
509         }
510     }
511     else if( p_sys->i_box_type == BOX_HELP || p_sys->i_box_type == BOX_INFO )
512     {
513         switch( i_key )
514         {
515             case KEY_HOME:
516                 p_sys->i_box_start = 0;
517                 return 1;
518             case KEY_END:
519                 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
520                 return 1;
521             case KEY_UP:
522                 if( p_sys->i_box_start > 0 ) p_sys->i_box_start--;
523                 return 1;
524             case KEY_DOWN:
525                 if( p_sys->i_box_start < p_sys->i_box_lines_total - 1 )
526                 {
527                     p_sys->i_box_start++;
528                 }
529                 return 1;
530             case KEY_PPAGE:
531                 p_sys->i_box_start -= p_sys->i_box_lines;
532                 if( p_sys->i_box_start < 0 ) p_sys->i_box_start = 0;
533                 return 1;
534             case KEY_NPAGE:
535                 p_sys->i_box_start += p_sys->i_box_lines;
536                 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
537                 {
538                     p_sys->i_box_start = p_sys->i_box_lines_total - 1;
539                 }
540                 return 1;
541             default:
542                 break;
543         }
544     }
545     else if( p_sys->i_box_type == BOX_NONE )
546     {
547         switch( i_key )
548         {
549             case KEY_HOME:
550                 p_sys->f_slider = 0;
551                 ManageSlider ( p_intf );
552                 return 1;
553             case KEY_END:
554                 p_sys->f_slider = 99.9;
555                 ManageSlider ( p_intf );
556                 return 1;
557             case KEY_UP:
558                 p_sys->f_slider += 5.0;
559                 if( p_sys->f_slider >= 99.0 ) p_sys->f_slider = 99.0;
560                 ManageSlider ( p_intf );
561                 return 1;
562             case KEY_DOWN:
563                 p_sys->f_slider -= 5.0;
564                 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
565                 ManageSlider ( p_intf );
566                 return 1;
567
568             default:
569                 break;
570         }
571     }
572     else if( p_sys->i_box_type == BOX_SEARCH && p_sys->psz_search_chain )
573     {
574         int i_chain_len;
575         i_chain_len = strlen( p_sys->psz_search_chain );
576         switch( i_key )
577         {
578             case 0x0c:      /* ^l */
579                 clear();
580                 return 1;
581             case KEY_ENTER:
582             case 0x0d:
583                 if( i_chain_len > 0 )
584                 {
585                     p_sys->psz_old_search = strdup( p_sys->psz_search_chain );
586                 }
587                 else if( p_sys->psz_old_search )
588                 {
589                     SearchPlaylist( p_intf, p_sys->psz_old_search );
590                 }
591                 p_sys->i_box_type = BOX_PLAYLIST;
592                 return 1;
593             case 0x1b:      /* Esc. */
594                 p_sys->i_box_plidx = p_sys->i_before_search;
595                 p_sys->i_box_type = BOX_PLAYLIST;
596                 return 1;
597             case KEY_BACKSPACE:
598                 if( i_chain_len > 0 )
599                 {
600                     p_sys->psz_search_chain[ i_chain_len - 1 ] = '\0';
601                 }
602                 break;
603             default:
604                 if( i_chain_len < SEARCH_CHAIN_SIZE )
605                 {
606                     p_sys->psz_search_chain[ i_chain_len++ ] = i_key;
607                     p_sys->psz_search_chain[ i_chain_len ] = 0;
608                 }
609                 break;
610         }
611         if( p_sys->psz_old_search )
612         {
613             free( p_sys->psz_old_search );
614             p_sys->psz_old_search = NULL;
615         }
616         SearchPlaylist( p_intf, p_sys->psz_search_chain );
617         return 1;
618     }
619     else if( p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain )
620     {
621         int i_chain_len;
622         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", p_input->psz_source );
1021
1022         /* State */
1023         var_Get( p_input, "state", &val );
1024         if( val.i_int == PLAYING_S )
1025         {
1026             mvnprintw( y++, 0, COLS, " State    : Playing" );
1027         }
1028         else if( val.i_int == PAUSE_S )
1029         {
1030             mvnprintw( y++, 0, COLS, " State    : Paused" );
1031         }
1032         else
1033         {
1034             y++;
1035         }
1036         if( val.i_int != INIT_S && val.i_int != END_S )
1037         {
1038             audio_volume_t i_volume;
1039
1040             /* Position */
1041             var_Get( p_input, "time", &val );
1042             msecstotimestr( buf1, val.i_time / 1000 );
1043
1044             var_Get( p_input, "length", &val );
1045             msecstotimestr( buf2, val.i_time / 1000 );
1046
1047             mvnprintw( y++, 0, COLS, " Position : %s/%s (%.2f%%)", buf1, buf2, p_sys->f_slider );
1048
1049             /* Volume */
1050             aout_VolumeGet( p_intf, &i_volume );
1051             mvnprintw( y++, 0, COLS, " Volume   : %i%%", i_volume*200/AOUT_VOLUME_MAX );
1052
1053             /* Title */
1054             if( !var_Get( p_input, "title", &val ) )
1055             {
1056                 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
1057                 if( val_list.p_list->i_count > 0 )
1058                 {
1059                     mvnprintw( y++, 0, COLS, " Title    : %d/%d", val.i_int, val_list.p_list->i_count );
1060                 }
1061                 var_Change( p_input, "title", VLC_VAR_FREELIST, &val_list, NULL );
1062             }
1063
1064             /* Chapter */
1065             if( !var_Get( p_input, "chapter", &val ) )
1066             {
1067                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
1068                 if( val_list.p_list->i_count > 0 )
1069                 {
1070                     mvnprintw( y++, 0, COLS, " Chapter  : %d/%d", val.i_int, val_list.p_list->i_count );
1071                 }
1072                 var_Change( p_input, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
1073             }
1074         }
1075         else
1076         {
1077             y++;
1078         }
1079     }
1080     else
1081     {
1082         mvnprintw( y++, 0, COLS, "Source: <no current item>" );
1083         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1084         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1085         DrawEmptyLine( p_sys->w, y++, 0, COLS );
1086     }
1087
1088     DrawBox( p_sys->w, y, 0, 3, COLS, "" );
1089     DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
1090     DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
1091     y += 3;
1092
1093     p_sys->i_box_y = y + 1;
1094     p_sys->i_box_lines = LINES - y - 2;
1095
1096     h = LINES - y;
1097     y_end = y + h - 1;
1098
1099     if( p_sys->i_box_type == BOX_HELP )
1100     {
1101         /* Help box */
1102         int l = 0;
1103         DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
1104
1105         MainBoxWrite( p_intf, l++, 1, "[Display]" );
1106         MainBoxWrite( p_intf, l++, 1, "     h,H         Show/Hide help box" );
1107         MainBoxWrite( p_intf, l++, 1, "     i           Show/Hide info box" );
1108         MainBoxWrite( p_intf, l++, 1, "     L           Show/Hide messages box" );
1109         MainBoxWrite( p_intf, l++, 1, "     P           Show/Hide playlist box" );
1110         MainBoxWrite( p_intf, l++, 1, "     B           Show/Hide filebrowser" );
1111         MainBoxWrite( p_intf, l++, 1, "" );
1112
1113         MainBoxWrite( p_intf, l++, 1, "[Global]" );
1114         MainBoxWrite( p_intf, l++, 1, "     q, Q        Quit" );
1115         MainBoxWrite( p_intf, l++, 1, "     s           Stop" );
1116         MainBoxWrite( p_intf, l++, 1, "     <space>     Pause/Play" );
1117         MainBoxWrite( p_intf, l++, 1, "     f           Toggle Fullscreen" );
1118         MainBoxWrite( p_intf, l++, 1, "     n, p        Next/Previous playlist item" );
1119         MainBoxWrite( p_intf, l++, 1, "     [, ]        Next/Previous title" );
1120         MainBoxWrite( p_intf, l++, 1, "     <, >        Next/Previous chapter" );
1121         MainBoxWrite( p_intf, l++, 1, "     <right>     Seek +1%%" );
1122         MainBoxWrite( p_intf, l++, 1, "     <left>      Seek -1%%" );
1123         MainBoxWrite( p_intf, l++, 1, "     a           Volume Up" );
1124         MainBoxWrite( p_intf, l++, 1, "     z           Volume Down" );
1125         MainBoxWrite( p_intf, l++, 1, "" );
1126
1127         MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
1128         MainBoxWrite( p_intf, l++, 1, "     r           Random" );
1129         MainBoxWrite( p_intf, l++, 1, "     l           Loop Playlist" );
1130         MainBoxWrite( p_intf, l++, 1, "     R           Repeat item" );
1131         MainBoxWrite( p_intf, l++, 1, "     o           Order Playlist by title" );
1132         MainBoxWrite( p_intf, l++, 1, "     O           Reverse order Playlist by title" );
1133         MainBoxWrite( p_intf, l++, 1, "     /           Look for an item" );
1134         MainBoxWrite( p_intf, l++, 1, "     A           Add an entry" );
1135         MainBoxWrite( p_intf, l++, 1, "     D, <del>    Delete an entry" );
1136         MainBoxWrite( p_intf, l++, 1, "     <backspace> Delete an entry" );
1137         MainBoxWrite( p_intf, l++, 1, "" );
1138
1139         MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
1140         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Navigate through the box line by line" );
1141         MainBoxWrite( p_intf, l++, 1, "     <pgup>,<pgdown> Navigate through the box page by page" );
1142         MainBoxWrite( p_intf, l++, 1, "" );
1143
1144         MainBoxWrite( p_intf, l++, 1, "[Player]" );
1145         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Seek +/-5%%" );
1146         MainBoxWrite( p_intf, l++, 1, "" );
1147
1148         MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
1149         MainBoxWrite( p_intf, l++, 1, "     Ctrl-l          Refresh the screen" );
1150
1151         p_sys->i_box_lines_total = l;
1152         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1153         {
1154             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1155         }
1156
1157         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1158         {
1159             y += l - p_sys->i_box_start;
1160         }
1161         else
1162         {
1163             y += p_sys->i_box_lines;
1164         }
1165     }
1166     else if( p_sys->i_box_type == BOX_INFO )
1167     {
1168         /* Info box */
1169         int l = 0;
1170         DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1171
1172         if( p_input )
1173         {
1174             int i,j;
1175             vlc_mutex_lock( &p_input->p_item->lock );
1176             for ( i = 0; i < p_input->p_item->i_categories; i++ )
1177             {
1178                 info_category_t *p_category = p_input->p_item->pp_categories[i];
1179                 if( y >= y_end ) break;
1180                 MainBoxWrite( p_intf, l++, 1, "  [%s]", p_category->psz_name );
1181                 for ( j = 0; j < p_category->i_infos; j++ )
1182                 {
1183                     info_t *p_info = p_category->pp_infos[j];
1184                     if( y >= y_end ) break;
1185                     MainBoxWrite( p_intf, l++, 1, "      %s: %s", p_info->psz_name, p_info->psz_value );
1186                 }
1187             }
1188             vlc_mutex_unlock( &p_input->p_item->lock );
1189         }
1190         else
1191         {
1192             MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1193         }
1194         p_sys->i_box_lines_total = l;
1195         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1196         {
1197             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1198         }
1199
1200         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1201         {
1202             y += l - p_sys->i_box_start;
1203         }
1204         else
1205         {
1206             y += p_sys->i_box_lines;
1207         }
1208     }
1209     else if( p_sys->i_box_type == BOX_LOG )
1210     {
1211         int i_line = 0;
1212         int i_stop;
1213         int i_start;
1214
1215         DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1216
1217         i_start = p_intf->p_sys->p_sub->i_start;
1218
1219         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1220         i_stop = *p_intf->p_sys->p_sub->pi_stop;
1221         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1222
1223         for( ;; )
1224         {
1225             static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1226             if( i_line >= h - 2 )
1227             {
1228                 break;
1229             }
1230             i_stop--;
1231             i_line++;
1232             if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1233             if( i_stop == i_start )
1234             {
1235                 break;
1236             }
1237             mvnprintw( y + h-2-i_line, 1, COLS - 2, "   [%s] %s",
1238                       ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1239                       p_sys->p_sub->p_msg[i_stop].psz_msg );
1240         }
1241
1242         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1243         p_intf->p_sys->p_sub->i_start = i_stop;
1244         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1245         y = y_end;
1246     }
1247     else if( p_sys->i_box_type == BOX_BROWSE )
1248     {
1249         /* Playlist box */
1250         int        i_start, i_stop;
1251         int        i_item;
1252         DrawBox( p_sys->w, y++, 0, h, COLS, " Browse " );
1253
1254         if( p_sys->i_box_plidx >= p_sys->i_dir_entries ) p_sys->i_box_plidx = p_sys->i_dir_entries - 1;
1255         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1256
1257         if( p_sys->i_box_plidx < (h - 2)/2 )
1258         {
1259             i_start = 0;
1260             i_stop = h - 2;
1261         }
1262         else if( p_sys->i_dir_entries - p_sys->i_box_plidx > (h - 2)/2 )
1263         {
1264             i_start = p_sys->i_box_plidx - (h - 2)/2;
1265             i_stop = i_start + h - 2;
1266         }
1267         else
1268         {
1269             i_stop = p_sys->i_dir_entries;
1270             i_start = p_sys->i_dir_entries - (h - 2);
1271         }
1272         if( i_start < 0 )
1273         {
1274             i_start = 0;
1275         }
1276         if( i_stop > p_sys->i_dir_entries )
1277         {
1278             i_stop = p_sys->i_dir_entries;
1279         }
1280
1281         for( i_item = i_start; i_item < i_stop; i_item++ )
1282         {
1283             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1284
1285             if( y >= y_end ) break;
1286             if( b_selected )
1287             {
1288                 attrset( A_REVERSE );
1289             }
1290             mvnprintw( y++, 1, COLS - 2, "%c %s", p_sys->pp_dir_entries[i_item]->b_file == VLC_TRUE ? '-' : '+',
1291                             p_sys->pp_dir_entries[i_item]->psz_path );
1292             if( b_selected )
1293             {
1294                 attroff ( A_REVERSE );
1295             }
1296         }
1297
1298     }
1299     else if( ( p_sys->i_box_type == BOX_PLAYLIST ||
1300                p_sys->i_box_type == BOX_SEARCH ||
1301                p_sys->i_box_type == BOX_OPEN  ) && p_sys->p_playlist )
1302     {
1303         /* Playlist box */
1304         playlist_t *p_playlist = p_sys->p_playlist;
1305         int        i_start, i_stop;
1306         int        i_item;
1307         DrawBox( p_sys->w, y++, 0, h, COLS, " Playlist " );
1308
1309         if( p_sys->i_box_plidx >= p_playlist->i_size ) p_sys->i_box_plidx = p_playlist->i_size - 1;
1310         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1311
1312         if( p_sys->i_box_plidx < (h - 2)/2 )
1313         {
1314             i_start = 0;
1315             i_stop = h - 2;
1316         }
1317         else if( p_playlist->i_size - p_sys->i_box_plidx > (h - 2)/2 )
1318         {
1319             i_start = p_sys->i_box_plidx - (h - 2)/2;
1320             i_stop = i_start + h - 2;
1321         }
1322         else
1323         {
1324             i_stop = p_playlist->i_size;
1325             i_start = p_playlist->i_size - (h - 2);
1326         }
1327         if( i_start < 0 )
1328         {
1329             i_start = 0;
1330         }
1331         if( i_stop > p_playlist->i_size )
1332         {
1333             i_stop = p_playlist->i_size;
1334         }
1335
1336         for( i_item = i_start; i_item < i_stop; i_item++ )
1337         {
1338             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1339             int c = p_playlist->i_index == i_item ? '>' : ' ';
1340
1341             if( y >= y_end ) break;
1342             if( b_selected )
1343             {
1344                 attrset( A_REVERSE );
1345             }
1346             if( !strcmp( p_playlist->pp_items[i_item]->input.psz_name,
1347                          p_playlist->pp_items[i_item]->input.psz_uri ) )
1348             {
1349                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s'",
1350                            c,
1351                            i_item,
1352                            p_playlist->pp_items[i_item]->input.psz_uri );
1353             }
1354             else
1355             {
1356                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s' (%s)",
1357                           c,
1358                           i_item,
1359                           p_playlist->pp_items[i_item]->input.psz_uri,
1360                           p_playlist->pp_items[i_item]->input.psz_name );
1361             }
1362             if( b_selected )
1363             {
1364                 attroff ( A_REVERSE );
1365             }
1366         }
1367     }
1368     else
1369     {
1370         y++;
1371     }
1372     if( p_sys->i_box_type == BOX_SEARCH )
1373     {
1374         DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1375         if( p_sys->psz_search_chain )
1376         {
1377             if( strlen( p_sys->psz_search_chain ) == 0 &&
1378                 p_sys->psz_old_search != NULL )
1379             {
1380                 /* Searching next entry */
1381                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1382             }
1383             else
1384             {
1385                 mvnprintw( 7, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1386             }
1387         }
1388     }
1389     if( p_sys->i_box_type == BOX_OPEN )
1390     {
1391         if( p_sys->psz_open_chain )
1392         {
1393             DrawEmptyLine( p_sys->w, 7, 1, COLS-2 );
1394             mvnprintw( 7, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1395         }
1396     }
1397
1398     while( y < y_end )
1399     {
1400         DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1401     }
1402
1403     refresh();
1404
1405     *t_last_refresh = time( 0 );
1406 }
1407
1408 static void Eject ( intf_thread_t *p_intf )
1409 {
1410     char *psz_device = NULL, *psz_parser, *psz_name;
1411
1412     /*
1413      * Get the active input
1414      * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
1415      * If it's neither of these, then return
1416      */
1417
1418     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1419                                                        FIND_ANYWHERE );
1420
1421     if( p_playlist == NULL )
1422     {
1423         return;
1424     }
1425
1426     vlc_mutex_lock( &p_playlist->object_lock );
1427
1428     if( p_playlist->i_index < 0 )
1429     {
1430         vlc_mutex_unlock( &p_playlist->object_lock );
1431         vlc_object_release( p_playlist );
1432         return;
1433     }
1434
1435     psz_name = p_playlist->pp_items[ p_playlist->i_index ]->input.psz_name;
1436
1437     if( psz_name )
1438     {
1439         if( !strncmp(psz_name, "dvd://", 4) )
1440         {
1441             switch( psz_name[strlen("dvd://")] )
1442             {
1443             case '\0':
1444             case '@':
1445                 psz_device = config_GetPsz( p_intf, "dvd" );
1446                 break;
1447             default:
1448                 /* Omit the first MRL-selector characters */
1449                 psz_device = strdup( psz_name + strlen("dvd://" ) );
1450                 break;
1451             }
1452         }
1453         else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
1454         {
1455             switch( psz_name[strlen(VCD_MRL)] )
1456             {
1457             case '\0':
1458             case '@':
1459                 psz_device = config_GetPsz( p_intf, VCD_MRL );
1460                 break;
1461             default:
1462                 /* Omit the beginning MRL-selector characters */
1463                 psz_device = strdup( psz_name + strlen(VCD_MRL) );
1464                 break;
1465             }
1466         }
1467         else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
1468         {
1469             switch( psz_name[strlen(CDDA_MRL)] )
1470             {
1471             case '\0':
1472             case '@':
1473                 psz_device = config_GetPsz( p_intf, "cd-audio" );
1474                 break;
1475             default:
1476                 /* Omit the beginning MRL-selector characters */
1477                 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
1478                 break;
1479             }
1480         }
1481         else
1482         {
1483             psz_device = strdup( psz_name );
1484         }
1485     }
1486
1487     vlc_mutex_unlock( &p_playlist->object_lock );
1488     vlc_object_release( p_playlist );
1489
1490     if( psz_device == NULL )
1491     {
1492         return;
1493     }
1494
1495     /* Remove what we have after @ */
1496     psz_parser = psz_device;
1497     for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
1498     {
1499         if( *psz_parser == '@' )
1500         {
1501             *psz_parser = '\0';
1502             break;
1503         }
1504     }
1505
1506     /* If there's a stream playing, we aren't allowed to eject ! */
1507     if( p_intf->p_sys->p_input == NULL )
1508     {
1509         msg_Dbg( p_intf, "ejecting %s", psz_device );
1510
1511         intf_Eject( p_intf, psz_device );
1512     }
1513
1514     free(psz_device);
1515     return;
1516 }
1517
1518 static void ReadDir( intf_thread_t *p_intf )
1519 {
1520     intf_sys_t     *p_sys = p_intf->p_sys;
1521     DIR *                       p_current_dir;
1522     struct dirent *             p_dir_content;
1523     int i;
1524
1525     if( p_sys->psz_current_dir && *p_sys->psz_current_dir )
1526     {
1527         /* Open the dir */
1528         p_current_dir = opendir( p_sys->psz_current_dir );
1529
1530         if( p_current_dir == NULL )
1531         {
1532             /* something went bad, get out of here ! */
1533 #ifdef HAVE_ERRNO_H
1534             msg_Warn( p_intf, "cannot open directory `%s' (%s)",
1535                       p_sys->psz_current_dir, strerror(errno));
1536 #else
1537             msg_Warn( p_intf, "cannot open directory `%s'", p_sys->psz_current_dir );
1538 #endif
1539             return;
1540         }
1541         
1542         /* Clean the old shit */
1543         for( i = 0; i < p_sys->i_dir_entries; i++ )
1544         {
1545             struct dir_entry_t *p_dir_entry = p_sys->pp_dir_entries[i];
1546             free( p_dir_entry->psz_path );
1547             REMOVE_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries, i );
1548             free( p_dir_entry );
1549         }
1550         p_sys->pp_dir_entries = NULL;
1551         p_sys->i_dir_entries = 0;
1552
1553         /* get the first directory entry */
1554         p_dir_content = readdir( p_current_dir );
1555
1556         /* while we still have entries in the directory */
1557         while( p_dir_content != NULL )
1558         {
1559             struct dir_entry_t *p_dir_entry;
1560             int i_size_entry = strlen( p_sys->psz_current_dir ) +
1561                                strlen( p_dir_content->d_name ) + 2;
1562             char *psz_uri = (char *)malloc( sizeof(char)*i_size_entry);
1563
1564             sprintf( psz_uri, "%s/%s", p_sys->psz_current_dir, p_dir_content->d_name );
1565
1566             if( ( p_dir_entry = malloc( sizeof( struct dir_entry_t) ) ) == NULL )
1567             {
1568                 free( psz_uri);
1569                 return;
1570             }
1571
1572 #if defined( S_ISDIR )
1573             struct stat stat_data;
1574             stat( psz_uri, &stat_data );
1575             if( S_ISDIR(stat_data.st_mode) )
1576 #elif defined( DT_DIR )
1577             if( p_dir_content->d_type & DT_DIR )
1578 #else
1579             if( 0 )
1580 #endif
1581             {
1582                 p_dir_entry->psz_path = strdup( p_dir_content->d_name );
1583                 p_dir_entry->b_file = VLC_FALSE;
1584                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1585                      p_sys->i_dir_entries, p_dir_entry );
1586             }
1587             else
1588             {
1589                 p_dir_entry->psz_path = strdup( p_dir_content->d_name );
1590                 p_dir_entry->b_file = VLC_TRUE;
1591                 INSERT_ELEM( p_sys->pp_dir_entries, p_sys->i_dir_entries,
1592                      p_sys->i_dir_entries, p_dir_entry );
1593             }
1594             free( psz_uri );
1595             /* Read next entry */
1596             p_dir_content = readdir( p_current_dir );
1597         }
1598         closedir( p_current_dir );
1599         return;
1600     }
1601     else
1602     {
1603         msg_Dbg( p_intf, "no current dir set" );
1604         return;
1605     }
1606 }
1607
1608 static void PlayPause( intf_thread_t *p_intf )
1609 {
1610     input_thread_t *p_input = p_intf->p_sys->p_input;
1611     vlc_value_t val;
1612
1613     if( p_input )
1614     {
1615         var_Get( p_input, "state", &val );
1616         if( val.i_int != PAUSE_S )
1617         {
1618             val.i_int = PAUSE_S;
1619         }
1620         else
1621         {
1622             val.i_int = PLAYING_S;
1623         }
1624         var_Set( p_input, "state", val );
1625     }
1626     else if( p_intf->p_sys->p_playlist )
1627     {
1628         playlist_Play( p_intf->p_sys->p_playlist );
1629     }
1630 }
1631
1632 /****************************************************************************
1633  *
1634  ****************************************************************************/
1635 static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title )
1636 {
1637     int i;
1638     int i_len;
1639
1640     if(  w > 3 && h > 2 )
1641     {
1642         if( title == NULL ) title = "";
1643         i_len = strlen( title );
1644
1645         if( i_len > w - 2 ) i_len = w - 2;
1646
1647         mvwaddch( win, y, x,    ACS_ULCORNER );
1648         mvwhline( win, y, x+1,  ACS_HLINE, ( w-i_len-2)/2 );
1649         mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
1650         mvwhline( win, y, x+(w-i_len)/2+i_len,  ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
1651         mvwaddch( win, y, x+w-1,ACS_URCORNER );
1652
1653         for( i = 0; i < h-2; i++ )
1654         {
1655             mvwaddch( win, y+i+1, x,     ACS_VLINE );
1656             mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
1657         }
1658
1659         mvwaddch( win, y+h-1, x,     ACS_LLCORNER );
1660         mvwhline( win, y+h-1, x+1,   ACS_HLINE, w - 2 );
1661         mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
1662     }
1663 }
1664
1665 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
1666 {
1667     if( w > 0 )
1668     {
1669         mvhline( y, x, ' ', w );
1670     }
1671 }
1672
1673 static void DrawLine( WINDOW *win, int y, int x, int w )
1674 {
1675     if( w > 0 )
1676     {
1677         attrset( A_REVERSE );
1678         mvhline( y, x, ' ', w );
1679         attroff ( A_REVERSE );
1680     }
1681 }