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"
31 #include "dialogs_provider.hpp"
33 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
34 vlc_value_t n, void *param );
36 static int ChangeTeletext( vlc_object_t *p_this, const char *var, vlc_value_t o,
37 vlc_value_t n, void *param );
39 static int ItemChanged( vlc_object_t *, const char *,
40 vlc_value_t, vlc_value_t, void * );
41 static int PLItemChanged( vlc_object_t *, const char *,
42 vlc_value_t, vlc_value_t, void * );
43 static int InterfaceChanged( vlc_object_t *, const char *,
44 vlc_value_t, vlc_value_t, void * );
45 static int StatisticsUpdated( vlc_object_t *, const char *,
46 vlc_value_t, vlc_value_t, void * );
47 static int InterfaceVoutChanged( vlc_object_t *, const char *,
48 vlc_value_t, vlc_value_t, void * );
49 static int ItemStateChanged( vlc_object_t *, const char *,
50 vlc_value_t, vlc_value_t, void * );
51 static int ItemRateChanged( vlc_object_t *, const char *,
52 vlc_value_t, vlc_value_t, void * );
53 static int ItemTitleChanged( vlc_object_t *, const char *,
54 vlc_value_t, vlc_value_t, void * );
55 static int VolumeChanged( vlc_object_t *, const char *,
56 vlc_value_t, vlc_value_t, void * );
58 /**********************************************************************
59 * InputManager implementation
60 **********************************************************************
61 * The Input Manager can be the main one around the playlist
62 * But can also be used for VLM dialog or similar
63 **********************************************************************/
65 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
66 QObject( parent ), p_intf( _p_intf )
68 i_old_playing_status = END_S;
75 b_transparentTelextext = false;
78 InputManager::~InputManager()
83 /* Define the Input used.
84 Add the callbacks on input
85 p_input is held once here */
86 void InputManager::setInput( input_thread_t *_p_input )
90 if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
92 vlc_object_hold( p_input );
93 emit statusChanged( PLAYING_S );
101 i_input_id = input_GetItem( p_input )->i_id;
107 emit rateChanged( INPUT_RATE_DEFAULT );
111 /* delete Input if it ever existed.
112 Delete the callbacls on input
113 p_input is released once here */
114 void InputManager::delInput()
119 i_old_playing_status = END_S;
124 emit positionUpdated( -1.0, 0 ,0 );
125 emit statusChanged( END_S );
126 emit nameChanged( "" );
127 emit artChanged( NULL );
128 emit rateChanged( INPUT_RATE_DEFAULT );
129 emit voutChanged( false );
130 vlc_object_release( p_input );
137 /* Add the callbacks on Input. Self explanatory */
138 void InputManager::addCallbacks()
140 /* We don't care about:
146 - position, time, length, because they are included in intf-change
148 /* src/input/input.c:1629 */
149 var_AddCallback( p_input, "state", ItemStateChanged, this );
150 /* src/input/es-out.c:552 */
151 var_AddCallback( p_input, "spu-es", ChangeSPU, this );
152 /* emit UpdateStatus so that main_interface updates controls
153 * if there is new videotracks (mpeg-ts)*/
154 var_AddCallback( p_input, "video-es", ItemStateChanged, this );
155 /* src/input/es-out.c: */
156 var_AddCallback( p_input, "teletext-es", ChangeTeletext, this );
157 /* src/input/input.c:1765 */
158 var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
159 /* src/input/input.c:2003 */
160 var_AddCallback( p_input, "title", ItemTitleChanged, this );
161 /* src/input/input.c:734 for timers update*/
162 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
163 /* src/input/input.c:710 for statistics update*/
164 var_AddCallback( p_input, "stats-change", StatisticsUpdated, this );
165 /* src/input/input.c for vout creation/destruction */
166 var_AddCallback( p_input, "intf-change-vout", InterfaceVoutChanged, this );
169 /* Delete the callbacks on Input. Self explanatory */
170 void InputManager::delCallbacks()
172 var_DelCallback( p_input, "spu-es", ChangeSPU, this );
173 var_DelCallback( p_input, "video-es", ItemStateChanged, this );
174 var_DelCallback( p_input, "teletext-es", ChangeTeletext, this );
175 var_DelCallback( p_input, "state", ItemStateChanged, this );
176 var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
177 var_DelCallback( p_input, "title", ItemTitleChanged, this );
178 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
179 var_DelCallback( p_input, "stats-change", StatisticsUpdated, this );
180 var_DelCallback( p_input, "intf-change-vout", InterfaceVoutChanged, this );
183 /* Convert the event from the callbacks in actions */
184 void InputManager::customEvent( QEvent *event )
186 int type = event->type();
187 IMEvent *ple = static_cast<IMEvent *>(event);
189 if ( type != PositionUpdate_Type &&
190 type != ItemChanged_Type &&
191 type != ItemRateChanged_Type &&
192 type != ItemTitleChanged_Type &&
193 type != ItemSpuChanged_Type &&
194 type != ItemTeletextChanged_Type &&
195 type != ItemStateChanged_Type &&
196 type != StatisticsUpdate_Type &&
197 type != InterfaceVoutUpdate_Type )
200 if( type == ItemStateChanged_Type )
206 if( !hasInput() ) return;
208 if( ( type != PositionUpdate_Type &&
209 type != ItemRateChanged_Type &&
210 type != ItemSpuChanged_Type &&
211 type != ItemTeletextChanged_Type &&
212 type != ItemStateChanged_Type &&
213 type != StatisticsUpdate_Type &&
214 type != InterfaceVoutUpdate_Type
216 && ( i_input_id != ple->i_id ) )
219 if( type != PositionUpdate_Type &&
220 type != StatisticsUpdate_Type )
221 msg_Dbg( p_intf, "New Event: type %i", type );
226 case PositionUpdate_Type:
229 case StatisticsUpdate_Type:
232 case ItemChanged_Type:
237 case ItemStateChanged_Type:
242 case ItemTitleChanged_Type:
246 case ItemRateChanged_Type:
249 case ItemSpuChanged_Type:
252 case ItemTeletextChanged_Type:
255 case InterfaceVoutUpdate_Type:
261 void InputManager::UpdateStats()
263 emit statisticsUpdated( input_GetItem( p_input ) );
266 void InputManager::UpdatePosition()
268 /* Update position */
269 int i_length, i_time; /* Int is enough, since we store seconds */
271 i_length = var_GetTime( p_input , "length" ) / 1000000;
272 i_time = var_GetTime( p_input , "time") / 1000000;
273 f_pos = var_GetFloat( p_input , "position" );
274 emit positionUpdated( f_pos, i_time, i_length );
277 void InputManager::UpdateNavigation()
279 /* Update navigation status */
280 vlc_value_t val; val.i_int = 0;
282 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
286 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
287 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
291 emit navigationChanged( 0 );
295 void InputManager::UpdateStatus()
297 /* Update playing status */
298 vlc_value_t val; val.i_int = 0;
299 var_Get( p_input, "state", &val );
300 if( i_old_playing_status != val.i_int )
302 i_old_playing_status = val.i_int;
303 emit statusChanged( val.i_int );
307 void InputManager::UpdateRate()
310 int i_new_rate = var_GetInteger( p_input, "rate");
311 if( i_new_rate != i_rate )
315 emit rateChanged( i_rate );
319 void InputManager::UpdateMeta()
321 /* Update text, name and nowplaying */
324 /* Try to get the Title, then the Name */
325 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
326 if( EMPTY_STR( psz_name ) )
329 psz_name = input_item_GetName( input_GetItem( p_input ) );
332 /* Try to get the nowplaying */
333 char *psz_nowplaying =
334 input_item_GetNowPlaying( input_GetItem( p_input ) );
335 if( !EMPTY_STR( psz_nowplaying ) )
337 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
339 else /* Do it ourself */
341 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
343 if( !EMPTY_STR( psz_artist ) )
344 text.sprintf( "%s - %s", psz_artist, psz_name );
346 text.sprintf( "%s", psz_name );
350 /* Free everything */
352 free( psz_nowplaying );
354 /* If we have Nothing */
357 psz_name = input_item_GetURI( input_GetItem( p_input ) );
358 text.sprintf( "%s", psz_name );
359 text = text.remove( 0, text.lastIndexOf( DIR_SEP ) + 1 );
363 if( old_name != text )
365 emit nameChanged( text );
370 bool InputManager::hasAudio()
375 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
376 return val.i_int > 0;
381 void InputManager::UpdateSPU()
386 void InputManager::UpdateTeletext()
389 telexToggle( var_GetInteger( p_input, "teletext-es" ) >= 0 );
391 telexToggle( false );
394 void InputManager::UpdateVout()
398 bool b_old_video = b_video;
400 vlc_object_t *p_vout = (vlc_object_t*)vlc_object_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
401 b_video = p_vout != NULL;
403 vlc_object_release( p_vout );
404 if( !!b_old_video != !!b_video )
405 emit voutChanged( b_video );
409 void InputManager::UpdateArt()
411 /* Update Art meta */
412 emit artChanged( input_GetItem( p_input ) );
415 /* User update of the slider */
416 void InputManager::sliderUpdate( float new_pos )
419 var_SetFloat( p_input, "position", new_pos );
422 /* User togglePlayPause */
423 void InputManager::togglePlayPause()
426 var_Get( p_input, "state", &state );
427 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
428 var_Set( p_input, "state", state );
429 emit statusChanged( state.i_int );
432 void InputManager::sectionPrev()
436 int i_type = var_Type( p_input, "next-chapter" );
437 vlc_value_t val; val.b_bool = true;
438 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
439 "prev-chapter":"prev-title", val );
443 void InputManager::sectionNext()
447 int i_type = var_Type( p_input, "next-chapter" );
448 vlc_value_t val; val.b_bool = true;
449 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
450 "next-chapter":"next-title", val );
454 void InputManager::sectionMenu()
458 vlc_value_t val, text;
461 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
464 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
466 for( int i = 0; i < val.p_list->i_count; i++ )
468 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
471 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
473 var_Set( p_input, "title 0", root );
477 void InputManager::telexGotoPage( int page )
481 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
482 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
484 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
487 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
488 "zvbi", FIND_ANYWHERE );
491 var_SetInteger( p_vbi, "vbi-page", page );
492 vlc_object_release( p_vbi );
496 emit setNewTelexPage( page );
499 void InputManager::telexToggle( bool b_enabled )
503 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
504 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
506 b_enabled = (i_teletext_es >= 0);
507 emit teletextEnabled( b_enabled );
508 if( b_enabled && (i_teletext_es == i_spu_es) )
512 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
513 "zvbi", FIND_ANYWHERE );
516 i_page = var_GetInteger( p_vbi, "vbi-page" );
517 vlc_object_release( p_vbi );
518 i_page = b_enabled ? i_page : 0;
519 telexGotoPage( i_page );
523 else emit teletextEnabled( b_enabled );
526 void InputManager::telexToggleButtons()
530 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
531 if( i_teletext_es >= 0 )
533 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
535 if( i_teletext_es == i_spu_es )
536 var_SetInteger( p_input, "spu-es", -1 );
538 var_SetInteger( p_input, "spu-es", i_teletext_es );
540 emit toggleTelexButtons();
545 void InputManager::telexSetTransparency()
550 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
551 "zvbi", FIND_ANYWHERE );
554 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
555 b_transparentTelextext = !b_transparentTelextext;
556 vlc_object_release( p_vbi );
559 emit toggleTelexTransparency();
562 void InputManager::slower()
565 var_SetVoid( p_input, "rate-slower" );
568 void InputManager::faster()
571 var_SetVoid( p_input, "rate-faster" );
574 void InputManager::normalRate()
577 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
580 void InputManager::setRate( int new_rate )
583 var_SetInteger( p_input, "rate", new_rate );
586 /**********************************************************************
587 * MainInputManager implementation. Wrap an input manager and
588 * take care of updating the main playlist input.
589 * Used in the main playlist Dialog
590 **********************************************************************/
591 MainInputManager * MainInputManager::instance = NULL;
593 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
594 : QObject(NULL), p_intf( _p_intf )
597 im = new InputManager( this, p_intf );
599 // var_AddCallback( THEPL, "item-change", PLItemChanged, this );
600 var_AddCallback( THEPL, "item-change", ItemChanged, im );
601 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
602 var_AddCallback( THEPL, "activity", PLItemChanged, this );
604 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
606 /* Warn our embedded IM about input changes */
607 CONNECT( this, inputChanged( input_thread_t * ),
608 im, setInput( input_thread_t * ) );
610 /* emit check if playlist has allready started playing */
612 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
613 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
614 QApplication::postEvent( this, static_cast<QEvent*>(event) );
618 MainInputManager::~MainInputManager()
622 var_DelCallback( p_input, "state", PLItemChanged, this );
623 vlc_object_release( p_input );
624 emit inputChanged( NULL );
627 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
629 var_DelCallback( THEPL, "activity", PLItemChanged, this );
630 var_DelCallback( THEPL, "item-change", ItemChanged, im );
631 // var_DelCallback( THEPL, "item-change", PLItemChanged, this );
633 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
636 void MainInputManager::customEvent( QEvent *event )
638 int type = event->type();
639 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
642 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
643 if( type == VolumeChanged_Type )
645 emit volumeChanged();
649 /* Should be PLItemChanged Event */
650 if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
652 vlc_mutex_lock( &p_intf->change_lock );
653 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
655 var_DelCallback( p_input, "state", PLItemChanged, this );
656 vlc_object_release( p_input );
657 emit inputChanged( NULL );
659 vlc_mutex_unlock( &p_intf->change_lock );
665 p_input = playlist_CurrentInput(THEPL);
668 var_AddCallback( p_input, "state", PLItemChanged, this );
669 emit inputChanged( p_input );
672 vlc_mutex_unlock( &p_intf->change_lock );
676 /* we are working as a dialogs provider */
677 playlist_t *p_playlist = pl_Hold( p_intf );
678 p_input = playlist_CurrentInput( p_playlist );
681 emit inputChanged( p_input );
682 vlc_object_release( p_input );
684 pl_Release( p_intf );
688 /* Playlist Control functions */
689 void MainInputManager::stop()
691 playlist_Stop( THEPL );
694 void MainInputManager::next()
696 playlist_Next( THEPL );
699 void MainInputManager::prev()
701 playlist_Prev( THEPL );
704 void MainInputManager::togglePlayPause()
706 if( p_input == NULL )
708 playlist_Play( THEPL );
711 getIM()->togglePlayPause();
714 bool MainInputManager::teletextState()
719 const int i_teletext_es = var_GetInteger( getInput(), "teletext-es" );
720 const int i_spu_es = var_GetInteger( getInput(), "spu-es" );
722 return i_teletext_es >= 0 && i_teletext_es == i_spu_es;
727 /* Static callbacks */
730 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
731 vlc_value_t oldval, vlc_value_t newval, void *param )
733 /* FIXME remove that static variable */
734 static int counter = 0;
735 InputManager *im = (InputManager*)param;
737 counter = ++counter % 4;
740 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
741 QApplication::postEvent( im, static_cast<QEvent*>(event) );
745 static int StatisticsUpdated( vlc_object_t *p_this, const char *psz_var,
746 vlc_value_t oldval, vlc_value_t newval, void *param )
748 InputManager *im = (InputManager*)param;
750 IMEvent *event = new IMEvent( StatisticsUpdate_Type, 0 );
751 QApplication::postEvent( im, static_cast<QEvent*>(event) );
755 static int InterfaceVoutChanged( vlc_object_t *p_this, const char *psz_var,
756 vlc_value_t oldval, vlc_value_t newval, void *param )
758 InputManager *im = (InputManager*)param;
760 IMEvent *event = new IMEvent( InterfaceVoutUpdate_Type, 0 );
761 QApplication::postEvent( im, static_cast<QEvent*>(event) );
765 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
766 vlc_value_t oldval, vlc_value_t newval, void *param )
768 InputManager *im = (InputManager*)param;
770 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
771 QApplication::postEvent( im, static_cast<QEvent*>(event) );
775 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
776 vlc_value_t oldval, vlc_value_t newval, void *param )
778 InputManager *im = (InputManager*)param;
780 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
781 QApplication::postEvent( im, static_cast<QEvent*>(event) );
785 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
786 vlc_value_t oldval, vlc_value_t newval, void *param )
788 InputManager *im = (InputManager*)param;
790 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
791 QApplication::postEvent( im, static_cast<QEvent*>(event) );
795 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
796 vlc_value_t oldval, vlc_value_t newval, void *param )
798 InputManager *im = (InputManager*)param;
800 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
801 QApplication::postEvent( im, static_cast<QEvent*>(event) );
805 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
806 vlc_value_t n, void *param )
808 InputManager *im = (InputManager*)param;
809 IMEvent *event = new IMEvent( ItemSpuChanged_Type, 0 );
810 QApplication::postEvent( im, static_cast<QEvent*>(event) );
814 static int ChangeTeletext( vlc_object_t *p_this, const char *var, vlc_value_t o,
815 vlc_value_t n, void *param )
818 InputManager *im = (InputManager*)param;
819 IMEvent *event = new IMEvent( ItemTeletextChanged_Type, 0 );
820 QApplication::postEvent( im, static_cast<QEvent*>(event) );
825 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
826 vlc_value_t oldval, vlc_value_t newval, void *param )
828 MainInputManager *mim = (MainInputManager*)param;
830 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
831 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
835 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
836 vlc_value_t oldval, vlc_value_t newval, void *param )
838 MainInputManager *mim = (MainInputManager*)param;
840 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
841 QApplication::postEvent( mim, static_cast<QEvent*>(event) );