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 *****************************************************************************/
29 #include "input_manager.hpp"
30 #include "dialogs_provider.hpp"
32 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
33 vlc_value_t n, void *param );
34 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
35 vlc_value_t n, void *param );
36 static int ItemChanged( vlc_object_t *, const char *,
37 vlc_value_t, vlc_value_t, void * );
38 static int InputChanged( vlc_object_t *, const char *,
39 vlc_value_t, vlc_value_t, void * );
40 static int InterfaceChanged( vlc_object_t *, const char *,
41 vlc_value_t, vlc_value_t, void * );
42 static int ItemStateChanged( vlc_object_t *, const char *,
43 vlc_value_t, vlc_value_t, void * );
44 static int ItemRateChanged( vlc_object_t *, const char *,
45 vlc_value_t, vlc_value_t, void * );
46 static int ItemTitleChanged( vlc_object_t *, const char *,
47 vlc_value_t, vlc_value_t, void * );
48 static int VolumeChanged( vlc_object_t *, const char *,
49 vlc_value_t, vlc_value_t, void * );
51 /**********************************************************************
52 * InputManager implementation
53 **********************************************************************/
55 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
56 QObject( parent ), p_intf( _p_intf )
58 i_old_playing_status = END_S;
64 InputManager::~InputManager()
69 void InputManager::setInput( input_thread_t *_p_input )
73 b_had_audio = b_had_video = b_has_audio = b_has_video = false;
76 vlc_object_yield( p_input );
78 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
79 b_has_video = val.i_int > 0;
80 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
81 b_has_audio = val.i_int > 0;
82 emit statusChanged( PLAYING_S );
87 void InputManager::delInput()
92 vlc_object_release( p_input );
95 i_old_playing_status = END_S;
98 emit positionUpdated( 0.0, 0 ,0 );
99 emit statusChanged( END_S );
100 emit nameChanged( "" );
101 emit artChanged( "" );
104 void InputManager::addCallbacks( void )
106 /* We don't care about:
113 - position, time, length, because they are included in intf-change
115 /* src/input/input.c:1629 */
116 var_AddCallback( p_input, "state", ItemStateChanged, this );
117 /* src/input/es-out.c:550 */
118 var_AddCallback( p_input, "audio-es", ChangeAudio, this );
119 /* src/input/es-out.c:551 */
120 var_AddCallback( p_input, "video-es", ChangeVideo, this );
121 /* src/input/input.c:1765 */
122 var_AddCallback( p_input, "rate", ItemRateChanged, this );
123 /* src/input/input.c:2003 */
124 var_AddCallback( p_input, "title", ItemTitleChanged, this );
125 /* src/input/input.c:734 for timers update*/
126 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
129 void InputManager::delCallbacks( void )
131 var_DelCallback( p_input, "audio-es", ChangeAudio, this );
132 var_DelCallback( p_input, "video-es", ChangeVideo, this );
133 var_DelCallback( p_input, "state", ItemStateChanged, this );
134 var_DelCallback( p_input, "rate", ItemRateChanged, this );
135 var_DelCallback( p_input, "title", ItemTitleChanged, this );
136 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
139 void InputManager::customEvent( QEvent *event )
141 int type = event->type();
142 if ( type != PositionUpdate_Type && type != ItemChanged_Type &&
143 type != ItemRateChanged_Type && type != ItemTitleChanged_Type &&
144 type != ItemStateChanged_Type )
147 if( !p_input || p_input->b_dead || p_input->b_die )
153 IMEvent *ime = static_cast<IMEvent *>(event);
157 case PositionUpdate_Type:
160 case ItemChanged_Type:
164 case ItemRateChanged_Type:
167 case ItemTitleChanged_Type:
170 case ItemStateChanged_Type:
176 void InputManager::UpdatePosition( void )
178 /* Update position */
179 int i_length, i_time; /* Int is enough, since we store seconds */
181 i_length = var_GetTime( p_input , "length" ) / 1000000;
182 i_time = var_GetTime( p_input , "time") / 1000000;
183 f_pos = var_GetFloat( p_input , "position" );
184 emit positionUpdated( f_pos, i_time, i_length );
187 void InputManager::UpdateTitle( void )
189 /* Update navigation status */
190 vlc_value_t val; val.i_int = 0;
191 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
192 msg_Dbg( p_intf, "updateTitle called" );
196 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
197 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
201 emit navigationChanged( 0 );
205 void InputManager::UpdateStatus( void )
207 /* Update playing status */
208 vlc_value_t val; val.i_int = 0;
209 var_Get( p_input, "state", &val );
210 if( i_old_playing_status != val.i_int )
212 i_old_playing_status = val.i_int;
213 emit statusChanged( val.i_int );
217 void InputManager::UpdateRate( void )
220 int i_new_rate = var_GetInteger( p_input, "rate");
221 if( i_new_rate != i_rate )
225 emit rateChanged( i_rate );
229 void InputManager::UpdateMeta( void )
231 /* Update text, name and nowplaying */
234 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
235 if( EMPTY_STR( psz_name ) )
238 psz_name = input_item_GetName( input_GetItem( p_input ) );
241 char *psz_nowplaying =
242 input_item_GetNowPlaying( input_GetItem( p_input ) );
243 if( !EMPTY_STR( psz_nowplaying ) )
245 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
249 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
250 if( !EMPTY_STR( psz_artist ) )
252 text.sprintf( "%s - %s", psz_artist, psz_name );
256 text.sprintf( "%s", psz_name );
257 } free( psz_artist );
260 free( psz_nowplaying );
262 if( old_name != text )
264 emit nameChanged( text );
270 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
271 url.sprintf("%s", psz_art );
275 artUrl = url.replace( "file://",QString("" ) );
276 emit artChanged( artUrl );
279 /* Update ZVBI status */
281 /* Update teletext status*/
282 emit teletextEnabled( true );/* FIXME */
286 /* User update of the slider */
287 void InputManager::sliderUpdate( float new_pos )
290 var_SetFloat( p_input, "position", new_pos );
293 void InputManager::togglePlayPause()
296 var_Get( p_input, "state", &state );
297 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
298 var_Set( p_input, "state", state );
299 emit statusChanged( state.i_int );
302 void InputManager::sectionPrev()
306 int i_type = var_Type( p_input, "next-chapter" );
307 vlc_value_t val; val.b_bool = VLC_TRUE;
308 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
309 "prev-chapter":"prev-title", val );
313 void InputManager::sectionNext()
317 int i_type = var_Type( p_input, "next-chapter" );
318 vlc_value_t val; val.b_bool = VLC_TRUE;
319 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
320 "next-chapter":"next-title", val );
324 void InputManager::sectionMenu()
327 var_SetInteger( p_input, "title 0", 2 );
331 void InputManager::telexGotoPage( int page )
336 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
337 "zvbi", FIND_ANYWHERE );
340 var_SetInteger( p_vbi, "vbi-page", page );
341 vlc_object_release( p_vbi );
346 void InputManager::telexToggle( bool b_enabled )
352 telexGotoPage( i_page );
355 void InputManager::telexSetTransparency( bool b_transp )
360 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
361 "zvbi", FIND_ANYWHERE );
364 var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
365 vlc_object_release( p_vbi );
371 void InputManager::slower()
374 var_SetVoid( p_input, "rate-slower" );
377 void InputManager::faster()
380 var_SetVoid( p_input, "rate-faster" );
383 void InputManager::normalRate()
386 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
389 void InputManager::setRate( int new_rate )
392 var_SetInteger( p_input, "rate", new_rate );
395 /**********************************************************************
396 * MainInputManager implementation. Wrap an input manager and
397 * take care of updating the main playlist input
398 **********************************************************************/
399 MainInputManager * MainInputManager::instance = NULL;
401 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
405 im = new InputManager( this, p_intf );
407 var_AddCallback( THEPL, "playlist-current", ItemChanged, im );
408 var_AddCallback( THEPL, "playlist-current", InputChanged, this );
409 var_AddCallback( THEPL, "intf-change", ItemChanged, this );
410 var_AddCallback( THEPL, "activity", InputChanged, this );
411 /* src/input/input.c:2076*/
412 var_AddCallback( THEPL, "item-change", ItemChanged, this );
414 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
416 /* Warn our embedded IM about input changes */
417 CONNECT( this, inputChanged( input_thread_t * ),
418 im, setInput( input_thread_t * ) );
421 MainInputManager::~MainInputManager()
425 vlc_object_release( p_input );
426 emit inputChanged( NULL );
428 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
429 var_DelCallback( THEPL, "playlist-current", InputChanged, this );
430 var_DelCallback( THEPL, "activity", InputChanged, this );
431 var_DelCallback( THEPL, "playlist-current", ItemChanged, this );
432 var_DelCallback( THEPL, "intf-change", ItemChanged, this );
433 var_DelCallback( THEPL, "item-change", ItemChanged, this );
436 void MainInputManager::customEvent( QEvent *event )
438 int type = event->type();
439 msg_Dbg( p_intf, "New MIM Event, type: %i", type );
440 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
443 if( type == VolumeChanged_Type )
445 emit volumeChanged();
449 /* Should be ItemChanged */
450 if( VLC_OBJECT_INTF == p_intf->i_object_type )
452 vlc_mutex_lock( &p_intf->change_lock );
453 if( p_input && ( p_input->b_dead || p_input->b_die ) )
455 var_DelCallback( p_input, "state", InputChanged, this );
456 vlc_object_release( p_input );
458 emit inputChanged( NULL );
464 p_input = THEPL->p_input;
465 if( p_input && !( p_input->b_die || p_input->b_dead) )
467 vlc_object_yield( p_input );
468 var_AddCallback( p_input, "state", InputChanged, this );
469 emit inputChanged( p_input );
475 vlc_mutex_unlock( &p_intf->change_lock );
479 /* we are working as a dialogs provider */
480 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
481 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
484 p_input = p_playlist->p_input;
485 vlc_object_yield( p_input );
486 emit inputChanged( p_input );
491 void MainInputManager::stop()
493 playlist_Stop( THEPL );
496 void MainInputManager::next()
498 playlist_Next( THEPL );
501 void MainInputManager::prev()
503 playlist_Prev( THEPL );
506 void MainInputManager::togglePlayPause()
508 if( p_input == NULL )
510 playlist_Play( THEPL );
513 getIM()->togglePlayPause();
516 /* Static callbacks */
519 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
520 vlc_value_t oldval, vlc_value_t newval, void *param )
522 static int counter = 0;
523 InputManager *im = (InputManager*)param;
525 counter = counter++ % 4;
528 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
529 QApplication::postEvent( im, static_cast<QEvent*>(event) );
533 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
534 vlc_value_t oldval, vlc_value_t newval, void *param )
536 InputManager *im = (InputManager*)param;
538 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
539 QApplication::postEvent( im, static_cast<QEvent*>(event) );
543 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
544 vlc_value_t oldval, vlc_value_t newval, void *param )
546 InputManager *im = (InputManager*)param;
548 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
549 QApplication::postEvent( im, static_cast<QEvent*>(event) );
553 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
554 vlc_value_t oldval, vlc_value_t newval, void *param )
556 InputManager *im = (InputManager*)param;
558 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
559 QApplication::postEvent( im, static_cast<QEvent*>(event) );
563 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
564 vlc_value_t oldval, vlc_value_t newval, void *param )
566 InputManager *im = (InputManager*)param;
568 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
569 QApplication::postEvent( im, static_cast<QEvent*>(event) );
574 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
575 vlc_value_t n, void *param )
577 InputManager *im = (InputManager*)param;
578 im->b_has_audio = true;
582 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
583 vlc_value_t n, void *param )
585 InputManager *im = (InputManager*)param;
586 im->b_has_video = true;
591 static int InputChanged( vlc_object_t *p_this, const char *psz_var,
592 vlc_value_t oldval, vlc_value_t newval, void *param )
594 MainInputManager *im = (MainInputManager*)param;
596 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
597 QApplication::postEvent( im, static_cast<QEvent*>(event) );
601 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
602 vlc_value_t oldval, vlc_value_t newval, void *param )
604 MainInputManager *im = (MainInputManager*)param;
606 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
607 QApplication::postEvent( im, static_cast<QEvent*>(event) );