]> git.sesse.net Git - vlc/blob - modules/gui/qt4/input_manager.cpp
Preliminary work for better audio visualization handling
[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 "input_manager.hpp"
27 #include "dialogs_provider.hpp"
28 #include "qt4.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     /* Subscribe to updates */
45     connect( THEDP->fixed_timer, SIGNAL( timeout() ), this, SLOT( update() ) );
46 }
47
48 InputManager::~InputManager()
49 {
50 }
51
52 void InputManager::setInput( input_thread_t *_p_input )
53 {
54     p_input = _p_input;
55     emit positionUpdated( 0.0,0,0 );
56     b_had_audio = b_had_video = b_has_audio = b_has_video = false;
57     if( p_input )
58     {
59         var_AddCallback( p_input, "audio-es", ChangeAudio, this );
60         var_AddCallback( p_input, "video-es", ChangeVideo, this );
61     }
62
63 }
64 void InputManager::delInput()
65 {
66     if( p_input )
67     {
68         var_DelCallback( p_input, "audio-es", ChangeAudio, this );
69         var_DelCallback( p_input, "video-es", ChangeVideo, this );
70     }
71 }
72
73 void InputManager::update()
74 {
75     /// \todo Emit the signals only if it changed
76     if( !p_input  ) return;
77
78     if( p_input->b_dead || p_input->b_die )
79     {
80         emit positionUpdated( 0.0, 0, 0 );
81         emit navigationChanged( 0 );
82         emit statusChanged( 0 ); // 0 = STOPPED, 1 = PLAY, 2 = PAUSE
83     }
84
85     if( !b_had_audio && b_has_audio )
86         emit audioStarted();
87     if( !b_had_video && b_has_video )
88         emit videoStarted();
89
90     /* Update position */
91     mtime_t i_length, i_time;
92     float f_pos;
93     i_length = var_GetTime( p_input, "length" ) / 1000000;
94     i_time = var_GetTime( p_input, "time") / 1000000;
95     f_pos = var_GetFloat( p_input, "position" );
96     emit positionUpdated( f_pos, i_time, i_length );
97
98     /* Update disc status */
99     vlc_value_t val;
100     var_Change( p_input, "title", VLC_VAR_CHOICESCOUNT, &val, NULL );
101     if( val.i_int > 0 )
102     {
103         vlc_value_t val;
104         var_Change( p_input, "chapter", VLC_VAR_CHOICESCOUNT, &val, NULL );
105         if( val.i_int > 0 )
106             emit navigationChanged( 1 ); // 1 = chapter, 2 = title, 0 = NO
107         else
108             emit navigationChanged( 2 );
109     }
110     else
111     {
112         emit navigationChanged( 0 );
113     }
114
115     /* Update text */
116     QString text;
117     if( p_input->input.p_item->p_meta &&
118         p_input->input.p_item->p_meta->psz_nowplaying &&
119         *p_input->input.p_item->p_meta->psz_nowplaying )
120     {
121         text.sprintf( "%s - %s",
122                   p_input->input.p_item->p_meta->psz_nowplaying,
123                   p_input->input.p_item->psz_name );
124     }
125     else
126     {
127         text.sprintf( "%s", p_input->input.p_item->psz_name );
128     }
129     emit nameChanged( text );
130
131     /* Update playing status */
132     var_Get( p_input, "state", &val );
133     val.i_int = val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S;
134     if( i_old_playing_status != val.i_int )
135     {
136         i_old_playing_status = val.i_int;
137         emit statusChanged(  val.i_int == PAUSE_S ? PAUSE_S : PLAYING_S );
138     }
139 }
140
141 void InputManager::sliderUpdate( float new_pos )
142 {
143     if( p_input && !p_input->b_die && !p_input->b_dead )
144         var_SetFloat( p_input, "position", new_pos );
145 }
146
147 void InputManager::togglePlayPause()
148 {
149     vlc_value_t state;
150     var_Get( p_input, "state", &state );
151     if( state.i_int != PAUSE_S )
152     {
153         /* A stream is being played, pause it */
154         state.i_int = PAUSE_S;
155     }
156     else
157     {
158         /* Stream is paused, resume it */
159         state.i_int = PLAYING_S;
160     }
161     var_Set( p_input, "state", state );
162     emit statusChanged( state.i_int );
163 }
164
165 /**********************************************************************
166  * MainInputManager implementation. Wrap an input manager and
167  * take care of updating the main playlist input
168  **********************************************************************/
169 MainInputManager * MainInputManager::instance = NULL;
170
171 MainInputManager::MainInputManager( intf_thread_t *_p_intf ) : QObject(NULL),
172                                                 p_intf( _p_intf )
173 {
174     p_input = NULL;
175     im = new InputManager( this, p_intf );
176     /* Get timer updates */
177     connect( DialogsProvider::getInstance(p_intf)->fixed_timer,
178              SIGNAL(timeout() ), this, SLOT( updateInput() ) );
179     /* Warn our embedded IM about input changes */
180     connect( this, SIGNAL( inputChanged( input_thread_t * ) ),
181              im, SLOT( setInput( input_thread_t * ) ) );
182 }
183
184 MainInputManager::~MainInputManager()
185 {
186     if( p_input ) vlc_object_release( p_input );
187 }
188
189 void MainInputManager::updateInput()
190 {
191     vlc_mutex_lock( &p_intf->change_lock );
192     if( p_input && p_input->b_dead )
193     {
194         vlc_object_release( p_input );
195         getIM()->delInput();
196         p_input = NULL;
197         emit inputChanged( NULL );
198     }
199
200     if( !p_input )
201     {
202         playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_intf,
203                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
204         assert( p_playlist );
205         PL_LOCK;
206         p_input = p_playlist->p_input;
207         if( p_input )
208         {
209             vlc_object_yield( p_input );
210             emit inputChanged( p_input );
211         }
212         PL_UNLOCK;
213         vlc_object_release( p_playlist );
214     }
215     vlc_mutex_unlock( &p_intf->change_lock );
216 }
217
218 void MainInputManager::togglePlayPause()
219 {
220     if( p_input == NULL )
221     {
222         playlist_Play( THEPL );
223         return;
224     }
225     getIM()->togglePlayPause();
226 }
227
228
229 static int ChangeAudio( vlc_object_t *p_this, const char *var, vlc_value_t o,
230                         vlc_value_t n, void *param )
231 {
232     InputManager *im = (InputManager*)param;
233     im->b_has_audio = true;
234 }
235 static int ChangeVideo( vlc_object_t *p_this, const char *var, vlc_value_t o,
236                         vlc_value_t n, void *param )
237 {
238     InputManager *im = (InputManager*)param;
239     im->b_has_video = true;
240 }