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_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" )
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" );
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)
101 // Called when the playlist changes
102 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
103 onIntfChange, this );
104 // Called when a playlist item is added
105 // TODO: properly handle item-append
106 var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
107 onIntfChange, this );
108 // Called when a playlist item is deleted
109 // TODO: properly handle item-deleted
110 var_AddCallback( pIntf->p_sys->p_playlist, "item-deleted",
111 onIntfChange, this );
112 // Called when the "interface shower" wants us to show the skin
113 var_AddCallback( pIntf->p_sys->p_playlist, "intf-show",
115 // Called when the current played item changes
116 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
117 onPlaylistChange, this );
118 // Called when a playlist item changed
119 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
120 onItemChange, this );
121 // Called when our skins2 demux wants us to load a new skin
122 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
124 // Callbacks for vout requests
125 getIntf()->pf_request_window = &getWindow;
126 getIntf()->pf_release_window = &releaseWindow;
127 getIntf()->pf_control_window = &controlWindow;
129 getIntf()->p_sys->p_input = NULL;
137 if( getIntf()->p_sys->p_input )
139 vlc_object_release( getIntf()->p_sys->p_input );
142 // Callbacks for vout requests
143 getIntf()->pf_request_window = NULL;
144 getIntf()->pf_release_window = NULL;
145 getIntf()->pf_control_window = NULL;
147 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
148 onIntfChange, this );
149 var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
150 onIntfChange, this );
151 var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
152 onIntfChange, this );
153 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-show",
155 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
156 onPlaylistChange, this );
157 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
158 onItemChange, this );
159 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
163 void VlcProc::registerVoutWindow( void *pVoutWindow )
165 m_handleSet.insert( pVoutWindow );
166 // Reparent the vout window
169 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
170 vout_Control( m_pVout, VOUT_CLOSE );
175 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
177 m_handleSet.erase( pVoutWindow );
181 void VlcProc::dropVout()
185 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
186 vout_Control( m_pVout, VOUT_CLOSE );
192 void VlcProc::manage()
194 // Did the user requested to quit vlc ?
195 if( getIntf()->b_die || getIntf()->p_vlc->b_die )
197 CmdQuit *pCmd = new CmdQuit( getIntf() );
198 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
199 pQueue->push( CmdGenericPtr( pCmd ) );
202 // Get the VLC variables
203 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
204 Volume *pVolume = (Volume*)m_cVarVolume.get();
205 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
206 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
207 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
208 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
209 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
210 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
211 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
212 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
214 // Refresh sound volume
215 audio_volume_t volume;
216 aout_VolumeGet( getIntf(), &volume );
217 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
218 // Set the mute variable
219 pVarMute->set( volume == 0 );
222 if( getIntf()->p_sys->p_input == NULL )
224 getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
225 getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
227 else if( getIntf()->p_sys->p_input->b_dead )
229 vlc_object_release( getIntf()->p_sys->p_input );
230 getIntf()->p_sys->p_input = NULL;
233 input_thread_t *pInput = getIntf()->p_sys->p_input;
235 if( pInput && !pInput->b_die )
237 // Refresh time variables
239 var_Get( pInput, "position", &pos );
240 pTime->set( pos.f_float, false );
242 // Get the status of the playlist
243 playlist_status_t status =
244 getIntf()->p_sys->p_playlist->status.i_status;
246 pVarPlaying->set( status == PLAYLIST_RUNNING );
247 pVarStopped->set( status == PLAYLIST_STOPPED );
248 pVarPaused->set( status == PLAYLIST_PAUSED );
250 pVarSeekable->set( pos.f_float != 0.0 );
254 pVarPlaying->set( false );
255 pVarPaused->set( false );
256 pVarStopped->set( true );
257 pVarSeekable->set( false );
258 pTime->set( 0, false );
261 // Refresh the random variable
263 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
264 pVarRandom->set( val.b_bool != 0 );
266 // Refresh the loop variable
267 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
268 pVarLoop->set( val.b_bool != 0 );
270 // Refresh the repeat variable
271 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
272 pVarRepeat->set( val.b_bool != 0 );
276 void VlcProc::CmdManage::execute()
278 // Just forward to VlcProc
283 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
284 vlc_value_t oldVal, vlc_value_t newVal,
287 VlcProc *pThis = (VlcProc*)pParam;
289 // Update the stream variable
290 playlist_t *p_playlist = (playlist_t*)pObj;
291 pThis->updateStreamName(p_playlist);
293 // Create a playlist notify command
294 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
296 // Push the command in the asynchronous command queue
297 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
298 pQueue->remove( "notify playlist" );
299 pQueue->push( CmdGenericPtr( pCmd ) );
305 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
306 vlc_value_t oldVal, vlc_value_t newVal,
311 VlcProc *pThis = (VlcProc*)pParam;
313 // Create a raise all command
314 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
315 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
317 // Push the command in the asynchronous command queue
318 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
319 pQueue->remove( "raise all windows" );
320 pQueue->push( CmdGenericPtr( pCmd ) );
327 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
328 vlc_value_t oldVal, vlc_value_t newVal,
331 VlcProc *pThis = (VlcProc*)pParam;
333 // Update the stream variable
334 playlist_t *p_playlist = (playlist_t*)pObj;
335 pThis->updateStreamName(p_playlist);
337 // Create a playlist notify command
338 // TODO: selective update
339 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
341 // Push the command in the asynchronous command queue
342 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
343 pQueue->remove( "notify playlist" );
344 pQueue->push( CmdGenericPtr( pCmd ) );
350 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
351 vlc_value_t oldVal, vlc_value_t newVal,
354 VlcProc *pThis = (VlcProc*)pParam;
356 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
358 // Update the stream variable
359 playlist_t *p_playlist = (playlist_t*)pObj;
360 pThis->updateStreamName(p_playlist);
362 // Create a playlist notify command
363 // TODO: selective update
364 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
366 // Push the command in the asynchronous command queue
367 pQueue->remove( "notify playlist" );
368 pQueue->push( CmdGenericPtr( pCmd ) );
374 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
375 vlc_value_t oldVal, vlc_value_t newVal,
378 VlcProc *pThis = (VlcProc*)pParam;
380 // Create a playlist notify command
381 CmdChangeSkin *pCmd =
382 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
384 // Push the command in the asynchronous command queue
385 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
386 pQueue->remove( "change skin" );
387 pQueue->push( CmdGenericPtr( pCmd ) );
393 void VlcProc::updateStreamName( playlist_t *p_playlist )
395 if( p_playlist->p_input )
397 VarText &rStreamName = getStreamNameVar();
398 VarText &rStreamURI = getStreamURIVar();
399 // XXX: we should not need to access p_input->psz_source directly, a
400 // getter should be provided by VLC core
401 string name = p_playlist->p_input->input.p_item->psz_name;
402 // XXX: This should be done in VLC core, not here...
403 // Remove path information if any
404 OSFactory *pFactory = OSFactory::instance( getIntf() );
405 string::size_type pos = name.rfind( pFactory->getDirSeparator() );
406 if( pos != string::npos )
408 name = name.substr( pos + 1, name.size() - pos + 1 );
410 UString srcName( getIntf(), name.c_str() );
411 UString srcURI( getIntf(),
412 p_playlist->p_input->input.p_item->psz_uri );
414 // Create commands to update the stream variables
415 CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
416 CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
417 // Push the commands in the asynchronous command queue
418 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
419 pQueue->push( CmdGenericPtr( pCmd1 ) );
420 pQueue->push( CmdGenericPtr( pCmd2 ) );
425 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
426 int *pXHint, int *pYHint,
427 unsigned int *pWidthHint,
428 unsigned int *pHeightHint )
430 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
431 pThis->m_pVout = pVout;
432 if( pThis->m_handleSet.empty() )
438 return *pThis->m_handleSet.begin();
443 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
445 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
446 pThis->m_pVout = NULL;
450 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
451 int query, va_list args )