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_resize.hpp"
39 #include "../commands/cmd_vars.hpp"
40 #include "../utils/var_bool.hpp"
43 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
45 if( pIntf->p_sys->p_vlcProc == NULL )
47 pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
50 return pIntf->p_sys->p_vlcProc;
54 void VlcProc::destroy( intf_thread_t *pIntf )
56 if( pIntf->p_sys->p_vlcProc )
58 delete pIntf->p_sys->p_vlcProc;
59 pIntf->p_sys->p_vlcProc = NULL;
64 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ), m_pVout( NULL ),
67 // Create a timer to poll the status of the vlc
68 OSFactory *pOsFactory = OSFactory::instance( pIntf );
69 m_pTimer = pOsFactory->createOSTimer( m_cmdManage );
70 m_pTimer->start( 100, false );
72 // Create and register VLC variables
73 VarManager *pVarManager = VarManager::instance( getIntf() );
75 #define REGISTER_VAR( var, type, name ) \
76 var = VariablePtr( new type( getIntf() ) ); \
77 pVarManager->registerVar( var, name );
78 REGISTER_VAR( m_cPlaylist, Playlist, "playlist" )
79 pVarManager->registerVar( getPlaylistVar().getPositionVarPtr(),
81 REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
82 REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
83 REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
84 REGISTER_VAR( m_cPlaytree, Playtree, "playtree" )
85 pVarManager->registerVar( getPlaytreeVar().getPositionVarPtr(),
87 pVarManager->registerVar( m_cVarRandom, "playtree.isRandom" );
88 pVarManager->registerVar( m_cVarLoop, "playtree.isLoop" );
89 pVarManager->registerVar( m_cVarRepeat, "playtree.isRepeat" );
90 REGISTER_VAR( m_cVarTime, StreamTime, "time" )
91 REGISTER_VAR( m_cVarVolume, Volume, "volume" )
92 REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
93 REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
94 REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
95 REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
96 REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
98 m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
99 pVarManager->registerVar( m_cVarStreamName, "streamName" );
100 m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
101 pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
104 // The object variable callbacks are called from other VLC threads,
105 // so they must put commands in the queue and NOT do anything else
106 // (X11 calls are not reentrant)
108 // Called when the playlist changes
109 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
110 onIntfChange, this );
111 // Called when a playlist item is added
112 // TODO: properly handle item-append
113 var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
114 onIntfChange, this );
115 // Called when a playlist item is deleted
116 // TODO: properly handle item-deleted
117 var_AddCallback( pIntf->p_sys->p_playlist, "item-deleted",
118 onIntfChange, this );
119 // Called when the "interface shower" wants us to show the skin
120 var_AddCallback( pIntf->p_sys->p_playlist, "intf-show",
122 // Called when the current played item changes
123 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
124 onPlaylistChange, this );
125 // Called when a playlist item changed
126 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
127 onItemChange, this );
128 // Called when our skins2 demux wants us to load a new skin
129 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
131 // Callbacks for vout requests
132 getIntf()->pf_request_window = &getWindow;
133 getIntf()->pf_release_window = &releaseWindow;
134 getIntf()->pf_control_window = &controlWindow;
136 getIntf()->p_sys->p_input = NULL;
144 if( getIntf()->p_sys->p_input )
146 vlc_object_release( getIntf()->p_sys->p_input );
149 // Callbacks for vout requests
150 getIntf()->pf_request_window = NULL;
151 getIntf()->pf_release_window = NULL;
152 getIntf()->pf_control_window = NULL;
154 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
155 onIntfChange, this );
156 var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
157 onIntfChange, this );
158 var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
159 onIntfChange, this );
160 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-show",
162 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
163 onPlaylistChange, this );
164 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
165 onItemChange, this );
166 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
170 void VlcProc::registerVoutWindow( void *pVoutWindow )
172 m_handleSet.insert( pVoutWindow );
173 // Reparent the vout window
176 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
177 vout_Control( m_pVout, VOUT_CLOSE );
182 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
184 m_handleSet.erase( pVoutWindow );
188 void VlcProc::dropVout()
192 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
193 vout_Control( m_pVout, VOUT_CLOSE );
199 void VlcProc::manage()
201 // Did the user requested to quit vlc ?
202 if( getIntf()->b_die || getIntf()->p_vlc->b_die )
204 CmdQuit *pCmd = new CmdQuit( getIntf() );
205 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
206 pQueue->push( CmdGenericPtr( pCmd ) );
209 // Get the VLC variables
210 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
211 Volume *pVolume = (Volume*)m_cVarVolume.get();
212 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
213 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
214 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
215 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
216 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
217 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
218 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
219 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
221 // Refresh sound volume
222 audio_volume_t volume;
223 aout_VolumeGet( getIntf(), &volume );
224 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
225 // Set the mute variable
226 pVarMute->set( volume == 0 );
229 if( getIntf()->p_sys->p_input == NULL )
231 getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
232 getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
234 else if( getIntf()->p_sys->p_input->b_dead )
236 vlc_object_release( getIntf()->p_sys->p_input );
237 getIntf()->p_sys->p_input = NULL;
240 input_thread_t *pInput = getIntf()->p_sys->p_input;
242 if( pInput && !pInput->b_die )
244 // Refresh time variables
246 var_Get( pInput, "position", &pos );
247 pTime->set( pos.f_float, false );
249 // Get the status of the playlist
250 playlist_status_t status =
251 getIntf()->p_sys->p_playlist->status.i_status;
253 pVarPlaying->set( status == PLAYLIST_RUNNING );
254 pVarStopped->set( status == PLAYLIST_STOPPED );
255 pVarPaused->set( status == PLAYLIST_PAUSED );
257 pVarSeekable->set( pos.f_float != 0.0 );
261 pVarPlaying->set( false );
262 pVarPaused->set( false );
263 pVarStopped->set( true );
264 pVarSeekable->set( false );
265 pTime->set( 0, false );
268 // Refresh the random variable
270 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
271 pVarRandom->set( val.b_bool != 0 );
273 // Refresh the loop variable
274 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
275 pVarLoop->set( val.b_bool != 0 );
277 // Refresh the repeat variable
278 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
279 pVarRepeat->set( val.b_bool != 0 );
283 void VlcProc::CmdManage::execute()
285 // Just forward to VlcProc
290 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
291 vlc_value_t oldVal, vlc_value_t newVal,
294 VlcProc *pThis = (VlcProc*)pParam;
296 // Update the stream variable
297 playlist_t *p_playlist = (playlist_t*)pObj;
298 pThis->updateStreamName(p_playlist);
300 // Create a playlist notify command
301 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
302 // Create a playtree notify command
303 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
305 // Push the command in the asynchronous command queue
306 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
307 pQueue->remove( "notify playlist" );
308 pQueue->remove( "playtree changed" );
309 pQueue->push( CmdGenericPtr( pCmd ) );
310 pQueue->push( CmdGenericPtr( pCmdTree ) );
316 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
317 vlc_value_t oldVal, vlc_value_t newVal,
322 VlcProc *pThis = (VlcProc*)pParam;
324 // Create a raise all command
325 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
326 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
328 // Push the command in the asynchronous command queue
329 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
330 pQueue->remove( "raise all windows" );
331 pQueue->push( CmdGenericPtr( pCmd ) );
338 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
339 vlc_value_t oldVal, vlc_value_t newVal,
342 VlcProc *pThis = (VlcProc*)pParam;
344 // Update the stream variable
345 playlist_t *p_playlist = (playlist_t*)pObj;
346 pThis->updateStreamName(p_playlist);
348 // Create a playlist notify command
349 // TODO: selective update
350 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
351 // Create a playtree notify command
352 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
355 // Push the command in the asynchronous command queue
356 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
357 pQueue->remove( "notify playlist" );
358 pQueue->remove( "playtree update" );
359 pQueue->push( CmdGenericPtr( pCmd ) );
360 pQueue->push( CmdGenericPtr( pCmdTree ) );
366 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
367 vlc_value_t oldVal, vlc_value_t newVal,
370 VlcProc *pThis = (VlcProc*)pParam;
372 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
374 // Update the stream variable
375 playlist_t *p_playlist = (playlist_t*)pObj;
376 pThis->updateStreamName(p_playlist);
378 // Create a playlist notify command
379 // TODO: selective update
380 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
381 // Create a playtree notify command
382 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
384 // Push the command in the asynchronous command queue
385 pQueue->remove( "notify playlist" );
386 pQueue->remove( "playtree changed" );
387 pQueue->push( CmdGenericPtr( pCmd ) );
388 pQueue->push( CmdGenericPtr( pCmdTree ) );
394 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
395 vlc_value_t oldVal, vlc_value_t newVal,
398 VlcProc *pThis = (VlcProc*)pParam;
400 // Create a playlist notify command
401 CmdChangeSkin *pCmd =
402 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
404 // Push the command in the asynchronous command queue
405 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
406 pQueue->remove( "change skin" );
407 pQueue->push( CmdGenericPtr( pCmd ) );
413 void VlcProc::updateStreamName( playlist_t *p_playlist )
415 if( p_playlist->p_input )
417 VarText &rStreamName = getStreamNameVar();
418 VarText &rStreamURI = getStreamURIVar();
419 // XXX: we should not need to access p_input->psz_source directly, a
420 // getter should be provided by VLC core
421 string name = p_playlist->p_input->input.p_item->psz_name;
422 // XXX: This should be done in VLC core, not here...
423 // Remove path information if any
424 OSFactory *pFactory = OSFactory::instance( getIntf() );
425 string::size_type pos = name.rfind( pFactory->getDirSeparator() );
426 if( pos != string::npos )
428 name = name.substr( pos + 1, name.size() - pos + 1 );
430 UString srcName( getIntf(), name.c_str() );
431 UString srcURI( getIntf(),
432 p_playlist->p_input->input.p_item->psz_uri );
434 // Create commands to update the stream variables
435 CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
436 CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
437 // Push the commands in the asynchronous command queue
438 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
439 pQueue->push( CmdGenericPtr( pCmd1 ) );
440 pQueue->push( CmdGenericPtr( pCmd2 ) );
445 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
446 int *pXHint, int *pYHint,
447 unsigned int *pWidthHint,
448 unsigned int *pHeightHint )
450 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
451 pThis->m_pVout = pVout;
452 if( pThis->m_handleSet.empty() )
458 // Get the window handle
459 void *pWindow = *pThis->m_handleSet.begin();
460 // Post a resize vout command
461 CmdResizeVout *pCmd = new CmdResizeVout( pThis->getIntf(), pWindow,
462 *pWidthHint, *pHeightHint );
463 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
464 pQueue->remove( "resize vout" );
465 pQueue->push( CmdGenericPtr( pCmd ) );
471 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
473 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
474 pThis->m_pVout = NULL;
478 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
479 int query, va_list args )
481 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
489 // Post a resize vout command
490 CmdResizeVout *pCmd =
491 new CmdResizeVout( pThis->getIntf(), pWindow,
492 pThis->m_pVout->i_window_width,
493 pThis->m_pVout->i_window_height );
494 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
495 pQueue->remove( "resize vout" );
496 pQueue->push( CmdGenericPtr( pCmd ) );
501 msg_Dbg( pIntf, "control query not supported" );