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 ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
34 vlc_value_t n, void *param );
35 static int ItemChanged( vlc_object_t *, const char *,
36 vlc_value_t, vlc_value_t, void * );
37 static int PLItemChanged( vlc_object_t *, const char *,
38 vlc_value_t, vlc_value_t, void * );
39 static int InterfaceChanged( vlc_object_t *, const char *,
40 vlc_value_t, vlc_value_t, void * );
41 static int ItemStateChanged( vlc_object_t *, const char *,
42 vlc_value_t, vlc_value_t, void * );
43 static int ItemRateChanged( vlc_object_t *, const char *,
44 vlc_value_t, vlc_value_t, void * );
45 static int ItemTitleChanged( vlc_object_t *, const char *,
46 vlc_value_t, vlc_value_t, void * );
47 static int VolumeChanged( vlc_object_t *, const char *,
48 vlc_value_t, vlc_value_t, void * );
50 /**********************************************************************
51 * InputManager implementation
52 **********************************************************************
53 * The Input Manager can be the main one around the playlist
54 * But can also be used for VLM dialog or similar
55 **********************************************************************/
57 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
58 QObject( parent ), p_intf( _p_intf )
60 i_old_playing_status = END_S;
66 b_transparentTelextext = false;
69 InputManager::~InputManager()
74 /* Define the Input used.
75 Add the callbacks on input
76 p_input is yield once here */
77 void InputManager::setInput( input_thread_t *_p_input )
81 if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
83 vlc_object_yield( p_input );
84 emit statusChanged( PLAYING_S );
90 i_input_id = input_GetItem( p_input )->i_id;
96 emit rateChanged( INPUT_RATE_DEFAULT );
101 /* delete Input if it ever existed.
102 Delete the callbacls on input
103 p_input is released once here */
104 void InputManager::delInput()
109 i_old_playing_status = END_S;
113 emit positionUpdated( 0.0, 0 ,0 );
114 emit statusChanged( END_S );
115 emit nameChanged( "" );
116 emit artChanged( "" );
117 emit rateChanged( INPUT_RATE_DEFAULT );
118 vlc_object_release( p_input );
124 /* Add the callbacks on Input. Self explanatory */
125 void InputManager::addCallbacks()
127 /* We don't care about:
133 - position, time, length, because they are included in intf-change
135 /* src/input/input.c:1629 */
136 var_AddCallback( p_input, "state", ItemStateChanged, this );
137 /* src/input/es-out.c:552 */
138 var_AddCallback( p_input, "spu-es", ChangeSPU, this );
139 /* src/input/input.c:1765 */
140 var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
141 /* src/input/input.c:2003 */
142 var_AddCallback( p_input, "title", ItemTitleChanged, this );
143 /* src/input/input.c:734 for timers update*/
144 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
147 /* Delete the callbacks on Input. Self explanatory */
148 void InputManager::delCallbacks()
150 var_DelCallback( p_input, "spu-es", ChangeSPU, this );
151 var_DelCallback( p_input, "state", ItemStateChanged, this );
152 var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
153 var_DelCallback( p_input, "title", ItemTitleChanged, this );
154 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
157 /* Convert the event from the callbacks in actions */
158 void InputManager::customEvent( QEvent *event )
160 int type = event->type();
161 IMEvent *ple = static_cast<IMEvent *>(event);
163 if ( type != PositionUpdate_Type &&
164 type != ItemChanged_Type &&
165 type != ItemRateChanged_Type &&
166 type != ItemTitleChanged_Type &&
167 type != ItemSpuChanged_Type &&
168 type != ItemStateChanged_Type )
171 if( !hasInput() ) return;
173 if( ( type != PositionUpdate_Type &&
174 type != ItemRateChanged_Type &&
175 type != ItemSpuChanged_Type &&
176 type != ItemStateChanged_Type
178 && ( i_input_id != ple->i_id ) )
181 if( type != PositionUpdate_Type )
182 msg_Dbg( p_intf, "New Event: type %i", type );
187 case PositionUpdate_Type:
190 case ItemChanged_Type:
195 case ItemStateChanged_Type:
200 case ItemTitleChanged_Type:
204 case ItemRateChanged_Type:
207 case ItemSpuChanged_Type:
213 void InputManager::UpdatePosition()
215 /* Update position */
216 int i_length, i_time; /* Int is enough, since we store seconds */
218 i_length = var_GetTime( p_input , "length" ) / 1000000;
219 i_time = var_GetTime( p_input , "time") / 1000000;
220 f_pos = var_GetFloat( p_input , "position" );
221 emit positionUpdated( f_pos, i_time, i_length );
224 void InputManager::UpdateNavigation()
226 /* Update navigation status */
227 vlc_value_t val; val.i_int = 0;
228 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
232 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
233 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
237 emit navigationChanged( 0 );
241 void InputManager::UpdateStatus()
243 /* Update playing status */
244 vlc_value_t val; val.i_int = 0;
245 var_Get( p_input, "state", &val );
246 if( i_old_playing_status != val.i_int )
248 i_old_playing_status = val.i_int;
249 emit statusChanged( val.i_int );
253 void InputManager::UpdateRate()
256 int i_new_rate = var_GetInteger( p_input, "rate");
257 if( i_new_rate != i_rate )
261 emit rateChanged( i_rate );
265 void InputManager::UpdateMeta()
267 /* Update text, name and nowplaying */
270 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
271 if( EMPTY_STR( psz_name ) )
274 psz_name = input_item_GetName( input_GetItem( p_input ) );
277 char *psz_nowplaying =
278 input_item_GetNowPlaying( input_GetItem( p_input ) );
279 if( !EMPTY_STR( psz_nowplaying ) )
281 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
285 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
286 if( !EMPTY_STR( psz_artist ) )
288 text.sprintf( "%s - %s", psz_artist, psz_name );
292 text.sprintf( "%s", psz_name );
297 free( psz_nowplaying );
299 if( old_name != text )
301 emit nameChanged( text );
306 bool InputManager::hasAudio()
311 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
312 return val.i_int > 0;
317 bool InputManager::hasVideo()
322 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
323 return val.i_int > 0;
329 void InputManager::UpdateSPU()
334 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
335 telexToggle( val.i_int > 0 );
339 telexToggle( false );
343 void InputManager::UpdateArt()
345 /* Update Art meta */
347 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
348 url.sprintf("%s", psz_art );
352 artUrl = url.replace( "file://",QString("" ) );
353 /* Taglib seems to define a attachment://, It won't work yet */
354 artUrl = url.replace( "attachment://",QString("" ) );
355 emit artChanged( artUrl );
356 msg_Dbg( p_intf, "Art: %s", qtu( artUrl ) );
360 /* User update of the slider */
361 void InputManager::sliderUpdate( float new_pos )
364 var_SetFloat( p_input, "position", new_pos );
367 /* User togglePlayPause */
368 void InputManager::togglePlayPause()
371 var_Get( p_input, "state", &state );
372 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
373 var_Set( p_input, "state", state );
374 emit statusChanged( state.i_int );
377 void InputManager::sectionPrev()
381 int i_type = var_Type( p_input, "next-chapter" );
382 vlc_value_t val; val.b_bool = true;
383 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
384 "prev-chapter":"prev-title", val );
388 void InputManager::sectionNext()
392 int i_type = var_Type( p_input, "next-chapter" );
393 vlc_value_t val; val.b_bool = true;
394 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
395 "next-chapter":"next-title", val );
399 void InputManager::sectionMenu()
403 vlc_value_t val; val.i_int = 2;
404 var_Set( p_input, "title 0", val );
408 void InputManager::telexGotoPage( int page )
413 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
414 "zvbi", FIND_ANYWHERE );
417 var_SetInteger( p_vbi, "vbi-page", page );
418 vlc_object_release( p_vbi );
421 emit setNewTelexPage( page );
424 void InputManager::telexToggle( bool b_enabled )
432 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
433 b_enabled = (val.i_int > 0);
434 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
435 "zvbi", FIND_ANYWHERE );
438 i_page = var_GetInteger( p_vbi, "vbi-page" );
439 vlc_object_release( p_vbi );
440 i_page = b_enabled ? i_page : 0;
441 telexGotoPage( i_page );
444 emit teletextEnabled( b_enabled );
447 void InputManager::telexToggleButtons()
449 emit toggleTelexButtons();
452 void InputManager::telexSetTransparency()
457 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
458 "zvbi", FIND_ANYWHERE );
461 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
462 b_transparentTelextext = !b_transparentTelextext;
463 vlc_object_release( p_vbi );
466 emit toggleTelexTransparency();
469 void InputManager::slower()
472 var_SetVoid( p_input, "rate-slower" );
475 void InputManager::faster()
478 var_SetVoid( p_input, "rate-faster" );
481 void InputManager::normalRate()
484 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
487 void InputManager::setRate( int new_rate )
490 var_SetInteger( p_input, "rate", new_rate );
493 /**********************************************************************
494 * MainInputManager implementation. Wrap an input manager and
495 * take care of updating the main playlist input.
496 * Used in the main playlist Dialog
497 **********************************************************************/
498 MainInputManager * MainInputManager::instance = NULL;
500 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
501 : QObject(NULL), p_intf( _p_intf )
504 im = new InputManager( this, p_intf );
506 // var_AddCallback( THEPL, "item-change", PLItemChanged, this );
507 var_AddCallback( THEPL, "item-change", ItemChanged, im );
508 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
509 var_AddCallback( THEPL, "activity", PLItemChanged, this );
511 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
513 // No necessary, I think TODO REMOVE ME at the end
514 //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
516 /* Warn our embedded IM about input changes */
517 CONNECT( this, inputChanged( input_thread_t * ),
518 im, setInput( input_thread_t * ) );
520 /* emit check if playlist has allready started playing */
522 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
523 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
524 QApplication::postEvent( this, static_cast<QEvent*>(event) );
528 MainInputManager::~MainInputManager()
532 var_DelCallback( p_input, "state", PLItemChanged, this );
533 vlc_object_release( p_input );
534 emit inputChanged( NULL );
537 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
539 var_DelCallback( THEPL, "activity", PLItemChanged, this );
540 var_DelCallback( THEPL, "item-change", ItemChanged, im );
541 // var_DelCallback( THEPL, "item-change", PLItemChanged, this );
543 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
546 void MainInputManager::customEvent( QEvent *event )
548 int type = event->type();
549 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
552 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
553 if( type == VolumeChanged_Type )
555 emit volumeChanged();
559 /* Should be PLItemChanged Event */
560 if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
562 vlc_mutex_lock( &p_intf->change_lock );
563 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
565 var_DelCallback( p_input, "state", PLItemChanged, this );
566 vlc_object_release( p_input );
567 emit inputChanged( NULL );
569 vlc_mutex_unlock( &p_intf->change_lock );
576 p_input = THEPL->p_input;
577 if( p_input && !( !vlc_object_alive (p_input) || p_input->b_dead) )
579 vlc_object_yield( p_input );
580 var_AddCallback( p_input, "state", PLItemChanged, this );
581 emit inputChanged( p_input );
587 vlc_mutex_unlock( &p_intf->change_lock );
591 /* we are working as a dialogs provider */
592 playlist_t *p_playlist = pl_Yield( p_intf );
593 p_input = playlist_CurrentInput( p_playlist );
594 emit inputChanged( p_input );
595 vlc_object_release( p_input );
599 /* Playlist Control functions */
600 void MainInputManager::stop()
602 playlist_Stop( THEPL );
605 void MainInputManager::next()
607 playlist_Next( THEPL );
610 void MainInputManager::prev()
612 playlist_Prev( THEPL );
615 void MainInputManager::togglePlayPause()
617 if( p_input == NULL )
619 playlist_Play( THEPL );
622 getIM()->togglePlayPause();
625 bool MainInputManager::teletextState()
632 p_vbi = (vlc_object_t *) vlc_object_find_name( getInput(),
633 "zvbi", FIND_ANYWHERE );
636 vlc_object_release( p_vbi );
639 var_Change( getInput(), "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
640 return (val.i_int > 0);
645 /* Static callbacks */
648 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
649 vlc_value_t oldval, vlc_value_t newval, void *param )
651 static int counter = 0;
652 InputManager *im = (InputManager*)param;
654 counter = ++counter % 4;
657 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
658 QApplication::postEvent( im, static_cast<QEvent*>(event) );
662 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
663 vlc_value_t oldval, vlc_value_t newval, void *param )
665 InputManager *im = (InputManager*)param;
667 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
668 QApplication::postEvent( im, static_cast<QEvent*>(event) );
672 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
673 vlc_value_t oldval, vlc_value_t newval, void *param )
675 InputManager *im = (InputManager*)param;
677 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
678 QApplication::postEvent( im, static_cast<QEvent*>(event) );
682 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
683 vlc_value_t oldval, vlc_value_t newval, void *param )
685 InputManager *im = (InputManager*)param;
687 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
688 QApplication::postEvent( im, static_cast<QEvent*>(event) );
692 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
693 vlc_value_t oldval, vlc_value_t newval, void *param )
695 InputManager *im = (InputManager*)param;
697 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
698 QApplication::postEvent( im, static_cast<QEvent*>(event) );
702 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
703 vlc_value_t n, void *param )
705 InputManager *im = (InputManager*)param;
706 IMEvent *event = new IMEvent( ItemSpuChanged_Type, 0 );
707 QApplication::postEvent( im, static_cast<QEvent*>(event) );
713 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
714 vlc_value_t oldval, vlc_value_t newval, void *param )
716 MainInputManager *mim = (MainInputManager*)param;
718 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
719 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
723 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
724 vlc_value_t oldval, vlc_value_t newval, void *param )
726 MainInputManager *mim = (MainInputManager*)param;
728 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
729 QApplication::postEvent( mim, static_cast<QEvent*>(event) );