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 pIntf->b_interaction = true;
169 getIntf()->p_sys->p_input = NULL;
177 if( getIntf()->p_sys->p_input )
179 vlc_object_release( getIntf()->p_sys->p_input );
182 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
183 onIntfChange, this );
184 var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
185 onItemAppend, this );
186 var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
187 onItemDelete, this );
188 var_DelCallback( getIntf()->p_libvlc, "intf-show",
190 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
191 onPlaylistChange, this );
192 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
193 onItemChange, this );
194 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
198 void VlcProc::registerVoutWindow( void *pVoutWindow )
200 m_handleSet.insert( pVoutWindow );
201 // Reparent the vout window
204 if( vout_Control( m_pVout, VOUT_REPARENT, 0 ) != VLC_SUCCESS )
205 vout_Control( m_pVout, VOUT_CLOSE );
210 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
212 m_handleSet.erase( pVoutWindow );
216 void VlcProc::dropVout()
220 if( vout_Control( m_pVout, VOUT_REPARENT, 0 ) != VLC_SUCCESS )
221 vout_Control( m_pVout, VOUT_CLOSE );
227 void VlcProc::manage()
229 // Did the user request to quit vlc ?
230 if( !vlc_object_alive( getIntf() ) )
232 CmdQuit *pCmd = new CmdQuit( getIntf() );
233 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
234 pQueue->push( CmdGenericPtr( pCmd ) );
241 void VlcProc::CmdManage::execute()
243 // Just forward to VlcProc
247 void VlcProc::refreshAudio()
251 // Check if the audio output has changed
252 aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
253 VLC_OBJECT_AOUT, FIND_ANYWHERE );
256 if( pAout != m_pAout )
258 // Register the equalizer callbacks
259 if( !var_AddCallback( pAout, "equalizer-bands",
260 onEqBandsChange, this ) &&
261 !var_AddCallback( pAout, "equalizer-preamp",
262 onEqPreampChange, this ) )
265 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
268 // Get the audio filters
269 pFilters = var_GetNonEmptyString( pAout, "audio-filter" );
270 vlc_object_release( pAout );
274 // Get the audio filters
275 pFilters = config_GetPsz( getIntf(), "audio-filter" );
278 // Refresh sound volume
279 audio_volume_t volume;
280 aout_VolumeGet( getIntf(), &volume );
281 Volume *pVolume = (Volume*)m_cVarVolume.get();
282 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
284 // Set the mute variable
285 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
286 pVarMute->set( volume == 0 );
288 // Refresh the equalizer variable
289 VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
290 pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
294 void VlcProc::refreshPlaylist()
296 // Refresh the random variable
297 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
299 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
300 pVarRandom->set( val.b_bool != 0 );
302 // Refresh the loop variable
303 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
304 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
305 pVarLoop->set( val.b_bool != 0 );
307 // Refresh the repeat variable
308 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
309 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
310 pVarRepeat->set( val.b_bool != 0 );
313 void VlcProc::refreshInput()
315 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
316 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
317 VarBoolImpl *pVarDvdActive = (VarBoolImpl*)m_cVarDvdActive.get();
318 VarBoolImpl *pVarHasVout = (VarBoolImpl*)m_cVarHasVout.get();
319 VarBoolImpl *pVarHasAudio = (VarBoolImpl*)m_cVarHasAudio.get();
320 VarText *pBitrate = (VarText*)m_cVarStreamBitRate.get();
321 VarText *pSampleRate = (VarText*)m_cVarStreamSampleRate.get();
322 VarBoolImpl *pVarFullscreen = (VarBoolImpl*)m_cVarFullscreen.get();
323 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
324 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
325 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
327 input_thread_t *pInput = getIntf()->p_sys->p_input;
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;
342 if( pInput && vlc_object_alive (pInput) )
344 // Refresh time variables
346 var_Get( pInput, "position", &pos );
347 pTime->set( pos.f_float, false );
348 pVarSeekable->set( pos.f_float != 0.0 );
350 // Refresh DVD detection
351 vlc_value_t chapters_count;
352 var_Change( pInput, "chapter", VLC_VAR_CHOICESCOUNT,
353 &chapters_count, NULL );
354 pVarDvdActive->set( chapters_count.i_int > 0 );
356 // Get the input bitrate
357 int bitrate = var_GetInteger( pInput, "bit-rate" ) / 1000;
358 pBitrate->set( UString::fromInt( getIntf(), bitrate ) );
360 // Get the audio sample rate
361 int sampleRate = var_GetInteger( pInput, "sample-rate" ) / 1000;
362 pSampleRate->set( UString::fromInt( getIntf(), sampleRate ) );
365 vlc_value_t audio_es;
366 var_Change( pInput, "audio-es", VLC_VAR_CHOICESCOUNT,
368 pVarHasAudio->set( audio_es.i_int > 0 );
370 // Refresh fullscreen status
371 vout_thread_t *pVout = (vout_thread_t *)vlc_object_find( pInput,
372 VLC_OBJECT_VOUT, FIND_CHILD );
373 pVarHasVout->set( pVout != NULL );
376 pVarFullscreen->set( pVout->b_fullscreen );
377 vlc_object_release( pVout );
380 // Refresh play/pause status
381 int state = var_GetInteger( pInput, "state" );
382 pVarStopped->set( false );
383 pVarPlaying->set( state != PAUSE_S );
384 pVarPaused->set( state == PAUSE_S );
388 pVarSeekable->set( false );
389 pVarDvdActive->set( false );
390 pTime->set( 0, false );
391 pVarFullscreen->set( false );
392 pVarHasAudio->set( false );
393 pVarHasVout->set( false );
394 pVarStopped->set( true );
395 pVarPlaying->set( false );
396 pVarPaused->set( false );
400 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
401 vlc_value_t oldVal, vlc_value_t newVal,
404 VlcProc *pThis = (VlcProc*)pParam;
406 // Update the stream variable
407 pThis->updateStreamName();
409 // Create a playtree notify command (for new style playtree)
410 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
412 // Push the command in the asynchronous command queue
413 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
414 pQueue->push( CmdGenericPtr( pCmdTree ) );
420 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
421 vlc_value_t oldVal, vlc_value_t newVal,
426 VlcProc *pThis = (VlcProc*)pParam;
428 // Create a raise all command
429 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
430 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
432 // Push the command in the asynchronous command queue
433 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
434 pQueue->push( CmdGenericPtr( pCmd ) );
441 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
442 vlc_value_t oldVal, vlc_value_t newVal,
445 VlcProc *pThis = (VlcProc*)pParam;
447 // Update the stream variable
448 pThis->updateStreamName();
450 // Create a playtree notify command
451 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
454 // Push the command in the asynchronous command queue
455 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
456 pQueue->push( CmdGenericPtr( pCmdTree ), true );
461 int VlcProc::onItemAppend( vlc_object_t *pObj, const char *pVariable,
462 vlc_value_t oldVal, vlc_value_t newVal,
465 VlcProc *pThis = (VlcProc*)pParam;
467 playlist_add_t *p_add = (playlist_add_t*)malloc( sizeof(
470 memcpy( p_add, newVal.p_address, sizeof( playlist_add_t ) ) ;
472 CmdGenericPtr ptrTree;
473 CmdPlaytreeAppend *pCmdTree = new CmdPlaytreeAppend( pThis->getIntf(),
475 ptrTree = CmdGenericPtr( pCmdTree );
477 // Push the command in the asynchronous command queue
478 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
479 pQueue->push( ptrTree , false );
484 int VlcProc::onItemDelete( vlc_object_t *pObj, const char *pVariable,
485 vlc_value_t oldVal, vlc_value_t newVal,
488 VlcProc *pThis = (VlcProc*)pParam;
490 int i_id = newVal.i_int;
492 CmdGenericPtr ptrTree;
493 CmdPlaytreeDelete *pCmdTree = new CmdPlaytreeDelete( pThis->getIntf(),
495 ptrTree = CmdGenericPtr( pCmdTree );
497 // Push the command in the asynchronous command queue
498 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
499 pQueue->push( ptrTree , false );
505 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
506 vlc_value_t oldVal, vlc_value_t newVal,
509 VlcProc *pThis = (VlcProc*)pParam;
511 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
513 // Update the stream variable
514 pThis->updateStreamName();
516 // Create two playtree notify commands: one for old item, one for new
517 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
519 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
520 pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(), newVal.i_int );
521 pQueue->push( CmdGenericPtr( pCmdTree ) , true );
527 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
528 vlc_value_t oldVal, vlc_value_t newVal,
531 VlcProc *pThis = (VlcProc*)pParam;
533 // Create a playlist notify command
534 CmdChangeSkin *pCmd =
535 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
537 // Push the command in the asynchronous command queue
538 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
539 pQueue->push( CmdGenericPtr( pCmd ) );
544 int VlcProc::onInteraction( vlc_object_t *pObj, const char *pVariable,
545 vlc_value_t oldVal, vlc_value_t newVal,
548 VlcProc *pThis = (VlcProc*)pParam;
549 interaction_dialog_t *p_dialog = (interaction_dialog_t *)(newVal.p_address);
551 CmdInteraction *pCmd = new CmdInteraction( pThis->getIntf(), p_dialog );
552 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
553 pQueue->push( CmdGenericPtr( pCmd ) );
558 void VlcProc::updateStreamName()
560 // Create a update item command
561 CmdUpdateItem *pCmdItem = new CmdUpdateItem( getIntf(), getStreamNameVar(), getStreamURIVar() );
563 // Push the command in the asynchronous command queue
564 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
565 pQueue->push( CmdGenericPtr( pCmdItem ) );
568 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
569 int *pXHint, int *pYHint,
570 unsigned int *pWidthHint,
571 unsigned int *pHeightHint )
573 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
574 if( pThis->m_handleSet.empty() )
580 pThis->m_pVout = pVout;
581 // Get the window handle
582 void *pWindow = *pThis->m_handleSet.begin();
583 // Post a resize vout command
584 CmdResizeVout *pCmd = new CmdResizeVout( pThis->getIntf(), pWindow,
585 *pWidthHint, *pHeightHint );
586 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
587 pQueue->push( CmdGenericPtr( pCmd ) );
593 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
595 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
596 pThis->m_pVout = NULL;
600 int VlcProc::controlWindow( struct vout_window_t *pWnd,
601 int query, va_list args )
603 intf_thread_t *pIntf = (intf_thread_t *)pWnd->p_private;
604 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
612 unsigned int i_width = va_arg( args, unsigned int );
613 unsigned int i_height = va_arg( args, unsigned int );
614 if( !i_width ) i_width = pThis->m_pVout->i_window_width;
615 if( !i_height ) i_height = pThis->m_pVout->i_window_height;
617 // Post a resize vout command
618 CmdResizeVout *pCmd =
619 new CmdResizeVout( pThis->getIntf(), pWnd->handle,
621 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
622 pQueue->push( CmdGenericPtr( pCmd ) );
627 msg_Dbg( pWnd, "control query not supported" );
635 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
636 vlc_value_t oldVal, vlc_value_t newVal,
639 VlcProc *pThis = (VlcProc*)pParam;
641 // Post a set equalizer bands command
642 CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
645 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
646 pQueue->push( CmdGenericPtr( pCmd ) );
652 int VlcProc::onEqPreampChange( vlc_object_t *pObj, const char *pVariable,
653 vlc_value_t oldVal, vlc_value_t newVal,
656 VlcProc *pThis = (VlcProc*)pParam;
657 EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)(pThis->m_cVarEqPreamp.get());
659 // Post a set preamp command
660 CmdSetEqPreamp *pCmd = new CmdSetEqPreamp( pThis->getIntf(), *pVarPreamp,
661 (newVal.f_float + 20.0) / 40.0 );
662 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
663 pQueue->push( CmdGenericPtr( pCmd ) );