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 );
130 emit voutListChanged( NULL, 0 );
132 /* Reset all InfoPanels but stats */
133 emit artChanged( NULL );
134 emit infoChanged( NULL );
135 emit metaChanged( NULL );
138 /* Convert the event from the callbacks in actions */
139 void InputManager::customEvent( QEvent *event )
141 int i_type = event->type();
142 IMEvent *ple = static_cast<IMEvent *>(event);
144 if ( i_type != PositionUpdate_Type &&
145 i_type != ItemChanged_Type &&
146 i_type != ItemRateChanged_Type &&
147 i_type != ItemTitleChanged_Type &&
148 i_type != ItemEsChanged_Type &&
149 i_type != ItemTeletextChanged_Type &&
150 i_type != ItemStateChanged_Type &&
151 i_type != StatisticsUpdate_Type &&
152 i_type != InterfaceVoutUpdate_Type &&
153 i_type != MetaChanged_Type &&
154 i_type != NameChanged_Type &&
155 i_type != InfoChanged_Type &&
156 i_type != SynchroChanged_Type &&
157 i_type != CachingEvent_Type &&
158 i_type != BookmarksChanged_Type &&
159 i_type != InterfaceAoutUpdate_Type )
162 if( !hasInput() ) return;
164 if( i_type == CachingEvent_Type )
167 if( ( i_type != PositionUpdate_Type &&
168 i_type != ItemRateChanged_Type &&
169 i_type != ItemEsChanged_Type &&
170 i_type != ItemTeletextChanged_Type &&
171 i_type != ItemStateChanged_Type &&
172 i_type != StatisticsUpdate_Type &&
173 i_type != InterfaceVoutUpdate_Type &&
174 i_type != MetaChanged_Type &&
175 i_type != NameChanged_Type &&
176 i_type != InfoChanged_Type &&
177 i_type != SynchroChanged_Type &&
178 i_type != BookmarksChanged_Type &&
179 i_type != InterfaceAoutUpdate_Type
181 && ( i_input_id != ple->i_id ) )
185 if( i_type != PositionUpdate_Type &&
186 i_type != StatisticsUpdate_Type )
187 msg_Dbg( p_intf, "New Event: type %i", i_type );
193 case PositionUpdate_Type:
196 case StatisticsUpdate_Type:
199 case ItemChanged_Type:
204 case ItemStateChanged_Type:
205 // TODO: Fusion with above state
208 // UpdateNavigation(); This shouldn't be useful now
209 // UpdateTeletext(); Same
211 case NameChanged_Type:
214 case MetaChanged_Type:
216 UpdateName(); /* Needed for NowPlaying */
217 UpdateArt(); /* Art is part of meta in the core */
219 case InfoChanged_Type:
222 case ItemTitleChanged_Type:
224 UpdateName(); /* Display the name of the Chapter, if exists */
226 case ItemRateChanged_Type:
229 case ItemEsChanged_Type:
230 // We don't do anything. Why ?
232 case ItemTeletextChanged_Type:
235 case InterfaceVoutUpdate_Type:
238 case SynchroChanged_Type:
239 emit synchroChanged();
241 case CachingEvent_Type:
244 case BookmarksChanged_Type:
245 emit bookmarksChanged();
247 case InterfaceAoutUpdate_Type:
251 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
255 /* Add the callbacks on Input. Self explanatory */
256 inline void InputManager::addCallbacks()
258 var_AddCallback( p_input, "intf-event", InputEvent, this );
261 /* Delete the callbacks on Input. Self explanatory */
262 inline void InputManager::delCallbacks()
264 var_DelCallback( p_input, "intf-event", InputEvent, this );
267 /* Static callbacks for IM */
268 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
269 vlc_value_t oldval, vlc_value_t newval, void *param )
271 InputManager *im = (InputManager*)param;
273 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
274 QApplication::postEvent( im, static_cast<QEvent*>(event) );
278 static int InputEvent( vlc_object_t *p_this, const char *,
279 vlc_value_t, vlc_value_t newval, void *param )
281 InputManager *im = (InputManager*)param;
284 switch( newval.i_int )
286 case INPUT_EVENT_STATE:
287 event = new IMEvent( ItemStateChanged_Type, 0 );
289 case INPUT_EVENT_RATE:
290 event = new IMEvent( ItemRateChanged_Type, 0 );
292 case INPUT_EVENT_TIMES:
293 event = new IMEvent( PositionUpdate_Type, 0 );
296 case INPUT_EVENT_TITLE:
297 case INPUT_EVENT_CHAPTER:
298 event = new IMEvent( ItemTitleChanged_Type, 0 );
302 event = new IMEvent( ItemEsChanged_Type, 0 );
304 case INPUT_EVENT_TELETEXT:
305 event = new IMEvent( ItemTeletextChanged_Type, 0 );
308 case INPUT_EVENT_STATISTICS:
309 event = new IMEvent( StatisticsUpdate_Type, 0 );
312 case INPUT_EVENT_VOUT:
313 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
315 case INPUT_EVENT_AOUT:
316 event = new IMEvent( InterfaceAoutUpdate_Type, 0 );
319 case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
320 event = new IMEvent( MetaChanged_Type, 0 );
322 case INPUT_EVENT_ITEM_INFO: /* Codec Info */
323 event = new IMEvent( InfoChanged_Type, 0 );
325 case INPUT_EVENT_ITEM_NAME:
326 event = new IMEvent( NameChanged_Type, 0 );
329 case INPUT_EVENT_AUDIO_DELAY:
330 case INPUT_EVENT_SUBTITLE_DELAY:
331 event = new IMEvent( SynchroChanged_Type, 0 );
334 case INPUT_EVENT_CACHE:
335 event = new IMEvent( CachingEvent_Type, 0 );
338 case INPUT_EVENT_BOOKMARK:
339 event = new IMEvent( BookmarksChanged_Type, 0 );
342 case INPUT_EVENT_RECORD:
343 /* This happens when a recording starts. What do we do then?
344 Display a red light? */
345 /* event = new IMEvent( RecordingEvent_Type, 0 );
348 case INPUT_EVENT_PROGRAM:
349 /* This is for PID changes */
350 /* event = new IMEvent( ProgramChanged_Type, 0 );
352 case INPUT_EVENT_SIGNAL:
353 /* This is for capture-card signals */
354 /* event = new IMEvent( SignalChanged_Type, 0 );
362 QApplication::postEvent( im, static_cast<QEvent*>(event) );
365 void InputManager::UpdatePosition()
367 /* Update position */
368 int i_length, i_time; /* Int is enough, since we store seconds */
370 i_length = var_GetTime( p_input , "length" ) / 1000000;
371 i_time = var_GetTime( p_input , "time") / 1000000;
372 f_pos = var_GetFloat( p_input , "position" );
373 emit positionUpdated( f_pos, i_time, i_length );
376 void InputManager::UpdateNavigation()
378 /* Update navigation status */
379 vlc_value_t val; val.i_int = 0;
382 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
386 emit titleChanged( true );
387 msg_Dbg( p_intf, "Title %i", val.i_int );
388 /* p_input != NULL since val.i_int != 0 */
390 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
391 emit chapterChanged( (val.i_int > 0) );
392 msg_Dbg( p_intf, "Chapter: %i", val.i_int );
395 emit titleChanged( false );
398 void InputManager::UpdateStatus()
400 /* Update playing status */
401 vlc_value_t val; val.i_int = 0;
402 var_Get( p_input, "state", &val );
403 if( i_old_playing_status != val.i_int )
405 i_old_playing_status = val.i_int;
406 emit statusChanged( val.i_int );
410 void InputManager::UpdateRate()
413 int i_new_rate = var_GetInteger( p_input, "rate");
414 if( i_new_rate != i_rate )
418 emit rateChanged( i_rate );
422 void InputManager::UpdateName()
424 /* Update text, name and nowplaying */
427 /* Try to get the Title, then the Name */
428 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
429 if( EMPTY_STR( psz_name ) )
432 psz_name = input_item_GetName( input_GetItem( p_input ) );
435 /* Try to get the nowplaying */
436 char *psz_nowplaying =
437 input_item_GetNowPlaying( input_GetItem( p_input ) );
438 if( !EMPTY_STR( psz_nowplaying ) )
440 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
442 else /* Do it ourself */
444 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
446 if( !EMPTY_STR( psz_artist ) )
447 text.sprintf( "%s - %s", psz_artist, psz_name );
449 text.sprintf( "%s", psz_name );
453 /* Free everything */
455 free( psz_nowplaying );
457 /* If we have Nothing */
460 psz_name = input_item_GetURI( input_GetItem( p_input ) );
461 text.sprintf( "%s", psz_name );
462 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
466 if( oldName != text )
468 emit nameChanged( text );
473 bool InputManager::hasAudio()
478 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
479 return val.i_int > 0;
484 void InputManager::UpdateTeletext()
487 telexActivation( var_GetInteger( p_input, "teletext-es" ) >= 0 );
489 telexActivation( false );
492 void InputManager::UpdateVout()
496 /* Get current vout lists from input */
498 vout_thread_t **pp_vout;
499 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
506 emit voutListChanged( pp_vout, i_vout );
509 bool b_old_video = b_video;
510 b_video = i_vout > 0;
511 if( !!b_old_video != !!b_video )
512 emit voutChanged( b_video );
514 /* Release the vout list */
515 for( int i = 0; i < i_vout; i++ )
516 vlc_object_release( (vlc_object_t*)pp_vout[i] );
520 void InputManager::UpdateAout()
527 void InputManager::UpdateCaching()
529 if(!hasInput()) return;
531 float f_newCache = var_GetFloat ( p_input, "cache" );
532 if( f_newCache != f_cache )
534 f_cache = f_newCache;
536 emit cachingChanged( f_cache );
540 void InputManager::requestArtUpdate()
544 playlist_t *p_playlist = pl_Hold( p_intf );
545 playlist_AskForArtEnqueue( p_playlist, input_GetItem( p_input ), pl_Unlocked );
546 pl_Release( p_intf );
550 /* No input will signal the cover art to update,
551 * let's do it ourself */
556 void InputManager::UpdateArt()
562 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
566 url = url.replace( "file://", QString("" ) );
567 /* Taglib seems to define a attachment://, It won't work yet */
568 url = url.replace( "attachment://", QString("" ) );
569 /* Update Art meta */
570 emit artChanged( url );
573 inline void InputManager::UpdateStats()
575 emit statisticsUpdated( input_GetItem( p_input ) );
578 inline void InputManager::UpdateMeta()
580 emit metaChanged( input_GetItem( p_input ) );
583 inline void InputManager::UpdateInfo()
585 emit infoChanged( input_GetItem( p_input ) );
588 /* User update of the slider */
589 void InputManager::sliderUpdate( float new_pos )
592 var_SetFloat( p_input, "position", new_pos );
595 /* User togglePlayPause */
596 void InputManager::togglePlayPause()
601 var_Get( p_input, "state", &state );
602 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
603 var_Set( p_input, "state", state );
604 emit statusChanged( state.i_int );
608 void InputManager::sectionPrev()
612 int i_type = var_Type( p_input, "next-chapter" );
613 vlc_value_t val; val.b_bool = true;
614 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
615 "prev-chapter":"prev-title", val );
619 void InputManager::sectionNext()
623 int i_type = var_Type( p_input, "next-chapter" );
624 vlc_value_t val; val.b_bool = true;
625 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
626 "next-chapter":"next-title", val );
630 void InputManager::sectionMenu()
634 vlc_value_t val, text;
637 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
640 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
642 for( int i = 0; i < val.p_list->i_count; i++ )
644 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
647 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
649 var_Set( p_input, "title 0", root );
657 /* Set a new Teletext Page */
658 void InputManager::telexSetPage( int page )
662 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
663 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
665 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
667 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
668 "zvbi", FIND_ANYWHERE );
671 var_SetInteger( p_vbi, "vbi-page", page );
672 vlc_object_release( p_vbi );
673 emit newTelexPageSet( page );
679 /* Set the transparency on teletext */
680 void InputManager::telexSetTransparency( bool b_transparentTelextext )
684 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
685 "zvbi", FIND_ANYWHERE );
688 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
689 vlc_object_release( p_vbi );
690 emit teletextTransparencyActivated( b_transparentTelextext );
695 void InputManager::telexActivation( bool b_enabled )
699 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
700 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
702 /* Teletext is possible. Show the buttons */
703 b_enabled = (i_teletext_es >= 0);
704 emit teletextPossible( b_enabled );
705 if( !b_enabled ) return;
707 /* If Teletext is selected */
708 if( i_teletext_es == i_spu_es )
710 /* Activate the buttons */
711 teletextActivated( true );
713 /* Then, find the current page */
715 vlc_object_t *p_vbi = (vlc_object_t *)
716 vlc_object_find_name( p_input, "zvbi", FIND_ANYWHERE );
719 i_page = var_GetInteger( p_vbi, "vbi-page" );
720 vlc_object_release( p_vbi );
721 emit newTelexPageSet( i_page );
726 emit teletextPossible( b_enabled );
729 void InputManager::activateTeletext( bool b_enable )
733 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
734 if( i_teletext_es >= 0 )
736 var_SetInteger( p_input, "spu-es", b_enable ? i_teletext_es : -1 );
741 void InputManager::reverse()
745 int i_rate = var_GetInteger( p_input, "rate" );
746 var_SetInteger( p_input, "rate", -i_rate );
750 void InputManager::slower()
753 var_SetVoid( p_input, "rate-slower" );
756 void InputManager::faster()
759 var_SetVoid( p_input, "rate-faster" );
762 void InputManager::normalRate()
765 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
768 void InputManager::setRate( int new_rate )
771 var_SetInteger( p_input, "rate", new_rate );
774 void InputManager::setAtoB()
778 timeA = var_GetTime( THEMIM->getInput(), "time" );
782 timeB = var_GetTime( THEMIM->getInput(), "time" );
783 var_SetTime( THEMIM->getInput(), "time" , timeA );
790 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
793 /* Function called regularly when in an AtoB loop */
794 void InputManager::AtoBLoop( int i_time )
798 if( ( i_time >= (int)( timeB/1000000 ) )
799 || ( i_time < (int)( timeA/1000000 ) ) )
800 var_SetTime( THEMIM->getInput(), "time" , timeA );
804 /**********************************************************************
805 * MainInputManager implementation. Wrap an input manager and
806 * take care of updating the main playlist input.
807 * Used in the main playlist Dialog
808 **********************************************************************/
809 MainInputManager * MainInputManager::instance = NULL;
811 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
812 : QObject(NULL), p_intf( _p_intf )
815 im = new InputManager( this, p_intf );
817 var_AddCallback( THEPL, "item-change", ItemChanged, im );
818 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
819 var_AddCallback( THEPL, "activity", PLItemChanged, this );
821 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
823 /* Warn our embedded IM about input changes */
824 CONNECT( this, inputChanged( input_thread_t * ),
825 im, setInput( input_thread_t * ) );
827 /* emit check if playlist has allready started playing */
829 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
830 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
831 QApplication::postEvent( this, static_cast<QEvent*>(event) );
834 MainInputManager::~MainInputManager()
838 emit inputChanged( NULL );
839 var_DelCallback( p_input, "state", PLItemChanged, this );
840 vlc_object_release( p_input );
843 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
845 var_DelCallback( THEPL, "activity", PLItemChanged, this );
846 var_DelCallback( THEPL, "item-change", ItemChanged, im );
848 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
851 void MainInputManager::customEvent( QEvent *event )
853 int type = event->type();
854 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
857 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
858 if( type == VolumeChanged_Type )
860 emit volumeChanged();
864 /* Should be PLItemChanged Event */
865 if( !p_intf->p_sys->b_isDialogProvider )
867 vlc_mutex_lock( &p_intf->change_lock );
868 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
870 emit inputChanged( NULL );
871 var_DelCallback( p_input, "state", PLItemChanged, this );
872 vlc_object_release( p_input );
874 vlc_mutex_unlock( &p_intf->change_lock );
880 p_input = playlist_CurrentInput(THEPL);
883 var_AddCallback( p_input, "state", PLItemChanged, this );
884 emit inputChanged( p_input );
887 vlc_mutex_unlock( &p_intf->change_lock );
891 /* we are working as a dialogs provider */
892 playlist_t *p_playlist = pl_Hold( p_intf );
893 p_input = playlist_CurrentInput( p_playlist );
896 emit inputChanged( p_input );
897 vlc_object_release( p_input );
899 pl_Release( p_intf );
903 /* Playlist Control functions */
904 void MainInputManager::stop()
906 playlist_Stop( THEPL );
909 void MainInputManager::next()
911 playlist_Next( THEPL );
914 void MainInputManager::prev()
916 playlist_Prev( THEPL );
919 void MainInputManager::togglePlayPause()
923 playlist_Play( THEPL );
925 getIM()->togglePlayPause();
928 /* Static callbacks for MIM */
929 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
930 vlc_value_t oldval, vlc_value_t newval, void *param )
932 MainInputManager *mim = (MainInputManager*)param;
934 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
935 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
939 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
940 vlc_value_t oldval, vlc_value_t newval, void *param )
942 MainInputManager *mim = (MainInputManager*)param;
944 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
945 QApplication::postEvent( mim, static_cast<QEvent*>(event) );