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 PLItemChanged( 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 );
93 i_old_playing_status = END_S;
97 emit positionUpdated( 0.0, 0 ,0 );
98 emit statusChanged( END_S );
99 emit nameChanged( "" );
100 emit artChanged( "" );
105 void InputManager::addCallbacks()
107 /* We don't care about:
114 - position, time, length, because they are included in intf-change
116 /* src/input/input.c:1629 */
117 var_AddCallback( p_input, "state", ItemStateChanged, this );
118 /* src/input/es-out.c:550 */
119 var_AddCallback( p_input, "audio-es", ChangeAudio, this );
120 /* src/input/es-out.c:551 */
121 var_AddCallback( p_input, "video-es", ChangeVideo, this );
122 /* src/input/input.c:1765 */
123 var_AddCallback( p_input, "rate", ItemRateChanged, this );
124 /* src/input/input.c:2003 */
125 var_AddCallback( p_input, "title", ItemTitleChanged, this );
126 /* src/input/input.c:734 for timers update*/
127 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
130 void InputManager::delCallbacks()
132 var_DelCallback( p_input, "audio-es", ChangeAudio, this );
133 var_DelCallback( p_input, "video-es", ChangeVideo, this );
134 var_DelCallback( p_input, "state", ItemStateChanged, this );
135 var_DelCallback( p_input, "rate", ItemRateChanged, this );
136 var_DelCallback( p_input, "title", ItemTitleChanged, this );
137 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
140 void InputManager::customEvent( QEvent *event )
142 int type = event->type();
143 if ( type != PositionUpdate_Type && type != ItemChanged_Type &&
144 type != ItemRateChanged_Type && type != ItemTitleChanged_Type &&
145 type != ItemStateChanged_Type )
148 msg_Dbg( p_intf, "New IM Event, type: %i", type );
150 /* Delete the input */
151 if( !p_input || p_input->b_dead || p_input->b_die )
159 case PositionUpdate_Type:
162 case ItemChanged_Type:
166 case ItemRateChanged_Type:
169 case ItemTitleChanged_Type:
172 case ItemStateChanged_Type:
178 void InputManager::UpdatePosition( void )
180 /* Update position */
181 int i_length, i_time; /* Int is enough, since we store seconds */
183 i_length = var_GetTime( p_input , "length" ) / 1000000;
184 i_time = var_GetTime( p_input , "time") / 1000000;
185 f_pos = var_GetFloat( p_input , "position" );
186 emit positionUpdated( f_pos, i_time, i_length );
189 void InputManager::UpdateTitle( void )
191 /* Update navigation status */
192 vlc_value_t val; val.i_int = 0;
193 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
194 msg_Dbg( p_intf, "updateTitle called" );
198 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
199 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
203 emit navigationChanged( 0 );
207 void InputManager::UpdateStatus( void )
209 /* Update playing status */
210 vlc_value_t val; val.i_int = 0;
211 var_Get( p_input, "state", &val );
212 if( i_old_playing_status != val.i_int )
214 i_old_playing_status = val.i_int;
215 emit statusChanged( val.i_int );
219 void InputManager::UpdateRate( void )
222 int i_new_rate = var_GetInteger( p_input, "rate");
223 if( i_new_rate != i_rate )
227 emit rateChanged( i_rate );
231 void InputManager::UpdateMeta( void )
233 /* Update text, name and nowplaying */
236 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
237 if( EMPTY_STR( psz_name ) )
240 psz_name = input_item_GetName( input_GetItem( p_input ) );
243 char *psz_nowplaying =
244 input_item_GetNowPlaying( input_GetItem( p_input ) );
245 if( !EMPTY_STR( psz_nowplaying ) )
247 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
251 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
252 if( !EMPTY_STR( psz_artist ) )
254 text.sprintf( "%s - %s", psz_artist, psz_name );
258 text.sprintf( "%s", psz_name );
263 free( psz_nowplaying );
265 if( old_name != text )
267 emit nameChanged( text );
271 /* Update Art meta */
273 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
274 url.sprintf("%s", psz_art );
278 artUrl = url.replace( "file://",QString("" ) );
279 emit artChanged( artUrl );
282 /* Update ZVBI status */
284 /* Update teletext status*/
285 emit teletextEnabled( true );/* FIXME */
289 /* User update of the slider */
290 void InputManager::sliderUpdate( float new_pos )
293 var_SetFloat( p_input, "position", new_pos );
296 void InputManager::togglePlayPause()
299 var_Get( p_input, "state", &state );
300 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
301 var_Set( p_input, "state", state );
302 emit statusChanged( state.i_int );
305 void InputManager::sectionPrev()
309 int i_type = var_Type( p_input, "next-chapter" );
310 vlc_value_t val; val.b_bool = VLC_TRUE;
311 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
312 "prev-chapter":"prev-title", val );
316 void InputManager::sectionNext()
320 int i_type = var_Type( p_input, "next-chapter" );
321 vlc_value_t val; val.b_bool = VLC_TRUE;
322 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
323 "next-chapter":"next-title", val );
327 void InputManager::sectionMenu()
330 var_SetInteger( p_input, "title 0", 2 );
334 void InputManager::telexGotoPage( int page )
339 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
340 "zvbi", FIND_ANYWHERE );
343 var_SetInteger( p_vbi, "vbi-page", page );
344 vlc_object_release( p_vbi );
349 void InputManager::telexToggle( bool b_enabled )
355 telexGotoPage( i_page );
358 void InputManager::telexSetTransparency( bool b_transp )
363 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
364 "zvbi", FIND_ANYWHERE );
367 var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
368 vlc_object_release( p_vbi );
374 void InputManager::slower()
377 var_SetVoid( p_input, "rate-slower" );
380 void InputManager::faster()
383 var_SetVoid( p_input, "rate-faster" );
386 void InputManager::normalRate()
389 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
392 void InputManager::setRate( int new_rate )
395 var_SetInteger( p_input, "rate", new_rate );
398 /**********************************************************************
399 * MainInputManager implementation. Wrap an input manager and
400 * take care of updating the main playlist input
401 **********************************************************************/
402 MainInputManager * MainInputManager::instance = NULL;
404 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
405 : QObject(NULL), p_intf( _p_intf )
408 im = new InputManager( this, p_intf );
410 // No necessary, I think
411 //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
413 var_AddCallback( THEPL, "item-change", PLItemChanged, this );
414 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
415 var_AddCallback( THEPL, "playlist-current", ItemChanged, im );
416 var_AddCallback( THEPL, "activity", PLItemChanged, this );
418 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
420 /* Warn our embedded IM about input changes */
421 CONNECT( this, inputChanged( input_thread_t * ),
422 im, setInput( input_thread_t * ) );
425 MainInputManager::~MainInputManager()
429 vlc_object_release( p_input );
430 emit inputChanged( NULL );
433 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
435 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
436 var_DelCallback( THEPL, "activity", PLItemChanged, this );
437 var_DelCallback( THEPL, "item-change", PLItemChanged, this );
439 // var_DelCallback( THEPL, "intf-change", ItemChanged, this );
442 void MainInputManager::customEvent( QEvent *event )
444 int type = event->type();
445 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
448 if( type == VolumeChanged_Type )
450 emit volumeChanged();
454 msg_Dbg( p_intf, "New MIM Event, type: %i", type );
455 /* Should be PLItemChanged */
456 if( VLC_OBJECT_INTF == p_intf->i_object_type )
458 vlc_mutex_lock( &p_intf->change_lock );
459 if( p_input && ( p_input->b_dead || p_input->b_die ) )
462 emit inputChanged( NULL );
468 p_input = THEPL->p_input;
469 if( p_input && !( p_input->b_die || p_input->b_dead) )
471 emit inputChanged( p_input );
477 vlc_mutex_unlock( &p_intf->change_lock );
481 /* we are working as a dialogs provider */
482 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
483 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
486 p_input = p_playlist->p_input;
487 emit inputChanged( p_input );
492 /* Playlist Control functions */
493 void MainInputManager::stop()
495 playlist_Stop( THEPL );
498 void MainInputManager::next()
500 playlist_Next( THEPL );
503 void MainInputManager::prev()
505 playlist_Prev( THEPL );
508 void MainInputManager::togglePlayPause()
510 if( p_input == NULL )
512 playlist_Play( THEPL );
515 getIM()->togglePlayPause();
518 /* Static callbacks */
521 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
522 vlc_value_t oldval, vlc_value_t newval, void *param )
524 static int counter = 0;
525 InputManager *im = (InputManager*)param;
527 counter = counter++ % 4;
530 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
531 QApplication::postEvent( im, static_cast<QEvent*>(event) );
535 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
536 vlc_value_t oldval, vlc_value_t newval, void *param )
538 InputManager *im = (InputManager*)param;
540 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
541 QApplication::postEvent( im, static_cast<QEvent*>(event) );
545 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
546 vlc_value_t oldval, vlc_value_t newval, void *param )
548 InputManager *im = (InputManager*)param;
550 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
551 QApplication::postEvent( im, static_cast<QEvent*>(event) );
555 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
556 vlc_value_t oldval, vlc_value_t newval, void *param )
558 InputManager *im = (InputManager*)param;
560 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
561 QApplication::postEvent( im, static_cast<QEvent*>(event) );
565 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
566 vlc_value_t oldval, vlc_value_t newval, void *param )
568 InputManager *im = (InputManager*)param;
570 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
571 QApplication::postEvent( im, static_cast<QEvent*>(event) );
576 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
577 vlc_value_t n, void *param )
579 InputManager *im = (InputManager*)param;
580 im->b_has_audio = true;
584 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
585 vlc_value_t n, void *param )
587 InputManager *im = (InputManager*)param;
588 im->b_has_video = true;
593 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
594 vlc_value_t oldval, vlc_value_t newval, void *param )
596 MainInputManager *mim = (MainInputManager*)param;
598 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
599 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
603 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
604 vlc_value_t oldval, vlc_value_t newval, void *param )
606 MainInputManager *mim = (MainInputManager*)param;
608 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
609 QApplication::postEvent( mim, static_cast<QEvent*>(event) );