1 /*****************************************************************************
2 * input_manager.cpp : Manage an input and interact with its GUI elements
3 ****************************************************************************
4 * Copyright (C) 2006-2007 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 *****************************************************************************/
25 #include "input_manager.hpp"
26 #include "dialogs_provider.hpp"
28 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
29 vlc_value_t n, void *param );
30 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
31 vlc_value_t n, void *param );
33 /**********************************************************************
34 * InputManager implementation
35 **********************************************************************/
37 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
38 QObject( parent ), p_intf( _p_intf )
40 i_old_playing_status = END_S;
44 ON_TIMEOUT( update() );
47 InputManager::~InputManager()
52 void InputManager::setInput( input_thread_t *_p_input )
56 emit positionUpdated( 0.0,0,0 );
57 b_had_audio = b_had_video = b_has_audio = b_has_video = false;
60 vlc_object_yield( p_input );
62 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
63 b_has_video = val.i_int > 0;
64 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
65 b_has_audio = val.i_int > 0;
66 var_AddCallback( p_input, "audio-es", ChangeAudio, this );
67 var_AddCallback( p_input, "video-es", ChangeVideo, this );
71 void InputManager::delInput()
75 var_DelCallback( p_input, "audio-es", ChangeAudio, this );
76 var_DelCallback( p_input, "video-es", ChangeVideo, this );
77 vlc_object_release( p_input );
82 void InputManager::update()
84 /// \todo Emit the signals only if it changed
85 if( !p_input ) return;
87 if( p_input->b_dead || p_input->b_die )
89 emit positionUpdated( 0.0, 0, 0 );
90 emit navigationChanged( 0 );
91 i_old_playing_status = 0;
92 emit statusChanged( END_S ); // see vlc_input.h, input_state_e enum
98 mtime_t i_length, i_time;
100 i_length = var_GetTime( p_input, "length" ) / 1000000;
101 i_time = var_GetTime( p_input, "time") / 1000000;
102 f_pos = var_GetFloat( p_input, "position" );
103 emit positionUpdated( f_pos, i_time, i_length );
105 int i_new_rate = var_GetInteger( p_input, "rate");
106 if( i_new_rate != i_rate )
110 emit rateChanged( i_rate );
113 /* Update navigation status */
114 vlc_value_t val; val.i_int = 0;
115 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
119 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
122 emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
126 emit navigationChanged( 2 );
131 emit navigationChanged( 0 );
136 char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
137 char *psz_nowplaying =
138 input_item_GetNowPlaying( input_GetItem( p_input ) );
139 char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
140 if( EMPTY_STR( psz_name ) )
143 psz_name = input_item_GetName( input_GetItem( p_input ) );
145 if( !EMPTY_STR( psz_nowplaying ) )
147 text.sprintf( "%s - %s", psz_nowplaying, psz_name );
149 else if( !EMPTY_STR( psz_artist ) )
151 text.sprintf( "%s - %s", psz_artist, psz_name );
155 text.sprintf( "%s", psz_name );
158 free( psz_nowplaying );
160 if( old_name != text )
162 emit nameChanged( text );
165 /* Update playing status */
166 var_Get( p_input, "state", &val );
167 val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
168 if( i_old_playing_status != val.i_int )
170 i_old_playing_status = val.i_int;
171 emit statusChanged( val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
175 void InputManager::sliderUpdate( float new_pos )
178 var_SetFloat( p_input, "position", new_pos );
181 void InputManager::togglePlayPause()
184 var_Get( p_input, "state", &state );
185 if( state.i_int != PAUSE_S )
187 /* A stream is being played, pause it */
188 state.i_int = PAUSE_S;
192 /* Stream is paused, resume it */
193 state.i_int = PLAYING_S;
195 var_Set( p_input, "state", state );
196 emit statusChanged( state.i_int );
199 void InputManager::sectionPrev()
203 int i_type = var_Type( p_input, "next-chapter" );
204 vlc_value_t val; val.b_bool = VLC_TRUE;
205 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
206 "prev-chapter":"prev-title", val );
210 void InputManager::sectionNext()
214 int i_type = var_Type( p_input, "next-chapter" );
215 vlc_value_t val; val.b_bool = VLC_TRUE;
216 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
217 "next-chapter":"next-title", val );
221 void InputManager::sectionMenu()
224 var_SetInteger( p_input, "title 0", 2 );
227 void InputManager::slower()
230 var_SetVoid( p_input, "rate-slower" );
233 void InputManager::faster()
236 var_SetVoid( p_input, "rate-faster" );
239 void InputManager::normalRate()
242 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
245 void InputManager::setRate( int new_rate )
248 var_SetInteger( p_input, "rate", new_rate );
251 /**********************************************************************
252 * MainInputManager implementation. Wrap an input manager and
253 * take care of updating the main playlist input
254 **********************************************************************/
255 MainInputManager * MainInputManager::instance = NULL;
257 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
261 im = new InputManager( this, p_intf );
262 ON_TIMEOUT( updateInput() );
263 /* Warn our embedded IM about input changes */
264 CONNECT( this, inputChanged( input_thread_t * ),
265 im, setInput( input_thread_t * ) );
268 MainInputManager::~MainInputManager()
270 if( p_input ) vlc_object_release( p_input );
273 void MainInputManager::updateInput()
275 if( VLC_OBJECT_INTF == p_intf->i_object_type )
277 vlc_mutex_lock( &p_intf->change_lock );
278 if( p_input && p_input->b_dead )
280 vlc_object_release( p_input );
283 emit inputChanged( NULL );
289 p_input = THEPL->p_input;
292 vlc_object_yield( p_input );
293 emit inputChanged( p_input );
297 vlc_mutex_unlock( &p_intf->change_lock );
300 /* we are working as a dialogs provider */
301 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
302 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
305 p_input = p_playlist->p_input;
306 emit inputChanged( p_input );
311 void MainInputManager::stop()
313 playlist_Stop( THEPL );
316 void MainInputManager::next()
318 playlist_Next( THEPL );
321 void MainInputManager::prev()
323 playlist_Prev( THEPL );
326 void MainInputManager::togglePlayPause()
328 if( p_input == NULL )
330 playlist_Play( THEPL );
333 getIM()->togglePlayPause();
336 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
337 vlc_value_t n, void *param )
339 InputManager *im = (InputManager*)param;
340 im->b_has_audio = true;
344 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
345 vlc_value_t n, void *param )
347 InputManager *im = (InputManager*)param;
348 im->b_has_video = true;