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 **********************************************************************
54 * The Input Manager can be the main one around the playlist
55 * But can also be used for VLM dialog or similar
56 **********************************************************************/
58 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
59 QObject( parent ), p_intf( _p_intf )
61 i_old_playing_status = END_S;
67 InputManager::~InputManager()
72 /* Define the Input used.
73 Add the callbacks on input
74 p_input is yield once here */
75 void InputManager::setInput( input_thread_t *_p_input )
79 b_had_audio = b_had_video = b_has_audio = b_has_video = false;
82 vlc_object_yield( p_input );
84 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
85 b_has_video = val.i_int > 0;
86 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
87 b_has_audio = val.i_int > 0;
88 emit statusChanged( PLAYING_S );
93 /* delete Input if it ever existed.
94 Delete the callbacls on input
95 p_input is released once here */
96 void InputManager::delInput()
101 vlc_object_release( p_input );
102 i_old_playing_status = END_S;
106 emit positionUpdated( 0.0, 0 ,0 );
107 emit statusChanged( END_S );
108 emit nameChanged( "" );
109 emit artChanged( "" );
114 /* Add the callbacks on Input. Self explanatory */
115 void InputManager::addCallbacks()
117 /* We don't care about:
124 - position, time, length, because they are included in intf-change
126 /* src/input/input.c:1629 */
127 var_AddCallback( p_input, "state", ItemStateChanged, this );
128 /* src/input/es-out.c:550 */
129 var_AddCallback( p_input, "audio-es", ChangeAudio, this );
130 /* src/input/es-out.c:551 */
131 var_AddCallback( p_input, "video-es", ChangeVideo, this );
132 /* src/input/input.c:1765 */
133 var_AddCallback( p_input, "rate", ItemRateChanged, this );
134 /* src/input/input.c:2003 */
135 var_AddCallback( p_input, "title", ItemTitleChanged, this );
136 /* src/input/input.c:734 for timers update*/
137 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
140 /* Delete the callbacks on Input. Self explanatory */
141 void InputManager::delCallbacks()
143 var_DelCallback( p_input, "audio-es", ChangeAudio, this );
144 var_DelCallback( p_input, "video-es", ChangeVideo, this );
145 var_DelCallback( p_input, "state", ItemStateChanged, this );
146 var_DelCallback( p_input, "rate", ItemRateChanged, this );
147 var_DelCallback( p_input, "title", ItemTitleChanged, this );
148 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
151 /* Convert the event from the callbacks in actions */
152 void InputManager::customEvent( QEvent *event )
154 int type = event->type();
155 if ( type != PositionUpdate_Type &&
156 type != ItemChanged_Type &&
157 type != ItemRateChanged_Type &&
158 type != ItemTitleChanged_Type &&
159 type != ItemStateChanged_Type )
162 /* Delete the input */
163 if( !p_input || p_input->b_dead || p_input->b_die )
172 case PositionUpdate_Type:
175 case ItemChanged_Type:
179 case ItemRateChanged_Type:
182 case ItemTitleChanged_Type:
185 case ItemStateChanged_Type:
191 void InputManager::UpdatePosition( void )
193 /* Update position */
194 int i_length, i_time; /* Int is enough, since we store seconds */
196 i_length = var_GetTime( p_input , "length" ) / 1000000;
197 i_time = var_GetTime( p_input , "time") / 1000000;
198 f_pos = var_GetFloat( p_input , "position" );
199 emit positionUpdated( f_pos, i_time, i_length );
202 void InputManager::UpdateTitle( void )
204 /* Update navigation status */
205 vlc_value_t val; val.i_int = 0;
206 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
207 msg_Dbg( p_intf, "updateTitle called" );
211 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
212 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
216 emit navigationChanged( 0 );
220 void InputManager::UpdateStatus( void )
222 /* Update playing status */
223 vlc_value_t val; val.i_int = 0;
224 var_Get( p_input, "state", &val );
225 if( i_old_playing_status != val.i_int )
227 i_old_playing_status = val.i_int;
228 emit statusChanged( val.i_int );
232 void InputManager::UpdateRate( void )
235 int i_new_rate = var_GetInteger( p_input, "rate");
236 if( i_new_rate != i_rate )
240 emit rateChanged( i_rate );
244 void InputManager::UpdateMeta( void )
246 /* Update text, name and nowplaying */
249 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
250 if( EMPTY_STR( psz_name ) )
253 psz_name = input_item_GetName( input_GetItem( p_input ) );
256 char *psz_nowplaying =
257 input_item_GetNowPlaying( input_GetItem( p_input ) );
258 if( !EMPTY_STR( psz_nowplaying ) )
260 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
264 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
265 if( !EMPTY_STR( psz_artist ) )
267 text.sprintf( "%s - %s", psz_artist, psz_name );
271 text.sprintf( "%s", psz_name );
276 free( psz_nowplaying );
278 if( old_name != text )
280 emit nameChanged( text );
284 /* Update Art meta */
286 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
287 url.sprintf("%s", psz_art );
291 artUrl = url.replace( "file://",QString("" ) );
292 emit artChanged( artUrl );
295 /* Update ZVBI status */
297 /* Update teletext status*/
298 emit teletextEnabled( true );/* FIXME */
302 /* User update of the slider */
303 void InputManager::sliderUpdate( float new_pos )
306 var_SetFloat( p_input, "position", new_pos );
309 /* User togglePlayPause */
310 void InputManager::togglePlayPause()
313 var_Get( p_input, "state", &state );
314 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
315 var_Set( p_input, "state", state );
316 emit statusChanged( state.i_int );
319 void InputManager::sectionPrev()
323 int i_type = var_Type( p_input, "next-chapter" );
324 vlc_value_t val; val.b_bool = VLC_TRUE;
325 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
326 "prev-chapter":"prev-title", val );
330 void InputManager::sectionNext()
334 int i_type = var_Type( p_input, "next-chapter" );
335 vlc_value_t val; val.b_bool = VLC_TRUE;
336 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
337 "next-chapter":"next-title", val );
341 void InputManager::sectionMenu()
344 var_SetInteger( p_input, "title 0", 2 );
348 void InputManager::telexGotoPage( int page )
353 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
354 "zvbi", FIND_ANYWHERE );
357 var_SetInteger( p_vbi, "vbi-page", page );
358 vlc_object_release( p_vbi );
363 void InputManager::telexToggle( bool b_enabled )
369 telexGotoPage( i_page );
372 void InputManager::telexSetTransparency( bool b_transp )
377 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
378 "zvbi", FIND_ANYWHERE );
381 var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
382 vlc_object_release( p_vbi );
388 void InputManager::slower()
391 var_SetVoid( p_input, "rate-slower" );
394 void InputManager::faster()
397 var_SetVoid( p_input, "rate-faster" );
400 void InputManager::normalRate()
403 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
406 void InputManager::setRate( int new_rate )
409 var_SetInteger( p_input, "rate", new_rate );
412 /**********************************************************************
413 * MainInputManager implementation. Wrap an input manager and
414 * take care of updating the main playlist input.
415 * Used in the main playlist Dialog
416 **********************************************************************/
417 MainInputManager * MainInputManager::instance = NULL;
419 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
420 : QObject(NULL), p_intf( _p_intf )
423 im = new InputManager( this, p_intf );
425 var_AddCallback( THEPL, "item-change", PLItemChanged, this );
426 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
427 var_AddCallback( THEPL, "activity", PLItemChanged, this );
429 var_AddCallback( THEPL, "playlist-current", ItemChanged, im );
431 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
433 // No necessary, I think TODO REMOVE ME at the end
434 //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
436 /* Warn our embedded IM about input changes */
437 CONNECT( this, inputChanged( input_thread_t * ),
438 im, setInput( input_thread_t * ) );
441 MainInputManager::~MainInputManager()
445 emit inputChanged( NULL );
448 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
450 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
451 var_DelCallback( THEPL, "activity", PLItemChanged, this );
452 var_DelCallback( THEPL, "item-change", PLItemChanged, this );
454 var_DelCallback( THEPL, "playlist-current", ItemChanged, im );
457 void MainInputManager::customEvent( QEvent *event )
459 int type = event->type();
460 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
463 if( type == VolumeChanged_Type )
465 emit volumeChanged();
469 /* Should be PLItemChanged Event */
470 if( VLC_OBJECT_INTF == p_intf->i_object_type )
472 vlc_mutex_lock( &p_intf->change_lock );
473 if( p_input && ( p_input->b_dead || p_input->b_die ) )
476 emit inputChanged( NULL );
482 p_input = THEPL->p_input;
483 if( p_input && !( p_input->b_die || p_input->b_dead) )
485 emit inputChanged( p_input );
491 vlc_mutex_unlock( &p_intf->change_lock );
495 /* we are working as a dialogs provider */
496 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
497 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
500 p_input = p_playlist->p_input;
501 emit inputChanged( p_input );
506 /* Playlist Control functions */
507 void MainInputManager::stop()
509 playlist_Stop( THEPL );
512 void MainInputManager::next()
514 playlist_Next( THEPL );
517 void MainInputManager::prev()
519 playlist_Prev( THEPL );
522 void MainInputManager::togglePlayPause()
524 if( p_input == NULL )
526 playlist_Play( THEPL );
529 getIM()->togglePlayPause();
532 /* Static callbacks */
535 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
536 vlc_value_t oldval, vlc_value_t newval, void *param )
538 static int counter = 0;
539 InputManager *im = (InputManager*)param;
541 counter = counter++ % 4;
544 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
545 QApplication::postEvent( im, static_cast<QEvent*>(event) );
549 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
550 vlc_value_t oldval, vlc_value_t newval, void *param )
552 InputManager *im = (InputManager*)param;
554 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
555 QApplication::postEvent( im, static_cast<QEvent*>(event) );
559 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
560 vlc_value_t oldval, vlc_value_t newval, void *param )
562 InputManager *im = (InputManager*)param;
564 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
565 QApplication::postEvent( im, static_cast<QEvent*>(event) );
569 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
570 vlc_value_t oldval, vlc_value_t newval, void *param )
572 InputManager *im = (InputManager*)param;
574 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
575 QApplication::postEvent( im, static_cast<QEvent*>(event) );
579 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
580 vlc_value_t oldval, vlc_value_t newval, void *param )
582 InputManager *im = (InputManager*)param;
584 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
585 QApplication::postEvent( im, static_cast<QEvent*>(event) );
590 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
591 vlc_value_t n, void *param )
593 InputManager *im = (InputManager*)param;
594 im->b_has_audio = true;
598 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
599 vlc_value_t n, void *param )
601 InputManager *im = (InputManager*)param;
602 im->b_has_video = true;
607 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
608 vlc_value_t oldval, vlc_value_t newval, void *param )
610 MainInputManager *mim = (MainInputManager*)param;
612 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
613 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
617 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
618 vlc_value_t oldval, vlc_value_t newval, void *param )
620 MainInputManager *mim = (MainInputManager*)param;
622 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
623 QApplication::postEvent( mim, static_cast<QEvent*>(event) );