Handle NULL value in "input-current" callback
[vlc.git] / modules / gui / skins2 / src / vlcproc.cpp
1 /*****************************************************************************
2  * vlcproc.cpp
3  *****************************************************************************
4  * Copyright (C) 2003-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *          Erwan Tulou      <erwan10@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_aout.h>
32 #include <vlc_vout.h>
33 #include <vlc_playlist.h>
34 #include <vlc_url.h>
35 #include <vlc_strings.h>
36
37 #include "vlcproc.hpp"
38 #include "os_factory.hpp"
39 #include "os_loop.hpp"
40 #include "os_timer.hpp"
41 #include "var_manager.hpp"
42 #include "vout_manager.hpp"
43 #include "fsc_window.hpp"
44 #include "theme.hpp"
45 #include "window_manager.hpp"
46 #include "../commands/async_queue.hpp"
47 #include "../commands/cmd_change_skin.hpp"
48 #include "../commands/cmd_show_window.hpp"
49 #include "../commands/cmd_quit.hpp"
50 #include "../commands/cmd_resize.hpp"
51 #include "../commands/cmd_vars.hpp"
52 #include "../commands/cmd_dialogs.hpp"
53 #include "../commands/cmd_audio.hpp"
54 #include "../commands/cmd_callbacks.hpp"
55 #include "../utils/var_bool.hpp"
56 #include "../utils/var_string.hpp"
57 #include <sstream>
58
59 #include <assert.h>
60
61 VlcProc *VlcProc::instance( intf_thread_t *pIntf )
62 {
63     if( pIntf->p_sys->p_vlcProc == NULL )
64     {
65         pIntf->p_sys->p_vlcProc = new VlcProc( pIntf );
66     }
67
68     return pIntf->p_sys->p_vlcProc;
69 }
70
71
72 void VlcProc::destroy( intf_thread_t *pIntf )
73 {
74     delete pIntf->p_sys->p_vlcProc;
75     pIntf->p_sys->p_vlcProc = NULL;
76 }
77
78 #define SET_BOOL(m,v)         ((VarBoolImpl*)(m).get())->set(v)
79 #define SET_STREAMTIME(m,v,b) ((StreamTime*)(m).get())->set(v,b)
80 #define SET_TEXT(m,v)         ((VarText*)(m).get())->set(v)
81 #define SET_STRING(m,v)       ((VarString*)(m).get())->set(v)
82 #define SET_VOLUME(m,v,b)     ((Volume*)(m).get())->setVolume(v,b)
83
84 VlcProc::VlcProc( intf_thread_t *pIntf ): SkinObject( pIntf ),
85     m_varEqBands( pIntf ), m_pVout( NULL )
86 {
87     // Create and register VLC variables
88     VarManager *pVarManager = VarManager::instance( getIntf() );
89
90 #define REGISTER_VAR( var, type, name ) \
91     var = VariablePtr( new type( getIntf() ) ); \
92     pVarManager->registerVar( var, name );
93     REGISTER_VAR( m_cVarRandom, VarBoolImpl, "playlist.isRandom" )
94     REGISTER_VAR( m_cVarLoop, VarBoolImpl, "playlist.isLoop" )
95     REGISTER_VAR( m_cVarRepeat, VarBoolImpl, "playlist.isRepeat" )
96     REGISTER_VAR( m_cPlaytree, Playtree, "playtree" )
97     pVarManager->registerVar( getPlaytreeVar().getPositionVarPtr(),
98                               "playtree.slider" );
99     pVarManager->registerVar( m_cVarRandom, "playtree.isRandom" );
100     pVarManager->registerVar( m_cVarLoop, "playtree.isLoop" );
101
102     REGISTER_VAR( m_cVarPlaying, VarBoolImpl, "vlc.isPlaying" )
103     REGISTER_VAR( m_cVarStopped, VarBoolImpl, "vlc.isStopped" )
104     REGISTER_VAR( m_cVarPaused, VarBoolImpl, "vlc.isPaused" )
105
106     /* Input variables */
107     pVarManager->registerVar( m_cVarRepeat, "playtree.isRepeat" );
108     REGISTER_VAR( m_cVarTime, StreamTime, "time" )
109     REGISTER_VAR( m_cVarSeekable, VarBoolImpl, "vlc.isSeekable" )
110     REGISTER_VAR( m_cVarDvdActive, VarBoolImpl, "dvd.isActive" )
111
112     REGISTER_VAR( m_cVarRecordable, VarBoolImpl, "vlc.canRecord" )
113     REGISTER_VAR( m_cVarRecording, VarBoolImpl, "vlc.isRecording" )
114
115     /* Vout variables */
116     REGISTER_VAR( m_cVarFullscreen, VarBoolImpl, "vlc.isFullscreen" )
117     REGISTER_VAR( m_cVarHasVout, VarBoolImpl, "vlc.hasVout" )
118
119     /* Aout variables */
120     REGISTER_VAR( m_cVarHasAudio, VarBoolImpl, "vlc.hasAudio" )
121     REGISTER_VAR( m_cVarVolume, Volume, "volume" )
122     REGISTER_VAR( m_cVarMute, VarBoolImpl, "vlc.isMute" )
123     REGISTER_VAR( m_cVarEqualizer, VarBoolImpl, "equalizer.isEnabled" )
124     REGISTER_VAR( m_cVarEqPreamp, EqualizerPreamp, "equalizer.preamp" )
125
126 #undef REGISTER_VAR
127     m_cVarSpeed = VariablePtr( new VarText( getIntf(), false ) );
128     pVarManager->registerVar( m_cVarSpeed, "speed" );
129     SET_TEXT( m_cVarSpeed, UString( getIntf(), "1") );
130     m_cVarStreamName = VariablePtr( new VarText( getIntf(), false ) );
131     pVarManager->registerVar( m_cVarStreamName, "streamName" );
132     m_cVarStreamURI = VariablePtr( new VarText( getIntf(), false ) );
133     pVarManager->registerVar( m_cVarStreamURI, "streamURI" );
134     m_cVarStreamBitRate = VariablePtr( new VarText( getIntf(), false ) );
135     pVarManager->registerVar( m_cVarStreamBitRate, "bitrate" );
136     m_cVarStreamSampleRate = VariablePtr( new VarText( getIntf(), false ) );
137     pVarManager->registerVar( m_cVarStreamSampleRate, "samplerate" );
138     m_cVarStreamArt = VariablePtr( new VarString( getIntf() ) );
139     pVarManager->registerVar( m_cVarStreamArt, "streamArt" );
140
141     // Register the equalizer bands
142     for( int i = 0; i < EqualizerBands::kNbBands; i++)
143     {
144         stringstream ss;
145         ss << "equalizer.band(" << i << ")";
146         pVarManager->registerVar( m_varEqBands.getBand( i ), ss.str() );
147     }
148
149     // XXX WARNING XXX
150     // The object variable callbacks are called from other VLC threads,
151     // so they must put commands in the queue and NOT do anything else
152     // (X11 calls are not reentrant)
153
154 #define ADD_CALLBACK( p_object, var ) \
155     var_AddCallback( p_object, var, onGenericCallback, this );
156
157     ADD_CALLBACK( pIntf->p_sys->p_playlist, "volume" )
158     ADD_CALLBACK( pIntf->p_sys->p_playlist, "mute" )
159     ADD_CALLBACK( pIntf->p_libvlc, "intf-toggle-fscontrol" )
160
161     ADD_CALLBACK( pIntf->p_sys->p_playlist, "random" )
162     ADD_CALLBACK( pIntf->p_sys->p_playlist, "loop" )
163     ADD_CALLBACK( pIntf->p_sys->p_playlist, "repeat" )
164
165 #undef ADD_CALLBACK
166
167     // Called when a playlist item is added
168     var_AddCallback( pIntf->p_sys->p_playlist, "playlist-item-append",
169                      onItemAppend, this );
170     // Called when a playlist item is deleted
171     // TODO: properly handle item-deleted
172     var_AddCallback( pIntf->p_sys->p_playlist, "playlist-item-deleted",
173                      onItemDelete, this );
174     // Called when the current input changes
175     var_AddCallback( pIntf->p_sys->p_playlist, "input-current",
176                      onInputNew, this );
177     // Called when a playlist item changed
178     var_AddCallback( pIntf->p_sys->p_playlist, "item-change",
179                      onItemChange, this );
180
181     // Called when we have an interaction dialog to display
182     var_Create( pIntf, "interaction", VLC_VAR_ADDRESS );
183     var_AddCallback( pIntf, "interaction", onInteraction, this );
184
185     // initialize variables refering to liblvc and playlist objects
186     init_variables();
187 }
188
189
190 VlcProc::~VlcProc()
191 {
192     if( m_pVout )
193     {
194         vlc_object_release( m_pVout );
195         m_pVout = NULL;
196     }
197
198     var_DelCallback( getIntf()->p_sys->p_playlist, "volume",
199                      onGenericCallback, this );
200     var_DelCallback( getIntf()->p_sys->p_playlist, "mute",
201                      onGenericCallback, this );
202     var_DelCallback( getIntf()->p_libvlc, "intf-toggle-fscontrol",
203                      onGenericCallback, this );
204
205     var_DelCallback( getIntf()->p_sys->p_playlist, "random",
206                      onGenericCallback, this );
207     var_DelCallback( getIntf()->p_sys->p_playlist, "loop",
208                      onGenericCallback, this );
209     var_DelCallback( getIntf()->p_sys->p_playlist, "repeat",
210                      onGenericCallback, this );
211
212     var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-item-append",
213                      onItemAppend, this );
214     var_DelCallback( getIntf()->p_sys->p_playlist, "playlist-item-deleted",
215                      onItemDelete, this );
216     var_DelCallback( getIntf()->p_sys->p_playlist, "input-current",
217                      onInputNew, this );
218     var_DelCallback( getIntf()->p_sys->p_playlist, "item-change",
219                      onItemChange, this );
220     var_DelCallback( getIntf(), "interaction", onInteraction, this );
221 }
222
223 int VlcProc::onInputNew( vlc_object_t *pObj, const char *pVariable,
224                          vlc_value_t oldval, vlc_value_t newval, void *pParam )
225 {
226     (void)pObj; (void)pVariable; (void)oldval;
227     VlcProc *pThis = (VlcProc*)pParam;
228     input_thread_t *pInput = static_cast<input_thread_t*>(newval.p_address);
229
230     if( pInput != NULL )
231     {
232         var_AddCallback( pInput, "intf-event", onGenericCallback2, pThis );
233         var_AddCallback( pInput, "bit-rate", onGenericCallback, pThis );
234         var_AddCallback( pInput, "sample-rate", onGenericCallback, pThis );
235         var_AddCallback( pInput, "can-record", onGenericCallback, pThis );
236     }
237     return VLC_SUCCESS;
238 }
239
240
241 int VlcProc::onItemChange( vlc_object_t *pObj, const char *pVariable,
242                            vlc_value_t oldval, vlc_value_t newval,
243                            void *pParam )
244 {
245     (void)pObj; (void)pVariable; (void)oldval;
246     VlcProc *pThis = (VlcProc*)pParam;
247     input_item_t *p_item = static_cast<input_item_t*>(newval.p_address);
248
249     // Create a playtree notify command
250     CmdItemUpdate *pCmdTree = new CmdItemUpdate( pThis->getIntf(),
251                                                          p_item );
252
253     // Push the command in the asynchronous command queue
254     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
255     pQueue->push( CmdGenericPtr( pCmdTree ), true );
256
257     return VLC_SUCCESS;
258 }
259
260 int VlcProc::onItemAppend( vlc_object_t *pObj, const char *pVariable,
261                            vlc_value_t oldVal, vlc_value_t newVal,
262                            void *pParam )
263 {
264     (void)pObj; (void)pVariable; (void)oldVal;
265     VlcProc *pThis = (VlcProc*)pParam;
266
267     playlist_add_t *p_add = static_cast<playlist_add_t*>(newVal.p_address);
268     CmdPlaytreeAppend *pCmdTree =
269         new CmdPlaytreeAppend( pThis->getIntf(), p_add );
270
271     // Push the command in the asynchronous command queue
272     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
273     pQueue->push( CmdGenericPtr( pCmdTree ), false );
274
275     return VLC_SUCCESS;
276 }
277
278 int VlcProc::onItemDelete( vlc_object_t *pObj, const char *pVariable,
279                            vlc_value_t oldVal, vlc_value_t newVal,
280                            void *pParam )
281 {
282     (void)pObj; (void)pVariable; (void)oldVal;
283     VlcProc *pThis = (VlcProc*)pParam;
284
285     int i_id = newVal.i_int;
286     CmdPlaytreeDelete *pCmdTree =
287         new CmdPlaytreeDelete( pThis->getIntf(), i_id);
288
289     // Push the command in the asynchronous command queue
290     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
291     pQueue->push( CmdGenericPtr( pCmdTree ), false );
292
293     return VLC_SUCCESS;
294 }
295
296 int VlcProc::onInteraction( vlc_object_t *pObj, const char *pVariable,
297                             vlc_value_t oldVal, vlc_value_t newVal,
298                             void *pParam )
299 {
300     (void)pObj; (void)pVariable; (void)oldVal;
301     VlcProc *pThis = (VlcProc*)pParam;
302     interaction_dialog_t *p_dialog = (interaction_dialog_t *)(newVal.p_address);
303
304     CmdInteraction *pCmd = new CmdInteraction( pThis->getIntf(), p_dialog );
305     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
306     pQueue->push( CmdGenericPtr( pCmd ) );
307     return VLC_SUCCESS;
308 }
309
310 int VlcProc::onEqBandsChange( vlc_object_t *pObj, const char *pVariable,
311                               vlc_value_t oldVal, vlc_value_t newVal,
312                               void *pParam )
313 {
314     (void)pObj; (void)pVariable; (void)oldVal;
315     VlcProc *pThis = (VlcProc*)pParam;
316
317     // Post a set equalizer bands command
318     CmdSetEqBands *pCmd = new CmdSetEqBands( pThis->getIntf(),
319                                              pThis->m_varEqBands,
320                                              newVal.psz_string );
321     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
322     pQueue->push( CmdGenericPtr( pCmd ) );
323
324     return VLC_SUCCESS;
325 }
326
327
328 int VlcProc::onEqPreampChange( vlc_object_t *pObj, const char *pVariable,
329                                vlc_value_t oldVal, vlc_value_t newVal,
330                                void *pParam )
331 {
332     (void)pObj; (void)pVariable; (void)oldVal;
333     VlcProc *pThis = (VlcProc*)pParam;
334     EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)(pThis->m_cVarEqPreamp.get());
335
336     // Post a set preamp command
337     CmdSetEqPreamp *pCmd = new CmdSetEqPreamp( pThis->getIntf(), *pVarPreamp,
338                                               (newVal.f_float + 20.0) / 40.0 );
339     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
340     pQueue->push( CmdGenericPtr( pCmd ) );
341
342     return VLC_SUCCESS;
343 }
344
345
346 int VlcProc::onGenericCallback( vlc_object_t *pObj, const char *pVariable,
347                                 vlc_value_t oldVal, vlc_value_t newVal,
348                                 void *pParam )
349 {
350     (void)oldVal;
351     VlcProc *pThis = (VlcProc*)pParam;
352     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
353
354 #define ADD_CALLBACK_ENTRY( var, func, remove ) \
355     { \
356     if( strcmp( pVariable, var ) == 0 ) \
357     { \
358         string label = var; \
359         CmdGeneric *pCmd = new CmdCallback( pThis->getIntf(), pObj, newVal, \
360                                             &VlcProc::func, label ); \
361         if( pCmd ) \
362             pQueue->push( CmdGenericPtr( pCmd ), remove ); \
363         return VLC_SUCCESS; \
364     } \
365     }
366
367     ADD_CALLBACK_ENTRY( "volume", on_volume_changed, true )
368     ADD_CALLBACK_ENTRY( "mute", on_mute_changed, true )
369
370     ADD_CALLBACK_ENTRY( "bit-rate", on_bit_rate_changed, false )
371     ADD_CALLBACK_ENTRY( "sample-rate", on_sample_rate_changed, false )
372     ADD_CALLBACK_ENTRY( "can-record", on_can_record_changed, false )
373
374     ADD_CALLBACK_ENTRY( "random", on_random_changed, false )
375     ADD_CALLBACK_ENTRY( "loop", on_loop_changed, false )
376     ADD_CALLBACK_ENTRY( "repeat", on_repeat_changed, false )
377
378     ADD_CALLBACK_ENTRY( "audio-filter", on_audio_filter_changed, false )
379
380     ADD_CALLBACK_ENTRY( "intf-toggle-fscontrol", on_intf_show_changed, false )
381
382     ADD_CALLBACK_ENTRY( "mouse-moved", on_mouse_moved_changed, false )
383
384 #undef ADD_CALLBACK_ENTRY
385
386     msg_Err( pThis->getIntf(), "no callback entry for %s", pVariable );
387     return VLC_EGENERIC;
388 }
389
390
391 int VlcProc::onGenericCallback2( vlc_object_t *pObj, const char *pVariable,
392                                  vlc_value_t oldVal, vlc_value_t newVal,
393                                  void *pParam )
394 {
395     (void)oldVal;
396     VlcProc *pThis = (VlcProc*)pParam;
397     AsyncQueue *pQueue = AsyncQueue::instance( pThis->getIntf() );
398
399     /**
400      * For intf-event, commands are labeled based on the value of newVal.
401      *
402      * For some values (e.g position), only keep the latest command
403      * when there are multiple pending commands (remove=true).
404      *
405      * for others, don't discard commands (remove=false)
406      **/
407     if( strcmp( pVariable, "intf-event" ) == 0 )
408     {
409         stringstream label;
410         bool b_remove;
411         switch( newVal.i_int )
412         {
413             case INPUT_EVENT_STATE:
414             case INPUT_EVENT_POSITION:
415             case INPUT_EVENT_RATE:
416             case INPUT_EVENT_ES:
417             case INPUT_EVENT_CHAPTER:
418             case INPUT_EVENT_RECORD:
419                 b_remove = true;
420                 break;
421             case INPUT_EVENT_VOUT:
422             case INPUT_EVENT_AOUT:
423             case INPUT_EVENT_DEAD:
424                 b_remove = false;
425                 break;
426             default:
427                 return VLC_SUCCESS;
428         }
429         label <<  pVariable << "_" << newVal.i_int;
430         CmdGeneric *pCmd = new CmdCallback( pThis->getIntf(), pObj, newVal,
431                                             &VlcProc::on_intf_event_changed,
432                                             label.str() );
433         if( pCmd )
434             pQueue->push( CmdGenericPtr( pCmd ), b_remove );
435
436         return VLC_SUCCESS;
437     }
438
439     msg_Err( pThis->getIntf(), "no callback entry for %s", pVariable );
440     return VLC_EGENERIC;
441 }
442
443
444 void VlcProc::on_intf_event_changed( vlc_object_t* p_obj, vlc_value_t newVal )
445 {
446     input_thread_t* pInput = (input_thread_t*) p_obj;
447
448     assert( getIntf()->p_sys->p_input == NULL || getIntf()->p_sys->p_input == pInput );
449
450     if( !getIntf()->p_sys->p_input )
451     {
452         msg_Dbg( getIntf(), "new input %p detected", pInput );
453
454         getIntf()->p_sys->p_input = pInput;
455         vlc_object_hold( pInput );
456
457         // update global variables pertaining to this input
458         update_current_input();
459
460         // ensure the playtree is also updated
461         // (highlights the new item to be played back)
462         getPlaytreeVar().onUpdateCurrent( true );
463     }
464
465     switch( newVal.i_int )
466     {
467         case INPUT_EVENT_STATE:
468         {
469             int state = var_GetInteger( pInput, "state" );
470             SET_BOOL( m_cVarStopped, false );
471             SET_BOOL( m_cVarPlaying, state != PAUSE_S );
472             SET_BOOL( m_cVarPaused, state == PAUSE_S );
473             break;
474         }
475
476         case INPUT_EVENT_POSITION:
477         {
478             float pos = var_GetFloat( pInput, "position" );
479             SET_STREAMTIME( m_cVarTime, pos, false );
480             SET_BOOL( m_cVarSeekable, pos != 0.0 );
481             break;
482         }
483
484         case INPUT_EVENT_RATE:
485         {
486             float rate = var_GetFloat( pInput, "rate" );
487             char* buffer;
488             if( asprintf( &buffer, "%.3g", rate ) != -1 )
489             {
490                 SET_TEXT( m_cVarSpeed, UString( getIntf(), buffer ) );
491                 free( buffer );
492             }
493             break;
494         }
495
496         case INPUT_EVENT_ES:
497         {
498             // Do we have audio
499             vlc_value_t audio_es;
500             var_Change( pInput, "audio-es", VLC_VAR_CHOICESCOUNT,
501                             &audio_es, NULL );
502             SET_BOOL( m_cVarHasAudio, audio_es.i_int > 0 );
503             break;
504         }
505
506         case INPUT_EVENT_VOUT:
507         {
508             vout_thread_t* pVout = input_GetVout( pInput );
509             SET_BOOL( m_cVarHasVout, pVout != NULL );
510             if( !pVout || pVout == m_pVout )
511             {
512                 // end of input or vout reuse (nothing to do)
513                 if( pVout )
514                     vlc_object_release( pVout );
515                 break;
516             }
517             if( m_pVout )
518             {
519                 // remove previous Vout callbacks
520                 var_DelCallback( m_pVout, "mouse-moved",
521                                  onGenericCallback, this );
522                 vlc_object_release( m_pVout );
523                 m_pVout = NULL;
524             }
525
526             // add new Vout callbackx
527             var_AddCallback( pVout, "mouse-moved",
528                              onGenericCallback, this );
529             m_pVout = pVout;
530             break;
531         }
532
533         case INPUT_EVENT_CHAPTER:
534         {
535             vlc_value_t chapters_count;
536             var_Change( pInput, "chapter", VLC_VAR_CHOICESCOUNT,
537                         &chapters_count, NULL );
538             SET_BOOL( m_cVarDvdActive, chapters_count.i_int > 0 );
539             break;
540         }
541
542         case INPUT_EVENT_RECORD:
543             SET_BOOL( m_cVarRecording, var_GetBool( pInput, "record" ) );
544             break;
545
546         case INPUT_EVENT_DEAD:
547             msg_Dbg( getIntf(), "end of input detected for %p", pInput );
548
549             var_DelCallback( pInput, "intf-event", onGenericCallback2, this );
550             var_DelCallback( pInput, "bit-rate", onGenericCallback, this );
551             var_DelCallback( pInput, "sample-rate", onGenericCallback, this );
552             var_DelCallback( pInput, "can-record" , onGenericCallback, this );
553             vlc_object_release( pInput );
554             getIntf()->p_sys->p_input = NULL;
555             reset_input();
556             break;
557
558         default:
559             break;
560     }
561 }
562
563 void VlcProc::on_bit_rate_changed( vlc_object_t* p_obj, vlc_value_t newVal )
564 {
565     (void)newVal;
566     input_thread_t* pInput = (input_thread_t*) p_obj;
567
568     assert( getIntf()->p_sys->p_input == NULL || getIntf()->p_sys->p_input == pInput );
569
570     int bitrate = var_GetInteger( pInput, "bit-rate" ) / 1000;
571     SET_TEXT( m_cVarStreamBitRate, UString::fromInt( getIntf(), bitrate ) );
572 }
573
574 void VlcProc::on_sample_rate_changed( vlc_object_t* p_obj, vlc_value_t newVal )
575 {
576     (void)newVal;
577     input_thread_t* pInput = (input_thread_t*) p_obj;
578
579     assert( getIntf()->p_sys->p_input == NULL || getIntf()->p_sys->p_input == pInput );
580
581     int sampleRate = var_GetInteger( pInput, "sample-rate" ) / 1000;
582     SET_TEXT( m_cVarStreamSampleRate, UString::fromInt(getIntf(),sampleRate) );
583 }
584
585 void VlcProc::on_can_record_changed( vlc_object_t* p_obj, vlc_value_t newVal )
586 {
587     (void)newVal;
588     input_thread_t* pInput = (input_thread_t*) p_obj;
589
590     assert( getIntf()->p_sys->p_input == NULL || getIntf()->p_sys->p_input == pInput );
591
592     SET_BOOL( m_cVarRecordable, var_GetBool(  pInput, "can-record" ) );
593 }
594
595 void VlcProc::on_random_changed( vlc_object_t* p_obj, vlc_value_t newVal )
596 {
597     (void)newVal;
598     playlist_t* pPlaylist = (playlist_t*) p_obj;
599
600     SET_BOOL( m_cVarRandom, var_GetBool( pPlaylist, "random" ) );
601 }
602
603 void VlcProc::on_loop_changed( vlc_object_t* p_obj, vlc_value_t newVal )
604 {
605     (void)newVal;
606     playlist_t* pPlaylist = (playlist_t*) p_obj;
607
608     SET_BOOL( m_cVarLoop, var_GetBool( pPlaylist, "loop" ) );
609 }
610
611 void VlcProc::on_repeat_changed( vlc_object_t* p_obj, vlc_value_t newVal )
612 {
613     (void)newVal;
614     playlist_t* pPlaylist = (playlist_t*) p_obj;
615
616     SET_BOOL( m_cVarRepeat, var_GetBool( pPlaylist, "repeat" ) );
617 }
618
619 void VlcProc::on_volume_changed( vlc_object_t* p_obj, vlc_value_t newVal )
620 {
621     (void)p_obj; (void)newVal;
622     playlist_t* pPlaylist = getIntf()->p_sys->p_playlist;
623
624     SET_VOLUME( m_cVarVolume, var_GetFloat( pPlaylist, "volume" ), false );
625 }
626
627 void VlcProc::on_mute_changed( vlc_object_t* p_obj, vlc_value_t newVal )
628 {
629     (void)p_obj;
630     SET_BOOL( m_cVarMute, newVal.b_bool );
631 }
632
633 void VlcProc::on_audio_filter_changed( vlc_object_t* p_obj, vlc_value_t newVal )
634 {
635     (void)p_obj;
636     char *pFilters = newVal.psz_string;
637     bool b_equalizer = pFilters && strstr( pFilters, "equalizer" );
638     SET_BOOL( m_cVarEqualizer, b_equalizer );
639 }
640
641 void VlcProc::on_intf_show_changed( vlc_object_t* p_obj, vlc_value_t newVal )
642 {
643     (void)p_obj; (void)newVal;
644     bool b_fullscreen = getFullscreenVar().get();
645
646     if( !b_fullscreen )
647     {
648         if( newVal.b_bool )
649         {
650             // Create a raise all command
651             CmdRaiseAll *pCmd = new CmdRaiseAll( getIntf(),
652                 getIntf()->p_sys->p_theme->getWindowManager() );
653
654             // Push the command in the asynchronous command queue
655             AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
656             pQueue->push( CmdGenericPtr( pCmd ) );
657         }
658     }
659     else
660     {
661         VoutManager* pVoutManager =  VoutManager::instance( getIntf() );
662         FscWindow *pWin = pVoutManager->getFscWindow();
663         if( pWin )
664         {
665             bool b_visible = pWin->getVisibleVar().get();
666             AsyncQueue *pQueue = AsyncQueue::instance( getIntf() );
667
668             if( !b_visible )
669             {
670                CmdShowWindow* pCmd = new CmdShowWindow( getIntf(),
671                              getIntf()->p_sys->p_theme->getWindowManager(),
672                              *pWin );
673                pQueue->push( CmdGenericPtr( pCmd ) );
674             }
675             else
676             {
677                CmdHideWindow* pCmd = new CmdHideWindow( getIntf(),
678                               getIntf()->p_sys->p_theme->getWindowManager(),
679                               *pWin );
680                pQueue->push( CmdGenericPtr( pCmd ) );
681             }
682         }
683     }
684 }
685
686 void VlcProc::on_mouse_moved_changed( vlc_object_t* p_obj, vlc_value_t newVal )
687 {
688     (void)p_obj; (void)newVal;
689     FscWindow* pFscWindow = VoutManager::instance( getIntf() )->getFscWindow();
690     if( pFscWindow )
691         pFscWindow->onMouseMoved();
692 }
693
694 void VlcProc::reset_input()
695 {
696     SET_BOOL( m_cVarSeekable, false );
697     SET_BOOL( m_cVarRecordable, false );
698     SET_BOOL( m_cVarRecording, false );
699     SET_BOOL( m_cVarDvdActive, false );
700     SET_BOOL( m_cVarHasAudio, false );
701     SET_BOOL( m_cVarHasVout, false );
702     SET_BOOL( m_cVarStopped, true );
703     SET_BOOL( m_cVarPlaying, false );
704     SET_BOOL( m_cVarPaused, false );
705
706     SET_STREAMTIME( m_cVarTime, 0, false );
707     SET_TEXT( m_cVarStreamName, UString( getIntf(), "") );
708     SET_TEXT( m_cVarStreamURI, UString( getIntf(), "") );
709     SET_TEXT( m_cVarStreamBitRate, UString( getIntf(), "") );
710     SET_TEXT( m_cVarStreamSampleRate, UString( getIntf(), "") );
711
712     getPlaytreeVar().onUpdateCurrent( false );
713 }
714
715 void VlcProc::init_variables()
716 {
717     playlist_t* pPlaylist = getIntf()->p_sys->p_playlist;
718
719     SET_BOOL( m_cVarRandom, var_GetBool( pPlaylist, "random" ) );
720     SET_BOOL( m_cVarLoop, var_GetBool( pPlaylist, "loop" ) );
721     SET_BOOL( m_cVarRepeat, var_GetBool( pPlaylist, "repeat" ) );
722
723     SET_VOLUME( m_cVarVolume, var_GetFloat( pPlaylist, "volume" ), false );
724     SET_BOOL( m_cVarMute, var_GetBool( pPlaylist, "mute" ) );
725
726     SET_BOOL( m_cVarStopped, true );
727
728     init_equalizer();
729 }
730
731
732 void VlcProc::update_current_input()
733 {
734     input_thread_t* pInput = getIntf()->p_sys->p_input;
735     if( !pInput )
736         return;
737
738     input_item_t *pItem = input_GetItem( pInput );
739     if( pItem )
740     {
741         // Update short name (as defined by --input-title-format)
742         char *psz_fmt = var_InheritString( getIntf(), "input-title-format" );
743         char *psz_name = str_format_meta( pInput, psz_fmt );
744         SET_TEXT( m_cVarStreamName, UString( getIntf(), psz_name ) );
745         free( psz_fmt );
746         free( psz_name );
747
748         // Update local path (if possible) or full uri
749         char *psz_uri = input_item_GetURI( pItem );
750         char *psz_path = make_path( psz_uri );
751         char *psz_save = psz_path ? psz_path : psz_uri;
752         SET_TEXT( m_cVarStreamURI, UString( getIntf(), psz_save ) );
753         free( psz_path );
754         free( psz_uri );
755
756         // Update art uri
757         char *psz_art = input_item_GetArtURL( pItem );
758         SET_STRING( m_cVarStreamArt, string( psz_art ? psz_art : "" ) );
759         free( psz_art );
760     }
761 }
762
763 void VlcProc::init_equalizer()
764 {
765     playlist_t* pPlaylist = getIntf()->p_sys->p_playlist;
766     audio_output_t* pAout = playlist_GetAout( pPlaylist );
767     if( pAout )
768     {
769         if( !var_Type( pAout, "equalizer-bands" ) )
770             var_Create( pAout, "equalizer-bands",
771                         VLC_VAR_STRING | VLC_VAR_DOINHERIT);
772         if( !var_Type( pAout, "equalizer-preamp" ) )
773             var_Create( pAout, "equalizer-preamp",
774                         VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
775
776         // New Aout (addCallbacks)
777         var_AddCallback( pAout, "audio-filter",
778                          onGenericCallback, this );
779         var_AddCallback( pAout, "equalizer-bands",
780                          onEqBandsChange, this );
781         var_AddCallback( pAout, "equalizer-preamp",
782                          onEqPreampChange, this );
783     }
784
785     // is equalizer enabled ?
786     char *pFilters = pAout ?
787                    var_GetNonEmptyString( pAout, "audio-filter" ) :
788                    var_InheritString( getIntf(), "audio-filter" );
789     bool b_equalizer = pFilters && strstr( pFilters, "equalizer" );
790     free( pFilters );
791     SET_BOOL( m_cVarEqualizer, b_equalizer );
792
793     // retrieve initial bands
794     char* bands = pAout ?
795                   var_GetString( pAout, "equalizer-bands" ) :
796                   var_InheritString( getIntf(), "equalizer-bands" );
797     if( bands )
798     {
799         m_varEqBands.set( bands );
800         free( bands );
801     }
802
803     // retrieve initial preamp
804     float preamp = pAout ?
805                    var_GetFloat( pAout, "equalizer-preamp" ) :
806                    var_InheritFloat( getIntf(), "equalizer-preamp" );
807     EqualizerPreamp *pVarPreamp = (EqualizerPreamp*)m_cVarEqPreamp.get();
808     pVarPreamp->set( (preamp + 20.0) / 40.0 );
809
810     if( pAout )
811         vlc_object_release( pAout);
812 }
813
814 void VlcProc::setFullscreenVar( bool b_fullscreen )
815 {
816     SET_BOOL( m_cVarFullscreen, b_fullscreen );
817 }
818
819 #undef  SET_BOOL
820 #undef  SET_STREAMTIME
821 #undef  SET_TEXT
822 #undef  SET_STRING
823 #undef  SET_VOLUME