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