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 );
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 InterfaceChanged( vlc_object_t *, const char *,
40 vlc_value_t, vlc_value_t, void * );
41 static int ItemStateChanged( vlc_object_t *, const char *,
42 vlc_value_t, vlc_value_t, void * );
43 static int ItemRateChanged( vlc_object_t *, const char *,
44 vlc_value_t, vlc_value_t, void * );
45 static int ItemTitleChanged( vlc_object_t *, const char *,
46 vlc_value_t, vlc_value_t, void * );
47 static int VolumeChanged( vlc_object_t *, const char *,
48 vlc_value_t, vlc_value_t, void * );
50 /**********************************************************************
51 * InputManager implementation
52 **********************************************************************
53 * The Input Manager can be the main one around the playlist
54 * But can also be used for VLM dialog or similar
55 **********************************************************************/
57 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
58 QObject( parent ), p_intf( _p_intf )
60 i_old_playing_status = END_S;
66 b_transparentTelextext = false;
69 InputManager::~InputManager()
74 /* Define the Input used.
75 Add the callbacks on input
76 p_input is yield once here */
77 void InputManager::setInput( input_thread_t *_p_input )
81 if( p_input && !( p_input->b_dead || p_input->b_die ) )
83 vlc_object_yield( p_input );
84 emit statusChanged( PLAYING_S );
90 i_input_id = input_GetItem( p_input )->i_id;
96 emit rateChanged( INPUT_RATE_DEFAULT );
101 /* delete Input if it ever existed.
102 Delete the callbacls on input
103 p_input is released once here */
104 void InputManager::delInput()
109 i_old_playing_status = END_S;
113 emit positionUpdated( 0.0, 0 ,0 );
114 emit statusChanged( END_S );
115 emit nameChanged( "" );
116 emit artChanged( "" );
117 emit rateChanged( INPUT_RATE_DEFAULT );
118 vlc_object_release( p_input );
124 /* Add the callbacks on Input. Self explanatory */
125 void InputManager::addCallbacks()
127 /* We don't care about:
133 - position, time, length, because they are included in intf-change
135 /* src/input/input.c:1629 */
136 var_AddCallback( p_input, "state", ItemStateChanged, this );
137 /* src/input/es-out.c:552 */
138 var_AddCallback( p_input, "spu-es", ChangeSPU, this );
139 /* src/input/input.c:1765 */
140 var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
141 /* src/input/input.c:2003 */
142 var_AddCallback( p_input, "title", ItemTitleChanged, this );
143 /* src/input/input.c:734 for timers update*/
144 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
147 /* Delete the callbacks on Input. Self explanatory */
148 void InputManager::delCallbacks()
150 var_DelCallback( p_input, "spu-es", ChangeSPU, this );
151 var_DelCallback( p_input, "state", ItemStateChanged, this );
152 var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
153 var_DelCallback( p_input, "title", ItemTitleChanged, this );
154 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
157 /* Convert the event from the callbacks in actions */
158 void InputManager::customEvent( QEvent *event )
160 int type = event->type();
161 IMEvent *ple = static_cast<IMEvent *>(event);
163 if ( type != PositionUpdate_Type &&
164 type != ItemChanged_Type &&
165 type != ItemRateChanged_Type &&
166 type != ItemTitleChanged_Type &&
167 type != ItemSpuChanged_Type &&
168 type != ItemStateChanged_Type )
171 if( !hasInput() ) return;
173 if( ( type != PositionUpdate_Type &&
174 type != ItemRateChanged_Type &&
175 type != ItemSpuChanged_Type &&
176 type != ItemStateChanged_Type
178 && ( i_input_id != ple->i_id ) )
181 if( type != PositionUpdate_Type )
182 msg_Dbg( p_intf, "New Event: type %i", type );
187 case PositionUpdate_Type:
190 case ItemChanged_Type:
196 case ItemRateChanged_Type:
199 case ItemTitleChanged_Type:
203 case ItemStateChanged_Type:
205 case ItemSpuChanged_Type:
211 void InputManager::UpdatePosition()
213 /* Update position */
214 int i_length, i_time; /* Int is enough, since we store seconds */
216 i_length = var_GetTime( p_input , "length" ) / 1000000;
217 i_time = var_GetTime( p_input , "time") / 1000000;
218 f_pos = var_GetFloat( p_input , "position" );
219 emit positionUpdated( f_pos, i_time, i_length );
222 void InputManager::UpdateNavigation()
224 /* Update navigation status */
225 vlc_value_t val; val.i_int = 0;
226 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
230 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
231 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
235 emit navigationChanged( 0 );
239 void InputManager::UpdateStatus()
241 /* Update playing status */
242 vlc_value_t val; val.i_int = 0;
243 var_Get( p_input, "state", &val );
244 if( i_old_playing_status != val.i_int )
246 i_old_playing_status = val.i_int;
247 emit statusChanged( val.i_int );
251 void InputManager::UpdateRate()
254 int i_new_rate = var_GetInteger( p_input, "rate");
255 if( i_new_rate != i_rate )
259 emit rateChanged( i_rate );
263 void InputManager::UpdateMeta()
265 /* Update text, name and nowplaying */
268 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
269 if( EMPTY_STR( psz_name ) )
272 psz_name = input_item_GetName( input_GetItem( p_input ) );
275 char *psz_nowplaying =
276 input_item_GetNowPlaying( input_GetItem( p_input ) );
277 if( !EMPTY_STR( psz_nowplaying ) )
279 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
283 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
284 if( !EMPTY_STR( psz_artist ) )
286 text.sprintf( "%s - %s", psz_artist, psz_name );
290 text.sprintf( "%s", psz_name );
295 free( psz_nowplaying );
297 if( old_name != text )
299 emit nameChanged( text );
304 bool InputManager::hasAudio()
309 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
310 return val.i_int > 0;
315 bool InputManager::hasVideo()
320 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
321 return val.i_int > 0;
327 void InputManager::UpdateSPU()
332 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
333 telexToggle( val.i_int > 0 );
337 telexToggle( false );
341 void InputManager::UpdateArt()
343 /* Update Art meta */
345 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
346 url.sprintf("%s", psz_art );
350 artUrl = url.replace( "file://",QString("" ) );
351 /* Taglib seems to define a attachment://, It won't work yet */
352 artUrl = url.replace( "attachment://",QString("" ) );
353 emit artChanged( artUrl );
354 msg_Dbg( p_intf, "Art: %s", qtu( artUrl ) );
358 /* User update of the slider */
359 void InputManager::sliderUpdate( float new_pos )
362 var_SetFloat( p_input, "position", new_pos );
365 /* User togglePlayPause */
366 void InputManager::togglePlayPause()
369 var_Get( p_input, "state", &state );
370 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
371 var_Set( p_input, "state", state );
372 emit statusChanged( state.i_int );
375 void InputManager::sectionPrev()
379 int i_type = var_Type( p_input, "next-chapter" );
380 vlc_value_t val; val.b_bool = true;
381 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
382 "prev-chapter":"prev-title", val );
386 void InputManager::sectionNext()
390 int i_type = var_Type( p_input, "next-chapter" );
391 vlc_value_t val; val.b_bool = true;
392 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
393 "next-chapter":"next-title", val );
397 void InputManager::sectionMenu()
401 vlc_value_t val; val.i_int = 2;
402 var_Set( p_input, "title 0", val );
406 void InputManager::telexGotoPage( int page )
411 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
412 "zvbi", FIND_ANYWHERE );
415 var_SetInteger( p_vbi, "vbi-page", page );
416 vlc_object_release( p_vbi );
419 emit setNewTelexPage( page );
422 void InputManager::telexToggle( bool b_enabled )
430 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
431 b_enabled = (val.i_int > 0);
432 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
433 "zvbi", FIND_ANYWHERE );
436 i_page = var_GetInteger( p_vbi, "vbi-page" );
437 vlc_object_release( p_vbi );
438 i_page = b_enabled ? i_page : 0;
439 telexGotoPage( i_page );
442 emit teletextEnabled( b_enabled );
445 void InputManager::telexToggleButtons()
447 emit toggleTelexButtons();
450 void InputManager::telexSetTransparency()
455 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
456 "zvbi", FIND_ANYWHERE );
459 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
460 b_transparentTelextext = !b_transparentTelextext;
461 vlc_object_release( p_vbi );
464 emit toggleTelexTransparency();
467 void InputManager::slower()
470 var_SetVoid( p_input, "rate-slower" );
473 void InputManager::faster()
476 var_SetVoid( p_input, "rate-faster" );
479 void InputManager::normalRate()
482 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
485 void InputManager::setRate( int new_rate )
488 var_SetInteger( p_input, "rate", new_rate );
491 /**********************************************************************
492 * MainInputManager implementation. Wrap an input manager and
493 * take care of updating the main playlist input.
494 * Used in the main playlist Dialog
495 **********************************************************************/
496 MainInputManager * MainInputManager::instance = NULL;
498 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
499 : QObject(NULL), p_intf( _p_intf )
502 im = new InputManager( this, p_intf );
504 // var_AddCallback( THEPL, "item-change", PLItemChanged, this );
505 var_AddCallback( THEPL, "item-change", ItemChanged, im );
506 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
507 var_AddCallback( THEPL, "activity", PLItemChanged, this );
509 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
511 // No necessary, I think TODO REMOVE ME at the end
512 //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
514 /* Warn our embedded IM about input changes */
515 CONNECT( this, inputChanged( input_thread_t * ),
516 im, setInput( input_thread_t * ) );
518 /* emit check if playlist has allready started playing */
520 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
521 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
522 QApplication::postEvent( this, static_cast<QEvent*>(event) );
526 MainInputManager::~MainInputManager()
530 var_DelCallback( p_input, "state", PLItemChanged, this );
531 vlc_object_release( p_input );
532 emit inputChanged( NULL );
535 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
537 var_DelCallback( THEPL, "activity", PLItemChanged, this );
538 var_DelCallback( THEPL, "item-change", ItemChanged, im );
539 // var_DelCallback( THEPL, "item-change", PLItemChanged, this );
541 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
544 void MainInputManager::customEvent( QEvent *event )
546 int type = event->type();
547 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
550 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
551 if( type == VolumeChanged_Type )
553 emit volumeChanged();
557 /* Should be PLItemChanged Event */
558 if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
560 vlc_mutex_lock( &p_intf->change_lock );
561 if( p_input && ( p_input->b_dead || p_input->b_die ) )
563 var_DelCallback( p_input, "state", PLItemChanged, this );
564 vlc_object_release( p_input );
565 emit inputChanged( NULL );
567 vlc_mutex_unlock( &p_intf->change_lock );
574 p_input = THEPL->p_input;
575 if( p_input && !( p_input->b_die || p_input->b_dead) )
577 vlc_object_yield( p_input );
578 var_AddCallback( p_input, "state", PLItemChanged, this );
579 emit inputChanged( p_input );
585 vlc_mutex_unlock( &p_intf->change_lock );
589 /* we are working as a dialogs provider */
590 playlist_t *p_playlist = pl_Yield( p_intf );
591 p_input = p_playlist->p_input;
592 emit inputChanged( p_input );
596 /* Playlist Control functions */
597 void MainInputManager::stop()
599 playlist_Stop( THEPL );
602 void MainInputManager::next()
604 playlist_Next( THEPL );
607 void MainInputManager::prev()
609 playlist_Prev( THEPL );
612 void MainInputManager::togglePlayPause()
614 if( p_input == NULL )
616 playlist_Play( THEPL );
619 getIM()->togglePlayPause();
622 bool MainInputManager::teletextState()
629 p_vbi = (vlc_object_t *) vlc_object_find_name( getInput(),
630 "zvbi", FIND_ANYWHERE );
633 vlc_object_release( p_vbi );
636 var_Change( getInput(), "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
637 return (val.i_int > 0);
642 /* Static callbacks */
645 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
646 vlc_value_t oldval, vlc_value_t newval, void *param )
648 static int counter = 0;
649 InputManager *im = (InputManager*)param;
651 counter = ++counter % 4;
654 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
655 QApplication::postEvent( im, static_cast<QEvent*>(event) );
659 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
660 vlc_value_t oldval, vlc_value_t newval, void *param )
662 InputManager *im = (InputManager*)param;
664 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
665 QApplication::postEvent( im, static_cast<QEvent*>(event) );
669 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
670 vlc_value_t oldval, vlc_value_t newval, void *param )
672 InputManager *im = (InputManager*)param;
674 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
675 QApplication::postEvent( im, static_cast<QEvent*>(event) );
679 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
680 vlc_value_t oldval, vlc_value_t newval, void *param )
682 InputManager *im = (InputManager*)param;
684 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
685 QApplication::postEvent( im, static_cast<QEvent*>(event) );
689 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
690 vlc_value_t oldval, vlc_value_t newval, void *param )
692 InputManager *im = (InputManager*)param;
694 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
695 QApplication::postEvent( im, static_cast<QEvent*>(event) );
699 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
700 vlc_value_t n, void *param )
702 InputManager *im = (InputManager*)param;
703 IMEvent *event = new IMEvent( ItemSpuChanged_Type, 0 );
704 QApplication::postEvent( im, static_cast<QEvent*>(event) );
710 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
711 vlc_value_t oldval, vlc_value_t newval, void *param )
713 MainInputManager *mim = (MainInputManager*)param;
715 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
716 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
720 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
721 vlc_value_t oldval, vlc_value_t newval, void *param )
723 MainInputManager *mim = (MainInputManager*)param;
725 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
726 QApplication::postEvent( mim, static_cast<QEvent*>(event) );