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