80543cbde9a5cd9e1f4f55c1e9988423da9fd860
[vlc.git] / 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
28 #include "vlcproc.hpp"
29 #include "os_factory.hpp"
30 #include "os_timer.hpp"
31 #include "var_manager.hpp"
32 #include "theme.hpp"
33 #include "window_manager.hpp"
34 #include "../commands/async_queue.hpp"
35 #include "../commands/cmd_change_skin.hpp"
36 #include "../commands/cmd_show_window.hpp"
37 #include "../commands/cmd_quit.hpp"
38 #include "../commands/cmd_vars.hpp"
39 #include "../utils/var_bool.hpp"
40
41
42 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
43 {
44     if( pIntf->p_sys->p_vlcProc == NULL )
45     {
46         pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
47     }
48
49     return pIntf->p_sys->p_vlcProc;
50 }
51
52
53 void VlcProc::destroy( intf_thread_t *pIntf )
54 {
55     if( pIntf->p_sys->p_vlcProc )
56     {
57         delete pIntf->p_sys->p_vlcProc;
58         pIntf->p_sys->p_vlcProc = NULL;
59     }
60 }
61
62
63 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ), m_pVout( NULL ),
64     m_cmdManage( this )
65 {
66     // Create a timer to poll the status of the vlc
67     OSFactory *pOsFactory = OSFactory::instance( pIntf );
68     m_pTimer = pOsFactory->createOSTimer( m_cmdManage );
69     m_pTimer->start( 100, false );
70
71     // Create and register VLC variables
72     VarManager *pVarManager = VarManager::instance( getIntf() );
73
74 #define REGISTER_VAR( var, type, name ) \
75     var = VariablePtr( new type( getIntf() ) ); \
76     pVarManager->registerVar( var, name );
77     REGISTER_VAR( m_cPlaylist, Playlist, "playlist" )
78     pVarManager->registerVar( getPlaylistVar().getPositionVarPtr(),
79                               "playlist.slider" );
80     REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
81     REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
82     REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
83     REGISTER_VAR( m_cVarTime, StreamTime, "time" )
84     REGISTER_VAR( m_cVarVolume, Volume, "volume" )
85     REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
86     REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
87     REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
88     REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
89     REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
90 #undef REGISTER_VAR
91     m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
92     pVarManager->registerVar( m_cVarStreamName, "streamName" );
93     m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
94     pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
95
96     // XXX WARNING XXX
97     // The object variable callbacks are called from other VLC threads,
98     // so they must put commands in the queue and NOT do anything else
99     // (X11 calls are not reentrant)
100
101     // Called when the playlist changes
102     var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
103                      onIntfChange, this );
104     // Called when a playlist item is added
105     // TODO: properly handle item-append
106     var_AddCallback( pIntf->p_sys->p_playlist, "item-append",
107                      onIntfChange, this );
108     // Called when a playlist item is deleted
109     // TODO: properly handle item-deleted
110     var_AddCallback( pIntf->p_sys->p_playlist, "item-deleted",
111                      onIntfChange, this );
112     // Called when the "interface shower" wants us to show the skin
113     var_AddCallback( pIntf->p_sys->p_playlist, "intf-show",
114                      onIntfShow, this );
115     // Called when the current played item changes
116     var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
117                      onPlaylistChange, this );
118     // Called when a playlist item changed
119     var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
120                      onItemChange, this );
121     // Called when our skins2 demux wants us to load a new skin
122     var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
123
124     // Callbacks for vout requests
125     getIntf()->pf_request_window = &getWindow;
126     getIntf()->pf_release_window = &releaseWindow;
127     getIntf()->pf_control_window = &controlWindow;
128
129     getIntf()->p_sys->p_input = NULL;
130 }
131
132
133 VlcProc::~VlcProc()
134 {
135     m_pTimer->stop();
136     delete( m_pTimer );
137     if( getIntf()->p_sys->p_input )
138     {
139         vlc_object_release( getIntf()->p_sys->p_input );
140     }
141
142     // Callbacks for vout requests
143     getIntf()->pf_request_window = NULL;
144     getIntf()->pf_release_window = NULL;
145     getIntf()->pf_control_window = NULL;
146
147     var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
148                      onIntfChange, this );
149     var_DelCallback( getIntf()->p_sys->p_playlist, "item-append",
150                      onIntfChange, this );
151     var_DelCallback( getIntf()->p_sys->p_playlist, "item-deleted",
152                      onIntfChange, this );
153     var_DelCallback( getIntf()->p_sys->p_playlist, "intf-show",
154                      onIntfShow, this );
155     var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
156                      onPlaylistChange, this );
157     var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
158                      onItemChange, this );
159     var_DelCallback( getIntf(), "skin-to-load", onSkinToLoad, this );
160 }
161
162
163 void VlcProc::registerVoutWindow( void *pVoutWindow )
164 {
165     m_handleSet.insert( pVoutWindow );
166     // Reparent the vout window
167     if( m_pVout )
168     {
169         if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
170             vout_Control( m_pVout, VOUT_CLOSE );
171     }
172 }
173
174
175 void VlcProc::unregisterVoutWindow( void *pVoutWindow )
176 {
177     m_handleSet.erase( pVoutWindow );
178 }
179
180
181 void VlcProc::dropVout()
182 {
183     if( m_pVout )
184     {
185         if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
186             vout_Control( m_pVout, VOUT_CLOSE );
187         m_pVout = NULL;
188     }
189 }
190
191
192 void VlcProc::manage()
193 {
194     // Did the user requested to quit vlc ?
195     if( getIntf()->b_die || getIntf()->p_vlc->b_die )
196     {
197         CmdQuit *pCmd = new CmdQuit( getIntf() );
198         AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
199         pQueue->push( CmdGenericPtr( pCmd ) );
200     }
201
202     // Get the VLC variables
203     StreamTime *pTime = (StreamTime*)m_cVarTime.get();
204     Volume *pVolume = (Volume*)m_cVarVolume.get();
205     VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
206     VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
207     VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
208     VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
209     VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
210     VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
211     VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
212     VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
213
214     // Refresh sound volume
215     audio_volume_t volume;
216     aout_VolumeGet( getIntf(), &volume );
217     pVolume->set( (double)volume * 2.0 / AOUT_VOLUME_MAX );
218     // Set the mute variable
219     pVarMute->set( volume == 0 );
220
221     // Update the input
222     if( getIntf()->p_sys->p_input == NULL )
223     {
224         getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
225             getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
226     }
227     else if( getIntf()->p_sys->p_input->b_dead )
228     {
229         vlc_object_release( getIntf()->p_sys->p_input );
230         getIntf()->p_sys->p_input = NULL;
231     }
232
233     input_thread_t *pInput = getIntf()->p_sys->p_input;
234
235     if( pInput && !pInput->b_die )
236     {
237         // Refresh time variables
238         vlc_value_t pos;
239         var_Get( pInput, "position", &pos );
240         pTime->set( pos.f_float, false );
241
242         // Get the status of the playlist
243         playlist_status_t status =
244             getIntf()->p_sys->p_playlist->status.i_status;
245
246         pVarPlaying->set( status == PLAYLIST_RUNNING );
247         pVarStopped->set( status == PLAYLIST_STOPPED );
248         pVarPaused->set( status == PLAYLIST_PAUSED );
249
250         pVarSeekable->set( pos.f_float != 0.0 );
251     }
252     else
253     {
254         pVarPlaying->set( false );
255         pVarPaused->set( false );
256         pVarStopped->set( true );
257         pVarSeekable->set( false );
258         pTime->set( 0, false );
259     }
260
261     // Refresh the random variable
262     vlc_value_t val;
263     var_Get( getIntf()->p_sys->p_playlist, "random", &val );
264     pVarRandom->set( val.b_bool != 0 );
265
266     // Refresh the loop variable
267     var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
268     pVarLoop->set( val.b_bool != 0 );
269
270     // Refresh the repeat variable
271     var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
272     pVarRepeat->set( val.b_bool != 0 );
273 }
274
275
276 void VlcProc::CmdManage::execute()
277 {
278     // Just forward to VlcProc
279     m_pParent->manage();
280 }
281
282
283 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
284                            vlc_value_t oldVal, vlc_value_t newVal,
285                            void *pParam )
286 {
287     VlcProc *pThis = (VlcProc*)pParam;
288
289     // Update the stream variable
290     playlist_t *p_playlist = (playlist_t*)pObj;
291     pThis->updateStreamName(p_playlist);
292
293     // Create a playlist notify command
294     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
295
296     // Push the command in the asynchronous command queue
297     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
298     pQueue->remove( "notify playlist" );
299     pQueue->push( CmdGenericPtr( pCmd ) );
300
301     return VLC_SUCCESS;
302 }
303
304
305 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
306                          vlc_value_t oldVal, vlc_value_t newVal,
307                          void *pParam )
308 {
309     if (newVal.i_int)
310     {
311         VlcProc *pThis = (VlcProc*)pParam;
312
313         // Create a raise all command
314         CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
315             pThis->getIntf()->p_sys->p_theme->getWindowManager() );
316
317         // Push the command in the asynchronous command queue
318         AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
319         pQueue->remove( "raise all windows" );
320         pQueue->push( CmdGenericPtr( pCmd ) );
321     }
322
323     return VLC_SUCCESS;
324 }
325
326
327 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
328                            vlc_value_t oldVal, vlc_value_t newVal,
329                            void *pParam )
330 {
331     VlcProc *pThis = (VlcProc*)pParam;
332
333     // Update the stream variable
334     playlist_t *p_playlist = (playlist_t*)pObj;
335     pThis->updateStreamName(p_playlist);
336
337     // Create a playlist notify command
338     // TODO: selective update
339     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
340
341     // Push the command in the asynchronous command queue
342     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
343     pQueue->remove( "notify playlist" );
344     pQueue->push( CmdGenericPtr( pCmd ) );
345
346     return VLC_SUCCESS;
347 }
348
349
350 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
351                                vlc_value_t oldVal, vlc_value_t newVal,
352                                void *pParam )
353 {
354     VlcProc *pThis = (VlcProc*)pParam;
355
356     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
357
358     // Update the stream variable
359     playlist_t *p_playlist = (playlist_t*)pObj;
360     pThis->updateStreamName(p_playlist);
361
362     // Create a playlist notify command
363     // TODO: selective update
364     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
365
366     // Push the command in the asynchronous command queue
367     pQueue->remove( "notify playlist" );
368     pQueue->push( CmdGenericPtr( pCmd ) );
369
370     return VLC_SUCCESS;
371 }
372
373
374 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
375                            vlc_value_t oldVal, vlc_value_t newVal,
376                            void *pParam )
377 {
378     VlcProc *pThis = (VlcProc*)pParam;
379
380     // Create a playlist notify command
381     CmdChangeSkin *pCmd =
382         new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
383
384     // Push the command in the asynchronous command queue
385     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
386     pQueue->remove( "change skin" );
387     pQueue->push( CmdGenericPtr( pCmd ) );
388
389     return VLC_SUCCESS;
390 }
391
392
393 void VlcProc::updateStreamName( playlist_t *p_playlist )
394 {
395     if( p_playlist->p_input )
396     {
397         VarText &rStreamName = getStreamNameVar();
398         VarText &rStreamURI = getStreamURIVar();
399         // XXX: we should not need to access p_input->psz_source directly, a
400         // getter should be provided by VLC core
401         string name = p_playlist->p_input->input.p_item->psz_name;
402         // XXX: This should be done in VLC core, not here...
403         // Remove path information if any
404         OSFactory *pFactory = OSFactory::instance( getIntf() );
405         string::size_type pos = name.rfind( pFactory->getDirSeparator() );
406         if( pos != string::npos )
407         {
408             name = name.substr( pos + 1, name.size() - pos + 1 );
409         }
410         UString srcName( getIntf(), name.c_str() );
411         UString srcURI( getIntf(),
412                          p_playlist->p_input->input.p_item->psz_uri );
413
414         // Create commands to update the stream variables
415         CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
416         CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
417         // Push the commands in the asynchronous command queue
418         AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
419         pQueue->push( CmdGenericPtr( pCmd1 ) );
420         pQueue->push( CmdGenericPtr( pCmd2 ) );
421     }
422 }
423
424
425 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
426                           int *pXHint, int *pYHint,
427                           unsigned int *pWidthHint,
428                           unsigned int *pHeightHint )
429 {
430     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
431     pThis->m_pVout = pVout;
432     if( pThis->m_handleSet.empty() )
433     {
434         return NULL;
435     }
436     else
437     {
438         return *pThis->m_handleSet.begin();
439     }
440 }
441
442
443 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
444 {
445     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
446     pThis->m_pVout = NULL;
447 }
448
449
450 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
451                             int query, va_list args )
452 {
453     return VLC_SUCCESS;
454 }
455