]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/vlcproc.cpp
* winamp2.xml: generic theme file for winamp2 skins. Still a
[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 ),
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 #undef REGISTER_VAR
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" );
106
107     // Register the equalizer bands
108     for( int i = 0; i < EqualizerBands::kNbBands; i++)
109     {
110         stringstream ss;
111         ss << "equalizer.band(" << i << ")";
112         pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
113     }
114
115     // XXX WARNING XXX
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)
119
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",
133                      onIntfShow, this );
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 );
142
143     // Callbacks for vout requests
144     getIntf()->pf_request_window = &getWindow;
145     getIntf()->pf_release_window = &releaseWindow;
146     getIntf()->pf_control_window = &controlWindow;
147
148     getIntf()->p_sys->p_input = NULL;
149 }
150
151
152 VlcProc::~VlcProc()
153 {
154     m_pTimer->stop();
155     delete( m_pTimer );
156     if( getIntf()->p_sys->p_input )
157     {
158         vlc_object_release( getIntf()->p_sys->p_input );
159     }
160
161     // Callbacks for vout requests
162     getIntf()->pf_request_window = NULL;
163     getIntf()->pf_release_window = NULL;
164     getIntf()->pf_control_window = NULL;
165
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",
173                      onIntfShow, this );
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 );
179 }
180
181
182 void VlcProc::registerVoutWindow( void *pVoutWindow )
183 {
184     m_handleSet.insert( pVoutWindow );
185     // Reparent the vout window
186     if( m_pVout )
187     {
188         if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
189             vout_Control( m_pVout, VOUT_CLOSE );
190     }
191 }
192
193
194 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
195 {
196     m_handleSet.erase( pVoutWindow );
197 }
198
199
200 void VlcProc::dropVout()
201 {
202     if( m_pVout )
203     {
204         if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
205             vout_Control( m_pVout, VOUT_CLOSE );
206         m_pVout = NULL;
207     }
208 }
209
210
211 void VlcProc::manage()
212 {
213     // Did the user requested to quit vlc ?
214     if( getIntf()->b_die || getIntf()->p_vlc->b_die )
215     {
216         CmdQuit *pCmd = new CmdQuit( getIntf() );
217         AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
218         pQueue->push( CmdGenericPtr( pCmd ) );
219     }
220
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();
230
231     // Refresh audio variables
232     refreshAudio();
233
234    // Update the input
235     if( getIntf()->p_sys->p_input == NULL )
236     {
237         getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
238             getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
239     }
240     else if( getIntf()->p_sys->p_input->b_dead )
241     {
242         vlc_object_release( getIntf()->p_sys->p_input );
243         getIntf()->p_sys->p_input = NULL;
244     }
245
246     input_thread_t *pInput = getIntf()->p_sys->p_input;
247
248     if( pInput && !pInput->b_die )
249     {
250         // Refresh time variables
251         vlc_value_t pos;
252         var_Get( pInput, "position", &pos );
253         pTime->set( pos.f_float, false );
254
255         // Get the status of the playlist
256         playlist_status_t status =
257             getIntf()->p_sys->p_playlist->status.i_status;
258
259         pVarPlaying->set( status == PLAYLIST_RUNNING );
260         pVarStopped->set( status == PLAYLIST_STOPPED );
261         pVarPaused->set( status == PLAYLIST_PAUSED );
262
263         pVarSeekable->set( pos.f_float != 0.0 );
264     }
265     else
266     {
267         pVarPlaying->set( false );
268         pVarPaused->set( false );
269         pVarStopped->set( true );
270         pVarSeekable->set( false );
271         pTime->set( 0, false );
272     }
273
274     // Refresh the random variable
275     vlc_value_t val;
276     var_Get( getIntf()->p_sys->p_playlist, "random", &val );
277     pVarRandom->set( val.b_bool != 0 );
278
279     // Refresh the loop variable
280     var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
281     pVarLoop->set( val.b_bool != 0 );
282
283     // Refresh the repeat variable
284     var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
285     pVarRepeat->set( val.b_bool != 0 );
286 }
287
288
289 void VlcProc::CmdManage::execute()
290 {
291     // Just forward to VlcProc
292     m_pParent->manage();
293 }
294
295
296 void VlcProc::refreshAudio()
297 {
298     char *pFilters = NULL;
299
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 );
303     if( pAout )
304     {
305         if( pAout != m_pAout )
306         {
307             // Register the equalizer callback
308             if( !var_AddCallback( pAout, "equalizer-bands",
309                                  onEqBandsChange, this ) )
310             {
311                 m_pAout = pAout;
312                 //char * psz_bands = var_GetString( p_aout, "equalizer-bands" );
313             }
314         }
315         // Get the audio filters
316         pFilters = var_GetString( pAout, "audio-filter" );
317         vlc_object_release( pAout );
318     }
319     else
320     {
321         // Get the audio filters
322         pFilters = config_GetPsz( getIntf(), "audio-filter" );
323     }
324
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 );
330
331     // Set the mute variable
332     VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
333     pVarMute->set( volume == 0 );
334
335     // Refresh the equalizer variable
336     VarBoolImpl *pVarEqualizer = (VarBoolImpl*)m_cVarEqualizer.get();
337     pVarEqualizer->set( pFilters && strstr( pFilters, "equalizer" ) );
338 }
339
340
341 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
342                            vlc_value_t oldVal, vlc_value_t newVal,
343                            void *pParam )
344 {
345     VlcProc *pThis = (VlcProc*)pParam;
346
347     // Update the stream variable
348     playlist_t *p_playlist = (playlist_t*)pObj;
349     pThis->updateStreamName(p_playlist);
350
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() );
355
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 ) );
360
361     return VLC_SUCCESS;
362 }
363
364
365 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
366                          vlc_value_t oldVal, vlc_value_t newVal,
367                          void *pParam )
368 {
369     if (newVal.i_int)
370     {
371         VlcProc *pThis = (VlcProc*)pParam;
372
373         // Create a raise all command
374         CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
375             pThis->getIntf()->p_sys->p_theme->getWindowManager() );
376
377         // Push the command in the asynchronous command queue
378         AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
379         pQueue->push( CmdGenericPtr( pCmd ) );
380     }
381
382     return VLC_SUCCESS;
383 }
384
385
386 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
387                            vlc_value_t oldVal, vlc_value_t newVal,
388                            void *pParam )
389 {
390     VlcProc *pThis = (VlcProc*)pParam;
391
392     // Update the stream variable
393     playlist_t *p_playlist = (playlist_t*)pObj;
394     pThis->updateStreamName(p_playlist);
395
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(),
401                                                          newVal.i_int );
402
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 );
407
408     return VLC_SUCCESS;
409 }
410
411
412 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
413                                vlc_value_t oldVal, vlc_value_t newVal,
414                                void *pParam )
415 {
416     VlcProc *pThis = (VlcProc*)pParam;
417
418     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
419
420     // Update the stream variable
421     playlist_t *p_playlist = (playlist_t*)pObj;
422     pThis->updateStreamName(p_playlist);
423
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() );
429
430     // Push the command in the asynchronous command queue
431     pQueue->push( CmdGenericPtr( pCmd ) );
432     pQueue->push( CmdGenericPtr( pCmdTree ) );
433
434     return VLC_SUCCESS;
435 }
436
437
438 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
439                            vlc_value_t oldVal, vlc_value_t newVal,
440                            void *pParam )
441 {
442     VlcProc *pThis = (VlcProc*)pParam;
443
444     // Create a playlist notify command
445     CmdChangeSkin *pCmd =
446         new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
447
448     // Push the command in the asynchronous command queue
449     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
450     pQueue->push( CmdGenericPtr( pCmd ) );
451
452     return VLC_SUCCESS;
453 }
454
455
456 void VlcProc::updateStreamName( playlist_t *p_playlist )
457 {
458     if( p_playlist->p_input )
459     {
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 )
470         {
471             name = name.substr( pos + 1, name.size() - pos + 1 );
472         }
473         UString srcName( getIntf(), name.c_str() );
474         UString srcURI( getIntf(),
475                          p_playlist->p_input->input.p_item->psz_uri );
476
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 );
484     }
485 }
486
487
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 )
492 {
493     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
494     pThis->m_pVout = pVout;
495     if( pThis->m_handleSet.empty() )
496     {
497         return NULL;
498     }
499     else
500     {
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 ) );
508         return pWindow;
509     }
510 }
511
512
513 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
514 {
515     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
516     pThis->m_pVout = NULL;
517 }
518
519
520 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
521                             int query, va_list args )
522 {
523     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
524
525     switch( query )
526     {
527         case VOUT_SET_SIZE:
528         {
529             if( pThis->m_pVout )
530             {
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;
535
536                 // Post a resize vout command
537                 CmdResizeVout *pCmd =
538                     new CmdResizeVout( pThis->getIntf(), pWindow,
539                                        i_width, i_height );
540                 AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
541                 pQueue->push( CmdGenericPtr( pCmd ) );
542             }
543         }
544
545         default:
546             msg_Dbg( pIntf, "control query not supported" );
547             break;
548     }
549
550     return VLC_SUCCESS;
551 }
552
553
554 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
555                               vlc_value_t oldVal, vlc_value_t newVal,
556                               void *pParam )
557 {
558     VlcProc *pThis = (VlcProc*)pParam;
559
560     // Post a set equalizer bands command
561     CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
562                                              pThis->m_varEqBands,
563                                              newVal.psz_string );
564     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
565     pQueue->push( CmdGenericPtr( pCmd ) );
566
567     return VLC_SUCCESS;
568 }
569