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 ),
68 m_pVout( NULL ), 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" )
100 REGISTER_VAR( m_cVarEqualizer, VarBoolImpl, "equalizer.isEnabled" )
102 m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
103 pVarManager->registerVar( m_cVarStreamName, "streamName" );
104 m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
105 pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
107 // Register the equalizer bands
108 for( int i = 0; i < EqualizerBands::kNbBands; i++)
111 ss << "equalizer.band(" << i << ")";
112 pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
116 // The object variable callbacks are called from other VLC threads,
117 // so they must put commands in the queue and NOT do anything else
118 // (X11 calls are not reentrant)
120 // Called when the playlist changes
121 var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
122 onIntfChange, this );
123 // Called when a playlist item is added
124 // TODO: properly handle item-append
125 var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
126 onIntfChange, this );
127 // Called when a playlist item is deleted
128 // TODO: properly handle item-deleted
129 var_AddCallback( pIntf->p_sys->p_playlist, "item-deleted",
130 onIntfChange, this );
131 // Called when the "interface shower" wants us to show the skin
132 var_AddCallback( pIntf->p_sys->p_playlist, "intf-show",
134 // Called when the current played item changes
135 var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
136 onPlaylistChange, this );
137 // Called when a playlist item changed
138 var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
139 onItemChange, this );
140 // Called when our skins2 demux wants us to load a new skin
141 var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
143 // Callbacks for vout requests
144 getIntf()->pf_request_window = &getWindow;
145 getIntf()->pf_release_window = &releaseWindow;
146 getIntf()->pf_control_window = &controlWindow;
148 getIntf()->p_sys->p_input = NULL;
156 if( getIntf()->p_sys->p_input )
158 vlc_object_release( getIntf()->p_sys->p_input );
161 // Callbacks for vout requests
162 getIntf()->pf_request_window = NULL;
163 getIntf()->pf_release_window = NULL;
164 getIntf()->pf_control_window = NULL;
166 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
167 onIntfChange, this );
168 var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
169 onIntfChange, this );
170 var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
171 onIntfChange, this );
172 var_DelCallback( getIntf()->p_sys->p_playlist, "intf-show",
174 var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
175 onPlaylistChange, this );
176 var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
177 onItemChange, this );
178 var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
182 void VlcProc::registerVoutWindow( void *pVoutWindow )
184 m_handleSet.insert( pVoutWindow );
185 // Reparent the vout window
188 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
189 vout_Control( m_pVout, VOUT_CLOSE );
194 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
196 m_handleSet.erase( pVoutWindow );
200 void VlcProc::dropVout()
204 if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
205 vout_Control( m_pVout, VOUT_CLOSE );
211 void VlcProc::manage()
213 // Did the user requested to quit vlc ?
214 if( getIntf()->b_die || getIntf()->p_vlc->b_die )
216 CmdQuit *pCmd = new CmdQuit( getIntf() );
217 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
218 pQueue->push( CmdGenericPtr( pCmd ) );
221 // Get the VLC variables
222 StreamTime *pTime = (StreamTime*)m_cVarTime.get();
223 VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
224 VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
225 VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
226 VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
227 VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
228 VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
229 VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
231 // Refresh audio variables
235 if( getIntf()->p_sys->p_input == NULL )
237 getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
238 getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
240 else if( getIntf()->p_sys->p_input->b_dead )
242 vlc_object_release( getIntf()->p_sys->p_input );
243 getIntf()->p_sys->p_input = NULL;
246 input_thread_t *pInput = getIntf()->p_sys->p_input;
248 if( pInput && !pInput->b_die )
250 // Refresh time variables
252 var_Get( pInput, "position", &pos );
253 pTime->set( pos.f_float, false );
255 // Get the status of the playlist
256 playlist_status_t status =
257 getIntf()->p_sys->p_playlist->status.i_status;
259 pVarPlaying->set( status == PLAYLIST_RUNNING );
260 pVarStopped->set( status == PLAYLIST_STOPPED );
261 pVarPaused->set( status == PLAYLIST_PAUSED );
263 pVarSeekable->set( pos.f_float != 0.0 );
267 pVarPlaying->set( false );
268 pVarPaused->set( false );
269 pVarStopped->set( true );
270 pVarSeekable->set( false );
271 pTime->set( 0, false );
274 // Refresh the random variable
276 var_Get( getIntf()->p_sys->p_playlist, "random", &val );
277 pVarRandom->set( val.b_bool != 0 );
279 // Refresh the loop variable
280 var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
281 pVarLoop->set( val.b_bool != 0 );
283 // Refresh the repeat variable
284 var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
285 pVarRepeat->set( val.b_bool != 0 );
289 void VlcProc::CmdManage::execute()
291 // Just forward to VlcProc
296 void VlcProc::refreshAudio()
298 char *pFilters = NULL;
300 // Check if the audio output has changed
301 aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
302 VLC_OBJECT_AOUT, FIND_ANYWHERE );
305 if( pAout != m_pAout )
307 // Register the equalizer callback
308 if( !var_AddCallback( pAout, "equalizer-bands",
309 onEqBandsChange, this ) )
312 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
315 // Get the audio filters
316 pFilters = var_GetString( pAout, "audio-filter" );
317 vlc_object_release( pAout );
321 // Get the audio filters
322 pFilters = config_GetPsz( getIntf(), "audio-filter" );
325 // Refresh sound volume
326 audio_volume_t volume;
327 aout_VolumeGet( getIntf(), &volume );
328 Volume *pVolume = (Volume*)m_cVarVolume.get();
329 pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
331 // Set the mute variable
332 VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
333 pVarMute->set( volume == 0 );
335 // Refresh the equalizer variable
336 VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
337 pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
341 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
342 vlc_value_t oldVal, vlc_value_t newVal,
345 VlcProc *pThis = (VlcProc*)pParam;
347 // Update the stream variable
348 playlist_t *p_playlist = (playlist_t*)pObj;
349 pThis->updateStreamName(p_playlist);
351 // Create a playlist notify command
352 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
353 // Create a playtree notify command
354 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
356 // Push the command in the asynchronous command queue
357 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
358 pQueue->push( CmdGenericPtr( pCmd ) );
359 pQueue->push( CmdGenericPtr( pCmdTree ) );
365 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
366 vlc_value_t oldVal, vlc_value_t newVal,
371 VlcProc *pThis = (VlcProc*)pParam;
373 // Create a raise all command
374 CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
375 pThis->getIntf()->p_sys->p_theme->getWindowManager() );
377 // Push the command in the asynchronous command queue
378 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
379 pQueue->push( CmdGenericPtr( pCmd ) );
386 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
387 vlc_value_t oldVal, vlc_value_t newVal,
390 VlcProc *pThis = (VlcProc*)pParam;
392 // Update the stream variable
393 playlist_t *p_playlist = (playlist_t*)pObj;
394 pThis->updateStreamName(p_playlist);
396 // Create a playlist notify command
397 // TODO: selective update
398 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
399 // Create a playtree notify command
400 CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
403 // Push the command in the asynchronous command queue
404 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
405 pQueue->push( CmdGenericPtr( pCmd ) );
406 pQueue->push( CmdGenericPtr( pCmdTree ), false );
412 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
413 vlc_value_t oldVal, vlc_value_t newVal,
416 VlcProc *pThis = (VlcProc*)pParam;
418 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
420 // Update the stream variable
421 playlist_t *p_playlist = (playlist_t*)pObj;
422 pThis->updateStreamName(p_playlist);
424 // Create a playlist notify command
425 // TODO: selective update
426 CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
427 // Create a playtree notify command
428 CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
430 // Push the command in the asynchronous command queue
431 pQueue->push( CmdGenericPtr( pCmd ) );
432 pQueue->push( CmdGenericPtr( pCmdTree ) );
438 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
439 vlc_value_t oldVal, vlc_value_t newVal,
442 VlcProc *pThis = (VlcProc*)pParam;
444 // Create a playlist notify command
445 CmdChangeSkin *pCmd =
446 new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
448 // Push the command in the asynchronous command queue
449 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
450 pQueue->push( CmdGenericPtr( pCmd ) );
456 void VlcProc::updateStreamName( playlist_t *p_playlist )
458 if( p_playlist->p_input )
460 VarText &rStreamName = getStreamNameVar();
461 VarText &rStreamURI = getStreamURIVar();
462 // XXX: we should not need to access p_input->psz_source directly, a
463 // getter should be provided by VLC core
464 string name = p_playlist->p_input->input.p_item->psz_name;
465 // XXX: This should be done in VLC core, not here...
466 // Remove path information if any
467 OSFactory *pFactory = OSFactory::instance( getIntf() );
468 string::size_type pos = name.rfind( pFactory->getDirSeparator() );
469 if( pos != string::npos )
471 name = name.substr( pos + 1, name.size() - pos + 1 );
473 UString srcName( getIntf(), name.c_str() );
474 UString srcURI( getIntf(),
475 p_playlist->p_input->input.p_item->psz_uri );
477 // Create commands to update the stream variables
478 CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
479 CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
480 // Push the commands in the asynchronous command queue
481 AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
482 pQueue->push( CmdGenericPtr( pCmd1 ), false );
483 pQueue->push( CmdGenericPtr( pCmd2 ), false );
488 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
489 int *pXHint, int *pYHint,
490 unsigned int *pWidthHint,
491 unsigned int *pHeightHint )
493 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
494 pThis->m_pVout = pVout;
495 if( pThis->m_handleSet.empty() )
501 // Get the window handle
502 void *pWindow = *pThis->m_handleSet.begin();
503 // Post a resize vout command
504 CmdResizeVout *pCmd = new CmdResizeVout( pThis->getIntf(), pWindow,
505 *pWidthHint, *pHeightHint );
506 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
507 pQueue->push( CmdGenericPtr( pCmd ) );
513 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
515 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
516 pThis->m_pVout = NULL;
520 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
521 int query, va_list args )
523 VlcProc *pThis = pIntf->p_sys->p_vlcProc;
531 unsigned int i_width = va_arg( args, unsigned int );
532 unsigned int i_height = va_arg( args, unsigned int );
533 if( !i_width ) i_width = pThis->m_pVout->i_window_width;
534 if( !i_height ) i_height = pThis->m_pVout->i_window_height;
536 // Post a resize vout command
537 CmdResizeVout *pCmd =
538 new CmdResizeVout( pThis->getIntf(), pWindow,
540 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
541 pQueue->push( CmdGenericPtr( pCmd ) );
546 msg_Dbg( pIntf, "control query not supported" );
554 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
555 vlc_value_t oldVal, vlc_value_t newVal,
558 VlcProc *pThis = (VlcProc*)pParam;
560 // Post a set equalizer bands command
561 CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
564 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
565 pQueue->push( CmdGenericPtr( pCmd ) );