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>
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 VolumeChanged( vlc_object_t *, const char *,
41 vlc_value_t, vlc_value_t, void * );
43 static int InputEvent( vlc_object_t *, const char *,
44 vlc_value_t, vlc_value_t, void * );
47 /**********************************************************************
48 * InputManager implementation
49 **********************************************************************
50 * The Input Manager can be the main one around the playlist
51 * But can also be used for VLM dialog or similar
52 **********************************************************************/
54 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
55 QObject( parent ), p_intf( _p_intf )
57 i_old_playing_status = END_S;
69 InputManager::~InputManager()
74 /* Define the Input used.
75 Add the callbacks on input
76 p_input is held 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 msg_Dbg( p_intf, "IM: Setting an input" );
84 vlc_object_hold( p_input );
85 emit statusChanged( PLAYING_S );
92 i_input_id = input_GetItem( p_input )->i_id;
98 emit rateChanged( INPUT_RATE_DEFAULT );
102 /* delete Input if it ever existed.
103 Delete the callbacls on input
104 p_input is released once here */
105 void InputManager::delInput()
107 if( !p_input ) return;
108 msg_Dbg( p_intf, "IM: Deleting the input" );
111 i_old_playing_status = END_S;
119 vlc_object_release( p_input );
122 emit positionUpdated( -1.0, 0 ,0 );
123 emit rateChanged( INPUT_RATE_DEFAULT ); /* TODO: Do we want this ? */
124 emit nameChanged( "" );
125 emit chapterChanged( 0 );
126 emit titleChanged( 0 );
127 emit statusChanged( END_S );
129 emit teletextPossible( false );
130 emit AtoBchanged( false, false );
131 emit voutChanged( false );
132 emit voutListChanged( NULL, 0 );
134 /* Reset all InfoPanels but stats */
135 emit artChanged( NULL );
136 emit infoChanged( NULL );
137 emit metaChanged( NULL );
140 /* Convert the event from the callbacks in actions */
141 void InputManager::customEvent( QEvent *event )
143 int i_type = event->type();
144 IMEvent *ple = static_cast<IMEvent *>(event);
146 assert( !( i_type != PositionUpdate_Type &&
147 i_type != ItemChanged_Type &&
148 i_type != ItemRateChanged_Type &&
149 i_type != ItemTitleChanged_Type &&
150 i_type != ItemEsChanged_Type &&
151 i_type != ItemTeletextChanged_Type &&
152 i_type != ItemStateChanged_Type &&
153 i_type != StatisticsUpdate_Type &&
154 i_type != InterfaceVoutUpdate_Type &&
155 i_type != MetaChanged_Type &&
156 i_type != NameChanged_Type &&
157 i_type != InfoChanged_Type &&
158 i_type != SynchroChanged_Type &&
159 i_type != CachingEvent_Type &&
160 i_type != BookmarksChanged_Type &&
161 i_type != InterfaceAoutUpdate_Type &&
162 i_type != RecordingEvent_Type ) );
164 if( !hasInput() ) return;
166 if( i_type == CachingEvent_Type )
169 if( ( i_type != PositionUpdate_Type &&
170 i_type != ItemRateChanged_Type &&
171 i_type != ItemEsChanged_Type &&
172 i_type != ItemTeletextChanged_Type &&
173 i_type != ItemStateChanged_Type &&
174 i_type != StatisticsUpdate_Type &&
175 i_type != InterfaceVoutUpdate_Type &&
176 i_type != MetaChanged_Type &&
177 i_type != NameChanged_Type &&
178 i_type != InfoChanged_Type &&
179 i_type != SynchroChanged_Type &&
180 i_type != BookmarksChanged_Type &&
181 i_type != InterfaceAoutUpdate_Type &&
182 i_type != RecordingEvent_Type
184 && ( i_input_id != ple->i_id ) )
188 if( i_type != PositionUpdate_Type &&
189 i_type != StatisticsUpdate_Type )
190 msg_Dbg( p_intf, "New Event: type %i", i_type );
196 case PositionUpdate_Type:
199 case StatisticsUpdate_Type:
202 case ItemChanged_Type:
207 case ItemStateChanged_Type:
208 // TODO: Fusion with above state
211 // UpdateNavigation(); This shouldn't be useful now
212 // UpdateTeletext(); Same
214 case NameChanged_Type:
217 case MetaChanged_Type:
219 UpdateName(); /* Needed for NowPlaying */
220 UpdateArt(); /* Art is part of meta in the core */
222 case InfoChanged_Type:
225 case ItemTitleChanged_Type:
227 UpdateName(); /* Display the name of the Chapter, if exists */
229 case ItemRateChanged_Type:
232 case ItemEsChanged_Type:
233 // We don't do anything. Why ?
235 case ItemTeletextChanged_Type:
238 case InterfaceVoutUpdate_Type:
241 case SynchroChanged_Type:
242 emit synchroChanged();
244 case CachingEvent_Type:
247 case BookmarksChanged_Type:
248 emit bookmarksChanged();
250 case InterfaceAoutUpdate_Type:
253 case RecordingEvent_Type:
257 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
261 /* Add the callbacks on Input. Self explanatory */
262 inline void InputManager::addCallbacks()
264 var_AddCallback( p_input, "intf-event", InputEvent, this );
267 /* Delete the callbacks on Input. Self explanatory */
268 inline void InputManager::delCallbacks()
270 var_DelCallback( p_input, "intf-event", InputEvent, this );
273 /* Static callbacks for IM */
274 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
275 vlc_value_t oldval, vlc_value_t newval, void *param )
277 InputManager *im = (InputManager*)param;
279 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
280 QApplication::postEvent( im, static_cast<QEvent*>(event) );
284 static int InputEvent( vlc_object_t *p_this, const char *,
285 vlc_value_t, vlc_value_t newval, void *param )
287 InputManager *im = (InputManager*)param;
290 switch( newval.i_int )
292 case INPUT_EVENT_STATE:
293 event = new IMEvent( ItemStateChanged_Type, 0 );
295 case INPUT_EVENT_RATE:
296 event = new IMEvent( ItemRateChanged_Type, 0 );
298 case INPUT_EVENT_TIMES:
299 event = new IMEvent( PositionUpdate_Type, 0 );
302 case INPUT_EVENT_TITLE:
303 case INPUT_EVENT_CHAPTER:
304 event = new IMEvent( ItemTitleChanged_Type, 0 );
308 event = new IMEvent( ItemEsChanged_Type, 0 );
310 case INPUT_EVENT_TELETEXT:
311 event = new IMEvent( ItemTeletextChanged_Type, 0 );
314 case INPUT_EVENT_STATISTICS:
315 event = new IMEvent( StatisticsUpdate_Type, 0 );
318 case INPUT_EVENT_VOUT:
319 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
321 case INPUT_EVENT_AOUT:
322 event = new IMEvent( InterfaceAoutUpdate_Type, 0 );
325 case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
326 event = new IMEvent( MetaChanged_Type, 0 );
328 case INPUT_EVENT_ITEM_INFO: /* Codec Info */
329 event = new IMEvent( InfoChanged_Type, 0 );
331 case INPUT_EVENT_ITEM_NAME:
332 event = new IMEvent( NameChanged_Type, 0 );
335 case INPUT_EVENT_AUDIO_DELAY:
336 case INPUT_EVENT_SUBTITLE_DELAY:
337 event = new IMEvent( SynchroChanged_Type, 0 );
340 case INPUT_EVENT_CACHE:
341 event = new IMEvent( CachingEvent_Type, 0 );
344 case INPUT_EVENT_BOOKMARK:
345 event = new IMEvent( BookmarksChanged_Type, 0 );
348 case INPUT_EVENT_RECORD:
349 event = new IMEvent( RecordingEvent_Type, 0 );
352 case INPUT_EVENT_PROGRAM:
353 /* This is for PID changes */
354 /* event = new IMEvent( ProgramChanged_Type, 0 );
356 case INPUT_EVENT_SIGNAL:
357 /* This is for capture-card signals */
358 /* event = new IMEvent( SignalChanged_Type, 0 );
366 QApplication::postEvent( im, static_cast<QEvent*>(event) );
369 void InputManager::UpdatePosition()
371 /* Update position */
372 int i_length, i_time; /* Int is enough, since we store seconds */
374 i_length = var_GetTime( p_input , "length" ) / 1000000;
375 i_time = var_GetTime( p_input , "time") / 1000000;
376 f_pos = var_GetFloat( p_input , "position" );
377 emit positionUpdated( f_pos, i_time, i_length );
380 void InputManager::UpdateNavigation()
382 /* Update navigation status */
383 vlc_value_t val; val.i_int = 0;
386 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
390 emit titleChanged( true );
391 msg_Dbg( p_intf, "Title %i", val.i_int );
392 /* p_input != NULL since val.i_int != 0 */
394 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
395 emit chapterChanged( (val.i_int > 0) );
396 msg_Dbg( p_intf, "Chapter: %i", val.i_int );
399 emit titleChanged( false );
402 void InputManager::UpdateStatus()
404 /* Update playing status */
405 vlc_value_t val; val.i_int = 0;
406 var_Get( p_input, "state", &val );
407 if( i_old_playing_status != val.i_int )
409 i_old_playing_status = val.i_int;
410 emit statusChanged( val.i_int );
414 void InputManager::UpdateRate()
417 int i_new_rate = var_GetInteger( p_input, "rate");
418 if( i_new_rate != i_rate )
422 emit rateChanged( i_rate );
426 void InputManager::UpdateName()
428 /* Update text, name and nowplaying */
431 /* Try to get the Title, then the Name */
432 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
433 if( EMPTY_STR( psz_name ) )
436 psz_name = input_item_GetName( input_GetItem( p_input ) );
439 /* Try to get the nowplaying */
440 char *psz_nowplaying =
441 input_item_GetNowPlaying( input_GetItem( p_input ) );
442 if( !EMPTY_STR( psz_nowplaying ) )
444 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
446 else /* Do it ourself */
448 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
450 if( !EMPTY_STR( psz_artist ) )
451 text.sprintf( "%s - %s", psz_artist, psz_name );
453 text.sprintf( "%s", psz_name );
457 /* Free everything */
459 free( psz_nowplaying );
461 /* If we have Nothing */
464 psz_name = input_item_GetURI( input_GetItem( p_input ) );
465 text.sprintf( "%s", psz_name );
466 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
470 if( oldName != text )
472 emit nameChanged( text );
477 bool InputManager::hasAudio()
482 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
483 return val.i_int > 0;
488 void InputManager::UpdateTeletext()
491 telexActivation( var_GetInteger( p_input, "teletext-es" ) >= 0 );
493 telexActivation( false );
496 void InputManager::UpdateVout()
500 /* Get current vout lists from input */
502 vout_thread_t **pp_vout;
503 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
510 emit voutListChanged( pp_vout, i_vout );
513 bool b_old_video = b_video;
514 b_video = i_vout > 0;
515 if( !!b_old_video != !!b_video )
516 emit voutChanged( b_video );
518 /* Release the vout list */
519 for( int i = 0; i < i_vout; i++ )
520 vlc_object_release( (vlc_object_t*)pp_vout[i] );
524 void InputManager::UpdateAout()
531 void InputManager::UpdateCaching()
533 if(!hasInput()) return;
535 float f_newCache = var_GetFloat ( p_input, "cache" );
536 if( f_newCache != f_cache )
538 f_cache = f_newCache;
540 emit cachingChanged( f_cache );
544 void InputManager::requestArtUpdate()
548 playlist_t *p_playlist = pl_Hold( p_intf );
549 playlist_AskForArtEnqueue( p_playlist, input_GetItem( p_input ), pl_Unlocked );
550 pl_Release( p_intf );
554 /* No input will signal the cover art to update,
555 * let's do it ourself */
560 void InputManager::UpdateArt()
566 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
570 url = url.replace( "file://", QString("" ) );
571 /* Taglib seems to define a attachment://, It won't work yet */
572 url = url.replace( "attachment://", QString("" ) );
573 /* Update Art meta */
574 emit artChanged( url );
577 inline void InputManager::UpdateStats()
579 emit statisticsUpdated( input_GetItem( p_input ) );
582 inline void InputManager::UpdateMeta()
584 emit metaChanged( input_GetItem( p_input ) );
587 inline void InputManager::UpdateInfo()
589 emit infoChanged( input_GetItem( p_input ) );
592 void InputManager::UpdateRecord()
596 emit recordingStateChanged( var_GetBool( p_input, "record" ) );
600 /* User update of the slider */
601 void InputManager::sliderUpdate( float new_pos )
604 var_SetFloat( p_input, "position", new_pos );
607 /* User togglePlayPause */
608 void InputManager::togglePlayPause()
613 var_Get( p_input, "state", &state );
614 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
615 var_Set( p_input, "state", state );
616 emit statusChanged( state.i_int );
620 void InputManager::sectionPrev()
624 int i_type = var_Type( p_input, "next-chapter" );
625 vlc_value_t val; val.b_bool = true;
626 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
627 "prev-chapter":"prev-title", val );
631 void InputManager::sectionNext()
635 int i_type = var_Type( p_input, "next-chapter" );
636 vlc_value_t val; val.b_bool = true;
637 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
638 "next-chapter":"next-title", val );
642 void InputManager::sectionMenu()
646 vlc_value_t val, text;
649 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
652 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
654 for( int i = 0; i < val.p_list->i_count; i++ )
656 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
659 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
661 var_Set( p_input, "title 0", root );
669 /* Set a new Teletext Page */
670 void InputManager::telexSetPage( int page )
674 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
675 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
677 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
679 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
680 "zvbi", FIND_ANYWHERE );
683 var_SetInteger( p_vbi, "vbi-page", page );
684 vlc_object_release( p_vbi );
685 emit newTelexPageSet( page );
691 /* Set the transparency on teletext */
692 void InputManager::telexSetTransparency( bool b_transparentTelextext )
696 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
697 "zvbi", FIND_ANYWHERE );
700 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
701 vlc_object_release( p_vbi );
702 emit teletextTransparencyActivated( b_transparentTelextext );
707 void InputManager::telexActivation( bool b_enabled )
711 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
712 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
714 /* Teletext is possible. Show the buttons */
715 b_enabled = (i_teletext_es >= 0);
716 emit teletextPossible( b_enabled );
717 if( !b_enabled ) return;
719 /* If Teletext is selected */
720 if( i_teletext_es == i_spu_es )
722 /* Activate the buttons */
723 teletextActivated( true );
725 /* Then, find the current page */
727 vlc_object_t *p_vbi = (vlc_object_t *)
728 vlc_object_find_name( p_input, "zvbi", FIND_ANYWHERE );
731 i_page = var_GetInteger( p_vbi, "vbi-page" );
732 vlc_object_release( p_vbi );
733 emit newTelexPageSet( i_page );
738 emit teletextPossible( b_enabled );
741 void InputManager::activateTeletext( bool b_enable )
745 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
746 if( i_teletext_es >= 0 )
748 var_SetInteger( p_input, "spu-es", b_enable ? i_teletext_es : -1 );
753 void InputManager::reverse()
757 int i_rate = var_GetInteger( p_input, "rate" );
758 var_SetInteger( p_input, "rate", -i_rate );
762 void InputManager::slower()
765 var_SetVoid( p_input, "rate-slower" );
768 void InputManager::faster()
771 var_SetVoid( p_input, "rate-faster" );
774 void InputManager::normalRate()
777 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
780 void InputManager::setRate( int new_rate )
783 var_SetInteger( p_input, "rate", new_rate );
786 void InputManager::setAtoB()
790 timeA = var_GetTime( THEMIM->getInput(), "time" );
794 timeB = var_GetTime( THEMIM->getInput(), "time" );
795 var_SetTime( THEMIM->getInput(), "time" , timeA );
802 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
805 /* Function called regularly when in an AtoB loop */
806 void InputManager::AtoBLoop( int i_time )
810 if( ( i_time >= (int)( timeB/1000000 ) )
811 || ( i_time < (int)( timeA/1000000 ) ) )
812 var_SetTime( THEMIM->getInput(), "time" , timeA );
816 /**********************************************************************
817 * MainInputManager implementation. Wrap an input manager and
818 * take care of updating the main playlist input.
819 * Used in the main playlist Dialog
820 **********************************************************************/
821 MainInputManager * MainInputManager::instance = NULL;
823 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
824 : QObject(NULL), p_intf( _p_intf )
827 im = new InputManager( this, p_intf );
829 var_AddCallback( THEPL, "item-change", ItemChanged, im );
830 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
831 var_AddCallback( THEPL, "activity", PLItemChanged, this );
833 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
835 /* Warn our embedded IM about input changes */
836 CONNECT( this, inputChanged( input_thread_t * ),
837 im, setInput( input_thread_t * ) );
839 /* emit check if playlist has allready started playing */
841 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
843 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
844 customEvent( static_cast<QEvent*>(event) );
847 MainInputManager::~MainInputManager()
851 emit inputChanged( NULL );
852 var_DelCallback( p_input, "state", PLItemChanged, this );
853 vlc_object_release( p_input );
856 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
858 var_DelCallback( THEPL, "activity", PLItemChanged, this );
859 var_DelCallback( THEPL, "item-change", ItemChanged, im );
861 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
864 vout_thread_t* MainInputManager::getVout()
866 return p_input ? input_GetVout( p_input ) : NULL;
869 aout_instance_t * MainInputManager::getAout()
871 return p_input ? input_GetAout( p_input ) : NULL;
874 void MainInputManager::customEvent( QEvent *event )
876 int type = event->type();
877 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
880 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
881 if( type == VolumeChanged_Type )
883 emit volumeChanged();
887 /* Should be PLItemChanged Event */
888 if( !p_intf->p_sys->b_isDialogProvider )
890 vlc_mutex_lock( &p_intf->change_lock );
891 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
893 emit inputChanged( NULL );
894 var_DelCallback( p_input, "state", PLItemChanged, this );
895 vlc_object_release( p_input );
897 vlc_mutex_unlock( &p_intf->change_lock );
903 p_input = playlist_CurrentInput(THEPL);
906 var_AddCallback( p_input, "state", PLItemChanged, this );
907 emit inputChanged( p_input );
910 vlc_mutex_unlock( &p_intf->change_lock );
914 /* we are working as a dialogs provider */
915 playlist_t *p_playlist = pl_Hold( p_intf );
916 p_input = playlist_CurrentInput( p_playlist );
919 emit inputChanged( p_input );
920 vlc_object_release( p_input );
922 pl_Release( p_intf );
926 /* Playlist Control functions */
927 void MainInputManager::stop()
929 playlist_Stop( THEPL );
932 void MainInputManager::next()
934 playlist_Next( THEPL );
937 void MainInputManager::prev()
939 playlist_Prev( THEPL );
942 void MainInputManager::togglePlayPause()
946 playlist_Play( THEPL );
948 getIM()->togglePlayPause();
951 /* Static callbacks for MIM */
952 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
953 vlc_value_t oldval, vlc_value_t newval, void *param )
955 MainInputManager *mim = (MainInputManager*)param;
957 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
958 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
962 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
963 vlc_value_t oldval, vlc_value_t newval, void *param )
965 MainInputManager *mim = (MainInputManager*)param;
967 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
968 QApplication::postEvent( mim, static_cast<QEvent*>(event) );