]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/vlcproc.cpp
* all: added a new variable "equalizer.preamp" (self-explanatory ;) in skins
[vlc] / modules / gui / skins2 / src / vlcproc.cpp
1 /*****************************************************************************
2  * vlcproc.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teuli�e <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include <vlc/aout.h>
26 #include <vlc/vout.h>
27 #include <aout_internal.h>
28
29 #include "vlcproc.hpp"
30 #include "os_factory.hpp"
31 #include "os_timer.hpp"
32 #include "var_manager.hpp"
33 #include "theme.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"
42 #include <sstream>
43
44
45 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
46 {
47     if( pIntf->p_sys->p_vlcProc == NULL )
48     {
49         pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
50     }
51
52     return pIntf->p_sys->p_vlcProc;
53 }
54
55
56 void VlcProc::destroy( intf_thread_t *pIntf )
57 {
58     if( pIntf->p_sys->p_vlcProc )
59     {
60         delete pIntf->p_sys->p_vlcProc;
61         pIntf->p_sys->p_vlcProc = NULL;
62     }
63 }
64
65
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 )
69 {
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 );
74
75     // Create and register VLC variables
76     VarManager *pVarManager = VarManager::instance( getIntf() );
77
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(),
83                               "playlist.slider" );
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(),
89                               "playtree.slider" );
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" )
101     REGISTER_VAR( m_cVarEqPreamp, EqualizerPreamp, "equalizer.preamp" )
102 #undef REGISTER_VAR
103     m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
104     pVarManager->registerVar( m_cVarStreamName, "streamName" );
105     m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
106     pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
107
108     // Register the equalizer bands
109     for( int i = 0; i < EqualizerBands::kNbBands; i++)
110     {
111         stringstream ss;
112         ss << "equalizer.band(" << i << ")";
113         pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
114     }
115
116     // XXX WARNING XXX
117     // The object variable callbacks are called from other VLC threads,
118     // so they must put commands in the queue and NOT do anything else
119     // (X11 calls are not reentrant)
120
121     // Called when the playlist changes
122     var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
123                      onIntfChange, this );
124     // Called when a playlist item is added
125     // TODO: properly handle item-append
126     var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
127                      onIntfChange, 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                      onIntfChange, this );
132     // Called when the "interface shower" wants us to show the skin
133     var_AddCallback( pIntf->p_sys->p_playlist, "intf-show",
134                      onIntfShow, this );
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 );
143
144     // Callbacks for vout requests
145     getIntf()->pf_request_window = &getWindow;
146     getIntf()->pf_release_window = &releaseWindow;
147     getIntf()->pf_control_window = &controlWindow;
148
149     getIntf()->p_sys->p_input = NULL;
150 }
151
152
153 VlcProc::~VlcProc()
154 {
155     m_pTimer->stop();
156     delete( m_pTimer );
157     if( getIntf()->p_sys->p_input )
158     {
159         vlc_object_release( getIntf()->p_sys->p_input );
160     }
161
162     // Callbacks for vout requests
163     getIntf()->pf_request_window = NULL;
164     getIntf()->pf_release_window = NULL;
165     getIntf()->pf_control_window = NULL;
166
167     var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
168                      onIntfChange, this );
169     var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
170                      onIntfChange, this );
171     var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
172                      onIntfChange, this );
173     var_DelCallback( getIntf()->p_sys->p_playlist, "intf-show",
174                      onIntfShow, this );
175     var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
176                      onPlaylistChange, this );
177     var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
178                      onItemChange, this );
179     var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
180 }
181
182
183 void VlcProc::registerVoutWindow( void *pVoutWindow )
184 {
185     m_handleSet.insert( pVoutWindow );
186     // Reparent the vout window
187     if( m_pVout )
188     {
189         if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
190             vout_Control( m_pVout, VOUT_CLOSE );
191     }
192 }
193
194
195 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
196 {
197     m_handleSet.erase( pVoutWindow );
198 }
199
200
201 void VlcProc::dropVout()
202 {
203     if( m_pVout )
204     {
205         if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
206             vout_Control( m_pVout, VOUT_CLOSE );
207         m_pVout = NULL;
208     }
209 }
210
211
212 void VlcProc::manage()
213 {
214     // Did the user requested to quit vlc ?
215     if( getIntf()->b_die || getIntf()->p_vlc->b_die )
216     {
217         CmdQuit *pCmd = new CmdQuit( getIntf() );
218         AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
219         pQueue->push( CmdGenericPtr( pCmd ) );
220     }
221
222     // Get the VLC variables
223     StreamTime *pTime = (StreamTime*)m_cVarTime.get();
224     VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
225     VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
226     VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
227     VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
228     VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
229     VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
230     VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
231
232     // Refresh audio variables
233     refreshAudio();
234
235    // Update the input
236     if( getIntf()->p_sys->p_input == NULL )
237     {
238         getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
239             getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
240     }
241     else if( getIntf()->p_sys->p_input->b_dead )
242     {
243         vlc_object_release( getIntf()->p_sys->p_input );
244         getIntf()->p_sys->p_input = NULL;
245     }
246
247     input_thread_t *pInput = getIntf()->p_sys->p_input;
248
249     if( pInput && !pInput->b_die )
250     {
251         // Refresh time variables
252         vlc_value_t pos;
253         var_Get( pInput, "position", &pos );
254         pTime->set( pos.f_float, false );
255
256         // Get the status of the playlist
257         playlist_status_t status =
258             getIntf()->p_sys->p_playlist->status.i_status;
259
260         pVarPlaying->set( status == PLAYLIST_RUNNING );
261         pVarStopped->set( status == PLAYLIST_STOPPED );
262         pVarPaused->set( status == PLAYLIST_PAUSED );
263
264         pVarSeekable->set( pos.f_float != 0.0 );
265     }
266     else
267     {
268         pVarPlaying->set( false );
269         pVarPaused->set( false );
270         pVarStopped->set( true );
271         pVarSeekable->set( false );
272         pTime->set( 0, false );
273     }
274
275     // Refresh the random variable
276     vlc_value_t val;
277     var_Get( getIntf()->p_sys->p_playlist, "random", &val );
278     pVarRandom->set( val.b_bool != 0 );
279
280     // Refresh the loop variable
281     var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
282     pVarLoop->set( val.b_bool != 0 );
283
284     // Refresh the repeat variable
285     var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
286     pVarRepeat->set( val.b_bool != 0 );
287 }
288
289
290 void VlcProc::CmdManage::execute()
291 {
292     // Just forward to VlcProc
293     m_pParent->manage();
294 }
295
296
297 void VlcProc::refreshAudio()
298 {
299     char *pFilters = NULL;
300
301     // Check if the audio output has changed
302     aout_instance_t *pAout = (aout_instance_t *)vlc_object_find( getIntf(),
303             VLC_OBJECT_AOUT, FIND_ANYWHERE );
304     if( pAout )
305     {
306         if( pAout != m_pAout )
307         {
308             // Register the equalizer callbacks
309             if( !var_AddCallback( pAout, "equalizer-bands",
310                                   onEqBandsChange, this ) &&
311                 !var_AddCallback( pAout, "equalizer-preamp",
312                                   onEqPreampChange, this ) )
313             {
314                 m_pAout = pAout;
315                 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
316             }
317         }
318         // Get the audio filters
319         pFilters = var_GetString( pAout, "audio-filter" );
320         vlc_object_release( pAout );
321     }
322     else
323     {
324         // Get the audio filters
325         pFilters = config_GetPsz( getIntf(), "audio-filter" );
326     }
327
328     // Refresh sound volume
329     audio_volume_t volume;
330     aout_VolumeGet( getIntf(), &volume );
331     Volume *pVolume = (Volume*)m_cVarVolume.get();
332     pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
333
334     // Set the mute variable
335     VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
336     pVarMute->set( volume == 0 );
337
338     // Refresh the equalizer variable
339     VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
340     pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
341 }
342
343
344 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
345                            vlc_value_t oldVal, vlc_value_t newVal,
346                            void *pParam )
347 {
348     VlcProc *pThis = (VlcProc*)pParam;
349
350     // Update the stream variable
351     playlist_t *p_playlist = (playlist_t*)pObj;
352     pThis->updateStreamName(p_playlist);
353
354     // Create a playlist notify command
355     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
356     // Create a playtree notify command
357     CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
358
359     // Push the command in the asynchronous command queue
360     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
361     pQueue->push( CmdGenericPtr( pCmd ) );
362     pQueue->push( CmdGenericPtr( pCmdTree ) );
363
364     return VLC_SUCCESS;
365 }
366
367
368 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
369                          vlc_value_t oldVal, vlc_value_t newVal,
370                          void *pParam )
371 {
372     if (newVal.i_int)
373     {
374         VlcProc *pThis = (VlcProc*)pParam;
375
376         // Create a raise all command
377         CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
378             pThis->getIntf()->p_sys->p_theme->getWindowManager() );
379
380         // Push the command in the asynchronous command queue
381         AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
382         pQueue->push( CmdGenericPtr( pCmd ) );
383     }
384
385     return VLC_SUCCESS;
386 }
387
388
389 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
390                            vlc_value_t oldVal, vlc_value_t newVal,
391                            void *pParam )
392 {
393     VlcProc *pThis = (VlcProc*)pParam;
394
395     // Update the stream variable
396     playlist_t *p_playlist = (playlist_t*)pObj;
397     pThis->updateStreamName(p_playlist);
398
399     // Create a playlist notify command
400     // TODO: selective update
401     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
402     // Create a playtree notify command
403     CmdPlaytreeUpdate *pCmdTree = new CmdPlaytreeUpdate( pThis->getIntf(),
404                                                          newVal.i_int );
405
406     // Push the command in the asynchronous command queue
407     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
408     pQueue->push( CmdGenericPtr( pCmd ) );
409     pQueue->push( CmdGenericPtr( pCmdTree ), false );
410
411     return VLC_SUCCESS;
412 }
413
414
415 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
416                                vlc_value_t oldVal, vlc_value_t newVal,
417                                void *pParam )
418 {
419     VlcProc *pThis = (VlcProc*)pParam;
420
421     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
422
423     // Update the stream variable
424     playlist_t *p_playlist = (playlist_t*)pObj;
425     pThis->updateStreamName(p_playlist);
426
427     // Create a playlist notify command
428     // TODO: selective update
429     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
430     // Create a playtree notify command
431     CmdPlaytreeChanged *pCmdTree = new CmdPlaytreeChanged( pThis->getIntf() );
432
433     // Push the command in the asynchronous command queue
434     pQueue->push( CmdGenericPtr( pCmd ) );
435     pQueue->push( CmdGenericPtr( pCmdTree ) );
436
437     return VLC_SUCCESS;
438 }
439
440
441 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
442                            vlc_value_t oldVal, vlc_value_t newVal,
443                            void *pParam )
444 {
445     VlcProc *pThis = (VlcProc*)pParam;
446
447     // Create a playlist notify command
448     CmdChangeSkin *pCmd =
449         new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
450
451     // Push the command in the asynchronous command queue
452     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
453     pQueue->push( CmdGenericPtr( pCmd ) );
454
455     return VLC_SUCCESS;
456 }
457
458
459 void VlcProc::updateStreamName( playlist_t *p_playlist )
460 {
461     if( p_playlist->p_input )
462     {
463         VarText &rStreamName = getStreamNameVar();
464         VarText &rStreamURI = getStreamURIVar();
465         // XXX: we should not need to access p_input->psz_source directly, a
466         // getter should be provided by VLC core
467         string name = p_playlist->p_input->input.p_item->psz_name;
468         // XXX: This should be done in VLC core, not here...
469         // Remove path information if any
470         OSFactory *pFactory = OSFactory::instance( getIntf() );
471         string::size_type pos = name.rfind( pFactory->getDirSeparator() );
472         if( pos != string::npos )
473         {
474             name = name.substr( pos + 1, name.size() - pos + 1 );
475         }
476         UString srcName( getIntf(), name.c_str() );
477         UString srcURI( getIntf(),
478                          p_playlist->p_input->input.p_item->psz_uri );
479
480         // Create commands to update the stream variables
481         CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
482         CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
483         // Push the commands in the asynchronous command queue
484         AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
485         pQueue->push( CmdGenericPtr( pCmd1 ), false );
486         pQueue->push( CmdGenericPtr( pCmd2 ), false );
487     }
488 }
489
490
491 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
492                           int *pXHint, int *pYHint,
493                           unsigned int *pWidthHint,
494                           unsigned int *pHeightHint )
495 {
496     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
497     pThis->m_pVout = pVout;
498     if( pThis->m_handleSet.empty() )
499     {
500         return NULL;
501     }
502     else
503     {
504         // Get the window handle
505         void *pWindow = *pThis->m_handleSet.begin();
506         // Post a resize vout command
507         CmdResizeVout *pCmd = new CmdResizeVout( pThis->getIntf(), pWindow,
508                                                  *pWidthHint, *pHeightHint );
509         AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
510         pQueue->push( CmdGenericPtr( pCmd ) );
511         return pWindow;
512     }
513 }
514
515
516 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
517 {
518     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
519     pThis->m_pVout = NULL;
520 }
521
522
523 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
524                             int query, va_list args )
525 {
526     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
527
528     switch( query )
529     {
530         case VOUT_SET_SIZE:
531         {
532             if( pThis->m_pVout )
533             {
534                 unsigned int i_width  = va_arg( args, unsigned int );
535                 unsigned int i_height = va_arg( args, unsigned int );
536                 if( !i_width ) i_width = pThis->m_pVout->i_window_width;
537                 if( !i_height ) i_height = pThis->m_pVout->i_window_height;
538
539                 // Post a resize vout command
540                 CmdResizeVout *pCmd =
541                     new CmdResizeVout( pThis->getIntf(), pWindow,
542                                        i_width, i_height );
543                 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
544                 pQueue->push( CmdGenericPtr( pCmd ) );
545             }
546         }
547
548         default:
549             msg_Dbg( pIntf, "control query not supported" );
550             break;
551     }
552
553     return VLC_SUCCESS;
554 }
555
556
557 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
558                               vlc_value_t oldVal, vlc_value_t newVal,
559                               void *pParam )
560 {
561     VlcProc *pThis = (VlcProc*)pParam;
562
563     // Post a set equalizer bands command
564     CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
565                                              pThis->m_varEqBands,
566                                              newVal.psz_string );
567     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
568     pQueue->push( CmdGenericPtr( pCmd ) );
569
570     return VLC_SUCCESS;
571 }
572
573
574 int VlcProc::onEqPreampChange( vlc_object_t *pObj, const char *pVariable,
575                                vlc_value_t oldVal, vlc_value_t newVal,
576                                void *pParam )
577 {
578     VlcProc *pThis = (VlcProc*)pParam;
579     EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)(pThis->m_cVarEqPreamp.get());
580
581     // Post a set preamp command
582     CmdSetEqPreamp *pCmd = new CmdSetEqPreamp( pThis->getIntf(), *pVarPreamp,
583                                               (newVal.f_float + 20.0) / 40.0 );
584     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
585     pQueue->push( CmdGenericPtr( pCmd ) );
586
587     return VLC_SUCCESS;
588 }
589