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