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 vlc_object_hold( p_input );
82 emit statusChanged( PLAYING_S );
89 i_input_id = input_GetItem( p_input )->i_id;
95 emit rateChanged( INPUT_RATE_DEFAULT );
99 /* delete Input if it ever existed.
100 Delete the callbacls on input
101 p_input is released once here */
102 void InputManager::delInput()
107 i_old_playing_status = END_S;
114 emit positionUpdated( -1.0, 0 ,0 );
115 emit statusChanged( END_S );
116 emit nameChanged( "" );
117 emit artChanged( NULL );
118 emit rateChanged( INPUT_RATE_DEFAULT );
119 emit voutChanged( false );
120 vlc_object_release( p_input );
126 /* Add the callbacks on Input. Self explanatory */
127 void InputManager::addCallbacks()
129 var_AddCallback( p_input, "intf-event", InputEvent, this );
132 /* Delete the callbacks on Input. Self explanatory */
133 void InputManager::delCallbacks()
135 var_DelCallback( p_input, "intf-event", InputEvent, this );
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 )
156 if( !hasInput() ) return;
158 if( ( i_type != PositionUpdate_Type &&
159 i_type != ItemRateChanged_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
167 && ( i_input_id != ple->i_id ) )
170 if( i_type != PositionUpdate_Type &&
171 i_type != StatisticsUpdate_Type )
172 msg_Dbg( p_intf, "New Event: type %i", i_type );
177 case PositionUpdate_Type:
180 case StatisticsUpdate_Type:
183 case ItemChanged_Type:
188 case MetaChanged_Type:
192 case ItemStateChanged_Type:
198 case ItemTitleChanged_Type:
202 case ItemRateChanged_Type:
205 case ItemEsChanged_Type:
208 case ItemTeletextChanged_Type:
211 case InterfaceVoutUpdate_Type:
215 msg_Warn( p_intf, "This shouldn't happen: %i", i_type );
219 void InputManager::UpdateStats()
221 emit statisticsUpdated( input_GetItem( p_input ) );
224 void InputManager::UpdatePosition()
226 /* Update position */
227 int i_length, i_time; /* Int is enough, since we store seconds */
229 i_length = var_GetTime( p_input , "length" ) / 1000000;
230 i_time = var_GetTime( p_input , "time") / 1000000;
231 f_pos = var_GetFloat( p_input , "position" );
232 emit positionUpdated( f_pos, i_time, i_length );
235 void InputManager::UpdateNavigation()
237 /* Update navigation status */
238 vlc_value_t val; val.i_int = 0;
241 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
245 emit titleChanged( true );
246 /* p_input != NULL since val.i_int != 0 */
248 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
249 emit chapterChanged( (val.i_int > 0) );
252 emit titleChanged( false );
255 void InputManager::UpdateStatus()
257 /* Update playing status */
258 vlc_value_t val; val.i_int = 0;
259 var_Get( p_input, "state", &val );
260 if( i_old_playing_status != val.i_int )
262 i_old_playing_status = val.i_int;
263 emit statusChanged( val.i_int );
267 void InputManager::UpdateRate()
270 int i_new_rate = var_GetInteger( p_input, "rate");
271 if( i_new_rate != i_rate )
275 emit rateChanged( i_rate );
279 void InputManager::UpdateMeta()
281 /* Update text, name and nowplaying */
284 /* Try to get the Title, then the Name */
285 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
286 if( EMPTY_STR( psz_name ) )
289 psz_name = input_item_GetName( input_GetItem( p_input ) );
292 /* Try to get the nowplaying */
293 char *psz_nowplaying =
294 input_item_GetNowPlaying( input_GetItem( p_input ) );
295 if( !EMPTY_STR( psz_nowplaying ) )
297 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
299 else /* Do it ourself */
301 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
303 if( !EMPTY_STR( psz_artist ) )
304 text.sprintf( "%s - %s", psz_artist, psz_name );
306 text.sprintf( "%s", psz_name );
310 /* Free everything */
312 free( psz_nowplaying );
314 /* If we have Nothing */
317 psz_name = input_item_GetURI( input_GetItem( p_input ) );
318 text.sprintf( "%s", psz_name );
319 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
323 if( oldName != text )
325 emit nameChanged( text );
330 bool InputManager::hasAudio()
335 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
336 return val.i_int > 0;
341 void InputManager::UpdateSPU()
346 void InputManager::UpdateTeletext()
349 telexActivation( var_GetInteger( p_input, "teletext-es" ) >= 0 );
351 telexActivation( false );
354 void InputManager::UpdateVout()
358 bool b_old_video = b_video;
360 vlc_object_t *p_vout = (vlc_object_t*)vlc_object_find( p_input,
361 VLC_OBJECT_VOUT, FIND_CHILD );
362 b_video = p_vout != NULL;
364 vlc_object_release( p_vout );
365 if( !!b_old_video != !!b_video )
366 emit voutChanged( b_video );
370 void InputManager::UpdateArt()
372 /* Update Art meta */
373 emit artChanged( input_GetItem( p_input ) );
376 /* User update of the slider */
377 void InputManager::sliderUpdate( float new_pos )
380 var_SetFloat( p_input, "position", new_pos );
383 /* User togglePlayPause */
384 void InputManager::togglePlayPause()
389 var_Get( p_input, "state", &state );
390 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
391 var_Set( p_input, "state", state );
392 emit statusChanged( state.i_int );
396 void InputManager::sectionPrev()
400 int i_type = var_Type( p_input, "next-chapter" );
401 vlc_value_t val; val.b_bool = true;
402 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
403 "prev-chapter":"prev-title", val );
407 void InputManager::sectionNext()
411 int i_type = var_Type( p_input, "next-chapter" );
412 vlc_value_t val; val.b_bool = true;
413 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
414 "next-chapter":"next-title", val );
418 void InputManager::sectionMenu()
422 vlc_value_t val, text;
425 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
428 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
430 for( int i = 0; i < val.p_list->i_count; i++ )
432 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
435 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
437 var_Set( p_input, "title 0", root );
445 /* Set a new Teletext Page */
446 void InputManager::telexSetPage( int page )
450 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
451 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
453 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
455 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
456 "zvbi", FIND_ANYWHERE );
459 var_SetInteger( p_vbi, "vbi-page", page );
460 vlc_object_release( p_vbi );
461 emit newTelexPageSet( page );
467 /* Set the transparency on teletext */
468 void InputManager::telexSetTransparency( bool b_transparentTelextext )
472 vlc_object_t *p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
473 "zvbi", FIND_ANYWHERE );
476 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
477 vlc_object_release( p_vbi );
478 emit teletextTransparencyActivated( b_transparentTelextext );
483 void InputManager::telexActivation( bool b_enabled )
487 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
488 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
490 /* Teletext is possible. Show the buttons */
491 b_enabled = (i_teletext_es >= 0);
492 emit teletextPossible( b_enabled );
493 if( !b_enabled ) return;
495 /* If Teletext is selected */
496 if( i_teletext_es == i_spu_es )
498 /* Activate the buttons */
499 teletextActivated( true );
501 /* Then, find the current page */
503 vlc_object_t *p_vbi = (vlc_object_t *)
504 vlc_object_find_name( p_input, "zvbi", FIND_ANYWHERE );
507 i_page = var_GetInteger( p_vbi, "vbi-page" );
508 vlc_object_release( p_vbi );
509 emit newTelexPageSet( i_page );
514 emit teletextPossible( b_enabled );
517 void InputManager::activateTeletext( bool b_enable )
521 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
522 if( i_teletext_es >= 0 )
524 var_SetInteger( p_input, "spu-es", b_enable ? i_teletext_es : -1 );
529 void InputManager::reverse()
533 int i_rate = var_GetInteger( p_input, "rate" );
534 var_SetInteger( p_input, "rate", -i_rate );
538 void InputManager::slower()
541 var_SetVoid( p_input, "rate-slower" );
544 void InputManager::faster()
547 var_SetVoid( p_input, "rate-faster" );
550 void InputManager::normalRate()
553 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
556 void InputManager::setRate( int new_rate )
559 var_SetInteger( p_input, "rate", new_rate );
562 void InputManager::setAtoB()
566 timeA = var_GetTime( THEMIM->getInput(), "time" );
570 timeB = var_GetTime( THEMIM->getInput(), "time" );
571 var_SetTime( THEMIM->getInput(), "time" , timeA );
578 emit AtoBchanged( (timeA != 0 ), (timeB != 0 ) );
581 /* Function called regularly when in an AtoB loop */
582 void InputManager::AtoBLoop( int i_time )
586 if( ( i_time >= (int)( timeB/1000000 ) )
587 || ( i_time < (int)( timeA/1000000 ) ) )
588 var_SetTime( THEMIM->getInput(), "time" , timeA );
592 /**********************************************************************
593 * MainInputManager implementation. Wrap an input manager and
594 * take care of updating the main playlist input.
595 * Used in the main playlist Dialog
596 **********************************************************************/
597 MainInputManager * MainInputManager::instance = NULL;
599 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
600 : QObject(NULL), p_intf( _p_intf )
603 im = new InputManager( this, p_intf );
605 var_AddCallback( THEPL, "item-change", ItemChanged, im );
606 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
607 var_AddCallback( THEPL, "activity", PLItemChanged, this );
609 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
611 /* Warn our embedded IM about input changes */
612 CONNECT( this, inputChanged( input_thread_t * ),
613 im, setInput( input_thread_t * ) );
615 /* emit check if playlist has allready started playing */
617 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
618 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
619 QApplication::postEvent( this, static_cast<QEvent*>(event) );
622 MainInputManager::~MainInputManager()
626 var_DelCallback( p_input, "state", PLItemChanged, this );
627 vlc_object_release( p_input );
628 emit inputChanged( NULL );
631 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
633 var_DelCallback( THEPL, "activity", PLItemChanged, this );
634 var_DelCallback( THEPL, "item-change", ItemChanged, im );
636 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
639 void MainInputManager::customEvent( QEvent *event )
641 int type = event->type();
642 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
645 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
646 if( type == VolumeChanged_Type )
648 emit volumeChanged();
652 /* Should be PLItemChanged Event */
653 if( !p_intf->p_sys->b_isDialogProvider )
655 vlc_mutex_lock( &p_intf->change_lock );
656 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
658 var_DelCallback( p_input, "state", PLItemChanged, this );
659 vlc_object_release( p_input );
660 emit inputChanged( NULL );
662 vlc_mutex_unlock( &p_intf->change_lock );
668 p_input = playlist_CurrentInput(THEPL);
671 var_AddCallback( p_input, "state", PLItemChanged, this );
672 emit inputChanged( p_input );
675 vlc_mutex_unlock( &p_intf->change_lock );
679 /* we are working as a dialogs provider */
680 playlist_t *p_playlist = pl_Hold( p_intf );
681 p_input = playlist_CurrentInput( p_playlist );
684 emit inputChanged( p_input );
685 vlc_object_release( p_input );
687 pl_Release( p_intf );
691 /* Playlist Control functions */
692 void MainInputManager::stop()
694 playlist_Stop( THEPL );
697 void MainInputManager::next()
699 playlist_Next( THEPL );
702 void MainInputManager::prev()
704 playlist_Prev( THEPL );
707 void MainInputManager::togglePlayPause()
711 playlist_Play( THEPL );
713 getIM()->togglePlayPause();
716 /* Static callbacks */
719 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
720 vlc_value_t oldval, vlc_value_t newval, void *param )
722 InputManager *im = (InputManager*)param;
724 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
725 QApplication::postEvent( im, static_cast<QEvent*>(event) );
729 static int InputEvent( vlc_object_t *p_this, const char *,
730 vlc_value_t, vlc_value_t newval, void *param )
732 InputManager *im = (InputManager*)param;
735 switch( newval.i_int )
737 case INPUT_EVENT_STATE:
738 event = new IMEvent( ItemStateChanged_Type, 0 );
740 case INPUT_EVENT_RATE:
741 event = new IMEvent( ItemRateChanged_Type, 0 );
743 case INPUT_EVENT_TIMES:
744 event = new IMEvent( PositionUpdate_Type, 0 );
747 case INPUT_EVENT_TITLE:
748 case INPUT_EVENT_CHAPTER: /* TODO is that correct ? */
749 event = new IMEvent( ItemTitleChanged_Type, 0 );
753 event = new IMEvent( ItemEsChanged_Type, 0 );
755 case INPUT_EVENT_TELETEXT:
756 event = new IMEvent( ItemTeletextChanged_Type, 0 );
759 case INPUT_EVENT_VOUT:
760 event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
763 case INPUT_EVENT_STATISTICS:
764 event = new IMEvent( StatisticsUpdate_Type, 0 );
767 case INPUT_EVENT_ITEM_META:
768 case INPUT_EVENT_ITEM_INFO:
769 case INPUT_EVENT_ITEM_NAME:
770 event = new IMEvent( MetaChanged_Type, 0 );
773 case INPUT_EVENT_PROGRAM:
774 case INPUT_EVENT_RECORD:
775 case INPUT_EVENT_SIGNAL:
776 case INPUT_EVENT_AUDIO_DELAY:
777 case INPUT_EVENT_SUBTITLE_DELAY:
778 case INPUT_EVENT_BOOKMARK:
779 case INPUT_EVENT_CACHE:
786 QApplication::postEvent( im, static_cast<QEvent*>(event) );
791 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
792 vlc_value_t oldval, vlc_value_t newval, void *param )
794 MainInputManager *mim = (MainInputManager*)param;
796 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
797 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
801 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
802 vlc_value_t oldval, vlc_value_t newval, void *param )
804 MainInputManager *mim = (MainInputManager*)param;
806 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
807 QApplication::postEvent( mim, static_cast<QEvent*>(event) );