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 ItemStateChanged( vlc_object_t *, const char *,
46 vlc_value_t, vlc_value_t, void * );
47 static int ItemRateChanged( vlc_object_t *, const char *,
48 vlc_value_t, vlc_value_t, void * );
49 static int ItemTitleChanged( vlc_object_t *, const char *,
50 vlc_value_t, vlc_value_t, void * );
51 static int VolumeChanged( vlc_object_t *, const char *,
52 vlc_value_t, vlc_value_t, void * );
54 /**********************************************************************
55 * InputManager implementation
56 **********************************************************************
57 * The Input Manager can be the main one around the playlist
58 * But can also be used for VLM dialog or similar
59 **********************************************************************/
61 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
62 QObject( parent ), p_intf( _p_intf )
64 i_old_playing_status = END_S;
70 b_transparentTelextext = false;
73 InputManager::~InputManager()
78 /* Define the Input used.
79 Add the callbacks on input
80 p_input is yield once here */
81 void InputManager::setInput( input_thread_t *_p_input )
85 if( p_input && !( p_input->b_dead || !vlc_object_alive (p_input) ) )
87 vlc_object_yield( p_input );
88 emit statusChanged( PLAYING_S );
95 i_input_id = input_GetItem( p_input )->i_id;
101 emit rateChanged( INPUT_RATE_DEFAULT );
106 /* delete Input if it ever existed.
107 Delete the callbacls on input
108 p_input is released once here */
109 void InputManager::delInput()
114 i_old_playing_status = END_S;
118 emit positionUpdated( 0.0, 0 ,0 );
119 emit statusChanged( END_S );
120 emit nameChanged( "" );
121 emit artChanged( "" );
122 emit rateChanged( INPUT_RATE_DEFAULT );
123 vlc_object_release( p_input );
130 /* Add the callbacks on Input. Self explanatory */
131 void InputManager::addCallbacks()
133 /* We don't care about:
139 - position, time, length, because they are included in intf-change
141 /* src/input/input.c:1629 */
142 var_AddCallback( p_input, "state", ItemStateChanged, this );
143 /* src/input/es-out.c:552 */
144 var_AddCallback( p_input, "spu-es", ChangeSPU, this );
145 /* src/input/es-out.c: */
146 var_AddCallback( p_input, "teletext-es", ChangeTeletext, this );
147 /* src/input/input.c:1765 */
148 var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
149 /* src/input/input.c:2003 */
150 var_AddCallback( p_input, "title", ItemTitleChanged, this );
151 /* src/input/input.c:734 for timers update*/
152 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
155 /* Delete the callbacks on Input. Self explanatory */
156 void InputManager::delCallbacks()
158 var_DelCallback( p_input, "spu-es", ChangeSPU, this );
159 var_DelCallback( p_input, "teletext-es", ChangeTeletext, this );
160 var_DelCallback( p_input, "state", ItemStateChanged, this );
161 var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
162 var_DelCallback( p_input, "title", ItemTitleChanged, this );
163 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
166 /* Convert the event from the callbacks in actions */
167 void InputManager::customEvent( QEvent *event )
169 int type = event->type();
170 IMEvent *ple = static_cast<IMEvent *>(event);
172 if ( type != PositionUpdate_Type &&
173 type != ItemChanged_Type &&
174 type != ItemRateChanged_Type &&
175 type != ItemTitleChanged_Type &&
176 type != ItemSpuChanged_Type &&
177 type != ItemTeletextChanged_Type &&
178 type != ItemStateChanged_Type )
181 if( !hasInput() ) return;
183 if( ( type != PositionUpdate_Type &&
184 type != ItemRateChanged_Type &&
185 type != ItemSpuChanged_Type &&
186 type != ItemTeletextChanged_Type &&
187 type != ItemStateChanged_Type
189 && ( i_input_id != ple->i_id ) )
192 if( type != PositionUpdate_Type )
193 msg_Dbg( p_intf, "New Event: type %i", type );
198 case PositionUpdate_Type:
201 case ItemChanged_Type:
206 case ItemStateChanged_Type:
211 case ItemTitleChanged_Type:
215 case ItemRateChanged_Type:
218 case ItemSpuChanged_Type:
221 case ItemTeletextChanged_Type:
227 void InputManager::UpdatePosition()
229 /* Update position */
230 int i_length, i_time; /* Int is enough, since we store seconds */
232 i_length = var_GetTime( p_input , "length" ) / 1000000;
233 i_time = var_GetTime( p_input , "time") / 1000000;
234 f_pos = var_GetFloat( p_input , "position" );
235 emit positionUpdated( f_pos, i_time, i_length );
238 void InputManager::UpdateNavigation()
240 /* Update navigation status */
241 vlc_value_t val; val.i_int = 0;
242 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
246 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
247 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
251 emit navigationChanged( 0 );
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 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
285 if( EMPTY_STR( psz_name ) )
288 psz_name = input_item_GetName( input_GetItem( p_input ) );
291 char *psz_nowplaying =
292 input_item_GetNowPlaying( input_GetItem( p_input ) );
293 if( !EMPTY_STR( psz_nowplaying ) )
295 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
299 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
300 if( !EMPTY_STR( psz_artist ) )
302 text.sprintf( "%s - %s", psz_artist, psz_name );
306 text.sprintf( "%s", psz_name );
311 free( psz_nowplaying );
313 if( old_name != text )
315 emit nameChanged( text );
320 bool InputManager::hasAudio()
325 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
326 return val.i_int > 0;
331 bool InputManager::hasVideo()
336 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
337 return val.i_int > 0;
343 void InputManager::UpdateSPU()
348 void InputManager::UpdateTeletext()
351 telexToggle( var_GetInteger( p_input, "teletext-es" ) >= 0 );
353 telexToggle( false );
358 void InputManager::UpdateArt()
360 /* Update Art meta */
362 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
363 url.sprintf("%s", psz_art );
367 artUrl = url.replace( "file://",QString("" ) );
368 /* Taglib seems to define a attachment://, It won't work yet */
369 artUrl = url.replace( "attachment://",QString("" ) );
370 emit artChanged( artUrl );
371 msg_Dbg( p_intf, "Art: %s", qtu( artUrl ) );
375 /* User update of the slider */
376 void InputManager::sliderUpdate( float new_pos )
379 var_SetFloat( p_input, "position", new_pos );
382 /* User togglePlayPause */
383 void InputManager::togglePlayPause()
386 var_Get( p_input, "state", &state );
387 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
388 var_Set( p_input, "state", state );
389 emit statusChanged( state.i_int );
392 void InputManager::sectionPrev()
396 int i_type = var_Type( p_input, "next-chapter" );
397 vlc_value_t val; val.b_bool = true;
398 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
399 "prev-chapter":"prev-title", val );
403 void InputManager::sectionNext()
407 int i_type = var_Type( p_input, "next-chapter" );
408 vlc_value_t val; val.b_bool = true;
409 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
410 "next-chapter":"next-title", val );
414 void InputManager::sectionMenu()
418 vlc_value_t val; val.i_int = 2;
419 var_Set( p_input, "title 0", val );
423 void InputManager::telexGotoPage( int page )
427 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
428 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
430 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
433 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
434 "zvbi", FIND_ANYWHERE );
437 var_SetInteger( p_vbi, "vbi-page", page );
438 vlc_object_release( p_vbi );
442 emit setNewTelexPage( page );
445 void InputManager::telexToggle( bool b_enabled )
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 b_enabled = i_teletext_es >= 0;
455 if( b_enabled && i_teletext_es == i_spu_es )
458 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
459 "zvbi", FIND_ANYWHERE );
462 i_page = var_GetInteger( p_vbi, "vbi-page" );
463 vlc_object_release( p_vbi );
464 i_page = b_enabled ? i_page : 0;
465 telexGotoPage( i_page );
469 emit teletextEnabled( b_enabled );
472 void InputManager::telexToggleButtons()
476 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
477 if( i_teletext_es >= 0 )
479 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
481 if( i_teletext_es == i_spu_es )
482 var_SetInteger( p_input, "spu-es", -1 );
484 var_SetInteger( p_input, "spu-es", i_teletext_es );
486 emit toggleTelexButtons();
491 void InputManager::telexSetTransparency()
496 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
497 "zvbi", FIND_ANYWHERE );
500 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
501 b_transparentTelextext = !b_transparentTelextext;
502 vlc_object_release( p_vbi );
505 emit toggleTelexTransparency();
508 void InputManager::slower()
511 var_SetVoid( p_input, "rate-slower" );
514 void InputManager::faster()
517 var_SetVoid( p_input, "rate-faster" );
520 void InputManager::normalRate()
523 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
526 void InputManager::setRate( int new_rate )
529 var_SetInteger( p_input, "rate", new_rate );
532 /**********************************************************************
533 * MainInputManager implementation. Wrap an input manager and
534 * take care of updating the main playlist input.
535 * Used in the main playlist Dialog
536 **********************************************************************/
537 MainInputManager * MainInputManager::instance = NULL;
539 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
540 : QObject(NULL), p_intf( _p_intf )
543 im = new InputManager( this, p_intf );
545 // var_AddCallback( THEPL, "item-change", PLItemChanged, this );
546 var_AddCallback( THEPL, "item-change", ItemChanged, im );
547 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
548 var_AddCallback( THEPL, "activity", PLItemChanged, this );
550 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
552 // No necessary, I think TODO REMOVE ME at the end
553 //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
555 /* Warn our embedded IM about input changes */
556 CONNECT( this, inputChanged( input_thread_t * ),
557 im, setInput( input_thread_t * ) );
559 /* emit check if playlist has allready started playing */
561 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
562 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
563 QApplication::postEvent( this, static_cast<QEvent*>(event) );
567 MainInputManager::~MainInputManager()
571 var_DelCallback( p_input, "state", PLItemChanged, this );
572 vlc_object_release( p_input );
573 emit inputChanged( NULL );
576 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
578 var_DelCallback( THEPL, "activity", PLItemChanged, this );
579 var_DelCallback( THEPL, "item-change", ItemChanged, im );
580 // var_DelCallback( THEPL, "item-change", PLItemChanged, this );
582 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
585 void MainInputManager::customEvent( QEvent *event )
587 int type = event->type();
588 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
591 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
592 if( type == VolumeChanged_Type )
594 emit volumeChanged();
598 /* Should be PLItemChanged Event */
599 if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
601 vlc_mutex_lock( &p_intf->change_lock );
602 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
604 var_DelCallback( p_input, "state", PLItemChanged, this );
605 vlc_object_release( p_input );
606 emit inputChanged( NULL );
608 vlc_mutex_unlock( &p_intf->change_lock );
615 p_input = THEPL->p_input;
616 if( p_input && !( !vlc_object_alive (p_input) || p_input->b_dead) )
618 vlc_object_yield( p_input );
619 var_AddCallback( p_input, "state", PLItemChanged, this );
620 emit inputChanged( p_input );
626 vlc_mutex_unlock( &p_intf->change_lock );
630 /* we are working as a dialogs provider */
631 playlist_t *p_playlist = pl_Yield( p_intf );
632 p_input = playlist_CurrentInput( p_playlist );
633 emit inputChanged( p_input );
634 vlc_object_release( p_input );
635 pl_Release( p_intf );
639 /* Playlist Control functions */
640 void MainInputManager::stop()
642 playlist_Stop( THEPL );
645 void MainInputManager::next()
647 playlist_Next( THEPL );
650 void MainInputManager::prev()
652 playlist_Prev( THEPL );
655 void MainInputManager::togglePlayPause()
657 if( p_input == NULL )
659 playlist_Play( THEPL );
662 getIM()->togglePlayPause();
665 bool MainInputManager::teletextState()
670 const int i_teletext_es = var_GetInteger( getInput(), "teletext-es" );
671 const int i_spu_es = var_GetInteger( getInput(), "spu-es" );
673 return i_teletext_es >= 0 && i_teletext_es == i_spu_es;
678 /* Static callbacks */
681 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
682 vlc_value_t oldval, vlc_value_t newval, void *param )
684 static int counter = 0;
685 InputManager *im = (InputManager*)param;
687 counter = ++counter % 4;
690 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
691 QApplication::postEvent( im, static_cast<QEvent*>(event) );
695 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
696 vlc_value_t oldval, vlc_value_t newval, void *param )
698 InputManager *im = (InputManager*)param;
700 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
701 QApplication::postEvent( im, static_cast<QEvent*>(event) );
705 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
706 vlc_value_t oldval, vlc_value_t newval, void *param )
708 InputManager *im = (InputManager*)param;
710 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
711 QApplication::postEvent( im, static_cast<QEvent*>(event) );
715 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
716 vlc_value_t oldval, vlc_value_t newval, void *param )
718 InputManager *im = (InputManager*)param;
720 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
721 QApplication::postEvent( im, static_cast<QEvent*>(event) );
725 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
726 vlc_value_t oldval, vlc_value_t newval, void *param )
728 InputManager *im = (InputManager*)param;
730 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
731 QApplication::postEvent( im, static_cast<QEvent*>(event) );
735 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
736 vlc_value_t n, void *param )
738 InputManager *im = (InputManager*)param;
739 IMEvent *event = new IMEvent( ItemSpuChanged_Type, 0 );
740 QApplication::postEvent( im, static_cast<QEvent*>(event) );
744 static int ChangeTeletext( vlc_object_t *p_this, const char *var, vlc_value_t o,
745 vlc_value_t n, void *param )
748 InputManager *im = (InputManager*)param;
749 IMEvent *event = new IMEvent( ItemTeletextChanged_Type, 0 );
750 QApplication::postEvent( im, static_cast<QEvent*>(event) );
755 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
756 vlc_value_t oldval, vlc_value_t newval, void *param )
758 MainInputManager *mim = (MainInputManager*)param;
760 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
761 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
765 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
766 vlc_value_t oldval, vlc_value_t newval, void *param )
768 MainInputManager *mim = (MainInputManager*)param;
770 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
771 QApplication::postEvent( mim, static_cast<QEvent*>(event) );