]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Remove unused headerfile
[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 "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     p_input = NULL;
42     ON_TIMEOUT( update() );
43 }
44
45 InputManager::~InputManager()
46 {
47     delInput();
48 }
49
50 void InputManager::setInput( input_thread_t *_p_input )
51 {
52     delInput();
53     p_input = _p_input;
54     emit positionUpdated( 0.0,0,0 );
55     b_had_audio = b_had_video = b_has_audio = b_has_video = false;
56     if( p_input )
57     {
58         vlc_object_yield( p_input );
59         vlc_value_t val;
60         var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
61         b_has_video = val.i_int > 0;
62         var_Change( p_input, "audio-es", VLC_VAR_CHOICESCOUNT, &val, NULL );
63         b_has_audio = val.i_int > 0;
64         var_AddCallback( p_input, "audio-es", ChangeAudio, this );
65         var_AddCallback( p_input, "video-es", ChangeVideo, this );
66     }
67 }
68
69 void InputManager::delInput()
70 {
71     if( p_input )
72     {
73         var_DelCallback( p_input, "audio-es", ChangeAudio, this );
74         var_DelCallback( p_input, "video-es", ChangeVideo, this );
75         vlc_object_release( p_input );
76         p_input = NULL;
77     }
78 }
79
80 void InputManager::update()
81 {
82     /// \todo Emit the signals only if it changed
83     if( !p_input ) return;
84
85     if( p_input->b_dead || p_input->b_die )
86     {
87         emit positionUpdated( 0.0, 0, 0 );
88         msg_Dbg( p_intf, "*********** NAV 0");
89         emit navigationChanged( 0 );
90         emit statusChanged( 0 ); // 0 = STOPPED, 1 = PLAY, 2 = PAUSE
91         delInput();
92         return;
93     }
94
95     if( !b_had_audio && b_has_audio )
96         emit audioStarted();
97     if( !b_had_video && b_has_video )
98         emit videoStarted();
99
100     /* Update position */
101     mtime_t i_length, i_time;
102     float f_pos;
103     i_length = var_GetTime( p_input, "length" ) / 1000000;
104     i_time = var_GetTime( p_input, "time") / 1000000;
105     f_pos = var_GetFloat( p_input, "position" );
106     emit positionUpdated( f_pos, i_time, i_length );
107
108     /* Update disc status */
109     vlc_value_t val; val.i_int = 0;
110     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
111     if( val.i_int > 0 )
112     {
113         val.i_int = 0;
114         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
115         if( val.i_int > 0 )
116         {
117             msg_Dbg( p_intf, "******* CHAPTER");
118             emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
119         }
120         else
121         {
122             msg_Dbg( p_intf, "******* TITLE");
123             emit navigationChanged( 2 );
124         }
125     }
126     else
127     {
128         emit navigationChanged( 0 );
129     }
130
131     /* Update text */
132     QString text;
133     if( input_GetItem(p_input)->p_meta &&
134         input_GetItem(p_input)->p_meta->psz_nowplaying &&
135         *input_GetItem(p_input)->p_meta->psz_nowplaying )
136     {
137         text.sprintf( "%s - %s",
138                   input_GetItem(p_input)->p_meta->psz_nowplaying,
139                   input_GetItem(p_input)->psz_name );
140     }
141     else
142     {
143         text.sprintf( "%s", input_GetItem(p_input)->psz_name );
144     }
145     emit nameChanged( text );
146
147     /* Update playing status */
148     var_Get( p_input, "state", &val );
149     val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
150     if( i_old_playing_status != val.i_int )
151     {
152         i_old_playing_status = val.i_int;
153         emit statusChanged(  val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
154     }
155 }
156
157 void InputManager::sliderUpdate( float new_pos )
158 {
159     if( hasInput() )
160         var_SetFloat( p_input, "position", new_pos );
161 }
162
163 void InputManager::togglePlayPause()
164 {
165     vlc_value_t state;
166     var_Get( p_input, "state", &state );
167     if( state.i_int != PAUSE_S )
168     {
169         /* A stream is being played, pause it */
170         state.i_int = PAUSE_S;
171     }
172     else
173     {
174         /* Stream is paused, resume it */
175         state.i_int = PLAYING_S;
176     }
177     var_Set( p_input, "state", state );
178     emit statusChanged( state.i_int );
179 }
180
181 void InputManager::sectionPrev()
182 {
183     if( hasInput() )
184     {
185         int i_type = var_Type( p_input, "prev-chapter" );
186         vlc_value_t val; val.b_bool = VLC_TRUE;
187         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
188                             "prev-chapter":"prev-title", val );
189     }
190 }
191
192 void InputManager::sectionNext()
193 {
194     if( hasInput() )
195     {
196         int i_type = var_Type( p_input, "prev-chapter" );
197         vlc_value_t val; val.b_bool = VLC_TRUE;
198         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
199                             "next-chapter":"next-title", val );
200     }
201 }
202
203 void InputManager::sectionMenu()
204 {
205     if( hasInput() )
206         var_SetInteger( p_input, "title 0", 2);
207 }
208
209 void InputManager::slower()
210 {
211     if( hasInput() )
212         var_SetVoid( p_input, "rate-slower" );
213 }
214
215 void InputManager::faster()
216 {
217     if( hasInput() )
218         var_SetVoid( p_input, "rate-faster" );
219 }
220
221 void InputManager::normalRate()
222 {
223     if( hasInput() )
224         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
225 }
226
227 /**********************************************************************
228  * MainInputManager implementation. Wrap an input manager and
229  * take care of updating the main playlist input
230  **********************************************************************/
231 MainInputManager * MainInputManager::instance = NULL;
232
233 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
234                                                 p_intf( _p_intf )
235 {
236     p_input = NULL;
237     im = new InputManager( this, p_intf );
238     ON_TIMEOUT( updateInput() );
239     /* Warn our embedded IM about input changes */
240     CONNECT( this, inputChanged( input_thread_t * ),
241              im,   setInput( input_thread_t * ) );
242 }
243
244 MainInputManager::~MainInputManager()
245 {
246     if( p_input ) vlc_object_release( p_input );
247 }
248
249 void MainInputManager::updateInput()
250 {
251     vlc_mutex_lock( &p_intf->change_lock );
252     if( p_input && p_input->b_dead )
253     {
254         vlc_object_release( p_input );
255         getIM()->delInput();
256         p_input = NULL;
257         emit inputChanged( NULL );
258     }
259
260     if( !p_input )
261     {
262         QPL_LOCK;
263         p_input = THEPL->p_input;
264         if( p_input )
265         {
266             vlc_object_yield( p_input );
267             emit inputChanged( p_input );
268         }
269         QPL_UNLOCK;
270     }
271     vlc_mutex_unlock( &p_intf->change_lock );
272 }
273
274 void MainInputManager::togglePlayPause()
275 {
276     if( p_input == NULL )
277     {
278         playlist_Play( THEPL );
279         return;
280     }
281     getIM()->togglePlayPause();
282 }
283
284
285 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
286                         vlc_value_t n, void *param )
287 {
288     InputManager *im = (InputManager*)param;
289     im->b_has_audio = true;
290     return 0;
291 }
292
293 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
294                         vlc_value_t n, void *param )
295 {
296     InputManager *im = (InputManager*)param;
297     im->b_has_video = true;
298     return 0;
299 }