]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/extended_panels.cpp
Qt4 - Extended Panels. try to fix the sizing ...
[vlc] / modules / gui / qt4 / components / extended_panels.cpp
1 /*****************************************************************************
2  * extended_panels.cpp : Extended controls panels
3  ****************************************************************************
4  * Copyright ( C ) 2006-2007 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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <QLabel>
29 #include <QVariant>
30 #include <QString>
31 #include <QFont>
32 #include <QGridLayout>
33 #include <QSignalMapper>
34 #include <QComboBox>
35
36 #include "components/extended_panels.hpp"
37 #include "dialogs/preferences.hpp"
38 #include "dialogs_provider.hpp"
39 #include "qt4.hpp"
40
41 #include "../../audio_filter/equalizer_presets.h"
42 #include <vlc_aout.h>
43 #include <vlc_intf_strings.h>
44 #include <vlc_vout.h>
45 #include <vlc_osd.h>
46
47 #include <iostream>
48 #include <string.h>
49
50 #if 0
51 class ConfClickHandler : public QObject
52 {
53 public:
54     ConfClickHandler( intf_thread_t *_p_intf, ExtVideo *_e ) : QObject ( _e ) {
55         e = _e; p_intf = _p_intf;
56     }
57     virtual ~ConfClickHandler() {}
58     bool eventFilter( QObject *obj, QEvent *evt )
59     {
60         if( evt->type() == QEvent::MouseButtonPress )
61         {
62             e->gotoConf( obj );
63             return true;
64         }
65         return false;
66     }
67 private:
68     ExtVideo* e;
69     intf_thread_t *p_intf;
70 };
71 #endif
72
73 QString ModuleFromWidgetName( QObject *obj )
74 {
75     return obj->objectName().replace( "Enable","" );
76 }
77
78 QString OptionFromWidgetName( QObject *obj )
79 {
80     /* Gruik ? ... nah */
81     QString option = obj->objectName().replace( "Slider", "" )
82                                       .replace( "Combo" , "" )
83                                       .replace( "Dial"  , "" )
84                                       .replace( "Check" , "" )
85                                       .replace( "Spin"  , "" )
86                                       .replace( "Text"  , "" );
87     for( char a = 'A'; a <= 'Z'; a++ )
88     {
89         option = option.replace( QString( a ),
90                                  QString( '-' ) + QString( a + 'a' - 'A' ) );
91     }
92     return option;
93 }
94
95 ExtVideo::ExtVideo( intf_thread_t *_p_intf, QTabWidget *_parent ) :
96                            p_intf( _p_intf )
97 {
98     ui.setupUi( _parent );
99
100 #define SETUP_VFILTER( widget ) \
101     { \
102         vlc_object_t *p_obj = ( vlc_object_t * ) \
103             vlc_object_find_name( p_intf->p_libvlc, \
104                                   #widget, \
105                                   FIND_CHILD ); \
106         QCheckBox *checkbox = qobject_cast<QCheckBox*>( ui.widget##Enable ); \
107         QGroupBox *groupbox = qobject_cast<QGroupBox*>( ui.widget##Enable ); \
108         if( p_obj ) \
109         { \
110             vlc_object_release( p_obj ); \
111             if( checkbox ) checkbox->setChecked( true ); \
112             else groupbox->setChecked( true ); \
113         } \
114         else \
115         { \
116             if( checkbox ) checkbox->setChecked( false ); \
117             else groupbox->setChecked( false ); \
118         } \
119     } \
120     CONNECT( ui.widget##Enable, clicked(), this, updateFilters() );
121 #define SETUP_VFILTER_OPTION( widget, signal ) \
122     initComboBoxItems( ui.widget ); \
123     setWidgetValue( ui.widget ); \
124     CONNECT( ui.widget, signal, this, updateFilterOptions() );
125
126     SETUP_VFILTER( adjust )
127     SETUP_VFILTER_OPTION( hueSlider, valueChanged( int ) )
128     SETUP_VFILTER_OPTION( contrastSlider, valueChanged( int ) )
129     SETUP_VFILTER_OPTION( brightnessSlider, valueChanged( int ) )
130     SETUP_VFILTER_OPTION( saturationSlider, valueChanged( int ) )
131     SETUP_VFILTER_OPTION( gammaSlider, valueChanged( int ) )
132     SETUP_VFILTER_OPTION( brightnessThresholdCheck, stateChanged( int ) )
133
134     SETUP_VFILTER( extract )
135     SETUP_VFILTER_OPTION( extractComponentText, textChanged( QString ) )
136
137     SETUP_VFILTER( colorthres )
138     SETUP_VFILTER_OPTION( colorthresColorText, textChanged( QString ) )
139     SETUP_VFILTER_OPTION( colorthresSaturationthresSlider, valueChanged( int ) )
140     SETUP_VFILTER_OPTION( colorthresSimilaritythresSlider, valueChanged( int ) )
141
142     SETUP_VFILTER( invert )
143
144     SETUP_VFILTER( gradient )
145     SETUP_VFILTER_OPTION( gradientModeCombo, currentIndexChanged( QString ) )
146     SETUP_VFILTER_OPTION( gradientTypeCheck, stateChanged( int ) )
147     SETUP_VFILTER_OPTION( gradientCartoonCheck, stateChanged( int ) )
148
149     SETUP_VFILTER( motionblur )
150     SETUP_VFILTER_OPTION( blurFactorSlider, valueChanged( int ) )
151
152     SETUP_VFILTER( motiondetect )
153
154     SETUP_VFILTER( noise )
155
156     SETUP_VFILTER( psychedelic )
157
158     SETUP_VFILTER( sharpen )
159     SETUP_VFILTER_OPTION( sharpenSigmaSlider, valueChanged( int ) )
160
161     SETUP_VFILTER( ripple )
162
163     SETUP_VFILTER( wave )
164
165     SETUP_VFILTER( transform )
166     SETUP_VFILTER_OPTION( transformTypeCombo, currentIndexChanged( QString ) )
167
168     SETUP_VFILTER( rotate )
169     SETUP_VFILTER_OPTION( rotateAngleDial, valueChanged( int ) )
170     ui.rotateAngleDial->setWrapping( true );
171     ui.rotateAngleDial->setNotchesVisible( true );
172
173     SETUP_VFILTER( puzzle )
174     SETUP_VFILTER_OPTION( puzzleRowsSpin, valueChanged( int ) )
175     SETUP_VFILTER_OPTION( puzzleColsSpin, valueChanged( int ) )
176     SETUP_VFILTER_OPTION( puzzleBlackSlotCheck, stateChanged( int ) )
177
178     SETUP_VFILTER( magnify )
179
180     SETUP_VFILTER( clone )
181     SETUP_VFILTER_OPTION( cloneCountSpin, valueChanged( int ) )
182
183     SETUP_VFILTER( wall )
184     SETUP_VFILTER_OPTION( wallRowsSpin, valueChanged( int ) )
185     SETUP_VFILTER_OPTION( wallColsSpin, valueChanged( int ) )
186
187     SETUP_VFILTER( erase )
188     SETUP_VFILTER_OPTION( eraseMaskText, editingFinished() )
189     SETUP_VFILTER_OPTION( eraseYSpin, valueChanged( int ) )
190     SETUP_VFILTER_OPTION( eraseXSpin, valueChanged( int ) )
191
192     SETUP_VFILTER( marq )
193     SETUP_VFILTER_OPTION( marqMarqueeText, textChanged( QString ) )
194     SETUP_VFILTER_OPTION( marqPositionCombo, currentIndexChanged( QString ) )
195
196     SETUP_VFILTER( logo )
197     SETUP_VFILTER_OPTION( logoFileText, editingFinished() )
198     SETUP_VFILTER_OPTION( logoYSpin, valueChanged( int ) )
199     SETUP_VFILTER_OPTION( logoXSpin, valueChanged( int ) )
200     SETUP_VFILTER_OPTION( logoTransparencySlider, valueChanged( int ) )
201
202 #undef SETUP_VFILTER
203 #undef SETUP_VFILTER_OPTION
204 }
205
206 ExtVideo::~ExtVideo()
207 {
208 }
209
210 void ExtVideo::ChangeVFiltersString( char *psz_name, vlc_bool_t b_add )
211 {
212     vout_thread_t *p_vout;
213     char *psz_parser, *psz_string;
214     const char *psz_filter_type;
215
216     /* Please leave p_libvlc_global. This is where cached modules are
217      * stored. We're not trying to find a module instance. */
218     module_t *p_obj = module_Find( p_intf, psz_name );
219     if( !p_obj )
220     {
221         msg_Err( p_intf, "Unable to find filter module \"%s\n.", psz_name );
222         return;
223     }
224
225     if( module_IsCapable( p_obj, "video filter2" ) )
226     {
227         psz_filter_type = "video-filter";
228     }
229     else if( module_IsCapable( p_obj, "video filter" ) )
230     {
231         psz_filter_type = "vout-filter";
232     }
233     else if( module_IsCapable( p_obj, "sub filter" ) )
234     {
235         psz_filter_type = "sub-filter";
236     }
237     else
238     {
239         module_Put( p_obj );
240         msg_Err( p_intf, "Unknown video filter type." );
241         return;
242     }
243     module_Put( p_obj );
244
245     psz_string = config_GetPsz( p_intf, psz_filter_type );
246
247     if( !psz_string ) psz_string = strdup( "" );
248
249     psz_parser = strstr( psz_string, psz_name );
250
251     if( b_add )
252     {
253         if( !psz_parser )
254         {
255             psz_parser = psz_string;
256             asprintf( &psz_string, ( *psz_string ) ? "%s:%s" : "%s%s",
257                             psz_string, psz_name );
258             free( psz_parser );
259         }
260         else
261         {
262             return;
263         }
264     }
265     else
266     {
267         if( psz_parser )
268         {
269             if( *( psz_parser + strlen( psz_name ) ) == ':' )
270             {
271                 memmove( psz_parser, psz_parser + strlen( psz_name ) + 1,
272                          strlen( psz_parser + strlen( psz_name ) + 1 ) + 1 );
273             }
274             else
275             {
276                 *psz_parser = '\0';
277             }
278
279             /* Remove trailing : : */
280             if( strlen( psz_string ) > 0 &&
281                 *( psz_string + strlen( psz_string ) -1 ) == ':' )
282             {
283                 *( psz_string + strlen( psz_string ) -1 ) = '\0';
284             }
285         }
286         else
287         {
288             free( psz_string );
289             return;
290         }
291     }
292     /* Vout is not kept, so put that in the config */
293     config_PutPsz( p_intf, psz_filter_type, psz_string );
294     if( !strcmp( psz_filter_type, "video-filter" ) )
295         ui.videoFilterText->setText( psz_string );
296     else if( !strcmp( psz_filter_type, "vout-filter" ) )
297         ui.voutFilterText->setText( psz_string );
298     else if( !strcmp( psz_filter_type, "sub-filter" ) )
299         ui.subpictureFilterText->setText( psz_string );
300
301     /* Try to set on the fly */
302     p_vout = ( vout_thread_t * )vlc_object_find( p_intf, VLC_OBJECT_VOUT,
303                                               FIND_ANYWHERE );
304     if( p_vout )
305     {
306         if( !strcmp( psz_filter_type, "sub-filter" ) )
307             var_SetString( p_vout->p_spu, psz_filter_type, psz_string );
308         else
309             var_SetString( p_vout, psz_filter_type, psz_string );
310         vlc_object_release( p_vout );
311     }
312
313     free( psz_string );
314 }
315
316 void ExtVideo::updateFilters()
317 {
318     QString module = ModuleFromWidgetName( sender() );
319     //std::cout << "Module name: " << module.toStdString() << std::endl;
320
321     QCheckBox *checkbox = qobject_cast<QCheckBox*>( sender() );
322     QGroupBox *groupbox = qobject_cast<QGroupBox*>( sender() );
323
324     ChangeVFiltersString( qtu( module ),
325                           checkbox ? checkbox->isChecked()
326                                    : groupbox->isChecked() );
327 }
328
329 void ExtVideo::initComboBoxItems( QObject *widget )
330 {
331     QComboBox *combobox = qobject_cast<QComboBox*>( widget );
332     if( !combobox ) return;
333     QString option = OptionFromWidgetName( widget );
334     module_config_t *p_item = config_FindConfig( VLC_OBJECT( p_intf ),
335                                                  option.toStdString().c_str() );
336     if( p_item )
337     {
338         int i_type = p_item->i_type & CONFIG_ITEM;
339         for( int i_index = 0; i_index < p_item->i_list; i_index++ )
340         {
341             if( i_type == CONFIG_ITEM_INTEGER
342              || i_type == CONFIG_ITEM_BOOL )
343                 combobox->addItem( qfu( p_item->ppsz_list_text[i_index] ),
344                                    p_item->pi_list[i_index] );
345             else if( i_type == CONFIG_ITEM_STRING )
346                 combobox->addItem( qfu( p_item->ppsz_list_text[i_index] ),
347                                    p_item->ppsz_list[i_index] );
348         }
349     }
350     else
351     {
352         msg_Err( p_intf, "Couldn't find option \"%s\".",
353                  option.toStdString().c_str() );
354     }
355 }
356
357 void ExtVideo::setWidgetValue( QObject *widget )
358 {
359     QString module = ModuleFromWidgetName( widget->parent() );
360     //std::cout << "Module name: " << module.toStdString() << std::endl;
361     QString option = OptionFromWidgetName( widget );
362     //std::cout << "Option name: " << option.toStdString() << std::endl;
363
364     vlc_object_t *p_obj = ( vlc_object_t * )
365         vlc_object_find_name( p_intf->p_libvlc,
366                               module.toStdString().c_str(),
367                               FIND_CHILD );
368     int i_type;
369     vlc_value_t val;
370
371     if( !p_obj )
372     {
373 #if 0
374         msg_Dbg( p_intf,
375                  "Module instance %s not found, looking in config values.",
376                  module.toStdString().c_str() );
377 #endif
378         i_type = config_GetType( p_intf, option.toStdString().c_str() ) & 0xf0;
379         switch( i_type )
380         {
381             case VLC_VAR_INTEGER:
382             case VLC_VAR_BOOL:
383                 val.i_int = config_GetInt( p_intf, option.toStdString().c_str() );
384                 break;
385             case VLC_VAR_FLOAT:
386                 val.f_float = config_GetFloat( p_intf, option.toStdString().c_str() );
387                 break;
388             case VLC_VAR_STRING:
389                 val.psz_string = config_GetPsz( p_intf, option.toStdString().c_str() );
390                 break;
391         }
392     }
393     else
394     {
395         i_type = var_Type( p_obj, option.toStdString().c_str() ) & 0xf0;
396         var_Get( p_obj, option.toStdString().c_str(), &val );
397         vlc_object_release( p_obj );
398     }
399
400     /* Try to cast to all the widgets we're likely to encounter. Only
401      * one of the casts is expected to work. */
402     QSlider        *slider        = qobject_cast<QSlider*>       ( widget );
403     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( widget );
404     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( widget );
405     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( widget );
406     QDial          *dial          = qobject_cast<QDial*>         ( widget );
407     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( widget );
408     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( widget );
409
410     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
411     {
412         int i_int = 0;
413         if( slider )        slider->setValue( val.i_int );
414         else if( checkbox ) checkbox->setCheckState( val.i_int? Qt::Checked
415                                                               : Qt::Unchecked );
416         else if( spinbox )  spinbox->setValue( val.i_int );
417         else if( dial )     dial->setValue( ( 540-val.i_int )%360 );
418         else if( lineedit )
419         {
420             char str[30];
421             sprintf( str, "%06X", val.i_int );
422             lineedit->setText( str );
423         }
424         else if( combobox ) combobox->setCurrentIndex(
425                             combobox->findData( val.i_int ) );
426         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
427     }
428     else if( i_type == VLC_VAR_FLOAT )
429     {
430         double f_float = 0;
431         if( slider ) slider->setValue( ( int )( val.f_float*( double )slider->tickInterval() ) ); /* hack alert! */
432         else if( doublespinbox ) doublespinbox->setValue( val.f_float );
433         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
434     }
435     else if( i_type == VLC_VAR_STRING )
436     {
437         if( lineedit ) lineedit->setText( qfu( val.psz_string ) );
438         else if( combobox ) combobox->setCurrentIndex(
439                             combobox->findData( qfu( val.psz_string ) ) );
440         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
441         free( val.psz_string );
442     }
443     else
444         msg_Err( p_intf,
445                  "Module %s's %s variable is of an unsupported type ( %d )",
446                  module.toStdString().c_str(),
447                  option.toStdString().c_str(),
448                  i_type );
449 }
450
451 void ExtVideo::updateFilterOptions()
452 {
453     QString module = ModuleFromWidgetName( sender()->parent() );
454     //std::cout << "Module name: " << module.toStdString() << std::endl;
455     QString option = OptionFromWidgetName( sender() );
456     //std::cout << "Option name: " << option.toStdString() << std::endl;
457
458     vlc_object_t *p_obj = ( vlc_object_t * )
459         vlc_object_find_name( p_intf->p_libvlc,
460                               module.toStdString().c_str(),
461                               FIND_CHILD );
462     if( !p_obj )
463     {
464         msg_Err( p_intf, "Module %s not found.", module.toStdString().c_str() );
465         return;
466     }
467
468     int i_type = var_Type( p_obj, option.toStdString().c_str() );
469     bool b_is_command = ( i_type & VLC_VAR_ISCOMMAND );
470     if( !b_is_command )
471     {
472         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.",
473                  module.toStdString().c_str(),
474                  option.toStdString().c_str() );
475         /* FIXME: restart automatically somewhere near the end of this function */
476     }
477
478     /* Try to cast to all the widgets we're likely to encounter. Only
479      * one of the casts is expected to work. */
480     QSlider        *slider        = qobject_cast<QSlider*>       ( sender() );
481     QCheckBox      *checkbox      = qobject_cast<QCheckBox*>     ( sender() );
482     QSpinBox       *spinbox       = qobject_cast<QSpinBox*>      ( sender() );
483     QDoubleSpinBox *doublespinbox = qobject_cast<QDoubleSpinBox*>( sender() );
484     QDial          *dial          = qobject_cast<QDial*>         ( sender() );
485     QLineEdit      *lineedit      = qobject_cast<QLineEdit*>     ( sender() );
486     QComboBox      *combobox      = qobject_cast<QComboBox*>     ( sender() );
487
488     i_type &= 0xf0;
489     if( i_type == VLC_VAR_INTEGER || i_type == VLC_VAR_BOOL )
490     {
491         int i_int = 0;
492         if( slider )        i_int = slider->value();
493         else if( checkbox ) i_int = checkbox->checkState() == Qt::Checked;
494         else if( spinbox )  i_int = spinbox->value();
495         else if( dial )     i_int = ( 540-dial->value() )%360;
496         else if( lineedit ) i_int = lineedit->text().toInt( NULL,16 );
497         else if( combobox ) i_int = combobox->itemData( combobox->currentIndex() ).toInt();
498         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
499         config_PutInt( p_intf, option.toStdString().c_str(), i_int );
500         if( b_is_command )
501         {
502             if( i_type == VLC_VAR_INTEGER )
503                 var_SetInteger( p_obj, option.toStdString().c_str(), i_int );
504             else
505                 var_SetBool( p_obj, option.toStdString().c_str(), i_int );
506         }
507     }
508     else if( i_type == VLC_VAR_FLOAT )
509     {
510         double f_float = 0;
511         if( slider )             f_float = ( double )slider->value()
512                                          / ( double )slider->tickInterval(); /* hack alert! */
513         else if( doublespinbox ) f_float = doublespinbox->value();
514         else if( lineedit ) f_float = lineedit->text().toDouble();
515         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
516         config_PutFloat( p_intf, option.toStdString().c_str(), f_float );
517         if( b_is_command )
518             var_SetFloat( p_obj, option.toStdString().c_str(), f_float );
519     }
520     else if( i_type == VLC_VAR_STRING )
521     {
522         char *psz_string = NULL;
523         if( lineedit ) psz_string = strdup( qtu( lineedit->text() ) );
524         else if( combobox ) psz_string = strdup( qtu( combobox->itemData(
525                                          combobox->currentIndex() ).toString() ) );
526         else msg_Warn( p_intf, "Oops %s %s %d", __FILE__, __func__, __LINE__ );
527         config_PutPsz( p_intf, option.toStdString().c_str(), psz_string );
528         if( b_is_command )
529             var_SetString( p_obj, option.toStdString().c_str(), psz_string );
530         free( psz_string );
531     }
532     else
533         msg_Err( p_intf,
534                  "Module %s's %s variable is of an unsupported type ( %d )",
535                  module.toStdString().c_str(),
536                  option.toStdString().c_str(),
537                  i_type );
538
539     vlc_object_release( p_obj );
540 }
541
542 #if 0
543 void ExtVideo::gotoConf( QObject* src )
544 {
545 #define SHOWCONF( module ) \
546     if( src->objectName().contains( module ) ) \
547     { \
548         PrefsDialog::getInstance( p_intf )->showModulePrefs( module ); \
549         return; \
550     }
551     SHOWCONF( "clone" );
552     SHOWCONF( "magnify" );
553     SHOWCONF( "wave" );
554     SHOWCONF( "ripple" );
555     SHOWCONF( "invert" );
556     SHOWCONF( "puzzle" );
557     SHOWCONF( "wall" );
558     SHOWCONF( "gradient" );
559     SHOWCONF( "colorthres" )
560 }
561 #endif
562
563 /**********************************************************************
564  * v4l2 controls
565  **********************************************************************/
566
567 ExtV4l2::ExtV4l2( intf_thread_t *_p_intf, QWidget *_parent )
568     : QWidget( _parent ), p_intf( _p_intf )
569 {
570     ui.setupUi( this );
571
572     BUTTONACT( ui.refresh, Refresh() );
573
574     box = NULL;
575 }
576
577 ExtV4l2::~ExtV4l2()
578 {
579     if( box )
580         delete box;
581 }
582
583 void ExtV4l2::showEvent( QShowEvent *event )
584 {
585     QWidget::showEvent( event );
586     Refresh();
587 }
588
589 void ExtV4l2::Refresh( void )
590 {
591     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( p_intf, "v4l2", FIND_ANYWHERE );
592     ui.help->hide();
593     if( box )
594     {
595         ui.vboxLayout->removeWidget( box );
596         delete box;
597         box = NULL;
598     }
599     if( p_obj )
600     {
601         msg_Dbg( p_intf, "Found v4l2 instance" );
602         vlc_value_t val, text, name;
603         int i_ret = var_Change( p_obj, "controls", VLC_VAR_GETCHOICES,
604                                 &val, &text );
605         if( i_ret < 0 )
606         {
607             msg_Err( p_intf, "Oops, v4l2 object doesn't have a 'controls' variable." );
608             ui.help->show();
609             vlc_object_release( p_obj );
610             return;
611         }
612
613         box = new QGroupBox( this );
614         ui.vboxLayout->addWidget( box );
615         QVBoxLayout *layout = new QVBoxLayout( box );
616         box->setLayout( layout );
617
618         for( int i = 0; i < val.p_list->i_count; i++ )
619         {
620             const char *psz_var = text.p_list->p_values[i].psz_string;
621             var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
622             const char *psz_label = name.psz_string;
623             msg_Dbg( p_intf, "v4l2 control \"%x\": %s (%s)",
624                      val.p_list->p_values[i].i_int, psz_var, name.psz_string );
625
626             int i_type = var_Type( p_obj, psz_var );
627             switch( i_type & VLC_VAR_TYPE )
628             {
629                 case VLC_VAR_INTEGER:
630                 {
631                     QLabel *label = new QLabel( psz_label, box );
632                     QHBoxLayout *hlayout = new QHBoxLayout();
633                     hlayout->addWidget( label );
634                     int i_val = var_GetInteger( p_obj, psz_var );
635                     if( i_type & VLC_VAR_HASCHOICE )
636                     {
637                         QComboBox *combobox = new QComboBox( box );
638                         combobox->setObjectName( psz_var );
639
640                         vlc_value_t val2, text2;
641                         var_Change( p_obj, psz_var, VLC_VAR_GETCHOICES,
642                                     &val2, &text2 );
643                         for( int j = 0; j < val2.p_list->i_count; j++ )
644                         {
645                             combobox->addItem(
646                                        text2.p_list->p_values[j].psz_string,
647                                        val2.p_list->p_values[j].i_int );
648                             if( i_val == val2.p_list->p_values[j].i_int )
649                                 combobox->setCurrentIndex( j );
650                         }
651                         var_Change( p_obj, psz_var, VLC_VAR_FREELIST,
652                                     &val2, &text2 );
653
654                         CONNECT( combobox, currentIndexChanged( int ), this,
655                                  ValueChange( int ) );
656                         hlayout->addWidget( combobox );
657                     }
658                     else
659                     {
660                         QSlider *slider = new QSlider( box );
661                         slider->setObjectName( psz_var );
662                         slider->setOrientation( Qt::Horizontal );
663                         vlc_value_t val2;
664                         var_Change( p_obj, psz_var, VLC_VAR_GETMIN,
665                                     &val2, NULL );
666                         slider->setMinimum( val2.i_int );
667                         var_Change( p_obj, psz_var, VLC_VAR_GETMAX,
668                                     &val2, NULL );
669                         slider->setMaximum( val2.i_int );
670                         var_Change( p_obj, psz_var, VLC_VAR_GETSTEP,
671                                     &val2, NULL );
672                         slider->setSingleStep( val2.i_int );
673                         slider->setValue( i_val );
674
675                         CONNECT( slider, valueChanged( int ), this,
676                                  ValueChange( int ) );
677                         hlayout->addWidget( slider );
678                     }
679                     layout->addLayout( hlayout );
680                     break;
681                 }
682                 case VLC_VAR_BOOL:
683                 {
684                     QCheckBox *button = new QCheckBox( psz_label, box );
685                     button->setObjectName( psz_var );
686                     button->setChecked( var_GetBool( p_obj, psz_var ) );
687
688                     CONNECT( button, clicked( bool ), this,
689                              ValueChange( bool ) );
690                     layout->addWidget( button );
691                     break;
692                 }
693                 case VLC_VAR_VOID:
694                 {
695                     if( i_type & VLC_VAR_ISCOMMAND )
696                     {
697                         QPushButton *button = new QPushButton( psz_label, box );
698                         button->setObjectName( psz_var );
699
700                         CONNECT( button, clicked( bool ), this,
701                                  ValueChange( bool ) );
702                         layout->addWidget( button );
703                     }
704                     else
705                     {
706                         QLabel *label = new QLabel( psz_label, box );
707                         layout->addWidget( label );
708                     }
709                     break;
710                 }
711                 default:
712                     msg_Warn( p_intf, "Unhandled var type for %s", psz_var );
713                     break;
714             }
715             free( name.psz_string );
716         }
717         var_Change( p_obj, "controls", VLC_VAR_FREELIST, &val, &text );
718         vlc_object_release( p_obj );
719     }
720     else
721     {
722         msg_Dbg( p_intf, "Couldn't find v4l2 instance" );
723         ui.help->show();
724     }
725
726 }
727
728 void ExtV4l2::ValueChange( bool value )
729 {
730     ValueChange( (int)value );
731 }
732
733 void ExtV4l2::ValueChange( int value )
734 {
735     QObject *s = sender();
736     vlc_object_t *p_obj = (vlc_object_t*)vlc_object_find_name( p_intf, "v4l2", FIND_ANYWHERE );
737     if( p_obj )
738     {
739         char *psz_var = strdup( s->objectName().toStdString().c_str() );
740         int i_type = var_Type( p_obj, psz_var );
741         switch( i_type & VLC_VAR_TYPE )
742         {
743             case VLC_VAR_INTEGER:
744                 if( i_type & VLC_VAR_HASCHOICE )
745                 {
746                     QComboBox *combobox = qobject_cast<QComboBox*>( s );
747                     value = combobox->itemData( value ).toInt();
748                 }
749                 var_SetInteger( p_obj, psz_var, value );
750                 break;
751             case VLC_VAR_BOOL:
752                 var_SetBool( p_obj, psz_var, value );
753                 break;
754             case VLC_VAR_VOID:
755                 var_SetVoid( p_obj, psz_var );
756                 break;
757         }
758         free( psz_var );
759         vlc_object_release( p_obj );
760     }
761     else
762     {
763         msg_Warn( p_intf, "Oops, v4l2 object isn't available anymore" );
764         Refresh();
765     }
766 }
767
768 /**********************************************************************
769  * Equalizer
770  **********************************************************************/
771
772 static const QString band_frequencies[] =
773 {
774     "  60 Hz  ", " 170 Hz ", " 310 Hz ", " 600 Hz ", "  1 kHz ",
775     "  3 kHz  ", "  6 kHz ", " 12 kHz ", " 14 kHz ", " 16 kHz "
776 };
777
778 Equalizer::Equalizer( intf_thread_t *_p_intf, QWidget *_parent ) :
779                             QWidget( _parent ) , p_intf( _p_intf )
780 {
781     QFont smallFont = QApplication::font( static_cast<QWidget*>( 0 ) );
782     smallFont.setPointSize( smallFont.pointSize() - 3 );
783
784     ui.setupUi( this );
785     presetsComboBox = ui.presetsCombo;
786
787     ui.preampLabel->setFont( smallFont );
788     ui.preampSlider->setMaximum( 400 );
789     for( int i = 0 ; i < NB_PRESETS ; i ++ )
790     {
791         ui.presetsCombo->addItem( qtr( preset_list_text[i] ),
792                                   QVariant( i ) );
793     }
794     CONNECT( ui.presetsCombo, activated( int ), this, setPreset( int ) );
795
796     BUTTONACT( ui.enableCheck, enable() );
797     BUTTONACT( ui.eq2PassCheck, set2Pass() );
798
799     CONNECT( ui.preampSlider, valueChanged( int ), this, setPreamp() );
800
801     QGridLayout *grid = new QGridLayout( ui.frame );
802     grid->setMargin( 0 );
803     for( int i = 0 ; i < BANDS ; i++ )
804     {
805         bands[i] = new QSlider( Qt::Vertical );
806         bands[i]->setMaximum( 400 );
807         bands[i]->setValue( 200 );
808         CONNECT( bands[i], valueChanged( int ), this, setBand() );
809         band_texts[i] = new QLabel( band_frequencies[i] + "\n0.0dB" );
810         band_texts[i]->setFont( smallFont );
811         grid->addWidget( bands[i], 0, i );
812         grid->addWidget( band_texts[i], 1, i );
813     }
814
815     /* Write down initial values */
816     aout_instance_t *p_aout = ( aout_instance_t * )vlc_object_find( p_intf,
817                                     VLC_OBJECT_AOUT, FIND_ANYWHERE );
818     char *psz_af;
819     char *psz_bands;
820     float f_preamp;
821     if( p_aout )
822     {
823         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
824         if( var_GetBool( p_aout, "equalizer-2pass" ) )
825             ui.eq2PassCheck->setChecked( true );
826         psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
827         f_preamp = var_GetFloat( p_aout, "equalizer-preamp" );
828         vlc_object_release( p_aout );
829     }
830     else
831     {
832         psz_af = config_GetPsz( p_intf, "audio-filter" );
833         if( config_GetInt( p_intf, "equalizer-2pass" ) )
834             ui.eq2PassCheck->setChecked( true );
835         psz_bands = config_GetPsz( p_intf, "equalizer-bands" );
836         f_preamp = config_GetFloat( p_intf, "equalizer-preamp" );
837     }
838     if( psz_af && strstr( psz_af, "equalizer" ) != NULL )
839         ui.enableCheck->setChecked( true );
840     free( psz_af );
841     enable( ui.enableCheck->isChecked() );
842
843     setValues( psz_bands, f_preamp );
844 }
845
846 Equalizer::~Equalizer()
847 {
848 }
849
850 void Equalizer::enable()
851 {
852     bool en = ui.enableCheck->isChecked();
853     aout_EnableFilter( VLC_OBJECT( p_intf ), "equalizer",
854                        en ? VLC_TRUE : VLC_FALSE );
855 //    aout_EnableFilter( VLC_OBJECT( p_intf ), "upmixer",
856 //                       en ? VLC_TRUE : VLC_FALSE );
857 //     aout_EnableFilter( VLC_OBJECT( p_intf ), "vsurround",
858 //                       en ? VLC_TRUE : VLC_FALSE );
859      enable( en );
860 }
861
862 void Equalizer::enable( bool en )
863 {
864     ui.eq2PassCheck->setEnabled( en );
865     ui.preampLabel->setEnabled( en );
866     ui.preampSlider->setEnabled( en  );
867     for( int i = 0 ; i< BANDS; i++ )
868     {
869         bands[i]->setEnabled( en ); band_texts[i]->setEnabled( en );
870     }
871 }
872
873 void Equalizer::set2Pass()
874 {
875     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
876                                  VLC_OBJECT_AOUT, FIND_ANYWHERE );
877     vlc_bool_t b_2p = ui.eq2PassCheck->isChecked();
878
879     if( p_aout == NULL )
880         config_PutInt( p_intf, "equalizer-2pass", b_2p );
881     else
882     {
883         var_SetBool( p_aout, "equalizer-2pass", b_2p );
884         config_PutInt( p_intf, "equalizer-2pass", b_2p );
885         for( int i = 0; i < p_aout->i_nb_inputs; i++ )
886         {
887             p_aout->pp_inputs[i]->b_restart = VLC_TRUE;
888         }
889         vlc_object_release( p_aout );
890     }
891 }
892
893 void Equalizer::setPreamp()
894 {
895     float f= ( float )(  ui.preampSlider->value() ) /10 - 20;
896     char psz_val[5];
897     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
898                                        VLC_OBJECT_AOUT, FIND_ANYWHERE );
899
900     sprintf( psz_val, "%.1f", f );
901     ui.preampLabel->setText( qtr( "Preamp\n" ) + psz_val + qtr( "dB" ) );
902     if( p_aout )
903     {
904         delCallbacks( p_aout );
905         var_SetFloat( p_aout, "equalizer-preamp", f );
906         addCallbacks( p_aout );
907         vlc_object_release( p_aout );
908     }
909     config_PutFloat( p_intf, "equalizer-preamp", f );
910 }
911
912 void Equalizer::setBand()
913 {
914     char psz_values[102]; memset( psz_values, 0, 102 );
915
916     /**\todo smoothing */
917
918     for( int i = 0 ; i< BANDS ; i++ )
919     {
920         char psz_val[5];
921         float f_val = ( float )(  bands[i]->value() ) / 10 - 20 ;
922         sprintf( psz_values, "%s %f", psz_values, f_val );
923         sprintf( psz_val, "% 5.1f", f_val );
924         band_texts[i]->setText( band_frequencies[i] + "\n" + psz_val + "dB" );
925     }
926     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
927                                           VLC_OBJECT_AOUT, FIND_ANYWHERE );
928     if( p_aout )
929     {
930         delCallbacks( p_aout );
931         var_SetString( p_aout, "equalizer-bands", psz_values );
932         addCallbacks( p_aout );
933         vlc_object_release( p_aout );
934     }
935 }
936 void Equalizer::setValues( char *psz_bands, float f_preamp )
937 {
938     char *p = psz_bands;
939     if ( p )
940     {
941         for( int i = 0; i < 10; i++ )
942         {
943             char psz_val[5];
944             float f = strtof( p, &p );
945             int  i_val= ( int )( ( f + 20 ) * 10 );
946             bands[i]->setValue(  i_val );
947             sprintf( psz_val, "% 5.1f", f );
948             band_texts[i]->setText( band_frequencies[i] + "\n" + psz_val +
949                                     "dB" );
950             if( p == NULL || *p == '\0' ) break;
951             p++;
952             if( *p == '\0' )  break;
953         }
954     }
955     char psz_val[5];
956     int i_val = ( int )( ( f_preamp + 20 ) * 10 );
957     sprintf( psz_val, "%.1f", f_preamp );
958     ui.preampSlider->setValue( i_val );
959     ui.preampLabel->setText( qtr( "Preamp\n" ) + psz_val + qtr( "dB" ) );
960 }
961
962 void Equalizer::setPreset( int preset )
963 {
964     aout_instance_t *p_aout= ( aout_instance_t * )vlc_object_find( p_intf,
965                                                 VLC_OBJECT_AOUT, FIND_ANYWHERE );
966
967     char psz_values[102]; memset( psz_values, 0, 102 );
968     for( int i = 0 ; i< 10 ;i++ )
969         sprintf( psz_values, "%s %.1f", psz_values,
970                                         eqz_preset_10b[preset]->f_amp[i] );
971
972     if( p_aout )
973     {
974         delCallbacks( p_aout );
975         var_SetString( p_aout, "equalizer-bands", psz_values );
976         var_SetFloat( p_aout, "equalizer-preamp",
977                       eqz_preset_10b[preset]->f_preamp );
978         addCallbacks( p_aout );
979         vlc_object_release( p_aout );
980     }
981     config_PutPsz( p_intf, "equalizer-bands", psz_values );
982     config_PutFloat( p_intf, "equalizer-preamp",
983                     eqz_preset_10b[preset]->f_preamp );
984
985     setValues( psz_values, eqz_preset_10b[preset]->f_preamp );
986 }
987
988 static int PresetCallback( vlc_object_t *p_this, char const *psz_cmd,
989                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
990 {
991     char *psz_preset = newval.psz_string;
992     Equalizer *eq = ( Equalizer * )p_data;
993     eq->presetsComboBox->setCurrentIndex( eq->presetsComboBox->findText( qfu( psz_preset ) ) );
994     return VLC_SUCCESS;
995 }
996
997 void Equalizer::delCallbacks( aout_instance_t *p_aout )
998 {
999     //var_DelCallback( p_aout, "equalizer-bands", EqzCallback, this );
1000     //var_DelCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1001     var_DelCallback( p_aout, "equalizer-preset", PresetCallback, this );
1002 }
1003
1004 void Equalizer::addCallbacks( aout_instance_t *p_aout )
1005 {
1006     //var_AddCallback( p_aout, "equalizer-bands", EqzCallback, this );
1007     //var_AddCallback( p_aout, "equalizer-preamp", EqzCallback, this );
1008     var_AddCallback( p_aout, "equalizer-preset", PresetCallback, this );
1009 }
1010
1011 /**********************************************************************
1012  * Audio filters
1013  **********************************************************************/
1014
1015 /**********************************************************************
1016  * Spatializer
1017  **********************************************************************/
1018 static const char *psz_control_names[] =
1019 {
1020     "Roomsize", "Width" , "Wet", "Dry", "Damp"
1021 };
1022
1023 Spatializer::Spatializer( intf_thread_t *_p_intf, QWidget *_parent ) :
1024     QWidget( _parent ) , p_intf( _p_intf )
1025 {
1026     QFont smallFont = QApplication::font( static_cast<QWidget*>( 0 ) );
1027     smallFont.setPointSize( smallFont.pointSize() - 3 );
1028
1029     QGridLayout *layout = new QGridLayout( this );
1030     layout->setMargin( 0 );
1031
1032     enableCheck = new QCheckBox( qfu( "Enable spatializer" ) );
1033     layout->addWidget( enableCheck, 0, 0, 1, NUM_SP_CTRL );
1034
1035     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1036     {
1037         spatCtrl[i] = new QSlider( Qt::Vertical );
1038         if( i < 2 )
1039         {
1040             spatCtrl[i]->setMaximum( 10 );
1041             spatCtrl[i]->setValue( 2 );
1042         }
1043         else
1044         {
1045             spatCtrl[i]->setMaximum( 10 );
1046             spatCtrl[i]->setValue( 0 );
1047             spatCtrl[i]->setMinimum( -10 );
1048         }
1049         oldControlVars[i] = spatCtrl[i]->value();
1050         CONNECT( spatCtrl[i], valueChanged( int ), this, setInitValues() );
1051         ctrl_texts[i] = new QLabel( qfu( psz_control_names[i] ) + "\n" );
1052         ctrl_texts[i]->setFont( smallFont );
1053         ctrl_readout[i] = new QLabel( "" );
1054         ctrl_readout[i]->setFont( smallFont );
1055         layout->addWidget( spatCtrl[i], 1, i );
1056         layout->addWidget( ctrl_readout[i], 2, i );
1057         layout->addWidget( ctrl_texts[i], 3, i );
1058     }
1059
1060     BUTTONACT( enableCheck, enable() );
1061
1062     /* Write down initial values */
1063     aout_instance_t *p_aout = ( aout_instance_t * )
1064         vlc_object_find( p_intf, VLC_OBJECT_AOUT, FIND_ANYWHERE );
1065     char *psz_af;
1066
1067     if( p_aout )
1068     {
1069         psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
1070         for( int i = 0; i < NUM_SP_CTRL ; i++ )
1071         {
1072             controlVars[i] = var_GetFloat( p_aout, psz_control_names[i] );
1073         }
1074         vlc_object_release( p_aout );
1075     }
1076     else
1077     {
1078         psz_af = config_GetPsz( p_aout, "audio-filter" );
1079         for( int i = 0; i < NUM_SP_CTRL ; i++ )
1080         {
1081             controlVars[i] = config_GetFloat( p_intf, psz_control_names[i] );
1082         }
1083     }
1084     if( psz_af && strstr( psz_af, "spatializer" ) != NULL )
1085         enableCheck->setChecked( true );
1086     free( psz_af );
1087     enable( enableCheck->isChecked() );
1088     setValues( controlVars );
1089 }
1090
1091 Spatializer::~Spatializer()
1092 {
1093 }
1094
1095 void Spatializer::enable()
1096 {
1097     bool en = enableCheck->isChecked();
1098     aout_EnableFilter( VLC_OBJECT( p_intf ), "spatializer",
1099             en ? VLC_TRUE : VLC_FALSE );
1100     enable( en );
1101 }
1102
1103 void Spatializer::enable( bool en )
1104 {
1105     for( int i = 0 ; i< NUM_SP_CTRL; i++ )
1106     {
1107         spatCtrl[i]->setEnabled( en );
1108         ctrl_texts[i]->setEnabled( en );
1109         ctrl_readout[i]->setEnabled( en );
1110     }
1111 }
1112 void Spatializer::setInitValues()
1113 {
1114     setValues( controlVars );
1115 }
1116
1117 void Spatializer::setValues( float *controlVars )
1118 {
1119     char psz_val[5];
1120     char var_name[5];
1121     aout_instance_t *p_aout= ( aout_instance_t * )
1122         vlc_object_find( p_intf, VLC_OBJECT_AOUT, FIND_ANYWHERE );
1123
1124     for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1125     {
1126         float f= ( float )(  spatCtrl[i]->value() );
1127         sprintf( psz_val, "%.1f", f );
1128         ctrl_readout[i]->setText( psz_val );
1129     }
1130     if( p_aout )
1131     {
1132         for( int i = 0 ; i < NUM_SP_CTRL ; i++ )
1133         {
1134             if( oldControlVars[i] != spatCtrl[i]->value() )
1135             {
1136                 var_SetFloat( p_aout, psz_control_names[i],
1137                         ( float )spatCtrl[i]->value() );
1138                 config_PutFloat( p_intf, psz_control_names[i],
1139                         ( float ) spatCtrl[i]->value() );
1140                 oldControlVars[i] = ( float ) spatCtrl[i]->value();
1141             }
1142         }
1143         vlc_object_release( p_aout );
1144     }
1145
1146 }
1147 void Spatializer::delCallbacks( aout_instance_t *p_aout )
1148 {
1149     //    var_DelCallback( p_aout, "Spatializer-bands", EqzCallback, this );
1150     //    var_DelCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
1151 }
1152
1153 void Spatializer::addCallbacks( aout_instance_t *p_aout )
1154 {
1155     //    var_AddCallback( p_aout, "Spatializer-bands", EqzCallback, this );
1156     //    var_AddCallback( p_aout, "Spatializer-preamp", EqzCallback, this );
1157 }
1158
1159 /**********************************************************************
1160  * Video filters / Adjust
1161  **********************************************************************/
1162
1163 /**********************************************************************
1164  * Extended playbak controls
1165  **********************************************************************/