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( (input_item_t *)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 )
174 /* Update duration of file */
176 UpdateMeta( ple->i_id );
178 case ItemStateChanged_Type:
179 // TODO: Fusion with above state
182 // UpdateNavigation(); This shouldn't be useful now
183 // UpdateTeletext(); Same
185 case NameChanged_Type:
188 case MetaChanged_Type:
190 UpdateName(); /* Needed for NowPlaying */
191 UpdateArt(); /* Art is part of meta in the core */
193 case InfoChanged_Type:
196 case ItemTitleChanged_Type:
198 UpdateName(); /* Display the name of the Chapter, if exists */
200 case ItemRateChanged_Type:
203 case ItemEsChanged_Type:
205 // We don't do anything ES related. Why ?
207 case ItemTeletextChanged_Type:
210 case InterfaceVoutUpdate_Type:
213 case SynchroChanged_Type:
214 emit synchroChanged();
216 case CachingEvent_Type:
219 case BookmarksChanged_Type:
220 emit bookmarksChanged();
222 case InterfaceAoutUpdate_Type:
225 case RecordingEvent_Type:
229 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
234 /* Add the callbacks on Input. Self explanatory */
235 inline void InputManager::addCallbacks()
237 var_AddCallback( p_input, "intf-event", InputEvent, this );
240 /* Delete the callbacks on Input. Self explanatory */
241 inline void InputManager::delCallbacks()
243 var_DelCallback( p_input, "intf-event", InputEvent, this );
246 /* Static callbacks for IM */
247 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
248 vlc_value_t oldval, vlc_value_t newval, void *param )
250 InputManager *im = (InputManager*)param;
252 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
253 QApplication::postEvent( im, event );
257 static int InputEvent( vlc_object_t *p_this, const char *,
258 vlc_value_t, vlc_value_t newval, void *param )
260 InputManager *im = (InputManager*)param;
263 switch( newval.i_int )
265 case INPUT_EVENT_STATE:
266 event = new IMEvent( ItemStateChanged_Type, 0 );
268 case INPUT_EVENT_RATE:
269 event = new IMEvent( ItemRateChanged_Type, 0 );
271 case INPUT_EVENT_TIMES:
272 event = new IMEvent( PositionUpdate_Type, 0 );
275 case INPUT_EVENT_TITLE:
276 case INPUT_EVENT_CHAPTER:
277 event = new IMEvent( ItemTitleChanged_Type, 0 );
281 event = new IMEvent( ItemEsChanged_Type, 0 );
283 case INPUT_EVENT_TELETEXT:
284 event = new IMEvent( ItemTeletextChanged_Type, 0 );
287 case INPUT_EVENT_STATISTICS:
288 event = new IMEvent( StatisticsUpdate_Type, 0 );
291 case INPUT_EVENT_VOUT:
292 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
294 case INPUT_EVENT_AOUT:
295 event = new IMEvent( InterfaceAoutUpdate_Type, 0 );
298 case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
299 event = new IMEvent( MetaChanged_Type, 0 );
301 case INPUT_EVENT_ITEM_INFO: /* Codec Info */
302 event = new IMEvent( InfoChanged_Type, 0 );
304 case INPUT_EVENT_ITEM_NAME:
305 event = new IMEvent( NameChanged_Type, 0 );
308 case INPUT_EVENT_AUDIO_DELAY:
309 case INPUT_EVENT_SUBTITLE_DELAY:
310 event = new IMEvent( SynchroChanged_Type, 0 );
313 case INPUT_EVENT_CACHE:
314 event = new IMEvent( CachingEvent_Type, 0 );
317 case INPUT_EVENT_BOOKMARK:
318 event = new IMEvent( BookmarksChanged_Type, 0 );
321 case INPUT_EVENT_RECORD:
322 event = new IMEvent( RecordingEvent_Type, 0 );
325 case INPUT_EVENT_PROGRAM:
326 /* This is for PID changes */
327 /* event = new IMEvent( ProgramChanged_Type, 0 );
329 case INPUT_EVENT_SIGNAL:
330 /* This is for capture-card signals */
331 /* event = new IMEvent( SignalChanged_Type, 0 );
339 QApplication::postEvent( im, event );
342 static int VbiEvent( vlc_object_t *, const char *,
343 vlc_value_t, vlc_value_t, void *param )
345 InputManager *im = (InputManager*)param;
346 IMEvent *event = new IMEvent( ItemTeletextChanged_Type, 0 );
349 QApplication::postEvent( im, event );
353 void InputManager::UpdatePosition()
355 /* Update position */
356 int i_length, i_time; /* Int is enough, since we store seconds */
358 i_length = var_GetTime( p_input , "length" ) / 1000000;
359 i_time = var_GetTime( p_input , "time") / 1000000;
360 f_pos = var_GetFloat( p_input , "position" );
361 emit positionUpdated( f_pos, i_time, i_length );
364 void InputManager::UpdateNavigation()
366 /* Update navigation status */
367 vlc_value_t val; val.i_int = 0;
370 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
374 emit titleChanged( true );
375 msg_Dbg( p_intf, "Title %i", val.i_int );
376 /* p_input != NULL since val.i_int != 0 */
378 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
379 emit chapterChanged( (val.i_int > 0) );
380 msg_Dbg( p_intf, "Chapter: %i", val.i_int );
383 emit titleChanged( false );
386 void InputManager::UpdateStatus()
388 /* Update playing status */
389 vlc_value_t val; val.i_int = 0;
390 var_Get( p_input, "state", &val );
391 if( i_old_playing_status != val.i_int )
393 i_old_playing_status = val.i_int;
394 emit statusChanged( val.i_int );
398 void InputManager::UpdateRate()
401 int i_new_rate = var_GetInteger( p_input, "rate");
402 if( i_new_rate != i_rate )
406 emit rateChanged( i_rate );
410 void InputManager::UpdateName()
412 /* Update text, name and nowplaying */
415 /* Try to get the Title, then the Name */
416 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
417 if( EMPTY_STR( psz_name ) )
420 psz_name = input_item_GetName( input_GetItem( p_input ) );
423 /* Try to get the nowplaying */
424 char *psz_nowplaying =
425 input_item_GetNowPlaying( input_GetItem( p_input ) );
426 if( !EMPTY_STR( psz_nowplaying ) )
428 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
430 else /* Do it ourself */
432 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
434 if( !EMPTY_STR( psz_artist ) )
435 text.sprintf( "%s - %s", psz_artist, psz_name );
437 text.sprintf( "%s", psz_name );
441 /* Free everything */
443 free( psz_nowplaying );
445 /* If we have Nothing */
448 psz_name = input_item_GetURI( input_GetItem( p_input ) );
449 text.sprintf( "%s", psz_name );
450 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
454 if( oldName != text )
456 emit nameChanged( text );
461 bool InputManager::hasAudio()
466 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
467 return val.i_int > 0;
472 void InputManager::UpdateTeletext()
476 const bool b_enabled = var_CountChoices( p_input, "teletext-es" ) > 0;
477 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
479 /* Teletext is possible. Show the buttons */
480 emit teletextPossible( b_enabled );
482 /* If Teletext is selected */
483 if( b_enabled && i_teletext_es >= 0 )
485 /* Then, find the current page */
487 bool b_transparent = false;
489 vlc_object_t *p_vbi = (vlc_object_t *)
490 vlc_object_find_name( p_input, "zvbi", FIND_CHILD );
494 /* We deleted it (if not here, it does not harm), because
495 * var_AddCallback will silently add a duplicated one */
496 var_DelCallback( p_vbi, "vbi-page", VbiEvent, this );
497 /* This callback is not remove explicitly, but interfaces
498 * are guaranted to outlive input */
499 var_AddCallback( p_vbi, "vbi-page", VbiEvent, this );
501 i_page = var_GetInteger( p_vbi, "vbi-page" );
502 b_transparent = !var_GetBool( p_vbi, "vbi-opaque" );
503 vlc_object_release( p_vbi );
505 emit newTelexPageSet( i_page );
506 emit teletextTransparencyActivated( b_transparent );
509 emit teletextActivated( b_enabled && i_teletext_es >= 0 );
513 emit teletextActivated( false );
514 emit teletextPossible( false );
518 void InputManager::UpdateVout()
522 /* Get current vout lists from input */
524 vout_thread_t **pp_vout;
525 if( input_Control( p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout ) )
532 emit voutListChanged( pp_vout, i_vout );
535 bool b_old_video = b_video;
536 b_video = i_vout > 0;
537 if( !!b_old_video != !!b_video )
538 emit voutChanged( b_video );
540 /* Release the vout list */
541 for( int i = 0; i < i_vout; i++ )
542 vlc_object_release( (vlc_object_t*)pp_vout[i] );
546 void InputManager::UpdateAout()
553 void InputManager::UpdateCaching()
555 if(!hasInput()) return;
557 float f_newCache = var_GetFloat ( p_input, "cache" );
558 if( f_newCache != f_cache )
560 f_cache = f_newCache;
562 emit cachingChanged( f_cache );
566 void InputManager::requestArtUpdate()
570 playlist_t *p_playlist = pl_Hold( p_intf );
571 playlist_AskForArtEnqueue( p_playlist, input_GetItem( p_input ), pl_Unlocked );
572 pl_Release( p_intf );
576 /* No input will signal the cover art to update,
577 * let's do it ourself */
582 void InputManager::UpdateArt()
588 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
592 url = url.replace( "file://", QString("" ) );
593 /* Taglib seems to define a attachment://, It won't work yet */
594 url = url.replace( "attachment://", QString("" ) );
595 /* Update Art meta */
596 emit artChanged( url );
599 inline void InputManager::UpdateStats()
601 emit statisticsUpdated( input_GetItem( p_input ) );
604 inline void InputManager::UpdateMeta( int id )
606 emit metaChanged( id );
609 inline void InputManager::UpdateMeta()
611 emit metaChanged( input_GetItem( p_input ) );
614 inline void InputManager::UpdateInfo()
616 emit infoChanged( input_GetItem( p_input ) );
619 void InputManager::UpdateRecord()
623 emit recordingStateChanged( var_GetBool( p_input, "record" ) );
627 /* User update of the slider */
628 void InputManager::sliderUpdate( float new_pos )
631 var_SetFloat( p_input, "position", new_pos );
634 /* User togglePlayPause */
635 void InputManager::togglePlayPause()
640 var_Get( p_input, "state", &state );
641 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
642 var_Set( p_input, "state", state );
643 emit statusChanged( state.i_int );
647 void InputManager::sectionPrev()
651 int i_type = var_Type( p_input, "next-chapter" );
652 vlc_value_t val; val.b_bool = true;
653 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
654 "prev-chapter":"prev-title", val );
658 void InputManager::sectionNext()
662 int i_type = var_Type( p_input, "next-chapter" );
663 vlc_value_t val; val.b_bool = true;
664 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
665 "next-chapter":"next-title", val );
669 void InputManager::sectionMenu()
673 vlc_value_t val, text;
676 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
679 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
681 for( int i = 0; i < val.p_list->i_count; i++ )
683 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
686 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
688 var_Set( p_input, "title 0", root );
696 /* Set a new Teletext Page */
697 void InputManager::telexSetPage( int page )
701 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
703 if( i_teletext_es >= 0 )
705 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
706 "zvbi", FIND_CHILD );
709 var_SetInteger( p_vbi, "vbi-page", page );
710 vlc_object_release( p_vbi );
711 emit newTelexPageSet( page );
717 /* Set the transparency on teletext */
718 void InputManager::telexSetTransparency( bool b_transparentTelextext )
722 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
723 "zvbi", FIND_CHILD );
726 var_SetBool( p_vbi, "vbi-opaque", !b_transparentTelextext );
727 vlc_object_release( p_vbi );
728 emit teletextTransparencyActivated( b_transparentTelextext );
733 void InputManager::activateTeletext( bool b_enable )
737 if( hasInput() && !var_Change( p_input, "teletext-es", VLC_VAR_GETLIST, &list, &text ) )
739 if( list.p_list->i_count > 0 )
741 /* Prefer the page 100 if it is present */
743 for( i = 0; i < text.p_list->i_count; i++ )
745 /* The description is the page number as a string */
746 const char *psz_page = text.p_list->p_values[i].psz_string;
747 if( psz_page && !strcmp( psz_page, "100" ) )
750 if( i >= list.p_list->i_count )
752 var_SetInteger( p_input, "spu-es", b_enable ? list.p_list->p_values[i].i_int : -1 );
754 var_Change( p_input, "teletext-es", VLC_VAR_FREELIST, &list, &text );
758 void InputManager::reverse()
762 int i_rate = var_GetInteger( p_input, "rate" );
763 var_SetInteger( p_input, "rate", -i_rate );
767 void InputManager::slower()
770 var_SetVoid( p_input, "rate-slower" );
773 void InputManager::faster()
776 var_SetVoid( p_input, "rate-faster" );
779 void InputManager::normalRate()
782 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
785 void InputManager::setRate( int new_rate )
788 var_SetInteger( p_input, "rate", new_rate );
791 void InputManager::setAtoB()
795 timeA = var_GetTime( THEMIM->getInput(), "time" );
799 timeB = var_GetTime( THEMIM->getInput(), "time" );
800 var_SetTime( THEMIM->getInput(), "time" , timeA );
807 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
810 /* Function called regularly when in an AtoB loop */
811 void InputManager::AtoBLoop( int i_time )
815 if( ( i_time >= (int)( timeB/1000000 ) )
816 || ( i_time < (int)( timeA/1000000 ) ) )
817 var_SetTime( THEMIM->getInput(), "time" , timeA );
821 /**********************************************************************
822 * MainInputManager implementation. Wrap an input manager and
823 * take care of updating the main playlist input.
824 * Used in the main playlist Dialog
825 **********************************************************************/
826 MainInputManager * MainInputManager::instance = NULL;
828 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
829 : QObject(NULL), p_intf( _p_intf )
832 im = new InputManager( this, p_intf );
834 var_AddCallback( THEPL, "item-change", ItemChanged, im );
835 var_AddCallback( THEPL, "item-current", PLItemChanged, this );
836 var_AddCallback( THEPL, "activity", PLItemChanged, this );
838 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
840 /* Warn our embedded IM about input changes */
841 CONNECT( this, inputChanged( input_thread_t * ),
842 im, setInput( input_thread_t * ) );
844 /* emit check if playlist has allready started playing */
846 var_Change( THEPL, "item-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
848 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
849 customEvent( event );
853 MainInputManager::~MainInputManager()
857 emit inputChanged( NULL );
858 var_DelCallback( p_input, "state", PLItemChanged, this );
859 vlc_object_release( p_input );
862 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
864 var_DelCallback( THEPL, "activity", PLItemChanged, this );
865 var_DelCallback( THEPL, "item-change", ItemChanged, im );
867 var_DelCallback( THEPL, "item-current", PLItemChanged, this );
870 vout_thread_t* MainInputManager::getVout()
872 return p_input ? input_GetVout( p_input ) : NULL;
875 aout_instance_t * MainInputManager::getAout()
877 return p_input ? input_GetAout( p_input ) : NULL;
880 void MainInputManager::customEvent( QEvent *event )
882 int type = event->type();
883 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
886 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
887 if( type == VolumeChanged_Type )
889 emit volumeChanged();
893 /* Should be PLItemChanged Event */
894 if( !p_intf->p_sys->b_isDialogProvider )
896 vlc_mutex_lock( &p_intf->change_lock );
897 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
899 emit inputChanged( p_input );
900 var_DelCallback( p_input, "state", PLItemChanged, this );
901 vlc_object_release( p_input );
903 vlc_mutex_unlock( &p_intf->change_lock );
909 p_input = playlist_CurrentInput(THEPL);
912 var_AddCallback( p_input, "state", PLItemChanged, this );
913 emit inputChanged( p_input );
916 vlc_mutex_unlock( &p_intf->change_lock );
920 /* we are working as a dialogs provider */
921 playlist_t *p_playlist = pl_Hold( p_intf );
922 p_input = playlist_CurrentInput( p_playlist );
925 emit inputChanged( p_input );
926 vlc_object_release( p_input );
928 pl_Release( p_intf );
932 /* Playlist Control functions */
933 void MainInputManager::stop()
935 playlist_Stop( THEPL );
938 void MainInputManager::next()
940 playlist_Next( THEPL );
943 void MainInputManager::prev()
945 playlist_Prev( THEPL );
948 void MainInputManager::togglePlayPause()
952 playlist_Play( THEPL );
954 getIM()->togglePlayPause();
957 /* Static callbacks for MIM */
958 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
959 vlc_value_t oldval, vlc_value_t newval, void *param )
961 MainInputManager *mim = (MainInputManager*)param;
963 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
964 QApplication::postEvent( mim, event );
968 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
969 vlc_value_t oldval, vlc_value_t newval, void *param )
971 MainInputManager *mim = (MainInputManager*)param;
973 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
974 QApplication::postEvent( mim, event );