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