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