aff01e632252ce53723c169c283499209d839671
[vlc.git] / modules / gui / ncurses / 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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <stdio.h>
32 #include <time.h>
33
34 #include <curses.h>
35
36 #include <vlc/vlc.h>
37 #include <vlc/intf.h>
38 #include <vlc/vout.h>
39
40 #ifdef HAVE_CDDAX
41 #define CDDA_MRL "cddax://"
42 #else
43 #define CDDA_MRL "cdda://"
44 #endif
45
46 #ifdef HAVE_VCDX
47 #define VCD_MRL "vcdx://"
48 #else
49 #define VCD_MRL "vcdx://"
50 #endif
51
52 #define SEARCH_CHAIN_SIZE 20
53 #define OPEN_CHAIN_SIZE 50
54
55 /*****************************************************************************
56  * Local prototypes.
57  *****************************************************************************/
58 static int  Open           ( vlc_object_t * );
59 static void Close          ( vlc_object_t * );
60
61 static void Run            ( intf_thread_t * );
62 static void PlayPause      ( intf_thread_t * );
63 static void Eject          ( intf_thread_t * );
64
65 static int  HandleKey      ( intf_thread_t *, int );
66 static void Redraw         ( intf_thread_t *, time_t * );
67 static void SearchPlaylist ( intf_thread_t *, char * );
68 static void ManageSlider   ( intf_thread_t * );
69
70 /*****************************************************************************
71  * Module descriptor
72  *****************************************************************************/
73 vlc_module_begin();
74     set_description( _("ncurses interface") );
75     set_capability( "interface", 10 );
76     set_callbacks( Open, Close );
77     add_shortcut( "curses" );
78 vlc_module_end();
79
80 /*****************************************************************************
81  * intf_sys_t: description and status of ncurses interface
82  *****************************************************************************/
83 enum
84 {
85     BOX_NONE,
86     BOX_HELP,
87     BOX_INFO,
88     BOX_LOG,
89     BOX_PLAYLIST,
90     BOX_SEARCH,
91     BOX_OPEN
92 };
93 struct intf_sys_t
94 {
95     playlist_t     *p_playlist;
96     input_thread_t *p_input;
97
98     float           f_slider;
99     float           f_slider_old;
100
101     WINDOW          *w;
102
103     int             i_box_type;
104     int             i_box_y;
105     int             i_box_lines;
106     int             i_box_lines_total;
107     int             i_box_start;
108
109     int             i_box_plidx;    /* Playlist index */
110     int             b_box_plidx_follow;
111
112     int             b_box_cleared;
113
114     msg_subscription_t* p_sub;                  /* message bank subscription */
115
116     char            *psz_search_chain;          /* for playlist searching    */
117     char            *psz_old_search;            /* for searching next        */
118     int             i_before_search;
119
120     char            *psz_open_chain;
121 };
122
123 static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title );
124 static void DrawLine( WINDOW *win, int y, int x, int w );
125 static void DrawEmptyLine( WINDOW *win, int y, int x, int w );
126
127 /*****************************************************************************
128  * Open: initialize and create window
129  *****************************************************************************/
130 static int Open( vlc_object_t *p_this )
131 {
132     intf_thread_t *p_intf = (intf_thread_t *)p_this;
133     intf_sys_t    *p_sys;
134     vlc_value_t    val;
135
136     /* Allocate instance and initialize some members */
137     p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
138     p_sys->p_playlist = NULL;
139     p_sys->p_input = NULL;
140     p_sys->f_slider = 0.0;
141     p_sys->f_slider_old = 0.0;
142     p_sys->i_box_type = BOX_PLAYLIST;
143     p_sys->i_box_lines = 0;
144     p_sys->i_box_start= 0;
145     p_sys->i_box_lines_total = 0;
146     p_sys->b_box_plidx_follow = VLC_TRUE;
147     p_sys->b_box_cleared = VLC_FALSE;
148     p_sys->i_box_plidx = 0;
149     p_sys->p_sub = msg_Subscribe( p_intf );
150
151     /* Initialize the curses library */
152     p_sys->w = initscr();
153     keypad( p_sys->w, TRUE );
154     /* Don't do NL -> CR/NL */
155     nonl();
156     /* Take input chars one at a time */
157     cbreak();
158     /* Don't echo */
159     noecho();
160
161     curs_set(0);
162     timeout(0);
163
164     clear();
165
166     /* exported function */
167     p_intf->pf_run = Run;
168
169     /* Set quiet mode */
170     val.i_int = -1;
171     var_Set( p_intf->p_vlc, "verbose", val );
172
173     /* Initialize search chain */
174     p_sys->psz_search_chain = (char *)malloc( SEARCH_CHAIN_SIZE + 1 );
175     p_sys->psz_old_search = NULL;
176     p_sys->i_before_search = 0;
177
178     /* Initialize open chain */
179     p_sys->psz_open_chain = (char *)malloc( OPEN_CHAIN_SIZE + 1 );
180
181     return VLC_SUCCESS;
182 }
183
184 /*****************************************************************************
185  * Close: destroy interface window
186  *****************************************************************************/
187 static void Close( vlc_object_t *p_this )
188 {
189     intf_thread_t *p_intf = (intf_thread_t *)p_this;
190     intf_sys_t    *p_sys = p_intf->p_sys;
191
192     if( p_sys->psz_search_chain ) free( p_sys->psz_search_chain );
193     if( p_sys->psz_old_search ) free( p_sys->psz_old_search );
194     if( p_sys->psz_open_chain ) free( p_sys->psz_open_chain );
195
196     if( p_sys->p_input )
197     {
198         vlc_object_release( p_sys->p_input );
199     }
200     if( p_sys->p_playlist )
201     {
202         vlc_object_release( p_sys->p_playlist );
203     }
204
205     /* Close the ncurses interface */
206     endwin();
207
208     msg_Unsubscribe( p_intf, p_sys->p_sub );
209
210     /* Destroy structure */
211     free( p_sys );
212 }
213
214 /*****************************************************************************
215  * Run: ncurses thread
216  *****************************************************************************/
217 static void Run( intf_thread_t *p_intf )
218 {
219     intf_sys_t    *p_sys = p_intf->p_sys;
220
221     int i_key;
222     time_t t_last_refresh;
223
224     /*
225      * force drawing the interface for the first time
226      */
227     t_last_refresh = ( time( 0 ) - 1);
228
229     while( !p_intf->b_die )
230     {
231         msleep( INTF_IDLE_SLEEP );
232
233         /* Update the input */
234         if( p_sys->p_playlist == NULL )
235         {
236             p_sys->p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
237         }
238         if( p_sys->p_playlist )
239         {
240             vlc_mutex_lock( &p_sys->p_playlist->object_lock );
241             if( p_sys->p_input == NULL )
242             {
243                 p_sys->p_input = p_sys->p_playlist->p_input;
244                 if( p_sys->p_input )
245                 {
246                     if( !p_sys->p_input->b_dead )
247                     {
248                         vlc_object_yield( p_sys->p_input );
249                     }
250                 }
251             }
252             else if( p_sys->p_input->b_dead )
253             {
254                 vlc_object_release( p_sys->p_input );
255                 p_sys->p_input = NULL;
256                 p_sys->f_slider = p_sys->f_slider_old = 0.0;
257                 p_sys->b_box_cleared = VLC_FALSE;
258             }
259             vlc_mutex_unlock( &p_sys->p_playlist->object_lock );
260         }
261
262         if( p_sys->b_box_plidx_follow && p_sys->p_playlist->i_index >= 0 )
263         {
264             p_sys->i_box_plidx = p_sys->p_playlist->i_index;
265         }
266
267
268         while( ( i_key = getch()) != -1 )
269         {
270             /*
271              * HandleKey returns 1 if the screen needs to be redrawn
272              */
273             if ( HandleKey( p_intf, i_key ) )
274             {
275                 Redraw( p_intf, &t_last_refresh );
276             }
277         }
278         /* Hack */
279         if( p_sys->f_slider > 0.0001 && !p_sys->b_box_cleared )
280         {
281             clear();
282             Redraw( p_intf, &t_last_refresh );
283             p_sys->b_box_cleared = VLC_TRUE;
284         }
285
286         /*
287          * redraw the screen every second
288          */
289         if ( (time(0) - t_last_refresh) >= 1 )
290         {
291             ManageSlider ( p_intf );
292             Redraw( p_intf, &t_last_refresh );
293         }
294     }
295 }
296
297 /* following functions are local */
298
299 static int HandleKey( intf_thread_t *p_intf, int i_key )
300 {
301     intf_sys_t *p_sys = p_intf->p_sys;
302     vlc_value_t val;
303
304     if( p_sys->i_box_type == BOX_PLAYLIST && p_sys->p_playlist )
305     {
306         int b_ret = VLC_TRUE;
307
308         switch( i_key )
309         {
310             /* Playlist sort */
311             case 'r':
312                 playlist_Sort( p_sys->p_playlist, SORT_RANDOM, ORDER_NORMAL );
313                 return 1;
314             case 'o':
315                 playlist_Sort( p_sys->p_playlist, SORT_TITLE, ORDER_NORMAL );
316                 return 1;
317             case 'O':
318                 playlist_Sort( p_sys->p_playlist, SORT_TITLE, ORDER_REVERSE );
319                 return 1;
320
321             /* Playlist navigation */
322             case KEY_HOME:
323                 p_sys->i_box_plidx = 0;
324                 break;
325             case KEY_END:
326                 p_sys->i_box_plidx = p_sys->p_playlist->i_size - 1;
327                 break;
328             case KEY_UP:
329                 p_sys->i_box_plidx--;
330                 break;
331             case KEY_DOWN:
332                 p_sys->i_box_plidx++;
333                 break;
334             case KEY_PPAGE:
335                 p_sys->i_box_plidx -= p_sys->i_box_lines;
336                 break;
337             case KEY_NPAGE:
338                 p_sys->i_box_plidx += p_sys->i_box_lines;
339                 break;
340             case KEY_BACKSPACE:
341             case KEY_DC:
342             {
343                 int i_item = p_sys->p_playlist->i_index;
344
345                 playlist_Delete( p_sys->p_playlist, p_sys->i_box_plidx );
346                 if( i_item < p_sys->p_playlist->i_size && i_item != p_sys->p_playlist->i_index )
347                 {
348                     playlist_Goto( p_sys->p_playlist, i_item );
349                 }
350                 break;
351             }
352
353             case KEY_ENTER:
354             case 0x0d:
355                 playlist_Goto( p_sys->p_playlist, p_sys->i_box_plidx );
356                 break;
357             default:
358                 b_ret = VLC_FALSE;
359                 break;
360         }
361
362         if( b_ret )
363         {
364             if( p_sys->i_box_plidx >= p_sys->p_playlist->i_size ) p_sys->i_box_plidx = p_sys->p_playlist->i_size - 1;
365             if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
366             if( p_sys->i_box_plidx == p_sys->p_playlist->i_index )
367                 p_sys->b_box_plidx_follow = VLC_TRUE;
368             else
369                 p_sys->b_box_plidx_follow = VLC_FALSE;
370             return 1;
371         }
372     }
373     else if( p_sys->i_box_type == BOX_HELP || p_sys->i_box_type == BOX_INFO )
374     {
375         switch( i_key )
376         {
377             case KEY_HOME:
378                 p_sys->i_box_start = 0;
379                 return 1;
380             case KEY_END:
381                 p_sys->i_box_start = p_sys->i_box_lines_total - 1;
382                 return 1;
383             case KEY_UP:
384                 if( p_sys->i_box_start > 0 ) p_sys->i_box_start--;
385                 return 1;
386             case KEY_DOWN:
387                 if( p_sys->i_box_start < p_sys->i_box_lines_total - 1 )
388                 {
389                     p_sys->i_box_start++;
390                 }
391                 return 1;
392             case KEY_PPAGE:
393                 p_sys->i_box_start -= p_sys->i_box_lines;
394                 if( p_sys->i_box_start < 0 ) p_sys->i_box_start = 0;
395                 return 1;
396             case KEY_NPAGE:
397                 p_sys->i_box_start += p_sys->i_box_lines;
398                 if( p_sys->i_box_start >= p_sys->i_box_lines_total )
399                 {
400                     p_sys->i_box_start = p_sys->i_box_lines_total - 1;
401                 }
402                 return 1;
403             default:
404                 break;
405         }
406     }
407     else if( p_sys->i_box_type == BOX_NONE )
408     {
409         switch( i_key )
410         {
411             case KEY_HOME:
412                 p_sys->f_slider = 0;
413                 ManageSlider ( p_intf );
414                 return 1;
415             case KEY_END:
416                 p_sys->f_slider = 99.9;
417                 ManageSlider ( p_intf );
418                 return 1;
419             case KEY_UP:
420                 p_sys->f_slider += 5.0;
421                 if( p_sys->f_slider >= 99.0 ) p_sys->f_slider = 99.0;
422                 ManageSlider ( p_intf );
423                 return 1;
424             case KEY_DOWN:
425                 p_sys->f_slider -= 5.0;
426                 if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
427                 ManageSlider ( p_intf );
428                 return 1;
429
430             default:
431                 break;
432         }
433     }
434     else if( p_sys->i_box_type == BOX_SEARCH && p_sys->psz_search_chain )
435     {
436         int i_chain_len;
437         i_chain_len = strlen( p_sys->psz_search_chain );
438         switch( i_key )
439         {
440             case 0x0c:      /* ^l */
441                 clear();
442                 return 1;
443             case KEY_ENTER:
444             case 0x0d:
445                 if( i_chain_len > 0 )
446                 {
447                     p_sys->psz_old_search = strdup( p_sys->psz_search_chain );
448                 }
449                 else if( p_sys->psz_old_search )
450                 {
451                     SearchPlaylist( p_intf, p_sys->psz_old_search );
452                 }
453                 p_sys->i_box_type = BOX_PLAYLIST;
454                 return 1;
455             case 0x1b:      /* Esc. */
456                 p_sys->i_box_plidx = p_sys->i_before_search;
457                 p_sys->i_box_type = BOX_PLAYLIST;
458                 return 1;
459             case KEY_BACKSPACE:
460                 if( i_chain_len > 0 )
461                 {
462                     p_sys->psz_search_chain[ i_chain_len - 1 ] = '\0';
463                 }
464                 break;
465             default:
466                 if( i_chain_len < SEARCH_CHAIN_SIZE )
467                 {
468                     p_sys->psz_search_chain[ i_chain_len++ ] = i_key;
469                     p_sys->psz_search_chain[ i_chain_len ] = 0;
470                 }
471                 break;
472         }
473         if( p_sys->psz_old_search )
474         {
475             free( p_sys->psz_old_search );
476             p_sys->psz_old_search = NULL;
477         }
478         SearchPlaylist( p_intf, p_sys->psz_search_chain );
479         return 1;
480     }
481     else if( p_sys->i_box_type == BOX_OPEN && p_sys->psz_open_chain )
482     {
483         int i_chain_len;
484         i_chain_len = strlen( p_sys->psz_open_chain );
485         playlist_t *p_playlist = p_sys->p_playlist;
486
487         switch( i_key )
488         {
489             case 0x0c:      /* ^l */
490                 clear();
491                 return 1;
492             case KEY_ENTER:
493             case 0x0d:
494                 if( p_playlist && i_chain_len > 0 )
495                 {
496                     playlist_Add( p_playlist, p_sys->psz_open_chain,
497                                   p_sys->psz_open_chain,
498                                   PLAYLIST_GO|PLAYLIST_APPEND, PLAYLIST_END );
499                     p_sys->b_box_plidx_follow = VLC_TRUE;
500                 }
501                 p_sys->i_box_type = BOX_PLAYLIST;
502                 return 1;
503             case 0x1b:      /* Esc. */
504                 p_sys->i_box_type = BOX_PLAYLIST;
505                 return 1;
506             case KEY_BACKSPACE:
507                 if( i_chain_len > 0 )
508                 {
509                     p_sys->psz_open_chain[ i_chain_len - 1 ] = '\0';
510                 }
511                 break;
512             default:
513                 if( i_chain_len < OPEN_CHAIN_SIZE )
514                 {
515                     p_sys->psz_open_chain[ i_chain_len++ ] = i_key;
516                     p_sys->psz_open_chain[ i_chain_len ] = 0;
517                 }
518                 break;
519         }
520         return 1;
521     }
522
523
524     /* Common keys */
525     switch( i_key )
526     {
527         case 'q':
528         case 'Q':
529         case 0x1b:  /* Esc */
530             p_intf->b_die = 1;
531             return 0;
532
533         /* Box switching */
534         case 'i':
535             if( p_sys->i_box_type == BOX_INFO )
536                 p_sys->i_box_type = BOX_NONE;
537             else
538                 p_sys->i_box_type = BOX_INFO;
539             p_sys->i_box_lines_total = 0;
540             return 1;
541         case 'l':
542             if( p_sys->i_box_type == BOX_LOG )
543                 p_sys->i_box_type = BOX_NONE;
544             else
545                 p_sys->i_box_type = BOX_LOG;
546             return 1;
547         case 'P':
548             if( p_sys->i_box_type == BOX_PLAYLIST )
549                 p_sys->i_box_type = BOX_NONE;
550             else
551                 p_sys->i_box_type = BOX_PLAYLIST;
552             return 1;
553         case 'h':
554         case 'H':
555             if( p_sys->i_box_type == BOX_HELP )
556                 p_sys->i_box_type = BOX_NONE;
557             else
558                 p_sys->i_box_type = BOX_HELP;
559             p_sys->i_box_lines_total = 0;
560             return 1;
561         case '/':
562             if( p_sys->i_box_type != BOX_SEARCH )
563             {
564                 if( p_sys->psz_search_chain == NULL )
565                 {
566                     return 1;
567                 }
568                 p_sys->psz_search_chain[0] = '\0';
569                 p_sys->b_box_plidx_follow = VLC_FALSE;
570                 p_sys->i_before_search = p_sys->i_box_plidx;
571                 p_sys->i_box_type = BOX_SEARCH;
572             }
573             return 1;
574         case 0x0f:      /* '^o': open */
575             if( p_sys->i_box_type != BOX_OPEN )
576             {
577                 if( p_sys->psz_open_chain == NULL )
578                 {
579                     return 1;
580                 }
581                 p_sys->psz_open_chain[0] = '\0';
582                 p_sys->i_box_type = BOX_OPEN;
583             }
584             return 1;
585
586         /* Navigation */
587         case KEY_RIGHT:
588             p_sys->f_slider += 1.0;
589             if( p_sys->f_slider > 99.9 ) p_sys->f_slider = 99.9;
590             ManageSlider ( p_intf );
591             return 1;
592
593         case KEY_LEFT:
594             p_sys->f_slider -= 1.0;
595             if( p_sys->f_slider < 0.0 ) p_sys->f_slider = 0.0;
596             ManageSlider ( p_intf );
597             return 1;
598
599         /* Common control */
600         case 'f':
601         {
602             vout_thread_t *p_vout;
603             if( p_intf->p_sys->p_input )
604             {
605                 p_vout = vlc_object_find( p_intf->p_sys->p_input,
606                                           VLC_OBJECT_VOUT, FIND_CHILD );
607                 if( p_vout )
608                 {
609                     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
610                     vlc_object_release( p_vout );
611                 }
612             }
613             return 0;
614         }
615
616         case ' ':
617             PlayPause( p_intf );
618             return 1;
619
620         case 's':
621             if( p_intf->p_sys->p_playlist )
622             {
623                 playlist_Stop( p_intf->p_sys->p_playlist );
624             }
625             return 1;
626
627         case 'e':
628             Eject( p_intf );
629             return 1;
630
631         case '[':
632             if( p_sys->p_input )
633             {
634                 val.b_bool = VLC_TRUE;
635                 var_Set( p_sys->p_input, "prev-title", val );
636             }
637             return 1;
638
639         case ']':
640             if( p_sys->p_input )
641             {
642                 val.b_bool = VLC_TRUE;
643                 var_Set( p_sys->p_input, "next-title", val );
644             }
645             return 1;
646
647         case '<':
648             if( p_sys->p_input )
649             {
650                 val.b_bool = VLC_TRUE;
651                 var_Set( p_sys->p_input, "prev-chapter", val );
652             }
653             return 1;
654
655         case '>':
656             if( p_sys->p_input )
657             {
658                 val.b_bool = VLC_TRUE;
659                 var_Set( p_sys->p_input, "next-chapter", val );
660             }
661             return 1;
662
663         case 'p':
664             if( p_intf->p_sys->p_playlist )
665             {
666                 playlist_Prev( p_intf->p_sys->p_playlist );
667             }
668             clear();
669             return 1;
670         case 'n':
671             if( p_intf->p_sys->p_playlist )
672             {
673                 playlist_Next( p_intf->p_sys->p_playlist );
674             }
675             clear();
676             return 1;
677
678         /*
679          * ^l should clear and redraw the screen
680          */
681         case 0x0c:
682             clear();
683             return 1;
684
685         default:
686             return 0;
687     }
688 }
689
690 static void ManageSlider ( intf_thread_t *p_intf )
691 {
692     intf_sys_t     *p_sys = p_intf->p_sys;
693     input_thread_t *p_input = p_sys->p_input;
694     vlc_value_t     val;
695
696     if( p_input == NULL )
697     {
698         return;
699     }
700     var_Get( p_input, "state", &val );
701     if( val.i_int != PLAYING_S )
702     {
703         return;
704     }
705
706     var_Get( p_input, "position", &val );
707     if( p_sys->f_slider == p_sys->f_slider_old )
708     {
709         p_sys->f_slider =
710         p_sys->f_slider_old = 100 * val.f_float;
711     }
712     else
713     {
714         p_sys->f_slider_old = p_sys->f_slider;
715
716         val.f_float = p_sys->f_slider / 100.0;
717         var_Set( p_input, "position", val );
718     }
719 }
720
721 static void SearchPlaylist( intf_thread_t *p_intf, char *psz_searchstring )
722 {
723     bool b_ok = false;
724     int i_current;
725     int i_first = 0 ;
726     int i_item = -1;
727     intf_sys_t *p_sys = p_intf->p_sys;
728     playlist_t *p_playlist = p_sys->p_playlist;
729
730     if( p_sys->i_before_search >= 0 )
731         i_first = p_sys->i_before_search;
732
733     if( ( ! psz_searchstring ) ||  strlen( psz_searchstring ) <= 0 )
734     {
735         p_sys->i_box_plidx = p_sys->i_before_search;
736         return;
737     }
738
739     for( i_current = i_first + 1; i_current < p_playlist->i_size;
740          i_current++ )
741     {
742         if( strcasestr( p_playlist->pp_items[i_current]->input.psz_name,
743                         psz_searchstring ) != NULL
744             || strcasestr( p_playlist->pp_items[i_current]->input.psz_uri,
745                            psz_searchstring ) != NULL )
746         {
747             i_item = i_current;
748             b_ok = true;
749             break;
750         }
751     }
752     if( !b_ok )
753     {
754         for( i_current = 0; i_current < i_first; i_current++ )
755         {
756             if( strcasestr( p_playlist->pp_items[i_current]->input.psz_name,
757                             psz_searchstring ) != NULL
758                 || strcasestr( p_playlist->pp_items[i_current]->input.psz_uri,
759                                psz_searchstring ) != NULL )
760             {
761                 i_item = i_current;
762                 b_ok = true;
763                 break;
764             }
765         }
766     }
767
768     if( i_item < 0 || i_item >= p_playlist->i_size ) return;
769
770     p_sys->i_box_plidx = i_item;
771 }
772
773
774 static void mvnprintw( int y, int x, int w, const char *p_fmt, ... )
775 {
776     va_list  vl_args;
777     char    *p_buf = NULL;
778     int      i_len;
779
780     va_start ( vl_args, p_fmt );
781     vasprintf ( &p_buf, p_fmt, vl_args );
782     va_end ( vl_args );
783
784     if ( p_buf == NULL )
785     {
786         return;
787     }
788     if(  w > 0 )
789     {
790         if( ( i_len = strlen( p_buf ) ) > w )
791         {
792             int i_cut = i_len - w;
793             int x1 = i_len/2 - i_cut/2;
794             int x2 = x1 + i_cut;
795
796             if( i_len > x2 )
797             {
798                 memmove( &p_buf[x1], &p_buf[x2], i_len - x2 );
799             }
800             p_buf[w] = '\0';
801             if( w > 7 )
802             {
803                 p_buf[w/2-1] = '.';
804                 p_buf[w/2  ] = '.';
805                 p_buf[w/2+1] = '.';
806             }
807             mvprintw( y, x, "%s", p_buf );
808         }
809         else
810         {
811             mvprintw( y, x, "%s", p_buf );
812             mvhline( y, x + i_len, ' ', w - i_len );
813         }
814     }
815 }
816 static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt, ... )
817 {
818     intf_sys_t     *p_sys = p_intf->p_sys;
819
820     va_list  vl_args;
821     char    *p_buf = NULL;
822
823     if( l < p_sys->i_box_start || l - p_sys->i_box_start >= p_sys->i_box_lines )
824     {
825         return;
826     }
827
828     va_start ( vl_args, p_fmt );
829     vasprintf ( &p_buf, p_fmt, vl_args );
830     va_end ( vl_args );
831
832     if( p_buf == NULL )
833     {
834         return;
835     }
836
837     mvnprintw( p_sys->i_box_y + l - p_sys->i_box_start, x, COLS - x - 1, "%s", p_buf );
838 }
839
840 static void Redraw ( intf_thread_t *p_intf, time_t *t_last_refresh )
841 {
842     intf_sys_t     *p_sys = p_intf->p_sys;
843     input_thread_t *p_input = p_sys->p_input;
844     int y = 0;
845     int h;
846     int y_end;
847
848     //clear();
849
850     /* Title */
851     attrset ( A_REVERSE );
852     mvnprintw( y, 0, COLS, VOUT_TITLE " (ncurses interface) [ h for help ]" );
853     attroff ( A_REVERSE );
854     y += 2;
855
856     /* Infos */
857     if( p_input && !p_input->b_dead )
858     {
859         char buf1[MSTRTIME_MAX_SIZE];
860         char buf2[MSTRTIME_MAX_SIZE];
861         vlc_value_t val;
862         vlc_value_t val_list;
863
864         /* Source */
865         mvnprintw( y++, 0, COLS, " Source   : %s", p_input->psz_source );
866
867         /* State */
868         var_Get( p_input, "state", &val );
869         if( val.i_int == PLAYING_S )
870         {
871             mvnprintw( y++, 0, COLS, " State    : Playing" );
872         }
873         else if( val.i_int == PAUSE_S )
874         {
875             mvnprintw( y++, 0, COLS, " State    : Paused" );
876         }
877         else
878         {
879             y++;
880         }
881         if( val.i_int != INIT_S && val.i_int != END_S )
882         {
883             /* Position */
884             var_Get( p_input, "time", &val );
885             msecstotimestr( buf1, val.i_time / 1000 );
886
887             var_Get( p_input, "length", &val );
888             msecstotimestr( buf2, val.i_time / 1000 );
889
890             mvnprintw( y++, 0, COLS, " Position : %s/%s (%.2f%%)", buf1, buf2, p_sys->f_slider );
891
892             /* Title */
893             if( !var_Get( p_input, "title", &val ) )
894             {
895                 var_Change( p_input, "title", VLC_VAR_GETCHOICES, &val_list, NULL );
896                 if( val_list.p_list->i_count > 0 )
897                 {
898                     mvnprintw( y++, 0, COLS, " Title    : %d/%d", val.i_int, val_list.p_list->i_count );
899                 }
900                 var_Change( p_input, "title", VLC_VAR_FREELIST, &val_list, NULL );
901             }
902
903             /* Chapter */
904             if( !var_Get( p_input, "chapter", &val ) )
905             {
906                 var_Change( p_input, "chapter", VLC_VAR_GETCHOICES, &val_list, NULL );
907                 if( val_list.p_list->i_count > 0 )
908                 {
909                     mvnprintw( y++, 0, COLS, " Chapter  : %d/%d", val.i_int, val_list.p_list->i_count );
910                 }
911                 var_Change( p_input, "chapter", VLC_VAR_FREELIST, &val_list, NULL );
912             }
913         }
914         else
915         {
916             y++;
917         }
918     }
919     else
920     {
921         mvnprintw( y++, 0, COLS, "Source: <no current item>" );
922         DrawEmptyLine( p_sys->w, y++, 0, COLS );
923         DrawEmptyLine( p_sys->w, y++, 0, COLS );
924     }
925
926     DrawBox( p_sys->w, y, 0, 3, COLS, "" );
927     DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
928     DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
929     y += 3;
930
931     p_sys->i_box_y = y + 1;
932     p_sys->i_box_lines = LINES - y - 2;
933
934     h = LINES - y;
935     y_end = y + h - 1;
936
937     if( p_sys->i_box_type == BOX_HELP )
938     {
939         /* Help box */
940         int l = 0;
941         DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
942
943         MainBoxWrite( p_intf, l++, 1, "[Display]" );
944         MainBoxWrite( p_intf, l++, 1, "     h,H         Show/Hide help box" );
945         MainBoxWrite( p_intf, l++, 1, "     i           Show/Hide informations box" );
946         MainBoxWrite( p_intf, l++, 1, "     l           Show/Hide logs box" );
947         MainBoxWrite( p_intf, l++, 1, "     P           Show/Hide playlist box" );
948         MainBoxWrite( p_intf, l++, 1, "" );
949
950         MainBoxWrite( p_intf, l++, 1, "[Global]" );
951         MainBoxWrite( p_intf, l++, 1, "     q, Q        Quit" );
952         MainBoxWrite( p_intf, l++, 1, "     s           Stop" );
953         MainBoxWrite( p_intf, l++, 1, "   <space>       Pause/Play" );
954         MainBoxWrite( p_intf, l++, 1, "     n, p        Next/Previous item" );
955         MainBoxWrite( p_intf, l++, 1, "     [, ]        Next/Previous title" );
956         MainBoxWrite( p_intf, l++, 1, "     <, >        Next/Previous title" );
957         MainBoxWrite( p_intf, l++, 1, "     <right>     Seek +1%%" );
958         MainBoxWrite( p_intf, l++, 1, "     <left>      Seek -1%%" );
959         MainBoxWrite( p_intf, l++, 1, "" );
960
961         MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
962         MainBoxWrite( p_intf, l++, 1, "     r           Randomize playlist" );
963         MainBoxWrite( p_intf, l++, 1, "     o           Order Playlist" );
964         MainBoxWrite( p_intf, l++, 1, "     O           Reverse order Playlist" );
965         MainBoxWrite( p_intf, l++, 1, "     /           Look for an item" );
966         MainBoxWrite( p_intf, l++, 1, "   Ctrl-o        Add an entry" );
967         MainBoxWrite( p_intf, l++, 1, "   <del>         Delete an entry" );
968         MainBoxWrite( p_intf, l++, 1, "  <backspace>    Delete an entry" );
969         MainBoxWrite( p_intf, l++, 1, "" );
970
971         MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
972         MainBoxWrite( p_intf, l++, 1, "  <up>,<down>    Navigate through the box line by line" );
973         MainBoxWrite( p_intf, l++, 1, " <pgup>,<pgdown> Navigate through the box page by page" );
974         MainBoxWrite( p_intf, l++, 1, "" );
975
976         MainBoxWrite( p_intf, l++, 1, "[Player]" );
977         MainBoxWrite( p_intf, l++, 1, "  <up>,<down>    Seek +/-5%%" );
978         MainBoxWrite( p_intf, l++, 1, "" );
979
980         MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
981         MainBoxWrite( p_intf, l++, 1, "   Ctrl-l        Refresh the screen" );
982
983         p_sys->i_box_lines_total = l;
984         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
985         {
986             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
987         }
988
989         if( l - p_sys->i_box_start < p_sys->i_box_lines )
990         {
991             y += l - p_sys->i_box_start;
992         }
993         else
994         {
995             y += p_sys->i_box_lines;
996         }
997     }
998     else if( p_sys->i_box_type == BOX_INFO )
999     {
1000         /* Info box */
1001         int l = 0;
1002         DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1003
1004         if( p_input )
1005         {
1006             int i,j;
1007
1008             vlc_mutex_lock( &p_input->stream.stream_lock );
1009             for ( i = 0; i < p_input->p_item->i_categories; i++ )
1010             {
1011                 info_category_t *p_category = p_input->p_item->pp_categories[i];
1012                 if( y >= y_end ) break;
1013                 MainBoxWrite( p_intf, l++, 1, "  [%s]", p_category->psz_name );
1014                 for ( j = 0; j < p_category->i_infos; j++ )
1015                 {
1016                     info_t *p_info = p_category->pp_infos[j];
1017                     if( y >= y_end ) break;
1018                     MainBoxWrite( p_intf, l++, 1, "      %s: %s", p_info->psz_name, p_info->psz_value );
1019                 }
1020             }
1021             vlc_mutex_unlock( &p_input->p_item->lock );
1022         }
1023         else
1024         {
1025             MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1026         }
1027         p_sys->i_box_lines_total = l;
1028         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1029         {
1030             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1031         }
1032
1033         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1034         {
1035             y += l - p_sys->i_box_start;
1036         }
1037         else
1038         {
1039             y += p_sys->i_box_lines;
1040         }
1041     }
1042     else if( p_sys->i_box_type == BOX_LOG )
1043     {
1044         int i_line = 0;
1045         int i_stop;
1046         int i_start;
1047
1048         DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1049
1050         i_start = p_intf->p_sys->p_sub->i_start;
1051
1052         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1053         i_stop = *p_intf->p_sys->p_sub->pi_stop;
1054         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1055
1056         for( ;; )
1057         {
1058             static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1059             if( i_line >= h - 2 )
1060             {
1061                 break;
1062             }
1063             i_stop--;
1064             i_line++;
1065             if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1066             if( i_stop == i_start )
1067             {
1068                 break;
1069             }
1070             mvnprintw( y + h-2-i_line, 1, COLS - 2, "   [%s] %s",
1071                       ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1072                       p_sys->p_sub->p_msg[i_stop].psz_msg );
1073         }
1074
1075         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1076         p_intf->p_sys->p_sub->i_start = i_stop;
1077         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1078         y = y_end;
1079     }
1080     else if( ( p_sys->i_box_type == BOX_PLAYLIST ||
1081                p_sys->i_box_type == BOX_SEARCH ||
1082                p_sys->i_box_type == BOX_OPEN  ) && p_sys->p_playlist )
1083     {
1084         /* Playlist box */
1085         playlist_t *p_playlist = p_sys->p_playlist;
1086         int        i_start, i_stop;
1087         int        i_item;
1088         DrawBox( p_sys->w, y++, 0, h, COLS, " Playlist " );
1089
1090         if( p_sys->i_box_plidx >= p_playlist->i_size ) p_sys->i_box_plidx = p_playlist->i_size - 1;
1091         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1092
1093         if( p_sys->i_box_plidx < (h - 2)/2 )
1094         {
1095             i_start = 0;
1096             i_stop = h - 2;
1097         }
1098         else if( p_playlist->i_size - p_sys->i_box_plidx > (h - 2)/2 )
1099         {
1100             i_start = p_sys->i_box_plidx - (h - 2)/2;
1101             i_stop = i_start + h - 2;
1102         }
1103         else
1104         {
1105             i_stop = p_playlist->i_size;
1106             i_start = p_playlist->i_size - (h - 2);
1107         }
1108         if( i_start < 0 )
1109         {
1110             i_start = 0;
1111         }
1112         if( i_stop > p_playlist->i_size )
1113         {
1114             i_stop = p_playlist->i_size;
1115         }
1116
1117         for( i_item = i_start; i_item < i_stop; i_item++ )
1118         {
1119             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1120             int c = p_playlist->i_index == i_item ? '>' : ' ';
1121
1122             if( y >= y_end ) break;
1123             if( b_selected )
1124             {
1125                 attrset( A_REVERSE );
1126             }
1127             if( !strcmp( p_playlist->pp_items[i_item]->input.psz_name,
1128                          p_playlist->pp_items[i_item]->input.psz_uri ) )
1129             {
1130                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s'",
1131                            c,
1132                            i_item,
1133                            p_playlist->pp_items[i_item]->input.psz_uri );
1134             }
1135             else
1136             {
1137                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s' (%s)",
1138                           c,
1139                           i_item,
1140                           p_playlist->pp_items[i_item]->input.psz_uri,
1141                           p_playlist->pp_items[i_item]->input.psz_name );
1142             }
1143             if( b_selected )
1144             {
1145                 attroff ( A_REVERSE );
1146             }
1147         }
1148     }
1149     else
1150     {
1151         y++;
1152     }
1153     if( p_sys->i_box_type == BOX_SEARCH )
1154     {
1155         DrawEmptyLine( p_sys->w, 6, 1, COLS-2 );
1156         if( p_sys->psz_search_chain )
1157         {
1158             if( strlen( p_sys->psz_search_chain ) == 0 &&
1159                 p_sys->psz_old_search != NULL )
1160             {
1161                 /* Searching next entry */
1162                 mvnprintw( 6, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1163             }
1164             else
1165             {
1166                 mvnprintw( 6, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1167             }
1168         }
1169     }
1170     if( p_sys->i_box_type == BOX_OPEN )
1171     {
1172         DrawEmptyLine( p_sys->w, 6, 1, COLS-2 );
1173         if( p_sys->psz_open_chain )
1174         {
1175             mvnprintw( 6, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1176         }
1177     }
1178
1179     while( y < y_end )
1180     {
1181         DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1182     }
1183
1184     refresh();
1185
1186     *t_last_refresh = time( 0 );
1187 }
1188
1189 static void Eject ( intf_thread_t *p_intf )
1190 {
1191     char *psz_device = NULL, *psz_parser, *psz_name;
1192
1193     /*
1194      * Get the active input
1195      * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
1196      * If it's neither of these, then return
1197      */
1198
1199     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1200                                                        FIND_ANYWHERE );
1201
1202     if( p_playlist == NULL )
1203     {
1204         return;
1205     }
1206
1207     vlc_mutex_lock( &p_playlist->object_lock );
1208
1209     if( p_playlist->i_index < 0 )
1210     {
1211         vlc_mutex_unlock( &p_playlist->object_lock );
1212         vlc_object_release( p_playlist );
1213         return;
1214     }
1215
1216     psz_name = p_playlist->pp_items[ p_playlist->i_index ]->input.psz_name;
1217
1218     if( psz_name )
1219     {
1220         if( !strncmp(psz_name, "dvd://", 4) )
1221         {
1222             switch( psz_name[strlen("dvd://")] )
1223             {
1224             case '\0':
1225             case '@':
1226                 psz_device = config_GetPsz( p_intf, "dvd" );
1227                 break;
1228             default:
1229                 /* Omit the first MRL-selector characters */
1230                 psz_device = strdup( psz_name + strlen("dvd://" ) );
1231                 break;
1232             }
1233         }
1234         else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
1235         {
1236             switch( psz_name[strlen(VCD_MRL)] )
1237             {
1238             case '\0':
1239             case '@':
1240                 psz_device = config_GetPsz( p_intf, VCD_MRL );
1241                 break;
1242             default:
1243                 /* Omit the beginning MRL-selector characters */
1244                 psz_device = strdup( psz_name + strlen(VCD_MRL) );
1245                 break;
1246             }
1247         }
1248         else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
1249         {
1250             switch( psz_name[strlen(CDDA_MRL)] )
1251             {
1252             case '\0':
1253             case '@':
1254                 psz_device = config_GetPsz( p_intf, "cd-audio" );
1255                 break;
1256             default:
1257                 /* Omit the beginning MRL-selector characters */
1258                 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
1259                 break;
1260             }
1261         }
1262         else
1263         {
1264             psz_device = strdup( psz_name );
1265         }
1266     }
1267
1268     vlc_mutex_unlock( &p_playlist->object_lock );
1269     vlc_object_release( p_playlist );
1270
1271     if( psz_device == NULL )
1272     {
1273         return;
1274     }
1275
1276     /* Remove what we have after @ */
1277     psz_parser = psz_device;
1278     for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
1279     {
1280         if( *psz_parser == '@' )
1281         {
1282             *psz_parser = '\0';
1283             break;
1284         }
1285     }
1286
1287     /* If there's a stream playing, we aren't allowed to eject ! */
1288     if( p_intf->p_sys->p_input == NULL )
1289     {
1290         msg_Dbg( p_intf, "ejecting %s", psz_device );
1291
1292         intf_Eject( p_intf, psz_device );
1293     }
1294
1295     free(psz_device);
1296     return;
1297 }
1298
1299 static void PlayPause ( intf_thread_t *p_intf )
1300 {
1301     input_thread_t *p_input = p_intf->p_sys->p_input;
1302     vlc_value_t val;
1303
1304     if( p_input )
1305     {
1306         var_Get( p_input, "state", &val );
1307         if( val.i_int != PAUSE_S )
1308         {
1309             val.i_int = PAUSE_S;
1310         }
1311         else
1312         {
1313             val.i_int = PLAYING_S;
1314         }
1315         var_Set( p_input, "state", val );
1316     }
1317     else if( p_intf->p_sys->p_playlist )
1318     {
1319         playlist_Play( p_intf->p_sys->p_playlist );
1320     }
1321 }
1322
1323 /****************************************************************************
1324  *
1325  ****************************************************************************/
1326 static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title )
1327 {
1328     int i;
1329     int i_len;
1330
1331     if(  w > 3 && h > 2 )
1332     {
1333         if( title == NULL ) title = "";
1334         i_len = strlen( title );
1335
1336         if( i_len > w - 2 ) i_len = w - 2;
1337
1338         mvwaddch( win, y, x,    ACS_ULCORNER );
1339         mvwhline( win, y, x+1,  ACS_HLINE, ( w-i_len-2)/2 );
1340         mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
1341         mvwhline( win, y, x+(w-i_len)/2+i_len,  ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
1342         mvwaddch( win, y, x+w-1,ACS_URCORNER );
1343
1344         for( i = 0; i < h-2; i++ )
1345         {
1346             mvwaddch( win, y+i+1, x,     ACS_VLINE );
1347             mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
1348         }
1349
1350         mvwaddch( win, y+h-1, x,     ACS_LLCORNER );
1351         mvwhline( win, y+h-1, x+1,   ACS_HLINE, w - 2 );
1352         mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
1353     }
1354 }
1355
1356 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
1357 {
1358     if( w > 0 )
1359     {
1360         mvhline( y, x, ' ', w );
1361     }
1362 }
1363
1364 static void DrawLine( WINDOW *win, int y, int x, int w )
1365 {
1366     if( w > 0 )
1367     {
1368         attrset( A_REVERSE );
1369         mvhline( y, x, ' ', w );
1370         attroff ( A_REVERSE );
1371     }
1372 }