]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/vlcproc.cpp
Remove bogus executable permissions
[vlc] / modules / gui / skins2 / src / vlcproc.cpp
1 /*****************************************************************************
2  * vlcproc.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
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 ),
64                   m_pVoutWindow( NULL ), m_pVout( NULL )
65 {
66     // Create a timer to poll the status of the vlc
67     OSFactory *pOsFactory = OSFactory::instance( pIntf );
68     m_pTimer = pOsFactory->createOSTimer( Callback( this, &doManage ) );
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::setVoutWindow( void *pVoutWindow )
164 {
165     m_pVoutWindow = 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::dropVout()
176 {
177     if( m_pVout )
178     {
179         if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
180             vout_Control( m_pVout, VOUT_CLOSE );
181         m_pVout = NULL;
182     }
183 }
184
185
186 void VlcProc::manage()
187 {
188     // Did the user requested to quit vlc ?
189     if( getIntf()->b_die || getIntf()->p_vlc->b_die )
190     {
191         CmdQuit *pCmd = new CmdQuit( getIntf() );
192         AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
193         pQueue->push( CmdGenericPtr( pCmd ) );
194     }
195
196     // Get the VLC variables
197     StreamTime *pTime = (StreamTime*)m_cVarTime.get();
198     Volume *pVolume = (Volume*)m_cVarVolume.get();
199     VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
200     VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
201     VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
202     VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
203     VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
204     VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
205     VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
206     VarBoolImpl *pVarRepeat = (VarBoolImpl*)m_cVarRepeat.get();
207
208     // Refresh sound volume
209     audio_volume_t volume;
210     aout_VolumeGet( getIntf(), &volume );
211     pVolume->set( (double)volume / AOUT_VOLUME_MAX );
212     // Set the mute variable
213     pVarMute->set( volume == 0 );
214
215     // Update the input
216     if( getIntf()->p_sys->p_input == NULL )
217     {
218         getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
219             getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
220     }
221     else if( getIntf()->p_sys->p_input->b_dead )
222     {
223         vlc_object_release( getIntf()->p_sys->p_input );
224         getIntf()->p_sys->p_input = NULL;
225     }
226
227     input_thread_t *pInput = getIntf()->p_sys->p_input;
228
229     if( pInput && !pInput->b_die )
230     {
231         // Refresh time variables
232         vlc_value_t pos;
233         var_Get( pInput, "position", &pos );
234         pTime->set( pos.f_float, false );
235
236         // Get the status of the playlist
237         playlist_status_t status =
238             getIntf()->p_sys->p_playlist->status.i_status;
239
240         pVarPlaying->set( status == PLAYLIST_RUNNING );
241         pVarStopped->set( status == PLAYLIST_STOPPED );
242         pVarPaused->set( status == PLAYLIST_PAUSED );
243
244         pVarSeekable->set( pos.f_float != 0.0 );
245     }
246     else
247     {
248         pVarPlaying->set( false );
249         pVarPaused->set( false );
250         pVarStopped->set( true );
251         pVarSeekable->set( false );
252         pTime->set( 0, false );
253     }
254
255     // Refresh the random variable
256     vlc_value_t val;
257     var_Get( getIntf()->p_sys->p_playlist, "random", &val );
258     pVarRandom->set( val.b_bool );
259
260     // Refresh the loop variable
261     var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
262     pVarLoop->set( val.b_bool );
263
264     // Refresh the repeat variable
265     var_Get( getIntf()->p_sys->p_playlist, "repeat", &val );
266     pVarRepeat->set( val.b_bool );
267 }
268
269
270 void VlcProc::doManage( SkinObject *pObj )
271 {
272     VlcProc *pThis = (VlcProc*)pObj;
273     pThis->manage();
274 }
275
276
277 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
278                            vlc_value_t oldVal, vlc_value_t newVal,
279                            void *pParam )
280 {
281     VlcProc *pThis = (VlcProc*)pParam;
282
283     // Update the stream variable
284     playlist_t *p_playlist = (playlist_t*)pObj;
285     pThis->updateStreamName(p_playlist);
286
287     // Create a playlist notify command
288     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
289
290     // Push the command in the asynchronous command queue
291     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
292     pQueue->remove( "notify playlist" );
293     pQueue->push( CmdGenericPtr( pCmd ) );
294
295     return VLC_SUCCESS;
296 }
297
298
299 int VlcProc::onIntfShow( vlc_object_t *pObj, const char *pVariable,
300                          vlc_value_t oldVal, vlc_value_t newVal,
301                          void *pParam )
302 {
303     if (newVal.i_int)
304     {
305         VlcProc *pThis = (VlcProc*)pParam;
306
307         // Create a raise all command
308         CmdRaiseAll *pCmd = new CmdRaiseAll( pThis->getIntf(),
309             pThis->getIntf()->p_sys->p_theme->getWindowManager() );
310
311         // Push the command in the asynchronous command queue
312         AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
313         pQueue->remove( "raise all windows" );
314         pQueue->push( CmdGenericPtr( pCmd ) );
315     }
316
317     return VLC_SUCCESS;
318 }
319
320
321 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
322                            vlc_value_t oldVal, vlc_value_t newVal,
323                            void *pParam )
324 {
325     VlcProc *pThis = (VlcProc*)pParam;
326
327     // Update the stream variable
328     playlist_t *p_playlist = (playlist_t*)pObj;
329     pThis->updateStreamName(p_playlist);
330
331     // Create a playlist notify command
332     // TODO: selective update
333     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
334
335     // Push the command in the asynchronous command queue
336     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
337     pQueue->remove( "notify playlist" );
338     pQueue->push( CmdGenericPtr( pCmd ) );
339
340     return VLC_SUCCESS;
341 }
342
343
344 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
345                                vlc_value_t oldVal, vlc_value_t newVal,
346                                void *pParam )
347 {
348     VlcProc *pThis = (VlcProc*)pParam;
349
350     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
351
352     // Update the stream variable
353     playlist_t *p_playlist = (playlist_t*)pObj;
354     pThis->updateStreamName(p_playlist);
355
356     // Create a playlist notify command
357     // TODO: selective update
358     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
359
360     // Push the command in the asynchronous command queue
361     pQueue->remove( "notify playlist" );
362     pQueue->push( CmdGenericPtr( pCmd ) );
363
364     return VLC_SUCCESS;
365 }
366
367
368 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
369                            vlc_value_t oldVal, vlc_value_t newVal,
370                            void *pParam )
371 {
372     VlcProc *pThis = (VlcProc*)pParam;
373
374     // Create a playlist notify command
375     CmdChangeSkin *pCmd =
376         new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
377
378     // Push the command in the asynchronous command queue
379     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
380     pQueue->remove( "change skin" );
381     pQueue->push( CmdGenericPtr( pCmd ) );
382
383     return VLC_SUCCESS;
384 }
385
386
387 void VlcProc::updateStreamName( playlist_t *p_playlist )
388 {
389     if( p_playlist->p_input )
390     {
391         VarText &rStreamName = getStreamNameVar();
392         VarText &rStreamURI = getStreamURIVar();
393         // XXX: we should not need to access p_input->psz_source directly, a
394         // getter should be provided by VLC core
395         string name = p_playlist->p_input->input.p_item->psz_name;
396         // XXX: This should be done in VLC core, not here...
397         // Remove path information if any
398         OSFactory *pFactory = OSFactory::instance( getIntf() );
399         string::size_type pos = name.rfind( pFactory->getDirSeparator() );
400         if( pos != string::npos )
401         {
402             name = name.substr( pos + 1, name.size() - pos + 1 );
403         }
404         UString srcName( getIntf(), name.c_str() );
405         UString srcURI( getIntf(),
406                          p_playlist->p_input->input.p_item->psz_uri );
407
408         // Create commands to update the stream variables
409         CmdSetText *pCmd1 = new CmdSetText( getIntf(), rStreamName, srcName );
410         CmdSetText *pCmd2 = new CmdSetText( getIntf(), rStreamURI, srcURI );
411         // Push the commands in the asynchronous command queue
412         AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
413         pQueue->push( CmdGenericPtr( pCmd1 ) );
414         pQueue->push( CmdGenericPtr( pCmd2 ) );
415     }
416 }
417
418
419 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
420                           int *pXHint, int *pYHint,
421                           unsigned int *pWidthHint,
422                           unsigned int *pHeightHint )
423 {
424     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
425     pThis->m_pVout = pVout;
426     return pThis->m_pVoutWindow;
427 }
428
429
430 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
431 {
432     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
433     pThis->m_pVout = NULL;
434 }
435
436
437 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
438                             int query, va_list args )
439 {
440     return VLC_SUCCESS;
441 }
442