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 *****************************************************************************/
31 #include "input_manager.hpp"
33 #include <QApplication>
35 static int ItemChanged( vlc_object_t *, const char *,
36 vlc_value_t, vlc_value_t, void * );
37 static int PLItemChanged( vlc_object_t *, const char *,
38 vlc_value_t, vlc_value_t, void * );
39 static int VolumeChanged( vlc_object_t *, const char *,
40 vlc_value_t, vlc_value_t, void * );
42 static int InputEvent( vlc_object_t *, const char *,
43 vlc_value_t, vlc_value_t, void * );
46 /**********************************************************************
47 * InputManager implementation
48 **********************************************************************
49 * The Input Manager can be the main one around the playlist
50 * But can also be used for VLM dialog or similar
51 **********************************************************************/
53 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
54 QObject( parent ), p_intf( _p_intf )
56 i_old_playing_status = END_S;
68 InputManager::~InputManager()
73 /* Define the Input used.
74 Add the callbacks on input
75 p_input is held once here */
76 void InputManager::setInput( input_thread_t *_p_input )
80 if( p_input && !( p_input->b_dead || !vlc_object_alive (p_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()
108 i_old_playing_status = END_S;
115 emit positionUpdated( -1.0, 0 ,0 );
116 emit statusChanged( END_S );
117 emit nameChanged( "" );
118 emit artChanged( NULL );
119 emit rateChanged( INPUT_RATE_DEFAULT );
120 emit voutChanged( false );
121 vlc_object_release( p_input );
127 /* Add the callbacks on Input. Self explanatory */
128 void InputManager::addCallbacks()
130 var_AddCallback( p_input, "intf-event", InputEvent, this );
133 /* Delete the callbacks on Input. Self explanatory */
134 void InputManager::delCallbacks()
136 var_DelCallback( p_input, "intf-event", InputEvent, this );
139 /* Convert the event from the callbacks in actions */
140 void InputManager::customEvent( QEvent *event )
142 int i_type = event->type();
143 IMEvent *ple = static_cast<IMEvent *>(event);
145 if ( i_type != PositionUpdate_Type &&
146 i_type != ItemChanged_Type &&
147 i_type != ItemRateChanged_Type &&
148 i_type != ItemTitleChanged_Type &&
149 i_type != ItemEsChanged_Type &&
150 i_type != ItemTeletextChanged_Type &&
151 i_type != ItemStateChanged_Type &&
152 i_type != StatisticsUpdate_Type &&
153 i_type != InterfaceVoutUpdate_Type &&
154 i_type != MetaChanged_Type )
157 if( !hasInput() ) return;
159 if( ( i_type != PositionUpdate_Type &&
160 i_type != ItemRateChanged_Type &&
161 i_type != ItemEsChanged_Type &&
162 i_type != ItemTeletextChanged_Type &&
163 i_type != ItemStateChanged_Type &&
164 i_type != StatisticsUpdate_Type &&
165 i_type != InterfaceVoutUpdate_Type &&
166 i_type != MetaChanged_Type
168 && ( i_input_id != ple->i_id ) )
171 if( i_type != PositionUpdate_Type &&
172 i_type != StatisticsUpdate_Type )
173 msg_Dbg( p_intf, "New Event: type %i", i_type );
178 case PositionUpdate_Type:
181 case StatisticsUpdate_Type:
184 case ItemChanged_Type:
189 case MetaChanged_Type:
193 case ItemStateChanged_Type:
199 case ItemTitleChanged_Type:
203 case ItemRateChanged_Type:
206 case ItemEsChanged_Type:
209 case ItemTeletextChanged_Type:
212 case InterfaceVoutUpdate_Type:
216 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
220 void InputManager::UpdateStats()
222 emit statisticsUpdated( input_GetItem( p_input ) );
225 void InputManager::UpdatePosition()
227 /* Update position */
228 int i_length, i_time; /* Int is enough, since we store seconds */
230 i_length = var_GetTime( p_input , "length" ) / 1000000;
231 i_time = var_GetTime( p_input , "time") / 1000000;
232 f_pos = var_GetFloat( p_input , "position" );
233 emit positionUpdated( f_pos, i_time, i_length );
236 void InputManager::UpdateNavigation()
238 /* Update navigation status */
239 vlc_value_t val; val.i_int = 0;
242 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
246 emit titleChanged( true );
247 /* p_input != NULL since val.i_int != 0 */
249 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
250 emit chapterChanged( (val.i_int > 0) );
253 emit titleChanged( false );
256 void InputManager::UpdateStatus()
258 /* Update playing status */
259 vlc_value_t val; val.i_int = 0;
260 var_Get( p_input, "state", &val );
261 if( i_old_playing_status != val.i_int )
263 i_old_playing_status = val.i_int;
264 emit statusChanged( val.i_int );
268 void InputManager::UpdateRate()
271 int i_new_rate = var_GetInteger( p_input, "rate");
272 if( i_new_rate != i_rate )
276 emit rateChanged( i_rate );
280 void InputManager::UpdateMeta()
282 /* Update text, name and nowplaying */
285 /* Try to get the Title, then the Name */
286 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
287 if( EMPTY_STR( psz_name ) )
290 psz_name = input_item_GetName( input_GetItem( p_input ) );
293 /* Try to get the nowplaying */
294 char *psz_nowplaying =
295 input_item_GetNowPlaying( input_GetItem( p_input ) );
296 if( !EMPTY_STR( psz_nowplaying ) )
298 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
300 else /* Do it ourself */
302 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
304 if( !EMPTY_STR( psz_artist ) )
305 text.sprintf( "%s - %s", psz_artist, psz_name );
307 text.sprintf( "%s", psz_name );
311 /* Free everything */
313 free( psz_nowplaying );
315 /* If we have Nothing */
318 psz_name = input_item_GetURI( input_GetItem( p_input ) );
319 text.sprintf( "%s", psz_name );
320 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
324 if( oldName != text )
326 emit nameChanged( text );
331 bool InputManager::hasAudio()
336 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
337 return val.i_int > 0;
342 void InputManager::UpdateSPU()
347 void InputManager::UpdateTeletext()
350 telexActivation( var_GetInteger( p_input, "teletext-es" ) >= 0 );
352 telexActivation( false );
355 void InputManager::UpdateVout()
359 bool b_old_video = b_video;
361 vlc_object_t *p_vout = (vlc_object_t*)vlc_object_find( p_input,
362 VLC_OBJECT_VOUT, FIND_CHILD );
363 b_video = p_vout != NULL;
365 vlc_object_release( p_vout );
366 if( !!b_old_video != !!b_video )
367 emit voutChanged( b_video );
371 void InputManager::UpdateArt()
373 /* Update Art meta */
374 emit artChanged( input_GetItem( p_input ) );
377 /* User update of the slider */
378 void InputManager::sliderUpdate( float new_pos )
381 var_SetFloat( p_input, "position", new_pos );
384 /* User togglePlayPause */
385 void InputManager::togglePlayPause()
390 var_Get( p_input, "state", &state );
391 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
392 var_Set( p_input, "state", state );
393 emit statusChanged( state.i_int );
397 void InputManager::sectionPrev()
401 int i_type = var_Type( p_input, "next-chapter" );
402 vlc_value_t val; val.b_bool = true;
403 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
404 "prev-chapter":"prev-title", val );
408 void InputManager::sectionNext()
412 int i_type = var_Type( p_input, "next-chapter" );
413 vlc_value_t val; val.b_bool = true;
414 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
415 "next-chapter":"next-title", val );
419 void InputManager::sectionMenu()
423 vlc_value_t val, text;
426 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
429 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
431 for( int i = 0; i < val.p_list->i_count; i++ )
433 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
436 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
438 var_Set( p_input, "title 0", root );
446 /* Set a new Teletext Page */
447 void InputManager::telexSetPage( int page )
451 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
452 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
454 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
456 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
457 "zvbi", FIND_ANYWHERE );
460 var_SetInteger( p_vbi, "vbi-page", page );
461 vlc_object_release( p_vbi );
462 emit newTelexPageSet( page );
468 /* Set the transparency on teletext */
469 void InputManager::telexSetTransparency( bool b_transparentTelextext )
473 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
474 "zvbi", FIND_ANYWHERE );
477 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
478 vlc_object_release( p_vbi );
479 emit teletextTransparencyActivated( b_transparentTelextext );
484 void InputManager::telexActivation( bool b_enabled )
488 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
489 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
491 /* Teletext is possible. Show the buttons */
492 b_enabled = (i_teletext_es >= 0);
493 emit teletextPossible( b_enabled );
494 if( !b_enabled ) return;
496 /* If Teletext is selected */
497 if( i_teletext_es == i_spu_es )
499 /* Activate the buttons */
500 teletextActivated( true );
502 /* Then, find the current page */
504 vlc_object_t *p_vbi = (vlc_object_t *)
505 vlc_object_find_name( p_input, "zvbi", FIND_ANYWHERE );
508 i_page = var_GetInteger( p_vbi, "vbi-page" );
509 vlc_object_release( p_vbi );
510 emit newTelexPageSet( i_page );
515 emit teletextPossible( b_enabled );
518 void InputManager::activateTeletext( bool b_enable )
522 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
523 if( i_teletext_es >= 0 )
525 var_SetInteger( p_input, "spu-es", b_enable ? i_teletext_es : -1 );
530 void InputManager::reverse()
534 int i_rate = var_GetInteger( p_input, "rate" );
535 var_SetInteger( p_input, "rate", -i_rate );
539 void InputManager::slower()
542 var_SetVoid( p_input, "rate-slower" );
545 void InputManager::faster()
548 var_SetVoid( p_input, "rate-faster" );
551 void InputManager::normalRate()
554 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
557 void InputManager::setRate( int new_rate )
560 var_SetInteger( p_input, "rate", new_rate );
563 void InputManager::setAtoB()
567 timeA = var_GetTime( THEMIM->getInput(), "time" );
571 timeB = var_GetTime( THEMIM->getInput(), "time" );
572 var_SetTime( THEMIM->getInput(), "time" , timeA );
579 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
582 /* Function called regularly when in an AtoB loop */
583 void InputManager::AtoBLoop( int i_time )
587 if( ( i_time >= (int)( timeB/1000000 ) )
588 || ( i_time < (int)( timeA/1000000 ) ) )
589 var_SetTime( THEMIM->getInput(), "time" , timeA );
593 /**********************************************************************
594 * MainInputManager implementation. Wrap an input manager and
595 * take care of updating the main playlist input.
596 * Used in the main playlist Dialog
597 **********************************************************************/
598 MainInputManager * MainInputManager::instance = NULL;
600 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
601 : QObject(NULL), p_intf( _p_intf )
604 im = new InputManager( this, p_intf );
606 var_AddCallback( THEPL, "item-change", ItemChanged, im );
607 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
608 var_AddCallback( THEPL, "activity", PLItemChanged, this );
610 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
612 /* Warn our embedded IM about input changes */
613 CONNECT( this, inputChanged( input_thread_t * ),
614 im, setInput( input_thread_t * ) );
616 /* emit check if playlist has allready started playing */
618 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
619 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
620 QApplication::postEvent( this, static_cast<QEvent*>(event) );
623 MainInputManager::~MainInputManager()
627 var_DelCallback( p_input, "state", PLItemChanged, this );
628 vlc_object_release( p_input );
629 emit inputChanged( NULL );
632 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
634 var_DelCallback( THEPL, "activity", PLItemChanged, this );
635 var_DelCallback( THEPL, "item-change", ItemChanged, im );
637 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
640 void MainInputManager::customEvent( QEvent *event )
642 int type = event->type();
643 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
646 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
647 if( type == VolumeChanged_Type )
649 emit volumeChanged();
653 /* Should be PLItemChanged Event */
654 if( !p_intf->p_sys->b_isDialogProvider )
656 vlc_mutex_lock( &p_intf->change_lock );
657 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
659 var_DelCallback( p_input, "state", PLItemChanged, this );
660 vlc_object_release( p_input );
661 emit inputChanged( NULL );
663 vlc_mutex_unlock( &p_intf->change_lock );
669 p_input = playlist_CurrentInput(THEPL);
672 var_AddCallback( p_input, "state", PLItemChanged, this );
673 emit inputChanged( p_input );
676 vlc_mutex_unlock( &p_intf->change_lock );
680 /* we are working as a dialogs provider */
681 playlist_t *p_playlist = pl_Hold( p_intf );
682 p_input = playlist_CurrentInput( p_playlist );
685 emit inputChanged( p_input );
686 vlc_object_release( p_input );
688 pl_Release( p_intf );
692 /* Playlist Control functions */
693 void MainInputManager::stop()
695 playlist_Stop( THEPL );
698 void MainInputManager::next()
700 playlist_Next( THEPL );
703 void MainInputManager::prev()
705 playlist_Prev( THEPL );
708 void MainInputManager::togglePlayPause()
712 playlist_Play( THEPL );
714 getIM()->togglePlayPause();
717 /* Static callbacks */
720 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
721 vlc_value_t oldval, vlc_value_t newval, void *param )
723 InputManager *im = (InputManager*)param;
725 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
726 QApplication::postEvent( im, static_cast<QEvent*>(event) );
730 static int InputEvent( vlc_object_t *p_this, const char *,
731 vlc_value_t, vlc_value_t newval, void *param )
733 InputManager *im = (InputManager*)param;
736 switch( newval.i_int )
738 case INPUT_EVENT_STATE:
739 event = new IMEvent( ItemStateChanged_Type, 0 );
741 case INPUT_EVENT_RATE:
742 event = new IMEvent( ItemRateChanged_Type, 0 );
744 case INPUT_EVENT_TIMES:
745 event = new IMEvent( PositionUpdate_Type, 0 );
748 case INPUT_EVENT_TITLE:
749 case INPUT_EVENT_CHAPTER: /* TODO is that correct ? */
750 event = new IMEvent( ItemTitleChanged_Type, 0 );
754 event = new IMEvent( ItemEsChanged_Type, 0 );
756 case INPUT_EVENT_TELETEXT:
757 event = new IMEvent( ItemTeletextChanged_Type, 0 );
760 case INPUT_EVENT_VOUT:
761 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
764 case INPUT_EVENT_STATISTICS:
765 event = new IMEvent( StatisticsUpdate_Type, 0 );
768 case INPUT_EVENT_ITEM_META:
769 case INPUT_EVENT_ITEM_INFO:
770 case INPUT_EVENT_ITEM_NAME:
771 event = new IMEvent( MetaChanged_Type, 0 );
774 case INPUT_EVENT_PROGRAM:
775 case INPUT_EVENT_RECORD:
776 case INPUT_EVENT_SIGNAL:
777 case INPUT_EVENT_AUDIO_DELAY:
778 case INPUT_EVENT_SUBTITLE_DELAY:
779 case INPUT_EVENT_BOOKMARK:
780 case INPUT_EVENT_CACHE:
787 QApplication::postEvent( im, static_cast<QEvent*>(event) );
792 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
793 vlc_value_t oldval, vlc_value_t newval, void *param )
795 MainInputManager *mim = (MainInputManager*)param;
797 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
798 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
802 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
803 vlc_value_t oldval, vlc_value_t newval, void *param )
805 MainInputManager *mim = (MainInputManager*)param;
807 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
808 QApplication::postEvent( mim, static_cast<QEvent*>(event) );