]> git.sesse.net Git - vlc/blob - modules/gui/ncurses.c
* ncurses intf
[vlc] / modules / gui / ncurses.c
1 /*****************************************************************************
2  * ncurses.c : NCurses plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Yoann Peronneau <yoann@videolan.org>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                      /* malloc(), free() */
31 #include <string.h>
32 #include <errno.h>                                                 /* ENOMEM */
33 #include <stdio.h>
34 #include <time.h>
35
36 #include <curses.h>
37
38 #include <vlc/vlc.h>
39 #include <vlc/intf.h>
40 #include <vlc/vout.h>
41 #include <vlc/aout.h>
42
43 #ifdef HAVE_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     }
961
962     DrawBox( p_sys->w, y, 0, 3, COLS, "" );
963     DrawEmptyLine( p_sys->w, y+1, 1, COLS-2);
964     DrawLine( p_sys->w, y+1, 1, (int)(p_intf->p_sys->f_slider/100.0 * (COLS -2)) );
965     y += 3;
966
967     p_sys->i_box_y = y + 1;
968     p_sys->i_box_lines = LINES - y - 2;
969
970     h = LINES - y;
971     y_end = y + h - 1;
972
973     if( p_sys->i_box_type == BOX_HELP )
974     {
975         /* Help box */
976         int l = 0;
977         DrawBox( p_sys->w, y++, 0, h, COLS, " Help " );
978
979         MainBoxWrite( p_intf, l++, 1, "[Display]" );
980         MainBoxWrite( p_intf, l++, 1, "     h,H         Show/Hide help box" );
981         MainBoxWrite( p_intf, l++, 1, "     i           Show/Hide info box" );
982         MainBoxWrite( p_intf, l++, 1, "     l           Show/Hide messages box" );
983         MainBoxWrite( p_intf, l++, 1, "     P           Show/Hide playlist box" );
984         MainBoxWrite( p_intf, l++, 1, "" );
985
986         MainBoxWrite( p_intf, l++, 1, "[Global]" );
987         MainBoxWrite( p_intf, l++, 1, "     q, Q        Quit" );
988         MainBoxWrite( p_intf, l++, 1, "     s           Stop" );
989         MainBoxWrite( p_intf, l++, 1, "     <space>     Pause/Play" );
990         MainBoxWrite( p_intf, l++, 1, "     f           Toggle Fullscreen" );
991         MainBoxWrite( p_intf, l++, 1, "     n, p        Next/Previous playlist item" );
992         MainBoxWrite( p_intf, l++, 1, "     [, ]        Next/Previous title" );
993         MainBoxWrite( p_intf, l++, 1, "     <, >        Next/Previous chapter" );
994         MainBoxWrite( p_intf, l++, 1, "     <right>     Seek +1%%" );
995         MainBoxWrite( p_intf, l++, 1, "     <left>      Seek -1%%" );
996         MainBoxWrite( p_intf, l++, 1, "     a           Volume Up" );
997         MainBoxWrite( p_intf, l++, 1, "     z           Volume Down" );
998         MainBoxWrite( p_intf, l++, 1, "" );
999
1000         MainBoxWrite( p_intf, l++, 1, "[Playlist]" );
1001         MainBoxWrite( p_intf, l++, 1, "     r           Random" );
1002         MainBoxWrite( p_intf, l++, 1, "     l           Loop Playlist" );
1003         MainBoxWrite( p_intf, l++, 1, "     R           Repeat item" );
1004         MainBoxWrite( p_intf, l++, 1, "     o           Order Playlist by title" );
1005         MainBoxWrite( p_intf, l++, 1, "     O           Reverse order Playlist by title" );
1006         MainBoxWrite( p_intf, l++, 1, "     /           Look for an item" );
1007         MainBoxWrite( p_intf, l++, 1, "     A           Add an entry" );
1008         MainBoxWrite( p_intf, l++, 1, "     D, <del>    Delete an entry" );
1009         MainBoxWrite( p_intf, l++, 1, "     <backspace> Delete an entry" );
1010         MainBoxWrite( p_intf, l++, 1, "" );
1011
1012         MainBoxWrite( p_intf, l++, 1, "[Boxes]" );
1013         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Navigate through the box line by line" );
1014         MainBoxWrite( p_intf, l++, 1, "     <pgup>,<pgdown> Navigate through the box page by page" );
1015         MainBoxWrite( p_intf, l++, 1, "" );
1016
1017         MainBoxWrite( p_intf, l++, 1, "[Player]" );
1018         MainBoxWrite( p_intf, l++, 1, "     <up>,<down>     Seek +/-5%%" );
1019         MainBoxWrite( p_intf, l++, 1, "" );
1020
1021         MainBoxWrite( p_intf, l++, 1, "[Miscellaneous]" );
1022         MainBoxWrite( p_intf, l++, 1, "     Ctrl-l          Refresh the screen" );
1023
1024         p_sys->i_box_lines_total = l;
1025         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1026         {
1027             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1028         }
1029
1030         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1031         {
1032             y += l - p_sys->i_box_start;
1033         }
1034         else
1035         {
1036             y += p_sys->i_box_lines;
1037         }
1038     }
1039     else if( p_sys->i_box_type == BOX_INFO )
1040     {
1041         /* Info box */
1042         int l = 0;
1043         DrawBox( p_sys->w, y++, 0, h, COLS, " Information " );
1044
1045         if( p_input )
1046         {
1047             int i,j;
1048             vlc_mutex_lock( &p_input->p_item->lock );
1049             for ( i = 0; i < p_input->p_item->i_categories; i++ )
1050             {
1051                 info_category_t *p_category = p_input->p_item->pp_categories[i];
1052                 if( y >= y_end ) break;
1053                 MainBoxWrite( p_intf, l++, 1, "  [%s]", p_category->psz_name );
1054                 for ( j = 0; j < p_category->i_infos; j++ )
1055                 {
1056                     info_t *p_info = p_category->pp_infos[j];
1057                     if( y >= y_end ) break;
1058                     MainBoxWrite( p_intf, l++, 1, "      %s: %s", p_info->psz_name, p_info->psz_value );
1059                 }
1060             }
1061             vlc_mutex_unlock( &p_input->p_item->lock );
1062         }
1063         else
1064         {
1065             MainBoxWrite( p_intf, l++, 1, "No item currently playing" );
1066         }
1067         p_sys->i_box_lines_total = l;
1068         if( p_sys->i_box_start >= p_sys->i_box_lines_total )
1069         {
1070             p_sys->i_box_start = p_sys->i_box_lines_total - 1;
1071         }
1072
1073         if( l - p_sys->i_box_start < p_sys->i_box_lines )
1074         {
1075             y += l - p_sys->i_box_start;
1076         }
1077         else
1078         {
1079             y += p_sys->i_box_lines;
1080         }
1081     }
1082     else if( p_sys->i_box_type == BOX_LOG )
1083     {
1084         int i_line = 0;
1085         int i_stop;
1086         int i_start;
1087
1088         DrawBox( p_sys->w, y++, 0, h, COLS, " Logs " );
1089
1090         i_start = p_intf->p_sys->p_sub->i_start;
1091
1092         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1093         i_stop = *p_intf->p_sys->p_sub->pi_stop;
1094         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1095
1096         for( ;; )
1097         {
1098             static const char *ppsz_type[4] = { "", "error", "warning", "debug" };
1099             if( i_line >= h - 2 )
1100             {
1101                 break;
1102             }
1103             i_stop--;
1104             i_line++;
1105             if( i_stop < 0 ) i_stop += VLC_MSG_QSIZE;
1106             if( i_stop == i_start )
1107             {
1108                 break;
1109             }
1110             mvnprintw( y + h-2-i_line, 1, COLS - 2, "   [%s] %s",
1111                       ppsz_type[p_sys->p_sub->p_msg[i_stop].i_type],
1112                       p_sys->p_sub->p_msg[i_stop].psz_msg );
1113         }
1114
1115         vlc_mutex_lock( p_intf->p_sys->p_sub->p_lock );
1116         p_intf->p_sys->p_sub->i_start = i_stop;
1117         vlc_mutex_unlock( p_intf->p_sys->p_sub->p_lock );
1118         y = y_end;
1119     }
1120     else if( ( p_sys->i_box_type == BOX_PLAYLIST ||
1121                p_sys->i_box_type == BOX_SEARCH ||
1122                p_sys->i_box_type == BOX_OPEN  ) && p_sys->p_playlist )
1123     {
1124         /* Playlist box */
1125         playlist_t *p_playlist = p_sys->p_playlist;
1126         int        i_start, i_stop;
1127         int        i_item;
1128         DrawBox( p_sys->w, y++, 0, h, COLS, " Playlist " );
1129
1130         if( p_sys->i_box_plidx >= p_playlist->i_size ) p_sys->i_box_plidx = p_playlist->i_size - 1;
1131         if( p_sys->i_box_plidx < 0 ) p_sys->i_box_plidx = 0;
1132
1133         if( p_sys->i_box_plidx < (h - 2)/2 )
1134         {
1135             i_start = 0;
1136             i_stop = h - 2;
1137         }
1138         else if( p_playlist->i_size - p_sys->i_box_plidx > (h - 2)/2 )
1139         {
1140             i_start = p_sys->i_box_plidx - (h - 2)/2;
1141             i_stop = i_start + h - 2;
1142         }
1143         else
1144         {
1145             i_stop = p_playlist->i_size;
1146             i_start = p_playlist->i_size - (h - 2);
1147         }
1148         if( i_start < 0 )
1149         {
1150             i_start = 0;
1151         }
1152         if( i_stop > p_playlist->i_size )
1153         {
1154             i_stop = p_playlist->i_size;
1155         }
1156
1157         for( i_item = i_start; i_item < i_stop; i_item++ )
1158         {
1159             vlc_bool_t b_selected = ( p_sys->i_box_plidx == i_item );
1160             int c = p_playlist->i_index == i_item ? '>' : ' ';
1161
1162             if( y >= y_end ) break;
1163             if( b_selected )
1164             {
1165                 attrset( A_REVERSE );
1166             }
1167             if( !strcmp( p_playlist->pp_items[i_item]->input.psz_name,
1168                          p_playlist->pp_items[i_item]->input.psz_uri ) )
1169             {
1170                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s'",
1171                            c,
1172                            i_item,
1173                            p_playlist->pp_items[i_item]->input.psz_uri );
1174             }
1175             else
1176             {
1177                 mvnprintw( y++, 1, COLS - 2, "%c %d - '%s' (%s)",
1178                           c,
1179                           i_item,
1180                           p_playlist->pp_items[i_item]->input.psz_uri,
1181                           p_playlist->pp_items[i_item]->input.psz_name );
1182             }
1183             if( b_selected )
1184             {
1185                 attroff ( A_REVERSE );
1186             }
1187         }
1188     }
1189     else
1190     {
1191         y++;
1192     }
1193     if( p_sys->i_box_type == BOX_SEARCH )
1194     {
1195         DrawEmptyLine( p_sys->w, 6, 1, COLS-2 );
1196         if( p_sys->psz_search_chain )
1197         {
1198             if( strlen( p_sys->psz_search_chain ) == 0 &&
1199                 p_sys->psz_old_search != NULL )
1200             {
1201                 /* Searching next entry */
1202                 mvnprintw( 6, 1, COLS-2, "Find: %s", p_sys->psz_old_search );
1203             }
1204             else
1205             {
1206                 mvnprintw( 6, 1, COLS-2, "Find: %s", p_sys->psz_search_chain );
1207             }
1208         }
1209     }
1210     if( p_sys->i_box_type == BOX_OPEN )
1211     {
1212         DrawEmptyLine( p_sys->w, 6, 1, COLS-2 );
1213         if( p_sys->psz_open_chain )
1214         {
1215             mvnprintw( 6, 1, COLS-2, "Open: %s", p_sys->psz_open_chain );
1216         }
1217     }
1218
1219     while( y < y_end )
1220     {
1221         DrawEmptyLine( p_sys->w, y++, 1, COLS - 2 );
1222     }
1223
1224     refresh();
1225
1226     *t_last_refresh = time( 0 );
1227 }
1228
1229 static void Eject ( intf_thread_t *p_intf )
1230 {
1231     char *psz_device = NULL, *psz_parser, *psz_name;
1232
1233     /*
1234      * Get the active input
1235      * Determine whether we can eject a media, ie it's a DVD, VCD or CD-DA
1236      * If it's neither of these, then return
1237      */
1238
1239     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
1240                                                        FIND_ANYWHERE );
1241
1242     if( p_playlist == NULL )
1243     {
1244         return;
1245     }
1246
1247     vlc_mutex_lock( &p_playlist->object_lock );
1248
1249     if( p_playlist->i_index < 0 )
1250     {
1251         vlc_mutex_unlock( &p_playlist->object_lock );
1252         vlc_object_release( p_playlist );
1253         return;
1254     }
1255
1256     psz_name = p_playlist->pp_items[ p_playlist->i_index ]->input.psz_name;
1257
1258     if( psz_name )
1259     {
1260         if( !strncmp(psz_name, "dvd://", 4) )
1261         {
1262             switch( psz_name[strlen("dvd://")] )
1263             {
1264             case '\0':
1265             case '@':
1266                 psz_device = config_GetPsz( p_intf, "dvd" );
1267                 break;
1268             default:
1269                 /* Omit the first MRL-selector characters */
1270                 psz_device = strdup( psz_name + strlen("dvd://" ) );
1271                 break;
1272             }
1273         }
1274         else if( !strncmp(psz_name, VCD_MRL, strlen(VCD_MRL)) )
1275         {
1276             switch( psz_name[strlen(VCD_MRL)] )
1277             {
1278             case '\0':
1279             case '@':
1280                 psz_device = config_GetPsz( p_intf, VCD_MRL );
1281                 break;
1282             default:
1283                 /* Omit the beginning MRL-selector characters */
1284                 psz_device = strdup( psz_name + strlen(VCD_MRL) );
1285                 break;
1286             }
1287         }
1288         else if( !strncmp(psz_name, CDDA_MRL, strlen(CDDA_MRL) ) )
1289         {
1290             switch( psz_name[strlen(CDDA_MRL)] )
1291             {
1292             case '\0':
1293             case '@':
1294                 psz_device = config_GetPsz( p_intf, "cd-audio" );
1295                 break;
1296             default:
1297                 /* Omit the beginning MRL-selector characters */
1298                 psz_device = strdup( psz_name + strlen(CDDA_MRL) );
1299                 break;
1300             }
1301         }
1302         else
1303         {
1304             psz_device = strdup( psz_name );
1305         }
1306     }
1307
1308     vlc_mutex_unlock( &p_playlist->object_lock );
1309     vlc_object_release( p_playlist );
1310
1311     if( psz_device == NULL )
1312     {
1313         return;
1314     }
1315
1316     /* Remove what we have after @ */
1317     psz_parser = psz_device;
1318     for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
1319     {
1320         if( *psz_parser == '@' )
1321         {
1322             *psz_parser = '\0';
1323             break;
1324         }
1325     }
1326
1327     /* If there's a stream playing, we aren't allowed to eject ! */
1328     if( p_intf->p_sys->p_input == NULL )
1329     {
1330         msg_Dbg( p_intf, "ejecting %s", psz_device );
1331
1332         intf_Eject( p_intf, psz_device );
1333     }
1334
1335     free(psz_device);
1336     return;
1337 }
1338
1339 static void PlayPause ( intf_thread_t *p_intf )
1340 {
1341     input_thread_t *p_input = p_intf->p_sys->p_input;
1342     vlc_value_t val;
1343
1344     if( p_input )
1345     {
1346         var_Get( p_input, "state", &val );
1347         if( val.i_int != PAUSE_S )
1348         {
1349             val.i_int = PAUSE_S;
1350         }
1351         else
1352         {
1353             val.i_int = PLAYING_S;
1354         }
1355         var_Set( p_input, "state", val );
1356     }
1357     else if( p_intf->p_sys->p_playlist )
1358     {
1359         playlist_Play( p_intf->p_sys->p_playlist );
1360     }
1361 }
1362
1363 /****************************************************************************
1364  *
1365  ****************************************************************************/
1366 static void DrawBox( WINDOW *win, int y, int x, int h, int w, char *title )
1367 {
1368     int i;
1369     int i_len;
1370
1371     if(  w > 3 && h > 2 )
1372     {
1373         if( title == NULL ) title = "";
1374         i_len = strlen( title );
1375
1376         if( i_len > w - 2 ) i_len = w - 2;
1377
1378         mvwaddch( win, y, x,    ACS_ULCORNER );
1379         mvwhline( win, y, x+1,  ACS_HLINE, ( w-i_len-2)/2 );
1380         mvwprintw( win,y, x+1+(w-i_len-2)/2, "%s", title );
1381         mvwhline( win, y, x+(w-i_len)/2+i_len,  ACS_HLINE, w - 1 - ((w-i_len)/2+i_len) );
1382         mvwaddch( win, y, x+w-1,ACS_URCORNER );
1383
1384         for( i = 0; i < h-2; i++ )
1385         {
1386             mvwaddch( win, y+i+1, x,     ACS_VLINE );
1387             mvwaddch( win, y+i+1, x+w-1, ACS_VLINE );
1388         }
1389
1390         mvwaddch( win, y+h-1, x,     ACS_LLCORNER );
1391         mvwhline( win, y+h-1, x+1,   ACS_HLINE, w - 2 );
1392         mvwaddch( win, y+h-1, x+w-1, ACS_LRCORNER );
1393     }
1394 }
1395
1396 static void DrawEmptyLine( WINDOW *win, int y, int x, int w )
1397 {
1398     if( w > 0 )
1399     {
1400         mvhline( y, x, ' ', w );
1401     }
1402 }
1403
1404 static void DrawLine( WINDOW *win, int y, int x, int w )
1405 {
1406     if( w > 0 )
1407     {
1408         attrset( A_REVERSE );
1409         mvhline( y, x, ' ', w );
1410         attroff ( A_REVERSE );
1411     }
1412 }