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