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;
66 f_cache = -1.; /* impossible initial value, different from all */
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);
150 if( i_type != PositionUpdate_Type &&
151 i_type != StatisticsUpdate_Type )
152 msg_Dbg( p_intf, "New Event: type %i", i_type );
158 case PositionUpdate_Type:
161 case StatisticsUpdate_Type:
164 case ItemChanged_Type:
165 /* Ignore ItemChanged_Type event that does not apply to our input */
166 if( i_input_id == ple->i_id )
173 case ItemStateChanged_Type:
174 // TODO: Fusion with above state
177 // UpdateNavigation(); This shouldn't be useful now
178 // UpdateTeletext(); Same
180 case NameChanged_Type:
183 case MetaChanged_Type:
185 UpdateName(); /* Needed for NowPlaying */
186 UpdateArt(); /* Art is part of meta in the core */
188 case InfoChanged_Type:
191 case ItemTitleChanged_Type:
193 UpdateName(); /* Display the name of the Chapter, if exists */
195 case ItemRateChanged_Type:
198 case ItemEsChanged_Type:
199 // We don't do anything. Why ?
201 case ItemTeletextChanged_Type:
204 case InterfaceVoutUpdate_Type:
207 case SynchroChanged_Type:
208 emit synchroChanged();
210 case CachingEvent_Type:
213 case BookmarksChanged_Type:
214 emit bookmarksChanged();
216 case InterfaceAoutUpdate_Type:
219 case RecordingEvent_Type:
223 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
228 /* Add the callbacks on Input. Self explanatory */
229 inline void InputManager::addCallbacks()
231 var_AddCallback( p_input, "intf-event", InputEvent, this );
234 /* Delete the callbacks on Input. Self explanatory */
235 inline void InputManager::delCallbacks()
237 var_DelCallback( p_input, "intf-event", InputEvent, this );
240 /* Static callbacks for IM */
241 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
242 vlc_value_t oldval, vlc_value_t newval, void *param )
244 InputManager *im = (InputManager*)param;
246 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
247 QApplication::postEvent( im, event );
251 static int InputEvent( vlc_object_t *p_this, const char *,
252 vlc_value_t, vlc_value_t newval, void *param )
254 InputManager *im = (InputManager*)param;
257 switch( newval.i_int )
259 case INPUT_EVENT_STATE:
260 event = new IMEvent( ItemStateChanged_Type, 0 );
262 case INPUT_EVENT_RATE:
263 event = new IMEvent( ItemRateChanged_Type, 0 );
265 case INPUT_EVENT_TIMES:
266 event = new IMEvent( PositionUpdate_Type, 0 );
269 case INPUT_EVENT_TITLE:
270 case INPUT_EVENT_CHAPTER:
271 event = new IMEvent( ItemTitleChanged_Type, 0 );
275 event = new IMEvent( ItemEsChanged_Type, 0 );
277 case INPUT_EVENT_TELETEXT:
278 event = new IMEvent( ItemTeletextChanged_Type, 0 );
281 case INPUT_EVENT_STATISTICS:
282 event = new IMEvent( StatisticsUpdate_Type, 0 );
285 case INPUT_EVENT_VOUT:
286 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
288 case INPUT_EVENT_AOUT:
289 event = new IMEvent( InterfaceAoutUpdate_Type, 0 );
292 case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
293 event = new IMEvent( MetaChanged_Type, 0 );
295 case INPUT_EVENT_ITEM_INFO: /* Codec Info */
296 event = new IMEvent( InfoChanged_Type, 0 );
298 case INPUT_EVENT_ITEM_NAME:
299 event = new IMEvent( NameChanged_Type, 0 );
302 case INPUT_EVENT_AUDIO_DELAY:
303 case INPUT_EVENT_SUBTITLE_DELAY:
304 event = new IMEvent( SynchroChanged_Type, 0 );
307 case INPUT_EVENT_CACHE:
308 event = new IMEvent( CachingEvent_Type, 0 );
311 case INPUT_EVENT_BOOKMARK:
312 event = new IMEvent( BookmarksChanged_Type, 0 );
315 case INPUT_EVENT_RECORD:
316 event = new IMEvent( RecordingEvent_Type, 0 );
319 case INPUT_EVENT_PROGRAM:
320 /* This is for PID changes */
321 /* event = new IMEvent( ProgramChanged_Type, 0 );
323 case INPUT_EVENT_SIGNAL:
324 /* This is for capture-card signals */
325 /* event = new IMEvent( SignalChanged_Type, 0 );
333 QApplication::postEvent( im, event );
336 void InputManager::UpdatePosition()
338 /* Update position */
339 int i_length, i_time; /* Int is enough, since we store seconds */
341 i_length = var_GetTime( p_input , "length" ) / 1000000;
342 i_time = var_GetTime( p_input , "time") / 1000000;
343 f_pos = var_GetFloat( p_input , "position" );
344 emit positionUpdated( f_pos, i_time, i_length );
347 void InputManager::UpdateNavigation()
349 /* Update navigation status */
350 vlc_value_t val; val.i_int = 0;
353 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
357 emit titleChanged( true );
358 msg_Dbg( p_intf, "Title %i", val.i_int );
359 /* p_input != NULL since val.i_int != 0 */
361 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
362 emit chapterChanged( (val.i_int > 0) );
363 msg_Dbg( p_intf, "Chapter: %i", val.i_int );
366 emit titleChanged( false );
369 void InputManager::UpdateStatus()
371 /* Update playing status */
372 vlc_value_t val; val.i_int = 0;
373 var_Get( p_input, "state", &val );
374 if( i_old_playing_status != val.i_int )
376 i_old_playing_status = val.i_int;
377 emit statusChanged( val.i_int );
381 void InputManager::UpdateRate()
384 int i_new_rate = var_GetInteger( p_input, "rate");
385 if( i_new_rate != i_rate )
389 emit rateChanged( i_rate );
393 void InputManager::UpdateName()
395 /* Update text, name and nowplaying */
398 /* Try to get the Title, then the Name */
399 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
400 if( EMPTY_STR( psz_name ) )
403 psz_name = input_item_GetName( input_GetItem( p_input ) );
406 /* Try to get the nowplaying */
407 char *psz_nowplaying =
408 input_item_GetNowPlaying( input_GetItem( p_input ) );
409 if( !EMPTY_STR( psz_nowplaying ) )
411 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
413 else /* Do it ourself */
415 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
417 if( !EMPTY_STR( psz_artist ) )
418 text.sprintf( "%s - %s", psz_artist, psz_name );
420 text.sprintf( "%s", psz_name );
424 /* Free everything */
426 free( psz_nowplaying );
428 /* If we have Nothing */
431 psz_name = input_item_GetURI( input_GetItem( p_input ) );
432 text.sprintf( "%s", psz_name );
433 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
437 if( oldName != text )
439 emit nameChanged( text );
444 bool InputManager::hasAudio()
449 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
450 return val.i_int > 0;
455 void InputManager::UpdateTeletext()
458 telexActivation( var_GetInteger( p_input, "teletext-es" ) >= 0 );
460 telexActivation( false );
463 void InputManager::UpdateVout()
467 /* Get current vout lists from input */
469 vout_thread_t **pp_vout;
470 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
477 emit voutListChanged( pp_vout, i_vout );
480 bool b_old_video = b_video;
481 b_video = i_vout > 0;
482 if( !!b_old_video != !!b_video )
483 emit voutChanged( b_video );
485 /* Release the vout list */
486 for( int i = 0; i < i_vout; i++ )
487 vlc_object_release( (vlc_object_t*)pp_vout[i] );
491 void InputManager::UpdateAout()
498 void InputManager::UpdateCaching()
500 if(!hasInput()) return;
502 float f_newCache = var_GetFloat ( p_input, "cache" );
503 if( f_newCache != f_cache )
505 f_cache = f_newCache;
507 emit cachingChanged( f_cache );
511 void InputManager::requestArtUpdate()
515 playlist_t *p_playlist = pl_Hold( p_intf );
516 playlist_AskForArtEnqueue( p_playlist, input_GetItem( p_input ), pl_Unlocked );
517 pl_Release( p_intf );
521 /* No input will signal the cover art to update,
522 * let's do it ourself */
527 void InputManager::UpdateArt()
533 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
537 url = url.replace( "file://", QString("" ) );
538 /* Taglib seems to define a attachment://, It won't work yet */
539 url = url.replace( "attachment://", QString("" ) );
540 /* Update Art meta */
541 emit artChanged( url );
544 inline void InputManager::UpdateStats()
546 emit statisticsUpdated( input_GetItem( p_input ) );
549 inline void InputManager::UpdateMeta()
551 emit metaChanged( input_GetItem( p_input ) );
554 inline void InputManager::UpdateInfo()
556 emit infoChanged( input_GetItem( p_input ) );
559 void InputManager::UpdateRecord()
563 emit recordingStateChanged( var_GetBool( p_input, "record" ) );
567 /* User update of the slider */
568 void InputManager::sliderUpdate( float new_pos )
571 var_SetFloat( p_input, "position", new_pos );
574 /* User togglePlayPause */
575 void InputManager::togglePlayPause()
580 var_Get( p_input, "state", &state );
581 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
582 var_Set( p_input, "state", state );
583 emit statusChanged( state.i_int );
587 void InputManager::sectionPrev()
591 int i_type = var_Type( p_input, "next-chapter" );
592 vlc_value_t val; val.b_bool = true;
593 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
594 "prev-chapter":"prev-title", val );
598 void InputManager::sectionNext()
602 int i_type = var_Type( p_input, "next-chapter" );
603 vlc_value_t val; val.b_bool = true;
604 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
605 "next-chapter":"next-title", val );
609 void InputManager::sectionMenu()
613 vlc_value_t val, text;
616 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
619 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
621 for( int i = 0; i < val.p_list->i_count; i++ )
623 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
626 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
628 var_Set( p_input, "title 0", root );
636 /* Set a new Teletext Page */
637 void InputManager::telexSetPage( int page )
641 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
642 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
644 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
646 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
647 "zvbi", FIND_ANYWHERE );
650 var_SetInteger( p_vbi, "vbi-page", page );
651 vlc_object_release( p_vbi );
652 emit newTelexPageSet( page );
658 /* Set the transparency on teletext */
659 void InputManager::telexSetTransparency( bool b_transparentTelextext )
663 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
664 "zvbi", FIND_ANYWHERE );
667 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
668 vlc_object_release( p_vbi );
669 emit teletextTransparencyActivated( b_transparentTelextext );
674 void InputManager::telexActivation( bool b_enabled )
678 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
679 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
681 /* Teletext is possible. Show the buttons */
682 b_enabled = (i_teletext_es >= 0);
683 emit teletextPossible( b_enabled );
684 if( !b_enabled ) return;
686 /* If Teletext is selected */
687 if( i_teletext_es == i_spu_es )
689 /* Activate the buttons */
690 teletextActivated( true );
692 /* Then, find the current page */
694 vlc_object_t *p_vbi = (vlc_object_t *)
695 vlc_object_find_name( p_input, "zvbi", FIND_ANYWHERE );
698 i_page = var_GetInteger( p_vbi, "vbi-page" );
699 vlc_object_release( p_vbi );
700 emit newTelexPageSet( i_page );
705 emit teletextPossible( b_enabled );
708 void InputManager::activateTeletext( bool b_enable )
712 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
713 if( i_teletext_es >= 0 )
715 var_SetInteger( p_input, "spu-es", b_enable ? i_teletext_es : -1 );
720 void InputManager::reverse()
724 int i_rate = var_GetInteger( p_input, "rate" );
725 var_SetInteger( p_input, "rate", -i_rate );
729 void InputManager::slower()
732 var_SetVoid( p_input, "rate-slower" );
735 void InputManager::faster()
738 var_SetVoid( p_input, "rate-faster" );
741 void InputManager::normalRate()
744 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
747 void InputManager::setRate( int new_rate )
750 var_SetInteger( p_input, "rate", new_rate );
753 void InputManager::setAtoB()
757 timeA = var_GetTime( THEMIM->getInput(), "time" );
761 timeB = var_GetTime( THEMIM->getInput(), "time" );
762 var_SetTime( THEMIM->getInput(), "time" , timeA );
769 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
772 /* Function called regularly when in an AtoB loop */
773 void InputManager::AtoBLoop( int i_time )
777 if( ( i_time >= (int)( timeB/1000000 ) )
778 || ( i_time < (int)( timeA/1000000 ) ) )
779 var_SetTime( THEMIM->getInput(), "time" , timeA );
783 /**********************************************************************
784 * MainInputManager implementation. Wrap an input manager and
785 * take care of updating the main playlist input.
786 * Used in the main playlist Dialog
787 **********************************************************************/
788 MainInputManager * MainInputManager::instance = NULL;
790 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
791 : QObject(NULL), p_intf( _p_intf )
794 im = new InputManager( this, p_intf );
796 var_AddCallback( THEPL, "item-change", ItemChanged, im );
797 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
798 var_AddCallback( THEPL, "activity", PLItemChanged, this );
800 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
802 /* Warn our embedded IM about input changes */
803 CONNECT( this, inputChanged( input_thread_t * ),
804 im, setInput( input_thread_t * ) );
806 /* emit check if playlist has allready started playing */
808 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
810 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
811 customEvent( event );
815 MainInputManager::~MainInputManager()
819 emit inputChanged( NULL );
820 var_DelCallback( p_input, "state", PLItemChanged, this );
821 vlc_object_release( p_input );
824 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
826 var_DelCallback( THEPL, "activity", PLItemChanged, this );
827 var_DelCallback( THEPL, "item-change", ItemChanged, im );
829 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
832 vout_thread_t* MainInputManager::getVout()
834 return p_input ? input_GetVout( p_input ) : NULL;
837 aout_instance_t * MainInputManager::getAout()
839 return p_input ? input_GetAout( p_input ) : NULL;
842 void MainInputManager::customEvent( QEvent *event )
844 int type = event->type();
845 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
848 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
849 if( type == VolumeChanged_Type )
851 emit volumeChanged();
855 /* Should be PLItemChanged Event */
856 if( !p_intf->p_sys->b_isDialogProvider )
858 vlc_mutex_lock( &p_intf->change_lock );
859 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
861 emit inputChanged( NULL );
862 var_DelCallback( p_input, "state", PLItemChanged, this );
863 vlc_object_release( p_input );
865 vlc_mutex_unlock( &p_intf->change_lock );
871 p_input = playlist_CurrentInput(THEPL);
874 var_AddCallback( p_input, "state", PLItemChanged, this );
875 emit inputChanged( p_input );
878 vlc_mutex_unlock( &p_intf->change_lock );
882 /* we are working as a dialogs provider */
883 playlist_t *p_playlist = pl_Hold( p_intf );
884 p_input = playlist_CurrentInput( p_playlist );
887 emit inputChanged( p_input );
888 vlc_object_release( p_input );
890 pl_Release( p_intf );
894 /* Playlist Control functions */
895 void MainInputManager::stop()
897 playlist_Stop( THEPL );
900 void MainInputManager::next()
902 playlist_Next( THEPL );
905 void MainInputManager::prev()
907 playlist_Prev( THEPL );
910 void MainInputManager::togglePlayPause()
914 playlist_Play( THEPL );
916 getIM()->togglePlayPause();
919 /* Static callbacks for MIM */
920 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
921 vlc_value_t oldval, vlc_value_t newval, void *param )
923 MainInputManager *mim = (MainInputManager*)param;
925 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
926 QApplication::postEvent( mim, event );
930 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
931 vlc_value_t oldval, vlc_value_t newval, void *param )
933 MainInputManager *mim = (MainInputManager*)param;
935 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
936 QApplication::postEvent( mim, event );