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