]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/vlcproc.cpp
* skins: Reparent the embedded vout window when switching to the wx intf
[vlc] / modules / gui / skins2 / src / vlcproc.cpp
1 /*****************************************************************************
2  * vlcproc.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include <vlc/aout.h>
26 #include <vlc/vout.h>
27
28 #include "vlcproc.hpp"
29 #include "os_factory.hpp"
30 #include "os_timer.hpp"
31 #include "var_manager.hpp"
32 #include "theme.hpp"
33 #include "window_manager.hpp"
34 #include "../commands/async_queue.hpp"
35 #include "../commands/cmd_change_skin.hpp"
36 #include "../commands/cmd_show_window.hpp"
37 #include "../commands/cmd_quit.hpp"
38 #include "../commands/cmd_vars.hpp"
39 #include "../utils/var_bool.hpp"
40
41
42 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
43 {
44     if( pIntf->p_sys->p_vlcProc == NULL )
45     {
46         pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
47     }
48
49     return pIntf->p_sys->p_vlcProc;
50 }
51
52
53 void VlcProc::destroy( intf_thread_t *pIntf )
54 {
55     if( pIntf->p_sys->p_vlcProc )
56     {
57         delete pIntf->p_sys->p_vlcProc;
58         pIntf->p_sys->p_vlcProc = NULL;
59     }
60 }
61
62
63 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ),
64                   m_pVoutWindow( NULL ), m_pVout( NULL )
65 {
66     // Create a timer to poll the status of the vlc
67     OSFactory *pOsFactory = OSFactory::instance( pIntf );
68     m_pTimer = pOsFactory->createOSTimer( Callback( this, &doManage ) );
69     m_pTimer->start( 100, false );
70
71     // Create and register VLC variables
72     VarManager *pVarManager = VarManager::instance( getIntf() );
73
74 #define REGISTER_VAR( var, type, name ) \
75     var = VariablePtr( new type( getIntf() ) ); \
76     pVarManager->registerVar( var, name );
77     REGISTER_VAR( m_cPlaylist, Playlist, "playlist" )
78     pVarManager->registerVar( getPlaylistVar().getPositionVarPtr(),
79                               "playlist.slider" );
80     REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
81     REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
82     REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
83     REGISTER_VAR( m_cVarTime, StreamTime, "time" )
84     REGISTER_VAR( m_cVarVolume, Volume, "volume" )
85     REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
86     REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
87     REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
88     REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
89     REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
90 #undef REGISTER_VAR
91     m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
92     pVarManager->registerVar( m_cVarStreamName, "streamName" );
93     m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
94     pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
95
96     // XXX WARNING XXX
97     // The object variable callbacks are called from other VLC threads,
98     // so they must put commands in the queue and NOT do anything else
99     // (X11 calls are not reentrant)
100
101     // Called when the playlist changes
102     var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
103                      onIntfChange, this );
104     /* FIXME : properly handle item-append*/
105     var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
106                      onIntfChange, this );
107     // Called when the "interface shower" wants us to show the skin
108     var_AddCallback( pIntf->p_sys->p_playlist, "intf-show",
109                      onIntfShow, this );
110     // Called when the current played item changes
111     var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
112                      onPlaylistChange, this );
113     // Called when a playlist item changed
114     var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
115                      onItemChange, this );
116     // Called when our skins2 demux wants us to load a new skin
117     var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
118
119     // Callbacks for vout requests
120     getIntf()->pf_request_window = &getWindow;
121     getIntf()->pf_release_window = &releaseWindow;
122     getIntf()->pf_control_window = &controlWindow;
123
124     getIntf()->p_sys->p_input = NULL;
125 }
126
127
128 VlcProc::~VlcProc()
129 {
130     m_pTimer->stop();
131     delete( m_pTimer );
132     if( getIntf()->p_sys->p_input )
133     {
134         vlc_object_release( getIntf()->p_sys->p_input );
135     }
136
137     // Callbacks for vout requests
138     getIntf()->pf_request_window = NULL;
139     getIntf()->pf_release_window = NULL;
140     getIntf()->pf_control_window = NULL;
141
142     var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
143                      onIntfChange, this );
144     var_DelCallback( getIntf()->p_sys->p_playlist, "intf-show",
145                      onIntfShow, this );
146     var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
147                      onPlaylistChange, this );
148     var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
149                      onItemChange, this );
150     var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
151 }
152
153
154 void VlcProc::setVoutWindow( void *pVoutWindow )
155 {
156     m_pVoutWindow = pVoutWindow;
157     // Reparent the vout window
158     if( m_pVout )
159     {
160         if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
161             vout_Control( m_pVout, VOUT_CLOSE );
162     }
163 }
164
165
166 void VlcProc::dropVout()
167 {
168     if( m_pVout )
169     {
170         if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
171             vout_Control( m_pVout, VOUT_CLOSE );
172         m_pVout = NULL;
173     }
174 }
175
176
177 void VlcProc::manage()
178 {
179     // Did the user requested to quit vlc ?
180     if( getIntf()->b_die || getIntf()->p_vlc->b_die )
181     {
182         CmdQuit *pCmd = new CmdQuit( getIntf() );
183         AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
184         pQueue->push( CmdGenericPtr( pCmd ) );
185     }
186
187     // Get the VLC variables
188     StreamTime *pTime = (StreamTime*)m_cVarTime.get();
189     Volume *pVolume = (Volume*)m_cVarVolume.get();
190     VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
191     VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
192     VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
193     VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
194     VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
195     VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
196     VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
197     VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
198
199     // Refresh sound volume
200     audio_volume_t volume;
201     aout_VolumeGet( getIntf(), &volume );
202     pVolume->set( (double)volume / AOUT_VOLUME_MAX );
203     // Set the mute variable
204     pVarMute->set( volume == 0 );
205
206     // Update the input
207     if( getIntf()->p_sys->p_input == NULL )
208     {
209         getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
210             getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
211     }
212     else if( getIntf()->p_sys->p_input->b_dead )
213     {
214         vlc_object_release( getIntf()->p_sys->p_input );
215         getIntf()->p_sys->p_input = NULL;
216     }
217
218     input_thread_t *pInput = getIntf()->p_sys->p_input;
219
220     if( pInput && !pInput->b_die )
221     {
222         // Refresh time variables
223         vlc_value_t pos;
224         var_Get( pInput, "position", &pos );
225         pTime->set( pos.f_float, false );
226
227         // Get the status of the playlist
228         playlist_status_t status =
229             getIntf()->p_sys->p_playlist->status.i_status;
230
231         pVarPlaying->set( status == PLAYLIST_RUNNING );
232         pVarStopped->set( status == PLAYLIST_STOPPED );
233         pVarPaused->set( status == PLAYLIST_PAUSED );
234
235         pVarSeekable->set( pos.f_float != 0.0 );
236     }
237     else
238     {
239         pVarPlaying->set( false );
240         pVarPaused->set( false );
241         pVarStopped->set( true );
242         pVarSeekable->set( false );
243         pTime->set( 0, false );
244     }
245
246     // Refresh the random variable
247     vlc_value_t val;
248     var_Get( getIntf()->p_sys->p_playlist, "random", &val );
249     pVarRandom->set( val.b_bool );
250
251     // Refresh the loop variable
252     var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
253     pVarLoop->set( val.b_bool );
254
255     // Refresh the repeat variable
256     var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
257     pVarRepeat->set( val.b_bool );
258 }
259
260
261 void VlcProc::doManage( SkinObject *pObj )
262 {
263     VlcProc *pThis = (VlcProc*)pObj;
264     pThis->manage();
265 }
266
267
268 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
269                            vlc_value_t oldVal, vlc_value_t newVal,
270                            void *pParam )
271 {
272     VlcProc *pThis = (VlcProc*)pParam;
273
274     // Update the stream variable
275     playlist_t *p_playlist = (playlist_t*)pObj;
276     pThis->updateStreamName(p_playlist);
277
278     // Create a playlist notify command
279     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
280
281     // Push the command in the asynchronous command queue
282     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
283     pQueue->remove( "notify playlist" );
284     pQueue->push( CmdGenericPtr( pCmd ) );
285
286     return VLC_SUCCESS;
287 }
288
289
290 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
291                          vlc_value_t oldVal, vlc_value_t newVal,
292                          void *pParam )
293 {
294     if (newVal.i_int)
295     {
296         VlcProc *pThis = (VlcProc*)pParam;
297
298         // Create a raise all command
299         CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
300             pThis->getIntf()->p_sys->p_theme->getWindowManager() );
301
302         // Push the command in the asynchronous command queue
303         AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
304         pQueue->remove( "raise all windows" );
305         pQueue->push( CmdGenericPtr( pCmd ) );
306     }
307
308     return VLC_SUCCESS;
309 }
310
311
312 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
313                            vlc_value_t oldVal, vlc_value_t newVal,
314                            void *pParam )
315 {
316     VlcProc *pThis = (VlcProc*)pParam;
317
318     // Update the stream variable
319     playlist_t *p_playlist = (playlist_t*)pObj;
320     pThis->updateStreamName(p_playlist);
321
322     // Create a playlist notify command
323     // TODO: selective update
324     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
325
326     // Push the command in the asynchronous command queue
327     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
328     pQueue->remove( "notify playlist" );
329     pQueue->push( CmdGenericPtr( pCmd ) );
330
331     return VLC_SUCCESS;
332 }
333
334
335 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
336                                vlc_value_t oldVal, vlc_value_t newVal,
337                                void *pParam )
338 {
339     VlcProc *pThis = (VlcProc*)pParam;
340
341     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
342
343     // Update the stream variable
344     playlist_t *p_playlist = (playlist_t*)pObj;
345     pThis->updateStreamName(p_playlist);
346
347     // Create a playlist notify command
348     // TODO: selective update
349     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
350
351     // Push the command in the asynchronous command queue
352     pQueue->remove( "notify playlist" );
353     pQueue->push( CmdGenericPtr( pCmd ) );
354
355     return VLC_SUCCESS;
356 }
357
358
359 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
360                            vlc_value_t oldVal, vlc_value_t newVal,
361                            void *pParam )
362 {
363     VlcProc *pThis = (VlcProc*)pParam;
364
365     // Create a playlist notify command
366     CmdChangeSkin *pCmd =
367         new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
368
369     // Push the command in the asynchronous command queue
370     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
371     pQueue->remove( "change skin" );
372     pQueue->push( CmdGenericPtr( pCmd ) );
373
374     return VLC_SUCCESS;
375 }
376
377
378 void VlcProc::updateStreamName( playlist_t *p_playlist )
379 {
380     if( p_playlist->p_input )
381     {
382         VarText &rStreamName = getStreamNameVar();
383         VarText &rStreamURI = getStreamURIVar();
384         // XXX: we should not need to access p_input->psz_source directly, a
385         // getter should be provided by VLC core
386 #warning "FIXME!"
387         string name = p_playlist->p_input->input.p_item->psz_name;
388         // XXX: This should be done in VLC core, not here...
389         // Remove path information if any
390         OSFactory *pFactory = OSFactory::instance( getIntf() );
391         string::size_type pos = name.rfind( pFactory->getDirSeparator() );
392         if( pos != string::npos )
393         {
394             name = name.substr( pos + 1, name.size() - pos + 1 );
395         }
396         UString srcName( getIntf(), name.c_str() );
397         UString srcURI( getIntf(),
398                          p_playlist->p_input->input.p_item->psz_uri );
399
400         // Create commands to update the stream variables
401         CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
402         CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
403         // Push the commands in the asynchronous command queue
404         AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
405         pQueue->push( CmdGenericPtr( pCmd1 ) );
406         pQueue->push( CmdGenericPtr( pCmd2 ) );
407     }
408 }
409
410
411 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
412                           int *pXHint, int *pYHint,
413                           unsigned int *pWidthHint,
414                           unsigned int *pHeightHint )
415 {
416     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
417     pThis->m_pVout = pVout;
418     return pThis->m_pVoutWindow;
419 }
420
421
422 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
423 {
424     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
425     pThis->m_pVout = NULL;
426 }
427
428
429 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
430                             int query, va_list args )
431 {
432     return VLC_SUCCESS;
433 }
434