]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
0b88bc3f4b998a350265d618bc1fde1788e31474
[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 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 <assert.h>
25
26 #include "qt4.hpp"
27 #include "input_manager.hpp"
28 #include "dialogs_provider.hpp"
29
30 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
31                         vlc_value_t n, void *param );
32 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
33                         vlc_value_t n, void *param );
34
35 /**********************************************************************
36  * InputManager implementation
37  **********************************************************************/
38
39 InputManager::InputManager( QObject *parent, intf_thread_t *_p_intf) :
40                            QObject( parent ), p_intf( _p_intf )
41 {
42     i_old_playing_status = END_S;
43     p_input = NULL;
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 ) return;
86
87     if( p_input->b_dead || p_input->b_die )
88     {
89         emit positionUpdated( 0.0, 0, 0 );
90         msg_Dbg( p_intf, "*********** NAV 0");
91         emit navigationChanged( 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     if( input_GetItem(p_input)->p_meta &&
136         input_GetItem(p_input)->p_meta->psz_nowplaying &&
137         *input_GetItem(p_input)->p_meta->psz_nowplaying )
138     {
139         text.sprintf( "%s - %s",
140                   input_GetItem(p_input)->p_meta->psz_nowplaying,
141                   input_GetItem(p_input)->psz_name );
142     }
143     else
144     {
145         text.sprintf( "%s", input_GetItem(p_input)->psz_name );
146     }
147     emit nameChanged( text );
148
149     /* Update playing status */
150     var_Get( p_input, "state", &val );
151     val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
152     if( i_old_playing_status != val.i_int )
153     {
154         i_old_playing_status = val.i_int;
155         emit statusChanged(  val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
156     }
157 }
158
159 void InputManager::sliderUpdate( float new_pos )
160 {
161     if( hasInput() )
162         var_SetFloat( p_input, "position", new_pos );
163 }
164
165 void InputManager::togglePlayPause()
166 {
167     vlc_value_t state;
168     var_Get( p_input, "state", &state );
169     if( state.i_int != PAUSE_S )
170     {
171         /* A stream is being played, pause it */
172         state.i_int = PAUSE_S;
173     }
174     else
175     {
176         /* Stream is paused, resume it */
177         state.i_int = PLAYING_S;
178     }
179     var_Set( p_input, "state", state );
180     emit statusChanged( state.i_int );
181 }
182
183 void InputManager::sectionPrev()
184 {
185     if( hasInput() )
186     {
187         int i_type = var_Type( p_input, "prev-chapter" );
188         vlc_value_t val; val.b_bool = VLC_TRUE;
189         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
190                             "prev-chapter":"prev-title", val );
191     }
192 }
193
194 void InputManager::sectionNext()
195 {
196     if( hasInput() )
197     {
198         int i_type = var_Type( p_input, "prev-chapter" );
199         vlc_value_t val; val.b_bool = VLC_TRUE;
200         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
201                             "next-chapter":"next-title", val );
202     }
203 }
204
205 void InputManager::sectionMenu()
206 {
207     if( hasInput() )
208         var_SetInteger( p_input, "title 0", 2);
209 }
210
211 void InputManager::slower()
212 {
213     if( hasInput() )
214         var_SetVoid( p_input, "rate-slower" );
215 }
216
217 void InputManager::faster()
218 {
219     if( hasInput() )
220         var_SetVoid( p_input, "rate-faster" );
221 }
222
223 void InputManager::normalRate()
224 {
225     if( hasInput() )
226         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
227 }
228
229 /**********************************************************************
230  * MainInputManager implementation. Wrap an input manager and
231  * take care of updating the main playlist input
232  **********************************************************************/
233 MainInputManager * MainInputManager::instance = NULL;
234
235 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
236                                                 p_intf( _p_intf )
237 {
238     p_input = NULL;
239     im = new InputManager( this, p_intf );
240     ON_TIMEOUT( updateInput() );
241     /* Warn our embedded IM about input changes */
242     CONNECT( this, inputChanged( input_thread_t * ),
243              im,   setInput( input_thread_t * ) );
244 }
245
246 MainInputManager::~MainInputManager()
247 {
248     if( p_input ) vlc_object_release( p_input );
249 }
250
251 void MainInputManager::updateInput()
252 {
253     vlc_mutex_lock( &p_intf->change_lock );
254     if( p_input && p_input->b_dead )
255     {
256         vlc_object_release( p_input );
257         getIM()->delInput();
258         p_input = NULL;
259         emit inputChanged( NULL );
260     }
261
262     if( !p_input )
263     {
264         QPL_LOCK;
265         p_input = THEPL->p_input;
266         if( p_input )
267         {
268             vlc_object_yield( p_input );
269             emit inputChanged( p_input );
270         }
271         QPL_UNLOCK;
272     }
273     vlc_mutex_unlock( &p_intf->change_lock );
274 }
275
276 void MainInputManager::togglePlayPause()
277 {
278     if( p_input == NULL )
279     {
280         playlist_Play( THEPL );
281         return;
282     }
283     getIM()->togglePlayPause();
284 }
285
286
287 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
288                         vlc_value_t n, void *param )
289 {
290     InputManager *im = (InputManager*)param;
291     im->b_has_audio = true;
292     return 0;
293 }
294
295 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
296                         vlc_value_t n, void *param )
297 {
298     InputManager *im = (InputManager*)param;
299     im->b_has_video = true;
300     return 0;
301 }