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