]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/vlcproc.cpp
* modules/gui/skins2/parser/interpreter.cpp: Added the "vlc.ontop()" action
[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 "../commands/async_queue.hpp"
33 #include "../commands/cmd_change_skin.hpp"
34 #include "../commands/cmd_quit.hpp"
35 #include "../commands/cmd_vars.hpp"
36 #include "../utils/var_bool.hpp"
37
38
39 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
40 {
41     if( pIntf->p_sys->p_vlcProc == NULL )
42     {
43         pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
44     }
45
46     return pIntf->p_sys->p_vlcProc;
47 }
48
49
50 void VlcProc::destroy( intf_thread_t *pIntf )
51 {
52     if( pIntf->p_sys->p_vlcProc )
53     {
54         delete pIntf->p_sys->p_vlcProc;
55         pIntf->p_sys->p_vlcProc = NULL;
56     }
57 }
58
59
60 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ),
61                   m_pVoutWindow( NULL ), m_pVout( NULL )
62 {
63     // Create a timer to poll the status of the vlc
64     OSFactory *pOsFactory = OSFactory::instance( pIntf );
65     m_pTimer = pOsFactory->createOSTimer( Callback( this, &doManage ) );
66     m_pTimer->start( 100, false );
67
68     // Create and register VLC variables
69     VarManager *pVarManager = VarManager::instance( getIntf() );
70
71 #define REGISTER_VAR( var, type, name ) \
72     var = VariablePtr( new type( getIntf() ) ); \
73     pVarManager->registerVar( var, name );
74     REGISTER_VAR( m_cPlaylist, Playlist, "playlist" )
75     pVarManager->registerVar( getPlaylistVar().getPositionVarPtr(),
76                               "playlist.slider" );
77     REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
78     REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
79     REGISTER_VAR( m_cVarTime, StreamTime, "time" )
80     REGISTER_VAR( m_cVarVolume, Volume, "volume" )
81     REGISTER_VAR( m_cVarStream, Stream, "stream" )
82     REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
83     REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
84     REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
85     REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
86     REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
87 #undef REGISTER_VAR
88
89     // XXX WARNING XXX
90     // The object variable callbacks are called from other VLC threads,
91     // so they must put commands in the queue and NOT do anything else
92     // (X11 calls are not reentrant)
93
94     // Called when the playlist changes
95     var_AddCallback( pIntf->p_sys->p_playlist, "intf-change",
96                      onIntfChange, this );
97     // Called when the current played item changes
98     var_AddCallback( pIntf->p_sys->p_playlist, "playlist-current",
99                      onPlaylistChange, this );
100     // Called when a playlist item changed
101     var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
102                      onItemChange, this );
103     // Called when our skins2 demux wants us to load a new skin
104     var_AddCallback( pIntf, "skin-to-load", onSkinToLoad, this );
105
106     // Callbacks for vout requests
107     getIntf()->pf_request_window = &getWindow;
108     getIntf()->pf_release_window = &releaseWindow;
109     getIntf()->pf_control_window = &controlWindow;
110
111     getIntf()->p_sys->p_input = NULL;
112 }
113
114
115 VlcProc::~VlcProc()
116 {
117     m_pTimer->stop();
118     delete( m_pTimer );
119     if( getIntf()->p_sys->p_input )
120     {
121         vlc_object_release( getIntf()->p_sys->p_input );
122     }
123
124     // Callbacks for vout requests
125     getIntf()->pf_request_window = NULL;
126     getIntf()->pf_release_window = NULL;
127     getIntf()->pf_control_window = NULL;
128
129     var_DelCallback( getIntf()->p_sys->p_playlist, "intf-change",
130                      onIntfChange, this );
131     var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-current",
132                      onPlaylistChange, this );
133     var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
134                      onItemChange, this );
135 }
136
137
138 void VlcProc::setVoutWindow( void *pVoutWindow )
139 {
140     m_pVoutWindow = pVoutWindow;
141     // Reparent the vout window
142     if( m_pVout )
143     {
144         if( vout_Control( m_pVout, VOUT_REPARENT ) != VLC_SUCCESS )
145             vout_Control( m_pVout, VOUT_CLOSE );
146     }
147 }
148
149
150 void VlcProc::manage()
151 {
152     // Did the user requested to quit vlc ?
153     if( getIntf()->b_die || getIntf()->p_vlc->b_die )
154     {
155         CmdQuit *pCmd = new CmdQuit( getIntf() );
156         AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
157         pQueue->push( CmdGenericPtr( pCmd ) );
158     }
159
160     // Get the VLC variables
161     StreamTime *pTime = (StreamTime*)m_cVarTime.get();
162     Volume *pVolume = (Volume*)m_cVarVolume.get();
163     VarBoolImpl *pVarPlaying = (VarBoolImpl*)m_cVarPlaying.get();
164     VarBoolImpl *pVarStopped = (VarBoolImpl*)m_cVarStopped.get();
165     VarBoolImpl *pVarPaused = (VarBoolImpl*)m_cVarPaused.get();
166     VarBoolImpl *pVarSeekable = (VarBoolImpl*)m_cVarSeekable.get();
167     VarBoolImpl *pVarMute = (VarBoolImpl*)m_cVarMute.get();
168     VarBoolImpl *pVarRandom = (VarBoolImpl*)m_cVarRandom.get();
169     VarBoolImpl *pVarLoop = (VarBoolImpl*)m_cVarLoop.get();
170
171     // Refresh sound volume
172     audio_volume_t volume;
173     aout_VolumeGet( getIntf(), &volume );
174     pVolume->set( (double)volume / AOUT_VOLUME_MAX );
175     // Set the mute variable
176     pVarMute->set( volume == 0 );
177
178     // Update the input
179     if( getIntf()->p_sys->p_input == NULL )
180     {
181         getIntf()->p_sys->p_input = (input_thread_t *)vlc_object_find(
182             getIntf(), VLC_OBJECT_INPUT, FIND_ANYWHERE );
183     }
184     else if( getIntf()->p_sys->p_input->b_dead )
185     {
186         vlc_object_release( getIntf()->p_sys->p_input );
187         getIntf()->p_sys->p_input = NULL;
188     }
189
190     input_thread_t *pInput = getIntf()->p_sys->p_input;
191
192     if( pInput && !pInput->b_die )
193     {
194         // Refresh time variables
195         vlc_value_t pos;
196         var_Get( pInput, "position", &pos );
197         pTime->set( pos.f_float, false );
198
199         // Get the status of the playlist
200         playlist_status_t status = getIntf()->p_sys->p_playlist->i_status;
201
202         pVarPlaying->set( status == PLAYLIST_RUNNING );
203         pVarStopped->set( status == PLAYLIST_STOPPED );
204         pVarPaused->set( status == PLAYLIST_PAUSED );
205
206         pVarSeekable->set( pos.f_float != 0.0 );
207     }
208     else
209     {
210         pVarPlaying->set( false );
211         pVarPaused->set( false );
212         pVarStopped->set( true );
213         pVarSeekable->set( false );
214         pTime->set( 0, false );
215     }
216
217     // Refresh the random variable
218     vlc_value_t val;
219     var_Get( getIntf()->p_sys->p_playlist, "random", &val );
220     pVarRandom->set( val.b_bool );
221
222     // Refresh the loop variable
223     var_Get( getIntf()->p_sys->p_playlist, "loop", &val );
224     pVarLoop->set( val.b_bool );
225 }
226
227
228 void VlcProc::doManage( SkinObject *pObj )
229 {
230     VlcProc *pThis = (VlcProc*)pObj;
231     pThis->manage();
232 }
233
234
235 int VlcProc::onIntfChange( vlc_object_t *pObj, const char *pVariable,
236                            vlc_value_t oldVal, vlc_value_t newVal,
237                            void *pParam )
238 {
239     VlcProc *pThis = ( VlcProc* )pParam;
240
241     // Create a playlist notify command
242     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
243
244     // Push the command in the asynchronous command queue
245     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
246     pQueue->remove( "notify playlist" );
247     pQueue->push( CmdGenericPtr( pCmd ) );
248
249     return VLC_SUCCESS;
250 }
251
252
253 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
254                            vlc_value_t oldVal, vlc_value_t newVal,
255                            void *pParam )
256 {
257     VlcProc *pThis = ( VlcProc* )pParam;
258
259     // Create a playlist notify command
260     // TODO: selective update
261     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
262
263     // Push the command in the asynchronous command queue
264     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
265     pQueue->remove( "notify playlist" );
266     pQueue->push( CmdGenericPtr( pCmd ) );
267
268     return VLC_SUCCESS;
269 }
270
271
272 int VlcProc::onPlaylistChange( vlc_object_t *pObj, const char *pVariable,
273                                vlc_value_t oldVal, vlc_value_t newVal,
274                                void *pParam )
275 {
276     VlcProc *pThis = ( VlcProc* )pParam;
277
278     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
279
280     playlist_t *p_playlist = (playlist_t*)pObj;
281     if( p_playlist->p_input )
282     {
283         // Create a command to update the stream variable
284         // XXX: we should not need to access p_input->psz_source directly, a
285         // getter should be provided by VLC core
286         Stream *pStream = (Stream*)pThis->m_cVarStream.get();
287 #warning "FIXME!"
288         UString srcName( pThis->getIntf(),
289                          p_playlist->p_input->input.p_item->psz_uri );
290         CmdSetStream *pCmd = new CmdSetStream( pThis->getIntf(), *pStream,
291                                                srcName, false );
292         // Push the command in the asynchronous command queue
293         pQueue->remove( "set stream" );
294         pQueue->push( CmdGenericPtr( pCmd ) );
295     }
296
297     // Create a playlist notify command
298     // TODO: selective update
299     CmdNotifyPlaylist *pCmd = new CmdNotifyPlaylist( pThis->getIntf() );
300
301     // Push the command in the asynchronous command queue
302     pQueue->remove( "notify playlist" );
303     pQueue->push( CmdGenericPtr( pCmd ) );
304
305     return VLC_SUCCESS;
306 }
307
308
309 int VlcProc::onSkinToLoad( vlc_object_t *pObj, const char *pVariable,
310                            vlc_value_t oldVal, vlc_value_t newVal,
311                            void *pParam )
312 {
313     VlcProc *pThis = ( VlcProc* )pParam;
314
315     // Create a playlist notify command
316     CmdChangeSkin *pCmd =
317         new CmdChangeSkin( pThis->getIntf(), newVal.psz_string );
318
319     // Push the command in the asynchronous command queue
320     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
321     pQueue->remove( "change skin" );
322     pQueue->push( CmdGenericPtr( pCmd ) );
323
324     return VLC_SUCCESS;
325 }
326
327
328 void *VlcProc::getWindow( intf_thread_t *pIntf, vout_thread_t *pVout,
329                           int *pXHint, int *pYHint,
330                           unsigned int *pWidthHint,
331                           unsigned int *pHeightHint )
332 {
333     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
334     pThis->m_pVout = pVout;
335     return pThis->m_pVoutWindow;
336 }
337
338
339 void VlcProc::releaseWindow( intf_thread_t *pIntf, void *pWindow )
340 {
341     VlcProc *pThis = pIntf->p_sys->p_vlcProc;
342     pThis->m_pVout = NULL;
343 }
344
345
346 int VlcProc::controlWindow( intf_thread_t *pIntf, void *pWindow,
347                             int query, va_list args )
348 {
349     return VLC_SUCCESS;
350 }
351