1 /*****************************************************************************
2 * input_manager.cpp : Manage an input and interact with its GUI elements
3 ****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
27 #include "input_manager.hpp"
28 #include "dialogs_provider.hpp"
30 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
31 vlc_value_t n, void *param );
32 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
33 vlc_value_t n, void *param );
35 /**********************************************************************
36 * InputManager implementation
37 **********************************************************************/
39 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
40 QObject( parent ), p_intf( _p_intf )
42 i_old_playing_status = END_S;
44 ON_TIMEOUT( update() );
47 InputManager::~InputManager()
51 void InputManager::setInput( input_thread_t *_p_input )
54 emit positionUpdated( 0.0,0,0 );
55 b_had_audio = b_had_video = b_has_audio = b_has_video = false;
59 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
60 b_has_video = val.i_int > 0;
61 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
62 b_has_audio = val.i_int > 0;
63 var_AddCallback( p_input, "audio-es", ChangeAudio, this );
64 var_AddCallback( p_input, "video-es", ChangeVideo, this );
68 void InputManager::delInput()
72 var_DelCallback( p_input, "audio-es", ChangeAudio, this );
73 var_DelCallback( p_input, "video-es", ChangeVideo, this );
77 void InputManager::update()
79 /// \todo Emit the signals only if it changed
80 if( !p_input ) return;
82 if( p_input->b_dead || p_input->b_die )
84 emit positionUpdated( 0.0, 0, 0 );
85 emit navigationChanged( 0 );
86 emit statusChanged( 0 ); // 0 = STOPPED, 1 = PLAY, 2 = PAUSE
89 if( !b_had_audio && b_has_audio )
91 if( !b_had_video && b_has_video )
95 mtime_t i_length, i_time;
97 i_length = var_GetTime( p_input, "length" ) / 1000000;
98 i_time = var_GetTime( p_input, "time") / 1000000;
99 f_pos = var_GetFloat( p_input, "position" );
100 emit positionUpdated( f_pos, i_time, i_length );
102 /* Update disc status */
104 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
108 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
110 emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
112 emit navigationChanged( 2 );
116 emit navigationChanged( 0 );
121 if( p_input->input.p_item->p_meta &&
122 p_input->input.p_item->p_meta->psz_nowplaying &&
123 *p_input->input.p_item->p_meta->psz_nowplaying )
125 text.sprintf( "%s - %s",
126 p_input->input.p_item->p_meta->psz_nowplaying,
127 p_input->input.p_item->psz_name );
131 text.sprintf( "%s", p_input->input.p_item->psz_name );
133 emit nameChanged( text );
135 /* Update playing status */
136 var_Get( p_input, "state", &val );
137 val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
138 if( i_old_playing_status != val.i_int )
140 i_old_playing_status = val.i_int;
141 emit statusChanged( val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
145 void InputManager::sliderUpdate( float new_pos )
148 var_SetFloat( p_input, "position", new_pos );
151 void InputManager::togglePlayPause()
154 var_Get( p_input, "state", &state );
155 if( state.i_int != PAUSE_S )
157 /* A stream is being played, pause it */
158 state.i_int = PAUSE_S;
162 /* Stream is paused, resume it */
163 state.i_int = PLAYING_S;
165 var_Set( p_input, "state", state );
166 emit statusChanged( state.i_int );
169 void InputManager::slower()
172 var_SetVoid( p_input, "rate-slower" );
175 void InputManager::faster()
178 var_SetVoid( p_input, "rate-faster" );
181 void InputManager::normalRate()
184 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
187 /**********************************************************************
188 * MainInputManager implementation. Wrap an input manager and
189 * take care of updating the main playlist input
190 **********************************************************************/
191 MainInputManager * MainInputManager::instance = NULL;
193 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
197 im = new InputManager( this, p_intf );
198 ON_TIMEOUT( updateInput() );
199 /* Warn our embedded IM about input changes */
200 CONNECT( this, inputChanged( input_thread_t * ),
201 im, setInput( input_thread_t * ) );
204 MainInputManager::~MainInputManager()
206 if( p_input ) vlc_object_release( p_input );
209 void MainInputManager::updateInput()
211 vlc_mutex_lock( &p_intf->change_lock );
212 if( p_input && p_input->b_dead )
214 vlc_object_release( p_input );
217 emit inputChanged( NULL );
223 p_input = THEPL->p_input;
226 vlc_object_yield( p_input );
227 emit inputChanged( p_input );
231 vlc_mutex_unlock( &p_intf->change_lock );
234 void MainInputManager::togglePlayPause()
236 if( p_input == NULL )
238 playlist_Play( THEPL );
241 getIM()->togglePlayPause();
245 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
246 vlc_value_t n, void *param )
248 InputManager *im = (InputManager*)param;
249 im->b_has_audio = true;
253 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
254 vlc_value_t n, void *param )
256 InputManager *im = (InputManager*)param;
257 im->b_has_video = true;