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 = VLC_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 = VLC_TRUE;
378 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
379 "next-chapter":"next-title", val );
383 void InputManager::sectionMenu()
386 var_SetInteger( p_input, "title 0", 2 );
390 void InputManager::telexGotoPage( int page )
395 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
396 "zvbi", FIND_ANYWHERE );
399 var_SetInteger( p_vbi, "vbi-page", page );
400 vlc_object_release( p_vbi );
405 void InputManager::telexToggle( bool b_enabled )
407 int i_page = b_enabled ? 100 : 0 ;
409 telexGotoPage( i_page );
412 void InputManager::telexSetTransparency( bool b_transp )
417 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
418 "zvbi", FIND_ANYWHERE );
421 var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
422 vlc_object_release( p_vbi );
428 void InputManager::slower()
431 var_SetVoid( p_input, "rate-slower" );
434 void InputManager::faster()
437 var_SetVoid( p_input, "rate-faster" );
440 void InputManager::normalRate()
443 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
446 void InputManager::setRate( int new_rate )
449 var_SetInteger( p_input, "rate", new_rate );
452 /**********************************************************************
453 * MainInputManager implementation. Wrap an input manager and
454 * take care of updating the main playlist input.
455 * Used in the main playlist Dialog
456 **********************************************************************/
457 MainInputManager * MainInputManager::instance = NULL;
459 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
460 : QObject(NULL), p_intf( _p_intf )
463 im = new InputManager( this, p_intf );
465 // var_AddCallback( THEPL, "item-change", PLItemChanged, this );
466 var_AddCallback( THEPL, "item-change", ItemChanged, im );
467 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
468 var_AddCallback( THEPL, "activity", PLItemChanged, this );
470 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
472 // No necessary, I think TODO REMOVE ME at the end
473 //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
475 /* Warn our embedded IM about input changes */
476 CONNECT( this, inputChanged( input_thread_t * ),
477 im, setInput( input_thread_t * ) );
480 MainInputManager::~MainInputManager()
484 var_DelCallback( p_input, "state", PLItemChanged, this );
485 vlc_object_release( p_input );
486 emit inputChanged( NULL );
489 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
491 var_DelCallback( THEPL, "activity", PLItemChanged, this );
492 var_DelCallback( THEPL, "item-change", ItemChanged, im );
493 // var_DelCallback( THEPL, "item-change", PLItemChanged, this );
495 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
498 void MainInputManager::customEvent( QEvent *event )
500 int type = event->type();
501 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
504 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
505 if( type == VolumeChanged_Type )
507 emit volumeChanged();
511 /* Should be PLItemChanged Event */
512 if( VLC_OBJECT_INTF == p_intf->i_object_type )
514 vlc_mutex_lock( &p_intf->change_lock );
515 if( p_input && ( p_input->b_dead || p_input->b_die ) )
517 var_DelCallback( p_input, "state", PLItemChanged, this );
518 vlc_object_release( p_input );
519 emit inputChanged( NULL );
521 vlc_mutex_unlock( &p_intf->change_lock );
528 p_input = THEPL->p_input;
529 if( p_input && !( p_input->b_die || p_input->b_dead) )
531 vlc_object_yield( p_input );
532 var_AddCallback( p_input, "state", PLItemChanged, this );
533 emit inputChanged( p_input );
539 vlc_mutex_unlock( &p_intf->change_lock );
543 /* we are working as a dialogs provider */
544 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
545 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
548 p_input = p_playlist->p_input;
549 emit inputChanged( p_input );
554 /* Playlist Control functions */
555 void MainInputManager::stop()
557 playlist_Stop( THEPL );
560 void MainInputManager::next()
562 playlist_Next( THEPL );
565 void MainInputManager::prev()
567 playlist_Prev( THEPL );
570 void MainInputManager::togglePlayPause()
572 if( p_input == NULL )
574 playlist_Play( THEPL );
577 getIM()->togglePlayPause();
580 /* Static callbacks */
583 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
584 vlc_value_t oldval, vlc_value_t newval, void *param )
586 static int counter = 0;
587 InputManager *im = (InputManager*)param;
589 counter = ++counter % 4;
592 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
593 QApplication::postEvent( im, static_cast<QEvent*>(event) );
597 static int ItemStateChanged( vlc_object_t *p_this, const char *psz_var,
598 vlc_value_t oldval, vlc_value_t newval, void *param )
600 InputManager *im = (InputManager*)param;
602 IMEvent *event = new IMEvent( ItemStateChanged_Type, 0 );
603 QApplication::postEvent( im, static_cast<QEvent*>(event) );
607 static int ItemRateChanged( vlc_object_t *p_this, const char *psz_var,
608 vlc_value_t oldval, vlc_value_t newval, void *param )
610 InputManager *im = (InputManager*)param;
612 IMEvent *event = new IMEvent( ItemRateChanged_Type, 0 );
613 QApplication::postEvent( im, static_cast<QEvent*>(event) );
617 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
618 vlc_value_t oldval, vlc_value_t newval, void *param )
620 InputManager *im = (InputManager*)param;
622 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
623 QApplication::postEvent( im, static_cast<QEvent*>(event) );
627 static int ItemChanged( vlc_object_t *p_this, const char *psz_var,
628 vlc_value_t oldval, vlc_value_t newval, void *param )
630 InputManager *im = (InputManager*)param;
632 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
633 QApplication::postEvent( im, static_cast<QEvent*>(event) );
638 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
639 vlc_value_t n, void *param )
641 InputManager *im = (InputManager*)param;
642 im->b_has_audio = true;
646 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
647 vlc_value_t n, void *param )
649 InputManager *im = (InputManager*)param;
650 im->b_has_video = true;
655 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
656 vlc_value_t oldval, vlc_value_t newval, void *param )
658 MainInputManager *mim = (MainInputManager*)param;
660 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
661 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
665 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
666 vlc_value_t oldval, vlc_value_t newval, void *param )
668 MainInputManager *mim = (MainInputManager*)param;
670 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
671 QApplication::postEvent( mim, static_cast<QEvent*>(event) );