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_Warn( p_intf, "Setting the 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 msg_Warn( p_intf, "Deleting the input" );
106 if( !p_input ) return;
109 i_old_playing_status = END_S;
117 emit positionUpdated( -1.0, 0 ,0 );
118 emit rateChanged( INPUT_RATE_DEFAULT ); /* TODO: Do we want this ? */
119 emit nameChanged( "" );
120 emit chapterChanged( 0 );
121 emit titleChanged( 0 );
122 emit statusChanged( END_S );
124 /* Reset InfoPanels but stats */
125 emit artChanged( NULL );
126 emit infoChanged( NULL );
127 emit metaChanged( NULL );
129 emit teletextPossible( false );
130 emit voutChanged( false );
132 emit AtoBchanged( false, false );
133 vlc_object_release( p_input ); /* FIXME: Can't we release sooner ? */
138 /* Add the callbacks on Input. Self explanatory */
139 inline void InputManager::addCallbacks()
141 var_AddCallback( p_input, "intf-event", InputEvent, this );
144 /* Delete the callbacks on Input. Self explanatory */
145 inline void InputManager::delCallbacks()
147 var_DelCallback( p_input, "intf-event", InputEvent, this );
150 /* Convert the event from the callbacks in actions */
151 void InputManager::customEvent( QEvent *event )
153 int i_type = event->type();
154 IMEvent *ple = static_cast<IMEvent *>(event);
156 if ( i_type != PositionUpdate_Type &&
157 i_type != ItemChanged_Type &&
158 i_type != ItemRateChanged_Type &&
159 i_type != ItemTitleChanged_Type &&
160 i_type != ItemEsChanged_Type &&
161 i_type != ItemTeletextChanged_Type &&
162 i_type != ItemStateChanged_Type &&
163 i_type != StatisticsUpdate_Type &&
164 i_type != InterfaceVoutUpdate_Type &&
165 i_type != MetaChanged_Type &&
166 i_type != NameChanged_Type &&
167 i_type != InfoChanged_Type )
170 if( !hasInput() ) return;
172 if( ( i_type != PositionUpdate_Type &&
173 i_type != ItemRateChanged_Type &&
174 i_type != ItemEsChanged_Type &&
175 i_type != ItemTeletextChanged_Type &&
176 i_type != ItemStateChanged_Type &&
177 i_type != StatisticsUpdate_Type &&
178 i_type != InterfaceVoutUpdate_Type &&
179 i_type != MetaChanged_Type &&
180 i_type != NameChanged_Type &&
181 i_type != InfoChanged_Type
183 && ( i_input_id != ple->i_id ) )
186 if( i_type != PositionUpdate_Type &&
187 i_type != StatisticsUpdate_Type )
188 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:
239 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
243 void InputManager::UpdatePosition()
245 /* Update position */
246 int i_length, i_time; /* Int is enough, since we store seconds */
248 i_length = var_GetTime( p_input , "length" ) / 1000000;
249 i_time = var_GetTime( p_input , "time") / 1000000;
250 f_pos = var_GetFloat( p_input , "position" );
251 emit positionUpdated( f_pos, i_time, i_length );
254 void InputManager::UpdateNavigation()
256 /* Update navigation status */
257 vlc_value_t val; val.i_int = 0;
260 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
264 emit titleChanged( true );
265 /* p_input != NULL since val.i_int != 0 */
267 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
268 emit chapterChanged( (val.i_int > 0) );
271 emit titleChanged( false );
274 void InputManager::UpdateStatus()
276 /* Update playing status */
277 vlc_value_t val; val.i_int = 0;
278 var_Get( p_input, "state", &val );
279 if( i_old_playing_status != val.i_int )
281 i_old_playing_status = val.i_int;
282 emit statusChanged( val.i_int );
286 void InputManager::UpdateRate()
289 int i_new_rate = var_GetInteger( p_input, "rate");
290 if( i_new_rate != i_rate )
294 emit rateChanged( i_rate );
298 void InputManager::UpdateName()
300 /* Update text, name and nowplaying */
303 /* Try to get the Title, then the Name */
304 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
305 if( EMPTY_STR( psz_name ) )
308 psz_name = input_item_GetName( input_GetItem( p_input ) );
311 /* Try to get the nowplaying */
312 char *psz_nowplaying =
313 input_item_GetNowPlaying( input_GetItem( p_input ) );
314 if( !EMPTY_STR( psz_nowplaying ) )
316 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
318 else /* Do it ourself */
320 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
322 if( !EMPTY_STR( psz_artist ) )
323 text.sprintf( "%s - %s", psz_artist, psz_name );
325 text.sprintf( "%s", psz_name );
329 /* Free everything */
331 free( psz_nowplaying );
333 /* If we have Nothing */
336 psz_name = input_item_GetURI( input_GetItem( p_input ) );
337 text.sprintf( "%s", psz_name );
338 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
342 if( oldName != text )
344 emit nameChanged( text );
349 bool InputManager::hasAudio()
354 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
355 return val.i_int > 0;
360 void InputManager::UpdateTeletext()
363 telexActivation( var_GetInteger( p_input, "teletext-es" ) >= 0 );
365 telexActivation( false );
368 void InputManager::UpdateVout()
372 bool b_old_video = b_video;
374 vlc_object_t *p_vout = (vlc_object_t*)vlc_object_find( p_input,
375 VLC_OBJECT_VOUT, FIND_CHILD );
376 b_video = p_vout != NULL;
378 vlc_object_release( p_vout );
379 if( !!b_old_video != !!b_video )
380 emit voutChanged( b_video );
384 inline void InputManager::UpdateArt()
386 /* Update Art meta */
387 emit artChanged( input_GetItem( p_input ) );
390 inline void InputManager::UpdateStats()
392 emit statisticsUpdated( input_GetItem( p_input ) );
395 inline void InputManager::UpdateMeta()
397 emit metaChanged( input_GetItem( p_input ) );
400 inline void InputManager::UpdateInfo()
402 emit infoChanged( input_GetItem( p_input ) );
405 /* User update of the slider */
406 void InputManager::sliderUpdate( float new_pos )
409 var_SetFloat( p_input, "position", new_pos );
412 /* User togglePlayPause */
413 void InputManager::togglePlayPause()
418 var_Get( p_input, "state", &state );
419 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
420 var_Set( p_input, "state", state );
421 emit statusChanged( state.i_int );
425 void InputManager::sectionPrev()
429 int i_type = var_Type( p_input, "next-chapter" );
430 vlc_value_t val; val.b_bool = true;
431 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
432 "prev-chapter":"prev-title", val );
436 void InputManager::sectionNext()
440 int i_type = var_Type( p_input, "next-chapter" );
441 vlc_value_t val; val.b_bool = true;
442 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
443 "next-chapter":"next-title", val );
447 void InputManager::sectionMenu()
451 vlc_value_t val, text;
454 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
457 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
459 for( int i = 0; i < val.p_list->i_count; i++ )
461 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
464 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
466 var_Set( p_input, "title 0", root );
474 /* Set a new Teletext Page */
475 void InputManager::telexSetPage( int page )
479 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
480 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
482 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
484 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
485 "zvbi", FIND_ANYWHERE );
488 var_SetInteger( p_vbi, "vbi-page", page );
489 vlc_object_release( p_vbi );
490 emit newTelexPageSet( page );
496 /* Set the transparency on teletext */
497 void InputManager::telexSetTransparency( bool b_transparentTelextext )
501 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
502 "zvbi", FIND_ANYWHERE );
505 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
506 vlc_object_release( p_vbi );
507 emit teletextTransparencyActivated( b_transparentTelextext );
512 void InputManager::telexActivation( bool b_enabled )
516 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
517 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
519 /* Teletext is possible. Show the buttons */
520 b_enabled = (i_teletext_es >= 0);
521 emit teletextPossible( b_enabled );
522 if( !b_enabled ) return;
524 /* If Teletext is selected */
525 if( i_teletext_es == i_spu_es )
527 /* Activate the buttons */
528 teletextActivated( true );
530 /* Then, find the current page */
532 vlc_object_t *p_vbi = (vlc_object_t *)
533 vlc_object_find_name( p_input, "zvbi", FIND_ANYWHERE );
536 i_page = var_GetInteger( p_vbi, "vbi-page" );
537 vlc_object_release( p_vbi );
538 emit newTelexPageSet( i_page );
543 emit teletextPossible( b_enabled );
546 void InputManager::activateTeletext( bool b_enable )
550 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
551 if( i_teletext_es >= 0 )
553 var_SetInteger( p_input, "spu-es", b_enable ? i_teletext_es : -1 );
558 void InputManager::reverse()
562 int i_rate = var_GetInteger( p_input, "rate" );
563 var_SetInteger( p_input, "rate", -i_rate );
567 void InputManager::slower()
570 var_SetVoid( p_input, "rate-slower" );
573 void InputManager::faster()
576 var_SetVoid( p_input, "rate-faster" );
579 void InputManager::normalRate()
582 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
585 void InputManager::setRate( int new_rate )
588 var_SetInteger( p_input, "rate", new_rate );
591 void InputManager::setAtoB()
595 timeA = var_GetTime( THEMIM->getInput(), "time" );
599 timeB = var_GetTime( THEMIM->getInput(), "time" );
600 var_SetTime( THEMIM->getInput(), "time" , timeA );
607 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
610 /* Function called regularly when in an AtoB loop */
611 void InputManager::AtoBLoop( int i_time )
615 if( ( i_time >= (int)( timeB/1000000 ) )
616 || ( i_time < (int)( timeA/1000000 ) ) )
617 var_SetTime( THEMIM->getInput(), "time" , timeA );
621 /**********************************************************************
622 * MainInputManager implementation. Wrap an input manager and
623 * take care of updating the main playlist input.
624 * Used in the main playlist Dialog
625 **********************************************************************/
626 MainInputManager * MainInputManager::instance = NULL;
628 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
629 : QObject(NULL), p_intf( _p_intf )
632 im = new InputManager( this, p_intf );
634 var_AddCallback( THEPL, "item-change", ItemChanged, im );
635 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
636 var_AddCallback( THEPL, "activity", PLItemChanged, this );
638 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
640 /* Warn our embedded IM about input changes */
641 CONNECT( this, inputChanged( input_thread_t * ),
642 im, setInput( input_thread_t * ) );
644 /* emit check if playlist has allready started playing */
646 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
647 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
648 QApplication::postEvent( this, static_cast<QEvent*>(event) );
651 MainInputManager::~MainInputManager()
655 var_DelCallback( p_input, "state", PLItemChanged, this );
656 vlc_object_release( p_input );
657 emit inputChanged( NULL );
660 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
662 var_DelCallback( THEPL, "activity", PLItemChanged, this );
663 var_DelCallback( THEPL, "item-change", ItemChanged, im );
665 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
668 void MainInputManager::customEvent( QEvent *event )
670 int type = event->type();
671 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
674 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
675 if( type == VolumeChanged_Type )
677 emit volumeChanged();
681 /* Should be PLItemChanged Event */
682 if( !p_intf->p_sys->b_isDialogProvider )
684 vlc_mutex_lock( &p_intf->change_lock );
685 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
687 var_DelCallback( p_input, "state", PLItemChanged, this );
688 vlc_object_release( p_input );
689 emit inputChanged( NULL );
691 vlc_mutex_unlock( &p_intf->change_lock );
697 p_input = playlist_CurrentInput(THEPL);
700 var_AddCallback( p_input, "state", PLItemChanged, this );
701 emit inputChanged( p_input );
704 vlc_mutex_unlock( &p_intf->change_lock );
708 /* we are working as a dialogs provider */
709 playlist_t *p_playlist = pl_Hold( p_intf );
710 p_input = playlist_CurrentInput( p_playlist );
713 emit inputChanged( p_input );
714 vlc_object_release( p_input );
716 pl_Release( p_intf );
720 /* Playlist Control functions */
721 void MainInputManager::stop()
723 playlist_Stop( THEPL );
726 void MainInputManager::next()
728 playlist_Next( THEPL );
731 void MainInputManager::prev()
733 playlist_Prev( THEPL );
736 void MainInputManager::togglePlayPause()
740 playlist_Play( THEPL );
742 getIM()->togglePlayPause();
745 /* Static callbacks */
748 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
749 vlc_value_t oldval, vlc_value_t newval, void *param )
751 InputManager *im = (InputManager*)param;
753 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
754 QApplication::postEvent( im, static_cast<QEvent*>(event) );
758 static int InputEvent( vlc_object_t *p_this, const char *,
759 vlc_value_t, vlc_value_t newval, void *param )
761 InputManager *im = (InputManager*)param;
764 switch( newval.i_int )
766 case INPUT_EVENT_STATE:
767 event = new IMEvent( ItemStateChanged_Type, 0 );
769 case INPUT_EVENT_RATE:
770 event = new IMEvent( ItemRateChanged_Type, 0 );
772 case INPUT_EVENT_TIMES:
773 event = new IMEvent( PositionUpdate_Type, 0 );
776 case INPUT_EVENT_TITLE:
777 case INPUT_EVENT_CHAPTER:
778 event = new IMEvent( ItemTitleChanged_Type, 0 );
782 event = new IMEvent( ItemEsChanged_Type, 0 );
784 case INPUT_EVENT_TELETEXT:
785 event = new IMEvent( ItemTeletextChanged_Type, 0 );
788 case INPUT_EVENT_VOUT:
789 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
792 case INPUT_EVENT_STATISTICS:
793 event = new IMEvent( StatisticsUpdate_Type, 0 );
796 case INPUT_EVENT_ITEM_META: /* Codec MetaData + Art */
797 event = new IMEvent( MetaChanged_Type, 0 );
799 case INPUT_EVENT_ITEM_INFO: /* Codec Info */
800 event = new IMEvent( InfoChanged_Type, 0 );
802 case INPUT_EVENT_ITEM_NAME:
803 event = new IMEvent( NameChanged_Type, 0 );
806 case INPUT_EVENT_PROGRAM:
807 case INPUT_EVENT_RECORD:
808 case INPUT_EVENT_SIGNAL:
809 case INPUT_EVENT_AUDIO_DELAY:
810 case INPUT_EVENT_SUBTITLE_DELAY:
811 case INPUT_EVENT_BOOKMARK:
812 case INPUT_EVENT_CACHE:
819 QApplication::postEvent( im, static_cast<QEvent*>(event) );
824 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
825 vlc_value_t oldval, vlc_value_t newval, void *param )
827 MainInputManager *mim = (MainInputManager*)param;
829 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
830 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
834 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
835 vlc_value_t oldval, vlc_value_t newval, void *param )
837 MainInputManager *mim = (MainInputManager*)param;
839 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
840 QApplication::postEvent( mim, static_cast<QEvent*>(event) );