1 /*****************************************************************************
2 * input_manager.cpp : Manage an input and interact with its GUI elements
3 ****************************************************************************
4 * Copyright (C) 2006-2007 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 #include "input_manager.hpp"
26 #include "dialogs_provider.hpp"
28 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
29 vlc_value_t n, void *param );
30 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
31 vlc_value_t n, void *param );
32 static int ItemChanged( vlc_object_t *, const char *,
33 vlc_value_t, vlc_value_t, void * );
34 static int InterfaceChanged( vlc_object_t *, const char *,
35 vlc_value_t, vlc_value_t, void * );
36 static int ItemStateChanged( vlc_object_t *, const char *,
37 vlc_value_t, vlc_value_t, void * );
38 static int ItemRateChanged( vlc_object_t *, const char *,
39 vlc_value_t, vlc_value_t, void * );
40 static int ItemTitleChanged( vlc_object_t *, const char *,
41 vlc_value_t, vlc_value_t, void * );
42 static int VolumeChanged( vlc_object_t *, const char *,
43 vlc_value_t, vlc_value_t, void * );
45 /**********************************************************************
46 * InputManager implementation
47 **********************************************************************/
49 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
50 QObject( parent ), p_intf( _p_intf )
52 i_old_playing_status = END_S;
56 var_AddCallback( THEPL, "playlist-current", ItemChanged, this );
57 var_AddCallback( THEPL, "intf-change", ItemChanged, this );
60 InputManager::~InputManager()
63 var_DelCallback( THEPL, "playlist-current", ItemChanged, this );
64 var_DelCallback( THEPL, "intf-change", ItemChanged, this );
67 void InputManager::setInput( input_thread_t *_p_input )
71 b_had_audio = b_had_video = b_has_audio = b_has_video = false;
74 vlc_object_yield( p_input );
76 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
77 b_has_video = val.i_int > 0;
78 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
79 b_has_audio = val.i_int > 0;
80 emit statusChanged( PLAYING_S );
85 void InputManager::delInput()
90 vlc_object_release( p_input );
93 i_old_playing_status = END_S;
96 emit positionUpdated( 0.0, 0 ,0 );
97 emit statusChanged( END_S );
98 emit nameChanged( "" );
99 emit artChanged( "" );
102 void InputManager::delCallbacks( void )
104 var_DelCallback( p_input, "audio-es", ChangeAudio, this );
105 var_DelCallback( p_input, "video-es", ChangeVideo, this );
106 var_DelCallback( THEPL, "state", ItemStateChanged, this );
107 var_DelCallback( p_input, "rate", ItemRateChanged, this );
108 var_DelCallback( p_input, "title", ItemTitleChanged, this );
109 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
110 var_DelCallback( THEPL, "item-change", ItemChanged, this );
113 void InputManager::addCallbacks( void )
115 var_AddCallback( p_input, "audio-es", ChangeAudio, this );
116 var_AddCallback( p_input, "video-es", ChangeVideo, this );
117 /* src/playlist/control.c */
118 var_AddCallback( THEPL, "state", ItemStateChanged, this );
119 /* src/input/input.c:1765 */
120 var_AddCallback( p_input, "rate", ItemRateChanged, this );
121 /* src/input/input.c:2003 */
122 var_AddCallback( p_input, "title", ItemTitleChanged, this );
123 /* src/input/input.c:734 for timers update*/
124 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
125 /* src/input/input.c:2076*/
126 var_AddCallback( THEPL, "item-change", ItemChanged, this );
129 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
130 vlc_value_t oldval, vlc_value_t newval, void *param )
132 static int counter = 0;
133 InputManager *im = (InputManager*)param;
135 counter = counter++ % 4;
138 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
139 QApplication::postEvent( im, static_cast<QEvent*>(event) );
143 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
144 vlc_value_t oldval, vlc_value_t newval, void *param )
146 InputManager *im = (InputManager*)param;
148 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
149 QApplication::postEvent( im, static_cast<QEvent*>(event) );
153 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
154 vlc_value_t oldval, vlc_value_t newval, void *param )
156 InputManager *im = (InputManager*)param;
158 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
159 QApplication::postEvent( im, static_cast<QEvent*>(event) );
163 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
164 vlc_value_t oldval, vlc_value_t newval, void *param )
166 InputManager *im = (InputManager*)param;
168 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
169 QApplication::postEvent( im, static_cast<QEvent*>(event) );
173 static int InputChanged( vlc_object_t *p_this, const char *psz_var,
174 vlc_value_t oldval, vlc_value_t newval, void *param )
176 MainInputManager *im = (MainInputManager*)param;
178 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
179 QApplication::postEvent( im, static_cast<QEvent*>(event) );
183 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
184 vlc_value_t oldval, vlc_value_t newval, void *param )
186 MainInputManager *im = (MainInputManager*)param;
188 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
189 QApplication::postEvent( im, static_cast<QEvent*>(event) );
193 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
194 vlc_value_t oldval, vlc_value_t newval, void *param )
196 InputManager *im = (InputManager*)param;
198 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
199 QApplication::postEvent( im, static_cast<QEvent*>(event) );
203 void InputManager::customEvent( QEvent *event )
205 int type = event->type();
206 if ( type != PositionUpdate_Type && type != ItemChanged_Type &&
207 type != ItemRateChanged_Type && type != ItemTitleChanged_Type &&
208 type != ItemStateChanged_Type )
211 if(!p_input || p_input->b_dead || p_input->b_die )
217 IMEvent *ime = static_cast<IMEvent *>(event);
219 if ( type == PositionUpdate_Type )
223 else if ( type == ItemChanged_Type )
227 else if ( type == ItemRateChanged_Type )
231 else if ( type == ItemTitleChanged_Type )
235 else if (type == ItemStateChanged_Type )
241 void InputManager::UpdatePosition( void )
243 /* Update position */
244 int i_length, i_time; /* Int is enough, since we store seconds */
246 i_length = var_GetTime( p_input , "length" ) / 1000000;
247 i_time = var_GetTime( p_input , "time") / 1000000;
248 f_pos = var_GetFloat( p_input , "position" );
249 emit positionUpdated( f_pos, i_time, i_length );
252 void InputManager::UpdateTitle( void )
254 /* Update navigation status */
255 vlc_value_t val; val.i_int = 0;
256 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
260 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
261 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
265 emit navigationChanged( 0 );
269 void InputManager::UpdateStatus( void )
271 /* Update playing status */
272 vlc_value_t val; val.i_int = 0;
273 var_Get( p_input, "state", &val );
274 if( i_old_playing_status != val.i_int )
276 i_old_playing_status = val.i_int;
277 emit statusChanged( val.i_int );
281 void InputManager::UpdateRate( void )
284 int i_new_rate = var_GetInteger( p_input, "rate");
285 if( i_new_rate != i_rate )
289 emit rateChanged( i_rate );
293 void InputManager::UpdateMeta( void )
297 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
298 char *psz_nowplaying =
299 input_item_GetNowPlaying( input_GetItem( p_input ) );
300 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
301 if( EMPTY_STR( psz_name ) )
304 psz_name = input_item_GetName( input_GetItem( p_input ) );
306 if( !EMPTY_STR( psz_nowplaying ) )
308 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
310 else if( !EMPTY_STR( psz_artist ) )
312 text.sprintf( "%s - %s", psz_artist, psz_name );
316 text.sprintf( "%s", psz_name );
319 free( psz_nowplaying );
321 if( old_name != text )
323 emit nameChanged( text );
328 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
329 url.sprintf("%s", psz_art );
333 artUrl = url.replace( "file://",QString("" ) );
334 emit artChanged( artUrl );
337 /* Update teletext status*/
338 emit teletextEnabled( true );/* FIXME */
343 void InputManager::sliderUpdate( float new_pos )
345 if( hasInput() ) var_SetFloat( p_input, "position", new_pos );
348 void InputManager::togglePlayPause()
351 var_Get( p_input, "state", &state );
352 state.i_int = ( ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S );
353 msg_Dbg( p_input, "state : %d", state.i_int );
355 /* A stream is being played, pause it */
356 /* state.i_int = PAUSE_S;
360 /* Stream is paused, resume it */
361 /*state.i_int = PLAYING_S;
363 var_Set( p_input, "state", state );
364 emit statusChanged( state.i_int );
367 void InputManager::sectionPrev()
371 int i_type = var_Type( p_input, "next-chapter" );
372 vlc_value_t val; val.b_bool = VLC_TRUE;
373 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
374 "prev-chapter":"prev-title", val );
378 void InputManager::sectionNext()
382 int i_type = var_Type( p_input, "next-chapter" );
383 vlc_value_t val; val.b_bool = VLC_TRUE;
384 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
385 "next-chapter":"next-title", val );
389 void InputManager::sectionMenu()
392 var_SetInteger( p_input, "title 0", 2 );
396 void InputManager::telexGotoPage( int page )
401 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
402 "zvbi", FIND_ANYWHERE );
405 var_SetInteger( p_vbi, "vbi-page", page );
406 vlc_object_release( p_vbi );
411 void InputManager::telexToggle( bool b_enabled )
417 telexGotoPage( i_page );
420 void InputManager::telexSetTransparency( bool b_transp )
425 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
426 "zvbi", FIND_ANYWHERE );
429 var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
430 vlc_object_release( p_vbi );
436 void InputManager::slower()
439 var_SetVoid( p_input, "rate-slower" );
442 void InputManager::faster()
445 var_SetVoid( p_input, "rate-faster" );
448 void InputManager::normalRate()
451 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
454 void InputManager::setRate( int new_rate )
457 var_SetInteger( p_input, "rate", new_rate );
460 /**********************************************************************
461 * MainInputManager implementation. Wrap an input manager and
462 * take care of updating the main playlist input
463 **********************************************************************/
464 MainInputManager * MainInputManager::instance = NULL;
466 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
470 im = new InputManager( this, p_intf );
471 var_AddCallback( THEPL, "playlist-current", InputChanged, this );
472 var_AddCallback( THEPL, "activity", InputChanged, this );
473 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
474 /* Warn our embedded IM about input changes */
475 CONNECT( this, inputChanged( input_thread_t * ),
476 im, setInput( input_thread_t * ) );
479 MainInputManager::~MainInputManager()
483 vlc_object_release( p_input );
484 emit inputChanged( NULL );
486 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
487 var_DelCallback( THEPL, "playlist-current", InputChanged, this );
488 var_DelCallback( THEPL, "activity", InputChanged, this );
491 void MainInputManager::customEvent( QEvent *event )
493 int type = event->type();
494 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
497 if( type == VolumeChanged_Type )
499 emit volumeChanged();
503 if( VLC_OBJECT_INTF == p_intf->i_object_type )
505 vlc_mutex_lock( &p_intf->change_lock );
506 if( p_input && ( p_input->b_dead || p_input->b_die ) )
508 var_DelCallback( p_input, "state", InputChanged, this );
509 vlc_object_release( p_input );
511 emit inputChanged( NULL );
517 p_input = THEPL->p_input;
518 if( p_input && !( p_input->b_die || p_input->b_dead) )
520 vlc_object_yield( p_input );
521 var_AddCallback( p_input, "state", InputChanged, this );
522 emit inputChanged( p_input );
528 vlc_mutex_unlock( &p_intf->change_lock );
531 /* we are working as a dialogs provider */
532 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
533 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
536 p_input = p_playlist->p_input;
537 vlc_object_yield( p_input );
538 emit inputChanged( p_input );
543 void MainInputManager::stop()
545 playlist_Stop( THEPL );
548 void MainInputManager::next()
550 playlist_Next( THEPL );
553 void MainInputManager::prev()
555 playlist_Prev( THEPL );
558 void MainInputManager::togglePlayPause()
560 if( p_input == NULL )
562 playlist_Play( THEPL );
565 getIM()->togglePlayPause();
570 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
571 vlc_value_t n, void *param )
573 InputManager *im = (InputManager*)param;
574 im->b_has_audio = true;
578 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
579 vlc_value_t n, void *param )
581 InputManager *im = (InputManager*)param;
582 im->b_has_video = true;