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 * );
45 static int VbiEvent( vlc_object_t *, const char *,
46 vlc_value_t, vlc_value_t, void * );
49 /**********************************************************************
50 * InputManager implementation
51 **********************************************************************
52 * The Input Manager can be the main one around the playlist
53 * But can also be used for VLM dialog or similar
54 **********************************************************************/
56 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
57 QObject( parent ), p_intf( _p_intf )
59 i_old_playing_status = END_S;
68 f_cache = -1.; /* impossible initial value, different from all */
71 InputManager::~InputManager()
76 /* Define the Input used.
77 Add the callbacks on input
78 p_input is held once here */
79 void InputManager::setInput( input_thread_t *_p_input )
83 if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
85 msg_Dbg( p_intf, "IM: Setting an input" );
86 vlc_object_hold( p_input );
87 emit statusChanged( PLAYING_S );
94 i_input_id = input_GetItem( p_input )->i_id;
100 emit rateChanged( INPUT_RATE_DEFAULT );
104 /* delete Input if it ever existed.
105 Delete the callbacls on input
106 p_input is released once here */
107 void InputManager::delInput()
109 if( !p_input ) return;
110 msg_Dbg( p_intf, "IM: Deleting the input" );
113 i_old_playing_status = END_S;
121 vlc_object_release( p_input );
124 emit positionUpdated( -1.0, 0 ,0 );
125 emit rateChanged( INPUT_RATE_DEFAULT ); /* TODO: Do we want this ? */
126 emit nameChanged( "" );
127 emit chapterChanged( 0 );
128 emit titleChanged( 0 );
129 emit statusChanged( END_S );
131 emit teletextPossible( false );
132 emit AtoBchanged( false, false );
133 emit voutChanged( false );
134 emit voutListChanged( NULL, 0 );
136 /* Reset all InfoPanels but stats */
137 emit artChanged( NULL );
138 emit infoChanged( NULL );
139 emit metaChanged( NULL );
142 /* Convert the event from the callbacks in actions */
143 void InputManager::customEvent( QEvent *event )
145 int i_type = event->type();
146 IMEvent *ple = static_cast<IMEvent *>(event);
152 if( i_type != PositionUpdate_Type &&
153 i_type != StatisticsUpdate_Type &&
154 i_type != ItemChanged_Type )
155 msg_Dbg( p_intf, "New Event: type %i", i_type );
161 case PositionUpdate_Type:
164 case StatisticsUpdate_Type:
167 case ItemChanged_Type:
168 /* Ignore ItemChanged_Type event that does not apply to our input */
169 if( i_input_id == ple->i_id )
176 case ItemStateChanged_Type:
177 // TODO: Fusion with above state
180 // UpdateNavigation(); This shouldn't be useful now
181 // UpdateTeletext(); Same
183 case NameChanged_Type:
186 case MetaChanged_Type:
188 UpdateName(); /* Needed for NowPlaying */
189 UpdateArt(); /* Art is part of meta in the core */
191 case InfoChanged_Type:
194 case ItemTitleChanged_Type:
196 UpdateName(); /* Display the name of the Chapter, if exists */
198 case ItemRateChanged_Type:
201 case ItemEsChanged_Type:
203 // We don't do anything ES related. Why ?
205 case ItemTeletextChanged_Type:
208 case InterfaceVoutUpdate_Type:
211 case SynchroChanged_Type:
212 emit synchroChanged();
214 case CachingEvent_Type:
217 case BookmarksChanged_Type:
218 emit bookmarksChanged();
220 case InterfaceAoutUpdate_Type:
223 case RecordingEvent_Type:
227 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
232 /* Add the callbacks on Input. Self explanatory */
233 inline void InputManager::addCallbacks()
235 var_AddCallback( p_input, "intf-event", InputEvent, this );
238 /* Delete the callbacks on Input. Self explanatory */
239 inline void InputManager::delCallbacks()
241 var_DelCallback( p_input, "intf-event", InputEvent, this );
244 /* Static callbacks for IM */
245 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
246 vlc_value_t oldval, vlc_value_t newval, void *param )
248 InputManager *im = (InputManager*)param;
250 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
251 QApplication::postEvent( im, event );
255 static int InputEvent( vlc_object_t *p_this, const char *,
256 vlc_value_t, vlc_value_t newval, void *param )
258 InputManager *im = (InputManager*)param;
261 switch( newval.i_int )
263 case INPUT_EVENT_STATE:
264 event = new IMEvent( ItemStateChanged_Type, 0 );
266 case INPUT_EVENT_RATE:
267 event = new IMEvent( ItemRateChanged_Type, 0 );
269 case INPUT_EVENT_TIMES:
270 event = new IMEvent( PositionUpdate_Type, 0 );
273 case INPUT_EVENT_TITLE:
274 case INPUT_EVENT_CHAPTER:
275 event = new IMEvent( ItemTitleChanged_Type, 0 );
279 event = new IMEvent( ItemEsChanged_Type, 0 );
281 case INPUT_EVENT_TELETEXT:
282 event = new IMEvent( ItemTeletextChanged_Type, 0 );
285 case INPUT_EVENT_STATISTICS:
286 event = new IMEvent( StatisticsUpdate_Type, 0 );
289 case INPUT_EVENT_VOUT:
290 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
292 case INPUT_EVENT_AOUT:
293 event = new IMEvent( InterfaceAoutUpdate_Type, 0 );
296 case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
297 event = new IMEvent( MetaChanged_Type, 0 );
299 case INPUT_EVENT_ITEM_INFO: /* Codec Info */
300 event = new IMEvent( InfoChanged_Type, 0 );
302 case INPUT_EVENT_ITEM_NAME:
303 event = new IMEvent( NameChanged_Type, 0 );
306 case INPUT_EVENT_AUDIO_DELAY:
307 case INPUT_EVENT_SUBTITLE_DELAY:
308 event = new IMEvent( SynchroChanged_Type, 0 );
311 case INPUT_EVENT_CACHE:
312 event = new IMEvent( CachingEvent_Type, 0 );
315 case INPUT_EVENT_BOOKMARK:
316 event = new IMEvent( BookmarksChanged_Type, 0 );
319 case INPUT_EVENT_RECORD:
320 event = new IMEvent( RecordingEvent_Type, 0 );
323 case INPUT_EVENT_PROGRAM:
324 /* This is for PID changes */
325 /* event = new IMEvent( ProgramChanged_Type, 0 );
327 case INPUT_EVENT_SIGNAL:
328 /* This is for capture-card signals */
329 /* event = new IMEvent( SignalChanged_Type, 0 );
337 QApplication::postEvent( im, event );
340 static int VbiEvent( vlc_object_t *, const char *,
341 vlc_value_t, vlc_value_t, void *param )
343 InputManager *im = (InputManager*)param;
344 IMEvent *event = new IMEvent( ItemTeletextChanged_Type, 0 );
347 QApplication::postEvent( im, event );
351 void InputManager::UpdatePosition()
353 /* Update position */
354 int i_length, i_time; /* Int is enough, since we store seconds */
356 i_length = var_GetTime( p_input , "length" ) / 1000000;
357 i_time = var_GetTime( p_input , "time") / 1000000;
358 f_pos = var_GetFloat( p_input , "position" );
359 emit positionUpdated( f_pos, i_time, i_length );
362 void InputManager::UpdateNavigation()
364 /* Update navigation status */
365 vlc_value_t val; val.i_int = 0;
368 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
372 emit titleChanged( true );
373 msg_Dbg( p_intf, "Title %i", val.i_int );
374 /* p_input != NULL since val.i_int != 0 */
376 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
377 emit chapterChanged( (val.i_int > 0) );
378 msg_Dbg( p_intf, "Chapter: %i", val.i_int );
381 emit titleChanged( false );
384 void InputManager::UpdateStatus()
386 /* Update playing status */
387 vlc_value_t val; val.i_int = 0;
388 var_Get( p_input, "state", &val );
389 if( i_old_playing_status != val.i_int )
391 i_old_playing_status = val.i_int;
392 emit statusChanged( val.i_int );
396 void InputManager::UpdateRate()
399 int i_new_rate = var_GetInteger( p_input, "rate");
400 if( i_new_rate != i_rate )
404 emit rateChanged( i_rate );
408 void InputManager::UpdateName()
410 /* Update text, name and nowplaying */
413 /* Try to get the Title, then the Name */
414 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
415 if( EMPTY_STR( psz_name ) )
418 psz_name = input_item_GetName( input_GetItem( p_input ) );
421 /* Try to get the nowplaying */
422 char *psz_nowplaying =
423 input_item_GetNowPlaying( input_GetItem( p_input ) );
424 if( !EMPTY_STR( psz_nowplaying ) )
426 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
428 else /* Do it ourself */
430 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
432 if( !EMPTY_STR( psz_artist ) )
433 text.sprintf( "%s - %s", psz_artist, psz_name );
435 text.sprintf( "%s", psz_name );
439 /* Free everything */
441 free( psz_nowplaying );
443 /* If we have Nothing */
446 psz_name = input_item_GetURI( input_GetItem( p_input ) );
447 text.sprintf( "%s", psz_name );
448 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
452 if( oldName != text )
454 emit nameChanged( text );
459 bool InputManager::hasAudio()
464 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
465 return val.i_int > 0;
470 void InputManager::UpdateTeletext()
474 const bool b_enabled = var_CountChoices( p_input, "teletext-es" ) > 0;
475 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
477 /* Teletext is possible. Show the buttons */
478 emit teletextPossible( b_enabled );
480 /* If Teletext is selected */
481 if( b_enabled && i_teletext_es >= 0 )
483 /* Then, find the current page */
485 bool b_transparent = false;
487 vlc_object_t *p_vbi = (vlc_object_t *)
488 vlc_object_find_name( p_input, "zvbi", FIND_CHILD );
492 /* We deleted it (if not here, it does not harm), because
493 * var_AddCallback will silently add a duplicated one */
494 var_DelCallback( p_vbi, "vbi-page", VbiEvent, this );
495 /* This callback is not remove explicitly, but interfaces
496 * are guaranted to outlive input */
497 var_AddCallback( p_vbi, "vbi-page", VbiEvent, this );
499 i_page = var_GetInteger( p_vbi, "vbi-page" );
500 b_transparent = !var_GetBool( p_vbi, "vbi-opaque" );
501 vlc_object_release( p_vbi );
503 emit newTelexPageSet( i_page );
504 emit teletextTransparencyActivated( b_transparent );
507 emit teletextActivated( b_enabled && i_teletext_es >= 0 );
511 emit teletextActivated( false );
512 emit teletextPossible( false );
516 void InputManager::UpdateVout()
520 /* Get current vout lists from input */
522 vout_thread_t **pp_vout;
523 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
530 emit voutListChanged( pp_vout, i_vout );
533 bool b_old_video = b_video;
534 b_video = i_vout > 0;
535 if( !!b_old_video != !!b_video )
536 emit voutChanged( b_video );
538 /* Release the vout list */
539 for( int i = 0; i < i_vout; i++ )
540 vlc_object_release( (vlc_object_t*)pp_vout[i] );
544 void InputManager::UpdateAout()
551 void InputManager::UpdateCaching()
553 if(!hasInput()) return;
555 float f_newCache = var_GetFloat ( p_input, "cache" );
556 if( f_newCache != f_cache )
558 f_cache = f_newCache;
560 emit cachingChanged( f_cache );
564 void InputManager::requestArtUpdate()
568 playlist_t *p_playlist = pl_Hold( p_intf );
569 playlist_AskForArtEnqueue( p_playlist, input_GetItem( p_input ), pl_Unlocked );
570 pl_Release( p_intf );
574 /* No input will signal the cover art to update,
575 * let's do it ourself */
580 void InputManager::UpdateArt()
586 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
590 url = url.replace( "file://", QString("" ) );
591 /* Taglib seems to define a attachment://, It won't work yet */
592 url = url.replace( "attachment://", QString("" ) );
593 /* Update Art meta */
594 emit artChanged( url );
597 inline void InputManager::UpdateStats()
599 emit statisticsUpdated( input_GetItem( p_input ) );
602 inline void InputManager::UpdateMeta()
604 emit metaChanged( input_GetItem( p_input ) );
607 inline void InputManager::UpdateInfo()
609 emit infoChanged( input_GetItem( p_input ) );
612 void InputManager::UpdateRecord()
616 emit recordingStateChanged( var_GetBool( p_input, "record" ) );
620 /* User update of the slider */
621 void InputManager::sliderUpdate( float new_pos )
624 var_SetFloat( p_input, "position", new_pos );
627 /* User togglePlayPause */
628 void InputManager::togglePlayPause()
633 var_Get( p_input, "state", &state );
634 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
635 var_Set( p_input, "state", state );
636 emit statusChanged( state.i_int );
640 void InputManager::sectionPrev()
644 int i_type = var_Type( p_input, "next-chapter" );
645 vlc_value_t val; val.b_bool = true;
646 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
647 "prev-chapter":"prev-title", val );
651 void InputManager::sectionNext()
655 int i_type = var_Type( p_input, "next-chapter" );
656 vlc_value_t val; val.b_bool = true;
657 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
658 "next-chapter":"next-title", val );
662 void InputManager::sectionMenu()
666 vlc_value_t val, text;
669 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
672 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
674 for( int i = 0; i < val.p_list->i_count; i++ )
676 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
679 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
681 var_Set( p_input, "title 0", root );
689 /* Set a new Teletext Page */
690 void InputManager::telexSetPage( int page )
694 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
696 if( i_teletext_es >= 0 )
698 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
699 "zvbi", FIND_CHILD );
702 var_SetInteger( p_vbi, "vbi-page", page );
703 vlc_object_release( p_vbi );
704 emit newTelexPageSet( page );
710 /* Set the transparency on teletext */
711 void InputManager::telexSetTransparency( bool b_transparentTelextext )
715 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
716 "zvbi", FIND_CHILD );
719 var_SetBool( p_vbi, "vbi-opaque", !b_transparentTelextext );
720 vlc_object_release( p_vbi );
721 emit teletextTransparencyActivated( b_transparentTelextext );
726 void InputManager::activateTeletext( bool b_enable )
730 if( hasInput() && !var_Change( p_input, "teletext-es", VLC_VAR_GETLIST, &list, &text ) )
732 if( list.p_list->i_count > 0 )
734 /* Prefer the page 100 if it is present */
736 for( i = 0; i < text.p_list->i_count; i++ )
738 /* The description is the page number as a string */
739 const char *psz_page = text.p_list->p_values[i].psz_string;
740 if( psz_page && !strcmp( psz_page, "100" ) )
743 if( i >= list.p_list->i_count )
745 var_SetInteger( p_input, "spu-es", b_enable ? list.p_list->p_values[i].i_int : -1 );
747 var_Change( p_input, "teletext-es", VLC_VAR_FREELIST, &list, &text );
751 void InputManager::reverse()
755 int i_rate = var_GetInteger( p_input, "rate" );
756 var_SetInteger( p_input, "rate", -i_rate );
760 void InputManager::slower()
763 var_SetVoid( p_input, "rate-slower" );
766 void InputManager::faster()
769 var_SetVoid( p_input, "rate-faster" );
772 void InputManager::normalRate()
775 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
778 void InputManager::setRate( int new_rate )
781 var_SetInteger( p_input, "rate", new_rate );
784 void InputManager::setAtoB()
788 timeA = var_GetTime( THEMIM->getInput(), "time" );
792 timeB = var_GetTime( THEMIM->getInput(), "time" );
793 var_SetTime( THEMIM->getInput(), "time" , timeA );
800 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
803 /* Function called regularly when in an AtoB loop */
804 void InputManager::AtoBLoop( int i_time )
808 if( ( i_time >= (int)( timeB/1000000 ) )
809 || ( i_time < (int)( timeA/1000000 ) ) )
810 var_SetTime( THEMIM->getInput(), "time" , timeA );
814 /**********************************************************************
815 * MainInputManager implementation. Wrap an input manager and
816 * take care of updating the main playlist input.
817 * Used in the main playlist Dialog
818 **********************************************************************/
819 MainInputManager * MainInputManager::instance = NULL;
821 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
822 : QObject(NULL), p_intf( _p_intf )
825 im = new InputManager( this, p_intf );
827 var_AddCallback( THEPL, "item-change", ItemChanged, im );
828 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
829 var_AddCallback( THEPL, "activity", PLItemChanged, this );
831 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
833 /* Warn our embedded IM about input changes */
834 CONNECT( this, inputChanged( input_thread_t * ),
835 im, setInput( input_thread_t * ) );
837 /* emit check if playlist has allready started playing */
839 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
841 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
842 customEvent( event );
846 MainInputManager::~MainInputManager()
850 emit inputChanged( NULL );
851 var_DelCallback( p_input, "state", PLItemChanged, this );
852 vlc_object_release( p_input );
855 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
857 var_DelCallback( THEPL, "activity", PLItemChanged, this );
858 var_DelCallback( THEPL, "item-change", ItemChanged, im );
860 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
863 vout_thread_t* MainInputManager::getVout()
865 return p_input ? input_GetVout( p_input ) : NULL;
868 aout_instance_t * MainInputManager::getAout()
870 return p_input ? input_GetAout( p_input ) : NULL;
873 void MainInputManager::customEvent( QEvent *event )
875 int type = event->type();
876 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
879 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
880 if( type == VolumeChanged_Type )
882 emit volumeChanged();
886 /* Should be PLItemChanged Event */
887 if( !p_intf->p_sys->b_isDialogProvider )
889 vlc_mutex_lock( &p_intf->change_lock );
890 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
892 emit inputChanged( NULL );
893 var_DelCallback( p_input, "state", PLItemChanged, this );
894 vlc_object_release( p_input );
896 vlc_mutex_unlock( &p_intf->change_lock );
902 p_input = playlist_CurrentInput(THEPL);
905 var_AddCallback( p_input, "state", PLItemChanged, this );
906 emit inputChanged( p_input );
909 vlc_mutex_unlock( &p_intf->change_lock );
913 /* we are working as a dialogs provider */
914 playlist_t *p_playlist = pl_Hold( p_intf );
915 p_input = playlist_CurrentInput( p_playlist );
918 emit inputChanged( p_input );
919 vlc_object_release( p_input );
921 pl_Release( p_intf );
925 /* Playlist Control functions */
926 void MainInputManager::stop()
928 playlist_Stop( THEPL );
931 void MainInputManager::next()
933 playlist_Next( THEPL );
936 void MainInputManager::prev()
938 playlist_Prev( THEPL );
941 void MainInputManager::togglePlayPause()
945 playlist_Play( THEPL );
947 getIM()->togglePlayPause();
950 /* Static callbacks for MIM */
951 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
952 vlc_value_t oldval, vlc_value_t newval, void *param )
954 MainInputManager *mim = (MainInputManager*)param;
956 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
957 QApplication::postEvent( mim, event );
961 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
962 vlc_value_t oldval, vlc_value_t newval, void *param )
964 MainInputManager *mim = (MainInputManager*)param;
966 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
967 QApplication::postEvent( mim, event );