]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
a641bee5ed3feee494b18887e5da3ed7def13e64
[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 //TODO break that
83 void InputManager::update()
84 {
85     /// \todo Emit the signals only if it changed
86     if( !p_input )
87     {
88         emit nameChanged( "" );
89         return;
90     }
91
92     if( p_input->b_dead || p_input->b_die )
93     {
94         emit positionUpdated( 0.0, 0, 0 );
95         emit navigationChanged( 0 );
96         i_old_playing_status = 0;
97         emit statusChanged( END_S ); // see vlc_input.h, input_state_e enum
98         delInput();
99         emit artChanged( "" );
100         return;
101     }
102
103     /* Update position */
104     int i_length, i_time; /* Int is enough, since we store seconds */
105     float f_pos;
106     i_length = var_GetTime( p_input, "length" ) / 1000000;
107     i_time = var_GetTime( p_input, "time") / 1000000;
108     f_pos = var_GetFloat( p_input, "position" );
109     emit positionUpdated( f_pos, i_time, i_length );
110
111     /* Update Rate */
112     int i_new_rate = var_GetInteger( p_input, "rate");
113     if( i_new_rate != i_rate )
114     {
115         i_rate = i_new_rate;
116         /* Update rate */
117         emit rateChanged( i_rate );
118     }
119
120     /* Update navigation status */
121     vlc_value_t val; val.i_int = 0;
122     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
123     if( val.i_int > 0 )
124     {
125         val.i_int = 0;
126         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
127         emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
128         /*if( val.i_int > 0 )
129         {
130             emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
131         }
132         else
133         {
134             emit navigationChanged( 2 );
135         }*/
136     }
137     else
138     {
139         emit navigationChanged( 0 );
140     }
141
142 #ifdef ZVBI_COMPILED
143     /* Update teletext status*/
144     emit teletextEnabled( true );/* FIXME */
145 #endif
146
147     /* Update text */
148     QString text;
149     char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
150     char *psz_nowplaying =
151         input_item_GetNowPlaying( input_GetItem( p_input ) );
152     char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
153     if( EMPTY_STR( psz_name ) )
154     {
155         free( psz_name );
156         psz_name = input_item_GetName( input_GetItem( p_input ) );
157     }
158     if( !EMPTY_STR( psz_nowplaying ) )
159     {
160         text.sprintf( "%s - %s", psz_nowplaying, psz_name );
161     }
162     else if( !EMPTY_STR( psz_artist ) )
163     {
164         text.sprintf( "%s - %s", psz_artist, psz_name );
165     }
166     else
167     {
168         text.sprintf( "%s", psz_name );
169     }
170     free( psz_name );
171     free( psz_nowplaying );
172     free( psz_artist );
173     if( old_name != text )
174     {
175         emit nameChanged( text );
176         old_name=text;
177     }
178
179     /* Update playing status */
180     var_Get( p_input, "state", &val );
181     val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
182     if( i_old_playing_status != val.i_int )
183     {
184         i_old_playing_status = val.i_int;
185         emit statusChanged( val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
186     }
187
188     QString url;
189     char *psz_art = input_item_GetArtURL( input_GetItem( p_input ) );
190     url.sprintf("%s", psz_art );
191     free( psz_art );
192     if( artUrl != url )
193     {
194         artUrl = url.replace( "file://",QString("" ) );
195         emit artChanged( artUrl );
196     }
197 }
198
199 void InputManager::sliderUpdate( float new_pos )
200 {
201     if( hasInput() ) var_SetFloat( p_input, "position", new_pos );
202 }
203
204 void InputManager::togglePlayPause()
205 {
206     vlc_value_t state;
207     var_Get( p_input, "state", &state );
208     state.i_int = ( ( state.i_int != PAUSE_S ) ? PAUSE_S : PLAYING_S );
209     /*{
210         /* A stream is being played, pause it */
211        /* state.i_int = PAUSE_S;
212     }
213     else
214     {
215         /* Stream is paused, resume it */
216         /*state.i_int = PLAYING_S;
217     }*/
218     var_Set( p_input, "state", state );
219     emit statusChanged( state.i_int );
220 }
221
222 void InputManager::sectionPrev()
223 {
224     if( hasInput() )
225     {
226         int i_type = var_Type( p_input, "next-chapter" );
227         vlc_value_t val; val.b_bool = VLC_TRUE;
228         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
229                             "prev-chapter":"prev-title", val );
230     }
231 }
232
233 void InputManager::sectionNext()
234 {
235     if( hasInput() )
236     {
237         int i_type = var_Type( p_input, "next-chapter" );
238         vlc_value_t val; val.b_bool = VLC_TRUE;
239         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
240                             "next-chapter":"next-title", val );
241     }
242 }
243
244 void InputManager::sectionMenu()
245 {
246     if( hasInput() )
247         var_SetInteger( p_input, "title 0", 2 );
248 }
249
250 #ifdef ZVBI_COMPILED
251 void InputManager::telexGotoPage( int page )
252 {
253     if( hasInput() )
254     {
255         vlc_object_t *p_vbi;
256         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
257                     "zvbi", FIND_ANYWHERE );
258         if( p_vbi )
259         {
260             var_SetInteger( p_vbi, "vbi-page", page );
261             vlc_object_release( p_vbi );
262         }
263     }
264 }
265
266 void InputManager::telexToggle( bool b_enabled )
267 {
268     int i_page = 0;
269
270     if( b_enabled )
271         i_page = 100;
272     telexGotoPage( i_page );
273 }
274
275 void InputManager::telexSetTransparency( bool b_transp )
276 {
277     if( hasInput() )
278     {
279         vlc_object_t *p_vbi;
280         p_vbi = (vlc_object_t *) vlc_object_find_name( p_input,
281                     "zvbi", FIND_ANYWHERE );
282         if( p_vbi )
283         {
284             var_SetBool( p_input->p_libvlc, "vbi-opaque", b_transp );
285             vlc_object_release( p_vbi );
286         }
287     }
288 }
289 #endif
290
291 void InputManager::slower()
292 {
293     if( hasInput() )
294         var_SetVoid( p_input, "rate-slower" );
295 }
296
297 void InputManager::faster()
298 {
299     if( hasInput() )
300         var_SetVoid( p_input, "rate-faster" );
301 }
302
303 void InputManager::normalRate()
304 {
305     if( hasInput() )
306         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
307 }
308
309 void InputManager::setRate( int new_rate )
310 {
311     if( hasInput() )
312         var_SetInteger( p_input, "rate", new_rate );
313 }
314
315 /**********************************************************************
316  * MainInputManager implementation. Wrap an input manager and
317  * take care of updating the main playlist input
318  **********************************************************************/
319 MainInputManager * MainInputManager::instance = NULL;
320
321 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
322                                                 p_intf( _p_intf )
323 {
324     p_input = NULL;
325     im = new InputManager( this, p_intf );
326     ON_TIMEOUT( updateInput() );
327     /* Warn our embedded IM about input changes */
328     CONNECT( this, inputChanged( input_thread_t * ),
329              im,   setInput( input_thread_t * ) );
330 }
331
332 MainInputManager::~MainInputManager()
333 {
334     if( p_input ) vlc_object_release( p_input );
335 }
336
337 void MainInputManager::updateInput()
338 {
339     if( VLC_OBJECT_INTF == p_intf->i_object_type )
340     {
341         vlc_mutex_lock( &p_intf->change_lock );
342         if( p_input && p_input->b_dead )
343         {
344             vlc_object_release( p_input );
345             getIM()->delInput();
346             p_input = NULL;
347             emit inputChanged( NULL );
348         }
349
350         if( !p_input )
351         {
352             QPL_LOCK;
353             p_input = THEPL->p_input;
354             if( p_input )
355             {
356                 vlc_object_yield( p_input );
357                 emit inputChanged( p_input );
358             }
359             QPL_UNLOCK;
360         }
361         vlc_mutex_unlock( &p_intf->change_lock );
362     }
363     else {
364         /* we are working as a dialogs provider */
365         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
366                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
367         if( p_playlist )
368         {
369             p_input = p_playlist->p_input;
370             emit inputChanged( p_input );
371         }
372     }
373 }
374
375 void MainInputManager::stop()
376 {
377    playlist_Stop( THEPL );
378 }
379
380 void MainInputManager::next()
381 {
382    playlist_Next( THEPL );
383 }
384
385 void MainInputManager::prev()
386 {
387    playlist_Prev( THEPL );
388 }
389
390 void MainInputManager::togglePlayPause()
391 {
392     if( p_input == NULL )
393     {
394         playlist_Play( THEPL );
395         return;
396     }
397     getIM()->togglePlayPause();
398 }
399
400 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
401                         vlc_value_t n, void *param )
402 {
403     InputManager *im = (InputManager*)param;
404     im->b_has_audio = true;
405     return VLC_SUCCESS;
406 }
407
408 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
409                         vlc_value_t n, void *param )
410 {
411     InputManager *im = (InputManager*)param;
412     im->b_has_video = true;
413     return VLC_SUCCESS;
414 }