]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/extended_panels.cpp
Qt4: fix compressor and spatializer initializer order (fixes #7296)
[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             size_t i_len = strlen( psz_string );
366             if( i_len > 0 && *( psz_string + i_len - 1 ) == ':' )
367             {
368                 *( psz_string + i_len - 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 == NULL )
444     {
445         msg_Err( p_intf, "Couldn't find option \"%s\".", qtu( option ) );
446         return;
447     }
448
449     if( p_item->i_type == CONFIG_ITEM_INTEGER
450      || p_item->i_type == CONFIG_ITEM_BOOL )
451     {
452         int64_t *values;
453         char **texts;
454         ssize_t count = config_GetIntChoices( VLC_OBJECT( p_intf ),
455                                               qtu( option ), &values, &texts );
456         for( ssize_t i = 0; i < count; i++ )
457         {
458             combobox->addItem( qtr( texts[i] ), qlonglong(values[i]) );
459             free( texts[i] );
460         }
461         free( texts );
462         free( values );
463     }
464     else if( p_item->i_type == CONFIG_ITEM_STRING )
465     {
466         char **values;
467         char **texts;
468         ssize_t count = config_GetPszChoices( VLC_OBJECT( p_intf ),
469                                               qtu( option ), &values, &texts );
470         for( ssize_t i = 0; i < count; i++ )
471         {
472             combobox->addItem( qtr( texts[i] ), qfu(values[i]) );
473             free( texts[i] );
474             free( values[i] );
475         }
476         free( texts );
477         free( values );
478     }
479 }
480
481 void ExtVideo::setWidgetValue( QObject *widget )
482 {
483     QString module = ModuleFromWidgetName( widget->parent() );
484     //std::cout << "Module name: " << module.toStdString() << std::endl;
485     QString option = OptionFromWidgetName( widget );
486     //std::cout << "Option name: " << option.toStdString() << std::endl;
487
488     vlc_object_t *p_obj = ( vlc_object_t * )
489         vlc_object_find_name( p_intf->p_libvlc, qtu( module ) );
490     int i_type;
491     vlc_value_t val;
492
493     if( !p_obj )
494     {
495 #if 0
496         msg_Dbg( p_intf,
497                  "Module instance %s not found, looking in config values.",
498                  qtu( module ) );
499 #endif
500         i_type = config_GetType( p_intf, qtu( option ) ) & VLC_VAR_CLASS;
501         switch( i_type )
502         {
503             case VLC_VAR_INTEGER:
504             case VLC_VAR_BOOL:
505                 val.i_int = config_GetInt( p_intf, qtu( option ) );
506                 break;
507             case VLC_VAR_FLOAT:
508                 val.f_float = config_GetFloat( p_intf, qtu( option ) );
509                 break;
510             case VLC_VAR_STRING:
511                 val.psz_string = config_GetPsz( p_intf, qtu( option ) );
512                 break;
513         }
514     }
515     else
516     {
517         i_type = var_Type( p_obj, qtu( option ) ) & VLC_VAR_CLASS;
518         var_Get( p_obj, qtu( option ), &val );
519         vlc_object_release( p_obj );
520     }
521
522     /* Try to cast to all the widgets we're likely to encounter. Only
523      * one of the casts is expected to work. */
524     QSlider        *slider        = qobject_cast<QSlider*>       ( widget );
525     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( widget );
526     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( widget );
527     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( widget );
528     QDial          *dial          = qobject_cast<QDial*>         ( widget );
529     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( widget );
530     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( widget );
531
532     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
533     {
534         if( slider )        slider->setValue( val.i_int );
535         else if( checkbox ) checkbox->setCheckState( val.i_int? Qt::Checked
536                                                               : Qt::Unchecked );
537         else if( spinbox )  spinbox->setValue( val.i_int );
538         else if( dial )     dial->setValue( ( 540-val.i_int )%360 );
539         else if( lineedit )
540         {
541             char str[30];
542             snprintf( str, sizeof(str), "%06"PRIX64, val.i_int );
543             lineedit->setText( str );
544         }
545         else if( combobox ) combobox->setCurrentIndex(
546                             combobox->findData( qlonglong(val.i_int) ) );
547         else msg_Warn( p_intf, "Could not find the correct Integer widget" );
548     }
549     else if( i_type == VLC_VAR_FLOAT )
550     {
551         if( slider ) slider->setValue( ( int )( val.f_float*( double )slider->tickInterval() ) ); /* hack alert! */
552         else if( doublespinbox ) doublespinbox->setValue( val.f_float );
553         else msg_Warn( p_intf, "Could not find the correct Float widget" );
554     }
555     else if( i_type == VLC_VAR_STRING )
556     {
557         if( lineedit ) lineedit->setText( qfu( val.psz_string ) );
558         else if( combobox ) combobox->setCurrentIndex(
559                             combobox->findData( qfu( val.psz_string ) ) );
560         else msg_Warn( p_intf, "Could not find the correct String widget" );
561         free( val.psz_string );
562     }
563     else
564         if( p_obj )
565             msg_Err( p_intf,
566                      "Module %s's %s variable is of an unsupported type ( %d )",
567                      qtu( module ),
568                      qtu( option ),
569                      i_type );
570 }
571
572 void ExtVideo::updateFilterOptions()
573 {
574     QString module = ModuleFromWidgetName( sender()->parent() );
575     //std::cout << "Module name: " << module.toStdString() << std::endl;
576     QString option = OptionFromWidgetName( sender() );
577     //std::cout << "Option name: " << option.toStdString() << std::endl;
578
579     vlc_object_t *p_obj = ( vlc_object_t * )
580         vlc_object_find_name( p_intf->p_libvlc, qtu( module ) );
581     int i_type;
582     bool b_is_command;
583     if( !p_obj )
584     {
585         msg_Warn( p_intf, "Module %s not found. You'll need to restart the filter to take the change into account.", qtu( module ) );
586         i_type = config_GetType( p_intf, qtu( option ) );
587         b_is_command = false;
588     }
589     else
590     {
591         i_type = var_Type( p_obj, qtu( option ) );
592         if( i_type == 0 )
593             i_type = config_GetType( p_intf, qtu( option ) );
594         b_is_command = ( i_type & VLC_VAR_ISCOMMAND );
595     }
596
597     /* Try to cast to all the widgets we're likely to encounter. Only
598      * one of the casts is expected to work. */
599     QSlider        *slider        = qobject_cast<QSlider*>       ( sender() );
600     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( sender() );
601     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( sender() );
602     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( sender() );
603     QDial          *dial          = qobject_cast<QDial*>         ( sender() );
604     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( sender() );
605     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( sender() );
606
607     i_type &= VLC_VAR_CLASS;
608     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
609     {
610         int i_int = 0;
611         if( slider )        i_int = slider->value();
612         else if( checkbox ) i_int = checkbox->checkState() == Qt::Checked;
613         else if( spinbox )  i_int = spinbox->value();
614         else if( dial )     i_int = ( 540-dial->value() )%360;
615         else if( lineedit ) i_int = lineedit->text().toInt( NULL,16 );
616         else if( combobox ) i_int = combobox->itemData( combobox->currentIndex() ).toInt();
617         else msg_Warn( p_intf, "Could not find the correct Integer widget" );
618         config_PutInt( p_intf, qtu( option ), i_int );
619         if( b_is_command )
620         {
621             if( i_type == VLC_VAR_INTEGER )
622                 var_SetInteger( p_obj, qtu( option ), i_int );
623             else
624                 var_SetBool( p_obj, qtu( option ), i_int );
625         }
626     }
627     else if( i_type == VLC_VAR_FLOAT )
628     {
629         double f_float = 0;
630         if( slider )             f_float = ( double )slider->value()
631                                          / ( double )slider->tickInterval(); /* hack alert! */
632         else if( doublespinbox ) f_float = doublespinbox->value();
633         else if( lineedit ) f_float = lineedit->text().toDouble();
634         else msg_Warn( p_intf, "Could not find the correct Float widget" );
635         config_PutFloat( p_intf, qtu( option ), f_float );
636         if( b_is_command )
637             var_SetFloat( p_obj, qtu( option ), f_float );
638     }
639     else if( i_type == VLC_VAR_STRING )
640     {
641         QString val;
642         if( lineedit )
643             val = lineedit->text();
644         else if( combobox )
645             val = combobox->itemData( combobox->currentIndex() ).toString();
646         else
647             msg_Warn( p_intf, "Could not find the correct String widget" );
648         config_PutPsz( p_intf, qtu( option ), qtu( val ) );
649         if( b_is_command )
650             var_SetString( p_obj, qtu( option ), qtu( val ) );
651     }
652     else
653         msg_Err( p_intf,
654                  "Module %s's %s variable is of an unsupported type ( %d )",
655                  qtu( module ),
656                  qtu( option ),
657                  i_type );
658
659     if( !b_is_command )
660     {
661         msg_Warn( p_intf, "Module %s's %s variable isn't a command. Brute-restarting the filter.",
662                  qtu( module ),
663                  qtu( option ) );
664         ChangeVFiltersString( p_intf, qtu( module ), false );
665         ChangeVFiltersString( p_intf, qtu( module ), true );
666     }
667
668     if( p_obj ) vlc_object_release( p_obj );
669 }
670
671 #if 0
672 void ExtVideo::gotoConf( QObject* src )
673 {
674 #define SHOWCONF( module ) \
675     if( src->objectName().contains( module ) ) \
676     { \
677         PrefsDialog::getInstance( p_intf )->showModulePrefs( module ); \
678         return; \
679     }
680     SHOWCONF( "clone" );
681     SHOWCONF( "magnify" );
682     SHOWCONF( "wave" );
683     SHOWCONF( "ripple" );
684     SHOWCONF( "invert" );
685     SHOWCONF( "puzzle" );
686     SHOWCONF( "wall" );
687     SHOWCONF( "gradient" );
688     SHOWCONF( "colorthres" )
689 }
690 #endif
691
692 /**********************************************************************
693  * v4l2 controls
694  **********************************************************************/
695
696 ExtV4l2::ExtV4l2( intf_thread_t *_p_intf, QWidget *_parent )
697     : QWidget( _parent ), p_intf( _p_intf ), box( NULL )
698 {
699     QVBoxLayout *layout = new QVBoxLayout( this );
700     help = new QLabel( qtr("No v4l2 instance found.\n"
701       "Please check that the device has been opened with VLC and is playing.\n\n"
702       "Controls will automatically appear here.")
703       , this );
704     help->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
705     layout->addWidget( help );
706     setLayout( layout );
707 }
708
709 void ExtV4l2::showEvent( QShowEvent *event )
710 {
711     QWidget::showEvent( event );
712     Refresh();
713 }
714
715 void ExtV4l2::Refresh( void )
716 {
717     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( pl_Get(p_intf), "v4l2" );
718     help->hide();
719     if( box )
720     {
721         layout()->removeWidget( box );
722         delete box;
723         box = NULL;
724     }
725     if( p_obj )
726     {
727         vlc_value_t val, text;
728         int i_ret = var_Change( p_obj, "controls", VLC_VAR_GETCHOICES,
729                                 &val, &text );
730         if( i_ret < 0 )
731         {
732             msg_Err( p_intf, "Oops, v4l2 object doesn't have a 'controls' variable." );
733             help->show();
734             vlc_object_release( p_obj );
735             return;
736         }
737
738         box = new QGroupBox( this );
739         layout()->addWidget( box );
740         QVBoxLayout *layout = new QVBoxLayout( box );
741         box->setLayout( layout );
742
743         for( int i = 0; i < val.p_list->i_count; i++ )
744         {
745             vlc_value_t vartext;
746             const char *psz_var = text.p_list->p_values[i].psz_string;
747
748             if( var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &vartext, NULL ) )
749                 continue;
750
751             QString name = qtr( vartext.psz_string );
752             free( vartext.psz_string );
753             msg_Dbg( p_intf, "v4l2 control \"%"PRIx64"\": %s (%s)",
754                      val.p_list->p_values[i].i_int, psz_var, qtu( name ) );
755
756             int i_type = var_Type( p_obj, psz_var );
757             switch( i_type & VLC_VAR_TYPE )
758             {
759                 case VLC_VAR_INTEGER:
760                 {
761                     QLabel *label = new QLabel( name, box );
762                     QHBoxLayout *hlayout = new QHBoxLayout();
763                     hlayout->addWidget( label );
764                     int i_val = var_GetInteger( p_obj, psz_var );
765                     if( i_type & VLC_VAR_HASCHOICE )
766                     {
767                         QComboBox *combobox = new QComboBox( box );
768                         combobox->setObjectName( qfu( psz_var ) );
769
770                         vlc_value_t val2, text2;
771                         var_Change( p_obj, psz_var, VLC_VAR_GETCHOICES,
772                                     &val2, &text2 );
773                         for( int j = 0; j < val2.p_list->i_count; j++ )
774                         {
775                             combobox->addItem(
776                                        text2.p_list->p_values[j].psz_string,
777                                        qlonglong( val2.p_list->p_values[j].i_int) );
778                             if( i_val == val2.p_list->p_values[j].i_int )
779                                 combobox->setCurrentIndex( j );
780                         }
781                         var_FreeList( &val2, &text2 );
782
783                         CONNECT( combobox, currentIndexChanged( int ), this,
784                                  ValueChange( int ) );
785                         hlayout->addWidget( combobox );
786                     }
787                     else
788                     if( (i_type & VLC_VAR_HASMIN) && (i_type & VLC_VAR_HASMAX) )
789                     {
790                         QSlider *slider = new QSlider( box );
791                         slider->setObjectName( qfu( psz_var ) );
792                         slider->setOrientation( Qt::Horizontal );
793                         vlc_value_t val2;
794                         var_Change( p_obj, psz_var, VLC_VAR_GETMIN,
795                                     &val2, NULL );
796                         if( val2.i_int < INT_MIN )
797                             val2.i_int = INT_MIN; /* FIXME */
798                         slider->setMinimum( val2.i_int );
799                         var_Change( p_obj, psz_var, VLC_VAR_GETMAX,
800                                     &val2, NULL );
801                         if( val2.i_int > INT_MAX )
802                             val2.i_int = INT_MAX; /* FIXME */
803                         slider->setMaximum( val2.i_int );
804                         if( !var_Change( p_obj, psz_var, VLC_VAR_GETSTEP,
805                                          &val2, NULL ) )
806                             slider->setSingleStep( val2.i_int );
807                         slider->setValue( i_val );
808                         CONNECT( slider, valueChanged( int ), this,
809                                  ValueChange( int ) );
810                         hlayout->addWidget( slider );
811                     }
812                     else
813                     {
814                         QSpinBox *spinBox = new QSpinBox( box );
815                         spinBox->setObjectName( qfu( psz_var ) );
816                         spinBox->setMinimum( INT_MIN );
817                         spinBox->setMaximum( INT_MAX );
818                         spinBox->setValue( i_val );
819                         CONNECT( spinBox, valueChanged( int ), this,
820                                  ValueChange( int ) );
821                         hlayout->addWidget( spinBox );
822                     }
823                     layout->addLayout( hlayout );
824                     break;
825                 }
826                 case VLC_VAR_BOOL:
827                 {
828                     QCheckBox *button = new QCheckBox( name, box );
829                     button->setObjectName( qfu( psz_var ) );
830                     button->setChecked( var_GetBool( p_obj, psz_var ) );
831
832                     CONNECT( button, clicked( bool ), this,
833                              ValueChange( bool ) );
834                     layout->addWidget( button );
835                     break;
836                 }
837                 case VLC_VAR_VOID:
838                 {
839                     if( i_type & VLC_VAR_ISCOMMAND )
840                     {
841                         QPushButton *button = new QPushButton( name, box );
842                         button->setObjectName( qfu( psz_var ) );
843
844                         CONNECT( button, clicked( bool ), this,
845                                  ValueChange( bool ) );
846                         layout->addWidget( button );
847                     }
848                     else
849                     {
850                         QLabel *label = new QLabel( name, box );
851                         layout->addWidget( label );
852                     }
853                     break;
854                 }
855                 default:
856                     msg_Warn( p_intf, "Unhandled var type for %s", psz_var );
857                     break;
858             }
859         }
860         var_FreeList( &val, &text );
861         vlc_object_release( p_obj );
862     }
863     else
864     {
865         msg_Dbg( p_intf, "Couldn't find v4l2 instance" );
866         help->show();
867         if ( isVisible() )
868             QTimer::singleShot( 2000, this, SLOT(Refresh()) );
869     }
870 }
871
872 void ExtV4l2::ValueChange( bool value )
873 {
874     ValueChange( (int)value );
875 }
876
877 void ExtV4l2::ValueChange( int value )
878 {
879     QObject *s = sender();
880     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( pl_Get(p_intf), "v4l2" );
881     if( p_obj )
882     {
883         QString var = s->objectName();
884         int i_type = var_Type( p_obj, qtu( var ) );
885         switch( i_type & VLC_VAR_TYPE )
886         {
887             case VLC_VAR_INTEGER:
888                 if( i_type & VLC_VAR_HASCHOICE )
889                 {
890                     QComboBox *combobox = qobject_cast<QComboBox*>( s );
891                     value = combobox->itemData( value ).toInt();
892                 }
893                 var_SetInteger( p_obj, qtu( var ), value );
894                 break;
895             case VLC_VAR_BOOL:
896                 var_SetBool( p_obj, qtu( var ), value );
897                 break;
898             case VLC_VAR_VOID:
899                 var_TriggerCallback( p_obj, qtu( var ) );
900                 break;
901         }
902         vlc_object_release( p_obj );
903     }
904     else
905     {
906         msg_Warn( p_intf, "Oops, v4l2 object isn't available anymore" );
907         Refresh();
908     }
909 }
910
911 /**********************************************************************
912  * Equalizer
913  **********************************************************************/
914
915 static const QString vlc_band_frequencies[] =
916 {
917     "  60 Hz  ", " 170 Hz ", " 310 Hz ", " 600 Hz ", "  1 kHz ",
918     "  3 kHz  ", "  6 kHz ", " 12 kHz ", " 14 kHz ", " 16 kHz "
919 };
920
921 static const QString iso_band_frequencies[] =
922 {
923     " 31.25 Hz ", " 62.5 Hz ", " 125 Hz ", " 250 Hz ", " 500 Hz ",
924     "    1 kHz ", "   2 kHz ", "  4 kHz ", "  8 kHz ", " 16 kHz "
925 };
926
927 Equalizer::Equalizer( intf_thread_t *_p_intf, QWidget *_parent ) :
928                             QWidget( _parent ) , p_intf( _p_intf )
929 {
930     QFont smallFont = QApplication::font();
931     smallFont.setPointSize( smallFont.pointSize() - 3 );
932
933     ui.setupUi( this );
934     ui.preampLabel->setFont( smallFont );
935
936     /* Setup of presetsComboBox */
937     presetsComboBox = ui.presetsCombo;
938     CONNECT( presetsComboBox, activated( int ), this, setCorePreset( int ) );
939
940     b_vlcBands = var_InheritBool( p_intf, "equalizer-vlcfreqs" );
941
942     /* Add the sliders for the Bands */
943     QGridLayout *grid = new QGridLayout( ui.frame );
944     grid->setMargin( 0 );
945     for( int i = 0 ; i < BANDS ; i++ )
946     {
947         bands[i] = new QSlider( Qt::Vertical );
948         bands[i]->setMaximum( 400 );
949         bands[i]->setValue( 200 );
950         bands[i]->setMinimumWidth(36);
951         CONNECT( bands[i], valueChanged( int ), this, setCoreBands() );
952
953         QString val = QString("%1").arg( 0.0, 5, 'f', 1 );
954         band_texts[i] = new QLabel( (b_vlcBands ? vlc_band_frequencies[i]
955                                                : iso_band_frequencies[i]) + "\n" + val + "dB" );
956         band_texts[i]->setFont( smallFont );
957
958         grid->addWidget( bands[i], 0, i );
959         grid->addWidget( band_texts[i], 1, i );
960     }
961
962     /* Add the listed presets */
963     for( int i = 0 ; i < NB_PRESETS ; i ++ )
964     {
965         presetsComboBox->addItem( qtr( preset_list_text[i] ),
966                                   QVariant( preset_list[i] ) );
967     }
968
969     /* Connects */
970     BUTTONACT( ui.enableCheck, enable() );
971     BUTTONACT( ui.eq2PassCheck, set2Pass() );
972     CONNECT( ui.preampSlider, valueChanged( int ), this, setPreamp() );
973
974     /* Do the update from the value of the core */
975     updateUIFromCore();
976 }
977
978 /* Write down initial values */
979 void Equalizer::updateUIFromCore()
980 {
981     char *psz_af, *psz_pres, *psz_bands;
982     float f_preamp;
983     int i_preset;
984
985     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
986     if( p_aout )
987     {
988         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
989         psz_pres = var_GetString( p_aout, "equalizer-preset" );
990         if( var_GetBool( p_aout, "equalizer-2pass" ) )
991             ui.eq2PassCheck->setChecked( true );
992         f_preamp = var_GetFloat( p_aout, "equalizer-preamp" );
993         psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
994         i_preset = presetsComboBox->findData( QVariant( psz_pres ) );
995         vlc_object_release( p_aout );
996     }
997     else
998     {
999         psz_af = config_GetPsz( p_intf, "audio-filter" );
1000         psz_pres = config_GetPsz( p_intf, "equalizer-preset" );
1001         if( config_GetInt( p_intf, "equalizer-2pass" ) )
1002             ui.eq2PassCheck->setChecked( true );
1003         f_preamp = config_GetFloat( p_intf, "equalizer-preamp" );
1004         psz_bands = config_GetPsz( p_intf, "equalizer-bands" );
1005         i_preset = presetsComboBox->findData( QVariant( psz_pres ) );
1006     }
1007     if( psz_af && strstr( psz_af, "equalizer" ) != NULL )
1008         ui.enableCheck->setChecked( true );
1009     enable( ui.enableCheck->isChecked() );
1010
1011     presetsComboBox->setCurrentIndex( i_preset );
1012
1013     ui.preampSlider->setValue( (int)( ( f_preamp + 20 ) * 10 ) );
1014
1015     if( psz_bands && strlen( psz_bands ) > 1 )
1016     {
1017         char *psz_bands_orig = psz_bands;
1018         for( int i = 0; i < BANDS; i++ )
1019         {
1020             const float f = us_strtod(psz_bands, &psz_bands );
1021             bands[i]->setValue( (int)( ( f + 20 ) * 10 )  );
1022             if( psz_bands == NULL || *psz_bands == '\0' ) break;
1023             psz_bands++;
1024             if( *psz_bands == '\0' ) break;
1025         }
1026         free( psz_bands_orig );
1027     }
1028     else free( psz_bands );
1029
1030     free( psz_af );
1031     free( psz_pres );
1032 }
1033
1034 void Equalizer::changeFreqLabels( bool b_useVlcBands )
1035 {
1036     b_vlcBands = b_useVlcBands;
1037
1038     const QString *band_frequencies = b_vlcBands
1039                                     ? vlc_band_frequencies
1040                                     : iso_band_frequencies;
1041
1042     for( int i = 0; i < BANDS; i++ )
1043     {
1044         const float f_val = (float)( bands[i]->value() ) / 10 - 20;
1045         QString val = QString("%1").arg( f_val, 5, 'f', 1 );
1046         band_texts[i]->setText( band_frequencies[i] + "\n" + val + "dB" );
1047     }
1048 }
1049
1050 /* Function called when enableButton is toggled */
1051 void Equalizer::enable()
1052 {
1053     bool en = ui.enableCheck->isChecked();
1054     aout_EnableFilter( THEPL, "equalizer", en );
1055 //    aout_EnableFilter( THEPL, "upmixer", en );
1056 //     aout_EnableFilter( THEPL, "vsurround", en );
1057     enable( en );
1058
1059     if( presetsComboBox->currentIndex() < 0 )
1060         presetsComboBox->setCurrentIndex( 0 );
1061
1062 }
1063
1064 void Equalizer::enable( bool en )
1065 {
1066     ui.eq2PassCheck->setEnabled( en );
1067     presetsComboBox->setEnabled( en );
1068     ui.presetLabel->setEnabled( en );
1069     ui.preampLabel->setEnabled( en );
1070     ui.preampSlider->setEnabled( en  );
1071     for( int i = 0 ; i< BANDS; i++ )
1072     {
1073         bands[i]->setEnabled( en ); band_texts[i]->setEnabled( en );
1074     }
1075 }
1076
1077 /* Function called when the set2Pass button is activated */
1078 void Equalizer::set2Pass()
1079 {
1080     vlc_object_t *p_aout= (vlc_object_t *)THEMIM->getAout();
1081     bool b_2p = ui.eq2PassCheck->isChecked();
1082
1083     if( p_aout )
1084     {
1085         var_SetBool( p_aout, "equalizer-2pass", b_2p );
1086         vlc_object_release( p_aout );
1087     }
1088     config_PutInt( p_intf, "equalizer-2pass", b_2p );
1089 }
1090
1091 /* Function called when the preamp slider is moved */
1092 void Equalizer::setPreamp()
1093 {
1094     const float f = ( float )(  ui.preampSlider->value() ) /10 - 20;
1095     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1096
1097     ui.preampLabel->setText( qtr( "Preamp\n" ) + QString::number( f, 'f', 1 )
1098                                                + qtr( "dB" ) );
1099     if( p_aout )
1100     {
1101         //delCallbacks( p_aout );
1102         var_SetFloat( p_aout, "equalizer-preamp", f );
1103         //addCallbacks( p_aout );
1104         vlc_object_release( p_aout );
1105     }
1106     config_PutFloat( p_intf, "equalizer-preamp", f );
1107 }
1108
1109 void Equalizer::setCoreBands()
1110 {
1111     /**\todo smoothing */
1112
1113     const QString *band_frequencies = b_vlcBands
1114                                     ? vlc_band_frequencies
1115                                     : iso_band_frequencies;
1116
1117     QString values;
1118     for( int i = 0; i < BANDS; i++ )
1119     {
1120         const float f_val = (float)( bands[i]->value() ) / 10 - 20;
1121         QString val = QString("%1").arg( f_val, 5, 'f', 1 );
1122
1123         band_texts[i]->setText( band_frequencies[i] + "\n" + val + "dB" );
1124         values += " " + val;
1125     }
1126
1127     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1128     if( p_aout )
1129     {
1130         //delCallbacks( p_aout );
1131         var_SetString( p_aout, "equalizer-bands", qtu( values ) );
1132         //addCallbacks( p_aout );
1133         vlc_object_release( p_aout );
1134     }
1135 }
1136
1137 char * Equalizer::createValuesFromPreset( int i_preset )
1138 {
1139     QString values;
1140
1141     /* Create the QString in Qt */
1142     for( int i = 0 ; i< BANDS ;i++ )
1143         values += QString( " %1" ).arg( eqz_preset_10b[i_preset].f_amp[i], 5, 'f', 1 );
1144
1145     /* Convert it to char * */
1146     return strdup( values.toAscii().constData() );
1147 }
1148
1149 void Equalizer::setCorePreset( int i_preset )
1150 {
1151     if( i_preset < 0 )
1152         return;
1153
1154     /* Update pre-amplification in the UI */
1155     float f_preamp = eqz_preset_10b[i_preset].f_preamp;
1156     ui.preampSlider->setValue( (int)( ( f_preamp + 20 ) * 10 ) );
1157     ui.preampLabel->setText( qtr( "Preamp\n" )
1158                    + QString::number( f_preamp, 'f', 1 ) + qtr( "dB" ) );
1159
1160     char *psz_values = createValuesFromPreset( i_preset );
1161     if( !psz_values ) return ;
1162
1163     const QString *band_frequencies = b_vlcBands
1164                                     ? vlc_band_frequencies
1165                                     : iso_band_frequencies;
1166
1167     char *p = psz_values;
1168     for( int i = 0; i < BANDS && *p; i++ )
1169     {
1170         const float f = us_strtod( p, &p );
1171
1172         bands[i]->setValue( (int)( ( f + 20 ) * 10 )  );
1173         band_texts[i]->setText( band_frequencies[i] + "\n"
1174                               + QString("%1").arg( f, 5, 'f', 1 ) + "dB" );
1175         if( *p )
1176             p++; /* skip separator */
1177     }
1178
1179     /* Apply presets to audio output */
1180     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1181     if( p_aout )
1182     {
1183         var_SetString( p_aout , "equalizer-preset" , preset_list[i_preset] );
1184
1185         var_SetString( p_aout, "equalizer-bands", psz_values );
1186         var_SetFloat( p_aout, "equalizer-preamp",
1187                       eqz_preset_10b[i_preset].f_preamp );
1188         vlc_object_release( p_aout );
1189     }
1190     config_PutPsz( p_intf, "equalizer-bands", psz_values );
1191     config_PutPsz( p_intf, "equalizer-preset", preset_list[i_preset] );
1192     config_PutFloat( p_intf, "equalizer-preamp",
1193                     eqz_preset_10b[i_preset].f_preamp );
1194     free( psz_values );
1195 }
1196
1197 static int PresetCallback( vlc_object_t *p_this, char const *psz_cmd,
1198                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1199 {
1200     VLC_UNUSED( p_this ); VLC_UNUSED( psz_cmd ); VLC_UNUSED( oldval );
1201
1202     char *psz_preset = newval.psz_string;
1203     Equalizer *eq = ( Equalizer * )p_data;
1204     int i_preset = eq->presetsComboBox->findData( QVariant( psz_preset ) );
1205     eq->presetsComboBox->setCurrentIndex( i_preset );
1206     return VLC_SUCCESS;
1207 }
1208
1209 void Equalizer::delCallbacks( vlc_object_t *p_aout )
1210 {
1211     //var_DelCallback( p_aout, "equalizer-bands", EqzCallback, this );
1212     //var_DelCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1213     var_DelCallback( p_aout, "equalizer-preset", PresetCallback, this );
1214 }
1215
1216 void Equalizer::addCallbacks( vlc_object_t *p_aout )
1217 {
1218     //var_AddCallback( p_aout, "equalizer-bands", EqzCallback, this );
1219     //var_AddCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1220     var_AddCallback( p_aout, "equalizer-preset", PresetCallback, this );
1221 }
1222
1223 /**********************************************************************
1224  * Audio filters
1225  **********************************************************************/
1226
1227 /**********************************************************************
1228  * Dynamic range compressor
1229  **********************************************************************/
1230
1231 typedef struct
1232 {
1233     const char *psz_name;
1234     const char *psz_descs;
1235     const char *psz_units;
1236     const float f_min;      // min
1237     const float f_max;      // max
1238     const float f_value;    // value
1239     const float f_resolution; // resolution
1240 } comp_controls_t;
1241
1242 static const comp_controls_t comp_controls[] =
1243 {
1244     { "compressor-rms-peak",    _("RMS/peak"),       "",       0.0f,   1.0f,   0.00f, 0.001f },
1245     { "compressor-attack",      _("Attack"),       _(" ms"),   1.5f, 400.0f,  25.00f, 0.100f },
1246     { "compressor-release",     _("Release"),      _(" ms"),   2.0f, 800.0f, 100.00f, 0.100f },
1247     { "compressor-threshold",   _("Threshold"),    _(" dB"), -30.0f,   0.0f, -11.00f, 0.010f },
1248     { "compressor-ratio",       _("Ratio"),          ":1",     1.0f,  20.0f,   8.00f, 0.010f },
1249     { "compressor-knee",        _("Knee\nradius"), _(" dB"),   1.0f,  10.0f,   2.50f, 0.010f },
1250     { "compressor-makeup-gain", _("Makeup\ngain"), _(" dB"),   0.0f,  24.0f,   7.00f, 0.010f },
1251 };
1252
1253 Compressor::Compressor( intf_thread_t *_p_intf, QWidget *_parent )
1254            : QWidget( _parent ) , p_intf( _p_intf )
1255 {
1256     QFont smallFont = QApplication::font();
1257     smallFont.setPointSize( smallFont.pointSize() - 2 );
1258
1259     QGridLayout *layout = new QGridLayout( this );
1260
1261     enableCheck = new QCheckBox( qtr( "Enable dynamic range compressor" ) );
1262     layout->addWidget( enableCheck, 0, 0, 1, NUM_CP_CTRL );
1263
1264     for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
1265     {
1266         const int i_min = (int)( comp_controls[i].f_min
1267                                / comp_controls[i].f_resolution );
1268         const int i_max = (int)( comp_controls[i].f_max
1269                                / comp_controls[i].f_resolution );
1270         const int i_val = (int)( comp_controls[i].f_value
1271                                / comp_controls[i].f_resolution );
1272
1273         compCtrl[i] = new QSlider( Qt::Vertical );
1274         compCtrl[i]->setMinimum( i_min );
1275         compCtrl[i]->setMaximum( i_max );
1276         compCtrl[i]->setValue(   i_val );
1277
1278         oldControlVars[i] = comp_controls[i].f_value;
1279
1280         ctrl_texts[i] = new QLabel( qtr( comp_controls[i].psz_descs ) + "\n" );
1281         ctrl_texts[i]->setFont( smallFont );
1282         ctrl_texts[i]->setAlignment( Qt::AlignHCenter );
1283
1284         ctrl_readout[i] = new QLabel;
1285         ctrl_readout[i]->setFont( smallFont );
1286         ctrl_readout[i]->setAlignment( Qt::AlignHCenter );
1287
1288         layout->addWidget( compCtrl[i],     1, i, Qt::AlignHCenter );
1289         layout->addWidget( ctrl_readout[i], 2, i, Qt::AlignHCenter );
1290         layout->addWidget( ctrl_texts[i],   3, i, Qt::AlignHCenter );
1291     }
1292
1293     for( int i = 0; i < NUM_CP_CTRL; i++ )
1294         CONNECT( compCtrl[i], valueChanged( int ), this, setInitValues() );
1295
1296     BUTTONACT( enableCheck, enable() );
1297
1298     /* Write down initial values */
1299     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1300     char *psz_af;
1301
1302     if( p_aout )
1303     {
1304         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
1305         for( int i = 0; i < NUM_CP_CTRL; i++ )
1306         {
1307             controlVars[i] = var_GetFloat( p_aout,
1308                                            comp_controls[i].psz_name );
1309         }
1310         vlc_object_release( p_aout );
1311     }
1312     else
1313     {
1314         psz_af = config_GetPsz( p_intf, "audio-filter" );
1315         for( int i = 0; i < NUM_CP_CTRL; i++ )
1316         {
1317             controlVars[i] = config_GetFloat( p_intf,
1318                                               comp_controls[i].psz_name );
1319         }
1320     }
1321     if( psz_af && strstr( psz_af, "compressor" ) != NULL )
1322     {
1323         enableCheck->setChecked( true );
1324     }
1325     free( psz_af );
1326     enable( enableCheck->isChecked() );
1327     updateSliders( controlVars );
1328     setValues();
1329 }
1330
1331 void Compressor::enable()
1332 {
1333     bool en = enableCheck->isChecked();
1334     aout_EnableFilter( THEPL, "compressor", en );
1335     enable( en );
1336 }
1337
1338 void Compressor::enable( bool en )
1339 {
1340     for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
1341     {
1342         compCtrl[i]->setEnabled( en );
1343         ctrl_texts[i]->setEnabled( en );
1344         ctrl_readout[i]->setEnabled( en );
1345     }
1346 }
1347
1348 void Compressor::updateSliders( float * p_controlVars )
1349 {
1350     for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
1351     {
1352         if( oldControlVars[i] != p_controlVars[i] )
1353         {
1354             compCtrl[i]->setValue(
1355                     (int)( p_controlVars[i] / comp_controls[i].f_resolution ) );
1356         }
1357     }
1358 }
1359
1360 void Compressor::setInitValues()
1361 {
1362     setValues();
1363 }
1364
1365 void Compressor::setValues()
1366 {
1367     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1368
1369     for( int i = 0 ; i < NUM_CP_CTRL ; i++ )
1370     {
1371         float f = (float)( compCtrl[i]->value() ) * ( comp_controls[i].f_resolution );
1372         ctrl_readout[i]->setText( QString::number( f, 'f', 1 )
1373                                 + qtr( comp_controls[i].psz_units ) );
1374         if( oldControlVars[i] != f )
1375         {
1376             if( p_aout )
1377             {
1378                 var_SetFloat( p_aout, comp_controls[i].psz_name, f );
1379             }
1380             config_PutFloat( p_intf, comp_controls[i].psz_name, f );
1381             oldControlVars[i] = f;
1382         }
1383     }
1384     if( p_aout )
1385     {
1386         vlc_object_release( p_aout );
1387     }
1388 }
1389
1390 /**********************************************************************
1391  * Spatializer
1392  **********************************************************************/
1393 typedef struct
1394 {
1395     const char *psz_name;
1396     const char *psz_desc;
1397 } spat_controls_t;
1398
1399 static const spat_controls_t spat_controls[] =
1400 {
1401     { "spatializer-roomsize", _("Size") },
1402     { "spatializer-width",    _("Width") },
1403     { "spatializer-wet",      _("Wet") },
1404     { "spatializer-dry",      _("Dry") },
1405     { "spatializer-damp",     _("Damp") },
1406 };
1407
1408 Spatializer::Spatializer( intf_thread_t *_p_intf, QWidget *_parent )
1409             : QWidget( _parent ) , p_intf( _p_intf )
1410 {
1411     QFont smallFont = QApplication::font();
1412     smallFont.setPointSize( smallFont.pointSize() - 1 );
1413
1414     QGridLayout *layout = new QGridLayout( this );
1415
1416     enableCheck = new QCheckBox( qtr( "Enable spatializer" ) );
1417     layout->addWidget( enableCheck, 0, 0, 1, NUM_SP_CTRL );
1418
1419     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1420     {
1421         spatCtrl[i] = new QSlider( Qt::Vertical );
1422         spatCtrl[i]->setValue( (int)var_InheritFloat( p_intf, spat_controls[i].psz_name ) * 10. );
1423         oldControlVars[i] = spatCtrl[i]->value();
1424
1425         ctrl_texts[i] = new QLabel( qtr( spat_controls[i].psz_desc ) + "\n" );
1426         ctrl_texts[i]->setFont( smallFont );
1427
1428         ctrl_readout[i] = new QLabel;
1429         ctrl_readout[i]->setFont( smallFont );
1430
1431         layout->addWidget( spatCtrl[i],     1, i, Qt::AlignHCenter );
1432         layout->addWidget( ctrl_readout[i], 2, i, Qt::AlignHCenter );
1433         layout->addWidget( ctrl_texts[i],   3, i, Qt::AlignHCenter );
1434         spatCtrl[i]->setRange( 0, 10 );
1435     }
1436     spatCtrl[0]->setRange( 0, 11 );
1437
1438     for( int i = 0; i < NUM_SP_CTRL; i++ )
1439         CONNECT( spatCtrl[i], valueChanged( int ), this, setInitValues() );
1440
1441     BUTTONACT( enableCheck, enable() );
1442
1443     /* Write down initial values */
1444     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1445     char *psz_af;
1446
1447     if( p_aout )
1448     {
1449         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
1450         for( int i = 0; i < NUM_SP_CTRL ; i++ )
1451         {
1452             controlVars[i] = var_GetFloat( p_aout, spat_controls[i].psz_name );
1453         }
1454         vlc_object_release( p_aout );
1455     }
1456     else
1457     {
1458         psz_af = config_GetPsz( p_intf, "audio-filter" );
1459         for( int i = 0; i < NUM_SP_CTRL ; i++ )
1460         {
1461             controlVars[i] = config_GetFloat( p_intf, spat_controls[i].psz_name );
1462         }
1463     }
1464     if( psz_af && strstr( psz_af, "spatializer" ) != NULL )
1465         enableCheck->setChecked( true );
1466     free( psz_af );
1467     enable( enableCheck->isChecked() );
1468     setValues();
1469 }
1470
1471 void Spatializer::enable()
1472 {
1473     bool en = enableCheck->isChecked();
1474     aout_EnableFilter( THEPL, "spatializer", en );
1475     enable( en );
1476 }
1477
1478 void Spatializer::enable( bool en )
1479 {
1480     for( int i = 0 ; i< NUM_SP_CTRL; i++ )
1481     {
1482         spatCtrl[i]->setEnabled( en );
1483         ctrl_texts[i]->setEnabled( en );
1484         ctrl_readout[i]->setEnabled( en );
1485     }
1486 }
1487 void Spatializer::setInitValues()
1488 {
1489     setValues();
1490 }
1491
1492 void Spatializer::setValues()
1493 {
1494     vlc_object_t *p_aout = (vlc_object_t *)THEMIM->getAout();
1495
1496     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1497     {
1498         float f = (float)( spatCtrl[i]->value() ) / 10;
1499         ctrl_readout[i]->setText( QString::number( f, 'f',  1 ) );
1500     }
1501
1502     if( p_aout )
1503     {
1504         for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1505         {
1506             float f = (float)( spatCtrl[i]->value() ) / 10 ;
1507             if( oldControlVars[i] != spatCtrl[i]->value() )
1508             {
1509                 var_SetFloat( p_aout, spat_controls[i].psz_name, f );
1510                 config_PutFloat( p_intf, spat_controls[i].psz_name, f );
1511                 oldControlVars[i] = spatCtrl[i]->value();
1512             }
1513         }
1514         vlc_object_release( p_aout );
1515     }
1516
1517 }
1518 void Spatializer::delCallbacks( vlc_object_t *p_aout )
1519 {
1520     VLC_UNUSED( p_aout );
1521     //    var_DelCallback( p_aout, "Spatializer-bands", EqzCallback, this );
1522     //    var_DelCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
1523 }
1524
1525 void Spatializer::addCallbacks( vlc_object_t *p_aout )
1526 {
1527     VLC_UNUSED( p_aout );
1528     //    var_AddCallback( p_aout, "Spatializer-bands", EqzCallback, this );
1529     //    var_AddCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
1530 }
1531
1532 #include <QToolButton>
1533 #include <QGridLayout>
1534
1535 #define SUBSDELAY_CFG_MODE                     "subsdelay-mode"
1536 #define SUBSDELAY_CFG_FACTOR                   "subsdelay-factor"
1537 #define SUBSDELAY_MODE_ABSOLUTE                0
1538 #define SUBSDELAY_MODE_RELATIVE_SOURCE_DELAY   1
1539 #define SUBSDELAY_MODE_RELATIVE_SOURCE_CONTENT 2
1540
1541 SyncWidget::SyncWidget( QWidget *_parent ) : QWidget( _parent )
1542 {
1543     QHBoxLayout *layout = new QHBoxLayout;
1544     spinBox.setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1545     spinBox.setDecimals( 3 );
1546     spinBox.setMinimum( -600.0 );
1547     spinBox.setMaximum( 600.0 );
1548     spinBox.setSingleStep( 0.1 );
1549     spinBox.setSuffix( " s" );
1550     spinBox.setButtonSymbols( QDoubleSpinBox::PlusMinus );
1551     CONNECT( &spinBox, valueChanged( double ), this, valueChangedHandler( double ) );
1552     layout->addWidget( &spinBox );
1553     layout->addWidget( &spinLabel );
1554     layout->setContentsMargins( 0, 0, 0, 0 );
1555     setLayout( layout );
1556 }
1557
1558 void SyncWidget::valueChangedHandler( double d )
1559 {
1560     if ( d < 0 )
1561         spinLabel.setText( qtr("(Hastened)") );
1562     else if ( d > 0 )
1563         spinLabel.setText( qtr("(Delayed)") );
1564     else
1565         spinLabel.setText( "" );
1566     emit valueChanged( d );
1567 }
1568
1569 void SyncWidget::setValue( double d )
1570 {
1571     spinBox.setValue( d );
1572 }
1573
1574 SyncControls::SyncControls( intf_thread_t *_p_intf, QWidget *_parent ) :
1575                             QWidget( _parent ) , p_intf( _p_intf )
1576 {
1577     QGroupBox *AVBox, *subsBox;
1578     QToolButton *updateButton;
1579
1580     b_userAction = true;
1581
1582     QGridLayout *mainLayout = new QGridLayout( this );
1583
1584     /* AV sync */
1585     AVBox = new QGroupBox( qtr( "Audio/Video" ) );
1586     QGridLayout *AVLayout = new QGridLayout( AVBox );
1587
1588     QLabel *AVLabel = new QLabel;
1589     AVLabel->setText( qtr( "Audio track synchronization:" ) );
1590     AVLayout->addWidget( AVLabel, 0, 0, 1, 1 );
1591
1592     AVSpin = new SyncWidget( this );
1593     AVLayout->addWidget( AVSpin, 0, 2, 1, 1 );
1594     mainLayout->addWidget( AVBox, 1, 0, 1, 5 );
1595
1596     /* Subs */
1597     subsBox = new QGroupBox( qtr( "Subtitles/Video" ) );
1598     QGridLayout *subsLayout = new QGridLayout( subsBox );
1599
1600     QLabel *subsLabel = new QLabel;
1601     subsLabel->setText( qtr( "Subtitle track syncronization:" ) );
1602     subsLayout->addWidget( subsLabel, 0, 0, 1, 1 );
1603
1604     subsSpin = new SyncWidget( this );
1605     subsLayout->addWidget( subsSpin, 0, 2, 1, 1 );
1606
1607     QLabel *subSpeedLabel = new QLabel;
1608     subSpeedLabel->setText( qtr( "Subtitles speed:" ) );
1609     subsLayout->addWidget( subSpeedLabel, 1, 0, 1, 1 );
1610
1611     subSpeedSpin = new QDoubleSpinBox;
1612     subSpeedSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1613     subSpeedSpin->setDecimals( 3 );
1614     subSpeedSpin->setMinimum( 1 );
1615     subSpeedSpin->setMaximum( 100 );
1616     subSpeedSpin->setSingleStep( 0.2 );
1617     subSpeedSpin->setSuffix( " fps" );
1618     subSpeedSpin->setButtonSymbols( QDoubleSpinBox::PlusMinus );
1619     subsLayout->addWidget( subSpeedSpin, 1, 2, 1, 1 );
1620
1621     QLabel *subDurationLabel = new QLabel;
1622     subDurationLabel->setText( qtr( "Subtitles duration factor:" ) );
1623     subsLayout->addWidget( subDurationLabel, 2, 0, 1, 1 );
1624
1625     subDurationSpin = new QDoubleSpinBox;
1626     subDurationSpin->setAlignment( Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter );
1627     subDurationSpin->setDecimals( 3 );
1628     subDurationSpin->setMinimum( 0 );
1629     subDurationSpin->setMaximum( 20 );
1630     subDurationSpin->setSingleStep( 0.2 );
1631     subDurationSpin->setButtonSymbols( QDoubleSpinBox::PlusMinus );
1632     subsLayout->addWidget( subDurationSpin, 2, 2, 1, 1 );
1633
1634     mainLayout->addWidget( subsBox, 2, 0, 2, 5 );
1635
1636     updateButton = new QToolButton;
1637     updateButton->setAutoRaise( true );
1638     mainLayout->addWidget( updateButton, 0, 4, 1, 1 );
1639
1640     /* Various Connects */
1641     CONNECT( AVSpin, valueChanged ( double ), this, advanceAudio( double ) ) ;
1642     CONNECT( subsSpin, valueChanged ( double ), this, advanceSubs( double ) ) ;
1643     CONNECT( subSpeedSpin, valueChanged ( double ),
1644              this, adjustSubsSpeed( double ) );
1645     CONNECT( subDurationSpin, valueChanged ( double ),
1646              this, adjustSubsDuration( double ) );
1647
1648     CONNECT( THEMIM->getIM(), synchroChanged(), this, update() );
1649     BUTTON_SET_ACT_I( updateButton, "", update,
1650             qtr( "Force update of this dialog's values" ), update() );
1651
1652     initSubsDuration();
1653
1654     /* Set it */
1655     update();
1656 }
1657
1658 SyncControls::~SyncControls()
1659 {
1660     subsdelayClean();
1661 }
1662
1663 void SyncControls::clean()
1664 {
1665     b_userAction = false;
1666     AVSpin->setValue( 0.0 );
1667     subsSpin->setValue( 0.0 );
1668     subSpeedSpin->setValue( 1.0 );
1669     subsdelayClean();
1670     b_userAction = true;
1671 }
1672
1673 void SyncControls::update()
1674 {
1675     b_userAction = false;
1676
1677     int64_t i_delay;
1678     if( THEMIM->getInput() )
1679     {
1680         i_delay = var_GetTime( THEMIM->getInput(), "audio-delay" );
1681         AVSpin->setValue( ( (double)i_delay ) / 1000000 );
1682         i_delay = var_GetTime( THEMIM->getInput(), "spu-delay" );
1683         subsSpin->setValue( ( (double)i_delay ) / 1000000 );
1684         subSpeedSpin->setValue( var_GetFloat( THEMIM->getInput(), "sub-fps" ) );
1685         subDurationSpin->setValue( var_InheritFloat( p_intf, SUBSDELAY_CFG_FACTOR ) );
1686     }
1687     b_userAction = true;
1688 }
1689
1690 void SyncControls::advanceAudio( double f_advance )
1691 {
1692     if( THEMIM->getInput() && b_userAction )
1693     {
1694         int64_t i_delay = f_advance * 1000000;
1695         var_SetTime( THEMIM->getInput(), "audio-delay", i_delay );
1696     }
1697 }
1698
1699 void SyncControls::advanceSubs( double f_advance )
1700 {
1701     if( THEMIM->getInput() && b_userAction )
1702     {
1703         int64_t i_delay = f_advance * 1000000;
1704         var_SetTime( THEMIM->getInput(), "spu-delay", i_delay );
1705     }
1706 }
1707
1708 void SyncControls::adjustSubsSpeed( double f_fps )
1709 {
1710     if( THEMIM->getInput() && b_userAction )
1711     {
1712         var_SetFloat( THEMIM->getInput(), "sub-fps", f_fps );
1713     }
1714 }
1715
1716 void SyncControls::adjustSubsDuration( double f_factor )
1717 {
1718     if( THEMIM->getInput() && b_userAction )
1719     {
1720         subsdelaySetFactor( f_factor );
1721         ChangeVFiltersString( p_intf, "subsdelay", f_factor > 0 );
1722     }
1723 }
1724
1725 void SyncControls::initSubsDuration()
1726 {
1727     int i_mode = var_InheritInteger( p_intf, SUBSDELAY_CFG_MODE );
1728
1729     switch (i_mode)
1730     {
1731     default:
1732     case SUBSDELAY_MODE_ABSOLUTE:
1733         subDurationSpin->setToolTip( qtr( "Extend subtitles duration by this value.\n"
1734                                           "Set 0 to disable." ) );
1735         subDurationSpin->setSuffix( " s" );
1736         break;
1737     case SUBSDELAY_MODE_RELATIVE_SOURCE_DELAY:
1738         subDurationSpin->setToolTip( qtr( "Multiply subtitles duration by this value.\n"
1739                                           "Set 0 to disable." ) );
1740         subDurationSpin->setSuffix( "" );
1741         break;
1742     case SUBSDELAY_MODE_RELATIVE_SOURCE_CONTENT:
1743         subDurationSpin->setToolTip( qtr( "Recalculate subtitles duration according\n"
1744                                           "to their content and this value.\n"
1745                                           "Set 0 to disable." ) );
1746         subDurationSpin->setSuffix( "" );
1747         break;
1748     }
1749 }
1750
1751 void SyncControls::subsdelayClean()
1752 {
1753     /* Remove subsdelay filter */
1754     ChangeVFiltersString( p_intf, "subsdelay", false );
1755 }
1756
1757 void SyncControls::subsdelaySetFactor( double f_factor )
1758 {
1759     /* Set the factor in the preferences */
1760     config_PutFloat( p_intf, SUBSDELAY_CFG_FACTOR, f_factor );
1761
1762     /* Try to find an instance of subsdelay, and set its factor */
1763     vlc_object_t *p_obj = ( vlc_object_t * ) vlc_object_find_name( p_intf->p_libvlc, "subsdelay" );
1764     if( p_obj )
1765     {
1766         var_SetFloat( p_obj, SUBSDELAY_CFG_FACTOR, f_factor );
1767         vlc_object_release( p_obj );
1768     }
1769 }
1770
1771
1772 /**********************************************************************
1773  * Video filters / Adjust
1774  **********************************************************************/
1775
1776 /**********************************************************************
1777  * Extended playbak controls
1778  **********************************************************************/