]> git.sesse.net Git - vlc/blob - modules/gui/ncurses/ncurses.c
* InterfaceWindow.cpp: removed warnigs
[vlc] / modules / gui / ncurses / ncurses.c
1 /*****************************************************************************
2  * ncurses.c : NCurses plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: ncurses.c,v 1.2 2002/08/08 22:28:22 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *      
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdio.h>
31 #include <time.h>
32
33 #include <curses.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/intf.h>
37 #include <vlc/vout.h>
38
39 /*****************************************************************************
40  * Local prototypes.
41  *****************************************************************************/
42 static int  Open           ( vlc_object_t * );
43 static void Close          ( vlc_object_t * );             
44
45 static void Run            ( intf_thread_t * );                  
46 static void FullScreen     ( intf_thread_t * );
47 static void Play           ( intf_thread_t * );
48 static void Stop           ( intf_thread_t * );
49 static void Next           ( intf_thread_t * );
50 static void Eject          ( intf_thread_t * );
51 static void Pause          ( intf_thread_t * );
52 static void PrevTitle      ( intf_thread_t * );
53 static void NextTitle      ( intf_thread_t * );
54 static void PrevChapter    ( intf_thread_t * );
55 static void NextChapter    ( intf_thread_t * );
56
57 static int  HandleKey      ( intf_thread_t *, int );
58 static void Redraw           ( intf_thread_t *, time_t * );
59 static int  PrintFullLine  ( const char *p_fmt, ... );
60 static void ManageSlider   ( intf_thread_t * );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
66     set_description( _("ncurses interface module") );
67     set_capability( "interface", 10 );
68     set_callbacks( Open, Close );
69     add_shortcut( "curses" );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * intf_sys_t: description and status of ncurses interface
74  *****************************************************************************/
75 struct intf_sys_t
76 {
77     input_thread_t *    p_input;
78
79     float               f_slider_state;
80     float               f_slider_state_old;
81 };
82
83 /*****************************************************************************
84  * Open: initialize and create window
85  *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
87 {   
88     intf_thread_t *p_intf = (intf_thread_t *)p_this;
89
90     /* Allocate instance and initialize some members */
91     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
92     if( p_intf->p_sys == NULL )
93     {
94         msg_Err( p_intf, "out of memory" );
95         return( 1 );
96     }
97
98     p_intf->p_sys->p_input = NULL;
99
100     p_intf->pf_run = Run;
101
102     /* Initialize the curses library */
103     initscr();
104     /* Don't do NL -> CR/NL */
105     nonl();
106     /* Take input chars one at a time */
107     cbreak();
108     /* Don't echo */
109     noecho();
110
111     curs_set(0);
112     timeout(0);
113
114     clear();
115
116     return( 0 );
117 }
118
119 /*****************************************************************************
120  * Close: destroy interface window
121  *****************************************************************************/
122 static void Close( vlc_object_t *p_this )
123 {   
124     intf_thread_t *p_intf = (intf_thread_t *)p_this;
125
126     if( p_intf->p_sys->p_input )
127     {
128         vlc_object_release( p_intf->p_sys->p_input );
129     }
130
131     /* Close the ncurses interface */
132     endwin();
133
134     /* Destroy structure */
135     free( p_intf->p_sys );
136 }
137
138 /*****************************************************************************
139  * Run: ncurses thread
140  *****************************************************************************/
141 static void Run( intf_thread_t *p_intf )
142 {
143     signed char i_key;
144     time_t t_last_refresh;
145
146     /*
147      * force drawing the interface for the first time
148      */
149     t_last_refresh = ( time( 0 ) - 1);
150
151     while( !p_intf->b_die )
152     {
153         msleep( INTF_IDLE_SLEEP );
154
155         /* Update the input */ 
156         if( p_intf->p_sys->p_input == NULL )
157         {
158             p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
159                                                               FIND_ANYWHERE );
160         }
161         else if( p_intf->p_sys->p_input->b_dead )
162         {
163             vlc_object_release( p_intf->p_sys->p_input );
164             p_intf->p_sys->p_input = NULL;
165         }
166     
167         while( (i_key = getch()) != -1 )
168         {
169             /*
170              * HandleKey returns 1 if the screen needs to be redrawn
171              */
172             if ( HandleKey( p_intf, i_key ) )
173             {
174                 Redraw( p_intf, &t_last_refresh );
175             }
176         }
177
178         /*
179          * redraw the screen every second
180          */
181         if ( (time(0) - t_last_refresh) >= 1 )
182         {
183             ManageSlider ( p_intf );
184             Redraw( p_intf, &t_last_refresh );
185         }
186     }
187 }
188
189 /* following functions are local */
190
191 static int HandleKey( intf_thread_t *p_intf, int i_key )
192 {
193     switch( i_key )
194     {
195         case 'q':
196         case 'Q':
197             p_intf->b_die = 1;
198             return 0;
199
200         case 'f':
201             FullScreen( p_intf );
202             return 1;
203
204         case 'p':
205             Play( p_intf );
206             return 1;
207
208         case ' ':
209             Pause( p_intf );
210             return 1;
211
212         case 's':
213             Stop( p_intf );
214             return 1;
215
216         case 'n':
217             Next( p_intf );
218             return 1;
219
220         case 'e':
221             Eject( p_intf );
222             return 1;
223
224         case '[':
225             PrevTitle( p_intf );
226             break;
227
228         case ']':
229             NextTitle( p_intf );
230             break;
231
232         case '<':
233             PrevChapter( p_intf );
234             break;
235
236         case '>':
237             NextChapter( p_intf );
238             break;
239
240         case KEY_RIGHT:
241             p_intf->p_sys->f_slider_state += 100;
242             ManageSlider ( p_intf );
243             break;
244
245         case KEY_LEFT:
246             p_intf->p_sys->f_slider_state--;
247             ManageSlider ( p_intf );
248             break;
249
250         /*
251          * ^l should clear and redraw the screen
252          */
253         case 0x0c:
254             clear();
255             return 1;
256
257         default:
258             break;
259     }
260
261     return 0;
262 }
263
264 static int PrintFullLine ( const char *p_fmt, ... )
265 {
266     va_list  vl_args;
267     char *    p_buf        = NULL;
268     int       i_len;
269
270     va_start ( vl_args, p_fmt );
271     vasprintf ( &p_buf, p_fmt, vl_args );
272     va_end ( vl_args );
273
274     if ( p_buf == NULL )
275     {
276         return ( -1 );
277     }
278
279     i_len = strlen( p_buf );
280
281     /*
282      * make sure we don't exceed the border on the right side
283      */
284     if ( i_len > COLS )
285     {
286         p_buf[COLS] = '\0';
287         i_len = COLS;
288         printw( "%s", p_buf );
289     }
290     else
291     {
292         printw( "%s", p_buf );
293         hline( ' ', COLS - i_len );
294     }
295
296     free ( p_buf );
297
298     return i_len;
299 }
300
301 static void
302 Redraw ( intf_thread_t *p_intf, time_t *t_last_refresh )
303 {
304     int row = 0;
305
306     move ( row, 0 );
307
308     attrset ( A_REVERSE );
309     PrintFullLine( VOUT_TITLE " (ncurses interface)" );
310     attroff ( A_REVERSE );
311
312     row++;
313
314     row++;
315     move ( row, 0 );
316
317     if ( p_intf->p_sys->p_input != NULL )
318     {
319         PrintFullLine ( " DVD Chapter:%3d     DVD Title:%3d",
320             p_intf->p_sys->p_input->stream.p_selected_area->i_part,
321             p_intf->p_sys->p_input->stream.p_selected_area->i_id );
322     }
323
324     row++;
325     mvaddch ( row, 0, ACS_ULCORNER );
326     mvhline ( row, 1, ACS_HLINE, COLS-2 );
327     mvaddch ( row, COLS-1, ACS_URCORNER );
328
329     row++;
330     mvaddch ( row, 0, ACS_VLINE );
331     attrset ( A_REVERSE );
332     mvhline ( row, 1, ' ', ( (int) p_intf->p_sys->f_slider_state % COLS-2) );
333     attroff ( A_REVERSE );
334     mvaddch ( row, COLS-1, ACS_VLINE );
335
336     row++;
337     mvaddch ( row, 0, ACS_LLCORNER );
338     mvhline ( row, 1, ACS_HLINE, COLS-2 );
339     mvaddch ( row, COLS-1, ACS_LRCORNER );
340
341     refresh();
342
343     *t_last_refresh = time( 0 );
344 }
345
346 static void FullScreen( intf_thread_t *p_intf )
347 {
348     vout_thread_t *p_vout;
349
350     p_vout = vlc_object_find( p_intf->p_sys->p_input,
351                               VLC_OBJECT_VOUT, FIND_CHILD );
352     if( p_vout == NULL )
353     {
354         return;
355     }
356
357     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
358     vlc_object_release( p_vout );
359 }
360
361 static void Eject ( intf_thread_t *p_intf )
362 {
363     char *psz_device = NULL, *psz_parser, *psz_name;
364
365     /*
366      * Get the active input
367      * Determine whether we can eject a media, ie it's a VCD or DVD
368      * If it's neither a VCD nor a DVD, then return
369      */
370
371     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
372                                                        FIND_ANYWHERE );
373
374     if( p_playlist == NULL )
375     {
376         return;
377     }
378
379     vlc_mutex_lock( &p_playlist->object_lock );
380
381     if( p_playlist->i_index < 0 )
382     {   
383         vlc_mutex_unlock( &p_playlist->object_lock );
384         vlc_object_release( p_playlist );
385         return; 
386     }
387
388     psz_name = p_playlist->pp_items[ p_playlist->i_index ]->psz_name;
389
390     if( psz_name )
391     {
392         if( !strncmp(psz_name, "dvd:", 4) )
393         {
394             switch( psz_name[4] )
395             {
396             case '\0':
397             case '@':
398                 psz_device = config_GetPsz( p_intf, "dvd_device" );
399                 break;
400             default:
401                 /* Omit the first 4 characters */
402                 psz_device = strdup( psz_name + 4 );
403                 break;
404             }
405         }
406         else if( !strncmp(psz_name, "vcd:", 4) )
407         {
408             switch( psz_name[4] )
409             {
410             case '\0':
411             case '@':
412                 psz_device = config_GetPsz( p_intf, "vcd_device" );
413                 break;
414             default:
415                 /* Omit the first 4 characters */
416                 psz_device = strdup( psz_name + 4 );
417                 break;
418             }
419         }
420         else
421         {
422             psz_device = strdup( psz_name );
423         }
424     }
425
426     vlc_mutex_unlock( &p_playlist->object_lock );
427     vlc_object_release( p_playlist );
428
429     if( psz_device == NULL )
430     {
431         return;
432     }
433
434     /* Remove what we have after @ */
435     psz_parser = psz_device;
436     for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
437     {
438         if( *psz_parser == '@' )
439         {
440             *psz_parser = '\0';
441             break;
442         }
443     }
444
445     /* If there's a stream playing, we aren't allowed to eject ! */
446     if( p_intf->p_sys->p_input == NULL )
447     {
448         msg_Dbg( p_intf, "ejecting %s", psz_device );
449
450         intf_Eject( p_intf, psz_device );
451     }
452
453     free(psz_device);
454     return;
455 }
456
457 static void Play ( intf_thread_t *p_intf )
458 {
459     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
460                                                        FIND_ANYWHERE );
461     if( p_playlist )
462     {
463         vlc_mutex_lock( &p_playlist->object_lock );
464
465         if( p_playlist->i_size )
466         {
467             vlc_mutex_unlock( &p_playlist->object_lock );
468             playlist_Play( p_playlist );
469             vlc_object_release( p_playlist );
470         }
471         else
472         {
473             vlc_mutex_unlock( &p_playlist->object_lock );
474             vlc_object_release( p_playlist );
475         }
476     }
477 }
478
479 static void Pause ( intf_thread_t *p_intf )
480 {
481     if( p_intf->p_sys->p_input == NULL )
482     {
483         return;
484     }
485
486     input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PAUSE );
487
488     return;
489 }
490
491 static void Stop ( intf_thread_t *p_intf )
492 {
493     playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
494                                                        FIND_ANYWHERE );
495     if( p_playlist == NULL )
496     {
497         return;
498     }
499
500     playlist_Stop( p_playlist );
501     vlc_object_release( p_playlist );
502
503     return;
504 }
505
506 static void Next ( intf_thread_t *p_intf )
507 {
508     int i_id;
509     input_area_t * p_area;
510
511     i_id = p_intf->p_sys->p_input->stream.p_selected_area->i_id+1;
512
513     if ( i_id < p_intf->p_sys->p_input->stream.i_area_nb )
514     {
515         p_area = p_intf->p_sys->p_input->stream.pp_areas[i_id];
516
517         input_ChangeArea( p_intf->p_sys->p_input,
518                 (input_area_t *) p_area );
519
520         input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
521     }
522 }
523
524 static void ManageSlider ( intf_thread_t *p_intf )
525 {
526     if( p_intf->p_sys->p_input != NULL )
527     {
528         vlc_mutex_lock( &p_intf->p_sys->p_input->stream.stream_lock );
529
530         if( p_intf->p_sys->p_input->stream.b_seekable &&
531             p_intf->p_sys->p_input->stream.control.i_status == PLAYING_S )
532         {
533             float newvalue = p_intf->p_sys->f_slider_state;
534
535 #define p_area p_intf->p_sys->p_input->stream.p_selected_area
536
537             /* If the user hasn't touched the slider since the last time,
538              * then the input can safely change it */
539             if( newvalue == p_intf->p_sys->f_slider_state_old )
540             {
541                 /* Update the value */
542                 p_intf->p_sys->f_slider_state =
543                     p_intf->p_sys->f_slider_state_old =
544                     ( 100 * p_area->i_tell ) / p_area->i_size;
545             }
546             /* Otherwise, send message to the input if the user has
547              * finished dragging the slider */
548             else
549             {
550                 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
551
552                 /* release the lock to be able to seek */
553                 vlc_mutex_unlock( &p_intf->p_sys->p_input->stream.stream_lock );
554                 input_Seek( p_intf, i_seek, INPUT_SEEK_SET );
555                 vlc_mutex_lock( &p_intf->p_sys->p_input->stream.stream_lock );
556
557                 /* Update the old value */
558                 p_intf->p_sys->f_slider_state_old = newvalue;
559             }
560 #    undef p_area
561         }
562
563         vlc_mutex_unlock( &p_intf->p_sys->p_input->stream.stream_lock );
564     }
565 }
566
567 static void PrevTitle ( intf_thread_t *p_intf )
568 {
569     input_area_t *  p_area;
570     int             i_id;
571
572     i_id = p_intf->p_sys->p_input->stream.p_selected_area->i_id - 1;
573
574     /* Disallow area 0 since it is used for video_ts.vob */
575     if ( i_id > 0 )
576     {
577         p_area = p_intf->p_sys->p_input->stream.pp_areas[i_id];
578         input_ChangeArea( p_intf->p_sys->p_input, (input_area_t*)p_area );
579
580         input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
581     }
582 }
583
584 static void NextTitle ( intf_thread_t *p_intf )
585 {
586     input_area_t *  p_area;
587     int             i_id;
588
589     i_id = p_intf->p_sys->p_input->stream.p_selected_area->i_id + 1;
590
591     if ( i_id < p_intf->p_sys->p_input->stream.i_area_nb )
592     {
593         p_area = p_intf->p_sys->p_input->stream.pp_areas[i_id];
594         input_ChangeArea( p_intf->p_sys->p_input, (input_area_t*)p_area );
595
596         input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
597     }
598 }
599
600 static void PrevChapter ( intf_thread_t *p_intf )
601 {
602     input_area_t *  p_area;
603
604     p_area = p_intf->p_sys->p_input->stream.p_selected_area;
605
606     if ( p_area->i_part > 0 )
607     {
608         p_area->i_part--;
609         input_ChangeArea( p_intf->p_sys->p_input, (input_area_t*)p_area );
610
611         input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
612     }
613 }
614
615 static void NextChapter( intf_thread_t *p_intf )
616 {
617     input_area_t *  p_area;
618
619     p_area = p_intf->p_sys->p_input->stream.p_selected_area;
620
621     if ( p_area->i_part < p_area->i_part_nb )
622     {
623         p_area->i_part++;
624         input_ChangeArea( p_intf->p_sys->p_input, (input_area_t*)p_area );
625
626         input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
627     }
628 }
629