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 $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
29 #include <errno.h> /* ENOMEM */
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close ( vlc_object_t * );
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 * );
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 * );
62 /*****************************************************************************
64 *****************************************************************************/
66 set_description( _("ncurses interface module") );
67 set_capability( "interface", 10 );
68 set_callbacks( Open, Close );
69 add_shortcut( "curses" );
72 /*****************************************************************************
73 * intf_sys_t: description and status of ncurses interface
74 *****************************************************************************/
77 input_thread_t * p_input;
80 float f_slider_state_old;
83 /*****************************************************************************
84 * Open: initialize and create window
85 *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
88 intf_thread_t *p_intf = (intf_thread_t *)p_this;
90 /* Allocate instance and initialize some members */
91 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
92 if( p_intf->p_sys == NULL )
94 msg_Err( p_intf, "out of memory" );
98 p_intf->p_sys->p_input = NULL;
100 p_intf->pf_run = Run;
102 /* Initialize the curses library */
104 /* Don't do NL -> CR/NL */
106 /* Take input chars one at a time */
119 /*****************************************************************************
120 * Close: destroy interface window
121 *****************************************************************************/
122 static void Close( vlc_object_t *p_this )
124 intf_thread_t *p_intf = (intf_thread_t *)p_this;
126 if( p_intf->p_sys->p_input )
128 vlc_object_release( p_intf->p_sys->p_input );
131 /* Close the ncurses interface */
134 /* Destroy structure */
135 free( p_intf->p_sys );
138 /*****************************************************************************
139 * Run: ncurses thread
140 *****************************************************************************/
141 static void Run( intf_thread_t *p_intf )
144 time_t t_last_refresh;
147 * force drawing the interface for the first time
149 t_last_refresh = ( time( 0 ) - 1);
151 while( !p_intf->b_die )
153 msleep( INTF_IDLE_SLEEP );
155 /* Update the input */
156 if( p_intf->p_sys->p_input == NULL )
158 p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
161 else if( p_intf->p_sys->p_input->b_dead )
163 vlc_object_release( p_intf->p_sys->p_input );
164 p_intf->p_sys->p_input = NULL;
167 while( (i_key = getch()) != -1 )
170 * HandleKey returns 1 if the screen needs to be redrawn
172 if ( HandleKey( p_intf, i_key ) )
174 Redraw( p_intf, &t_last_refresh );
179 * redraw the screen every second
181 if ( (time(0) - t_last_refresh) >= 1 )
183 ManageSlider ( p_intf );
184 Redraw( p_intf, &t_last_refresh );
189 /* following functions are local */
191 static int HandleKey( intf_thread_t *p_intf, int i_key )
201 FullScreen( p_intf );
233 PrevChapter( p_intf );
237 NextChapter( p_intf );
241 p_intf->p_sys->f_slider_state += 100;
242 ManageSlider ( p_intf );
246 p_intf->p_sys->f_slider_state--;
247 ManageSlider ( p_intf );
251 * ^l should clear and redraw the screen
264 static int PrintFullLine ( const char *p_fmt, ... )
270 va_start ( vl_args, p_fmt );
271 vasprintf ( &p_buf, p_fmt, vl_args );
279 i_len = strlen( p_buf );
282 * make sure we don't exceed the border on the right side
288 printw( "%s", p_buf );
292 printw( "%s", p_buf );
293 hline( ' ', COLS - i_len );
302 Redraw ( intf_thread_t *p_intf, time_t *t_last_refresh )
308 attrset ( A_REVERSE );
309 PrintFullLine( VOUT_TITLE " (ncurses interface)" );
310 attroff ( A_REVERSE );
317 if ( p_intf->p_sys->p_input != NULL )
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 );
325 mvaddch ( row, 0, ACS_ULCORNER );
326 mvhline ( row, 1, ACS_HLINE, COLS-2 );
327 mvaddch ( row, COLS-1, ACS_URCORNER );
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 );
337 mvaddch ( row, 0, ACS_LLCORNER );
338 mvhline ( row, 1, ACS_HLINE, COLS-2 );
339 mvaddch ( row, COLS-1, ACS_LRCORNER );
343 *t_last_refresh = time( 0 );
346 static void FullScreen( intf_thread_t *p_intf )
348 vout_thread_t *p_vout;
350 p_vout = vlc_object_find( p_intf->p_sys->p_input,
351 VLC_OBJECT_VOUT, FIND_CHILD );
357 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
358 vlc_object_release( p_vout );
361 static void Eject ( intf_thread_t *p_intf )
363 char *psz_device = NULL, *psz_parser, *psz_name;
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
371 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
374 if( p_playlist == NULL )
379 vlc_mutex_lock( &p_playlist->object_lock );
381 if( p_playlist->i_index < 0 )
383 vlc_mutex_unlock( &p_playlist->object_lock );
384 vlc_object_release( p_playlist );
388 psz_name = p_playlist->pp_items[ p_playlist->i_index ]->psz_name;
392 if( !strncmp(psz_name, "dvd:", 4) )
394 switch( psz_name[4] )
398 psz_device = config_GetPsz( p_intf, "dvd_device" );
401 /* Omit the first 4 characters */
402 psz_device = strdup( psz_name + 4 );
406 else if( !strncmp(psz_name, "vcd:", 4) )
408 switch( psz_name[4] )
412 psz_device = config_GetPsz( p_intf, "vcd_device" );
415 /* Omit the first 4 characters */
416 psz_device = strdup( psz_name + 4 );
422 psz_device = strdup( psz_name );
426 vlc_mutex_unlock( &p_playlist->object_lock );
427 vlc_object_release( p_playlist );
429 if( psz_device == NULL )
434 /* Remove what we have after @ */
435 psz_parser = psz_device;
436 for( psz_parser = psz_device ; *psz_parser ; psz_parser++ )
438 if( *psz_parser == '@' )
445 /* If there's a stream playing, we aren't allowed to eject ! */
446 if( p_intf->p_sys->p_input == NULL )
448 msg_Dbg( p_intf, "ejecting %s", psz_device );
450 intf_Eject( p_intf, psz_device );
457 static void Play ( intf_thread_t *p_intf )
459 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
463 vlc_mutex_lock( &p_playlist->object_lock );
465 if( p_playlist->i_size )
467 vlc_mutex_unlock( &p_playlist->object_lock );
468 playlist_Play( p_playlist );
469 vlc_object_release( p_playlist );
473 vlc_mutex_unlock( &p_playlist->object_lock );
474 vlc_object_release( p_playlist );
479 static void Pause ( intf_thread_t *p_intf )
481 if( p_intf->p_sys->p_input == NULL )
486 input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PAUSE );
491 static void Stop ( intf_thread_t *p_intf )
493 playlist_t * p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
495 if( p_playlist == NULL )
500 playlist_Stop( p_playlist );
501 vlc_object_release( p_playlist );
506 static void Next ( intf_thread_t *p_intf )
509 input_area_t * p_area;
511 i_id = p_intf->p_sys->p_input->stream.p_selected_area->i_id+1;
513 if ( i_id < p_intf->p_sys->p_input->stream.i_area_nb )
515 p_area = p_intf->p_sys->p_input->stream.pp_areas[i_id];
517 input_ChangeArea( p_intf->p_sys->p_input,
518 (input_area_t *) p_area );
520 input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
524 static void ManageSlider ( intf_thread_t *p_intf )
526 if( p_intf->p_sys->p_input != NULL )
528 vlc_mutex_lock( &p_intf->p_sys->p_input->stream.stream_lock );
530 if( p_intf->p_sys->p_input->stream.b_seekable &&
531 p_intf->p_sys->p_input->stream.control.i_status == PLAYING_S )
533 float newvalue = p_intf->p_sys->f_slider_state;
535 #define p_area p_intf->p_sys->p_input->stream.p_selected_area
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 )
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;
546 /* Otherwise, send message to the input if the user has
547 * finished dragging the slider */
550 off_t i_seek = ( newvalue * p_area->i_size ) / 100;
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 );
557 /* Update the old value */
558 p_intf->p_sys->f_slider_state_old = newvalue;
563 vlc_mutex_unlock( &p_intf->p_sys->p_input->stream.stream_lock );
567 static void PrevTitle ( intf_thread_t *p_intf )
569 input_area_t * p_area;
572 i_id = p_intf->p_sys->p_input->stream.p_selected_area->i_id - 1;
574 /* Disallow area 0 since it is used for video_ts.vob */
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 );
580 input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
584 static void NextTitle ( intf_thread_t *p_intf )
586 input_area_t * p_area;
589 i_id = p_intf->p_sys->p_input->stream.p_selected_area->i_id + 1;
591 if ( i_id < p_intf->p_sys->p_input->stream.i_area_nb )
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 );
596 input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
600 static void PrevChapter ( intf_thread_t *p_intf )
602 input_area_t * p_area;
604 p_area = p_intf->p_sys->p_input->stream.p_selected_area;
606 if ( p_area->i_part > 0 )
609 input_ChangeArea( p_intf->p_sys->p_input, (input_area_t*)p_area );
611 input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
615 static void NextChapter( intf_thread_t *p_intf )
617 input_area_t * p_area;
619 p_area = p_intf->p_sys->p_input->stream.p_selected_area;
621 if ( p_area->i_part < p_area->i_part_nb )
624 input_ChangeArea( p_intf->p_sys->p_input, (input_area_t*)p_area );
626 input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );