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