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