1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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 *****************************************************************************/
28 #include "vlcproc.hpp"
29 #include "os_factory.hpp"
30 #include "os_timer.hpp"
31 #include "var_manager.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"
42 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
44 if( pIntf->p_sys->p_vlcProc == NULL )
46 pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
49 return pIntf->p_sys->p_vlcProc;
53 void VlcProc::destroy( intf_thread_t *pIntf )
55 if( pIntf->p_sys->p_vlcProc )
57 delete pIntf->p_sys->p_vlcProc;
58 pIntf->p_sys->p_vlcProc = NULL;
63 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ), m_pVout( NULL ),
66 // Create a timer to poll the status of the vlc
67 OSFactory *pOsFactory = OSFactory::instance( pIntf );
68 m_pTimer = pOsFactory->createOSTimer( m_cmdManage );
69 m_pTimer->start( 100, false );
71 // Create and register VLC variables
72 VarManager *pVarManager = VarManager::instance( getIntf() );
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(),
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_cPlaytree, Playtree, "playtree" )
84 pVarManager->registerVar( getPlaytreeVar().getPositionVarPtr(),
86 pVarManager->registerVar( m_cVarRandom, "playtree.isRandom" );
87 pVarManager->registerVar( m_cVarLoop, "playtree.isLoop" );
88 pVarManager->registerVar( m_cVarRepeat, "playtree.isRepeat" );
89 REGISTER_VAR( m_cVarTime, StreamTime, "time" )
90 REGISTER_VAR( m_cVarVolume, Volume, "volume" )
91 REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
92 REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
93 REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
94 REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
95 REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
97 m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
98 pVarManager->registerVar( m_cVarStreamName, "streamName" );
99 m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
100 pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
103 // The object variable callbacks are called from other VLC threads,
104 // so they must put commands in the queue and NOT do anything else
105 // (X11 calls are not reentrant)
107 // Called when the playlist changes
108 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
109 onIntfChange, this );
110 // Called when a playlist item is added
111 // TODO: properly handle item-append
112 var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
113 onIntfChange, this );
114 // Called when a playlist item is deleted
115 // TODO: properly handle item-deleted
116 var_AddCallback( pIntf->p_sys->p_playlist, "item-deleted",
117 onIntfChange, this );
118 // Called when the "interface shower" wants us to show the skin
119 var_AddCallback( pIntf->p_sys->p_playlist, "intf-show",
121 // Called when the current played item changes
122 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
123 onPlaylistChange, this );
124 // Called when a playlist item changed
125 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
126 onItemChange, this );
127 // Called when our skins2 demux wants us to load a new skin
128 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
130 // Callbacks for vout requests
131 getIntf()->pf_request_window = &getWindow;
132 getIntf()->pf_release_window = &releaseWindow;
133 getIntf()->pf_control_window = &controlWindow;
135 getIntf()->p_sys->p_input = NULL;
143 if( getIntf()->p_sys->p_input )
145 vlc_object_release( getIntf()->p_sys->p_input );
148 // Callbacks for vout requests
149 getIntf()->pf_request_window = NULL;
150 getIntf()->pf_release_window = NULL;
151 getIntf()->pf_control_window = NULL;
153 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
154 onIntfChange, this );
155 var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
156 onIntfChange, this );
157 var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
158 onIntfChange, this );
159 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-show",
161 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
162 onPlaylistChange, this );
163 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
164 onItemChange, this );
165 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
169 void VlcProc::registerVoutWindow( void *pVoutWindow )
171 m_handleSet.insert( pVoutWindow );
172 // Reparent the vout window
175 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
176 vout_Control( m_pVout, VOUT_CLOSE );
181 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
183 m_handleSet.erase( pVoutWindow );
187 void VlcProc::dropVout()
191 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
192 vout_Control( m_pVout, VOUT_CLOSE );
198 void VlcProc::manage()
200 // Did the user requested to quit vlc ?
201 if( getIntf()->b_die || getIntf()->p_vlc->b_die )
203 CmdQuit *pCmd = new CmdQuit( getIntf() );
204 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
205 pQueue->push( CmdGenericPtr( pCmd ) );
208 // Get the VLC variables
209 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
210 Volume *pVolume = (Volume*)m_cVarVolume.get();
211 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
212 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
213 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
214 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
215 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
216 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
217 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
218 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
220 // Refresh sound volume
221 audio_volume_t volume;
222 aout_VolumeGet( getIntf(), &volume );
223 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
224 // Set the mute variable
225 pVarMute->set( volume == 0 );
228 if( getIntf()->p_sys->p_input == NULL )
230 getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
231 getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
233 else if( getIntf()->p_sys->p_input->b_dead )
235 vlc_object_release( getIntf()->p_sys->p_input );
236 getIntf()->p_sys->p_input = NULL;
239 input_thread_t *pInput = getIntf()->p_sys->p_input;
241 if( pInput && !pInput->b_die )
243 // Refresh time variables
245 var_Get( pInput, "position", &pos );
246 pTime->set( pos.f_float, false );
248 // Get the status of the playlist
249 playlist_status_t status =
250 getIntf()->p_sys->p_playlist->status.i_status;
252 pVarPlaying->set( status == PLAYLIST_RUNNING );
253 pVarStopped->set( status == PLAYLIST_STOPPED );
254 pVarPaused->set( status == PLAYLIST_PAUSED );
256 pVarSeekable->set( pos.f_float != 0.0 );
260 pVarPlaying->set( false );
261 pVarPaused->set( false );
262 pVarStopped->set( true );
263 pVarSeekable->set( false );
264 pTime->set( 0, false );
267 // Refresh the random variable
269 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
270 pVarRandom->set( val.b_bool != 0 );
272 // Refresh the loop variable
273 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
274 pVarLoop->set( val.b_bool != 0 );
276 // Refresh the repeat variable
277 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
278 pVarRepeat->set( val.b_bool != 0 );
282 void VlcProc::CmdManage::execute()
284 // Just forward to VlcProc
289 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
290 vlc_value_t oldVal, vlc_value_t newVal,
293 VlcProc *pThis = (VlcProc*)pParam;
295 // Update the stream variable
296 playlist_t *p_playlist = (playlist_t*)pObj;
297 pThis->updateStreamName(p_playlist);
299 // Create a playlist notify command
300 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
301 // Create a playtree notify command
302 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
304 // Push the command in the asynchronous command queue
305 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
306 pQueue->remove( "notify playlist" );
307 pQueue->remove( "playtree changed" );
308 pQueue->push( CmdGenericPtr( pCmd ) );
309 pQueue->push( CmdGenericPtr( pCmdTree ) );
315 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
316 vlc_value_t oldVal, vlc_value_t newVal,
321 VlcProc *pThis = (VlcProc*)pParam;
323 // Create a raise all command
324 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
325 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
327 // Push the command in the asynchronous command queue
328 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
329 pQueue->remove( "raise all windows" );
330 pQueue->push( CmdGenericPtr( pCmd ) );
337 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
338 vlc_value_t oldVal, vlc_value_t newVal,
341 VlcProc *pThis = (VlcProc*)pParam;
343 // Update the stream variable
344 playlist_t *p_playlist = (playlist_t*)pObj;
345 pThis->updateStreamName(p_playlist);
347 // Create a playlist notify command
348 // TODO: selective update
349 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
350 // Create a playtree notify command
351 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
354 // Push the command in the asynchronous command queue
355 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
356 pQueue->remove( "notify playlist" );
357 pQueue->remove( "playtree update" );
358 pQueue->push( CmdGenericPtr( pCmd ) );
359 pQueue->push( CmdGenericPtr( pCmdTree ) );
365 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
366 vlc_value_t oldVal, vlc_value_t newVal,
369 VlcProc *pThis = (VlcProc*)pParam;
371 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
373 // Update the stream variable
374 playlist_t *p_playlist = (playlist_t*)pObj;
375 pThis->updateStreamName(p_playlist);
377 // Create a playlist notify command
378 // TODO: selective update
379 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
380 // Create a playtree notify command
381 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
383 // Push the command in the asynchronous command queue
384 pQueue->remove( "notify playlist" );
385 pQueue->remove( "playtree changed" );
386 pQueue->push( CmdGenericPtr( pCmd ) );
387 pQueue->push( CmdGenericPtr( pCmdTree ) );
393 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
394 vlc_value_t oldVal, vlc_value_t newVal,
397 VlcProc *pThis = (VlcProc*)pParam;
399 // Create a playlist notify command
400 CmdChangeSkin *pCmd =
401 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
403 // Push the command in the asynchronous command queue
404 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
405 pQueue->remove( "change skin" );
406 pQueue->push( CmdGenericPtr( pCmd ) );
412 void VlcProc::updateStreamName( playlist_t *p_playlist )
414 if( p_playlist->p_input )
416 VarText &rStreamName = getStreamNameVar();
417 VarText &rStreamURI = getStreamURIVar();
418 // XXX: we should not need to access p_input->psz_source directly, a
419 // getter should be provided by VLC core
420 string name = p_playlist->p_input->input.p_item->psz_name;
421 // XXX: This should be done in VLC core, not here...
422 // Remove path information if any
423 OSFactory *pFactory = OSFactory::instance( getIntf() );
424 string::size_type pos = name.rfind( pFactory->getDirSeparator() );
425 if( pos != string::npos )
427 name = name.substr( pos + 1, name.size() - pos + 1 );
429 UString srcName( getIntf(), name.c_str() );
430 UString srcURI( getIntf(),
431 p_playlist->p_input->input.p_item->psz_uri );
433 // Create commands to update the stream variables
434 CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
435 CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
436 // Push the commands in the asynchronous command queue
437 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
438 pQueue->push( CmdGenericPtr( pCmd1 ) );
439 pQueue->push( CmdGenericPtr( pCmd2 ) );
444 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
445 int *pXHint, int *pYHint,
446 unsigned int *pWidthHint,
447 unsigned int *pHeightHint )
449 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
450 pThis->m_pVout = pVout;
451 if( pThis->m_handleSet.empty() )
457 return *pThis->m_handleSet.begin();
462 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
464 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
465 pThis->m_pVout = NULL;
469 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
470 int query, va_list args )