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