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>
8 * Ilkka Ollakka <ileoo@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
26 #include "input_manager.hpp"
27 #include "dialogs_provider.hpp"
29 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
30 vlc_value_t n, void *param );
31 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
32 vlc_value_t n, void *param );
33 static int ItemChanged( vlc_object_t *, const char *,
34 vlc_value_t, vlc_value_t, void * );
35 static int InterfaceChanged( vlc_object_t *, const char *,
36 vlc_value_t, vlc_value_t, void * );
37 static int ItemStateChanged( vlc_object_t *, const char *,
38 vlc_value_t, vlc_value_t, void * );
39 static int ItemRateChanged( vlc_object_t *, const char *,
40 vlc_value_t, vlc_value_t, void * );
41 static int ItemTitleChanged( vlc_object_t *, const char *,
42 vlc_value_t, vlc_value_t, void * );
43 static int VolumeChanged( vlc_object_t *, const char *,
44 vlc_value_t, vlc_value_t, void * );
46 /**********************************************************************
47 * InputManager implementation
48 **********************************************************************/
50 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
51 QObject( parent ), p_intf( _p_intf )
53 i_old_playing_status = END_S;
57 var_AddCallback( THEPL, "playlist-current", ItemChanged, this );
58 var_AddCallback( THEPL, "intf-change", ItemChanged, this );
61 InputManager::~InputManager()
64 var_DelCallback( THEPL, "playlist-current", ItemChanged, this );
65 var_DelCallback( THEPL, "intf-change", ItemChanged, this );
68 void InputManager::setInput( input_thread_t *_p_input )
72 b_had_audio = b_had_video = b_has_audio = b_has_video = false;
75 vlc_object_yield( p_input );
77 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
78 b_has_video = val.i_int > 0;
79 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
80 b_has_audio = val.i_int > 0;
81 emit statusChanged( PLAYING_S );
86 void InputManager::delInput()
91 vlc_object_release( p_input );
94 i_old_playing_status = END_S;
97 emit positionUpdated( 0.0, 0 ,0 );
98 emit statusChanged( END_S );
99 emit nameChanged( "" );
100 emit artChanged( "" );
103 void InputManager::delCallbacks( void )
105 var_DelCallback( p_input, "audio-es", ChangeAudio, this );
106 var_DelCallback( p_input, "video-es", ChangeVideo, this );
107 var_DelCallback( THEPL, "state", ItemStateChanged, this );
108 var_DelCallback( p_input, "rate", ItemRateChanged, this );
109 var_DelCallback( p_input, "title", ItemTitleChanged, this );
110 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
111 var_DelCallback( THEPL, "item-change", ItemChanged, this );
114 void InputManager::addCallbacks( void )
116 var_AddCallback( p_input, "audio-es", ChangeAudio, this );
117 var_AddCallback( p_input, "video-es", ChangeVideo, this );
118 /* src/playlist/control.c */
119 var_AddCallback( THEPL, "state", ItemStateChanged, this );
120 /* src/input/input.c:1765 */
121 var_AddCallback( p_input, "rate", ItemRateChanged, this );
122 /* src/input/input.c:2003 */
123 var_AddCallback( p_input, "title", ItemTitleChanged, this );
124 /* src/input/input.c:734 for timers update*/
125 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
126 /* src/input/input.c:2076*/
127 var_AddCallback( THEPL, "item-change", ItemChanged, this );
130 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
131 vlc_value_t oldval, vlc_value_t newval, void *param )
133 static int counter = 0;
134 InputManager *im = (InputManager*)param;
136 counter = counter++ % 4;
139 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
140 QApplication::postEvent( im, static_cast<QEvent*>(event) );
144 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
145 vlc_value_t oldval, vlc_value_t newval, void *param )
147 InputManager *im = (InputManager*)param;
149 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
150 QApplication::postEvent( im, static_cast<QEvent*>(event) );
154 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
155 vlc_value_t oldval, vlc_value_t newval, void *param )
157 InputManager *im = (InputManager*)param;
159 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
160 QApplication::postEvent( im, static_cast<QEvent*>(event) );
164 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
165 vlc_value_t oldval, vlc_value_t newval, void *param )
167 InputManager *im = (InputManager*)param;
169 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
170 QApplication::postEvent( im, static_cast<QEvent*>(event) );
174 static int InputChanged( vlc_object_t *p_this, const char *psz_var,
175 vlc_value_t oldval, vlc_value_t newval, void *param )
177 MainInputManager *im = (MainInputManager*)param;
179 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
180 QApplication::postEvent( im, static_cast<QEvent*>(event) );
184 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
185 vlc_value_t oldval, vlc_value_t newval, void *param )
187 MainInputManager *im = (MainInputManager*)param;
189 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
190 QApplication::postEvent( im, static_cast<QEvent*>(event) );
194 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
195 vlc_value_t oldval, vlc_value_t newval, void *param )
197 InputManager *im = (InputManager*)param;
199 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
200 QApplication::postEvent( im, static_cast<QEvent*>(event) );
204 void InputManager::customEvent( QEvent *event )
206 int type = event->type();
207 if ( type != PositionUpdate_Type && type != ItemChanged_Type &&
208 type != ItemRateChanged_Type && type != ItemTitleChanged_Type &&
209 type != ItemStateChanged_Type )
212 if(!p_input || p_input->b_dead || p_input->b_die )
218 IMEvent *ime = static_cast<IMEvent *>(event);
220 if ( type == PositionUpdate_Type )
224 else if ( type == ItemChanged_Type )
229 else if ( type == ItemRateChanged_Type )
233 else if ( type == ItemTitleChanged_Type )
237 else if (type == ItemStateChanged_Type )
243 void InputManager::UpdatePosition( void )
245 /* Update position */
246 int i_length, i_time; /* Int is enough, since we store seconds */
248 i_length = var_GetTime( p_input , "length" ) / 1000000;
249 i_time = var_GetTime( p_input , "time") / 1000000;
250 f_pos = var_GetFloat( p_input , "position" );
251 emit positionUpdated( f_pos, i_time, i_length );
254 void InputManager::UpdateTitle( void )
256 /* Update navigation status */
257 vlc_value_t val; val.i_int = 0;
258 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
259 msg_Dbg( p_intf, "updateTitle called" );
263 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
264 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
268 emit navigationChanged( 0 );
272 void InputManager::UpdateStatus( void )
274 /* Update playing status */
275 vlc_value_t val; val.i_int = 0;
276 var_Get( p_input, "state", &val );
277 if( i_old_playing_status != val.i_int )
279 i_old_playing_status = val.i_int;
280 emit statusChanged( val.i_int );
284 void InputManager::UpdateRate( void )
287 int i_new_rate = var_GetInteger( p_input, "rate");
288 if( i_new_rate != i_rate )
292 emit rateChanged( i_rate );
296 void InputManager::UpdateMeta( void )
300 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
301 char *psz_nowplaying =
302 input_item_GetNowPlaying( input_GetItem( p_input ) );
303 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
304 if( EMPTY_STR( psz_name ) )
307 psz_name = input_item_GetName( input_GetItem( p_input ) );
309 if( !EMPTY_STR( psz_nowplaying ) )
311 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
313 else if( !EMPTY_STR( psz_artist ) )
315 text.sprintf( "%s - %s", psz_artist, psz_name );
319 text.sprintf( "%s", psz_name );
322 free( psz_nowplaying );
324 if( old_name != text )
326 emit nameChanged( text );
331 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
332 url.sprintf("%s", psz_art );
336 artUrl = url.replace( "file://",QString("" ) );
337 emit artChanged( artUrl );
340 /* Update teletext status*/
341 emit teletextEnabled( true );/* FIXME */
346 void InputManager::sliderUpdate( float new_pos )
348 if( hasInput() ) var_SetFloat( p_input, "position", new_pos );
351 void InputManager::togglePlayPause()
354 var_Get( p_input, "state", &state );
355 state.i_int = ( ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S );
356 msg_Dbg( p_input, "state : %d", state.i_int );
358 /* A stream is being played, pause it */
359 /* state.i_int = PAUSE_S;
363 /* Stream is paused, resume it */
364 /*state.i_int = PLAYING_S;
366 var_Set( p_input, "state", state );
367 emit statusChanged( state.i_int );
370 void InputManager::sectionPrev()
374 int i_type = var_Type( p_input, "next-chapter" );
375 vlc_value_t val; val.b_bool = VLC_TRUE;
376 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
377 "prev-chapter":"prev-title", val );
381 void InputManager::sectionNext()
385 int i_type = var_Type( p_input, "next-chapter" );
386 vlc_value_t val; val.b_bool = VLC_TRUE;
387 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
388 "next-chapter":"next-title", val );
392 void InputManager::sectionMenu()
395 var_SetInteger( p_input, "title 0", 2 );
399 void InputManager::telexGotoPage( int page )
404 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
405 "zvbi", FIND_ANYWHERE );
408 var_SetInteger( p_vbi, "vbi-page", page );
409 vlc_object_release( p_vbi );
414 void InputManager::telexToggle( bool b_enabled )
420 telexGotoPage( i_page );
423 void InputManager::telexSetTransparency( bool b_transp )
428 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
429 "zvbi", FIND_ANYWHERE );
432 var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
433 vlc_object_release( p_vbi );
439 void InputManager::slower()
442 var_SetVoid( p_input, "rate-slower" );
445 void InputManager::faster()
448 var_SetVoid( p_input, "rate-faster" );
451 void InputManager::normalRate()
454 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
457 void InputManager::setRate( int new_rate )
460 var_SetInteger( p_input, "rate", new_rate );
463 /**********************************************************************
464 * MainInputManager implementation. Wrap an input manager and
465 * take care of updating the main playlist input
466 **********************************************************************/
467 MainInputManager * MainInputManager::instance = NULL;
469 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
473 im = new InputManager( this, p_intf );
474 var_AddCallback( THEPL, "playlist-current", InputChanged, this );
475 var_AddCallback( THEPL, "activity", InputChanged, this );
476 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
477 /* Warn our embedded IM about input changes */
478 CONNECT( this, inputChanged( input_thread_t * ),
479 im, setInput( input_thread_t * ) );
482 MainInputManager::~MainInputManager()
486 vlc_object_release( p_input );
487 emit inputChanged( NULL );
489 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
490 var_DelCallback( THEPL, "playlist-current", InputChanged, this );
491 var_DelCallback( THEPL, "activity", InputChanged, this );
494 void MainInputManager::customEvent( QEvent *event )
496 int type = event->type();
497 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
500 if( type == VolumeChanged_Type )
502 emit volumeChanged();
506 if( VLC_OBJECT_INTF == p_intf->i_object_type )
508 vlc_mutex_lock( &p_intf->change_lock );
509 if( p_input && ( p_input->b_dead || p_input->b_die ) )
511 var_DelCallback( p_input, "state", InputChanged, this );
512 vlc_object_release( p_input );
514 emit inputChanged( NULL );
520 p_input = THEPL->p_input;
521 if( p_input && !( p_input->b_die || p_input->b_dead) )
523 vlc_object_yield( p_input );
524 var_AddCallback( p_input, "state", InputChanged, this );
525 emit inputChanged( p_input );
531 vlc_mutex_unlock( &p_intf->change_lock );
534 /* we are working as a dialogs provider */
535 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
536 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
539 p_input = p_playlist->p_input;
540 vlc_object_yield( p_input );
541 emit inputChanged( p_input );
546 void MainInputManager::stop()
548 playlist_Stop( THEPL );
551 void MainInputManager::next()
553 playlist_Next( THEPL );
556 void MainInputManager::prev()
558 playlist_Prev( THEPL );
561 void MainInputManager::togglePlayPause()
563 if( p_input == NULL )
565 playlist_Play( THEPL );
568 getIM()->togglePlayPause();
573 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
574 vlc_value_t n, void *param )
576 InputManager *im = (InputManager*)param;
577 im->b_has_audio = true;
581 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
582 vlc_value_t n, void *param )
584 InputManager *im = (InputManager*)param;
585 im->b_has_video = true;