]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Qt4 - removing some debug.
[vlc] / modules / gui / qt4 / input_manager.cpp
1 /*****************************************************************************
2  * input_manager.cpp : Manage an input and interact with its GUI elements
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include "qt4.hpp"
25 #include "input_manager.hpp"
26 #include "dialogs_provider.hpp"
27
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 );
32
33 /**********************************************************************
34  * InputManager implementation
35  **********************************************************************/
36
37 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
38                            QObject( parent ), p_intf( _p_intf )
39 {
40     i_old_playing_status = END_S;
41     old_name="";
42     p_input = NULL;
43     ON_TIMEOUT( update() );
44 }
45
46 InputManager::~InputManager()
47 {
48     delInput();
49 }
50
51 void InputManager::setInput( input_thread_t *_p_input )
52 {
53     delInput();
54     p_input = _p_input;
55     emit positionUpdated( 0.0,0,0 );
56     b_had_audio = b_had_video = b_has_audio = b_has_video = false;
57     if( p_input )
58     {
59         vlc_object_yield( p_input );
60         vlc_value_t val;
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 );
67     }
68 }
69
70 void InputManager::delInput()
71 {
72     if( p_input )
73     {
74         var_DelCallback( p_input, "audio-es", ChangeAudio, this );
75         var_DelCallback( p_input, "video-es", ChangeVideo, this );
76         vlc_object_release( p_input );
77         p_input = NULL;
78     }
79 }
80
81 void InputManager::update()
82 {
83     /// \todo Emit the signals only if it changed
84     if( !p_input ) return;
85
86     if( p_input->b_dead || p_input->b_die )
87     {
88         emit positionUpdated( 0.0, 0, 0 );
89         emit navigationChanged( 0 );
90         i_old_playing_status = 0;
91         emit statusChanged( 0 ); // 0 = STOPPED, 1 = PLAY, 2 = PAUSE
92         delInput();
93         return;
94     }
95
96     if( !b_had_audio && b_has_audio )
97         emit audioStarted();
98     if( !b_had_video && b_has_video )
99         emit videoStarted();
100
101     /* Update position */
102     mtime_t i_length, i_time;
103     float f_pos;
104     i_length = var_GetTime( p_input, "length" ) / 1000000;
105     i_time = var_GetTime( p_input, "time") / 1000000;
106     f_pos = var_GetFloat( p_input, "position" );
107     emit positionUpdated( f_pos, i_time, i_length );
108
109     /* Update disc status */
110     vlc_value_t val; val.i_int = 0;
111     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
112     if( val.i_int > 0 )
113     {
114         val.i_int = 0;
115         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
116         if( val.i_int > 0 )
117         {          
118             emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
119         }
120         else
121         {
122             emit navigationChanged( 2 );
123         }
124     }
125     else
126     {
127         emit navigationChanged( 0 );
128     }
129
130     /* Update text */
131     QString text;
132     char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
133     char *psz_nowplaying =
134         input_item_GetNowPlaying( input_GetItem( p_input ) );
135     char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
136     if( EMPTY_STR( psz_name ) )
137     {
138         free( psz_name );
139         psz_name = input_item_GetName( input_GetItem( p_input ) );
140     }
141     if( !EMPTY_STR( psz_nowplaying ) )
142     {
143         text.sprintf( "%s - %s", psz_nowplaying, psz_name );
144     }
145     else if( !EMPTY_STR( psz_artist ) )
146     {
147         text.sprintf( "%s - %s", psz_artist, psz_name );
148     }
149     else
150     {
151         text.sprintf( "%s", psz_name );
152     }
153     free( psz_name );
154     free( psz_nowplaying );
155     free( psz_artist );
156     if( old_name != text )
157     {
158         emit nameChanged( text );
159         old_name=text;
160     }
161     /* Update playing status */
162     var_Get( p_input, "state", &val );
163     val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
164     if( i_old_playing_status != val.i_int )
165     {
166         i_old_playing_status = val.i_int;
167         emit statusChanged(  val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
168     }
169 }
170
171 void InputManager::sliderUpdate( float new_pos )
172 {
173     if( hasInput() )
174         var_SetFloat( p_input, "position", new_pos );
175 }
176
177 void InputManager::togglePlayPause()
178 {
179     vlc_value_t state;
180     var_Get( p_input, "state", &state );
181     if( state.i_int != PAUSE_S )
182     {
183         /* A stream is being played, pause it */
184         state.i_int = PAUSE_S;
185     }
186     else
187     {
188         /* Stream is paused, resume it */
189         state.i_int = PLAYING_S;
190     }
191     var_Set( p_input, "state", state );
192     emit statusChanged( state.i_int );
193 }
194
195 void InputManager::sectionPrev()
196 {
197     if( hasInput() )
198     {
199         int i_type = var_Type( p_input, "next-chapter" );
200         vlc_value_t val; val.b_bool = VLC_TRUE;
201         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
202                             "prev-chapter":"prev-title", val );
203     }
204 }
205
206 void InputManager::sectionNext()
207 {
208     if( hasInput() )
209     {
210         int i_type = var_Type( p_input, "next-chapter" );
211         vlc_value_t val; val.b_bool = VLC_TRUE;
212         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
213                             "next-chapter":"next-title", val );
214     }
215 }
216
217 void InputManager::sectionMenu()
218 {
219     if( hasInput() )
220         var_SetInteger( p_input, "title 0", 2 );
221 }
222
223 void InputManager::slower()
224 {
225     if( hasInput() )
226         var_SetVoid( p_input, "rate-slower" );
227 }
228
229 void InputManager::faster()
230 {
231     if( hasInput() )
232         var_SetVoid( p_input, "rate-faster" );
233 }
234
235 void InputManager::normalRate()
236 {
237     if( hasInput() )
238         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
239 }
240
241 /**********************************************************************
242  * MainInputManager implementation. Wrap an input manager and
243  * take care of updating the main playlist input
244  **********************************************************************/
245 MainInputManager * MainInputManager::instance = NULL;
246
247 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
248                                                 p_intf( _p_intf )
249 {
250     p_input = NULL;
251     im = new InputManager( this, p_intf );
252     ON_TIMEOUT( updateInput() );
253     /* Warn our embedded IM about input changes */
254     CONNECT( this, inputChanged( input_thread_t * ),
255              im,   setInput( input_thread_t * ) );
256 }
257
258 MainInputManager::~MainInputManager()
259 {
260     if( p_input ) vlc_object_release( p_input );
261 }
262
263 void MainInputManager::updateInput()
264 {
265     if( VLC_OBJECT_INTF == p_intf->i_object_type )
266     {
267         vlc_mutex_lock( &p_intf->change_lock );
268         if( p_input && p_input->b_dead )
269         {
270             vlc_object_release( p_input );
271             getIM()->delInput();
272             p_input = NULL;
273             emit inputChanged( NULL );
274         }
275
276         if( !p_input )
277         {
278             QPL_LOCK;
279             p_input = THEPL->p_input;
280             if( p_input )
281             {
282                 vlc_object_yield( p_input );
283                 emit inputChanged( p_input );
284             }
285             QPL_UNLOCK;
286         }
287         vlc_mutex_unlock( &p_intf->change_lock );
288     }
289     else {
290         /* we are working as a dialogs provider */
291         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
292                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
293         if( p_playlist )
294         {
295             p_input = p_playlist->p_input;
296             emit inputChanged( p_input );
297         }
298     }
299 }
300
301 void MainInputManager::stop()
302 {
303    playlist_Stop( THEPL );
304 }
305
306 void MainInputManager::next()
307 {
308    playlist_Next( THEPL );
309 }
310
311 void MainInputManager::prev()
312 {
313    playlist_Prev( THEPL );
314 }
315
316 void MainInputManager::togglePlayPause()
317 {
318     if( p_input == NULL )
319     {
320         playlist_Play( THEPL );
321         return;
322     }
323     getIM()->togglePlayPause();
324 }
325
326 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
327                         vlc_value_t n, void *param )
328 {
329     InputManager *im = (InputManager*)param;
330     im->b_has_audio = true;
331     return 0;
332 }
333
334 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
335                         vlc_value_t n, void *param )
336 {
337     InputManager *im = (InputManager*)param;
338     im->b_has_video = true;
339     return 0;
340 }