]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Qt4 - teletext Panel. Not any function behind it. Not my job anymore :)
[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         return;
100     }
101
102     /* Update position */
103     int i_length, i_time; /* Int is enough, since we store seconds */
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 Rate */
111     int i_new_rate = var_GetInteger( p_input, "rate");
112     if( i_new_rate != i_rate )
113     {
114         i_rate = i_new_rate;
115         /* Update rate */
116         emit rateChanged( i_rate );
117     }
118
119     /* Update navigation status */
120     vlc_value_t val; val.i_int = 0;
121     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
122     if( val.i_int > 0 )
123     {
124         val.i_int = 0;
125         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
126         emit navigationChanged( (val.i_int > 0) ? 1 : 2 );
127         /*if( val.i_int > 0 )
128         {
129             emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
130         }
131         else
132         {
133             emit navigationChanged( 2 );
134         }*/
135     }
136     else
137     {
138         emit navigationChanged( 0 );
139     }
140
141     /* Update teletext status*/
142
143     /* emit teletextEnabled( true|false ) */
144
145
146     /* Update text */
147     QString text;
148     char *psz_name = input_item_GetTitle( input_GetItem( p_input ) );
149     char *psz_nowplaying =
150         input_item_GetNowPlaying( input_GetItem( p_input ) );
151     char *psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
152     if( EMPTY_STR( psz_name ) )
153     {
154         free( psz_name );
155         psz_name = input_item_GetName( input_GetItem( p_input ) );
156     }
157     if( !EMPTY_STR( psz_nowplaying ) )
158     {
159         text.sprintf( "%s - %s", psz_nowplaying, psz_name );
160     }
161     else if( !EMPTY_STR( psz_artist ) )
162     {
163         text.sprintf( "%s - %s", psz_artist, psz_name );
164     }
165     else
166     {
167         text.sprintf( "%s", psz_name );
168     }
169     free( psz_name );
170     free( psz_nowplaying );
171     free( psz_artist );
172     if( old_name != text )
173     {
174         emit nameChanged( text );
175         old_name=text;
176     }
177
178     /* Update playing status */
179     var_Get( p_input, "state", &val );
180     val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
181     if( i_old_playing_status != val.i_int )
182     {
183         i_old_playing_status = val.i_int;
184         emit statusChanged( val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
185     }
186 }
187
188 void InputManager::sliderUpdate( float new_pos )
189 {
190     if( hasInput() ) var_SetFloat( p_input, "position", new_pos );
191 }
192
193 void InputManager::togglePlayPause()
194 {
195     vlc_value_t state;
196     var_Get( p_input, "state", &state );
197     state.i_int = ( ( state.i_int != PAUSE_S ) ? PAUSE_S : PLAYING_S );
198     /*{
199         /* A stream is being played, pause it */
200        /* state.i_int = PAUSE_S;
201     }
202     else
203     {
204         /* Stream is paused, resume it */
205         /*state.i_int = PLAYING_S;
206     }*/
207     var_Set( p_input, "state", state );
208     emit statusChanged( state.i_int );
209 }
210
211 void InputManager::sectionPrev()
212 {
213     if( hasInput() )
214     {
215         int i_type = var_Type( p_input, "next-chapter" );
216         vlc_value_t val; val.b_bool = VLC_TRUE;
217         var_Set( p_input, (i_type & VLC_VAR_TYPE) != 0 ?
218                             "prev-chapter":"prev-title", val );
219     }
220 }
221
222 void InputManager::sectionNext()
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                             "next-chapter":"next-title", val );
230     }
231 }
232
233 void InputManager::sectionMenu()
234 {
235     if( hasInput() )
236         var_SetInteger( p_input, "title 0", 2 );
237 }
238
239 void InputManager::telexGotoPage( int page )
240 {
241     //TODO
242 }
243
244 void InputManager::telexToggle( bool b_enabled )
245 {
246     //TODO
247 }
248
249 void InputManager::telexSetTransparency( bool b_transp )
250 {
251     //TODO
252 }
253
254 void InputManager::slower()
255 {
256     if( hasInput() )
257         var_SetVoid( p_input, "rate-slower" );
258 }
259
260 void InputManager::faster()
261 {
262     if( hasInput() )
263         var_SetVoid( p_input, "rate-faster" );
264 }
265
266 void InputManager::normalRate()
267 {
268     if( hasInput() )
269         var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
270 }
271
272 void InputManager::setRate( int new_rate )
273 {
274     if( hasInput() )
275         var_SetInteger( p_input, "rate", new_rate );
276 }
277
278 /**********************************************************************
279  * MainInputManager implementation. Wrap an input manager and
280  * take care of updating the main playlist input
281  **********************************************************************/
282 MainInputManager * MainInputManager::instance = NULL;
283
284 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
285                                                 p_intf( _p_intf )
286 {
287     p_input = NULL;
288     im = new InputManager( this, p_intf );
289     ON_TIMEOUT( updateInput() );
290     /* Warn our embedded IM about input changes */
291     CONNECT( this, inputChanged( input_thread_t * ),
292              im,   setInput( input_thread_t * ) );
293 }
294
295 MainInputManager::~MainInputManager()
296 {
297     if( p_input ) vlc_object_release( p_input );
298 }
299
300 void MainInputManager::updateInput()
301 {
302     if( VLC_OBJECT_INTF == p_intf->i_object_type )
303     {
304         vlc_mutex_lock( &p_intf->change_lock );
305         if( p_input && p_input->b_dead )
306         {
307             vlc_object_release( p_input );
308             getIM()->delInput();
309             p_input = NULL;
310             emit inputChanged( NULL );
311         }
312
313         if( !p_input )
314         {
315             QPL_LOCK;
316             p_input = THEPL->p_input;
317             if( p_input )
318             {
319                 vlc_object_yield( p_input );
320                 emit inputChanged( p_input );
321             }
322             QPL_UNLOCK;
323         }
324         vlc_mutex_unlock( &p_intf->change_lock );
325     }
326     else {
327         /* we are working as a dialogs provider */
328         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
329                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
330         if( p_playlist )
331         {
332             p_input = p_playlist->p_input;
333             emit inputChanged( p_input );
334         }
335     }
336 }
337
338 void MainInputManager::stop()
339 {
340    playlist_Stop( THEPL );
341 }
342
343 void MainInputManager::next()
344 {
345    playlist_Next( THEPL );
346 }
347
348 void MainInputManager::prev()
349 {
350    playlist_Prev( THEPL );
351 }
352
353 void MainInputManager::togglePlayPause()
354 {
355     if( p_input == NULL )
356     {
357         playlist_Play( THEPL );
358         return;
359     }
360     getIM()->togglePlayPause();
361 }
362
363 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
364                         vlc_value_t n, void *param )
365 {
366     InputManager *im = (InputManager*)param;
367     im->b_has_audio = true;
368     return VLC_SUCCESS;
369 }
370
371 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
372                         vlc_value_t n, void *param )
373 {
374     InputManager *im = (InputManager*)param;
375     im->b_has_video = true;
376     return VLC_SUCCESS;
377 }