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 ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
34 vlc_value_t n, void *param );
35 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
36 vlc_value_t n, void *param );
37 static int ItemChanged( vlc_object_t *, const char *,
38 vlc_value_t, vlc_value_t, void * );
39 static int PLItemChanged( vlc_object_t *, const char *,
40 vlc_value_t, vlc_value_t, void * );
41 static int InterfaceChanged( vlc_object_t *, const char *,
42 vlc_value_t, vlc_value_t, void * );
43 static int ItemStateChanged( vlc_object_t *, const char *,
44 vlc_value_t, vlc_value_t, void * );
45 static int ItemRateChanged( vlc_object_t *, const char *,
46 vlc_value_t, vlc_value_t, void * );
47 static int ItemTitleChanged( vlc_object_t *, const char *,
48 vlc_value_t, vlc_value_t, void * );
49 static int VolumeChanged( vlc_object_t *, const char *,
50 vlc_value_t, vlc_value_t, void * );
52 /**********************************************************************
53 * InputManager implementation
54 **********************************************************************
55 * The Input Manager can be the main one around the playlist
56 * But can also be used for VLM dialog or similar
57 **********************************************************************/
59 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
60 QObject( parent ), p_intf( _p_intf )
62 i_old_playing_status = END_S;
63 b_had_audio = b_had_video = b_has_audio = b_has_video = false;
72 InputManager::~InputManager()
77 /* Define the Input used.
78 Add the callbacks on input
79 p_input is yield once here */
80 void InputManager::setInput( input_thread_t *_p_input )
84 b_had_audio = b_had_video = b_has_audio = b_has_video = false;
85 if( p_input && !( p_input->b_dead || p_input->b_die ) )
87 vlc_object_yield( p_input );
88 emit statusChanged( PLAYING_S );
94 i_input_id = input_GetItem( p_input )->i_id;
100 emit rateChanged( INPUT_RATE_DEFAULT );
104 /* delete Input if it ever existed.
105 Delete the callbacls on input
106 p_input is released once here */
107 void InputManager::delInput()
112 i_old_playing_status = END_S;
116 emit positionUpdated( 0.0, 0 ,0 );
117 emit statusChanged( END_S );
118 emit nameChanged( "" );
119 emit artChanged( "" );
120 emit rateChanged( INPUT_RATE_DEFAULT );
121 vlc_object_release( p_input );
126 /* Add the callbacks on Input. Self explanatory */
127 void InputManager::addCallbacks()
129 /* We don't care about:
136 - position, time, length, because they are included in intf-change
138 /* src/input/input.c:1629 */
139 var_AddCallback( p_input, "state", ItemStateChanged, this );
140 /* src/input/es-out.c:550 */
141 var_AddCallback( p_input, "audio-es", ChangeAudio, this );
142 /* src/input/es-out.c:551 */
143 var_AddCallback( p_input, "video-es", ChangeVideo, this );
144 /* src/input/input.c:1765 */
145 var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
146 /* src/input/input.c:2003 */
147 var_AddCallback( p_input, "title", ItemTitleChanged, this );
148 /* src/input/input.c:734 for timers update*/
149 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
152 /* Delete the callbacks on Input. Self explanatory */
153 void InputManager::delCallbacks()
155 var_DelCallback( p_input, "audio-es", ChangeAudio, this );
156 var_DelCallback( p_input, "video-es", ChangeVideo, this );
157 var_DelCallback( p_input, "state", ItemStateChanged, this );
158 var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
159 var_DelCallback( p_input, "title", ItemTitleChanged, this );
160 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
163 /* Convert the event from the callbacks in actions */
164 void InputManager::customEvent( QEvent *event )
166 int type = event->type();
167 IMEvent *ple = static_cast<IMEvent *>(event);
169 if ( type != PositionUpdate_Type &&
170 type != ItemChanged_Type &&
171 type != ItemRateChanged_Type &&
172 type != ItemTitleChanged_Type &&
173 type != ItemStateChanged_Type )
176 if( !hasInput() ) return;
178 if( ( type != PositionUpdate_Type &&
179 type != ItemRateChanged_Type &&
180 type != ItemStateChanged_Type
182 && ( i_input_id != ple->i_id ) )
185 if( type != PositionUpdate_Type )
186 msg_Dbg( p_intf, "New Event: type %i", type );
191 case PositionUpdate_Type:
194 case ItemChanged_Type:
201 case ItemRateChanged_Type:
204 case ItemTitleChanged_Type:
208 case ItemStateChanged_Type:
215 void InputManager::UpdatePosition()
217 /* Update position */
218 int i_length, i_time; /* Int is enough, since we store seconds */
220 i_length = var_GetTime( p_input , "length" ) / 1000000;
221 i_time = var_GetTime( p_input , "time") / 1000000;
222 f_pos = var_GetFloat( p_input , "position" );
223 emit positionUpdated( f_pos, i_time, i_length );
226 void InputManager::UpdateNavigation()
228 /* Update navigation status */
229 vlc_value_t val; val.i_int = 0;
230 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
234 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
235 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
239 emit navigationChanged( 0 );
243 void InputManager::UpdateStatus()
245 /* Update playing status */
246 vlc_value_t val; val.i_int = 0;
247 var_Get( p_input, "state", &val );
248 if( i_old_playing_status != val.i_int )
250 i_old_playing_status = val.i_int;
251 emit statusChanged( val.i_int );
255 void InputManager::UpdateRate()
258 int i_new_rate = var_GetInteger( p_input, "rate");
259 if( i_new_rate != i_rate )
263 emit rateChanged( i_rate );
267 void InputManager::UpdateMeta()
269 /* Update text, name and nowplaying */
272 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
273 if( EMPTY_STR( psz_name ) )
276 psz_name = input_item_GetName( input_GetItem( p_input ) );
279 char *psz_nowplaying =
280 input_item_GetNowPlaying( input_GetItem( p_input ) );
281 if( !EMPTY_STR( psz_nowplaying ) )
283 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
287 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
288 if( !EMPTY_STR( psz_artist ) )
290 text.sprintf( "%s - %s", psz_artist, psz_name );
294 text.sprintf( "%s", psz_name );
299 free( psz_nowplaying );
301 if( old_name != text )
303 emit nameChanged( text );
308 void InputManager::UpdateTracks()
310 /* Has Audio, has Video Tracks ? */
312 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
313 b_has_audio = val.i_int > 0;
314 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
315 b_has_video = val.i_int > 0;
316 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
317 b_has_subs = val.i_int > 0;
319 /* Update ZVBI status */
321 /* Update teletext status*/
323 emit teletextEnabled( false );/* FIXME */
327 void InputManager::UpdateArt()
329 /* Update Art meta */
331 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
332 url.sprintf("%s", psz_art );
336 artUrl = url.replace( "file://",QString("" ) );
337 /* Taglib seems to define a attachment://, It won't work yet */
338 artUrl = url.replace( "attachment://",QString("" ) );
339 emit artChanged( artUrl );
340 msg_Dbg( p_intf, "Art: %s", qtu( artUrl ) );
344 /* User update of the slider */
345 void InputManager::sliderUpdate( float new_pos )
348 var_SetFloat( p_input, "position", new_pos );
351 /* User togglePlayPause */
352 void InputManager::togglePlayPause()
355 var_Get( p_input, "state", &state );
356 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
357 var_Set( p_input, "state", state );
358 emit statusChanged( state.i_int );
361 void InputManager::sectionPrev()
365 int i_type = var_Type( p_input, "next-chapter" );
366 vlc_value_t val; val.b_bool = true;
367 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
368 "prev-chapter":"prev-title", val );
372 void InputManager::sectionNext()
376 int i_type = var_Type( p_input, "next-chapter" );
377 vlc_value_t val; val.b_bool = true;
378 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
379 "next-chapter":"next-title", val );
383 void InputManager::sectionMenu()
387 vlc_value_t val; val.i_int = 2;
388 var_Set( p_input, "title 0", val );
393 void InputManager::telexGotoPage( int page )
398 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
399 "zvbi", FIND_ANYWHERE );
402 var_SetInteger( p_vbi, "vbi-page", page );
403 vlc_object_release( p_vbi );
408 void InputManager::telexToggle( bool b_enabled )
410 int i_page = b_enabled ? 100 : 0 ;
412 telexGotoPage( i_page );
415 void InputManager::telexSetTransparency( bool b_transp )
420 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
421 "zvbi", FIND_ANYWHERE );
424 var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
425 vlc_object_release( p_vbi );
431 void InputManager::slower()
434 var_SetVoid( p_input, "rate-slower" );
437 void InputManager::faster()
440 var_SetVoid( p_input, "rate-faster" );
443 void InputManager::normalRate()
446 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
449 void InputManager::setRate( int new_rate )
452 var_SetInteger( p_input, "rate", new_rate );
455 /**********************************************************************
456 * MainInputManager implementation. Wrap an input manager and
457 * take care of updating the main playlist input.
458 * Used in the main playlist Dialog
459 **********************************************************************/
460 MainInputManager * MainInputManager::instance = NULL;
462 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
463 : QObject(NULL), p_intf( _p_intf )
466 im = new InputManager( this, p_intf );
468 // var_AddCallback( THEPL, "item-change", PLItemChanged, this );
469 var_AddCallback( THEPL, "item-change", ItemChanged, im );
470 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
471 var_AddCallback( THEPL, "activity", PLItemChanged, this );
473 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
475 // No necessary, I think TODO REMOVE ME at the end
476 //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
478 /* Warn our embedded IM about input changes */
479 CONNECT( this, inputChanged( input_thread_t * ),
480 im, setInput( input_thread_t * ) );
483 MainInputManager::~MainInputManager()
487 var_DelCallback( p_input, "state", PLItemChanged, this );
488 vlc_object_release( p_input );
489 emit inputChanged( NULL );
492 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
494 var_DelCallback( THEPL, "activity", PLItemChanged, this );
495 var_DelCallback( THEPL, "item-change", ItemChanged, im );
496 // var_DelCallback( THEPL, "item-change", PLItemChanged, this );
498 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
501 void MainInputManager::customEvent( QEvent *event )
503 int type = event->type();
504 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
507 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
508 if( type == VolumeChanged_Type )
510 emit volumeChanged();
514 /* Should be PLItemChanged Event */
515 if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
517 vlc_mutex_lock( &p_intf->change_lock );
518 if( p_input && ( p_input->b_dead || p_input->b_die ) )
520 var_DelCallback( p_input, "state", PLItemChanged, this );
521 vlc_object_release( p_input );
522 emit inputChanged( NULL );
524 vlc_mutex_unlock( &p_intf->change_lock );
531 p_input = THEPL->p_input;
532 if( p_input && !( p_input->b_die || p_input->b_dead) )
534 vlc_object_yield( p_input );
535 var_AddCallback( p_input, "state", PLItemChanged, this );
536 emit inputChanged( p_input );
542 vlc_mutex_unlock( &p_intf->change_lock );
546 /* we are working as a dialogs provider */
547 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
548 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
551 p_input = p_playlist->p_input;
552 emit inputChanged( p_input );
557 /* Playlist Control functions */
558 void MainInputManager::stop()
560 playlist_Stop( THEPL );
563 void MainInputManager::next()
565 playlist_Next( THEPL );
568 void MainInputManager::prev()
570 playlist_Prev( THEPL );
573 void MainInputManager::togglePlayPause()
575 if( p_input == NULL )
577 playlist_Play( THEPL );
580 getIM()->togglePlayPause();
583 /* Static callbacks */
586 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
587 vlc_value_t oldval, vlc_value_t newval, void *param )
589 static int counter = 0;
590 InputManager *im = (InputManager*)param;
592 counter = ++counter % 4;
595 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
596 QApplication::postEvent( im, static_cast<QEvent*>(event) );
600 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
601 vlc_value_t oldval, vlc_value_t newval, void *param )
603 InputManager *im = (InputManager*)param;
605 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
606 QApplication::postEvent( im, static_cast<QEvent*>(event) );
610 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
611 vlc_value_t oldval, vlc_value_t newval, void *param )
613 InputManager *im = (InputManager*)param;
615 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
616 QApplication::postEvent( im, static_cast<QEvent*>(event) );
620 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
621 vlc_value_t oldval, vlc_value_t newval, void *param )
623 InputManager *im = (InputManager*)param;
625 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
626 QApplication::postEvent( im, static_cast<QEvent*>(event) );
630 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
631 vlc_value_t oldval, vlc_value_t newval, void *param )
633 InputManager *im = (InputManager*)param;
635 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
636 QApplication::postEvent( im, static_cast<QEvent*>(event) );
641 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
642 vlc_value_t n, void *param )
644 InputManager *im = (InputManager*)param;
645 im->b_has_audio = true;
649 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
650 vlc_value_t n, void *param )
652 InputManager *im = (InputManager*)param;
653 im->b_has_video = true;
658 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
659 vlc_value_t oldval, vlc_value_t newval, void *param )
661 MainInputManager *mim = (MainInputManager*)param;
663 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
664 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
668 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
669 vlc_value_t oldval, vlc_value_t newval, void *param )
671 MainInputManager *mim = (MainInputManager*)param;
673 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
674 QApplication::postEvent( mim, static_cast<QEvent*>(event) );