1 /*****************************************************************************
2 * input_manager.cpp : Manage an input and interact with its GUI elements
3 ****************************************************************************
4 * Copyright (C) 2006-2008 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Ilkka Ollakka <ileoo@videolan.org>
9 * Jean-Baptiste <jb@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
30 #include "input_manager.hpp"
31 #include "dialogs_provider.hpp"
33 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
34 vlc_value_t n, void *param );
35 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
36 vlc_value_t n, void *param );
37 static int ItemChanged( vlc_object_t *, const char *,
38 vlc_value_t, vlc_value_t, void * );
39 static int PLItemChanged( vlc_object_t *, const char *,
40 vlc_value_t, vlc_value_t, void * );
41 static int InterfaceChanged( vlc_object_t *, const char *,
42 vlc_value_t, vlc_value_t, void * );
43 static int ItemStateChanged( vlc_object_t *, const char *,
44 vlc_value_t, vlc_value_t, void * );
45 static int ItemRateChanged( vlc_object_t *, const char *,
46 vlc_value_t, vlc_value_t, void * );
47 static int ItemTitleChanged( vlc_object_t *, const char *,
48 vlc_value_t, vlc_value_t, void * );
49 static int VolumeChanged( vlc_object_t *, const char *,
50 vlc_value_t, vlc_value_t, void * );
52 /**********************************************************************
53 * InputManager implementation
54 **********************************************************************
55 * The Input Manager can be the main one around the playlist
56 * But can also be used for VLM dialog or similar
57 **********************************************************************/
59 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
60 QObject( parent ), p_intf( _p_intf )
62 i_old_playing_status = END_S;
68 InputManager::~InputManager()
73 /* Define the Input used.
74 Add the callbacks on input
75 p_input is yield once here */
76 void InputManager::setInput( input_thread_t *_p_input )
80 b_had_audio = b_had_video = b_has_audio = b_has_video = false;
83 vlc_object_yield( p_input );
85 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
86 b_has_video = val.i_int > 0;
87 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
88 b_has_audio = val.i_int > 0;
89 emit statusChanged( PLAYING_S );
94 /* delete Input if it ever existed.
95 Delete the callbacls on input
96 p_input is released once here */
97 void InputManager::delInput()
102 vlc_object_release( p_input );
103 i_old_playing_status = END_S;
107 emit positionUpdated( 0.0, 0 ,0 );
108 emit statusChanged( END_S );
109 emit nameChanged( "" );
110 emit artChanged( "" );
115 /* Add the callbacks on Input. Self explanatory */
116 void InputManager::addCallbacks()
118 /* We don't care about:
125 - position, time, length, because they are included in intf-change
127 /* src/input/input.c:1629 */
128 var_AddCallback( p_input, "state", ItemStateChanged, this );
129 /* src/input/es-out.c:550 */
130 var_AddCallback( p_input, "audio-es", ChangeAudio, this );
131 /* src/input/es-out.c:551 */
132 var_AddCallback( p_input, "video-es", ChangeVideo, this );
133 /* src/input/input.c:1765 */
134 var_AddCallback( p_input, "rate", ItemRateChanged, this );
135 /* src/input/input.c:2003 */
136 var_AddCallback( p_input, "title", ItemTitleChanged, this );
137 /* src/input/input.c:734 for timers update*/
138 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
141 /* Delete the callbacks on Input. Self explanatory */
142 void InputManager::delCallbacks()
144 var_DelCallback( p_input, "audio-es", ChangeAudio, this );
145 var_DelCallback( p_input, "video-es", ChangeVideo, this );
146 var_DelCallback( p_input, "state", ItemStateChanged, this );
147 var_DelCallback( p_input, "rate", ItemRateChanged, this );
148 var_DelCallback( p_input, "title", ItemTitleChanged, this );
149 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
152 /* Convert the event from the callbacks in actions */
153 void InputManager::customEvent( QEvent *event )
155 int type = event->type();
156 if ( type != PositionUpdate_Type &&
157 type != ItemChanged_Type &&
158 type != ItemRateChanged_Type &&
159 type != ItemTitleChanged_Type &&
160 type != ItemStateChanged_Type )
163 /* Delete the input */
164 if( !p_input || p_input->b_dead || p_input->b_die )
173 case PositionUpdate_Type:
176 case ItemChanged_Type:
180 case ItemRateChanged_Type:
183 case ItemTitleChanged_Type:
186 case ItemStateChanged_Type:
192 void InputManager::UpdatePosition( void )
194 /* Update position */
195 int i_length, i_time; /* Int is enough, since we store seconds */
197 i_length = var_GetTime( p_input , "length" ) / 1000000;
198 i_time = var_GetTime( p_input , "time") / 1000000;
199 f_pos = var_GetFloat( p_input , "position" );
200 emit positionUpdated( f_pos, i_time, i_length );
203 void InputManager::UpdateTitle( void )
205 /* Update navigation status */
206 vlc_value_t val; val.i_int = 0;
207 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
208 msg_Dbg( p_intf, "updateTitle called" );
212 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
213 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
217 emit navigationChanged( 0 );
221 void InputManager::UpdateStatus( void )
223 /* Update playing status */
224 vlc_value_t val; val.i_int = 0;
225 var_Get( p_input, "state", &val );
226 if( i_old_playing_status != val.i_int )
228 i_old_playing_status = val.i_int;
229 emit statusChanged( val.i_int );
233 void InputManager::UpdateRate( void )
236 int i_new_rate = var_GetInteger( p_input, "rate");
237 if( i_new_rate != i_rate )
241 emit rateChanged( i_rate );
245 void InputManager::UpdateMeta( void )
247 /* Update text, name and nowplaying */
250 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
251 if( EMPTY_STR( psz_name ) )
254 psz_name = input_item_GetName( input_GetItem( p_input ) );
257 char *psz_nowplaying =
258 input_item_GetNowPlaying( input_GetItem( p_input ) );
259 if( !EMPTY_STR( psz_nowplaying ) )
261 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
265 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
266 if( !EMPTY_STR( psz_artist ) )
268 text.sprintf( "%s - %s", psz_artist, psz_name );
272 text.sprintf( "%s", psz_name );
277 free( psz_nowplaying );
279 if( old_name != text )
281 emit nameChanged( text );
285 /* Update Art meta */
287 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
288 url.sprintf("%s", psz_art );
292 artUrl = url.replace( "file://",QString("" ) );
293 emit artChanged( artUrl );
296 /* Update ZVBI status */
298 /* Update teletext status*/
299 emit teletextEnabled( true );/* FIXME */
303 /* User update of the slider */
304 void InputManager::sliderUpdate( float new_pos )
307 var_SetFloat( p_input, "position", new_pos );
310 /* User togglePlayPause */
311 void InputManager::togglePlayPause()
314 var_Get( p_input, "state", &state );
315 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
316 var_Set( p_input, "state", state );
317 emit statusChanged( state.i_int );
320 void InputManager::sectionPrev()
324 int i_type = var_Type( p_input, "next-chapter" );
325 vlc_value_t val; val.b_bool = VLC_TRUE;
326 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
327 "prev-chapter":"prev-title", val );
331 void InputManager::sectionNext()
335 int i_type = var_Type( p_input, "next-chapter" );
336 vlc_value_t val; val.b_bool = VLC_TRUE;
337 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
338 "next-chapter":"next-title", val );
342 void InputManager::sectionMenu()
345 var_SetInteger( p_input, "title 0", 2 );
349 void InputManager::telexGotoPage( int page )
354 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
355 "zvbi", FIND_ANYWHERE );
358 var_SetInteger( p_vbi, "vbi-page", page );
359 vlc_object_release( p_vbi );
364 void InputManager::telexToggle( bool b_enabled )
370 telexGotoPage( i_page );
373 void InputManager::telexSetTransparency( bool b_transp )
378 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
379 "zvbi", FIND_ANYWHERE );
382 var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
383 vlc_object_release( p_vbi );
389 void InputManager::slower()
392 var_SetVoid( p_input, "rate-slower" );
395 void InputManager::faster()
398 var_SetVoid( p_input, "rate-faster" );
401 void InputManager::normalRate()
404 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
407 void InputManager::setRate( int new_rate )
410 var_SetInteger( p_input, "rate", new_rate );
413 /**********************************************************************
414 * MainInputManager implementation. Wrap an input manager and
415 * take care of updating the main playlist input.
416 * Used in the main playlist Dialog
417 **********************************************************************/
418 MainInputManager * MainInputManager::instance = NULL;
420 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
421 : QObject(NULL), p_intf( _p_intf )
424 im = new InputManager( this, p_intf );
426 var_AddCallback( THEPL, "item-change", PLItemChanged, this );
427 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
428 var_AddCallback( THEPL, "activity", PLItemChanged, this );
430 var_AddCallback( THEPL, "playlist-current", ItemChanged, im );
432 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
434 // No necessary, I think TODO REMOVE ME at the end
435 //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
437 /* Warn our embedded IM about input changes */
438 CONNECT( this, inputChanged( input_thread_t * ),
439 im, setInput( input_thread_t * ) );
442 MainInputManager::~MainInputManager()
446 emit inputChanged( NULL );
449 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
451 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
452 var_DelCallback( THEPL, "activity", PLItemChanged, this );
453 var_DelCallback( THEPL, "item-change", PLItemChanged, this );
455 var_DelCallback( THEPL, "playlist-current", ItemChanged, im );
458 void MainInputManager::customEvent( QEvent *event )
460 int type = event->type();
461 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
464 if( type == VolumeChanged_Type )
466 emit volumeChanged();
470 /* Should be PLItemChanged Event */
471 if( VLC_OBJECT_INTF == p_intf->i_object_type )
473 vlc_mutex_lock( &p_intf->change_lock );
474 if( p_input && ( p_input->b_dead || p_input->b_die ) )
477 emit inputChanged( NULL );
483 p_input = THEPL->p_input;
484 if( p_input && !( p_input->b_die || p_input->b_dead) )
486 emit inputChanged( p_input );
492 vlc_mutex_unlock( &p_intf->change_lock );
496 /* we are working as a dialogs provider */
497 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
498 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
501 p_input = p_playlist->p_input;
502 emit inputChanged( p_input );
507 /* Playlist Control functions */
508 void MainInputManager::stop()
510 playlist_Stop( THEPL );
513 void MainInputManager::next()
515 playlist_Next( THEPL );
518 void MainInputManager::prev()
520 playlist_Prev( THEPL );
523 void MainInputManager::togglePlayPause()
525 if( p_input == NULL )
527 playlist_Play( THEPL );
530 getIM()->togglePlayPause();
533 /* Static callbacks */
536 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
537 vlc_value_t oldval, vlc_value_t newval, void *param )
539 static int counter = 0;
540 InputManager *im = (InputManager*)param;
542 counter = counter++ % 4;
545 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
546 QApplication::postEvent( im, static_cast<QEvent*>(event) );
550 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
551 vlc_value_t oldval, vlc_value_t newval, void *param )
553 InputManager *im = (InputManager*)param;
555 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
556 QApplication::postEvent( im, static_cast<QEvent*>(event) );
560 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
561 vlc_value_t oldval, vlc_value_t newval, void *param )
563 InputManager *im = (InputManager*)param;
565 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
566 QApplication::postEvent( im, static_cast<QEvent*>(event) );
570 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
571 vlc_value_t oldval, vlc_value_t newval, void *param )
573 InputManager *im = (InputManager*)param;
575 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
576 QApplication::postEvent( im, static_cast<QEvent*>(event) );
580 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
581 vlc_value_t oldval, vlc_value_t newval, void *param )
583 InputManager *im = (InputManager*)param;
585 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
586 QApplication::postEvent( im, static_cast<QEvent*>(event) );
591 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
592 vlc_value_t n, void *param )
594 InputManager *im = (InputManager*)param;
595 im->b_has_audio = true;
599 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
600 vlc_value_t n, void *param )
602 InputManager *im = (InputManager*)param;
603 im->b_has_video = true;
608 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
609 vlc_value_t oldval, vlc_value_t newval, void *param )
611 MainInputManager *mim = (MainInputManager*)param;
613 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
614 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
618 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
619 vlc_value_t oldval, vlc_value_t newval, void *param )
621 MainInputManager *mim = (MainInputManager*)param;
623 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
624 QApplication::postEvent( mim, static_cast<QEvent*>(event) );