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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
32 #include <vlc_playlist.h>
33 #include <vlc_window.h>
35 #include "vlcproc.hpp"
36 #include "os_factory.hpp"
37 #include "os_timer.hpp"
38 #include "var_manager.hpp"
40 #include "window_manager.hpp"
41 #include "../commands/async_queue.hpp"
42 #include "../commands/cmd_change_skin.hpp"
43 #include "../commands/cmd_show_window.hpp"
44 #include "../commands/cmd_quit.hpp"
45 #include "../commands/cmd_resize.hpp"
46 #include "../commands/cmd_vars.hpp"
47 #include "../commands/cmd_dialogs.hpp"
48 #include "../commands/cmd_update_item.hpp"
49 #include "../utils/var_bool.hpp"
53 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
55 if( pIntf->p_sys->p_vlcProc == NULL )
57 pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
60 return pIntf->p_sys->p_vlcProc;
64 void VlcProc::destroy( intf_thread_t *pIntf )
66 if( pIntf->p_sys->p_vlcProc )
68 delete pIntf->p_sys->p_vlcProc;
69 pIntf->p_sys->p_vlcProc = NULL;
74 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ),
75 m_varVoutSize( pIntf ), m_varEqBands( pIntf ),
76 m_pVout( NULL ), m_pAout( NULL ), m_cmdManage( this )
78 // Create a timer to poll the status of the vlc
79 OSFactory *pOsFactory = OSFactory::instance( pIntf );
80 m_pTimer = pOsFactory->createOSTimer( m_cmdManage );
81 m_pTimer->start( 100, false );
83 // Create and register VLC variables
84 VarManager *pVarManager = VarManager::instance( getIntf() );
86 #define REGISTER_VAR( var, type, name ) \
87 var = VariablePtr( new type( getIntf() ) ); \
88 pVarManager->registerVar( var, name );
89 REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
90 REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
91 REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
92 REGISTER_VAR( m_cPlaytree, Playtree, "playtree" )
93 pVarManager->registerVar( getPlaytreeVar().getPositionVarPtr(),
95 pVarManager->registerVar( m_cVarRandom, "playtree.isRandom" );
96 pVarManager->registerVar( m_cVarLoop, "playtree.isLoop" );
98 REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
99 REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
100 REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
102 /* Input variables */
103 pVarManager->registerVar( m_cVarRepeat, "playtree.isRepeat" );
104 REGISTER_VAR( m_cVarTime, StreamTime, "time" )
105 REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
106 REGISTER_VAR( m_cVarDvdActive, VarBoolImpl, "dvd.isActive" )
109 REGISTER_VAR( m_cVarFullscreen, VarBoolImpl, "vlc.isFullscreen" )
110 REGISTER_VAR( m_cVarHasVout, VarBoolImpl, "vlc.hasVout" )
113 REGISTER_VAR( m_cVarHasAudio, VarBoolImpl, "vlc.hasAudio" )
114 REGISTER_VAR( m_cVarVolume, Volume, "volume" )
115 REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
116 REGISTER_VAR( m_cVarEqualizer, VarBoolImpl, "equalizer.isEnabled" )
117 REGISTER_VAR( m_cVarEqPreamp, EqualizerPreamp, "equalizer.preamp" )
120 m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
121 pVarManager->registerVar( m_cVarStreamName, "streamName" );
122 m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
123 pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
124 m_cVarStreamBitRate = VariablePtr( new VarText( getIntf(), false ) );
125 pVarManager->registerVar( m_cVarStreamBitRate, "bitrate" );
126 m_cVarStreamSampleRate = VariablePtr( new VarText( getIntf(), false ) );
127 pVarManager->registerVar( m_cVarStreamSampleRate, "samplerate" );
129 // Register the equalizer bands
130 for( int i = 0; i < EqualizerBands::kNbBands; i++)
133 ss << "equalizer.band(" << i << ")";
134 pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
138 // The object variable callbacks are called from other VLC threads,
139 // so they must put commands in the queue and NOT do anything else
140 // (X11 calls are not reentrant)
142 // Called when the playlist changes
143 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
144 onIntfChange, this );
145 // Called when a playlist item is added
146 var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
147 onItemAppend, this );
148 // Called when a playlist item is deleted
149 // TODO: properly handle item-deleted
150 var_AddCallback( pIntf->p_sys->p_playlist, "item-deleted",
151 onItemDelete, this );
152 // Called when the "interface shower" wants us to show the skin
153 var_AddCallback( pIntf->p_libvlc, "intf-show",
155 // Called when the current played item changes
156 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
157 onPlaylistChange, this );
158 // Called when a playlist item changed
159 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
160 onItemChange, this );
161 // Called when our skins2 demux wants us to load a new skin
162 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
164 // Called when we have an interaction dialog to display
165 var_Create( pIntf, "interaction", VLC_VAR_ADDRESS );
166 var_AddCallback( pIntf, "interaction", onInteraction, this );
167 interaction_Register( pIntf );
169 getIntf()->p_sys->p_input = NULL;
177 if( getIntf()->p_sys->p_input )
179 vlc_object_release( getIntf()->p_sys->p_input );
182 interaction_Unregister( getIntf() );
184 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
185 onIntfChange, this );
186 var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
187 onItemAppend, this );
188 var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
189 onItemDelete, this );
190 var_DelCallback( getIntf()->p_libvlc, "intf-show",
192 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
193 onPlaylistChange, this );
194 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
195 onItemChange, this );
196 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
200 void VlcProc::registerVoutWindow( void *pVoutWindow )
202 m_handleSet.insert( pVoutWindow );
203 // Reparent the vout window
206 if( vout_Control( m_pVout, VOUT_REPARENT, 0 ) != VLC_SUCCESS )
207 vout_Control( m_pVout, VOUT_CLOSE );
212 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
214 m_handleSet.erase( pVoutWindow );
218 void VlcProc::dropVout()
222 if( vout_Control( m_pVout, VOUT_REPARENT, 0 ) != VLC_SUCCESS )
223 vout_Control( m_pVout, VOUT_CLOSE );
229 void VlcProc::manage()
231 // Did the user request to quit vlc ?
232 if( !vlc_object_alive( getIntf() ) )
234 CmdQuit *pCmd = new CmdQuit( getIntf() );
235 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
236 pQueue->push( CmdGenericPtr( pCmd ) );
243 void VlcProc::CmdManage::execute()
245 // Just forward to VlcProc
249 void VlcProc::refreshAudio()
253 // Check if the audio output has changed
254 aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
255 VLC_OBJECT_AOUT, FIND_ANYWHERE );
258 if( pAout != m_pAout )
260 // Register the equalizer callbacks
261 if( !var_AddCallback( pAout, "equalizer-bands",
262 onEqBandsChange, this ) &&
263 !var_AddCallback( pAout, "equalizer-preamp",
264 onEqPreampChange, this ) )
267 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
270 // Get the audio filters
271 pFilters = var_GetNonEmptyString( pAout, "audio-filter" );
272 vlc_object_release( pAout );
276 // Get the audio filters
277 pFilters = config_GetPsz( getIntf(), "audio-filter" );
280 // Refresh sound volume
281 audio_volume_t volume;
282 aout_VolumeGet( getIntf(), &volume );
283 Volume *pVolume = (Volume*)m_cVarVolume.get();
284 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
286 // Set the mute variable
287 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
288 pVarMute->set( volume == 0 );
290 // Refresh the equalizer variable
291 VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
292 pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
296 void VlcProc::refreshPlaylist()
298 // Refresh the random variable
299 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
301 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
302 pVarRandom->set( val.b_bool != 0 );
304 // Refresh the loop variable
305 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
306 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
307 pVarLoop->set( val.b_bool != 0 );
309 // Refresh the repeat variable
310 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
311 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
312 pVarRepeat->set( val.b_bool != 0 );
315 void VlcProc::refreshInput()
317 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
318 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
319 VarBoolImpl *pVarDvdActive = (VarBoolImpl*)m_cVarDvdActive.get();
320 VarBoolImpl *pVarHasVout = (VarBoolImpl*)m_cVarHasVout.get();
321 VarBoolImpl *pVarHasAudio = (VarBoolImpl*)m_cVarHasAudio.get();
322 VarText *pBitrate = (VarText*)m_cVarStreamBitRate.get();
323 VarText *pSampleRate = (VarText*)m_cVarStreamSampleRate.get();
324 VarBoolImpl *pVarFullscreen = (VarBoolImpl*)m_cVarFullscreen.get();
325 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
326 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
327 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
330 if( getIntf()->p_sys->p_input == NULL )
332 getIntf()->p_sys->p_input =
333 playlist_CurrentInput( getIntf()->p_sys->p_playlist );
335 else if( getIntf()->p_sys->p_input->b_dead )
337 vlc_object_release( getIntf()->p_sys->p_input );
338 getIntf()->p_sys->p_input = NULL;
341 input_thread_t *pInput = getIntf()->p_sys->p_input;
343 if( pInput && vlc_object_alive (pInput) )
345 // Refresh time variables
347 var_Get( pInput, "position", &pos );
348 pTime->set( pos.f_float, false );
349 pVarSeekable->set( pos.f_float != 0.0 );
351 // Refresh DVD detection
352 vlc_value_t chapters_count;
353 var_Change( pInput, "chapter", VLC_VAR_CHOICESCOUNT,
354 &chapters_count, NULL );
355 pVarDvdActive->set( chapters_count.i_int > 0 );
357 // Get the input bitrate
358 int bitrate = var_GetInteger( pInput, "bit-rate" ) / 1000;
359 pBitrate->set( UString::fromInt( getIntf(), bitrate ) );
361 // Get the audio sample rate
362 int sampleRate = var_GetInteger( pInput, "sample-rate" ) / 1000;
363 pSampleRate->set( UString::fromInt( getIntf(), sampleRate ) );
366 vlc_value_t audio_es;
367 var_Change( pInput, "audio-es", VLC_VAR_CHOICESCOUNT,
369 pVarHasAudio->set( audio_es.i_int > 0 );
371 // Refresh fullscreen status
372 vout_thread_t *pVout = (vout_thread_t *)vlc_object_find( pInput,
373 VLC_OBJECT_VOUT, FIND_CHILD );
374 pVarHasVout->set( pVout != NULL );
377 pVarFullscreen->set( pVout->b_fullscreen );
378 vlc_object_release( pVout );
381 // Refresh play/pause status
382 int state = var_GetInteger( pInput, "state" );
383 pVarStopped->set( false );
384 pVarPlaying->set( state != PAUSE_S );
385 pVarPaused->set( state == PAUSE_S );
389 pVarSeekable->set( false );
390 pVarDvdActive->set( false );
391 pTime->set( 0, false );
392 pVarFullscreen->set( false );
393 pVarHasAudio->set( false );
394 pVarHasVout->set( false );
395 pVarStopped->set( true );
396 pVarPlaying->set( false );
397 pVarPaused->set( false );
401 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
402 vlc_value_t oldVal, vlc_value_t newVal,
405 VlcProc *pThis = (VlcProc*)pParam;
407 // Update the stream variable
408 pThis->updateStreamName();
410 // Create a playtree notify command (for new style playtree)
411 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
413 // Push the command in the asynchronous command queue
414 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
415 pQueue->push( CmdGenericPtr( pCmdTree ) );
421 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
422 vlc_value_t oldVal, vlc_value_t newVal,
427 VlcProc *pThis = (VlcProc*)pParam;
429 // Create a raise all command
430 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
431 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
433 // Push the command in the asynchronous command queue
434 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
435 pQueue->push( CmdGenericPtr( pCmd ) );
442 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
443 vlc_value_t oldVal, vlc_value_t newVal,
446 VlcProc *pThis = (VlcProc*)pParam;
448 // Update the stream variable
449 pThis->updateStreamName();
451 // Create a playtree notify command
452 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
455 // Push the command in the asynchronous command queue
456 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
457 pQueue->push( CmdGenericPtr( pCmdTree ), true );
462 int VlcProc::onItemAppend( vlc_object_t *pObj, const char *pVariable,
463 vlc_value_t oldVal, vlc_value_t newVal,
466 VlcProc *pThis = (VlcProc*)pParam;
468 playlist_add_t *p_add = (playlist_add_t*)malloc( sizeof(
471 memcpy( p_add, newVal.p_address, sizeof( playlist_add_t ) ) ;
473 CmdGenericPtr ptrTree;
474 CmdPlaytreeAppend *pCmdTree = new CmdPlaytreeAppend( pThis->getIntf(),
476 ptrTree = CmdGenericPtr( pCmdTree );
478 // Push the command in the asynchronous command queue
479 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
480 pQueue->push( ptrTree , false );
485 int VlcProc::onItemDelete( vlc_object_t *pObj, const char *pVariable,
486 vlc_value_t oldVal, vlc_value_t newVal,
489 VlcProc *pThis = (VlcProc*)pParam;
491 int i_id = newVal.i_int;
493 CmdGenericPtr ptrTree;
494 CmdPlaytreeDelete *pCmdTree = new CmdPlaytreeDelete( pThis->getIntf(),
496 ptrTree = CmdGenericPtr( pCmdTree );
498 // Push the command in the asynchronous command queue
499 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
500 pQueue->push( ptrTree , false );
506 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
507 vlc_value_t oldVal, vlc_value_t newVal,
510 VlcProc *pThis = (VlcProc*)pParam;
512 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
514 // Update the stream variable
515 pThis->updateStreamName();
517 // Create two playtree notify commands: one for old item, one for new
518 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
520 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
521 pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(), newVal.i_int );
522 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
528 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
529 vlc_value_t oldVal, vlc_value_t newVal,
532 VlcProc *pThis = (VlcProc*)pParam;
534 // Create a playlist notify command
535 CmdChangeSkin *pCmd =
536 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
538 // Push the command in the asynchronous command queue
539 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
540 pQueue->push( CmdGenericPtr( pCmd ) );
545 int VlcProc::onInteraction( vlc_object_t *pObj, const char *pVariable,
546 vlc_value_t oldVal, vlc_value_t newVal,
549 VlcProc *pThis = (VlcProc*)pParam;
550 interaction_dialog_t *p_dialog = (interaction_dialog_t *)(newVal.p_address);
552 CmdInteraction *pCmd = new CmdInteraction( pThis->getIntf(), p_dialog );
553 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
554 pQueue->push( CmdGenericPtr( pCmd ) );
559 void VlcProc::updateStreamName()
561 // Create a update item command
562 CmdUpdateItem *pCmdItem = new CmdUpdateItem( getIntf(), getStreamNameVar(), getStreamURIVar() );
564 // Push the command in the asynchronous command queue
565 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
566 pQueue->push( CmdGenericPtr( pCmdItem ) );
569 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
570 int *pXHint, int *pYHint,
571 unsigned int *pWidthHint,
572 unsigned int *pHeightHint )
574 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
575 if( pThis->m_handleSet.empty() )
581 pThis->m_pVout = pVout;
582 // Get the window handle
583 void *pWindow = *pThis->m_handleSet.begin();
584 // Post a resize vout command
585 CmdResizeVout *pCmd = new CmdResizeVout( pThis->getIntf(), pWindow,
586 *pWidthHint, *pHeightHint );
587 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
588 pQueue->push( CmdGenericPtr( pCmd ) );
594 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
596 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
597 pThis->m_pVout = NULL;
601 int VlcProc::controlWindow( struct vout_window_t *pWnd,
602 int query, va_list args )
604 intf_thread_t *pIntf = (intf_thread_t *)pWnd->p_private;
605 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
613 unsigned int i_width = va_arg( args, unsigned int );
614 unsigned int i_height = va_arg( args, unsigned int );
615 if( !i_width ) i_width = pThis->m_pVout->i_window_width;
616 if( !i_height ) i_height = pThis->m_pVout->i_window_height;
618 // Post a resize vout command
619 CmdResizeVout *pCmd =
620 new CmdResizeVout( pThis->getIntf(), pWnd->handle,
622 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
623 pQueue->push( CmdGenericPtr( pCmd ) );
628 msg_Dbg( pWnd, "control query not supported" );
636 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
637 vlc_value_t oldVal, vlc_value_t newVal,
640 VlcProc *pThis = (VlcProc*)pParam;
642 // Post a set equalizer bands command
643 CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
646 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
647 pQueue->push( CmdGenericPtr( pCmd ) );
653 int VlcProc::onEqPreampChange( vlc_object_t *pObj, const char *pVariable,
654 vlc_value_t oldVal, vlc_value_t newVal,
657 VlcProc *pThis = (VlcProc*)pParam;
658 EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)(pThis->m_cVarEqPreamp.get());
660 // Post a set preamp command
661 CmdSetEqPreamp *pCmd = new CmdSetEqPreamp( pThis->getIntf(), *pVarPreamp,
662 (newVal.f_float + 20.0) / 40.0 );
663 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
664 pQueue->push( CmdGenericPtr( pCmd ) );