]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/extended_panels.cpp
Qt: Fix protectedness of customEvent
[vlc] / modules / gui / qt4 / components / extended_panels.cpp
1 /*****************************************************************************
2  * extended_panels.cpp : Extended controls panels
3  ****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Antoine Cellerier <dionoea .t videolan d@t org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #define __STDC_FORMAT_MACROS 1
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <QLabel>
32 #include <QVariant>
33 #include <QString>
34 #include <QFont>
35 #include <QGridLayout>
36 #include <QSignalMapper>
37 #include <QComboBox>
38 #include <QTimer>
39 #include <QFileDialog>
40
41 #include "components/extended_panels.hpp"
42 #include "dialogs/preferences.hpp"
43 #include "qt4.hpp"
44 #include "input_manager.hpp"
45 #include "util/qt_dirs.hpp"
46
47 #include "../../audio_filter/equalizer_presets.h"
48 #include <vlc_aout.h>
49 #include <vlc_intf_strings.h>
50 #include <vlc_vout.h>
51 #include <vlc_osd.h>
52 #include <vlc_modules.h>
53
54 #include <vlc_charset.h> /* us_strtod */
55
56 static void ChangeVFiltersString( struct intf_thread_t *p_intf, const char *psz_name, bool b_add );
57
58 #if 0
59 class ConfClickHandler : public QObject
60 {
61 public:
62     ConfClickHandler( intf_thread_t *_p_intf, ExtVideo *_e ) : QObject ( _e ) {
63         e = _e; p_intf = _p_intf;
64     }
65     virtual ~ConfClickHandler() {}
66     bool eventFilter( QObject *obj, QEvent *evt )
67     {
68         if( evt->type() == QEvent::MouseButtonPress )
69         {
70             e->gotoConf( obj );
71             return true;
72         }
73         return false;
74     }
75 private:
76     ExtVideo* e;
77     intf_thread_t *p_intf;
78 };
79 #endif
80
81 const QString ModuleFromWidgetName( QObject *obj )
82 {
83     return obj->objectName().replace( "Enable","" );
84 }
85
86 QString OptionFromWidgetName( QObject *obj )
87 {
88     /* Gruik ? ... nah */
89     QString option = obj->objectName().replace( "Slider", "" )
90                                       .replace( "Combo" , "" )
91                                       .replace( "Dial"  , "" )
92                                       .replace( "Check" , "" )
93                                       .replace( "Spin"  , "" )
94                                       .replace( "Text"  , "" );
95     for( char a = 'A'; a <= 'Z'; a++ )
96     {
97         option = option.replace( QString( a ),
98                                  QString( '-' ) + QString( a + 'a' - 'A' ) );
99     }
100     return option;
101 }
102
103 ExtVideo::ExtVideo( intf_thread_t *_p_intf, QTabWidget *_parent ) :
104             QObject( _parent ), p_intf( _p_intf )
105 {
106     ui.setupUi( _parent );
107
108 #define SETUP_VFILTER( widget ) \
109     { \
110         vlc_object_t *p_obj = ( vlc_object_t * ) \
111             vlc_object_find_name( p_intf->p_libvlc, \
112                                   #widget ); \
113         QCheckBox *checkbox = qobject_cast<QCheckBox*>( ui.widget##Enable ); \
114         QGroupBox *groupbox = qobject_cast<QGroupBox*>( ui.widget##Enable ); \
115         if( p_obj ) \
116         { \
117             vlc_object_release( p_obj ); \
118             if( checkbox ) checkbox->setChecked( true ); \
119             else groupbox->setChecked( true ); \
120         } \
121         else \
122         { \
123             if( checkbox ) checkbox->setChecked( false ); \
124             else groupbox->setChecked( false ); \
125         } \
126     } \
127     CONNECT( ui.widget##Enable, clicked(), this, updateFilters() );
128 #define SETUP_VFILTER_OPTION( widget, signal ) \
129     initComboBoxItems( ui.widget ); \
130     setWidgetValue( ui.widget ); \
131     CONNECT( ui.widget, signal, this, updateFilterOptions() );
132
133     SETUP_VFILTER( adjust )
134     SETUP_VFILTER_OPTION( hueSlider, valueChanged( int ) )
135     SETUP_VFILTER_OPTION( contrastSlider, valueChanged( int ) )
136     SETUP_VFILTER_OPTION( brightnessSlider, valueChanged( int ) )
137     SETUP_VFILTER_OPTION( saturationSlider, valueChanged( int ) )
138     SETUP_VFILTER_OPTION( gammaSlider, valueChanged( int ) )
139     SETUP_VFILTER_OPTION( brightnessThresholdCheck, stateChanged( int ) )
140
141     SETUP_VFILTER( extract )
142     SETUP_VFILTER_OPTION( extractComponentText, textChanged( const QString& ) )
143
144     SETUP_VFILTER( posterize )
145     SETUP_VFILTER_OPTION( posterizeLevelSpin, valueChanged( int ) )
146
147     SETUP_VFILTER( colorthres )
148     SETUP_VFILTER_OPTION( colorthresColorText, textChanged( const QString& ) )
149     SETUP_VFILTER_OPTION( colorthresSaturationthresSlider, valueChanged( int ) )
150     SETUP_VFILTER_OPTION( colorthresSimilaritythresSlider, valueChanged( int ) )
151
152     SETUP_VFILTER( sepia )
153     SETUP_VFILTER_OPTION( sepiaIntensitySpin, valueChanged( int ) )
154
155     SETUP_VFILTER( invert )
156
157     SETUP_VFILTER( gradient )
158     SETUP_VFILTER_OPTION( gradientModeCombo, currentIndexChanged( QString ) )
159     SETUP_VFILTER_OPTION( gradientTypeCheck, stateChanged( int ) )
160     SETUP_VFILTER_OPTION( gradientCartoonCheck, stateChanged( int ) )
161
162     SETUP_VFILTER( motionblur )
163     SETUP_VFILTER_OPTION( blurFactorSlider, valueChanged( int ) )
164
165     SETUP_VFILTER( motiondetect )
166
167 //    SETUP_VFILTER( noise )
168
169     SETUP_VFILTER( psychedelic )
170
171     SETUP_VFILTER( sharpen )
172     SETUP_VFILTER_OPTION( sharpenSigmaSlider, valueChanged( int ) )
173
174     SETUP_VFILTER( ripple )
175
176     SETUP_VFILTER( wave )
177
178     SETUP_VFILTER( transform )
179     SETUP_VFILTER_OPTION( transformTypeCombo, currentIndexChanged( QString ) )
180
181     SETUP_VFILTER( rotate )
182     SETUP_VFILTER_OPTION( rotateAngleDial, valueChanged( int ) )
183     ui.rotateAngleDial->setWrapping( true );
184     ui.rotateAngleDial->setNotchesVisible( true );
185
186     SETUP_VFILTER( puzzle )
187     SETUP_VFILTER_OPTION( puzzleRowsSpin, valueChanged( int ) )
188     SETUP_VFILTER_OPTION( puzzleColsSpin, valueChanged( int ) )
189     SETUP_VFILTER_OPTION( puzzleBlackSlotCheck, stateChanged( int ) )
190
191     SETUP_VFILTER( magnify )
192
193     SETUP_VFILTER( clone )
194     SETUP_VFILTER_OPTION( cloneCountSpin, valueChanged( int ) )
195
196     SETUP_VFILTER( wall )
197     SETUP_VFILTER_OPTION( wallRowsSpin, valueChanged( int ) )
198     SETUP_VFILTER_OPTION( wallColsSpin, valueChanged( int ) )
199
200     SETUP_VFILTER( panoramix )
201     SETUP_VFILTER_OPTION( panoramixRowsSpin, valueChanged( int ) )
202     SETUP_VFILTER_OPTION( panoramixColsSpin, valueChanged( int ) )
203
204
205     SETUP_VFILTER( erase )
206     SETUP_VFILTER_OPTION( eraseMaskText, editingFinished() )
207     SETUP_VFILTER_OPTION( eraseYSpin, valueChanged( int ) )
208     SETUP_VFILTER_OPTION( eraseXSpin, valueChanged( int ) )
209     BUTTONACT( ui.eraseBrowseBtn, browseEraseFile() );
210
211     SETUP_VFILTER( marq )
212     SETUP_VFILTER_OPTION( marqMarqueeText, textChanged( const QString& ) )
213     SETUP_VFILTER_OPTION( marqPositionCombo, currentIndexChanged( QString ) )
214
215     SETUP_VFILTER( logo )
216     SETUP_VFILTER_OPTION( logoFileText, editingFinished() )
217     SETUP_VFILTER_OPTION( logoYSpin, valueChanged( int ) )
218     SETUP_VFILTER_OPTION( logoXSpin, valueChanged( int ) )
219     SETUP_VFILTER_OPTION( logoOpacitySlider, valueChanged( int ) )
220     BUTTONACT( ui.logoBrowseBtn, browseLogo() );
221
222     SETUP_VFILTER( gradfun )
223     SETUP_VFILTER_OPTION( gradfunRadiusSlider, valueChanged( int ) )
224
225     SETUP_VFILTER( grain )
226     SETUP_VFILTER_OPTION( grainVarianceSlider, valueChanged( int ) )
227
228     if( module_exists( "atmo" ) )
229     {
230         SETUP_VFILTER( atmo )
231         SETUP_VFILTER_OPTION( atmoEdgeweightningSlider, valueChanged( int ) )
232         SETUP_VFILTER_OPTION( atmoBrightnessSlider, valueChanged( int ) )
233         SETUP_VFILTER_OPTION( atmoDarknesslimitSlider, valueChanged( int ) )
234         SETUP_VFILTER_OPTION( atmoMeanlengthSlider, valueChanged( int ) )
235         SETUP_VFILTER_OPTION( atmoMeanthresholdSlider, valueChanged( int ) )
236         SETUP_VFILTER_OPTION( atmoPercentnewSlider, valueChanged( int ) )
237         SETUP_VFILTER_OPTION( atmoFiltermodeCombo, currentIndexChanged( int ) )
238         SETUP_VFILTER_OPTION( atmoShowdotsCheck, stateChanged( int ) )
239     }
240     else
241     {
242         _parent->removeTab( _parent->indexOf( ui.tab_atmo ) );
243     }
244
245 #undef SETUP_VFILTER
246 #undef SETUP_VFILTER_OPTION
247
248     CONNECT( ui.cropTopPx, valueChanged( int ), this, cropChange() );
249     CONNECT( ui.cropBotPx, valueChanged( int ), this, cropChange() );
250     CONNECT( ui.cropLeftPx, valueChanged( int ), this, cropChange() );
251     CONNECT( ui.cropRightPx, valueChanged( int ), this, cropChange() );
252     CONNECT( ui.leftRightCropSync, toggled ( bool ), this, cropChange() );
253     CONNECT( ui.topBotCropSync, toggled ( bool ), this, cropChange() );
254     CONNECT( ui.topBotCropSync, toggled( bool ),
255              ui.cropBotPx, setDisabled( bool ) );
256     CONNECT( ui.leftRightCropSync, toggled( bool ),
257              ui.cropRightPx, setDisabled( bool ) );
258 }
259
260 void ExtVideo::cropChange()
261 {
262     if( ui.topBotCropSync->isChecked() )
263         ui.cropBotPx->setValue( ui.cropTopPx->value() );
264     if( ui.leftRightCropSync->isChecked() )
265         ui.cropRightPx->setValue( ui.cropLeftPx->value() );
266
267     vout_thread_t *p_vout = THEMIM->getVout();
268     if( p_vout )
269     {
270         var_SetInteger( p_vout, "crop-top", ui.cropTopPx->value() );
271         var_SetInteger( p_vout, "crop-bottom", ui.cropBotPx->value() );
272         var_SetInteger( p_vout, "crop-left", ui.cropLeftPx->value() );
273         var_SetInteger( p_vout, "crop-right", ui.cropRightPx->value() );
274         vlc_object_release( p_vout );
275     }
276 }
277
278 void ExtVideo::clean()
279 {
280     ui.cropTopPx->setValue( 0 );
281     ui.cropBotPx->setValue( 0 );
282     ui.cropLeftPx->setValue( 0 );
283     ui.cropRightPx->setValue( 0 );
284 }
285
286 static void ChangeVFiltersString( struct intf_thread_t *p_intf, const char *psz_name, bool b_add )
287 {
288     char *psz_parser, *psz_string;
289     const char *psz_filter_type;
290
291     module_t *p_obj = module_find( psz_name );
292     if( !p_obj )
293     {
294         msg_Err( p_intf, "Unable to find filter module \"%s\".", psz_name );
295         return;
296     }
297
298     if( module_provides( p_obj, "video splitter" ) )
299     {
300         psz_filter_type = "video-splitter";
301     }
302     else if( module_provides( p_obj, "video filter2" ) )
303     {
304         psz_filter_type = "video-filter";
305     }
306     else if( module_provides( p_obj, "sub source" ) )
307     {
308         psz_filter_type = "sub-source";
309     }
310     else if( module_provides( p_obj, "sub filter" ) )
311     {
312         psz_filter_type = "sub-filter";
313     }
314     else
315     {
316         module_release (p_obj);
317         msg_Err( p_intf, "Unknown video filter type." );
318         return;
319     }
320     module_release (p_obj);
321
322     psz_string = config_GetPsz( p_intf, psz_filter_type );
323
324     if( !psz_string ) psz_string = strdup( "" );
325
326     psz_parser = strstr( psz_string, psz_name );
327
328     if( b_add )
329     {
330         if( !psz_parser )
331         {
332             psz_parser = psz_string;
333             if( asprintf( &psz_string, ( *psz_string ) ? "%s:%s" : "%s%s",
334                             psz_string, psz_name ) == -1 )
335             {
336                 free( psz_parser );
337                 return;
338             }
339             free( psz_parser );
340         }
341         else
342         {
343             return;
344         }
345     }
346     else
347     {
348         if( psz_parser )
349         {
350             if( *( psz_parser + strlen( psz_name ) ) == ':' )
351             {
352                 memmove( psz_parser, psz_parser + strlen( psz_name ) + 1,
353                          strlen( psz_parser + strlen( psz_name ) + 1 ) + 1 );
354             }
355             else
356             {
357                 *psz_parser = '\0';
358             }
359
360             /* Remove trailing : : */
361             if( strlen( psz_string ) > 0 &&
362                 *( psz_string + strlen( psz_string ) -1 ) == ':' )
363             {
364                 *( psz_string + strlen( psz_string ) -1 ) = '\0';
365             }
366         }
367         else
368         {
369             free( psz_string );
370             return;
371         }
372     }
373     /* Vout is not kept, so put that in the config */
374     config_PutPsz( p_intf, psz_filter_type, psz_string );
375
376     /* Try to set on the fly */
377     if( !strcmp( psz_filter_type, "video-splitter" ) )
378     {
379         playlist_t *p_playlist = pl_Get( p_intf );
380         var_SetString( p_playlist, psz_filter_type, psz_string );
381     }
382     else
383     {
384         vout_thread_t *p_vout = THEMIM->getVout();
385         if( p_vout )
386         {
387             var_SetString( p_vout, psz_filter_type, psz_string );
388             vlc_object_release( p_vout );
389         }
390     }
391
392     free( psz_string );
393 }
394
395 void ExtVideo::updateFilters()
396 {
397     QString module = ModuleFromWidgetName( sender() );
398
399     QCheckBox *checkbox = qobject_cast<QCheckBox*>( sender() );
400     QGroupBox *groupbox = qobject_cast<QGroupBox*>( sender() );
401
402     ChangeVFiltersString( p_intf, qtu( module ),
403                           checkbox ? checkbox->isChecked()
404                                    : groupbox->isChecked() );
405 }
406
407 void ExtVideo::browseLogo()
408 {
409     QString file = QFileDialog::getOpenFileName( NULL, qtr( "Logo filenames" ),
410                    p_intf->p_sys->filepath, "Images (*.png *.jpg);;All (*)" );
411     ui.logoFileText->setText( toNativeSeparators( file ) );
412 }
413
414 void ExtVideo::browseEraseFile()
415 {
416     QString file = QFileDialog::getOpenFileName( NULL, qtr( "Image mask" ),
417                    p_intf->p_sys->filepath, "Images (*.png *.jpg);;All (*)" );
418     ui.eraseMaskText->setText( toNativeSeparators( file ) );
419 }
420
421 void ExtVideo::initComboBoxItems( QObject *widget )
422 {
423     QComboBox *combobox = qobject_cast<QComboBox*>( widget );
424     if( !combobox ) return;
425
426     QString option = OptionFromWidgetName( widget );
427     module_config_t *p_item = config_FindConfig( VLC_OBJECT( p_intf ),
428                                                  qtu( option ) );
429     if( p_item )
430     {
431         int i_type = p_item->i_type;
432         for( int i_index = 0; i_index < p_item->i_list; i_index++ )
433         {
434             if( i_type == CONFIG_ITEM_INTEGER
435              || i_type == CONFIG_ITEM_BOOL )
436                 combobox->addItem( qtr( p_item->ppsz_list_text[i_index] ),
437                                    p_item->pi_list[i_index] );
438             else if( i_type == CONFIG_ITEM_STRING )
439                 combobox->addItem( qtr( p_item->ppsz_list_text[i_index] ),
440                                    p_item->ppsz_list[i_index] );
441         }
442     }
443     else
444     {
445         msg_Err( p_intf, "Couldn't find option \"%s\".",
446                  qtu( option ) );
447     }
448 }
449
450 void ExtVideo::setWidgetValue( QObject *widget )
451 {
452     QString module = ModuleFromWidgetName( widget->parent() );
453     //std::cout << "Module name: " << module.toStdString() << std::endl;
454     QString option = OptionFromWidgetName( widget );
455     //std::cout << "Option name: " << option.toStdString() << std::endl;
456
457     vlc_object_t *p_obj = ( vlc_object_t * )
458         vlc_object_find_name( p_intf->p_libvlc, qtu( module ) );
459     int i_type;
460     vlc_value_t val;
461
462     if( !p_obj )
463     {
464 #if 0
465         msg_Dbg( p_intf,
466                  "Module instance %s not found, looking in config values.",
467                  qtu( module ) );
468 #endif
469         i_type = config_GetType( p_intf, qtu( option ) ) & VLC_VAR_CLASS;
470         switch( i_type )
471         {
472             case VLC_VAR_INTEGER:
473             case VLC_VAR_BOOL:
474                 val.i_int = config_GetInt( p_intf, qtu( option ) );
475                 break;
476             case VLC_VAR_FLOAT:
477                 val.f_float = config_GetFloat( p_intf, qtu( option ) );
478                 break;
479             case VLC_VAR_STRING:
480                 val.psz_string = config_GetPsz( p_intf, qtu( option ) );
481                 break;
482         }
483     }
484     else
485     {
486         i_type = var_Type( p_obj, qtu( option ) ) & VLC_VAR_CLASS;
487         var_Get( p_obj, qtu( option ), &val );
488         vlc_object_release( p_obj );
489     }
490
491     /* Try to cast to all the widgets we're likely to encounter. Only
492      * one of the casts is expected to work. */
493     QSlider        *slider        = qobject_cast<QSlider*>       ( widget );
494     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( widget );
495     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( widget );
496     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( widget );
497     QDial          *dial          = qobject_cast<QDial*>         ( widget );
498     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( widget );
499     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( widget );
500
501     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
502     {
503         if( slider )        slider->setValue( val.i_int );
504         else if( checkbox ) checkbox->setCheckState( val.i_int? Qt::Checked
505                                                               : Qt::Unchecked );
506         else if( spinbox )  spinbox->setValue( val.i_int );
507         else if( dial )     dial->setValue( ( 540-val.i_int )%360 );
508         else if( lineedit )
509         {
510             char str[30];
511             snprintf( str, sizeof(str), "%06"PRIX64, val.i_int );
512             lineedit->setText( str );
513         }
514         else if( combobox ) combobox->setCurrentIndex(
515                             combobox->findData( qlonglong(val.i_int) ) );
516         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
517     }
518     else if( i_type == VLC_VAR_FLOAT )
519     {
520         if( slider ) slider->setValue( ( int )( val.f_float*( double )slider->tickInterval() ) ); /* hack alert! */
521         else if( doublespinbox ) doublespinbox->setValue( val.f_float );
522         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
523     }
524     else if( i_type == VLC_VAR_STRING )
525     {
526         if( lineedit ) lineedit->setText( qfu( val.psz_string ) );
527         else if( combobox ) combobox->setCurrentIndex(
528                             combobox->findData( qfu( val.psz_string ) ) );
529         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
530         free( val.psz_string );
531     }
532     else
533         if( p_obj )
534             msg_Err( p_intf,
535                      "Module %s's %s variable is of an unsupported type ( %d )",
536                      qtu( module ),
537                      qtu( option ),
538                      i_type );
539 }
540
541 void ExtVideo::updateFilterOptions()
542 {
543     QString module = ModuleFromWidgetName( sender()->parent() );
544     //std::cout << "Module name: " << module.toStdString() << std::endl;
545     QString option = OptionFromWidgetName( sender() );
546     //std::cout << "Option name: " << option.toStdString() << std::endl;
547
548     vlc_object_t *p_obj = ( vlc_object_t * )
549         vlc_object_find_name( p_intf->p_libvlc, qtu( module ) );
550     int i_type;
551     bool b_is_command;
552     if( !p_obj )
553     {
554         msg_Warn( p_intf, "Module %s not found. You'll need to restart the filter to take the change into account.", qtu( module ) );
555         i_type = config_GetType( p_intf, qtu( option ) );
556         b_is_command = false;
557     }
558     else
559     {
560         i_type = var_Type( p_obj, qtu( option ) );
561         if( i_type == 0 )
562             i_type = config_GetType( p_intf, qtu( option ) );
563         b_is_command = ( i_type & VLC_VAR_ISCOMMAND );
564     }
565
566     if( !b_is_command )
567     {
568         msg_Warn( p_intf, "Module %s's %s variable isn't a command. You'll need to restart the filter to take change into account.",
569                  qtu( module ),
570                  qtu( option ) );
571         /* FIXME: restart automatically somewhere near the end of this function */
572     }
573
574     /* Try to cast to all the widgets we're likely to encounter. Only
575      * one of the casts is expected to work. */
576     QSlider        *slider        = qobject_cast<QSlider*>       ( sender() );
577     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( sender() );
578     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( sender() );
579     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( sender() );
580     QDial          *dial          = qobject_cast<QDial*>         ( sender() );
581     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( sender() );
582     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( sender() );
583
584     i_type &= VLC_VAR_CLASS;
585     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
586     {
587         int i_int = 0;
588         if( slider )        i_int = slider->value();
589         else if( checkbox ) i_int = checkbox->checkState() == Qt::Checked;
590         else if( spinbox )  i_int = spinbox->value();
591         else if( dial )     i_int = ( 540-dial->value() )%360;
592         else if( lineedit ) i_int = lineedit->text().toInt( NULL,16 );
593         else if( combobox ) i_int = combobox->itemData( combobox->currentIndex() ).toInt();
594         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
595         config_PutInt( p_intf, qtu( option ), i_int );
596         if( b_is_command )
597         {
598             if( i_type == VLC_VAR_INTEGER )
599                 var_SetInteger( p_obj, qtu( option ), i_int );
600             else
601                 var_SetBool( p_obj, qtu( option ), i_int );
602         }
603     }
604     else if( i_type == VLC_VAR_FLOAT )
605     {
606         double f_float = 0;
607         if( slider )             f_float = ( double )slider->value()
608                                          / ( double )slider->tickInterval(); /* hack alert! */
609         else if( doublespinbox ) f_float = doublespinbox->value();
610         else if( lineedit ) f_float = lineedit->text().toDouble();
611         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
612         config_PutFloat( p_intf, qtu( option ), f_float );
613         if( b_is_command )
614             var_SetFloat( p_obj, qtu( option ), f_float );
615     }
616     else if( i_type == VLC_VAR_STRING )
617     {
618         QString val;
619         if( lineedit )
620             val = lineedit->text();
621         else if( combobox )
622             val = combobox->itemData( combobox->currentIndex() ).toString();
623         else
624             msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
625         config_PutPsz( p_intf, qtu( option ), qtu( val ) );
626         if( b_is_command )
627             var_SetString( p_obj, qtu( option ), qtu( val ) );
628     }
629     else
630         msg_Err( p_intf,
631                  "Module %s's %s variable is of an unsupported type ( %d )",
632                  qtu( module ),
633                  qtu( option ),
634                  i_type );
635
636     if( p_obj ) vlc_object_release( p_obj );
637 }
638
639 #if 0
640 void ExtVideo::gotoConf( QObject* src )
641 {
642 #define SHOWCONF( module ) \
643     if( src->objectName().contains( module ) ) \
644     { \
645         PrefsDialog::getInstance( p_intf )->showModulePrefs( module ); \
646         return; \
647     }
648     SHOWCONF( "clone" );
649     SHOWCONF( "magnify" );
650     SHOWCONF( "wave" );
651     SHOWCONF( "ripple" );
652     SHOWCONF( "invert" );
653     SHOWCONF( "puzzle" );
654     SHOWCONF( "wall" );
655     SHOWCONF( "gradient" );
656     SHOWCONF( "colorthres" )
657 }
658 #endif
659
660 /**********************************************************************
661  * v4l2 controls
662  **********************************************************************/
663
664 ExtV4l2::ExtV4l2( intf_thread_t *_p_intf, QWidget *_parent )
665     : QWidget( _parent ), p_intf( _p_intf ), box( NULL )
666 {
667     QVBoxLayout *layout = new QVBoxLayout( this );
668     help = new QLabel( qtr("No v4l2 instance found.\n"
669       "Please check that the device has been opened with VLC and is playing.\n\n"
670       "Controls will automatically appear here.")
671       , this );
672     help->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
673     layout->addWidget( help );
674     setLayout( layout );
675 }
676
677 void ExtV4l2::showEvent( QShowEvent *event )
678 {
679     QWidget::showEvent( event );
680     Refresh();
681 }
682
683 void ExtV4l2::Refresh( void )
684 {
685     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( pl_Get(p_intf), "v4l2" );
686     help->hide();
687     if( box )
688     {
689         layout()->removeWidget( box );
690         delete box;
691         box = NULL;
692     }
693     if( p_obj )
694     {
695         vlc_value_t val, text, name;
696         int i_ret = var_Change( p_obj, "controls", VLC_VAR_GETCHOICES,
697                                 &val, &text );
698         if( i_ret < 0 )
699         {
700             msg_Err( p_intf, "Oops, v4l2 object doesn't have a 'controls' variable." );
701             help->show();
702             vlc_object_release( p_obj );
703             return;
704         }
705
706         box = new QGroupBox( this );
707         layout()->addWidget( box );
708         QVBoxLayout *layout = new QVBoxLayout( box );
709         box->setLayout( layout );
710
711         for( int i = 0; i < val.p_list->i_count; i++ )
712         {
713             const char *psz_var = text.p_list->p_values[i].psz_string;
714             var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
715             const char *psz_label = name.psz_string;
716             msg_Dbg( p_intf, "v4l2 control \"%"PRIx64"\": %s (%s)",
717                      val.p_list->p_values[i].i_int, psz_var, name.psz_string );
718
719             int i_type = var_Type( p_obj, psz_var );
720             switch( i_type & VLC_VAR_TYPE )
721             {
722                 case VLC_VAR_INTEGER:
723                 {
724                     QLabel *label = new QLabel( qtr( psz_label ), box );
725                     QHBoxLayout *hlayout = new QHBoxLayout();
726                     hlayout->addWidget( label );
727                     int i_val = var_GetInteger( p_obj, psz_var );
728                     if( i_type & VLC_VAR_HASCHOICE )
729                     {
730                         QComboBox *combobox = new QComboBox( box );
731                         combobox->setObjectName( qtr( psz_var ) );
732
733                         vlc_value_t val2, text2;
734                         var_Change( p_obj, psz_var, VLC_VAR_GETCHOICES,
735                                     &val2, &text2 );
736                         for( int j = 0; j < val2.p_list->i_count; j++ )
737                         {
738                             combobox->addItem(
739                                        text2.p_list->p_values[j].psz_string,
740                                        qlonglong( val2.p_list->p_values[j].i_int) );
741                             if( i_val == val2.p_list->p_values[j].i_int )
742                                 combobox->setCurrentIndex( j );
743                         }
744                         var_FreeList( &val2, &text2 );
745
746                         CONNECT( combobox, currentIndexChanged( int ), this,
747                                  ValueChange( int ) );
748                         hlayout->addWidget( combobox );
749                     }
750                     else
751                     {
752                         QSlider *slider = new QSlider( box );
753                         slider->setObjectName( qtr( psz_var ) );
754                         slider->setOrientation( Qt::Horizontal );
755                         vlc_value_t val2;
756                         var_Change( p_obj, psz_var, VLC_VAR_GETMIN,
757                                     &val2, NULL );
758                         slider->setMinimum( val2.i_int );
759                         var_Change( p_obj, psz_var, VLC_VAR_GETMAX,
760                                     &val2, NULL );
761                         slider->setMaximum( val2.i_int );
762                         var_Change( p_obj, psz_var, VLC_VAR_GETSTEP,
763                                     &val2, NULL );
764                         slider->setSingleStep( val2.i_int );
765                         slider->setValue( i_val );
766
767                         CONNECT( slider, valueChanged( int ), this,
768                                  ValueChange( int ) );
769                         hlayout->addWidget( slider );
770                     }
771                     layout->addLayout( hlayout );
772                     break;
773                 }
774                 case VLC_VAR_BOOL:
775                 {
776                     QCheckBox *button = new QCheckBox( qtr( psz_label ), box );
777                     button->setObjectName( qtr( psz_var ) );
778                     button->setChecked( var_GetBool( p_obj, psz_var ) );
779
780                     CONNECT( button, clicked( bool ), this,
781                              ValueChange( bool ) );
782                     layout->addWidget( button );
783                     break;
784                 }
785                 case VLC_VAR_VOID:
786                 {
787                     if( i_type & VLC_VAR_ISCOMMAND )
788                     {
789                         QPushButton *button = new QPushButton( qtr( psz_label ), box );
790                         button->setObjectName( qtr( psz_var ) );
791
792                         CONNECT( button, clicked( bool ), this,
793                                  ValueChange( bool ) );
794                         layout->addWidget( button );
795                     }
796                     else
797                     {
798                         QLabel *label = new QLabel( qtr( psz_label ), box );
799                         layout->addWidget( label );
800                     }
801                     break;
802                 }
803                 default:
804                     msg_Warn( p_intf, "Unhandled var type for %s", psz_var );
805                     break;
806             }
807             free( name.psz_string );
808         }
809         var_FreeList( &val, &text );
810         vlc_object_release( p_obj );
811     }
812     else
813     {
814         msg_Dbg( p_intf, "Couldn't find v4l2 instance" );
815         help->show();
816         if ( isVisible() )
817             QTimer::singleShot( 2000, this, SLOT(Refresh()) );
818     }
819 }
820
821 void ExtV4l2::ValueChange( bool value )
822 {
823     ValueChange( (int)value );
824 }
825
826 void ExtV4l2::ValueChange( int value )
827 {
828     QObject *s = sender();
829     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( pl_Get(p_intf), "v4l2" );
830     if( p_obj )
831     {
832         QString var = s->objectName();
833         int i_type = var_Type( p_obj, qtu( var ) );
834         switch( i_type & VLC_VAR_TYPE )
835         {
836             case VLC_VAR_INTEGER:
837                 if( i_type & VLC_VAR_HASCHOICE )
838                 {
839                     QComboBox *combobox = qobject_cast<QComboBox*>( s );
840                     value = combobox->itemData( value ).toInt();
841                 }
842                 var_SetInteger( p_obj, qtu( var ), value );
843                 break;
844             case VLC_VAR_BOOL:
845                 var_SetBool( p_obj, qtu( var ), value );
846                 break;
847             case VLC_VAR_VOID:
848                 var_TriggerCallback( p_obj, qtu( var ) );
849                 break;
850         }
851         vlc_object_release( p_obj );
852     }
853     else
854     {
855         msg_Warn( p_intf, "Oops, v4l2 object isn't available anymore" );
856         Refresh();
857     }
858 }
859
860 /**********************************************************************
861  * Equalizer
862  **********************************************************************/
863
864 static const QString band_frequencies[] =
865 {
866     "  60 Hz  ", " 170 Hz ", " 310 Hz ", " 600 Hz ", "  1 kHz ",
867     "  3 kHz  ", "  6 kHz ", " 12 kHz ", " 14 kHz ", " 16 kHz "
868 };
869
870 Equalizer::Equalizer( intf_thread_t *_p_intf, QWidget *_parent ) :
871                             QWidget( _parent ) , p_intf( _p_intf )
872 {
873     QFont smallFont = QApplication::font();
874     smallFont.setPointSize( smallFont.pointSize() - 3 );
875
876     ui.setupUi( this );
877     ui.preampLabel->setFont( smallFont );
878
879     /* Setup of presetsComboBox */
880     presetsComboBox = ui.presetsCombo;
881     CONNECT( presetsComboBox, activated( int ), this, setCorePreset( int ) );
882
883     /* Add the sliders for the Bands */
884     QGridLayout *grid = new QGridLayout( ui.frame );
885     grid->setMargin( 0 );
886     for( int i = 0 ; i < BANDS ; i++ )
887     {
888         bands[i] = new QSlider( Qt::Vertical );
889         bands[i]->setMaximum( 400 );
890         bands[i]->setValue( 200 );
891         CONNECT( bands[i], valueChanged( int ), this, setCoreBands() );
892
893         band_texts[i] = new QLabel( band_frequencies[i] + "\n0.0dB" );
894         band_texts[i]->setFont( smallFont );
895
896         grid->addWidget( bands[i], 0, i );
897         grid->addWidget( band_texts[i], 1, i );
898     }
899
900     /* Add the listed presets */
901     for( int i = 0 ; i < NB_PRESETS ; i ++ )
902     {
903         presetsComboBox->addItem( qtr( preset_list_text[i] ),
904                                   QVariant( preset_list[i] ) );
905     }
906
907     /* Connects */
908     BUTTONACT( ui.enableCheck, enable() );
909     BUTTONACT( ui.eq2PassCheck, set2Pass() );
910     CONNECT( ui.preampSlider, valueChanged( int ), this, setPreamp() );
911
912     /* Do the update from the value of the core */
913     updateUIFromCore();
914 }
915
916 /* Write down initial values */
917 void Equalizer::updateUIFromCore()
918 {
919     char *psz_af, *psz_pres, *psz_bands;
920     float f_preamp;
921     int i_preset;
922
923     aout_instance_t *p_aout = THEMIM->getAout();
924     if( p_aout )
925     {
926         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
927         psz_pres = var_GetString( p_aout, "equalizer-preset" );
928         if( var_GetBool( p_aout, "equalizer-2pass" ) )
929             ui.eq2PassCheck->setChecked( true );
930         f_preamp = var_GetFloat( p_aout, "equalizer-preamp" );
931         psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
932         i_preset = presetsComboBox->findData( QVariant( psz_pres ) );
933         vlc_object_release( p_aout );
934     }
935     else
936     {
937         psz_af = config_GetPsz( p_intf, "audio-filter" );
938         psz_pres = config_GetPsz( p_intf, "equalizer-preset" );
939         if( config_GetInt( p_intf, "equalizer-2pass" ) )
940             ui.eq2PassCheck->setChecked( true );
941         f_preamp = config_GetFloat( p_intf, "equalizer-preamp" );
942         psz_bands = config_GetPsz( p_intf, "equalizer-bands" );
943         i_preset = presetsComboBox->findData( QVariant( psz_pres ) );
944     }
945     if( psz_af && strstr( psz_af, "equalizer" ) != NULL )
946         ui.enableCheck->setChecked( true );
947     enable( ui.enableCheck->isChecked() );
948
949     presetsComboBox->setCurrentIndex( i_preset );
950
951     ui.preampSlider->setValue( (int)( ( f_preamp + 20 ) * 10 ) );
952
953     if( psz_bands && strlen( psz_bands ) > 1 )
954     {
955         char *psz_bands_orig = psz_bands;
956         for( int i = 0; i < BANDS; i++ )
957         {
958             const float f = us_strtod(psz_bands, &psz_bands );
959             bands[i]->setValue( (int)( ( f + 20 ) * 10 )  );
960             if( psz_bands == NULL || *psz_bands == '\0' ) break;
961             psz_bands++;
962             if( *psz_bands == '\0' ) break;
963         }
964         free( psz_bands_orig );
965     }
966     else free( psz_bands );
967
968     free( psz_af );
969     free( psz_pres );
970 }
971
972 /* Functin called when enableButton is toggled */
973 void Equalizer::enable()
974 {
975     bool en = ui.enableCheck->isChecked();
976     aout_EnableFilter( THEPL, "equalizer", en );
977 //    aout_EnableFilter( THEPL, "upmixer", en );
978 //     aout_EnableFilter( THEPL, "vsurround", en );
979     enable( en );
980
981     if( presetsComboBox->currentIndex() < 0 )
982         presetsComboBox->setCurrentIndex( 0 );
983
984 }
985
986 void Equalizer::enable( bool en )
987 {
988     ui.eq2PassCheck->setEnabled( en );
989     presetsComboBox->setEnabled( en );
990     ui.presetLabel->setEnabled( en );
991     ui.preampLabel->setEnabled( en );
992     ui.preampSlider->setEnabled( en  );
993     for( int i = 0 ; i< BANDS; i++ )
994     {
995         bands[i]->setEnabled( en ); band_texts[i]->setEnabled( en );
996     }
997 }
998
999 /* Function called when the set2Pass button is activated */
1000 void Equalizer::set2Pass()
1001 {
1002     aout_instance_t *p_aout= THEMIM->getAout();
1003     bool b_2p = ui.eq2PassCheck->isChecked();
1004
1005     if( p_aout )
1006     {
1007         var_SetBool( p_aout, "equalizer-2pass", b_2p );
1008         vlc_object_release( p_aout );
1009     }
1010     config_PutInt( p_intf, "equalizer-2pass", b_2p );
1011 }
1012
1013 /* Function called when the preamp slider is moved */
1014 void Equalizer::setPreamp()
1015 {
1016     const float f = ( float )(  ui.preampSlider->value() ) /10 - 20;
1017     aout_instance_t *p_aout = THEMIM->getAout();
1018
1019     ui.preampLabel->setText( qtr( "Preamp\n" ) + QString::number( f, 'f', 1 )
1020                                                + qtr( "dB" ) );
1021     if( p_aout )
1022     {
1023         //delCallbacks( p_aout );
1024         var_SetFloat( p_aout, "equalizer-preamp", f );
1025         //addCallbacks( p_aout );
1026         vlc_object_release( p_aout );
1027     }
1028     config_PutFloat( p_intf, "equalizer-preamp", f );
1029 }
1030
1031 void Equalizer::setCoreBands()
1032 {
1033     /**\todo smoothing */
1034
1035     QString values;
1036     for( int i = 0; i < BANDS; i++ )
1037     {
1038         const float f_val = (float)( bands[i]->value() ) / 10 - 20;
1039         QString val = QString("%1").arg( f_val, 5, 'f', 1 );
1040
1041         band_texts[i]->setText( band_frequencies[i] + "\n" + val + "dB" );
1042         values += " " + val;
1043     }
1044
1045     aout_instance_t *p_aout = THEMIM->getAout();
1046     if( p_aout )
1047     {
1048         //delCallbacks( p_aout );
1049         var_SetString( p_aout, "equalizer-bands", qtu( values ) );
1050         //addCallbacks( p_aout );
1051         vlc_object_release( p_aout );
1052     }
1053 }
1054
1055 char * Equalizer::createValuesFromPreset( int i_preset )
1056 {
1057     QString values;
1058
1059     /* Create the QString in Qt */
1060     for( int i = 0 ; i< BANDS ;i++ )
1061         values += QString( " %1" ).arg( eqz_preset_10b[i_preset]->f_amp[i], 5, 'f', 1 );
1062
1063     /* Convert it to char * */
1064     return strdup( values.toAscii().constData() );
1065 }
1066
1067 void Equalizer::setCorePreset( int i_preset )
1068 {
1069     if( i_preset < 0 )
1070         return;
1071
1072     /* Update pre-amplification in the UI */
1073     float f_preamp = eqz_preset_10b[i_preset]->f_preamp;
1074     ui.preampSlider->setValue( (int)( ( f_preamp + 20 ) * 10 ) );
1075     ui.preampLabel->setText( qtr( "Preamp\n" )
1076                    + QString::number( f_preamp, 'f', 1 ) + qtr( "dB" ) );
1077
1078     char *psz_values = createValuesFromPreset( i_preset );
1079     if( !psz_values ) return ;
1080
1081     char *p = psz_values;
1082     for( int i = 0; i < BANDS && *p; i++ )
1083     {
1084         const float f = us_strtod( p, &p );
1085
1086         bands[i]->setValue( (int)( ( f + 20 ) * 10 )  );
1087         band_texts[i]->setText( band_frequencies[i] + "\n"
1088                               + QString("%1").arg( f, 5, 'f', 1 ) + "dB" );
1089         if( *p )
1090             p++; /* skip separator */
1091     }
1092
1093     /* Apply presets to audio output */
1094     aout_instance_t *p_aout= THEMIM->getAout();
1095     if( p_aout )
1096     {
1097         var_SetString( p_aout , "equalizer-preset" , preset_list[i_preset] );
1098
1099         var_SetString( p_aout, "equalizer-bands", psz_values );
1100         var_SetFloat( p_aout, "equalizer-preamp",
1101                       eqz_preset_10b[i_preset]->f_preamp );
1102         vlc_object_release( p_aout );
1103     }
1104     config_PutPsz( p_intf, "equalizer-bands", psz_values );
1105     config_PutPsz( p_intf, "equalizer-preset", preset_list[i_preset] );
1106     config_PutFloat( p_intf, "equalizer-preamp",
1107                     eqz_preset_10b[i_preset]->f_preamp );
1108     free( psz_values );
1109 }
1110
1111 static int PresetCallback( vlc_object_t *p_this, char const *psz_cmd,
1112                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1113 {
1114     VLC_UNUSED( p_this ); VLC_UNUSED( psz_cmd ); VLC_UNUSED( oldval );
1115
1116     char *psz_preset = newval.psz_string;
1117     Equalizer *eq = ( Equalizer * )p_data;
1118     int i_preset = eq->presetsComboBox->findData( QVariant( psz_preset ) );
1119     eq->presetsComboBox->setCurrentIndex( i_preset );
1120     return VLC_SUCCESS;
1121 }
1122
1123 void Equalizer::delCallbacks( aout_instance_t *p_aout )
1124 {
1125     //var_DelCallback( p_aout, "equalizer-bands", EqzCallback, this );
1126     //var_DelCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1127     var_DelCallback( p_aout, "equalizer-preset", PresetCallback, this );
1128 }
1129
1130 void Equalizer::addCallbacks( aout_instance_t *p_aout )
1131 {
1132     //var_AddCallback( p_aout, "equalizer-bands", EqzCallback, this );
1133     //var_AddCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1134     var_AddCallback( p_aout, "equalizer-preset", PresetCallback, this );
1135 }
1136
1137 /**********************************************************************
1138  * Audio filters
1139  **********************************************************************/
1140
1141 /**********************************************************************
1142  * Dynamic range compressor
1143  **********************************************************************/
1144
1145 typedef struct
1146 {
1147     const char *psz_name;
1148     const char *psz_descs;
1149     const char *psz_units;
1150     const float f_min;      // min
1151     const float f_max;      // max
1152     const float f_value;    // value
1153     const float f_resolution; // resolution
1154 } comp_controls_t;
1155
1156 static const comp_controls_t comp_controls[] =
1157 {
1158     { "compressor-rms-peak",    _("RMS/peak"),       "",       0.0f,   1.0f,   0.00f, 0.001f },
1159     { "compressor-attack",      _("Attack"),       _(" ms"),   1.5f, 400.0f,  25.00f, 0.100f },
1160     { "compressor-release",     _("Release"),      _(" ms"),   2.0f, 800.0f, 100.00f, 0.100f },
1161     { "compressor-threshold",   _("Threshold"),    _(" dB"), -30.0f,   0.0f, -11.00f, 0.010f },
1162     { "compressor-ratio",       _("Ratio"),          ":1",     1.0f,  20.0f,   8.00f, 0.010f },
1163     { "compressor-knee",        _("Knee\nradius"), _(" dB"),   1.0f,  10.0f,   2.50f, 0.010f },
1164     { "compressor-makeup-gain", _("Makeup\ngain"), _(" dB"),   0.0f,  24.0f,   7.00f, 0.010f },
1165 };
1166
1167 Compressor::Compressor( intf_thread_t *_p_intf, QWidget *_parent )
1168            : QWidget( _parent ) , p_intf( _p_intf )
1169 {
1170     QFont smallFont = QApplication::font();
1171     smallFont.setPointSize( smallFont.pointSize() - 2 );
1172
1173     QGridLayout *layout = new QGridLayout( this );
1174
1175     enableCheck = new QCheckBox( qtr( "Enable dynamic range compressor" ) );
1176     layout->addWidget( enableCheck, 0, 0, 1, NUM_CP_CTRL );
1177
1178     for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
1179     {
1180         const int i_min = (int)( comp_controls[i].f_min
1181                                / comp_controls[i].f_resolution );
1182         const int i_max = (int)( comp_controls[i].f_max
1183                                / comp_controls[i].f_resolution );
1184         const int i_val = (int)( comp_controls[i].f_value
1185                                / comp_controls[i].f_resolution );
1186
1187         compCtrl[i] = new QSlider( Qt::Vertical );
1188         compCtrl[i]->setMinimum( i_min );
1189         compCtrl[i]->setMaximum( i_max );
1190         compCtrl[i]->setValue(   i_val );
1191
1192         oldControlVars[i] = comp_controls[i].f_value;
1193
1194         CONNECT( compCtrl[i], valueChanged( int ), this, setInitValues() );
1195
1196         ctrl_texts[i] = new QLabel( qtr( comp_controls[i].psz_descs ) + "\n" );
1197         ctrl_texts[i]->setFont( smallFont );
1198         ctrl_texts[i]->setAlignment( Qt::AlignHCenter );
1199
1200         ctrl_readout[i] = new QLabel;
1201         ctrl_readout[i]->setFont( smallFont );
1202         ctrl_readout[i]->setAlignment( Qt::AlignHCenter );
1203
1204         layout->addWidget( compCtrl[i],     1, i, Qt::AlignHCenter );
1205         layout->addWidget( ctrl_readout[i], 2, i, Qt::AlignHCenter );
1206         layout->addWidget( ctrl_texts[i],   3, i, Qt::AlignHCenter );
1207     }
1208
1209     BUTTONACT( enableCheck, enable() );
1210
1211     /* Write down initial values */
1212     aout_instance_t *p_aout = THEMIM->getAout();
1213     char *psz_af;
1214
1215     if( p_aout )
1216     {
1217         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
1218         for( int i = 0; i < NUM_CP_CTRL; i++ )
1219         {
1220             controlVars[i] = var_GetFloat( p_aout,
1221                                            comp_controls[i].psz_name );
1222         }
1223         vlc_object_release( p_aout );
1224     }
1225     else
1226     {
1227         psz_af = config_GetPsz( p_intf, "audio-filter" );
1228         for( int i = 0; i < NUM_CP_CTRL; i++ )
1229         {
1230             controlVars[i] = config_GetFloat( p_intf,
1231                                               comp_controls[i].psz_name );
1232         }
1233     }
1234     if( psz_af && strstr( psz_af, "compressor" ) != NULL )
1235     {
1236         enableCheck->setChecked( true );
1237     }
1238     free( psz_af );
1239     enable( enableCheck->isChecked() );
1240     updateSliders( controlVars );
1241     setValues();
1242 }
1243
1244 void Compressor::enable()
1245 {
1246     bool en = enableCheck->isChecked();
1247     aout_EnableFilter( THEPL, "compressor", en );
1248     enable( en );
1249 }
1250
1251 void Compressor::enable( bool en )
1252 {
1253     for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
1254     {
1255         compCtrl[i]->setEnabled( en );
1256         ctrl_texts[i]->setEnabled( en );
1257         ctrl_readout[i]->setEnabled( en );
1258     }
1259 }
1260
1261 void Compressor::updateSliders( float * controlVars )
1262 {
1263     for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
1264     {
1265         if( oldControlVars[i] != controlVars[i] )
1266         {
1267             compCtrl[i]->setValue(
1268                     (int)( controlVars[i] / comp_controls[i].f_resolution ) );
1269         }
1270     }
1271 }
1272
1273 void Compressor::setInitValues()
1274 {
1275     setValues();
1276 }
1277
1278 void Compressor::setValues()
1279 {
1280     aout_instance_t *p_aout = THEMIM->getAout();
1281
1282     for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
1283     {
1284         float f = (float)( compCtrl[i]->value() ) * ( comp_controls[i].f_resolution );
1285         ctrl_readout[i]->setText( QString::number( f, 'f', 1 )
1286                                 + qtr( comp_controls[i].psz_units ) );
1287         if( oldControlVars[i] != f )
1288         {
1289             if( p_aout )
1290             {
1291                 var_SetFloat( p_aout, comp_controls[i].psz_name, f );
1292             }
1293             config_PutFloat( p_intf, comp_controls[i].psz_name, f );
1294             oldControlVars[i] = f;
1295         }
1296     }
1297     if( p_aout )
1298     {
1299         vlc_object_release( p_aout );
1300     }
1301 }
1302
1303 /**********************************************************************
1304  * Spatializer
1305  **********************************************************************/
1306 typedef struct
1307 {
1308     const char *psz_name;
1309     const char *psz_desc;
1310 } spat_controls_t;
1311
1312 static const spat_controls_t spat_controls[] =
1313 {
1314     { "spatializer-roomsize", _("Size") },
1315     { "spatializer-width",    _("Width") },
1316     { "spatializer-wet",      _("Wet") },
1317     { "spatializer-dry",      _("Dry") },
1318     { "spatializer-damp",     _("Damp") },
1319 };
1320
1321 Spatializer::Spatializer( intf_thread_t *_p_intf, QWidget *_parent )
1322             : QWidget( _parent ) , p_intf( _p_intf )
1323 {
1324     QFont smallFont = QApplication::font();
1325     smallFont.setPointSize( smallFont.pointSize() - 2 );
1326
1327     QGridLayout *layout = new QGridLayout( this );
1328
1329     enableCheck = new QCheckBox( qtr( "Enable spatializer" ) );
1330     layout->addWidget( enableCheck, 0, 0, 1, NUM_SP_CTRL );
1331
1332     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1333     {
1334         spatCtrl[i] = new QSlider( Qt::Vertical );
1335         if( i < 2 )
1336         {
1337             spatCtrl[i]->setMaximum( 10 );
1338             spatCtrl[i]->setValue( 2 );
1339         }
1340         else
1341         {
1342             spatCtrl[i]->setMaximum( 10 );
1343             spatCtrl[i]->setValue( 0 );
1344             spatCtrl[i]->setMinimum( -10 );
1345         }
1346
1347         oldControlVars[i] = spatCtrl[i]->value();
1348
1349         CONNECT( spatCtrl[i], valueChanged( int ), this, setInitValues() );
1350
1351         ctrl_texts[i] = new QLabel( qtr( spat_controls[i].psz_desc ) + "\n" );
1352         ctrl_texts[i]->setFont( smallFont );
1353
1354         ctrl_readout[i] = new QLabel;
1355         ctrl_readout[i]->setFont( smallFont );
1356
1357         layout->addWidget( spatCtrl[i],     1, i, Qt::AlignHCenter );
1358         layout->addWidget( ctrl_readout[i], 2, i, Qt::AlignHCenter );
1359         layout->addWidget( ctrl_texts[i],   3, i, Qt::AlignHCenter );
1360     }
1361
1362     BUTTONACT( enableCheck, enable() );
1363
1364     /* Write down initial values */
1365     aout_instance_t *p_aout = THEMIM->getAout();
1366     char *psz_af;
1367
1368     if( p_aout )
1369     {
1370         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
1371         for( int i = 0; i < NUM_SP_CTRL ; i++ )
1372         {
1373             controlVars[i] = var_GetFloat( p_aout, spat_controls[i].psz_name );
1374         }
1375         vlc_object_release( p_aout );
1376     }
1377     else
1378     {
1379         psz_af = config_GetPsz( p_intf, "audio-filter" );
1380         for( int i = 0; i < NUM_SP_CTRL ; i++ )
1381         {
1382             controlVars[i] = config_GetFloat( p_intf, spat_controls[i].psz_name );
1383         }
1384     }
1385     if( psz_af && strstr( psz_af, "spatializer" ) != NULL )
1386         enableCheck->setChecked( true );
1387     free( psz_af );
1388     enable( enableCheck->isChecked() );
1389     setValues();
1390 }
1391
1392 void Spatializer::enable()
1393 {
1394     bool en = enableCheck->isChecked();
1395     aout_EnableFilter( THEPL, "spatializer", en );
1396     enable( en );
1397 }
1398
1399 void Spatializer::enable( bool en )
1400 {
1401     for( int i = 0 ; i< NUM_SP_CTRL; i++ )
1402     {
1403         spatCtrl[i]->setEnabled( en );
1404         ctrl_texts[i]->setEnabled( en );
1405         ctrl_readout[i]->setEnabled( en );
1406     }
1407 }
1408 void Spatializer::setInitValues()
1409 {
1410     setValues();
1411 }
1412
1413 void Spatializer::setValues()
1414 {
1415     aout_instance_t *p_aout = THEMIM->getAout();
1416
1417     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1418     {
1419         float f = (float)(  spatCtrl[i]->value() );
1420         ctrl_readout[i]->setText( QString::number( f, 'f',  1 ) );
1421     }
1422     if( p_aout )
1423     {
1424         for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1425         {
1426             if( oldControlVars[i] != spatCtrl[i]->value() )
1427             {
1428                 var_SetFloat( p_aout, spat_controls[i].psz_name,
1429                         ( float )spatCtrl[i]->value() );
1430                 config_PutFloat( p_intf, spat_controls[i].psz_name,
1431                         ( float ) spatCtrl[i]->value() );
1432                 oldControlVars[i] = ( float ) spatCtrl[i]->value();
1433             }
1434         }
1435         vlc_object_release( p_aout );
1436     }
1437
1438 }
1439 void Spatializer::delCallbacks( aout_instance_t *p_aout )
1440 {
1441     VLC_UNUSED( p_aout );
1442     //    var_DelCallback( p_aout, "Spatializer-bands", EqzCallback, this );
1443     //    var_DelCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
1444 }
1445
1446 void Spatializer::addCallbacks( aout_instance_t *p_aout )
1447 {
1448     VLC_UNUSED( p_aout );
1449     //    var_AddCallback( p_aout, "Spatializer-bands", EqzCallback, this );
1450     //    var_AddCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
1451 }
1452
1453 #include <QToolButton>
1454 #include <QGridLayout>
1455
1456 #define SUBSDELAY_CFG_MODE                     "subsdelay-mode"
1457 #define SUBSDELAY_CFG_FACTOR                   "subsdelay-factor"
1458 #define SUBSDELAY_MODE_ABSOLUTE                0
1459 #define SUBSDELAY_MODE_RELATIVE_SOURCE_DELAY   1
1460 #define SUBSDELAY_MODE_RELATIVE_SOURCE_CONTENT 2
1461
1462 SyncControls::SyncControls( intf_thread_t *_p_intf, QWidget *_parent ) :
1463                             QWidget( _parent ) , p_intf( _p_intf )
1464 {
1465     QGroupBox *AVBox, *subsBox;
1466
1467     QToolButton *moinsAV, *plusAV;
1468     QToolButton *moinssubs, *plussubs;
1469     QToolButton *moinssubSpeed, *plussubSpeed;
1470     QToolButton *moinssubDuration, *plussubDuration;
1471
1472     QToolButton *updateButton;
1473
1474     b_userAction = true;
1475
1476     QGridLayout *mainLayout = new QGridLayout( this );
1477
1478     /* AV sync */
1479     AVBox = new QGroupBox( qtr( "Audio/Video" ) );
1480     QGridLayout *AVLayout = new QGridLayout( AVBox );
1481
1482     moinsAV = new QToolButton;
1483     moinsAV->setToolButtonStyle( Qt::ToolButtonTextOnly );
1484     moinsAV->setAutoRaise( true );
1485     moinsAV->setText( "-" );
1486     AVLayout->addWidget( moinsAV, 0, 1, 1, 1 );
1487
1488     plusAV = new QToolButton;
1489     plusAV->setToolButtonStyle( Qt::ToolButtonTextOnly );
1490     plusAV->setAutoRaise( true );
1491     plusAV->setText( "+" );
1492     AVLayout->addWidget( plusAV, 0, 3, 1, 1 );
1493
1494     QLabel *AVLabel = new QLabel;
1495     AVLabel->setText( qtr( "Advance of audio over video:" ) );
1496     AVLayout->addWidget( AVLabel, 0, 0, 1, 1 );
1497
1498     AVSpin = new QDoubleSpinBox;
1499     AVSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1500     AVSpin->setDecimals( 3 );
1501     AVSpin->setMinimum( -600.0 );
1502     AVSpin->setMaximum( 600.0 );
1503     AVSpin->setSingleStep( 0.1 );
1504     AVSpin->setToolTip( qtr( "A positive value means that\n"
1505                              "the audio is ahead of the video" ) );
1506     AVSpin->setSuffix( " s" );
1507     AVLayout->addWidget( AVSpin, 0, 2, 1, 1 );
1508     mainLayout->addWidget( AVBox, 1, 0, 1, 5 );
1509
1510
1511     /* Subs */
1512     subsBox = new QGroupBox( qtr( "Subtitles/Video" ) );
1513     QGridLayout *subsLayout = new QGridLayout( subsBox );
1514
1515     moinssubs = new QToolButton;
1516     moinssubs->setToolButtonStyle( Qt::ToolButtonTextOnly );
1517     moinssubs->setAutoRaise( true );
1518     moinssubs->setText( "-" );
1519     subsLayout->addWidget( moinssubs, 0, 1, 1, 1 );
1520
1521     plussubs = new QToolButton;
1522     plussubs->setToolButtonStyle( Qt::ToolButtonTextOnly );
1523     plussubs->setAutoRaise( true );
1524     plussubs->setText( "+" );
1525     subsLayout->addWidget( plussubs, 0, 3, 1, 1 );
1526
1527     QLabel *subsLabel = new QLabel;
1528     subsLabel->setText( qtr( "Advance of subtitles over video:" ) );
1529     subsLayout->addWidget( subsLabel, 0, 0, 1, 1 );
1530
1531     subsSpin = new QDoubleSpinBox;
1532     subsSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1533     subsSpin->setDecimals( 3 );
1534     subsSpin->setMinimum( -600.0 );
1535     subsSpin->setMaximum( 600.0 );
1536     subsSpin->setSingleStep( 0.1 );
1537     subsSpin->setToolTip( qtr( "A positive value means that\n"
1538                              "the subtitles are ahead of the video" ) );
1539     subsSpin->setSuffix( " s" );
1540     subsLayout->addWidget( subsSpin, 0, 2, 1, 1 );
1541
1542
1543     moinssubSpeed = new QToolButton;
1544     moinssubSpeed->setToolButtonStyle( Qt::ToolButtonTextOnly );
1545     moinssubSpeed->setAutoRaise( true );
1546     moinssubSpeed->setText( "-" );
1547     subsLayout->addWidget( moinssubSpeed, 1, 1, 1, 1 );
1548
1549     plussubSpeed = new QToolButton;
1550     plussubSpeed->setToolButtonStyle( Qt::ToolButtonTextOnly );
1551     plussubSpeed->setAutoRaise( true );
1552     plussubSpeed->setText( "+" );
1553     subsLayout->addWidget( plussubSpeed, 1, 3, 1, 1 );
1554
1555     QLabel *subSpeedLabel = new QLabel;
1556     subSpeedLabel->setText( qtr( "Speed of the subtitles:" ) );
1557     subsLayout->addWidget( subSpeedLabel, 1, 0, 1, 1 );
1558
1559     subSpeedSpin = new QDoubleSpinBox;
1560     subSpeedSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1561     subSpeedSpin->setDecimals( 3 );
1562     subSpeedSpin->setMinimum( 1 );
1563     subSpeedSpin->setMaximum( 100 );
1564     subSpeedSpin->setSingleStep( 0.2 );
1565     subSpeedSpin->setSuffix( " fps" );
1566     subsLayout->addWidget( subSpeedSpin, 1, 2, 1, 1 );
1567
1568     moinssubDuration = new QToolButton;
1569     moinssubDuration->setToolButtonStyle( Qt::ToolButtonTextOnly );
1570     moinssubDuration->setAutoRaise( true );
1571     moinssubDuration->setText( "-" );
1572     subsLayout->addWidget( moinssubDuration, 2, 1, 1, 1 );
1573
1574     plussubDuration = new QToolButton;
1575     plussubDuration->setToolButtonStyle( Qt::ToolButtonTextOnly );
1576     plussubDuration->setAutoRaise( true );
1577     plussubDuration->setText( "+" );
1578     subsLayout->addWidget( plussubDuration, 2, 3, 1, 1 );
1579
1580     QLabel *subDurationLabel = new QLabel;
1581     subDurationLabel->setText( qtr( "Subtitles duration factor:" ) );
1582     subsLayout->addWidget( subDurationLabel, 2, 0, 1, 1 );
1583
1584     subDurationSpin = new QDoubleSpinBox;
1585     subDurationSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1586     subDurationSpin->setDecimals( 3 );
1587     subDurationSpin->setMinimum( 0 );
1588     subDurationSpin->setMaximum( 20 );
1589     subDurationSpin->setSingleStep( 0.2 );
1590     subsLayout->addWidget( subDurationSpin, 2, 2, 1, 1 );
1591
1592     mainLayout->addWidget( subsBox, 2, 0, 2, 5 );
1593
1594     updateButton = new QToolButton;
1595     updateButton->setAutoRaise( true );
1596     mainLayout->addWidget( updateButton, 0, 4, 1, 1 );
1597
1598
1599     /* Various Connects */
1600     CONNECT( moinsAV, clicked(), AVSpin, stepDown () );
1601     CONNECT( plusAV, clicked(), AVSpin, stepUp () );
1602     CONNECT( moinssubs, clicked(), subsSpin, stepDown () );
1603     CONNECT( plussubs, clicked(), subsSpin, stepUp () );
1604     CONNECT( moinssubSpeed, clicked(), subSpeedSpin, stepDown () );
1605     CONNECT( plussubSpeed, clicked(), subSpeedSpin, stepUp () );
1606     CONNECT( moinssubDuration, clicked(), subDurationSpin, stepDown () );
1607     CONNECT( plussubDuration, clicked(), subDurationSpin, stepUp () );
1608     CONNECT( AVSpin, valueChanged ( double ), this, advanceAudio( double ) ) ;
1609     CONNECT( subsSpin, valueChanged ( double ), this, advanceSubs( double ) ) ;
1610     CONNECT( subSpeedSpin, valueChanged ( double ),
1611              this, adjustSubsSpeed( double ) );
1612     CONNECT( subDurationSpin, valueChanged ( double ),
1613              this, adjustSubsDuration( double ) );
1614
1615     CONNECT( THEMIM->getIM(), synchroChanged(), this, update() );
1616     BUTTON_SET_ACT_I( updateButton, "", update,
1617             qtr( "Force update of this dialog's values" ), update() );
1618
1619     initSubsDuration();
1620
1621     /* Set it */
1622     update();
1623 }
1624
1625 SyncControls::~SyncControls()
1626 {
1627     subsdelayClean();
1628 }
1629
1630 void SyncControls::clean()
1631 {
1632     b_userAction = false;
1633     AVSpin->setValue( 0.0 );
1634     subsSpin->setValue( 0.0 );
1635     subSpeedSpin->setValue( 1.0 );
1636     subsdelayClean();
1637     b_userAction = true;
1638 }
1639
1640 void SyncControls::update()
1641 {
1642     b_userAction = false;
1643
1644     int64_t i_delay;
1645     if( THEMIM->getInput() )
1646     {
1647         i_delay = var_GetTime( THEMIM->getInput(), "audio-delay" );
1648         AVSpin->setValue( ( (double)i_delay ) / 1000000 );
1649         i_delay = var_GetTime( THEMIM->getInput(), "spu-delay" );
1650         subsSpin->setValue( ( (double)i_delay ) / 1000000 );
1651         subSpeedSpin->setValue( var_GetFloat( THEMIM->getInput(), "sub-fps" ) );
1652         subDurationSpin->setValue( var_InheritFloat( p_intf, SUBSDELAY_CFG_FACTOR ) );
1653     }
1654     b_userAction = true;
1655 }
1656
1657 void SyncControls::advanceAudio( double f_advance )
1658 {
1659     if( THEMIM->getInput() && b_userAction )
1660     {
1661         int64_t i_delay = f_advance * 1000000;
1662         var_SetTime( THEMIM->getInput(), "audio-delay", i_delay );
1663     }
1664 }
1665
1666 void SyncControls::advanceSubs( double f_advance )
1667 {
1668     if( THEMIM->getInput() && b_userAction )
1669     {
1670         int64_t i_delay = f_advance * 1000000;
1671         var_SetTime( THEMIM->getInput(), "spu-delay", i_delay );
1672     }
1673 }
1674
1675 void SyncControls::adjustSubsSpeed( double f_fps )
1676 {
1677     if( THEMIM->getInput() && b_userAction )
1678     {
1679         var_SetFloat( THEMIM->getInput(), "sub-fps", f_fps );
1680     }
1681 }
1682
1683 void SyncControls::adjustSubsDuration( double f_factor )
1684 {
1685     if( THEMIM->getInput() && b_userAction )
1686     {
1687         subsdelaySetFactor( f_factor );
1688         ChangeVFiltersString( p_intf, "subsdelay", f_factor > 0 );
1689     }
1690 }
1691
1692 void SyncControls::initSubsDuration()
1693 {
1694     int i_mode = var_InheritInteger( p_intf, SUBSDELAY_CFG_MODE );
1695
1696     switch (i_mode)
1697     {
1698     default:
1699     case SUBSDELAY_MODE_ABSOLUTE:
1700         subDurationSpin->setToolTip( qtr( "Extend subtitles duration by this value.\n"
1701                                           "Set 0 to disable." ) );
1702         subDurationSpin->setSuffix( " s" );
1703         break;
1704     case SUBSDELAY_MODE_RELATIVE_SOURCE_DELAY:
1705         subDurationSpin->setToolTip( qtr( "Multiply subtitles duration by this value.\n"
1706                                           "Set 0 to disable." ) );
1707         subDurationSpin->setSuffix( "" );
1708         break;
1709     case SUBSDELAY_MODE_RELATIVE_SOURCE_CONTENT:
1710         subDurationSpin->setToolTip( qtr( "Recalculate subtitles duration according\n"
1711                                           "to their content and this value.\n"
1712                                           "Set 0 to disable." ) );
1713         subDurationSpin->setSuffix( "" );
1714         break;
1715     }
1716 }
1717
1718 void SyncControls::subsdelayClean()
1719 {
1720     /* Remove subsdelay filter */
1721     ChangeVFiltersString( p_intf, "subsdelay", false );
1722 }
1723
1724 void SyncControls::subsdelaySetFactor( double f_factor )
1725 {
1726     /* Set the factor in the preferences */
1727     config_PutFloat( p_intf, SUBSDELAY_CFG_FACTOR, f_factor );
1728
1729     /* Try to find an instance of subsdelay, and set its factor */
1730     vlc_object_t *p_obj = ( vlc_object_t * ) vlc_object_find_name( p_intf->p_libvlc, "subsdelay" );
1731     if( p_obj )
1732     {
1733         var_SetFloat( p_obj, SUBSDELAY_CFG_FACTOR, f_factor );
1734         vlc_object_release( p_obj );
1735     }
1736 }
1737
1738
1739 /**********************************************************************
1740  * Video filters / Adjust
1741  **********************************************************************/
1742
1743 /**********************************************************************
1744  * Extended playbak controls
1745  **********************************************************************/