]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Input access locking. Part one
[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         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
93         delInput();
94         return;
95     }
96
97     if( !b_had_audio && b_has_audio )
98         emit audioStarted();
99     if( !b_had_video && b_has_video )
100         emit videoStarted();
101
102     /* Update position */
103     mtime_t i_length, i_time;
104     float f_pos;
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 );
109
110     /* Update disc status */
111     vlc_value_t val; val.i_int = 0;
112     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
113     if( val.i_int > 0 )
114     {
115         val.i_int = 0;
116         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
117         if( val.i_int > 0 )
118         {
119             msg_Dbg( p_intf, "******* CHAPTER");
120             emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
121         }
122         else
123         {
124             msg_Dbg( p_intf, "******* TITLE");
125             emit navigationChanged( 2 );
126         }
127     }
128     else
129     {
130         emit navigationChanged( 0 );
131     }
132
133     /* Update text */
134     QString text;
135     char *psz_name = input_GetName( input_GetItem( p_input ) );
136     char *psz_nowplaying = input_item_GetNowPlaying( input_GetItem( p_input );
137     char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
138     if( !EMPTY_STR( psz_nowplaying ) )
139     {
140         text.sprintf( "%s - %s", psz_now_playing, psz_name );
141     }
142     else if( !EMPTY_STR( psz_artist ) )
143     {
144         text.sprintf( "%s - %s", psz_artist, psz_name );
145     }
146     else
147     {
148         text.sprintf( "%s", psz_name );
149     }
150     free( psz_name );
151     free( psz_nowplaying );
152     free( psz_artist );
153     if( old_name != text )
154     {
155         emit nameChanged( text );
156         old_name=text;
157     }
158     /* Update playing status */
159     var_Get( p_input, "state", &val );
160     val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
161     if( i_old_playing_status != val.i_int )
162     {
163         i_old_playing_status = val.i_int;
164         emit statusChanged(  val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
165     }
166 }
167
168 void InputManager::sliderUpdate( float new_pos )
169 {
170     if( hasInput() )
171         var_SetFloat( p_input, "position", new_pos );
172 }
173
174 void InputManager::togglePlayPause()
175 {
176     vlc_value_t state;
177     var_Get( p_input, "state", &state );
178     if( state.i_int != PAUSE_S )
179     {
180         /* A stream is being played, pause it */
181         state.i_int = PAUSE_S;
182     }
183     else
184     {
185         /* Stream is paused, resume it */
186         state.i_int = PLAYING_S;
187     }
188     var_Set( p_input, "state", state );
189     emit statusChanged( state.i_int );
190 }
191
192 void InputManager::sectionPrev()
193 {
194     if( hasInput() )
195     {
196         int i_type = var_Type( p_input, "next-chapter" );
197         vlc_value_t val; val.b_bool = VLC_TRUE;
198         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
199                             "prev-chapter":"prev-title", val );
200     }
201 }
202
203 void InputManager::sectionNext()
204 {
205     if( hasInput() )
206     {
207         int i_type = var_Type( p_input, "next-chapter" );
208         vlc_value_t val; val.b_bool = VLC_TRUE;
209         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
210                             "next-chapter":"next-title", val );
211     }
212 }
213
214 void InputManager::sectionMenu()
215 {
216     if( hasInput() )
217         var_SetInteger( p_input, "title 0", 2);
218 }
219
220 void InputManager::slower()
221 {
222     if( hasInput() )
223         var_SetVoid( p_input, "rate-slower" );
224 }
225
226 void InputManager::faster()
227 {
228     if( hasInput() )
229         var_SetVoid( p_input, "rate-faster" );
230 }
231
232 void InputManager::normalRate()
233 {
234     if( hasInput() )
235         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
236 }
237
238 /**********************************************************************
239  * MainInputManager implementation. Wrap an input manager and
240  * take care of updating the main playlist input
241  **********************************************************************/
242 MainInputManager * MainInputManager::instance = NULL;
243
244 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
245                                                 p_intf( _p_intf )
246 {
247     p_input = NULL;
248     im = new InputManager( this, p_intf );
249     ON_TIMEOUT( updateInput() );
250     /* Warn our embedded IM about input changes */
251     CONNECT( this, inputChanged( input_thread_t * ),
252              im,   setInput( input_thread_t * ) );
253 }
254
255 MainInputManager::~MainInputManager()
256 {
257     if( p_input ) vlc_object_release( p_input );
258 }
259
260 void MainInputManager::updateInput()
261 {
262     if( VLC_OBJECT_INTF == p_intf->i_object_type )
263     {
264         vlc_mutex_lock( &p_intf->change_lock );
265         if( p_input && p_input->b_dead )
266         {
267             vlc_object_release( p_input );
268             getIM()->delInput();
269             p_input = NULL;
270             emit inputChanged( NULL );
271         }
272
273         if( !p_input )
274         {
275             QPL_LOCK;
276             p_input = THEPL->p_input;
277             if( p_input )
278             {
279                 vlc_object_yield( p_input );
280                 emit inputChanged( p_input );
281             }
282             QPL_UNLOCK;
283         }
284         vlc_mutex_unlock( &p_intf->change_lock );
285     }
286     else {
287         /* we are working as a dialogs provider */
288         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
289                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
290         if( p_playlist )
291         {
292             p_input = p_playlist->p_input;
293             emit inputChanged( p_input );
294         }
295     }
296 }
297
298 void MainInputManager::stop()
299 {
300    playlist_Stop( THEPL );
301 }
302
303 void MainInputManager::next()
304 {
305    playlist_Next( THEPL );
306 }
307
308 void MainInputManager::prev()
309 {
310    playlist_Prev( THEPL );
311 }
312
313 void MainInputManager::togglePlayPause()
314 {
315     if( p_input == NULL )
316     {
317         playlist_Play( THEPL );
318         return;
319     }
320     getIM()->togglePlayPause();
321 }
322
323 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
324                         vlc_value_t n, void *param )
325 {
326     InputManager *im = (InputManager*)param;
327     im->b_has_audio = true;
328     return 0;
329 }
330
331 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
332                         vlc_value_t n, void *param )
333 {
334     InputManager *im = (InputManager*)param;
335     im->b_has_video = true;
336     return 0;
337 }