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"
32 #include <QApplication>
34 static int ItemChanged( vlc_object_t *, const char *,
35 vlc_value_t, vlc_value_t, void * );
36 static int PLItemChanged( vlc_object_t *, const char *,
37 vlc_value_t, vlc_value_t, void * );
38 static int VolumeChanged( vlc_object_t *, const char *,
39 vlc_value_t, vlc_value_t, void * );
41 static int InputEvent( vlc_object_t *, const char *,
42 vlc_value_t, vlc_value_t, void * );
45 /**********************************************************************
46 * InputManager implementation
47 **********************************************************************
48 * The Input Manager can be the main one around the playlist
49 * But can also be used for VLM dialog or similar
50 **********************************************************************/
52 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
53 QObject( parent ), p_intf( _p_intf )
55 i_old_playing_status = END_S;
67 InputManager::~InputManager()
72 /* Define the Input used.
73 Add the callbacks on input
74 p_input is held once here */
75 void InputManager::setInput( input_thread_t *_p_input )
79 if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
81 msg_Dbg( p_intf, "IM: Setting an input" );
82 vlc_object_hold( p_input );
83 emit statusChanged( PLAYING_S );
90 i_input_id = input_GetItem( p_input )->i_id;
96 emit rateChanged( INPUT_RATE_DEFAULT );
100 /* delete Input if it ever existed.
101 Delete the callbacls on input
102 p_input is released once here */
103 void InputManager::delInput()
105 if( !p_input ) return;
106 msg_Dbg( p_intf, "IM: Deleting the input" );
109 i_old_playing_status = END_S;
117 vlc_object_release( p_input );
120 emit positionUpdated( -1.0, 0 ,0 );
121 emit rateChanged( INPUT_RATE_DEFAULT ); /* TODO: Do we want this ? */
122 emit nameChanged( "" );
123 emit chapterChanged( 0 );
124 emit titleChanged( 0 );
125 emit statusChanged( END_S );
127 emit teletextPossible( false );
128 emit AtoBchanged( false, false );
129 emit voutChanged( false );
131 /* Reset all InfoPanels but stats */
132 emit artChanged( NULL );
133 emit infoChanged( NULL );
134 emit metaChanged( NULL );
137 /* Add the callbacks on Input. Self explanatory */
138 inline void InputManager::addCallbacks()
140 var_AddCallback( p_input, "intf-event", InputEvent, this );
143 /* Delete the callbacks on Input. Self explanatory */
144 inline void InputManager::delCallbacks()
146 var_DelCallback( p_input, "intf-event", InputEvent, this );
149 /* Convert the event from the callbacks in actions */
150 void InputManager::customEvent( QEvent *event )
152 int i_type = event->type();
153 IMEvent *ple = static_cast<IMEvent *>(event);
155 if ( i_type != PositionUpdate_Type &&
156 i_type != ItemChanged_Type &&
157 i_type != ItemRateChanged_Type &&
158 i_type != ItemTitleChanged_Type &&
159 i_type != ItemEsChanged_Type &&
160 i_type != ItemTeletextChanged_Type &&
161 i_type != ItemStateChanged_Type &&
162 i_type != StatisticsUpdate_Type &&
163 i_type != InterfaceVoutUpdate_Type &&
164 i_type != MetaChanged_Type &&
165 i_type != NameChanged_Type &&
166 i_type != InfoChanged_Type &&
167 i_type != SynchroChanged_Type &&
168 i_type != CachingEvent_Type )
171 if( i_type == CachingEvent_Type )
174 if( !hasInput() ) return;
176 if( ( i_type != PositionUpdate_Type &&
177 i_type != ItemRateChanged_Type &&
178 i_type != ItemEsChanged_Type &&
179 i_type != ItemTeletextChanged_Type &&
180 i_type != ItemStateChanged_Type &&
181 i_type != StatisticsUpdate_Type &&
182 i_type != InterfaceVoutUpdate_Type &&
183 i_type != MetaChanged_Type &&
184 i_type != NameChanged_Type &&
185 i_type != InfoChanged_Type &&
186 i_type != SynchroChanged_Type
188 && ( i_input_id != ple->i_id ) )
192 if( i_type != PositionUpdate_Type &&
193 i_type != StatisticsUpdate_Type )
194 msg_Dbg( p_intf, "New Event: type %i", i_type );
200 case PositionUpdate_Type:
203 case StatisticsUpdate_Type:
206 case ItemChanged_Type:
211 case ItemStateChanged_Type:
212 // TODO: Fusion with above state
215 // UpdateNavigation(); This shouldn't be useful now
216 // UpdateTeletext(); Same
218 case NameChanged_Type:
221 case MetaChanged_Type:
223 UpdateName(); /* Needed for NowPlaying */
224 UpdateArt(); /* Art is part of meta in the core */
226 case InfoChanged_Type:
229 case ItemTitleChanged_Type:
231 UpdateName(); /* Display the name of the Chapter, if exists */
233 case ItemRateChanged_Type:
236 case ItemEsChanged_Type:
237 // We don't do anything. Why ?
239 case ItemTeletextChanged_Type:
242 case InterfaceVoutUpdate_Type:
245 case SynchroChanged_Type:
246 emit synchroChanged();
248 case CachingEvent_Type:
252 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
256 void InputManager::UpdatePosition()
258 /* Update position */
259 int i_length, i_time; /* Int is enough, since we store seconds */
261 i_length = var_GetTime( p_input , "length" ) / 1000000;
262 i_time = var_GetTime( p_input , "time") / 1000000;
263 f_pos = var_GetFloat( p_input , "position" );
264 emit positionUpdated( f_pos, i_time, i_length );
267 void InputManager::UpdateNavigation()
269 /* Update navigation status */
270 vlc_value_t val; val.i_int = 0;
273 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
277 emit titleChanged( true );
278 /* p_input != NULL since val.i_int != 0 */
280 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
281 emit chapterChanged( (val.i_int > 0) );
284 emit titleChanged( false );
287 void InputManager::UpdateStatus()
289 /* Update playing status */
290 vlc_value_t val; val.i_int = 0;
291 var_Get( p_input, "state", &val );
292 if( i_old_playing_status != val.i_int )
294 i_old_playing_status = val.i_int;
295 emit statusChanged( val.i_int );
299 void InputManager::UpdateRate()
302 int i_new_rate = var_GetInteger( p_input, "rate");
303 if( i_new_rate != i_rate )
307 emit rateChanged( i_rate );
311 void InputManager::UpdateName()
313 /* Update text, name and nowplaying */
316 /* Try to get the Title, then the Name */
317 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
318 if( EMPTY_STR( psz_name ) )
321 psz_name = input_item_GetName( input_GetItem( p_input ) );
324 /* Try to get the nowplaying */
325 char *psz_nowplaying =
326 input_item_GetNowPlaying( input_GetItem( p_input ) );
327 if( !EMPTY_STR( psz_nowplaying ) )
329 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
331 else /* Do it ourself */
333 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
335 if( !EMPTY_STR( psz_artist ) )
336 text.sprintf( "%s - %s", psz_artist, psz_name );
338 text.sprintf( "%s", psz_name );
342 /* Free everything */
344 free( psz_nowplaying );
346 /* If we have Nothing */
349 psz_name = input_item_GetURI( input_GetItem( p_input ) );
350 text.sprintf( "%s", psz_name );
351 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
355 if( oldName != text )
357 emit nameChanged( text );
362 bool InputManager::hasAudio()
367 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
368 return val.i_int > 0;
373 void InputManager::UpdateTeletext()
376 telexActivation( var_GetInteger( p_input, "teletext-es" ) >= 0 );
378 telexActivation( false );
381 void InputManager::UpdateVout()
385 bool b_old_video = b_video;
387 vlc_object_t *p_vout = (vlc_object_t*)vlc_object_find( p_input,
388 VLC_OBJECT_VOUT, FIND_CHILD );
389 b_video = p_vout != NULL;
391 vlc_object_release( p_vout );
392 if( !!b_old_video != !!b_video )
393 emit voutChanged( b_video );
397 void InputManager::UpdateCaching()
399 float f_newCache = var_GetFloat( p_input, "cache" );
400 if( f_newCache != f_cache )
402 f_newCache = f_cache;
404 emit cachingChanged( f_cache );
408 inline void InputManager::UpdateArt()
410 /* Update Art meta */
411 emit artChanged( input_GetItem( p_input ) );
414 inline void InputManager::UpdateStats()
416 emit statisticsUpdated( input_GetItem( p_input ) );
419 inline void InputManager::UpdateMeta()
421 emit metaChanged( input_GetItem( p_input ) );
424 inline void InputManager::UpdateInfo()
426 emit infoChanged( input_GetItem( p_input ) );
429 /* User update of the slider */
430 void InputManager::sliderUpdate( float new_pos )
433 var_SetFloat( p_input, "position", new_pos );
436 /* User togglePlayPause */
437 void InputManager::togglePlayPause()
442 var_Get( p_input, "state", &state );
443 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
444 var_Set( p_input, "state", state );
445 emit statusChanged( state.i_int );
449 void InputManager::sectionPrev()
453 int i_type = var_Type( p_input, "next-chapter" );
454 vlc_value_t val; val.b_bool = true;
455 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
456 "prev-chapter":"prev-title", val );
460 void InputManager::sectionNext()
464 int i_type = var_Type( p_input, "next-chapter" );
465 vlc_value_t val; val.b_bool = true;
466 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
467 "next-chapter":"next-title", val );
471 void InputManager::sectionMenu()
475 vlc_value_t val, text;
478 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
481 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
483 for( int i = 0; i < val.p_list->i_count; i++ )
485 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
488 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
490 var_Set( p_input, "title 0", root );
498 /* Set a new Teletext Page */
499 void InputManager::telexSetPage( int page )
503 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
504 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
506 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
508 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
509 "zvbi", FIND_ANYWHERE );
512 var_SetInteger( p_vbi, "vbi-page", page );
513 vlc_object_release( p_vbi );
514 emit newTelexPageSet( page );
520 /* Set the transparency on teletext */
521 void InputManager::telexSetTransparency( bool b_transparentTelextext )
525 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
526 "zvbi", FIND_ANYWHERE );
529 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
530 vlc_object_release( p_vbi );
531 emit teletextTransparencyActivated( b_transparentTelextext );
536 void InputManager::telexActivation( bool b_enabled )
540 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
541 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
543 /* Teletext is possible. Show the buttons */
544 b_enabled = (i_teletext_es >= 0);
545 emit teletextPossible( b_enabled );
546 if( !b_enabled ) return;
548 /* If Teletext is selected */
549 if( i_teletext_es == i_spu_es )
551 /* Activate the buttons */
552 teletextActivated( true );
554 /* Then, find the current page */
556 vlc_object_t *p_vbi = (vlc_object_t *)
557 vlc_object_find_name( p_input, "zvbi", FIND_ANYWHERE );
560 i_page = var_GetInteger( p_vbi, "vbi-page" );
561 vlc_object_release( p_vbi );
562 emit newTelexPageSet( i_page );
567 emit teletextPossible( b_enabled );
570 void InputManager::activateTeletext( bool b_enable )
574 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
575 if( i_teletext_es >= 0 )
577 var_SetInteger( p_input, "spu-es", b_enable ? i_teletext_es : -1 );
582 void InputManager::reverse()
586 int i_rate = var_GetInteger( p_input, "rate" );
587 var_SetInteger( p_input, "rate", -i_rate );
591 void InputManager::slower()
594 var_SetVoid( p_input, "rate-slower" );
597 void InputManager::faster()
600 var_SetVoid( p_input, "rate-faster" );
603 void InputManager::normalRate()
606 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
609 void InputManager::setRate( int new_rate )
612 var_SetInteger( p_input, "rate", new_rate );
615 void InputManager::setAtoB()
619 timeA = var_GetTime( THEMIM->getInput(), "time" );
623 timeB = var_GetTime( THEMIM->getInput(), "time" );
624 var_SetTime( THEMIM->getInput(), "time" , timeA );
631 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
634 /* Function called regularly when in an AtoB loop */
635 void InputManager::AtoBLoop( int i_time )
639 if( ( i_time >= (int)( timeB/1000000 ) )
640 || ( i_time < (int)( timeA/1000000 ) ) )
641 var_SetTime( THEMIM->getInput(), "time" , timeA );
645 /**********************************************************************
646 * MainInputManager implementation. Wrap an input manager and
647 * take care of updating the main playlist input.
648 * Used in the main playlist Dialog
649 **********************************************************************/
650 MainInputManager * MainInputManager::instance = NULL;
652 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
653 : QObject(NULL), p_intf( _p_intf )
656 im = new InputManager( this, p_intf );
658 var_AddCallback( THEPL, "item-change", ItemChanged, im );
659 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
660 var_AddCallback( THEPL, "activity", PLItemChanged, this );
662 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
664 /* Warn our embedded IM about input changes */
665 CONNECT( this, inputChanged( input_thread_t * ),
666 im, setInput( input_thread_t * ) );
668 /* emit check if playlist has allready started playing */
670 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
671 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
672 QApplication::postEvent( this, static_cast<QEvent*>(event) );
675 MainInputManager::~MainInputManager()
679 var_DelCallback( p_input, "state", PLItemChanged, this );
680 vlc_object_release( p_input );
681 emit inputChanged( NULL );
684 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
686 var_DelCallback( THEPL, "activity", PLItemChanged, this );
687 var_DelCallback( THEPL, "item-change", ItemChanged, im );
689 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
692 void MainInputManager::customEvent( QEvent *event )
694 int type = event->type();
695 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
698 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
699 if( type == VolumeChanged_Type )
701 emit volumeChanged();
705 /* Should be PLItemChanged Event */
706 if( !p_intf->p_sys->b_isDialogProvider )
708 vlc_mutex_lock( &p_intf->change_lock );
709 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
711 var_DelCallback( p_input, "state", PLItemChanged, this );
712 vlc_object_release( p_input );
713 emit inputChanged( NULL );
715 vlc_mutex_unlock( &p_intf->change_lock );
721 p_input = playlist_CurrentInput(THEPL);
724 var_AddCallback( p_input, "state", PLItemChanged, this );
725 emit inputChanged( p_input );
728 vlc_mutex_unlock( &p_intf->change_lock );
732 /* we are working as a dialogs provider */
733 playlist_t *p_playlist = pl_Hold( p_intf );
734 p_input = playlist_CurrentInput( p_playlist );
737 emit inputChanged( p_input );
738 vlc_object_release( p_input );
740 pl_Release( p_intf );
744 /* Playlist Control functions */
745 void MainInputManager::stop()
747 playlist_Stop( THEPL );
750 void MainInputManager::next()
752 playlist_Next( THEPL );
755 void MainInputManager::prev()
757 playlist_Prev( THEPL );
760 void MainInputManager::togglePlayPause()
764 playlist_Play( THEPL );
766 getIM()->togglePlayPause();
769 /* Static callbacks */
772 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
773 vlc_value_t oldval, vlc_value_t newval, void *param )
775 InputManager *im = (InputManager*)param;
777 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
778 QApplication::postEvent( im, static_cast<QEvent*>(event) );
782 static int InputEvent( vlc_object_t *p_this, const char *,
783 vlc_value_t, vlc_value_t newval, void *param )
785 InputManager *im = (InputManager*)param;
788 switch( newval.i_int )
790 case INPUT_EVENT_STATE:
791 event = new IMEvent( ItemStateChanged_Type, 0 );
793 case INPUT_EVENT_RATE:
794 event = new IMEvent( ItemRateChanged_Type, 0 );
796 case INPUT_EVENT_TIMES:
797 event = new IMEvent( PositionUpdate_Type, 0 );
800 case INPUT_EVENT_TITLE:
801 case INPUT_EVENT_CHAPTER:
802 event = new IMEvent( ItemTitleChanged_Type, 0 );
806 event = new IMEvent( ItemEsChanged_Type, 0 );
808 case INPUT_EVENT_TELETEXT:
809 event = new IMEvent( ItemTeletextChanged_Type, 0 );
812 case INPUT_EVENT_VOUT:
813 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
816 case INPUT_EVENT_STATISTICS:
817 event = new IMEvent( StatisticsUpdate_Type, 0 );
820 case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
821 event = new IMEvent( MetaChanged_Type, 0 );
823 case INPUT_EVENT_ITEM_INFO: /* Codec Info */
824 event = new IMEvent( InfoChanged_Type, 0 );
826 case INPUT_EVENT_ITEM_NAME:
827 event = new IMEvent( NameChanged_Type, 0 );
830 case INPUT_EVENT_AUDIO_DELAY:
831 case INPUT_EVENT_SUBTITLE_DELAY:
832 event = new IMEvent( SynchroChanged_Type, 0 );
835 case INPUT_EVENT_CACHE:
836 event = new IMEvent( CachingEvent_Type, 0 );
839 case INPUT_EVENT_PROGRAM:
840 case INPUT_EVENT_RECORD:
841 case INPUT_EVENT_SIGNAL:
842 case INPUT_EVENT_BOOKMARK:
849 QApplication::postEvent( im, static_cast<QEvent*>(event) );
854 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
855 vlc_value_t oldval, vlc_value_t newval, void *param )
857 MainInputManager *mim = (MainInputManager*)param;
859 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
860 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
864 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
865 vlc_value_t oldval, vlc_value_t newval, void *param )
867 MainInputManager *mim = (MainInputManager*)param;
869 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
870 QApplication::postEvent( mim, static_cast<QEvent*>(event) );