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 *****************************************************************************/
27 #include <aout_internal.h>
29 #include "vlcproc.hpp"
30 #include "os_factory.hpp"
31 #include "os_timer.hpp"
32 #include "var_manager.hpp"
34 #include "window_manager.hpp"
35 #include "../commands/async_queue.hpp"
36 #include "../commands/cmd_change_skin.hpp"
37 #include "../commands/cmd_show_window.hpp"
38 #include "../commands/cmd_quit.hpp"
39 #include "../commands/cmd_resize.hpp"
40 #include "../commands/cmd_vars.hpp"
41 #include "../utils/var_bool.hpp"
45 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
47 if( pIntf->p_sys->p_vlcProc == NULL )
49 pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
52 return pIntf->p_sys->p_vlcProc;
56 void VlcProc::destroy( intf_thread_t *pIntf )
58 if( pIntf->p_sys->p_vlcProc )
60 delete pIntf->p_sys->p_vlcProc;
61 pIntf->p_sys->p_vlcProc = NULL;
66 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ),
67 m_varVoutSize( pIntf ), m_varEqBands( pIntf ), m_pVout( NULL ),
68 m_pAout( NULL ), m_cmdManage( this )
70 // Create a timer to poll the status of the vlc
71 OSFactory *pOsFactory = OSFactory::instance( pIntf );
72 m_pTimer = pOsFactory->createOSTimer( m_cmdManage );
73 m_pTimer->start( 100, false );
75 // Create and register VLC variables
76 VarManager *pVarManager = VarManager::instance( getIntf() );
78 #define REGISTER_VAR( var, type, name ) \
79 var = VariablePtr( new type( getIntf() ) ); \
80 pVarManager->registerVar( var, name );
81 REGISTER_VAR( m_cPlaylist, Playlist, "playlist" )
82 pVarManager->registerVar( getPlaylistVar().getPositionVarPtr(),
84 REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
85 REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
86 REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
87 REGISTER_VAR( m_cPlaytree, Playtree, "playtree" )
88 pVarManager->registerVar( getPlaytreeVar().getPositionVarPtr(),
90 pVarManager->registerVar( m_cVarRandom, "playtree.isRandom" );
91 pVarManager->registerVar( m_cVarLoop, "playtree.isLoop" );
92 pVarManager->registerVar( m_cVarRepeat, "playtree.isRepeat" );
93 REGISTER_VAR( m_cVarTime, StreamTime, "time" )
94 REGISTER_VAR( m_cVarVolume, Volume, "volume" )
95 REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
96 REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
97 REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
98 REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
99 REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
101 m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
102 pVarManager->registerVar( m_cVarStreamName, "streamName" );
103 m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
104 pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
106 // Register the equalizer bands
107 for( int i = 0; i < EqualizerBands::kNbBands; i++)
110 ss << "equalizer.band(" << i << ")";
111 pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
115 // The object variable callbacks are called from other VLC threads,
116 // so they must put commands in the queue and NOT do anything else
117 // (X11 calls are not reentrant)
119 // Called when the playlist changes
120 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
121 onIntfChange, this );
122 // Called when a playlist item is added
123 // TODO: properly handle item-append
124 var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
125 onIntfChange, this );
126 // Called when a playlist item is deleted
127 // TODO: properly handle item-deleted
128 var_AddCallback( pIntf->p_sys->p_playlist, "item-deleted",
129 onIntfChange, this );
130 // Called when the "interface shower" wants us to show the skin
131 var_AddCallback( pIntf->p_sys->p_playlist, "intf-show",
133 // Called when the current played item changes
134 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
135 onPlaylistChange, this );
136 // Called when a playlist item changed
137 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
138 onItemChange, this );
139 // Called when our skins2 demux wants us to load a new skin
140 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
142 // Callbacks for vout requests
143 getIntf()->pf_request_window = &getWindow;
144 getIntf()->pf_release_window = &releaseWindow;
145 getIntf()->pf_control_window = &controlWindow;
147 getIntf()->p_sys->p_input = NULL;
155 if( getIntf()->p_sys->p_input )
157 vlc_object_release( getIntf()->p_sys->p_input );
160 // Callbacks for vout requests
161 getIntf()->pf_request_window = NULL;
162 getIntf()->pf_release_window = NULL;
163 getIntf()->pf_control_window = NULL;
165 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
166 onIntfChange, this );
167 var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
168 onIntfChange, this );
169 var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
170 onIntfChange, this );
171 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-show",
173 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
174 onPlaylistChange, this );
175 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
176 onItemChange, this );
177 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
181 void VlcProc::registerVoutWindow( void *pVoutWindow )
183 m_handleSet.insert( pVoutWindow );
184 // Reparent the vout window
187 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
188 vout_Control( m_pVout, VOUT_CLOSE );
193 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
195 m_handleSet.erase( pVoutWindow );
199 void VlcProc::dropVout()
203 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
204 vout_Control( m_pVout, VOUT_CLOSE );
210 void VlcProc::manage()
212 // Did the user requested to quit vlc ?
213 if( getIntf()->b_die || getIntf()->p_vlc->b_die )
215 CmdQuit *pCmd = new CmdQuit( getIntf() );
216 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
217 pQueue->push( CmdGenericPtr( pCmd ) );
220 // Get the VLC variables
221 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
222 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
223 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
224 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
225 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
226 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
227 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
228 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
230 // Refresh audio variables
234 if( getIntf()->p_sys->p_input == NULL )
236 getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
237 getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
239 else if( getIntf()->p_sys->p_input->b_dead )
241 vlc_object_release( getIntf()->p_sys->p_input );
242 getIntf()->p_sys->p_input = NULL;
245 input_thread_t *pInput = getIntf()->p_sys->p_input;
247 if( pInput && !pInput->b_die )
249 // Refresh time variables
251 var_Get( pInput, "position", &pos );
252 pTime->set( pos.f_float, false );
254 // Get the status of the playlist
255 playlist_status_t status =
256 getIntf()->p_sys->p_playlist->status.i_status;
258 pVarPlaying->set( status == PLAYLIST_RUNNING );
259 pVarStopped->set( status == PLAYLIST_STOPPED );
260 pVarPaused->set( status == PLAYLIST_PAUSED );
262 pVarSeekable->set( pos.f_float != 0.0 );
266 pVarPlaying->set( false );
267 pVarPaused->set( false );
268 pVarStopped->set( true );
269 pVarSeekable->set( false );
270 pTime->set( 0, false );
273 // Refresh the random variable
275 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
276 pVarRandom->set( val.b_bool != 0 );
278 // Refresh the loop variable
279 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
280 pVarLoop->set( val.b_bool != 0 );
282 // Refresh the repeat variable
283 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
284 pVarRepeat->set( val.b_bool != 0 );
288 void VlcProc::CmdManage::execute()
290 // Just forward to VlcProc
295 void VlcProc::refreshAudio()
297 // Check if the audio output has changed
298 aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
299 VLC_OBJECT_AOUT, FIND_ANYWHERE );
302 if( pAout != m_pAout )
304 // Register the equalizer callback
305 if( !var_AddCallback( pAout, "equalizer-bands",
306 onEqBandsChange, this ) )
310 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
313 vlc_object_release( pAout );
316 // Refresh sound volume
317 audio_volume_t volume;
318 aout_VolumeGet( getIntf(), &volume );
319 Volume *pVolume = (Volume*)m_cVarVolume.get();
320 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
322 // Set the mute variable
323 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
324 pVarMute->set( volume == 0 );
328 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
329 vlc_value_t oldVal, vlc_value_t newVal,
332 VlcProc *pThis = (VlcProc*)pParam;
334 // Update the stream variable
335 playlist_t *p_playlist = (playlist_t*)pObj;
336 pThis->updateStreamName(p_playlist);
338 // Create a playlist notify command
339 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
340 // Create a playtree notify command
341 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
343 // Push the command in the asynchronous command queue
344 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
345 pQueue->push( CmdGenericPtr( pCmd ) );
346 pQueue->push( CmdGenericPtr( pCmdTree ) );
352 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
353 vlc_value_t oldVal, vlc_value_t newVal,
358 VlcProc *pThis = (VlcProc*)pParam;
360 // Create a raise all command
361 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
362 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
364 // Push the command in the asynchronous command queue
365 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
366 pQueue->push( CmdGenericPtr( pCmd ) );
373 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
374 vlc_value_t oldVal, vlc_value_t newVal,
377 VlcProc *pThis = (VlcProc*)pParam;
379 // Update the stream variable
380 playlist_t *p_playlist = (playlist_t*)pObj;
381 pThis->updateStreamName(p_playlist);
383 // Create a playlist notify command
384 // TODO: selective update
385 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
386 // Create a playtree notify command
387 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
390 // Push the command in the asynchronous command queue
391 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
392 pQueue->push( CmdGenericPtr( pCmd ) );
393 pQueue->push( CmdGenericPtr( pCmdTree ) );
399 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
400 vlc_value_t oldVal, vlc_value_t newVal,
403 VlcProc *pThis = (VlcProc*)pParam;
405 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
407 // Update the stream variable
408 playlist_t *p_playlist = (playlist_t*)pObj;
409 pThis->updateStreamName(p_playlist);
411 // Create a playlist notify command
412 // TODO: selective update
413 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
414 // Create a playtree notify command
415 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
417 // Push the command in the asynchronous command queue
418 pQueue->push( CmdGenericPtr( pCmd ) );
419 pQueue->push( CmdGenericPtr( pCmdTree ) );
425 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
426 vlc_value_t oldVal, vlc_value_t newVal,
429 VlcProc *pThis = (VlcProc*)pParam;
431 // Create a playlist notify command
432 CmdChangeSkin *pCmd =
433 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
435 // Push the command in the asynchronous command queue
436 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
437 pQueue->push( CmdGenericPtr( pCmd ) );
443 void VlcProc::updateStreamName( playlist_t *p_playlist )
445 if( p_playlist->p_input )
447 VarText &rStreamName = getStreamNameVar();
448 VarText &rStreamURI = getStreamURIVar();
449 // XXX: we should not need to access p_input->psz_source directly, a
450 // getter should be provided by VLC core
451 string name = p_playlist->p_input->input.p_item->psz_name;
452 // XXX: This should be done in VLC core, not here...
453 // Remove path information if any
454 OSFactory *pFactory = OSFactory::instance( getIntf() );
455 string::size_type pos = name.rfind( pFactory->getDirSeparator() );
456 if( pos != string::npos )
458 name = name.substr( pos + 1, name.size() - pos + 1 );
460 UString srcName( getIntf(), name.c_str() );
461 UString srcURI( getIntf(),
462 p_playlist->p_input->input.p_item->psz_uri );
464 // Create commands to update the stream variables
465 CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
466 CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
467 // Push the commands in the asynchronous command queue
468 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
469 pQueue->push( CmdGenericPtr( pCmd1 ) );
470 pQueue->push( CmdGenericPtr( pCmd2 ) );
475 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
476 int *pXHint, int *pYHint,
477 unsigned int *pWidthHint,
478 unsigned int *pHeightHint )
480 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
481 pThis->m_pVout = pVout;
482 if( pThis->m_handleSet.empty() )
488 // Get the window handle
489 void *pWindow = *pThis->m_handleSet.begin();
490 // Post a resize vout command
491 CmdResizeVout *pCmd = new CmdResizeVout( pThis->getIntf(), pWindow,
492 *pWidthHint, *pHeightHint );
493 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
494 pQueue->push( CmdGenericPtr( pCmd ) );
500 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
502 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
503 pThis->m_pVout = NULL;
507 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
508 int query, va_list args )
510 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
518 unsigned int i_width = va_arg( args, unsigned int );
519 unsigned int i_height = va_arg( args, unsigned int );
520 if( !i_width ) i_width = pThis->m_pVout->i_window_width;
521 if( !i_height ) i_height = pThis->m_pVout->i_window_height;
523 // Post a resize vout command
524 CmdResizeVout *pCmd =
525 new CmdResizeVout( pThis->getIntf(), pWindow,
527 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
528 pQueue->push( CmdGenericPtr( pCmd ) );
533 msg_Dbg( pIntf, "control query not supported" );
541 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
542 vlc_value_t oldVal, vlc_value_t newVal,
545 VlcProc *pThis = (VlcProc*)pParam;
547 // Post a set equalizer bands command
548 CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
551 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
552 pQueue->push( CmdGenericPtr( pCmd ) );