1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teuli�e <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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 "../commands/cmd_dialogs.hpp"
42 #include "../utils/var_bool.hpp"
46 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
48 if( pIntf->p_sys->p_vlcProc == NULL )
50 pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
53 return pIntf->p_sys->p_vlcProc;
57 void VlcProc::destroy( intf_thread_t *pIntf )
59 if( pIntf->p_sys->p_vlcProc )
61 delete pIntf->p_sys->p_vlcProc;
62 pIntf->p_sys->p_vlcProc = NULL;
67 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ),
68 m_varVoutSize( pIntf ), m_varEqBands( pIntf ),
69 m_pVout( NULL ), m_pAout( NULL ), m_cmdManage( this )
71 // Create a timer to poll the status of the vlc
72 OSFactory *pOsFactory = OSFactory::instance( pIntf );
73 m_pTimer = pOsFactory->createOSTimer( m_cmdManage );
74 m_pTimer->start( 100, false );
76 // Create and register VLC variables
77 VarManager *pVarManager = VarManager::instance( getIntf() );
79 #define REGISTER_VAR( var, type, name ) \
80 var = VariablePtr( new type( getIntf() ) ); \
81 pVarManager->registerVar( var, name );
82 REGISTER_VAR( m_cPlaylist, Playlist, "playlist" )
83 pVarManager->registerVar( getPlaylistVar().getPositionVarPtr(),
85 REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
86 REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
87 REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
88 REGISTER_VAR( m_cPlaytree, Playtree, "playtree" )
89 pVarManager->registerVar( getPlaytreeVar().getPositionVarPtr(),
91 pVarManager->registerVar( m_cVarRandom, "playtree.isRandom" );
92 pVarManager->registerVar( m_cVarLoop, "playtree.isLoop" );
93 pVarManager->registerVar( m_cVarRepeat, "playtree.isRepeat" );
94 REGISTER_VAR( m_cVarTime, StreamTime, "time" )
95 REGISTER_VAR( m_cVarVolume, Volume, "volume" )
96 REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
97 REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
98 REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
99 REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
100 REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
101 REGISTER_VAR( m_cVarEqualizer, VarBoolImpl, "equalizer.isEnabled" )
102 REGISTER_VAR( m_cVarEqPreamp, EqualizerPreamp, "equalizer.preamp" )
104 m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
105 pVarManager->registerVar( m_cVarStreamName, "streamName" );
106 m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
107 pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
109 // Register the equalizer bands
110 for( int i = 0; i < EqualizerBands::kNbBands; i++)
113 ss << "equalizer.band(" << i << ")";
114 pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
118 // The object variable callbacks are called from other VLC threads,
119 // so they must put commands in the queue and NOT do anything else
120 // (X11 calls are not reentrant)
122 // Called when the playlist changes
123 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
124 onIntfChange, this );
125 // Called when a playlist item is added
126 var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
127 onItemAppend, this );
128 // Called when a playlist item is deleted
129 // TODO: properly handle item-deleted
130 var_AddCallback( pIntf->p_sys->p_playlist, "item-deleted",
131 onItemDelete, this );
132 // Called when the "interface shower" wants us to show the skin
133 var_AddCallback( pIntf->p_sys->p_playlist, "intf-show",
135 // Called when the current played item changes
136 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
137 onPlaylistChange, this );
138 // Called when a playlist item changed
139 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
140 onItemChange, this );
141 // Called when our skins2 demux wants us to load a new skin
142 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
144 // Called when we have an interaction dialog to display
145 var_Create( pIntf, "interaction", VLC_VAR_ADDRESS );
146 var_AddCallback( pIntf, "interaction", onInteraction, this );
147 pIntf->b_interaction = VLC_TRUE;
149 // Callbacks for vout requests
150 getIntf()->pf_request_window = &getWindow;
151 getIntf()->pf_release_window = &releaseWindow;
152 getIntf()->pf_control_window = &controlWindow;
154 getIntf()->p_sys->p_input = NULL;
162 if( getIntf()->p_sys->p_input )
164 vlc_object_release( getIntf()->p_sys->p_input );
167 // Callbacks for vout requests
168 getIntf()->pf_request_window = NULL;
169 getIntf()->pf_release_window = NULL;
170 getIntf()->pf_control_window = NULL;
172 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
173 onIntfChange, this );
174 var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
175 onItemAppend, this );
176 var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
177 onItemDelete, this );
178 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-show",
180 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
181 onPlaylistChange, this );
182 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
183 onItemChange, this );
184 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
188 void VlcProc::registerVoutWindow( void *pVoutWindow )
190 m_handleSet.insert( pVoutWindow );
191 // Reparent the vout window
194 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
195 vout_Control( m_pVout, VOUT_CLOSE );
200 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
202 m_handleSet.erase( pVoutWindow );
206 void VlcProc::dropVout()
210 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
211 vout_Control( m_pVout, VOUT_CLOSE );
217 void VlcProc::manage()
219 // Did the user requested to quit vlc ?
220 if( getIntf()->b_die || getIntf()->p_vlc->b_die )
222 CmdQuit *pCmd = new CmdQuit( getIntf() );
223 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
224 pQueue->push( CmdGenericPtr( pCmd ) );
227 // Get the VLC variables
228 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
229 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
230 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
231 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
232 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
233 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
234 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
235 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
237 // Refresh audio variables
241 if( getIntf()->p_sys->p_input == NULL )
243 getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
244 getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
246 else if( getIntf()->p_sys->p_input->b_dead )
248 vlc_object_release( getIntf()->p_sys->p_input );
249 getIntf()->p_sys->p_input = NULL;
252 input_thread_t *pInput = getIntf()->p_sys->p_input;
254 if( pInput && !pInput->b_die )
256 // Refresh time variables
258 var_Get( pInput, "position", &pos );
259 pTime->set( pos.f_float, false );
261 // Get the status of the playlist
262 playlist_status_t status =
263 getIntf()->p_sys->p_playlist->status.i_status;
265 pVarPlaying->set( status == PLAYLIST_RUNNING );
266 pVarStopped->set( status == PLAYLIST_STOPPED );
267 pVarPaused->set( status == PLAYLIST_PAUSED );
269 pVarSeekable->set( pos.f_float != 0.0 );
273 pVarPlaying->set( false );
274 pVarPaused->set( false );
275 pVarStopped->set( true );
276 pVarSeekable->set( false );
277 pTime->set( 0, false );
280 // Refresh the random variable
282 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
283 pVarRandom->set( val.b_bool != 0 );
285 // Refresh the loop variable
286 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
287 pVarLoop->set( val.b_bool != 0 );
289 // Refresh the repeat variable
290 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
291 pVarRepeat->set( val.b_bool != 0 );
295 void VlcProc::CmdManage::execute()
297 // Just forward to VlcProc
302 void VlcProc::refreshAudio()
304 char *pFilters = NULL;
306 // Check if the audio output has changed
307 aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
308 VLC_OBJECT_AOUT, FIND_ANYWHERE );
311 if( pAout != m_pAout )
313 // Register the equalizer callbacks
314 if( !var_AddCallback( pAout, "equalizer-bands",
315 onEqBandsChange, this ) &&
316 !var_AddCallback( pAout, "equalizer-preamp",
317 onEqPreampChange, this ) )
320 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
323 // Get the audio filters
324 pFilters = var_GetString( pAout, "audio-filter" );
325 vlc_object_release( pAout );
329 // Get the audio filters
330 pFilters = config_GetPsz( getIntf(), "audio-filter" );
333 // Refresh sound volume
334 audio_volume_t volume;
335 aout_VolumeGet( getIntf(), &volume );
336 Volume *pVolume = (Volume*)m_cVarVolume.get();
337 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
339 // Set the mute variable
340 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
341 pVarMute->set( volume == 0 );
343 // Refresh the equalizer variable
344 VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
345 pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
349 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
350 vlc_value_t oldVal, vlc_value_t newVal,
353 VlcProc *pThis = (VlcProc*)pParam;
355 // Update the stream variable
356 playlist_t *p_playlist = (playlist_t*)pObj;
357 pThis->updateStreamName(p_playlist);
359 // Create a playlist notify command (for old style playlist)
360 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
361 // Create a playtree notify command (for new style playtree)
362 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
364 // Push the command in the asynchronous command queue
365 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
366 pQueue->push( CmdGenericPtr( pCmd ) );
367 pQueue->push( CmdGenericPtr( pCmdTree ) );
373 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
374 vlc_value_t oldVal, vlc_value_t newVal,
379 VlcProc *pThis = (VlcProc*)pParam;
381 // Create a raise all command
382 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
383 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
385 // Push the command in the asynchronous command queue
386 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
387 pQueue->push( CmdGenericPtr( pCmd ) );
394 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
395 vlc_value_t oldVal, vlc_value_t newVal,
398 VlcProc *pThis = (VlcProc*)pParam;
400 // Update the stream variable
401 playlist_t *p_playlist = (playlist_t*)pObj;
402 pThis->updateStreamName(p_playlist);
404 // Create a playlist notify command
405 // TODO: selective update
406 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
407 // Create a playtree notify command
408 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
411 // Push the command in the asynchronous command queue
412 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
413 pQueue->push( CmdGenericPtr( pCmd ) );
414 pQueue->push( CmdGenericPtr( pCmdTree ), true );
419 int VlcProc::onItemAppend( vlc_object_t *pObj, const char *pVariable,
420 vlc_value_t oldVal, vlc_value_t newVal,
423 VlcProc *pThis = (VlcProc*)pParam;
425 playlist_add_t *p_add = (playlist_add_t*)malloc( sizeof(
428 memcpy( p_add, newVal.p_address, sizeof( playlist_add_t ) ) ;
430 CmdGenericPtr ptrTree;
431 CmdPlaytreeAppend *pCmdTree = new CmdPlaytreeAppend( pThis->getIntf(),
433 ptrTree = CmdGenericPtr( pCmdTree );
435 // Create a playlist notify command (for old style playlist)
436 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
438 // Push the command in the asynchronous command queue
439 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
440 pQueue->push( CmdGenericPtr( pCmd ) );
441 pQueue->push( ptrTree , false );
446 int VlcProc::onItemDelete( vlc_object_t *pObj, const char *pVariable,
447 vlc_value_t oldVal, vlc_value_t newVal,
450 VlcProc *pThis = (VlcProc*)pParam;
452 int i_id = newVal.i_int;
454 CmdGenericPtr ptrTree;
455 CmdPlaytreeDelete *pCmdTree = new CmdPlaytreeDelete( pThis->getIntf(),
457 ptrTree = CmdGenericPtr( pCmdTree );
459 // Create a playlist notify command (for old style playlist)
460 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
462 // Push the command in the asynchronous command queue
463 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
464 pQueue->push( CmdGenericPtr( pCmd ) );
465 pQueue->push( ptrTree , false );
473 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
474 vlc_value_t oldVal, vlc_value_t newVal,
477 VlcProc *pThis = (VlcProc*)pParam;
479 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
481 // Update the stream variable
482 playlist_t *p_playlist = (playlist_t*)pObj;
483 pThis->updateStreamName(p_playlist);
485 // Create a playlist notify command (old style playlist)
486 // TODO: selective update
487 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
488 pQueue->push( CmdGenericPtr( pCmd ) );
489 // Create two playtree notify commands: one for old item, one for new
490 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
492 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
493 pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(), newVal.i_int );
494 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
500 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
501 vlc_value_t oldVal, vlc_value_t newVal,
504 VlcProc *pThis = (VlcProc*)pParam;
506 // Create a playlist notify command
507 CmdChangeSkin *pCmd =
508 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
510 // Push the command in the asynchronous command queue
511 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
512 pQueue->push( CmdGenericPtr( pCmd ) );
517 int VlcProc::onInteraction( vlc_object_t *pObj, const char *pVariable,
518 vlc_value_t oldVal, vlc_value_t newVal,
521 VlcProc *pThis = (VlcProc*)pParam;
522 interaction_dialog_t *p_dialog = (interaction_dialog_t *)(newVal.p_address);
524 CmdInteraction *pCmd = new CmdInteraction( pThis->getIntf(), p_dialog );
525 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
526 pQueue->push( CmdGenericPtr( pCmd ) );
531 void VlcProc::updateStreamName( playlist_t *p_playlist )
533 if( p_playlist->p_input )
535 VarText &rStreamName = getStreamNameVar();
536 VarText &rStreamURI = getStreamURIVar();
537 // XXX: we should not need to access p_input->psz_source directly, a
538 // getter should be provided by VLC core
539 string name = p_playlist->p_input->input.p_item->psz_name;
540 // XXX: This should be done in VLC core, not here...
541 // Remove path information if any
542 OSFactory *pFactory = OSFactory::instance( getIntf() );
543 string::size_type pos = name.rfind( pFactory->getDirSeparator() );
544 if( pos != string::npos )
546 name = name.substr( pos + 1, name.size() - pos + 1 );
548 UString srcName( getIntf(), name.c_str() );
549 UString srcURI( getIntf(),
550 p_playlist->p_input->input.p_item->psz_uri );
552 // Create commands to update the stream variables
553 CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
554 CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
555 // Push the commands in the asynchronous command queue
556 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
557 pQueue->push( CmdGenericPtr( pCmd1 ), false );
558 pQueue->push( CmdGenericPtr( pCmd2 ), false );
563 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
564 int *pXHint, int *pYHint,
565 unsigned int *pWidthHint,
566 unsigned int *pHeightHint )
568 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
569 pThis->m_pVout = pVout;
570 if( pThis->m_handleSet.empty() )
576 // Get the window handle
577 void *pWindow = *pThis->m_handleSet.begin();
578 // Post a resize vout command
579 CmdResizeVout *pCmd = new CmdResizeVout( pThis->getIntf(), pWindow,
580 *pWidthHint, *pHeightHint );
581 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
582 pQueue->push( CmdGenericPtr( pCmd ) );
588 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
590 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
591 pThis->m_pVout = NULL;
595 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
596 int query, va_list args )
598 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
606 unsigned int i_width = va_arg( args, unsigned int );
607 unsigned int i_height = va_arg( args, unsigned int );
608 if( !i_width ) i_width = pThis->m_pVout->i_window_width;
609 if( !i_height ) i_height = pThis->m_pVout->i_window_height;
611 // Post a resize vout command
612 CmdResizeVout *pCmd =
613 new CmdResizeVout( pThis->getIntf(), pWindow,
615 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
616 pQueue->push( CmdGenericPtr( pCmd ) );
621 msg_Dbg( pIntf, "control query not supported" );
629 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
630 vlc_value_t oldVal, vlc_value_t newVal,
633 VlcProc *pThis = (VlcProc*)pParam;
635 // Post a set equalizer bands command
636 CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
639 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
640 pQueue->push( CmdGenericPtr( pCmd ) );
646 int VlcProc::onEqPreampChange( vlc_object_t *pObj, const char *pVariable,
647 vlc_value_t oldVal, vlc_value_t newVal,
650 VlcProc *pThis = (VlcProc*)pParam;
651 EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)(pThis->m_cVarEqPreamp.get());
653 // Post a set preamp command
654 CmdSetEqPreamp *pCmd = new CmdSetEqPreamp( pThis->getIntf(), *pVarPreamp,
655 (newVal.f_float + 20.0) / 40.0 );
656 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
657 pQueue->push( CmdGenericPtr( pCmd ) );