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