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