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