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;
43 ON_TIMEOUT( update() );
46 InputManager::~InputManager()
51 void InputManager::setInput( input_thread_t *_p_input )
55 emit positionUpdated( 0.0,0,0 );
56 b_had_audio = b_had_video = b_has_audio = b_has_video = false;
59 vlc_object_yield( p_input );
61 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
62 b_has_video = val.i_int > 0;
63 var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
64 b_has_audio = val.i_int > 0;
65 var_AddCallback( p_input, "audio-es", ChangeAudio, this );
66 var_AddCallback( p_input, "video-es", ChangeVideo, this );
70 void InputManager::delInput()
74 var_DelCallback( p_input, "audio-es", ChangeAudio, this );
75 var_DelCallback( p_input, "video-es", ChangeVideo, this );
76 vlc_object_release( p_input );
81 void InputManager::update()
83 /// \todo Emit the signals only if it changed
84 if( !p_input ) return;
86 if( p_input->b_dead || p_input->b_die )
88 emit positionUpdated( 0.0, 0, 0 );
89 msg_Dbg( p_intf, "*********** NAV 0");
90 emit navigationChanged( 0 );
91 i_old_playing_status = 0;
92 emit statusChanged( 0 ); // 0 = STOPPED, 1 = PLAY, 2 = PAUSE
97 if( !b_had_audio && b_has_audio )
99 if( !b_had_video && b_has_video )
102 /* Update position */
103 mtime_t i_length, i_time;
105 i_length = var_GetTime( p_input, "length" ) / 1000000;
106 i_time = var_GetTime( p_input, "time") / 1000000;
107 f_pos = var_GetFloat( p_input, "position" );
108 emit positionUpdated( f_pos, i_time, i_length );
110 /* Update disc status */
111 vlc_value_t val; val.i_int = 0;
112 var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
116 var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
119 msg_Dbg( p_intf, "******* CHAPTER");
120 emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
124 msg_Dbg( p_intf, "******* TITLE");
125 emit navigationChanged( 2 );
130 emit navigationChanged( 0 );
135 if( input_GetItem(p_input)->p_meta &&
136 input_GetItem(p_input)->p_meta->psz_nowplaying &&
137 *input_GetItem(p_input)->p_meta->psz_nowplaying )
139 text.sprintf( "%s - %s",
140 input_GetItem(p_input)->p_meta->psz_nowplaying,
141 input_GetItem(p_input)->psz_name );
143 else if( input_GetItem(p_input)->p_meta &&
144 input_GetItem(p_input)->p_meta->psz_artist &&
145 *input_GetItem(p_input)->p_meta->psz_artist )
147 text.sprintf( "%s - %s",
148 input_GetItem(p_input)->p_meta->psz_artist,
149 input_GetItem(p_input)->psz_name );
153 text.sprintf( "%s", input_GetItem(p_input)->psz_name );
155 if( old_name != text )
157 emit nameChanged( text );
160 /* Update playing status */
161 var_Get( p_input, "state", &val );
162 val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
163 if( i_old_playing_status != val.i_int )
165 i_old_playing_status = val.i_int;
166 emit statusChanged( val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
170 void InputManager::sliderUpdate( float new_pos )
173 var_SetFloat( p_input, "position", new_pos );
176 void InputManager::togglePlayPause()
179 var_Get( p_input, "state", &state );
180 if( state.i_int != PAUSE_S )
182 /* A stream is being played, pause it */
183 state.i_int = PAUSE_S;
187 /* Stream is paused, resume it */
188 state.i_int = PLAYING_S;
190 var_Set( p_input, "state", state );
191 emit statusChanged( state.i_int );
194 void InputManager::sectionPrev()
198 int i_type = var_Type( p_input, "next-chapter" );
199 vlc_value_t val; val.b_bool = VLC_TRUE;
200 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
201 "prev-chapter":"prev-title", val );
205 void InputManager::sectionNext()
209 int i_type = var_Type( p_input, "next-chapter" );
210 vlc_value_t val; val.b_bool = VLC_TRUE;
211 var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
212 "next-chapter":"next-title", val );
216 void InputManager::sectionMenu()
219 var_SetInteger( p_input, "title 0", 2);
222 void InputManager::slower()
225 var_SetVoid( p_input, "rate-slower" );
228 void InputManager::faster()
231 var_SetVoid( p_input, "rate-faster" );
234 void InputManager::normalRate()
237 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
240 /**********************************************************************
241 * MainInputManager implementation. Wrap an input manager and
242 * take care of updating the main playlist input
243 **********************************************************************/
244 MainInputManager * MainInputManager::instance = NULL;
246 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
250 im = new InputManager( this, p_intf );
251 ON_TIMEOUT( updateInput() );
252 /* Warn our embedded IM about input changes */
253 CONNECT( this, inputChanged( input_thread_t * ),
254 im, setInput( input_thread_t * ) );
257 MainInputManager::~MainInputManager()
259 if( p_input ) vlc_object_release( p_input );
262 void MainInputManager::updateInput()
264 if( VLC_OBJECT_INTF == p_intf->i_object_type )
266 vlc_mutex_lock( &p_intf->change_lock );
267 if( p_input && p_input->b_dead )
269 vlc_object_release( p_input );
272 emit inputChanged( NULL );
278 p_input = THEPL->p_input;
281 vlc_object_yield( p_input );
282 emit inputChanged( p_input );
286 vlc_mutex_unlock( &p_intf->change_lock );
289 /* we are working as a dialogs provider */
290 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
291 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
294 p_input = p_playlist->p_input;
295 emit inputChanged( p_input );
300 void MainInputManager::stop()
302 playlist_Stop( THEPL );
305 void MainInputManager::next()
307 playlist_Next( THEPL );
310 void MainInputManager::prev()
312 playlist_Prev( THEPL );
315 void MainInputManager::togglePlayPause()
317 if( p_input == NULL )
319 playlist_Play( THEPL );
322 getIM()->togglePlayPause();
325 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
326 vlc_value_t n, void *param )
328 InputManager *im = (InputManager*)param;
329 im->b_has_audio = true;
333 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
334 vlc_value_t n, void *param )
336 InputManager *im = (InputManager*)param;
337 im->b_has_video = true;