]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/controller.cpp
Qt: integrate the new volume to the customize dialog.
[vlc] / modules / gui / qt4 / components / controller.cpp
1 /*****************************************************************************
2  * Controller.cpp : Controller for the main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb@videolan.org>
8  *          Ilkka Ollakka <ileoo@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * ( at your option ) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_vout.h>
30 #include <vlc_keys.h>
31
32 #include "components/controller.hpp"
33 #include "components/controller_widget.hpp"
34 #include "components/interface_widgets.hpp"
35
36 #include "dialogs_provider.hpp" /* Opening Dialogs */
37 #include "input_manager.hpp"
38 #include "actions_manager.hpp"
39
40 #include "util/input_slider.hpp" /* InputSlider */
41 #include "util/customwidgets.hpp" /* qEventToKey */
42
43 #include <QSpacerItem>
44 #include <QToolButton>
45 #include <QHBoxLayout>
46 #include <QSignalMapper>
47 #include <QTimer>
48
49 /**********************************************************************
50  * TEH controls
51  **********************************************************************/
52
53 /******
54  * This is an abstract Toolbar/Controller
55  * This has helper to create any toolbar, any buttons and to manage the actions
56  *
57  *****/
58 AbstractController::AbstractController( intf_thread_t * _p_i, QWidget *_parent )
59                    : QFrame( _parent )
60 {
61     p_intf = _p_i;
62     advControls = NULL;
63
64     /* Main action provider */
65     toolbarActionsMapper = new QSignalMapper( this );
66     CONNECT( toolbarActionsMapper, mapped( int ),
67              ActionsManager::getInstance( p_intf  ), doAction( int ) );
68     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
69 }
70
71 /* Reemit some signals on status Change to activate some buttons */
72 void AbstractController::setStatus( int status )
73 {
74     bool b_hasInput = THEMIM->getIM()->hasInput();
75     /* Activate the interface buttons according to the presence of the input */
76     emit inputExists( b_hasInput );
77
78     emit inputPlaying( status == PLAYING_S );
79
80     emit inputIsRecordable( b_hasInput &&
81                             var_GetBool( THEMIM->getInput(), "can-record" ) );
82 }
83
84 /* Generic button setup */
85 void AbstractController::setupButton( QAbstractButton *aButton )
86 {
87     static QSizePolicy sizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
88     sizePolicy.setHorizontalStretch( 0 );
89     sizePolicy.setVerticalStretch( 0 );
90
91     aButton->setSizePolicy( sizePolicy );
92     aButton->setFixedSize( QSize( 26, 26 ) );
93     aButton->setIconSize( QSize( 20, 20 ) );
94     aButton->setFocusPolicy( Qt::NoFocus );
95 }
96
97 /* Open the generic config line for the toolbar, parse it
98  * and create the widgets accordingly */
99 void AbstractController::parseAndCreate( QString config,
100                                          QBoxLayout *controlLayout )
101 {
102     QStringList list = config.split( ";", QString::SkipEmptyParts ) ;
103     for( int i = 0; i < list.size(); i++ )
104     {
105         QStringList list2 = list.at( i ).split( "-" );
106         if( list2.size() < 1 )
107         {
108             msg_Warn( p_intf, "Parsing error. Report this" );
109             continue;
110         }
111
112         bool ok;
113         int i_option = WIDGET_NORMAL;
114         buttonType_e i_type = (buttonType_e)list2.at( 0 ).toInt( &ok );
115         if( !ok )
116         {
117             msg_Warn( p_intf, "Parsing error 0. Please report this" );
118             continue;
119         }
120
121         if( list2.size() > 1 )
122         {
123             i_option = list2.at( 1 ).toInt( &ok );
124             if( !ok )
125             {
126                 msg_Warn( p_intf, "Parsing error 1. Please report this" );
127                 continue;
128             }
129         }
130
131         createAndAddWidget( controlLayout, -1, i_type, i_option );
132     }
133 }
134
135 void AbstractController::createAndAddWidget( QBoxLayout *controlLayout,
136                                              int i_index,
137                                              buttonType_e i_type,
138                                              int i_option )
139 {
140     /* Special case for SPACERS, who aren't QWidgets */
141     if( i_type == WIDGET_SPACER )
142     {
143         controlLayout->insertSpacing( i_index, 16 );
144         return;
145     }
146
147     if(  i_type == WIDGET_SPACER_EXTEND )
148     {
149         controlLayout->insertStretch( i_index, 16 );
150         return;
151     }
152
153     QWidget *widg = createWidget( i_type, i_option );
154     if( !widg ) return;
155
156     controlLayout->insertWidget( i_index, widg );
157 }
158
159
160 #define CONNECT_MAP( a ) CONNECT( a, clicked(),  toolbarActionsMapper, map() )
161 #define SET_MAPPING( a, b ) toolbarActionsMapper->setMapping( a , b )
162 #define CONNECT_MAP_SET( a, b ) \
163     CONNECT_MAP( a ); \
164     SET_MAPPING( a, b );
165 #define BUTTON_SET_BAR( a_button ) \
166     a_button->setToolTip( tooltipL[button] );          \
167     a_button->setIcon( QIcon( iconL[button] ) );
168 #define BUTTON_SET_BAR2( button, image, tooltip ) \
169     button->setToolTip( tooltip );          \
170     button->setIcon( QIcon( ":/"#image ) );
171
172
173 #define ENABLE_ON_VIDEO( a ) \
174     CONNECT( THEMIM->getIM(), voutChanged( bool ), a, setEnabled( bool ) ); \
175     a->setEnabled( THEMIM->getIM()->hasVideo() ); /* TODO: is this necessary? when input is started before the interface? */
176
177 #define ENABLE_ON_INPUT( a ) \
178     CONNECT( this, inputExists( bool ), a, setEnabled( bool ) ); \
179     a->setEnabled( THEMIM->getIM()->hasInput() ); /* TODO: is this necessary? when input is started before the interface? */
180
181 QWidget *AbstractController::createWidget( buttonType_e button, int options )
182 {
183
184     bool b_flat = options & WIDGET_FLAT;
185     bool b_big = options & WIDGET_BIG;
186     bool b_shiny = options & WIDGET_SHINY;
187     bool b_special = false;
188
189     QWidget *widget = NULL;
190     switch( button )
191     {
192     case PLAY_BUTTON: {
193         PlayButton *playButton = new PlayButton;
194         setupButton( playButton );
195         BUTTON_SET_BAR(  playButton );
196         CONNECT_MAP_SET( playButton, PLAY_ACTION );
197         CONNECT( this, inputPlaying( bool ),
198                  playButton, updateButton( bool ));
199         widget = playButton;
200         }
201         break;
202     case STOP_BUTTON:{
203         QToolButton *stopButton = new QToolButton;
204         setupButton( stopButton );
205         CONNECT_MAP_SET( stopButton, STOP_ACTION );
206         BUTTON_SET_BAR(  stopButton );
207         widget = stopButton;
208         }
209         break;
210     case OPEN_BUTTON:{
211         QToolButton *openButton = new QToolButton;
212         setupButton( openButton );
213         CONNECT_MAP_SET( openButton, OPEN_ACTION );
214         BUTTON_SET_BAR( openButton );
215         widget = openButton;
216         }
217         break;
218     case PREVIOUS_BUTTON:{
219         QToolButton *prevButton = new QToolButton;
220         setupButton( prevButton );
221         CONNECT_MAP_SET( prevButton, PREVIOUS_ACTION );
222         BUTTON_SET_BAR( prevButton );
223         widget = prevButton;
224         }
225         break;
226     case NEXT_BUTTON:
227         {
228         QToolButton *nextButton = new QToolButton;
229         setupButton( nextButton );
230         CONNECT_MAP_SET( nextButton, NEXT_ACTION );
231         BUTTON_SET_BAR( nextButton );
232         widget = nextButton;
233         }
234         break;
235     case SLOWER_BUTTON:{
236         QToolButton *slowerButton = new QToolButton;
237         setupButton( slowerButton );
238         CONNECT_MAP_SET( slowerButton, SLOWER_ACTION );
239         BUTTON_SET_BAR(  slowerButton );
240         ENABLE_ON_INPUT( slowerButton );
241         widget = slowerButton;
242         }
243         break;
244     case FASTER_BUTTON:{
245         QToolButton *fasterButton = new QToolButton;
246         setupButton( fasterButton );
247         CONNECT_MAP_SET( fasterButton, FASTER_ACTION );
248         BUTTON_SET_BAR(  fasterButton );
249         ENABLE_ON_INPUT( fasterButton );
250         widget = fasterButton;
251         }
252         break;
253     case FRAME_BUTTON: {
254         QToolButton *frameButton = new QToolButton;
255         setupButton( frameButton );
256         CONNECT_MAP_SET( frameButton, FRAME_ACTION );
257         BUTTON_SET_BAR(  frameButton );
258         ENABLE_ON_VIDEO( frameButton );
259         widget = frameButton;
260         }
261         break;
262     case FULLSCREEN_BUTTON:{
263         QToolButton *fullscreenButton = new QToolButton;
264         setupButton( fullscreenButton );
265         CONNECT_MAP_SET( fullscreenButton, FULLSCREEN_ACTION );
266         BUTTON_SET_BAR( fullscreenButton );
267         ENABLE_ON_VIDEO( fullscreenButton );
268         widget = fullscreenButton;
269         }
270         break;
271     case DEFULLSCREEN_BUTTON:{
272         QToolButton *fullscreenButton = new QToolButton;
273         setupButton( fullscreenButton );
274         CONNECT_MAP_SET( fullscreenButton, FULLSCREEN_ACTION );
275         BUTTON_SET_BAR( fullscreenButton )
276         ENABLE_ON_VIDEO( fullscreenButton );
277         widget = fullscreenButton;
278         }
279         break;
280     case EXTENDED_BUTTON:{
281         QToolButton *extSettingsButton = new QToolButton;
282         setupButton( extSettingsButton );
283         CONNECT_MAP_SET( extSettingsButton, EXTENDED_ACTION );
284         BUTTON_SET_BAR( extSettingsButton )
285         widget = extSettingsButton;
286         }
287         break;
288     case PLAYLIST_BUTTON:{
289         QToolButton *playlistButton = new QToolButton;
290         setupButton( playlistButton );
291         CONNECT_MAP_SET( playlistButton, PLAYLIST_ACTION );
292         BUTTON_SET_BAR( playlistButton );
293         widget = playlistButton;
294         }
295         break;
296     case SNAPSHOT_BUTTON:{
297         QToolButton *snapshotButton = new QToolButton;
298         setupButton( snapshotButton );
299         CONNECT_MAP_SET( snapshotButton, SNAPSHOT_ACTION );
300         BUTTON_SET_BAR(  snapshotButton );
301         ENABLE_ON_VIDEO( snapshotButton );
302         widget = snapshotButton;
303         }
304         break;
305     case RECORD_BUTTON:{
306         QToolButton *recordButton = new QToolButton;
307         setupButton( recordButton );
308         CONNECT_MAP_SET( recordButton, RECORD_ACTION );
309         BUTTON_SET_BAR(  recordButton );
310         ENABLE_ON_INPUT( recordButton );
311         recordButton->setCheckable( true );
312         CONNECT( THEMIM->getIM(), recordingStateChanged( bool ),
313                  recordButton, setChecked( bool ) );
314         widget = recordButton;
315         }
316         break;
317     case ATOB_BUTTON: {
318         AtoB_Button *ABButton = new AtoB_Button;
319         setupButton( ABButton );
320         BUTTON_SET_BAR( ABButton );
321         ENABLE_ON_INPUT( ABButton );
322         CONNECT_MAP_SET( ABButton, ATOB_ACTION );
323         CONNECT( THEMIM->getIM(), AtoBchanged( bool, bool),
324                  ABButton, setIcons( bool, bool ) );
325         widget = ABButton;
326         }
327         break;
328     case INPUT_SLIDER: {
329         InputSlider *slider = new InputSlider( Qt::Horizontal, NULL );
330
331         /* Update the position when the IM has changed */
332         CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
333                 slider, setPosition( float, int, int ) );
334         /* And update the IM, when the position has changed */
335         CONNECT( slider, sliderDragged( float ),
336                  THEMIM->getIM(), sliderUpdate( float ) );
337         widget = slider;
338         }
339         break;
340     case MENU_BUTTONS:
341         widget = discFrame();
342         widget->hide();
343         break;
344     case TELETEXT_BUTTONS:
345         widget = telexFrame();
346         widget->hide();
347         break;
348     case VOLUME_SPECIAL:
349         b_special = true;
350     case VOLUME:
351         {
352             SoundWidget *snd = new SoundWidget( this, p_intf, b_shiny, b_special );
353             widget = snd;
354         }
355         break;
356     case TIME_LABEL:
357         {
358             TimeLabel *timeLabel = new TimeLabel( p_intf );
359             widget = timeLabel;
360         }
361         break;
362     case SPLITTER:
363         {
364             QFrame *line = new QFrame;
365             line->setFrameShape( QFrame::VLine );
366             line->setFrameShadow( QFrame::Raised );
367             line->setLineWidth( 0 );
368             line->setMidLineWidth( 1 );
369             widget = line;
370         }
371         break;
372     case ADVANCED_CONTROLLER:
373         {
374             advControls = new AdvControlsWidget( p_intf, this );
375             widget = advControls;
376         }
377         break;
378     case REVERSE_BUTTON:{
379         QToolButton *reverseButton = new QToolButton;
380         setupButton( reverseButton );
381         CONNECT_MAP_SET( reverseButton, REVERSE_ACTION );
382         BUTTON_SET_BAR(  reverseButton );
383         ENABLE_ON_INPUT( reverseButton );
384         widget = reverseButton;
385         }
386         break;
387     case SKIP_BACK_BUTTON: {
388         QToolButton *skipBakButton = new QToolButton;
389         setupButton( skipBakButton );
390         CONNECT_MAP_SET( skipBakButton, SKIP_BACK_ACTION );
391         BUTTON_SET_BAR(  skipBakButton );
392         ENABLE_ON_INPUT( skipBakButton );
393         widget = skipBakButton;
394         }
395         break;
396     case SKIP_FW_BUTTON: {
397         QToolButton *skipFwButton = new QToolButton;
398         setupButton( skipFwButton );
399         CONNECT_MAP_SET( skipFwButton, SKIP_FW_ACTION );
400         BUTTON_SET_BAR(  skipFwButton );
401         ENABLE_ON_INPUT( skipFwButton );
402         widget = skipFwButton;
403         }
404         break;
405     case QUIT_BUTTON: {
406         QToolButton *quitButton = new QToolButton;
407         setupButton( quitButton );
408         CONNECT_MAP_SET( quitButton, QUIT_ACTION );
409         BUTTON_SET_BAR(  quitButton );
410         widget = quitButton;
411         }
412         break;
413     default:
414         msg_Warn( p_intf, "This should not happen %i", button );
415         break;
416     }
417
418     /* Customize Buttons */
419     if( b_flat || b_big )
420     {
421         QToolButton *tmpButton = qobject_cast<QToolButton *>(widget);
422         if( tmpButton )
423         {
424             if( b_flat )
425                 tmpButton->setAutoRaise( b_flat );
426             if( b_big )
427             {
428                 tmpButton->setFixedSize( QSize( 32, 32 ) );
429                 tmpButton->setIconSize( QSize( 26, 26 ) );
430             }
431         }
432     }
433     return widget;
434 }
435
436 QFrame *AbstractController::discFrame()
437 {
438     /** Disc and Menus handling */
439     QFrame *discFrame = new QFrame( this );
440
441     QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
442     discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
443
444     QToolButton *prevSectionButton = new QToolButton( discFrame );
445     setupButton( prevSectionButton );
446     BUTTON_SET_BAR2( prevSectionButton, dvd_prev,
447             qtr("Previous Chapter/Title" ) );
448     discLayout->addWidget( prevSectionButton );
449
450     QToolButton *menuButton = new QToolButton( discFrame );
451     setupButton( menuButton );
452     discLayout->addWidget( menuButton );
453     BUTTON_SET_BAR2( menuButton, dvd_menu, qtr( "Menu" ) );
454
455     QToolButton *nextSectionButton = new QToolButton( discFrame );
456     setupButton( nextSectionButton );
457     discLayout->addWidget( nextSectionButton );
458     BUTTON_SET_BAR2( nextSectionButton, dvd_next,
459             qtr("Next Chapter/Title" ) );
460
461     /* Change the navigation button display when the IM
462        navigation changes */
463     CONNECT( THEMIM->getIM(), titleChanged( bool ),
464             discFrame, setVisible( bool ) );
465     CONNECT( THEMIM->getIM(), chapterChanged( bool ),
466             menuButton, setVisible( bool ) );
467     /* Changes the IM navigation when triggered on the nav buttons */
468     CONNECT( prevSectionButton, clicked(), THEMIM->getIM(),
469             sectionPrev() );
470     CONNECT( nextSectionButton, clicked(), THEMIM->getIM(),
471             sectionNext() );
472     CONNECT( menuButton, clicked(), THEMIM->getIM(),
473             sectionMenu() );
474
475     return discFrame;
476 }
477
478 QFrame *AbstractController::telexFrame()
479 {
480     /**
481      * Telextext QFrame
482      **/
483     TeletextController *telexFrame = new TeletextController;
484     QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
485     telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
486     CONNECT( THEMIM->getIM(), teletextPossible( bool ),
487              telexFrame, setVisible( bool ) );
488
489     /* On/Off button */
490     QToolButton *telexOn = new QToolButton;
491     telexFrame->telexOn = telexOn;
492     setupButton( telexOn );
493     BUTTON_SET_BAR2( telexOn, tv, qtr( "Teletext Activation" ) );
494     telexLayout->addWidget( telexOn );
495
496     /* Teletext Activation and set */
497     CONNECT( telexOn, clicked( bool ),
498              THEMIM->getIM(), activateTeletext( bool ) );
499     CONNECT( THEMIM->getIM(), teletextActivated( bool ),
500              telexFrame, enableTeletextButtons( bool ) );
501
502
503     /* Transparency button */
504     QToolButton *telexTransparent = new QToolButton;
505     telexFrame->telexTransparent = telexTransparent;
506     setupButton( telexTransparent );
507     BUTTON_SET_BAR2( telexTransparent, tvtelx,
508                      qtr( "Toggle Transparency " ) );
509     telexTransparent->setEnabled( false );
510     telexTransparent->setCheckable( true );
511     telexLayout->addWidget( telexTransparent );
512
513     /* Transparency change and set */
514     CONNECT( telexTransparent, clicked( bool ),
515             THEMIM->getIM(), telexSetTransparency( bool ) );
516     CONNECT( THEMIM->getIM(), teletextTransparencyActivated( bool ),
517              telexTransparent, setChecked( bool ) );
518
519
520     /* Page setting */
521     QSpinBox *telexPage = new QSpinBox;
522     telexFrame->telexPage = telexPage;
523     telexPage->setRange( 0, 999 );
524     telexPage->setValue( 100 );
525     telexPage->setAccelerated( true );
526     telexPage->setWrapping( true );
527     telexPage->setAlignment( Qt::AlignRight );
528     telexPage->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Minimum );
529     telexPage->setEnabled( false );
530     telexLayout->addWidget( telexPage );
531
532     /* Page change and set */
533     CONNECT( telexPage, valueChanged( int ),
534             THEMIM->getIM(), telexSetPage( int ) );
535     CONNECT( THEMIM->getIM(), newTelexPageSet( int ),
536             telexPage, setValue( int ) );
537
538     return telexFrame;
539 }
540 #undef CONNECT_MAP
541 #undef SET_MAPPING
542 #undef CONNECT_MAP_SET
543 #undef BUTTON_SET_BAR
544 #undef ENABLE_ON_VIDEO
545 #undef ENABLE_ON_INPUT
546
547 #include <QHBoxLayout>
548 /*****************************
549  * DA Control Widget !
550  *****************************/
551 ControlsWidget::ControlsWidget( intf_thread_t *_p_i,
552                                 bool b_advControls,
553                                 QWidget *_parent ) :
554                                 AbstractController( _p_i, _parent )
555 {
556     setSizePolicy( QSizePolicy::Preferred , QSizePolicy::Maximum );
557
558     /* advanced Controls handling */
559     b_advancedVisible = b_advControls;
560
561     QVBoxLayout *controlLayout = new QVBoxLayout( this );
562     controlLayout->setLayoutMargins( 6, 4, 6, 2, 5 );
563     controlLayout->setSpacing( 0 );
564     QHBoxLayout *controlLayout1 = new QHBoxLayout;
565     controlLayout1->setSpacing( 0 );
566
567     QString line1 = getSettings()->value( "MainToolbar1", MAIN_TB1_DEFAULT )
568                                         .toString();
569     parseAndCreate( line1, controlLayout1 );
570
571     QHBoxLayout *controlLayout2 = new QHBoxLayout;
572     controlLayout2->setSpacing( 0 );
573     QString line2 = getSettings()->value( "MainToolbar2", MAIN_TB2_DEFAULT )
574                                         .toString();
575     parseAndCreate( line2, controlLayout2 );
576
577     if( !b_advancedVisible && advControls ) advControls->hide();
578
579     controlLayout->addLayout( controlLayout1 );
580     controlLayout->addLayout( controlLayout2 );
581 }
582
583 ControlsWidget::~ControlsWidget()
584 {}
585
586 void ControlsWidget::toggleAdvanced()
587 {
588     if( !advControls ) return;
589
590     if( !b_advancedVisible )
591     {
592         advControls->show();
593         b_advancedVisible = true;
594     }
595     else
596     {
597         advControls->hide();
598         b_advancedVisible = false;
599     }
600     emit advancedControlsToggled( b_advancedVisible );
601 }
602
603 AdvControlsWidget::AdvControlsWidget( intf_thread_t *_p_i, QWidget *_parent ) :
604                                      AbstractController( _p_i, _parent )
605 {
606     controlLayout = new QHBoxLayout( this );
607     controlLayout->setMargin( 0 );
608     controlLayout->setSpacing( 0 );
609
610     QString line = getSettings()->value( "AdvToolbar", ADV_TB_DEFAULT )
611         .toString();
612     parseAndCreate( line, controlLayout );
613 }
614
615 InputControlsWidget::InputControlsWidget( intf_thread_t *_p_i, QWidget *_parent ) :
616                                      AbstractController( _p_i, _parent )
617 {
618     controlLayout = new QHBoxLayout( this );
619     controlLayout->setMargin( 0 );
620     controlLayout->setSpacing( 0 );
621
622     QString line = getSettings()->value( "InputToolbar", INPT_TB_DEFAULT ).toString();
623     parseAndCreate( line, controlLayout );
624 }
625 /**********************************************************************
626  * Fullscrenn control widget
627  **********************************************************************/
628 FullscreenControllerWidget::FullscreenControllerWidget( intf_thread_t *_p_i )
629                            : AbstractController( _p_i )
630 {
631     i_mouse_last_x      = -1;
632     i_mouse_last_y      = -1;
633     b_mouse_over        = false;
634     i_mouse_last_move_x = -1;
635     i_mouse_last_move_y = -1;
636 #if HAVE_TRANSPARENCY
637     b_slow_hide_begin   = false;
638     i_slow_hide_timeout = 1;
639 #endif
640     b_fullscreen        = false;
641     i_hide_timeout      = 1;
642     i_screennumber      = -1;
643
644     vout.clear();
645
646     setWindowFlags( Qt::ToolTip );
647     setMinimumWidth( 600 );
648
649     setFrameShape( QFrame::StyledPanel );
650     setFrameStyle( QFrame::Sunken );
651     setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
652
653     QVBoxLayout *controlLayout2 = new QVBoxLayout( this );
654     controlLayout2->setLayoutMargins( 5, 2, 5, 2, 5 );
655
656     /* First line */
657     InputControlsWidget *inputC = new InputControlsWidget( p_intf, this );
658     controlLayout2->addWidget( inputC );
659
660     controlLayout = new QHBoxLayout;
661     QString line = getSettings()->value( "MainWindow/FSCtoolbar", FSC_TB_DEFAULT ).toString();
662     parseAndCreate( line, controlLayout );
663     controlLayout2->addLayout( controlLayout );
664
665     /* hiding timer */
666     p_hideTimer = new QTimer( this );
667     CONNECT( p_hideTimer, timeout(), this, hideFSC() );
668     p_hideTimer->setSingleShot( true );
669
670     /* slow hiding timer */
671 #if HAVE_TRANSPARENCY
672     p_slowHideTimer = new QTimer( this );
673     CONNECT( p_slowHideTimer, timeout(), this, slowHideFSC() );
674 #endif
675
676     adjustSize ();  /* need to get real width and height for moving */
677
678 #ifdef WIN32TRICK
679     setWindowOpacity( 0.0 );
680     b_fscHidden = true;
681     adjustSize();
682     show();
683 #endif
684
685     vlc_mutex_init_recursive( &lock );
686
687     CONNECT( THEMIM->getIM(), voutListChanged( vout_thread_t **, int ), this, setVoutList( vout_thread_t **, int ) );
688 }
689
690 FullscreenControllerWidget::~FullscreenControllerWidget()
691 {
692     getSettings()->setValue( "FullScreen/pos", pos() );
693     setVoutList( NULL, 0 );
694     vlc_mutex_destroy( &lock );
695 }
696
697 /**
698  * Show fullscreen controller
699  */
700 void FullscreenControllerWidget::showFSC()
701 {
702     adjustSize();
703     /* center down */
704     int number = QApplication::desktop()->screenNumber( p_intf->p_sys->p_mi );
705     if( number != i_screennumber ||
706         screenRes != QApplication::desktop()->screenGeometry(number) )
707     {
708         screenRes = QApplication::desktop()->screenGeometry(number);
709         msg_Dbg( p_intf, "Calculation fullscreen controllers center");
710         /* screen has changed, calculate new position */
711         QPoint pos = QPoint( screenRes.x() + (screenRes.width() / 2) - (width() / 2),
712                              screenRes.y() + screenRes.height() - height());
713         move( pos );
714         i_screennumber = number;
715     }
716 #ifdef WIN32TRICK
717     // after quiting and going to fs, we need to call show()
718     if( isHidden() )
719         show();
720     if( b_fscHidden )
721     {
722         b_fscHidden = false;
723         setWindowOpacity( 1.0 );
724     }
725 #else
726     show();
727 #endif
728
729 #if HAVE_TRANSPARENCY
730     setWindowOpacity( DEFAULT_OPACITY );
731 #endif
732 }
733
734 /**
735  * Hide fullscreen controller
736  * FIXME: under windows it have to be done by moving out of screen
737  *        because hide() doesnt work
738  */
739 void FullscreenControllerWidget::hideFSC()
740 {
741 #ifdef WIN32TRICK
742     b_fscHidden = true;
743     setWindowOpacity( 0.0 );    // simulate hidding
744 #else
745     hide();
746 #endif
747 }
748
749 /**
750  * Plane to hide fullscreen controller
751  */
752 void FullscreenControllerWidget::planHideFSC()
753 {
754     vlc_mutex_lock( &lock );
755     int i_timeout = i_hide_timeout;
756     vlc_mutex_unlock( &lock );
757
758     p_hideTimer->start( i_timeout );
759
760 #if HAVE_TRANSPARENCY
761     b_slow_hide_begin = true;
762     i_slow_hide_timeout = i_timeout;
763     p_slowHideTimer->start( i_slow_hide_timeout / 2 );
764 #endif
765 }
766
767 /**
768  * Hidding fullscreen controller slowly
769  * Linux: need composite manager
770  * Windows: it is blinking, so it can be enabled by define TRASPARENCY
771  */
772 void FullscreenControllerWidget::slowHideFSC()
773 {
774 #if HAVE_TRANSPARENCY
775     if( b_slow_hide_begin )
776     {
777         b_slow_hide_begin = false;
778
779         p_slowHideTimer->stop();
780         /* the last part of time divided to 100 pieces */
781         p_slowHideTimer->start( (int)( i_slow_hide_timeout / 2 / ( windowOpacity() * 100 ) ) );
782
783     }
784     else
785     {
786 #ifdef WIN32TRICK
787          if ( windowOpacity() > 0.0 && !b_fscHidden )
788 #else
789          if ( windowOpacity() > 0.0 )
790 #endif
791          {
792              /* we should use 0.01 because of 100 pieces ^^^
793                 but than it cannt be done in time */
794              setWindowOpacity( windowOpacity() - 0.02 );
795          }
796
797          if ( windowOpacity() <= 0.0 )
798              p_slowHideTimer->stop();
799     }
800 #endif
801 }
802
803 /**
804  * event handling
805  * events: show, hide, start timer for hidding
806  */
807 void FullscreenControllerWidget::customEvent( QEvent *event )
808 {
809     bool b_fs;
810
811     switch( event->type() )
812     {
813         case FullscreenControlToggle_Type:
814             vlc_mutex_lock( &lock );
815             b_fs = b_fullscreen;
816             vlc_mutex_unlock( &lock );
817             if( b_fs )
818             {
819 #ifdef WIN32TRICK
820                 if( b_fscHidden )
821 #else
822                 if( isHidden() )
823 #endif
824                 {
825                     p_hideTimer->stop();
826                     showFSC();
827                 }
828                 else
829                     hideFSC();
830             }
831             break;
832         case FullscreenControlShow_Type:
833             vlc_mutex_lock( &lock );
834             b_fs = b_fullscreen;
835             vlc_mutex_unlock( &lock );
836
837 #ifdef WIN32TRICK
838             if( b_fs && b_fscHidden )
839 #else
840             if( b_fs && !isVisible() )
841 #endif
842                 showFSC();
843             break;
844         case FullscreenControlHide_Type:
845             hideFSC();
846             break;
847         case FullscreenControlPlanHide_Type:
848             if( !b_mouse_over ) // Only if the mouse is not over FSC
849                 planHideFSC();
850             break;
851         default:
852             break;
853     }
854 }
855
856 /**
857  * On mouse move
858  * moving with FSC
859  */
860 void FullscreenControllerWidget::mouseMoveEvent( QMouseEvent *event )
861 {
862     if ( event->buttons() == Qt::LeftButton )
863     {
864         int i_moveX = event->globalX() - i_mouse_last_x;
865         int i_moveY = event->globalY() - i_mouse_last_y;
866
867         move( x() + i_moveX, y() + i_moveY );
868
869         i_mouse_last_x = event->globalX();
870         i_mouse_last_y = event->globalY();
871     }
872 }
873
874 /**
875  * On mouse press
876  * store position of cursor
877  */
878 void FullscreenControllerWidget::mousePressEvent( QMouseEvent *event )
879 {
880     i_mouse_last_x = event->globalX();
881     i_mouse_last_y = event->globalY();
882 }
883
884 /**
885  * On mouse go above FSC
886  */
887 void FullscreenControllerWidget::enterEvent( QEvent *event )
888 {
889     b_mouse_over = true;
890
891     p_hideTimer->stop();
892 #if HAVE_TRANSPARENCY
893     p_slowHideTimer->stop();
894 #endif
895     event->accept();
896 }
897
898 /**
899  * On mouse go out from FSC
900  */
901 void FullscreenControllerWidget::leaveEvent( QEvent *event )
902 {
903     planHideFSC();
904
905     b_mouse_over = false;
906     event->accept();
907 }
908
909 /**
910  * When you get pressed key, send it to video output
911  * FIXME: clearing focus by clearFocus() to not getting
912  * key press events didnt work
913  */
914 void FullscreenControllerWidget::keyPressEvent( QKeyEvent *event )
915 {
916     int i_vlck = qtEventToVLCKey( event );
917     if( i_vlck > 0 )
918     {
919         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
920         event->accept();
921     }
922     else
923         event->ignore();
924 }
925
926 /* */
927 static int FullscreenControllerWidgetFullscreenChanged( vlc_object_t *vlc_object,
928                 const char *variable, vlc_value_t old_val,
929                 vlc_value_t new_val,  void *data )
930 {
931     vout_thread_t *p_vout = (vout_thread_t *) vlc_object;
932
933     msg_Dbg( p_vout, "Qt4: Fullscreen state changed" );
934     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
935
936     p_fs->fullscreenChanged( p_vout, new_val.b_bool, var_GetInteger( p_vout, "mouse-hide-timeout" ) );
937
938     return VLC_SUCCESS;
939 }
940 /* */
941 static int FullscreenControllerWidgetMouseMoved( vlc_object_t *vlc_object, const char *variable,
942                                                  vlc_value_t old_val, vlc_value_t new_val,
943                                                  void *data )
944 {
945     vout_thread_t *p_vout = (vout_thread_t *)vlc_object;
946     FullscreenControllerWidget *p_fs = (FullscreenControllerWidget *)data;
947
948     /* Get the value from the Vout - Trust the vout more than Qt */
949     const int i_mousex = var_GetInteger( p_vout, "mouse-x" );
950     const int i_mousey = var_GetInteger( p_vout, "mouse-y" );
951
952     p_fs->mouseChanged( p_vout, i_mousex, i_mousey );
953
954     return VLC_SUCCESS;
955 }
956
957 /**
958  * It is call to update the list of vout handled by the fullscreen controller
959  */
960 void FullscreenControllerWidget::setVoutList( vout_thread_t **pp_vout, int i_vout )
961 {
962     QList<vout_thread_t*> del;
963     QList<vout_thread_t*> add;
964
965     QList<vout_thread_t*> set;
966
967     /* */
968     for( int i = 0; i < i_vout; i++ )
969         set += pp_vout[i];
970
971     /* Vout to remove */
972     vlc_mutex_lock( &lock );
973     foreach( vout_thread_t *p_vout, vout )
974     {
975         if( !set.contains( p_vout ) )
976             del += p_vout;
977     }
978     vlc_mutex_unlock( &lock );
979
980     foreach( vout_thread_t *p_vout, del )
981     {
982         var_DelCallback( p_vout, "fullscreen",
983                          FullscreenControllerWidgetFullscreenChanged, this );
984         vlc_mutex_lock( &lock );
985         fullscreenChanged( p_vout, false, 0 );
986         vout.removeAll( p_vout );
987         vlc_mutex_unlock( &lock );
988     }
989
990     /* Vout to track */
991     vlc_mutex_lock( &lock );
992     foreach( vout_thread_t *p_vout, set )
993     {
994         if( !vout.contains( p_vout ) )
995             add += p_vout;
996     }
997     vlc_mutex_unlock( &lock );
998
999     foreach( vout_thread_t *p_vout, add )
1000     {
1001         vlc_object_hold( (vlc_object_t*)p_vout );
1002
1003         vlc_mutex_lock( &lock );
1004         vout.append( p_vout );
1005         var_AddCallback( p_vout, "fullscreen",
1006                          FullscreenControllerWidgetFullscreenChanged, this );
1007         /* I miss a add and fire */
1008         fullscreenChanged( p_vout, var_GetBool( p_vout, "fullscreen" ),
1009                            var_GetInteger( p_vout, "mouse-hide-timeout" ) );
1010         vlc_mutex_unlock( &lock );
1011     }
1012 }
1013 /**
1014  * Register and unregister callback for mouse moving
1015  */
1016 void FullscreenControllerWidget::fullscreenChanged( vout_thread_t *p_vout,
1017         bool b_fs, int i_timeout )
1018 {
1019     /* FIXME - multiple vout (ie multiple mouse position ?) and thread safety if multiple vout ? */
1020     msg_Dbg( p_vout, "Qt: Entering Fullscreen" );
1021
1022     vlc_mutex_lock( &lock );
1023     /* Entering fullscreen, register callback */
1024     if( b_fs && !b_fullscreen )
1025     {
1026         b_fullscreen = true;
1027         i_hide_timeout = i_timeout;
1028         var_AddCallback( p_vout, "mouse-moved",
1029                 FullscreenControllerWidgetMouseMoved, this );
1030     }
1031     /* Quitting fullscreen, unregistering callback */
1032     else if( !b_fs && b_fullscreen )
1033     {
1034         b_fullscreen = false;
1035         i_hide_timeout = i_timeout;
1036         var_DelCallback( p_vout, "mouse-moved",
1037                 FullscreenControllerWidgetMouseMoved, this );
1038
1039         /* Force fs hidding */
1040         IMEvent *eHide = new IMEvent( FullscreenControlHide_Type, 0 );
1041         QApplication::postEvent( this, eHide );
1042     }
1043     vlc_mutex_unlock( &lock );
1044 }
1045 /**
1046  * Mouse change callback (show/hide the controller on mouse movement)
1047  */
1048 void FullscreenControllerWidget::mouseChanged( vout_thread_t *p_vout, int i_mousex, int i_mousey )
1049 {
1050     bool b_toShow;
1051
1052     /* FIXME - multiple vout (ie multiple mouse position ?) and thread safety if multiple vout ? */
1053
1054     b_toShow = false;
1055     if( ( i_mouse_last_move_x == -1 || i_mouse_last_move_y == -1 ) ||
1056         ( abs( i_mouse_last_move_x - i_mousex ) > 2 ||
1057           abs( i_mouse_last_move_y - i_mousey ) > 2 ) )
1058     {
1059         i_mouse_last_move_x = i_mousex;
1060         i_mouse_last_move_y = i_mousey;
1061         b_toShow = true;
1062     }
1063
1064     if( b_toShow )
1065     {
1066         /* Show event */
1067         IMEvent *eShow = new IMEvent( FullscreenControlShow_Type, 0 );
1068         QApplication::postEvent( this, eShow );
1069
1070         /* Plan hide event */
1071         IMEvent *eHide = new IMEvent( FullscreenControlPlanHide_Type, 0 );
1072         QApplication::postEvent( this, eHide );
1073     }
1074 }
1075