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