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