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