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 );
36 static int ChangeTeletext( vlc_object_t *p_this, const char *var, vlc_value_t o,
37 vlc_value_t n, void *param );
39 static int ItemChanged( vlc_object_t *, const char *,
40 vlc_value_t, vlc_value_t, void * );
41 static int PLItemChanged( vlc_object_t *, const char *,
42 vlc_value_t, vlc_value_t, void * );
43 static int InterfaceChanged( vlc_object_t *, const char *,
44 vlc_value_t, vlc_value_t, void * );
45 static int InterfaceVoutChanged( vlc_object_t *, const char *,
46 vlc_value_t, vlc_value_t, void * );
47 static int ItemStateChanged( vlc_object_t *, const char *,
48 vlc_value_t, vlc_value_t, void * );
49 static int ItemRateChanged( vlc_object_t *, const char *,
50 vlc_value_t, vlc_value_t, void * );
51 static int ItemTitleChanged( vlc_object_t *, const char *,
52 vlc_value_t, vlc_value_t, void * );
53 static int VolumeChanged( vlc_object_t *, const char *,
54 vlc_value_t, vlc_value_t, void * );
56 /**********************************************************************
57 * InputManager implementation
58 **********************************************************************
59 * The Input Manager can be the main one around the playlist
60 * But can also be used for VLM dialog or similar
61 **********************************************************************/
63 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
64 QObject( parent ), p_intf( _p_intf )
66 i_old_playing_status = END_S;
73 b_transparentTelextext = false;
76 InputManager::~InputManager()
81 /* Define the Input used.
82 Add the callbacks on input
83 p_input is yield once here */
84 void InputManager::setInput( input_thread_t *_p_input )
88 if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
90 vlc_object_hold( p_input );
91 emit statusChanged( PLAYING_S );
99 i_input_id = input_GetItem( p_input )->i_id;
105 emit rateChanged( INPUT_RATE_DEFAULT );
109 /* delete Input if it ever existed.
110 Delete the callbacls on input
111 p_input is released once here */
112 void InputManager::delInput()
117 i_old_playing_status = END_S;
122 emit positionUpdated( -1.0, 0 ,0 );
123 emit statusChanged( END_S );
124 emit nameChanged( "" );
125 emit artChanged( NULL );
126 emit rateChanged( INPUT_RATE_DEFAULT );
127 emit voutChanged( false );
128 vlc_object_release( p_input );
135 /* Add the callbacks on Input. Self explanatory */
136 void InputManager::addCallbacks()
138 /* We don't care about:
144 - position, time, length, because they are included in intf-change
146 /* src/input/input.c:1629 */
147 var_AddCallback( p_input, "state", ItemStateChanged, this );
148 /* src/input/es-out.c:552 */
149 var_AddCallback( p_input, "spu-es", ChangeSPU, this );
150 /* emit UpdateStatus so that main_interface updates controls
151 * if there is new videotracks (mpeg-ts)*/
152 var_AddCallback( p_input, "video-es", ItemStateChanged, this );
153 /* src/input/es-out.c: */
154 var_AddCallback( p_input, "teletext-es", ChangeTeletext, this );
155 /* src/input/input.c:1765 */
156 var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
157 /* src/input/input.c:2003 */
158 var_AddCallback( p_input, "title", ItemTitleChanged, this );
159 /* src/input/input.c:734 for timers update*/
160 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
161 /* src/input/input.c for vout creation/destruction */
162 var_AddCallback( p_input, "intf-change-vout", InterfaceVoutChanged, this );
165 /* Delete the callbacks on Input. Self explanatory */
166 void InputManager::delCallbacks()
168 var_DelCallback( p_input, "spu-es", ChangeSPU, this );
169 var_DelCallback( p_input, "video-es", ItemStateChanged, this );
170 var_DelCallback( p_input, "teletext-es", ChangeTeletext, this );
171 var_DelCallback( p_input, "state", ItemStateChanged, this );
172 var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
173 var_DelCallback( p_input, "title", ItemTitleChanged, this );
174 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
175 var_DelCallback( p_input, "intf-change-vout", InterfaceVoutChanged, this );
178 /* Convert the event from the callbacks in actions */
179 void InputManager::customEvent( QEvent *event )
181 int type = event->type();
182 IMEvent *ple = static_cast<IMEvent *>(event);
184 if ( type != PositionUpdate_Type &&
185 type != ItemChanged_Type &&
186 type != ItemRateChanged_Type &&
187 type != ItemTitleChanged_Type &&
188 type != ItemSpuChanged_Type &&
189 type != ItemTeletextChanged_Type &&
190 type != ItemStateChanged_Type &&
191 type != InterfaceVoutUpdate_Type )
194 if( type == ItemStateChanged_Type )
200 if( !hasInput() ) return;
202 if( ( type != PositionUpdate_Type &&
203 type != ItemRateChanged_Type &&
204 type != ItemSpuChanged_Type &&
205 type != ItemTeletextChanged_Type &&
206 type != ItemStateChanged_Type &&
207 type != InterfaceVoutUpdate_Type
209 && ( i_input_id != ple->i_id ) )
212 if( type != PositionUpdate_Type )
213 msg_Dbg( p_intf, "New Event: type %i", type );
218 case PositionUpdate_Type:
221 case ItemChanged_Type:
226 case ItemStateChanged_Type:
231 case ItemTitleChanged_Type:
235 case ItemRateChanged_Type:
238 case ItemSpuChanged_Type:
241 case ItemTeletextChanged_Type:
244 case InterfaceVoutUpdate_Type:
250 void InputManager::UpdatePosition()
252 /* Update position */
253 int i_length, i_time; /* Int is enough, since we store seconds */
255 i_length = var_GetTime( p_input , "length" ) / 1000000;
256 i_time = var_GetTime( p_input , "time") / 1000000;
257 f_pos = var_GetFloat( p_input , "position" );
258 emit positionUpdated( f_pos, i_time, i_length );
261 void InputManager::UpdateNavigation()
263 /* Update navigation status */
264 vlc_value_t val; val.i_int = 0;
266 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
270 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
271 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
275 emit navigationChanged( 0 );
279 void InputManager::UpdateStatus()
281 /* Update playing status */
282 vlc_value_t val; val.i_int = 0;
283 var_Get( p_input, "state", &val );
284 if( i_old_playing_status != val.i_int )
286 i_old_playing_status = val.i_int;
287 emit statusChanged( val.i_int );
291 void InputManager::UpdateRate()
294 int i_new_rate = var_GetInteger( p_input, "rate");
295 if( i_new_rate != i_rate )
299 emit rateChanged( i_rate );
303 void InputManager::UpdateMeta()
305 /* Update text, name and nowplaying */
308 /* Try to get the Title, then the Name */
309 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
310 if( EMPTY_STR( psz_name ) )
313 psz_name = input_item_GetName( input_GetItem( p_input ) );
316 /* Try to get the nowplaying */
317 char *psz_nowplaying =
318 input_item_GetNowPlaying( input_GetItem( p_input ) );
319 if( !EMPTY_STR( psz_nowplaying ) )
321 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
323 else /* Do it ourself */
325 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
327 if( !EMPTY_STR( psz_artist ) )
328 text.sprintf( "%s - %s", psz_artist, psz_name );
330 text.sprintf( "%s", psz_name );
334 /* Free everything */
336 free( psz_nowplaying );
338 /* If we have Nothing */
341 psz_name = input_item_GetURI( input_GetItem( p_input ) );
342 text.sprintf( "%s", psz_name );
343 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
347 if( old_name != text )
349 emit nameChanged( text );
354 bool InputManager::hasAudio()
359 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
360 return val.i_int > 0;
365 void InputManager::UpdateSPU()
370 void InputManager::UpdateTeletext()
373 telexToggle( var_GetInteger( p_input, "teletext-es" ) >= 0 );
375 telexToggle( false );
378 void InputManager::UpdateVout()
382 bool b_old_video = b_video;
384 vlc_object_t *p_vout = (vlc_object_t*)vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
385 b_video = p_vout != NULL;
387 vlc_object_release( p_vout );
388 if( !!b_old_video != !!b_video )
389 emit voutChanged( b_video );
393 void InputManager::UpdateArt()
395 /* Update Art meta */
396 emit artChanged( input_GetItem( p_input ) );
399 /* User update of the slider */
400 void InputManager::sliderUpdate( float new_pos )
403 var_SetFloat( p_input, "position", new_pos );
406 /* User togglePlayPause */
407 void InputManager::togglePlayPause()
410 var_Get( p_input, "state", &state );
411 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
412 var_Set( p_input, "state", state );
413 emit statusChanged( state.i_int );
416 void InputManager::sectionPrev()
420 int i_type = var_Type( p_input, "next-chapter" );
421 vlc_value_t val; val.b_bool = true;
422 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
423 "prev-chapter":"prev-title", val );
427 void InputManager::sectionNext()
431 int i_type = var_Type( p_input, "next-chapter" );
432 vlc_value_t val; val.b_bool = true;
433 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
434 "next-chapter":"next-title", val );
438 void InputManager::sectionMenu()
442 vlc_value_t val, text;
445 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
448 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
450 for( int i = 0; i < val.p_list->i_count; i++ )
452 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
455 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
457 var_Set( p_input, "title 0", root );
461 void InputManager::telexGotoPage( int page )
465 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
466 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
468 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
471 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
472 "zvbi", FIND_ANYWHERE );
475 var_SetInteger( p_vbi, "vbi-page", page );
476 vlc_object_release( p_vbi );
480 emit setNewTelexPage( page );
483 void InputManager::telexToggle( bool b_enabled )
487 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
488 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
490 b_enabled = (i_teletext_es >= 0);
491 emit teletextEnabled( b_enabled );
492 if( b_enabled && (i_teletext_es == i_spu_es) )
496 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
497 "zvbi", FIND_ANYWHERE );
500 i_page = var_GetInteger( p_vbi, "vbi-page" );
501 vlc_object_release( p_vbi );
502 i_page = b_enabled ? i_page : 0;
503 telexGotoPage( i_page );
507 else emit teletextEnabled( b_enabled );
510 void InputManager::telexToggleButtons()
514 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
515 if( i_teletext_es >= 0 )
517 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
519 if( i_teletext_es == i_spu_es )
520 var_SetInteger( p_input, "spu-es", -1 );
522 var_SetInteger( p_input, "spu-es", i_teletext_es );
524 emit toggleTelexButtons();
529 void InputManager::telexSetTransparency()
534 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
535 "zvbi", FIND_ANYWHERE );
538 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
539 b_transparentTelextext = !b_transparentTelextext;
540 vlc_object_release( p_vbi );
543 emit toggleTelexTransparency();
546 void InputManager::slower()
549 var_SetVoid( p_input, "rate-slower" );
552 void InputManager::faster()
555 var_SetVoid( p_input, "rate-faster" );
558 void InputManager::normalRate()
561 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
564 void InputManager::setRate( int new_rate )
567 var_SetInteger( p_input, "rate", new_rate );
570 /**********************************************************************
571 * MainInputManager implementation. Wrap an input manager and
572 * take care of updating the main playlist input.
573 * Used in the main playlist Dialog
574 **********************************************************************/
575 MainInputManager * MainInputManager::instance = NULL;
577 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
578 : QObject(NULL), p_intf( _p_intf )
581 im = new InputManager( this, p_intf );
583 // var_AddCallback( THEPL, "item-change", PLItemChanged, this );
584 var_AddCallback( THEPL, "item-change", ItemChanged, im );
585 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
586 var_AddCallback( THEPL, "activity", PLItemChanged, this );
588 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
590 /* Warn our embedded IM about input changes */
591 CONNECT( this, inputChanged( input_thread_t * ),
592 im, setInput( input_thread_t * ) );
594 /* emit check if playlist has allready started playing */
596 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
597 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
598 QApplication::postEvent( this, static_cast<QEvent*>(event) );
602 MainInputManager::~MainInputManager()
606 var_DelCallback( p_input, "state", PLItemChanged, this );
607 vlc_object_release( p_input );
608 emit inputChanged( NULL );
611 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
613 var_DelCallback( THEPL, "activity", PLItemChanged, this );
614 var_DelCallback( THEPL, "item-change", ItemChanged, im );
615 // var_DelCallback( THEPL, "item-change", PLItemChanged, this );
617 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
620 void MainInputManager::customEvent( QEvent *event )
622 int type = event->type();
623 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
626 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
627 if( type == VolumeChanged_Type )
629 emit volumeChanged();
633 /* Should be PLItemChanged Event */
634 if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
636 vlc_mutex_lock( &p_intf->change_lock );
637 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
639 var_DelCallback( p_input, "state", PLItemChanged, this );
640 vlc_object_release( p_input );
641 emit inputChanged( NULL );
643 vlc_mutex_unlock( &p_intf->change_lock );
650 p_input = THEPL->p_input;
651 if( p_input && !( !vlc_object_alive (p_input) || p_input->b_dead) )
653 vlc_object_hold( p_input );
654 var_AddCallback( p_input, "state", PLItemChanged, this );
655 emit inputChanged( p_input );
661 vlc_mutex_unlock( &p_intf->change_lock );
665 /* we are working as a dialogs provider */
666 playlist_t *p_playlist = pl_Yield( p_intf );
667 p_input = playlist_CurrentInput( p_playlist );
670 emit inputChanged( p_input );
671 vlc_object_release( p_input );
673 pl_Release( p_intf );
677 /* Playlist Control functions */
678 void MainInputManager::stop()
680 playlist_Stop( THEPL );
683 void MainInputManager::next()
685 playlist_Next( THEPL );
688 void MainInputManager::prev()
690 playlist_Prev( THEPL );
693 void MainInputManager::togglePlayPause()
695 if( p_input == NULL )
697 playlist_Play( THEPL );
700 getIM()->togglePlayPause();
703 bool MainInputManager::teletextState()
708 const int i_teletext_es = var_GetInteger( getInput(), "teletext-es" );
709 const int i_spu_es = var_GetInteger( getInput(), "spu-es" );
711 return i_teletext_es >= 0 && i_teletext_es == i_spu_es;
716 /* Static callbacks */
719 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
720 vlc_value_t oldval, vlc_value_t newval, void *param )
722 /* FIXME remove that static variable */
723 static int counter = 0;
724 InputManager *im = (InputManager*)param;
726 counter = ++counter % 4;
729 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
730 QApplication::postEvent( im, static_cast<QEvent*>(event) );
734 static int InterfaceVoutChanged( vlc_object_t *p_this, const char *psz_var,
735 vlc_value_t oldval, vlc_value_t newval, void *param )
737 InputManager *im = (InputManager*)param;
739 IMEvent *event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
740 QApplication::postEvent( im, static_cast<QEvent*>(event) );
744 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
745 vlc_value_t oldval, vlc_value_t newval, void *param )
747 InputManager *im = (InputManager*)param;
749 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
750 QApplication::postEvent( im, static_cast<QEvent*>(event) );
754 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
755 vlc_value_t oldval, vlc_value_t newval, void *param )
757 InputManager *im = (InputManager*)param;
759 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
760 QApplication::postEvent( im, static_cast<QEvent*>(event) );
764 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
765 vlc_value_t oldval, vlc_value_t newval, void *param )
767 InputManager *im = (InputManager*)param;
769 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
770 QApplication::postEvent( im, static_cast<QEvent*>(event) );
774 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
775 vlc_value_t oldval, vlc_value_t newval, void *param )
777 InputManager *im = (InputManager*)param;
779 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
780 QApplication::postEvent( im, static_cast<QEvent*>(event) );
784 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
785 vlc_value_t n, void *param )
787 InputManager *im = (InputManager*)param;
788 IMEvent *event = new IMEvent( ItemSpuChanged_Type, 0 );
789 QApplication::postEvent( im, static_cast<QEvent*>(event) );
793 static int ChangeTeletext( vlc_object_t *p_this, const char *var, vlc_value_t o,
794 vlc_value_t n, void *param )
797 InputManager *im = (InputManager*)param;
798 IMEvent *event = new IMEvent( ItemTeletextChanged_Type, 0 );
799 QApplication::postEvent( im, static_cast<QEvent*>(event) );
804 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
805 vlc_value_t oldval, vlc_value_t newval, void *param )
807 MainInputManager *mim = (MainInputManager*)param;
809 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
810 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
814 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
815 vlc_value_t oldval, vlc_value_t newval, void *param )
817 MainInputManager *mim = (MainInputManager*)param;
819 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
820 QApplication::postEvent( mim, static_cast<QEvent*>(event) );