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 );
105 /* delete Input if it ever existed.
106 Delete the callbacls on input
107 p_input is released once here */
108 void InputManager::delInput()
113 i_old_playing_status = END_S;
117 emit positionUpdated( 0.0, 0 ,0 );
118 emit statusChanged( END_S );
119 emit nameChanged( "" );
120 emit artChanged( "" );
121 emit rateChanged( INPUT_RATE_DEFAULT );
122 vlc_object_release( p_input );
129 /* Add the callbacks on Input. Self explanatory */
130 void InputManager::addCallbacks()
132 /* We don't care about:
138 - position, time, length, because they are included in intf-change
140 /* src/input/input.c:1629 */
141 var_AddCallback( p_input, "state", ItemStateChanged, this );
142 /* src/input/es-out.c:552 */
143 var_AddCallback( p_input, "spu-es", ChangeSPU, this );
144 /* emit UpdateStatus so that main_interface updates controls
145 * if there is new videotracks (mpeg-ts)*/
146 var_AddCallback( p_input, "video-es", ItemStateChanged, this );
147 /* src/input/es-out.c: */
148 var_AddCallback( p_input, "teletext-es", ChangeTeletext, this );
149 /* src/input/input.c:1765 */
150 var_AddCallback( p_input, "rate-change", ItemRateChanged, this );
151 /* src/input/input.c:2003 */
152 var_AddCallback( p_input, "title", ItemTitleChanged, this );
153 /* src/input/input.c:734 for timers update*/
154 var_AddCallback( p_input, "intf-change", InterfaceChanged, this );
157 /* Delete the callbacks on Input. Self explanatory */
158 void InputManager::delCallbacks()
160 var_DelCallback( p_input, "spu-es", ChangeSPU, this );
161 var_DelCallback( p_input, "video-es", ItemStateChanged, this );
162 var_DelCallback( p_input, "teletext-es", ChangeTeletext, this );
163 var_DelCallback( p_input, "state", ItemStateChanged, this );
164 var_DelCallback( p_input, "rate-change", ItemRateChanged, this );
165 var_DelCallback( p_input, "title", ItemTitleChanged, this );
166 var_DelCallback( p_input, "intf-change", InterfaceChanged, this );
169 /* Convert the event from the callbacks in actions */
170 void InputManager::customEvent( QEvent *event )
172 int type = event->type();
173 IMEvent *ple = static_cast<IMEvent *>(event);
175 if ( type != PositionUpdate_Type &&
176 type != ItemChanged_Type &&
177 type != ItemRateChanged_Type &&
178 type != ItemTitleChanged_Type &&
179 type != ItemSpuChanged_Type &&
180 type != ItemTeletextChanged_Type &&
181 type != ItemStateChanged_Type )
184 if( !hasInput() ) return;
186 if( ( type != PositionUpdate_Type &&
187 type != ItemRateChanged_Type &&
188 type != ItemSpuChanged_Type &&
189 type != ItemTeletextChanged_Type &&
190 type != ItemStateChanged_Type
192 && ( i_input_id != ple->i_id ) )
195 if( type != PositionUpdate_Type )
196 msg_Dbg( p_intf, "New Event: type %i", type );
201 case PositionUpdate_Type:
204 case ItemChanged_Type:
209 case ItemStateChanged_Type:
214 case ItemTitleChanged_Type:
218 case ItemRateChanged_Type:
221 case ItemSpuChanged_Type:
224 case ItemTeletextChanged_Type:
230 void InputManager::UpdatePosition()
232 /* Update position */
233 int i_length, i_time; /* Int is enough, since we store seconds */
235 i_length = var_GetTime( p_input , "length" ) / 1000000;
236 i_time = var_GetTime( p_input , "time") / 1000000;
237 f_pos = var_GetFloat( p_input , "position" );
238 emit positionUpdated( f_pos, i_time, i_length );
241 void InputManager::UpdateNavigation()
243 /* Update navigation status */
244 vlc_value_t val; val.i_int = 0;
245 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
249 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
250 emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
254 emit navigationChanged( 0 );
258 void InputManager::UpdateStatus()
260 /* Update playing status */
261 vlc_value_t val; val.i_int = 0;
262 var_Get( p_input, "state", &val );
263 if( i_old_playing_status != val.i_int )
265 i_old_playing_status = val.i_int;
266 emit statusChanged( val.i_int );
270 void InputManager::UpdateRate()
273 int i_new_rate = var_GetInteger( p_input, "rate");
274 if( i_new_rate != i_rate )
278 emit rateChanged( i_rate );
282 void InputManager::UpdateMeta()
284 /* Update text, name and nowplaying */
287 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
288 if( EMPTY_STR( psz_name ) )
291 psz_name = input_item_GetName( input_GetItem( p_input ) );
294 char *psz_nowplaying =
295 input_item_GetNowPlaying( input_GetItem( p_input ) );
296 if( !EMPTY_STR( psz_nowplaying ) )
298 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
302 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
303 if( !EMPTY_STR( psz_artist ) )
305 text.sprintf( "%s - %s", psz_artist, psz_name );
309 text.sprintf( "%s", psz_name );
314 free( psz_nowplaying );
316 if( old_name != text )
318 emit nameChanged( text );
323 bool InputManager::hasAudio()
328 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
329 return val.i_int > 0;
334 bool InputManager::hasVideo()
339 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
340 return val.i_int > 0;
346 void InputManager::UpdateSPU()
351 void InputManager::UpdateTeletext()
354 telexToggle( var_GetInteger( p_input, "teletext-es" ) >= 0 );
356 telexToggle( false );
361 void InputManager::UpdateArt()
363 /* Update Art meta */
365 char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
366 url.sprintf("%s", psz_art );
370 artUrl = url.replace( "file://",QString("" ) );
371 /* Taglib seems to define a attachment://, It won't work yet */
372 artUrl = url.replace( "attachment://",QString("" ) );
373 emit artChanged( artUrl );
374 msg_Dbg( p_intf, "Art: %s", qtu( artUrl ) );
378 /* User update of the slider */
379 void InputManager::sliderUpdate( float new_pos )
382 var_SetFloat( p_input, "position", new_pos );
385 /* User togglePlayPause */
386 void InputManager::togglePlayPause()
389 var_Get( p_input, "state", &state );
390 state.i_int = ( state.i_int != PLAYING_S ) ? PLAYING_S : PAUSE_S;
391 var_Set( p_input, "state", state );
392 emit statusChanged( state.i_int );
395 void InputManager::sectionPrev()
399 int i_type = var_Type( p_input, "next-chapter" );
400 vlc_value_t val; val.b_bool = true;
401 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
402 "prev-chapter":"prev-title", val );
406 void InputManager::sectionNext()
410 int i_type = var_Type( p_input, "next-chapter" );
411 vlc_value_t val; val.b_bool = true;
412 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
413 "next-chapter":"next-title", val );
417 void InputManager::sectionMenu()
421 vlc_value_t val, text;
424 if( var_Change( p_input, "title 0", VLC_VAR_GETLIST, &val, &text ) < 0 )
427 /* XXX is it "Root" or "Title" we want here ?" (set 0 by default) */
429 for( int i = 0; i < val.p_list->i_count; i++ )
431 if( !strcmp( text.p_list->p_values[i].psz_string, "Title" ) )
434 var_Change( p_input, "title 0", VLC_VAR_FREELIST, &val, &text );
436 var_Set( p_input, "title 0", root );
440 void InputManager::telexGotoPage( int page )
444 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
445 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
447 if( i_teletext_es >= 0 && i_teletext_es == i_spu_es )
450 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
451 "zvbi", FIND_ANYWHERE );
454 var_SetInteger( p_vbi, "vbi-page", page );
455 vlc_object_release( p_vbi );
459 emit setNewTelexPage( page );
462 void InputManager::telexToggle( bool b_enabled )
466 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
467 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
469 b_enabled = (i_teletext_es >= 0);
470 emit teletextEnabled( b_enabled );
471 if( b_enabled && (i_teletext_es == i_spu_es) )
475 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
476 "zvbi", FIND_ANYWHERE );
479 i_page = var_GetInteger( p_vbi, "vbi-page" );
480 vlc_object_release( p_vbi );
481 i_page = b_enabled ? i_page : 0;
482 telexGotoPage( i_page );
486 else emit teletextEnabled( b_enabled );
489 void InputManager::telexToggleButtons()
493 const int i_teletext_es = var_GetInteger( p_input, "teletext-es" );
494 if( i_teletext_es >= 0 )
496 const int i_spu_es = var_GetInteger( p_input, "spu-es" );
498 if( i_teletext_es == i_spu_es )
499 var_SetInteger( p_input, "spu-es", -1 );
501 var_SetInteger( p_input, "spu-es", i_teletext_es );
503 emit toggleTelexButtons();
508 void InputManager::telexSetTransparency()
513 p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
514 "zvbi", FIND_ANYWHERE );
517 var_SetBool( p_vbi, "vbi-opaque", b_transparentTelextext );
518 b_transparentTelextext = !b_transparentTelextext;
519 vlc_object_release( p_vbi );
522 emit toggleTelexTransparency();
525 void InputManager::slower()
528 var_SetVoid( p_input, "rate-slower" );
531 void InputManager::faster()
534 var_SetVoid( p_input, "rate-faster" );
537 void InputManager::normalRate()
540 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
543 void InputManager::setRate( int new_rate )
546 var_SetInteger( p_input, "rate", new_rate );
549 /**********************************************************************
550 * MainInputManager implementation. Wrap an input manager and
551 * take care of updating the main playlist input.
552 * Used in the main playlist Dialog
553 **********************************************************************/
554 MainInputManager * MainInputManager::instance = NULL;
556 MainInputManager::MainInputManager( intf_thread_t *_p_intf )
557 : QObject(NULL), p_intf( _p_intf )
560 im = new InputManager( this, p_intf );
562 // var_AddCallback( THEPL, "item-change", PLItemChanged, this );
563 var_AddCallback( THEPL, "item-change", ItemChanged, im );
564 var_AddCallback( THEPL, "playlist-current", PLItemChanged, this );
565 var_AddCallback( THEPL, "activity", PLItemChanged, this );
567 var_AddCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
569 // No necessary, I think TODO REMOVE ME at the end
570 //var_AddCallback( THEPL, "intf-change", ItemChanged, im );
572 /* Warn our embedded IM about input changes */
573 CONNECT( this, inputChanged( input_thread_t * ),
574 im, setInput( input_thread_t * ) );
576 /* emit check if playlist has allready started playing */
578 var_Change( THEPL, "playlist-current", VLC_VAR_CHOICESCOUNT, &val, NULL );
579 IMEvent *event = new IMEvent( ItemChanged_Type, val.i_int);
580 QApplication::postEvent( this, static_cast<QEvent*>(event) );
584 MainInputManager::~MainInputManager()
588 var_DelCallback( p_input, "state", PLItemChanged, this );
589 vlc_object_release( p_input );
590 emit inputChanged( NULL );
593 var_DelCallback( p_intf->p_libvlc, "volume-change", VolumeChanged, this );
595 var_DelCallback( THEPL, "activity", PLItemChanged, this );
596 var_DelCallback( THEPL, "item-change", ItemChanged, im );
597 // var_DelCallback( THEPL, "item-change", PLItemChanged, this );
599 var_DelCallback( THEPL, "playlist-current", PLItemChanged, this );
602 void MainInputManager::customEvent( QEvent *event )
604 int type = event->type();
605 if ( type != ItemChanged_Type && type != VolumeChanged_Type )
608 // msg_Dbg( p_intf, "New MainIM Event of type: %i", type );
609 if( type == VolumeChanged_Type )
611 emit volumeChanged();
615 /* Should be PLItemChanged Event */
616 if( VLC_OBJECT_INTF == p_intf->i_object_type ) /* FIXME: don't use object type */
618 vlc_mutex_lock( &p_intf->change_lock );
619 if( p_input && ( p_input->b_dead || !vlc_object_alive (p_input) ) )
621 var_DelCallback( p_input, "state", PLItemChanged, this );
622 vlc_object_release( p_input );
623 emit inputChanged( NULL );
625 vlc_mutex_unlock( &p_intf->change_lock );
632 p_input = THEPL->p_input;
633 if( p_input && !( !vlc_object_alive (p_input) || p_input->b_dead) )
635 vlc_object_yield( p_input );
636 var_AddCallback( p_input, "state", PLItemChanged, this );
637 emit inputChanged( p_input );
643 vlc_mutex_unlock( &p_intf->change_lock );
647 /* we are working as a dialogs provider */
648 playlist_t *p_playlist = pl_Yield( p_intf );
649 p_input = playlist_CurrentInput( p_playlist );
652 emit inputChanged( p_input );
653 vlc_object_release( p_input );
655 pl_Release( p_intf );
659 /* Playlist Control functions */
660 void MainInputManager::stop()
662 playlist_Stop( THEPL );
665 void MainInputManager::next()
667 playlist_Next( THEPL );
670 void MainInputManager::prev()
672 playlist_Prev( THEPL );
675 void MainInputManager::togglePlayPause()
677 if( p_input == NULL )
679 playlist_Play( THEPL );
682 getIM()->togglePlayPause();
685 bool MainInputManager::teletextState()
690 const int i_teletext_es = var_GetInteger( getInput(), "teletext-es" );
691 const int i_spu_es = var_GetInteger( getInput(), "spu-es" );
693 return i_teletext_es >= 0 && i_teletext_es == i_spu_es;
698 /* Static callbacks */
701 static int InterfaceChanged( vlc_object_t *p_this, const char *psz_var,
702 vlc_value_t oldval, vlc_value_t newval, void *param )
704 static int counter = 0;
705 InputManager *im = (InputManager*)param;
707 counter = ++counter % 4;
710 IMEvent *event = new IMEvent( PositionUpdate_Type, 0 );
711 QApplication::postEvent( im, static_cast<QEvent*>(event) );
715 static int ItemStateChanged( 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( ItemStateChanged_Type, 0 );
721 QApplication::postEvent( im, static_cast<QEvent*>(event) );
725 static int ItemRateChanged( 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( ItemRateChanged_Type, 0 );
731 QApplication::postEvent( im, static_cast<QEvent*>(event) );
735 static int ItemTitleChanged( vlc_object_t *p_this, const char *psz_var,
736 vlc_value_t oldval, vlc_value_t newval, void *param )
738 InputManager *im = (InputManager*)param;
740 IMEvent *event = new IMEvent( ItemTitleChanged_Type, 0 );
741 QApplication::postEvent( im, static_cast<QEvent*>(event) );
745 static int ItemChanged( 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( ItemChanged_Type, newval.i_int );
751 QApplication::postEvent( im, static_cast<QEvent*>(event) );
755 static int ChangeSPU( vlc_object_t *p_this, const char *var, vlc_value_t o,
756 vlc_value_t n, void *param )
758 InputManager *im = (InputManager*)param;
759 IMEvent *event = new IMEvent( ItemSpuChanged_Type, 0 );
760 QApplication::postEvent( im, static_cast<QEvent*>(event) );
764 static int ChangeTeletext( vlc_object_t *p_this, const char *var, vlc_value_t o,
765 vlc_value_t n, void *param )
768 InputManager *im = (InputManager*)param;
769 IMEvent *event = new IMEvent( ItemTeletextChanged_Type, 0 );
770 QApplication::postEvent( im, static_cast<QEvent*>(event) );
775 static int PLItemChanged( vlc_object_t *p_this, const char *psz_var,
776 vlc_value_t oldval, vlc_value_t newval, void *param )
778 MainInputManager *mim = (MainInputManager*)param;
780 IMEvent *event = new IMEvent( ItemChanged_Type, newval.i_int );
781 QApplication::postEvent( mim, static_cast<QEvent*>(event) );
785 static int VolumeChanged( vlc_object_t *p_this, const char *psz_var,
786 vlc_value_t oldval, vlc_value_t newval, void *param )
788 MainInputManager *mim = (MainInputManager*)param;
790 IMEvent *event = new IMEvent( VolumeChanged_Type, newval.i_int );
791 QApplication::postEvent( mim, static_cast<QEvent*>(event) );