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