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 msg_Dbg( p_intf, "New Event: type %i", i_type );
160 case PositionUpdate_Type:
163 case StatisticsUpdate_Type:
166 case ItemChanged_Type:
167 /* Ignore ItemChanged_Type event that does not apply to our input */
168 if( i_input_id == ple->i_id )
175 case ItemStateChanged_Type:
176 // TODO: Fusion with above state
179 // UpdateNavigation(); This shouldn't be useful now
180 // UpdateTeletext(); Same
182 case NameChanged_Type:
185 case MetaChanged_Type:
187 UpdateName(); /* Needed for NowPlaying */
188 UpdateArt(); /* Art is part of meta in the core */
190 case InfoChanged_Type:
193 case ItemTitleChanged_Type:
195 UpdateName(); /* Display the name of the Chapter, if exists */
197 case ItemRateChanged_Type:
200 case ItemEsChanged_Type:
202 // We don't do anything ES related. Why ?
204 case ItemTeletextChanged_Type:
207 case InterfaceVoutUpdate_Type:
210 case SynchroChanged_Type:
211 emit synchroChanged();
213 case CachingEvent_Type:
216 case BookmarksChanged_Type:
217 emit bookmarksChanged();
219 case InterfaceAoutUpdate_Type:
222 case RecordingEvent_Type:
226 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
231 /* Add the callbacks on Input. Self explanatory */
232 inline void InputManager::addCallbacks()
234 var_AddCallback( p_input, "intf-event", InputEvent, this );
237 /* Delete the callbacks on Input. Self explanatory */
238 inline void InputManager::delCallbacks()
240 var_DelCallback( p_input, "intf-event", InputEvent, this );
243 /* Static callbacks for IM */
244 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
245 vlc_value_t oldval, vlc_value_t newval, void *param )
247 InputManager *im = (InputManager*)param;
249 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
250 QApplication::postEvent( im, event );
254 static int InputEvent( vlc_object_t *p_this, const char *,
255 vlc_value_t, vlc_value_t newval, void *param )
257 InputManager *im = (InputManager*)param;
260 switch( newval.i_int )
262 case INPUT_EVENT_STATE:
263 event = new IMEvent( ItemStateChanged_Type, 0 );
265 case INPUT_EVENT_RATE:
266 event = new IMEvent( ItemRateChanged_Type, 0 );
268 case INPUT_EVENT_TIMES:
269 event = new IMEvent( PositionUpdate_Type, 0 );
272 case INPUT_EVENT_TITLE:
273 case INPUT_EVENT_CHAPTER:
274 event = new IMEvent( ItemTitleChanged_Type, 0 );
278 event = new IMEvent( ItemEsChanged_Type, 0 );
280 case INPUT_EVENT_TELETEXT:
281 event = new IMEvent( ItemTeletextChanged_Type, 0 );
284 case INPUT_EVENT_STATISTICS:
285 event = new IMEvent( StatisticsUpdate_Type, 0 );
288 case INPUT_EVENT_VOUT:
289 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
291 case INPUT_EVENT_AOUT:
292 event = new IMEvent( InterfaceAoutUpdate_Type, 0 );
295 case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
296 event = new IMEvent( MetaChanged_Type, 0 );
298 case INPUT_EVENT_ITEM_INFO: /* Codec Info */
299 event = new IMEvent( InfoChanged_Type, 0 );
301 case INPUT_EVENT_ITEM_NAME:
302 event = new IMEvent( NameChanged_Type, 0 );
305 case INPUT_EVENT_AUDIO_DELAY:
306 case INPUT_EVENT_SUBTITLE_DELAY:
307 event = new IMEvent( SynchroChanged_Type, 0 );
310 case INPUT_EVENT_CACHE:
311 event = new IMEvent( CachingEvent_Type, 0 );
314 case INPUT_EVENT_BOOKMARK:
315 event = new IMEvent( BookmarksChanged_Type, 0 );
318 case INPUT_EVENT_RECORD:
319 event = new IMEvent( RecordingEvent_Type, 0 );
322 case INPUT_EVENT_PROGRAM:
323 /* This is for PID changes */
324 /* event = new IMEvent( ProgramChanged_Type, 0 );
326 case INPUT_EVENT_SIGNAL:
327 /* This is for capture-card signals */
328 /* event = new IMEvent( SignalChanged_Type, 0 );
336 QApplication::postEvent( im, event );
339 static int VbiEvent( vlc_object_t *, const char *,
340 vlc_value_t, vlc_value_t, void *param )
342 InputManager *im = (InputManager*)param;
343 IMEvent *event = new IMEvent( ItemTeletextChanged_Type, 0 );
346 QApplication::postEvent( im, event );
350 void InputManager::UpdatePosition()
352 /* Update position */
353 int i_length, i_time; /* Int is enough, since we store seconds */
355 i_length = var_GetTime( p_input , "length" ) / 1000000;
356 i_time = var_GetTime( p_input , "time") / 1000000;
357 f_pos = var_GetFloat( p_input , "position" );
358 emit positionUpdated( f_pos, i_time, i_length );
361 void InputManager::UpdateNavigation()
363 /* Update navigation status */
364 vlc_value_t val; val.i_int = 0;
367 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
371 emit titleChanged( true );
372 msg_Dbg( p_intf, "Title %i", val.i_int );
373 /* p_input != NULL since val.i_int != 0 */
375 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
376 emit chapterChanged( (val.i_int > 0) );
377 msg_Dbg( p_intf, "Chapter: %i", val.i_int );
380 emit titleChanged( false );
383 void InputManager::UpdateStatus()
385 /* Update playing status */
386 vlc_value_t val; val.i_int = 0;
387 var_Get( p_input, "state", &val );
388 if( i_old_playing_status != val.i_int )
390 i_old_playing_status = val.i_int;
391 emit statusChanged( val.i_int );
395 void InputManager::UpdateRate()
398 int i_new_rate = var_GetInteger( p_input, "rate");
399 if( i_new_rate != i_rate )
403 emit rateChanged( i_rate );
407 void InputManager::UpdateName()
409 /* Update text, name and nowplaying */
412 /* Try to get the Title, then the Name */
413 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
414 if( EMPTY_STR( psz_name ) )
417 psz_name = input_item_GetName( input_GetItem( p_input ) );
420 /* Try to get the nowplaying */
421 char *psz_nowplaying =
422 input_item_GetNowPlaying( input_GetItem( p_input ) );
423 if( !EMPTY_STR( psz_nowplaying ) )
425 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
427 else /* Do it ourself */
429 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
431 if( !EMPTY_STR( psz_artist ) )
432 text.sprintf( "%s - %s", psz_artist, psz_name );
434 text.sprintf( "%s", psz_name );
438 /* Free everything */
440 free( psz_nowplaying );
442 /* If we have Nothing */
445 psz_name = input_item_GetURI( input_GetItem( p_input ) );
446 text.sprintf( "%s", psz_name );
447 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
451 if( oldName != text )
453 emit nameChanged( text );
458 bool InputManager::hasAudio()
463 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
464 return val.i_int > 0;
469 void InputManager::UpdateTeletext()
473 const bool b_enabled = var_CountChoices( p_input, "teletext-es" ) > 0;
474 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
476 /* Teletext is possible. Show the buttons */
477 emit teletextPossible( b_enabled );
479 /* If Teletext is selected */
480 if( b_enabled && i_teletext_es >= 0 )
482 /* Then, find the current page */
484 bool b_transparent = false;
486 vlc_object_t *p_vbi = (vlc_object_t *)
487 vlc_object_find_name( p_input, "zvbi", FIND_CHILD );
491 /* We deleted it (if not here, it does not harm), because
492 * var_AddCallback will silently add a duplicated one */
493 var_DelCallback( p_vbi, "vbi-page", VbiEvent, this );
494 /* This callback is not remove explicitly, but interfaces
495 * are guaranted to outlive input */
496 var_AddCallback( p_vbi, "vbi-page", VbiEvent, this );
498 i_page = var_GetInteger( p_vbi, "vbi-page" );
499 b_transparent = !var_GetBool( p_vbi, "vbi-opaque" );
500 vlc_object_release( p_vbi );
502 emit newTelexPageSet( i_page );
503 emit teletextTransparencyActivated( b_transparent );
506 emit teletextActivated( b_enabled && i_teletext_es >= 0 );
510 emit teletextActivated( false );
511 emit teletextPossible( false );
515 void InputManager::UpdateVout()
519 /* Get current vout lists from input */
521 vout_thread_t **pp_vout;
522 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
529 emit voutListChanged( pp_vout, i_vout );
532 bool b_old_video = b_video;
533 b_video = i_vout > 0;
534 if( !!b_old_video != !!b_video )
535 emit voutChanged( b_video );
537 /* Release the vout list */
538 for( int i = 0; i < i_vout; i++ )
539 vlc_object_release( (vlc_object_t*)pp_vout[i] );
543 void InputManager::UpdateAout()
550 void InputManager::UpdateCaching()
552 if(!hasInput()) return;
554 float f_newCache = var_GetFloat ( p_input, "cache" );
555 if( f_newCache != f_cache )
557 f_cache = f_newCache;
559 emit cachingChanged( f_cache );
563 void InputManager::requestArtUpdate()
567 playlist_t *p_playlist = pl_Hold( p_intf );
568 playlist_AskForArtEnqueue( p_playlist, input_GetItem( p_input ), pl_Unlocked );
569 pl_Release( p_intf );
573 /* No input will signal the cover art to update,
574 * let's do it ourself */
579 void InputManager::UpdateArt()
585 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
589 url = url.replace( "file://", QString("" ) );
590 /* Taglib seems to define a attachment://, It won't work yet */
591 url = url.replace( "attachment://", QString("" ) );
592 /* Update Art meta */
593 emit artChanged( url );
596 inline void InputManager::UpdateStats()
598 emit statisticsUpdated( input_GetItem( p_input ) );
601 inline void InputManager::UpdateMeta()
603 emit metaChanged( input_GetItem( p_input ) );
606 inline void InputManager::UpdateInfo()
608 emit infoChanged( input_GetItem( p_input ) );
611 void InputManager::UpdateRecord()
615 emit recordingStateChanged( var_GetBool( p_input, "record" ) );
619 /* User update of the slider */
620 void InputManager::sliderUpdate( float new_pos )
623 var_SetFloat( p_input, "position", new_pos );
626 /* User togglePlayPause */
627 void InputManager::togglePlayPause()
632 var_Get( p_input, "state", &state );
633 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
634 var_Set( p_input, "state", state );
635 emit statusChanged( state.i_int );
639 void InputManager::sectionPrev()
643 int i_type = var_Type( p_input, "next-chapter" );
644 vlc_value_t val; val.b_bool = true;
645 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
646 "prev-chapter":"prev-title", val );
650 void InputManager::sectionNext()
654 int i_type = var_Type( p_input, "next-chapter" );
655 vlc_value_t val; val.b_bool = true;
656 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
657 "next-chapter":"next-title", val );
661 void InputManager::sectionMenu()
665 vlc_value_t val, text;
668 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
671 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
673 for( int i = 0; i < val.p_list->i_count; i++ )
675 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
678 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
680 var_Set( p_input, "title 0", root );
688 /* Set a new Teletext Page */
689 void InputManager::telexSetPage( int page )
693 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
695 if( i_teletext_es >= 0 )
697 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
698 "zvbi", FIND_CHILD );
701 var_SetInteger( p_vbi, "vbi-page", page );
702 vlc_object_release( p_vbi );
703 emit newTelexPageSet( page );
709 /* Set the transparency on teletext */
710 void InputManager::telexSetTransparency( bool b_transparentTelextext )
714 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
715 "zvbi", FIND_CHILD );
718 var_SetBool( p_vbi, "vbi-opaque", !b_transparentTelextext );
719 vlc_object_release( p_vbi );
720 emit teletextTransparencyActivated( b_transparentTelextext );
725 void InputManager::activateTeletext( bool b_enable )
729 if( hasInput() && !var_Change( p_input, "teletext-es", VLC_VAR_GETLIST, &list, &text ) )
731 if( list.p_list->i_count > 0 )
733 /* Prefer the page 100 if it is present */
735 for( i = 0; i < text.p_list->i_count; i++ )
737 /* The description is the page number as a string */
738 const char *psz_page = text.p_list->p_values[i].psz_string;
739 if( psz_page && !strcmp( psz_page, "100" ) )
742 if( i >= list.p_list->i_count )
744 var_SetInteger( p_input, "spu-es", b_enable ? list.p_list->p_values[i].i_int : -1 );
746 var_Change( p_input, "teletext-es", VLC_VAR_FREELIST, &list, &text );
750 void InputManager::reverse()
754 int i_rate = var_GetInteger( p_input, "rate" );
755 var_SetInteger( p_input, "rate", -i_rate );
759 void InputManager::slower()
762 var_SetVoid( p_input, "rate-slower" );
765 void InputManager::faster()
768 var_SetVoid( p_input, "rate-faster" );
771 void InputManager::normalRate()
774 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
777 void InputManager::setRate( int new_rate )
780 var_SetInteger( p_input, "rate", new_rate );
783 void InputManager::setAtoB()
787 timeA = var_GetTime( THEMIM->getInput(), "time" );
791 timeB = var_GetTime( THEMIM->getInput(), "time" );
792 var_SetTime( THEMIM->getInput(), "time" , timeA );
799 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
802 /* Function called regularly when in an AtoB loop */
803 void InputManager::AtoBLoop( int i_time )
807 if( ( i_time >= (int)( timeB/1000000 ) )
808 || ( i_time < (int)( timeA/1000000 ) ) )
809 var_SetTime( THEMIM->getInput(), "time" , timeA );
813 /**********************************************************************
814 * MainInputManager implementation. Wrap an input manager and
815 * take care of updating the main playlist input.
816 * Used in the main playlist Dialog
817 **********************************************************************/
818 MainInputManager * MainInputManager::instance = NULL;
820 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
821 : QObject(NULL), p_intf( _p_intf )
824 im = new InputManager( this, p_intf );
826 var_AddCallback( THEPL, "item-change", ItemChanged, im );
827 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
828 var_AddCallback( THEPL, "activity", PLItemChanged, this );
830 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
832 /* Warn our embedded IM about input changes */
833 CONNECT( this, inputChanged( input_thread_t * ),
834 im, setInput( input_thread_t * ) );
836 /* emit check if playlist has allready started playing */
838 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
840 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
841 customEvent( event );
845 MainInputManager::~MainInputManager()
849 emit inputChanged( NULL );
850 var_DelCallback( p_input, "state", PLItemChanged, this );
851 vlc_object_release( p_input );
854 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
856 var_DelCallback( THEPL, "activity", PLItemChanged, this );
857 var_DelCallback( THEPL, "item-change", ItemChanged, im );
859 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
862 vout_thread_t* MainInputManager::getVout()
864 return p_input ? input_GetVout( p_input ) : NULL;
867 aout_instance_t * MainInputManager::getAout()
869 return p_input ? input_GetAout( p_input ) : NULL;
872 void MainInputManager::customEvent( QEvent *event )
874 int type = event->type();
875 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
878 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
879 if( type == VolumeChanged_Type )
881 emit volumeChanged();
885 /* Should be PLItemChanged Event */
886 if( !p_intf->p_sys->b_isDialogProvider )
888 vlc_mutex_lock( &p_intf->change_lock );
889 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
891 emit inputChanged( NULL );
892 var_DelCallback( p_input, "state", PLItemChanged, this );
893 vlc_object_release( p_input );
895 vlc_mutex_unlock( &p_intf->change_lock );
901 p_input = playlist_CurrentInput(THEPL);
904 var_AddCallback( p_input, "state", PLItemChanged, this );
905 emit inputChanged( p_input );
908 vlc_mutex_unlock( &p_intf->change_lock );
912 /* we are working as a dialogs provider */
913 playlist_t *p_playlist = pl_Hold( p_intf );
914 p_input = playlist_CurrentInput( p_playlist );
917 emit inputChanged( p_input );
918 vlc_object_release( p_input );
920 pl_Release( p_intf );
924 /* Playlist Control functions */
925 void MainInputManager::stop()
927 playlist_Stop( THEPL );
930 void MainInputManager::next()
932 playlist_Next( THEPL );
935 void MainInputManager::prev()
937 playlist_Prev( THEPL );
940 void MainInputManager::togglePlayPause()
944 playlist_Play( THEPL );
946 getIM()->togglePlayPause();
949 /* Static callbacks for MIM */
950 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
951 vlc_value_t oldval, vlc_value_t newval, void *param )
953 MainInputManager *mim = (MainInputManager*)param;
955 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
956 QApplication::postEvent( mim, event );
960 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
961 vlc_value_t oldval, vlc_value_t newval, void *param )
963 MainInputManager *mim = (MainInputManager*)param;
965 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
966 QApplication::postEvent( mim, event );