]> git.sesse.net Git - vlc/blob - modules/gui/qt4/main_interface.cpp
Don't include config.h from the headers - refs #297.
[vlc] / modules / gui / qt4 / main_interface.cpp
1 /*****************************************************************************
2  * main_interface.cpp : Main interface
3  ****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@videolan.org>
9  *          Ilkka Ollakka <ileoo@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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "qt4.hpp"
30 #include "main_interface.hpp"
31 #include "input_manager.hpp"
32 #include "util/qvlcframe.hpp"
33 #include "util/customwidgets.hpp"
34 #include "dialogs_provider.hpp"
35 #include "components/interface_widgets.hpp"
36 #include "components/playlist/playlist.hpp"
37 #include "dialogs/extended.hpp"
38 #include "dialogs/playlist.hpp"
39 #include "menus.hpp"
40
41 #include <QMenuBar>
42 #include <QCloseEvent>
43 #include <QPushButton>
44 #include <QStatusBar>
45 #include <QKeyEvent>
46 #include <QUrl>
47 #include <QSystemTrayIcon>
48 #include <QSize>
49 #include <QMenu>
50 #include <QLabel>
51 #include <QSlider>
52 #include <QWidgetAction>
53 #include <QDockWidget>
54 #include <QToolBar>
55 #include <QGroupBox>
56 #include <QDate>
57 #include <QProgressBar>
58
59 #include <assert.h>
60 #include <vlc_keys.h>
61 #include <vlc_vout.h>
62
63 #define SET_WIDTH(i,j) i->widgetSize.setWidth(j)
64 #define SET_HEIGHT(i,j) i->widgetSize.setHeight(j)
65 #define SET_WH( i,j,k) i->widgetSize.setWidth(j); i->widgetSize.setHeight(k);
66
67 #define DS(i) i.width(),i.height()
68
69 /* Callback prototypes */
70 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
71                         vlc_value_t old_val, vlc_value_t new_val, void *param );
72 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
73                        vlc_value_t old_val, vlc_value_t new_val, void *param );
74 static int InteractCallback( vlc_object_t *, const char *, vlc_value_t,
75                              vlc_value_t, void *);
76 /* Video handling */
77 static void *DoRequest( intf_thread_t *p_intf, vout_thread_t *p_vout,
78                         int *pi1, int *pi2, unsigned int*pi3,unsigned int*pi4)
79 {
80     return p_intf->p_sys->p_mi->requestVideo( p_vout, pi1, pi2, pi3, pi4 );
81 }
82 static void DoRelease( intf_thread_t *p_intf, void *p_win )
83 {
84     return p_intf->p_sys->p_mi->releaseVideo( p_win );
85 }
86 static int DoControl( intf_thread_t *p_intf, void *p_win, int i_q, va_list a )
87 {
88     return p_intf->p_sys->p_mi->controlVideo( p_win, i_q, a );
89 }
90
91 MainInterface::MainInterface( intf_thread_t *_p_intf ) : QVLCMW( _p_intf )
92 {
93     /* Variables initialisation */
94     // need_components_update = false;
95     bgWidget = NULL; videoWidget = NULL; playlistWidget = NULL;
96     videoIsActive = false;
97     input_name = "";
98     playlistVisible = false;
99
100     /* Ask for privacy */
101     privacy();
102
103     /**
104      *  Configuration and settings
105      **/
106     settings = new QSettings( "vlc", "vlc-qt-interface" );
107     settings->beginGroup( "MainWindow" );
108
109     /* Main settings */
110     setFocusPolicy( Qt::StrongFocus );
111     setAcceptDrops( true );
112     setWindowIcon( QApplication::windowIcon() );
113     setWindowOpacity( config_GetFloat( p_intf, "qt-opacity" ) );
114
115     /* Set The Video In emebedded Mode or not */
116     videoEmbeddedFlag = false;
117     if( config_GetInt( p_intf, "embedded-video" ) ) videoEmbeddedFlag = true;
118
119     /* Are we in the enhanced always-video mode or not ? */
120     alwaysVideoFlag = false;
121     if( videoEmbeddedFlag && config_GetInt( p_intf, "qt-always-video" ) )
122         alwaysVideoFlag = true;
123
124     /* Set the other interface settings */
125     //TODO: I don't like that code
126     visualSelectorEnabled = settings->value( "visual-selector", false ).toBool();
127     notificationEnabled = config_GetInt( p_intf, "qt-notification" )
128                           ? true : false;
129
130     /**************************
131      *  UI and Widgets design
132      **************************/
133     setVLCWindowsTitle();
134     handleMainUi( settings );
135
136     /* Create a Dock to get the playlist */
137     dockPL = new QDockWidget( qtr( "Playlist" ), this );
138     dockPL->setSizePolicy( QSizePolicy::Preferred,
139                            QSizePolicy::Expanding );
140     dockPL->setFeatures( QDockWidget::AllDockWidgetFeatures );
141     dockPL->setAllowedAreas( Qt::LeftDockWidgetArea
142                            | Qt::RightDockWidgetArea
143                            | Qt::BottomDockWidgetArea );
144     dockPL->hide();
145
146     /************
147      * Menu Bar
148      ************/
149     QVLCMenu::createMenuBar( this, p_intf, visualSelectorEnabled );
150
151     /****************
152      *  Status Bar  *
153      ****************/
154     /* Widgets Creation*/
155     b_remainingTime = false;
156     timeLabel = new TimeLabel;
157     timeLabel->setText( " --:--/--:-- " );
158     timeLabel->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
159     nameLabel = new QLabel;
160     nameLabel->setTextInteractionFlags( Qt::TextSelectableByMouse
161                                       | Qt::TextSelectableByKeyboard );
162     speedLabel = new QLabel( "1.00x" );
163     speedLabel->setContextMenuPolicy ( Qt::CustomContextMenu );
164
165     /* Styling those labels */
166     timeLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
167     speedLabel->setFrameStyle( QFrame::Sunken | QFrame::Panel );
168     nameLabel->setFrameStyle( QFrame::Sunken | QFrame::StyledPanel);
169
170     pgBar = new QProgressBar;
171     pgBar->hide();
172
173     /* and adding those */
174     statusBar()->addWidget( nameLabel, 8 );
175     statusBar()->addPermanentWidget( speedLabel, 0 );
176     statusBar()->addPermanentWidget( pgBar, 0 );
177     statusBar()->addPermanentWidget( timeLabel, 0 );
178
179     /* timeLabel behaviour:
180        - double clicking opens the goto time dialog
181        - right-clicking and clicking just toggle between remaining and
182          elapsed time.*/
183     CONNECT( timeLabel, timeLabelClicked(), this, toggleTimeDisplay() );
184     CONNECT( timeLabel, timeLabelDoubleClicked(), THEDP, gotoTimeDialog() );
185     CONNECT( timeLabel, timeLabelDoubleClicked(), this, toggleTimeDisplay() );
186
187     /* Speed Label behaviour:
188        - right click gives the vertical speed slider */
189     CONNECT( speedLabel, customContextMenuRequested( QPoint ),
190              this, showSpeedMenu( QPoint ) );
191
192     /**********************
193      * Systray Management *
194      **********************/
195     sysTray = NULL;
196     bool b_createSystray = false;
197     bool b_systrayAvailable = QSystemTrayIcon::isSystemTrayAvailable();
198     if( config_GetInt( p_intf, "qt-start-minimized") )
199     {
200         if( b_systrayAvailable )
201         {
202             b_createSystray = true;
203             hide(); //FIXME BUG HERE
204         }
205         else msg_Warn( p_intf, "You can't minize if you haven't a system "
206                 "tray bar" );
207     }
208     if( config_GetInt( p_intf, "qt-system-tray") )
209         b_createSystray = true;
210
211     if( b_systrayAvailable && b_createSystray )
212             createSystray();
213
214     if( config_GetInt( p_intf, "qt-minimal-view" ) )
215         toggleMinimalView();
216
217     /********************
218      * Input Manager    *
219      ********************/
220     MainInputManager::getInstance( p_intf );
221
222     /********************
223      * Various CONNECTs *
224      ********************/
225     /* Connect the input manager to the GUI elements it manages */
226
227     /* It is also connected to the control->slider, see the ControlsWidget */
228     CONNECT( THEMIM->getIM(), positionUpdated( float, int, int ),
229              this, setDisplayPosition( float, int, int ) );
230     /* Change the SpeedRate in the Status */
231     CONNECT( THEMIM->getIM(), rateChanged( int ), this, setRate( int ) );
232
233     /**
234      * Connects on nameChanged()
235      * Those connects are not merged because different options can trigger
236      * them down.
237      */
238     /* Naming in the controller statusbar */
239     CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
240              setName( QString ) );
241     /* and in the systray */
242     if( sysTray )
243     {
244         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
245                  updateSystrayTooltipName( QString ) );
246     }
247     /* and in the title of the controller */
248     if( config_GetInt( p_intf, "qt-name-in-title" ) )
249     {
250         CONNECT( THEMIM->getIM(), nameChanged( QString ), this,
251              setVLCWindowsTitle( QString ) );
252     }
253
254     /**
255      * CONNECTS on PLAY_STATUS
256      **/
257     /* Status on the main controller */
258     CONNECT( THEMIM->getIM(), statusChanged( int ), this, setStatus( int ) );
259     /* and in the systray */
260     if( sysTray )
261     {
262         CONNECT( THEMIM->getIM(), statusChanged( int ), this,
263                  updateSystrayTooltipStatus( int ) );
264     }
265
266     /** OnTimeOut **/
267     // TODO
268     ON_TIMEOUT( updateOnTimer() );
269     //ON_TIMEOUT( debug() );
270
271     /**
272      * Callbacks
273      **/
274     var_Create( p_intf, "interaction", VLC_VAR_ADDRESS );
275     var_AddCallback( p_intf, "interaction", InteractCallback, this );
276     p_intf->b_interaction = VLC_TRUE;
277
278     /* Register callback for the intf-popupmenu variable */
279     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
280                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
281     if( p_playlist != NULL )
282     {
283         var_AddCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
284         var_AddCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
285         vlc_object_release( p_playlist );
286     }
287
288     /* VideoWidget connect mess to avoid different threads speaking to each other */
289     CONNECT( this, askReleaseVideo( void * ), this, releaseVideoSlot( void * ) );
290     CONNECT( this, askVideoToResize( unsigned int, unsigned int ),
291              videoWidget, SetSizing( unsigned int, unsigned int ) );
292     CONNECT( this, askUpdate(), this, doComponentsUpdate() );
293
294     CONNECT( dockPL, topLevelChanged( bool ), this, doComponentsUpdate() );
295     CONNECT( controls, advancedControlsToggled( bool ),
296              this, doComponentsUpdate() );
297
298     move( settings->value( "pos", QPoint( 0, 0 ) ).toPoint() );
299
300     QSize newSize = settings->value( "size", QSize( 350, 60 ) ).toSize();
301     if( newSize.isValid() )
302     {
303         resize( newSize );
304     }
305     else
306     {
307         msg_Warn( p_intf, "Invalid size in constructor" );
308     }
309
310     int tgPlay = settings->value( "playlist-visible", 0 ).toInt();
311     settings->endGroup();
312
313     if( tgPlay )
314     {
315         togglePlaylist();
316     }
317
318     updateGeometry();
319
320 }
321
322 MainInterface::~MainInterface()
323 {
324     msg_Dbg( p_intf, "Destroying the main interface" );
325
326     if( playlistWidget ) playlistWidget->savingSettings( settings );
327     if( ExtendedDialog::exists() )
328         ExtendedDialog::getInstance( p_intf )->savingSettings();
329
330     settings->beginGroup( "MainWindow" );
331     settings->setValue( "playlist-floats", (int)(dockPL->isFloating()) );
332     settings->setValue( "playlist-visible", (int)playlistVisible );
333     settings->setValue( "adv-controls",
334                         getControlsVisibilityStatus() & CONTROLS_ADVANCED );
335     settings->setValue( "pos", pos() );
336     settings->setValue( "size", size() );
337     if( bgWidget )
338         settings->setValue( "backgroundSize", bgWidget->size() );
339
340     settings->endGroup();
341     delete settings;
342
343     /* Unregister callback for the intf-popupmenu variable */
344     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_intf,
345                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
346     if( p_playlist != NULL )
347     {
348         var_DelCallback( p_playlist, "intf-popupmenu", PopupMenuCB, p_intf );
349         var_DelCallback( p_playlist, "intf-show", IntfShowCB, p_intf );
350         vlc_object_release( p_playlist );
351     }
352
353     p_intf->b_interaction = VLC_FALSE;
354     var_DelCallback( p_intf, "interaction", InteractCallback, this );
355
356     p_intf->pf_request_window = NULL;
357     p_intf->pf_release_window = NULL;
358     p_intf->pf_control_window = NULL;
359     p_intf->p_sys->p_mi = NULL;
360 }
361
362 /*****************************
363  *   Main UI handling        *
364  *****************************/
365
366 /**
367  * Give the decorations of the Main Window a correct Name.
368  * If nothing is given, set it to VLC...
369  **/
370 void MainInterface::setVLCWindowsTitle( QString aTitle )
371 {
372     if( aTitle.isEmpty() )
373     {
374         setWindowTitle( qtr( "VLC media player" ) );
375     }
376     else
377     {
378         setWindowTitle( aTitle + " - " + qtr( "VLC media player" ) );
379     }
380 }
381
382 void MainInterface::handleMainUi( QSettings *settings )
383 {
384     /* Create the main Widget and the mainLayout */
385     QWidget *main = new QWidget;
386     setCentralWidget( main );
387     mainLayout = new QVBoxLayout( main );
388
389     /* Margins, spacing */
390     main->setContentsMargins( 0, 0, 0, 0 );
391    // main->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum );
392     mainLayout->setMargin( 0 );
393
394     /* Create the CONTROLS Widget */
395     /* bool b_shiny = config_GetInt( p_intf, "qt-blingbling" ); */
396     controls = new ControlsWidget( p_intf, this,
397                    settings->value( "adv-controls", false ).toBool(),
398                    config_GetInt( p_intf, "qt-blingbling" ) );
399
400     /* Add the controls Widget to the main Widget */
401     mainLayout->insertWidget( 0, controls );
402
403     /* Create the Speed Control Widget */
404     speedControl = new SpeedControlWidget( p_intf );
405     speedControlMenu = new QMenu( this );
406
407     QWidgetAction *widgetAction = new QWidgetAction( speedControl );
408     widgetAction->setDefaultWidget( speedControl );
409     speedControlMenu->addAction( widgetAction );
410
411     /* Visualisation */
412     /* Disabled for now, they SUCK */
413     #if 0
414     visualSelector = new VisualSelector( p_intf );
415     mainLayout->insertWidget( 0, visualSelector );
416     visualSelector->hide();
417     #endif
418
419     /* And video Outputs */
420     if( alwaysVideoFlag )
421     {
422         bgWidget = new BackgroundWidget( p_intf );
423         bgWidget->resize(
424              settings->value( "backgroundSize", QSize( 300, 150 ) ).toSize() );
425         bgWidget->updateGeometry();
426         mainLayout->insertWidget( 0, bgWidget );
427         CONNECT( this, askBgWidgetToToggle(), bgWidget, toggle() );
428     }
429
430     if( videoEmbeddedFlag )
431     {
432         videoWidget = new VideoWidget( p_intf );
433         mainLayout->insertWidget( 0, videoWidget );
434
435         p_intf->pf_request_window  = ::DoRequest;
436         p_intf->pf_release_window  = ::DoRelease;
437         p_intf->pf_control_window  = ::DoControl;
438     }
439
440     /* Finish the sizing */
441     updateGeometry();
442 }
443
444 inline void MainInterface::privacy()
445 {
446     /**
447      * Ask for the network policy on FIRST STARTUP
448      **/
449     if( config_GetInt( p_intf, "qt-privacy-ask") )
450     {
451         QList<ConfigControl *> controls;
452         if( privacyDialog( controls ) == QDialog::Accepted )
453         {
454             QList<ConfigControl *>::Iterator i;
455             for(  i = controls.begin() ; i != controls.end() ; i++ )
456             {
457                 ConfigControl *c = qobject_cast<ConfigControl *>(*i);
458                 c->doApply( p_intf );
459             }
460
461             config_PutInt( p_intf,  "qt-privacy-ask" , 0 );
462             config_SaveConfigFile( p_intf, NULL );
463         }
464     }
465 }
466
467 int MainInterface::privacyDialog( QList<ConfigControl *> controls )
468 {
469     QDialog *privacy = new QDialog( this );
470
471     privacy->setWindowTitle( qtr( "Privacy and Network policies" ) );
472
473     QGridLayout *gLayout = new QGridLayout( privacy );
474
475     QGroupBox *blabla = new QGroupBox( qtr( "Privacy and Network Warning" ) );
476     QGridLayout *blablaLayout = new QGridLayout( blabla );
477     QLabel *text = new QLabel( qtr(
478         "<p>The <i>VideoLAN Team</i> doesn't like when an application goes "
479         "online without authorization.</p>\n "
480         "<p><i>VLC media player</i> can request limited information on "
481         "the Internet, especially to get CD covers and songs metadata or to know "
482         "if updates are available.</p>\n"
483         "<p><i>VLC media player</i> <b>DOES NOT</b> send or collect <b>ANY</b> "
484         "information, even anonymously, about your usage.</p>\n"
485         "<p>Therefore please check the following options, the default being "
486         "almost no access on the web.</p>\n") );
487     text->setWordWrap( true );
488     text->setTextFormat( Qt::RichText );
489
490     blablaLayout->addWidget( text, 0, 0 ) ;
491
492     QGroupBox *options = new QGroupBox;
493     QGridLayout *optionsLayout = new QGridLayout( options );
494
495     gLayout->addWidget( blabla, 0, 0, 1, 3 );
496     gLayout->addWidget( options, 1, 0, 1, 3 );
497     module_config_t *p_config;
498     ConfigControl *control;
499     int line = 0;
500
501 #define CONFIG_GENERIC( option, type )                            \
502     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
503     if( p_config )                                                \
504     {                                                             \
505         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
506                 p_config, options, false, optionsLayout, line );  \
507         controls.append( control );                               \
508     }
509
510 #define CONFIG_GENERIC_NOBOOL( option, type )                     \
511     p_config =  config_FindConfig( VLC_OBJECT(p_intf), option );  \
512     if( p_config )                                                \
513     {                                                             \
514         control =  new type ## ConfigControl( VLC_OBJECT(p_intf), \
515                 p_config, options, optionsLayout, line );  \
516         controls.append( control );                               \
517     }
518
519     CONFIG_GENERIC( "album-art", IntegerList ); line++;
520     CONFIG_GENERIC_NOBOOL( "fetch-meta", Bool ); line++;
521 #ifdef UPDATE_CHECK
522     CONFIG_GENERIC_NOBOOL( "qt-updates-notif", Bool );
523 #endif
524     QPushButton *ok = new QPushButton( qtr( "Ok" ) );
525
526     gLayout->addWidget( ok, 2, 2 );
527
528     CONNECT( ok, clicked(), privacy, accept() );
529     return privacy->exec();
530 }
531
532 //FIXME remove me at the end...
533 void MainInterface::debug()
534 {
535     msg_Dbg( p_intf, "size: %i - %i", size().height(), size().width() );
536     msg_Dbg( p_intf, "sizeHint: %i - %i", sizeHint().height(), sizeHint().width() );
537 }
538
539 /**********************************************************************
540  * Handling of sizing of the components
541  **********************************************************************/
542
543 /* This function is probably wrong, but we don't have many many choices...
544    Since we can't know from the playlist Widget if we are inside a dock or not,
545    because the playlist Widget can be called by THEDP, as a separate windows for
546    the skins.
547    Maybe the other solution is to redefine the sizeHint() of the playlist and
548    ask _parent->isFloating()...
549    If you think this would be better, please FIXME it...
550 */
551 QSize MainInterface::sizeHint() const
552 {
553     int nwidth  = controls->sizeHint().width();
554     int nheight = controls->isVisible() ?
555                   controls->size().height()
556                   + menuBar()->size().height()
557                   + statusBar()->size().height()
558                   : 0 ;
559
560     msg_Dbg( p_intf, "1 %i %i", nheight, nwidth );
561     if( VISIBLE( bgWidget ) )
562     {
563         nheight += bgWidget->size().height();
564         nwidth  = bgWidget->size().width();
565         msg_Dbg( p_intf, "1b %i %i", nheight, nwidth );
566     }
567     else if( videoIsActive )
568     {
569         nheight += videoWidget->size().height();
570         nwidth  = videoWidget->size().width();
571         msg_Dbg( p_intf, "2 %i %i", nheight, nwidth );
572     }
573     if( !dockPL->isFloating() && dockPL->isVisible() && dockPL->widget()  )
574     {
575         nheight += dockPL->size().height();
576         nwidth = __MAX( nwidth, dockPL->size().width() );
577         msg_Dbg( p_intf, "3 %i %i", nheight, nwidth );
578     }
579     msg_Dbg( p_intf, "4 %i %i", nheight, nwidth );
580     return QSize( nwidth, nheight );
581 }
582
583 #if 0
584 /* FIXME This is dead code and need to be removed AT THE END */
585 void MainInterface::resizeEvent( QResizeEvent *e )
586 {
587     if( videoWidget )
588         videoWidget->widgetSize.setWidth( e->size().width() - addSize.width() );
589     if( videoWidget && videoIsActive && videoWidget->widgetSize.height() > 1 )
590     {
591         SET_WH( videoWidget, e->size().width() - addSize.width(),
592                              e->size().height()  - addSize.height() );
593         videoWidget->updateGeometry();
594     }
595     if( VISIBLE( playlistWidget ) )
596     {
597 //        SET_WH( playlistWidget , e->size().width() - addSize.width(),
598               //                   e->size().height() - addSize.height() );
599         playlistWidget->updateGeometry();
600     }
601 }
602 #endif
603
604 /****************************************************************************
605  * Small right-click menu for rate control
606  ****************************************************************************/
607 void MainInterface::showSpeedMenu( QPoint pos )
608 {
609     speedControlMenu->exec( QCursor::pos() - pos
610                           + QPoint( 0, speedLabel->height() ) );
611 }
612
613 /****************************************************************************
614  * Video Handling
615  ****************************************************************************/
616 class SetVideoOnTopQtEvent : public QEvent
617 {
618 public:
619     SetVideoOnTopQtEvent( bool _onTop ) :
620       QEvent( (QEvent::Type)SetVideoOnTopEvent_Type ), onTop( _onTop)
621     {}
622
623     bool OnTop() const
624     {
625         return onTop;
626     }
627
628 private:
629     bool onTop;
630 };
631
632 /* function called from ::DoRequest in order to show a nice VideoWidget
633     at the good size */
634 void *MainInterface::requestVideo( vout_thread_t *p_nvout, int *pi_x,
635                                    int *pi_y, unsigned int *pi_width,
636                                    unsigned int *pi_height )
637 {
638     bool bgWasVisible = false;
639
640     /* Request the videoWidget */
641     void *ret = videoWidget->request( p_nvout,pi_x, pi_y, pi_width, pi_height );
642     if( ret ) /* The videoWidget is available */
643     {
644         /* Did we have a bg ? Hide it! */
645         if( VISIBLE( bgWidget) )
646         {
647             bgWasVisible = true;
648             emit askBgWidgetToToggle();
649         }
650
651         /*if( THEMIM->getIM()->hasVideo() || !bgWasVisible )
652         {
653             videoWidget->widgetSize = QSize( *pi_width, *pi_height );
654         }
655         else /* Background widget available, use its size */
656         /*{
657             /* Ok, our visualizations are bad, so don't do this for the moment
658              * use the requested size anyway */
659             // videoWidget->widgetSize = bgWidget->widgeTSize;
660           /*  videoWidget->widgetSize = QSize( *pi_width, *pi_height );
661         }*/
662
663         videoIsActive = true;
664
665         emit askVideoToResize( *pi_width, *pi_height );
666         emit askUpdate();
667     }
668     return ret;
669 }
670
671 void MainInterface::releaseVideo( void *p_win )
672 {
673     emit askReleaseVideo( p_win );
674 }
675
676 void MainInterface::releaseVideoSlot( void *p_win )
677 {
678     videoWidget->release( p_win );
679     videoWidget->hide();
680
681     if( bgWidget )// WORONG
682         bgWidget->show();
683
684     videoIsActive = false;
685     emit askUpdate();
686 }
687
688 int MainInterface::controlVideo( void *p_window, int i_query, va_list args )
689 {
690     int i_ret = VLC_EGENERIC;
691     switch( i_query )
692     {
693         case VOUT_GET_SIZE:
694         {
695             unsigned int *pi_width  = va_arg( args, unsigned int * );
696             unsigned int *pi_height = va_arg( args, unsigned int * );
697             *pi_width = videoWidget->width();
698             *pi_height = videoWidget->height();
699             i_ret = VLC_SUCCESS;
700             break;
701         }
702         case VOUT_SET_SIZE:
703         {
704             unsigned int i_width  = va_arg( args, unsigned int );
705             unsigned int i_height = va_arg( args, unsigned int );
706             emit askVideoToResize( i_width, i_height );
707             emit askUpdate();
708             updateGeometry();
709             i_ret = VLC_SUCCESS;
710             break;
711         }
712         case VOUT_SET_STAY_ON_TOP:
713         {
714             int i_arg = va_arg( args, int );
715             QApplication::postEvent( this, new SetVideoOnTopQtEvent( i_arg ) );
716             i_ret = VLC_SUCCESS;
717             break;
718         }
719         default:
720             msg_Warn( p_intf, "unsupported control query" );
721             break;
722     }
723     return i_ret;
724 }
725
726 /*****************************************************************************
727  * Playlist, Visualisation and Menus handling
728  *****************************************************************************/
729 /**
730  * Toggle the playlist widget or dialog
731  **/
732 void MainInterface::togglePlaylist()
733 {
734     /* CREATION
735     If no playlist exist, then create one and attach it to the DockPL*/
736     if( !playlistWidget )
737     {
738         playlistWidget = new PlaylistWidget( p_intf, settings, dockPL );
739
740         /* Add it to the parent DockWidget */
741         dockPL->setWidget( playlistWidget );
742
743         /* Add the dock to the main Interface */
744         addDockWidget( Qt::BottomDockWidgetArea, dockPL );
745
746         /* Make the playlist floating is requested. Default is not. */
747         settings->beginGroup( "MainWindow" );
748         if( settings->value( "playlist-floats", 1 ).toInt() )
749         {
750             msg_Dbg( p_intf, "we don't want the playlist inside");
751             dockPL->setFloating( true );
752         }
753         settings->endGroup();
754         settings->beginGroup( "playlist" );
755         dockPL->move( settings->value( "pos", QPoint( 0,0 ) ).toPoint() );
756         QSize newSize = settings->value( "size", QSize( 400, 300 ) ).toSize();
757         if( newSize.isValid() )
758             dockPL->resize( newSize );
759         settings->endGroup();
760
761         dockPL->show();
762         playlistVisible = true;
763     }
764     else
765     {
766     /* toggle the visibility of the playlist */
767        TOGGLEV( dockPL );
768        resize( sizeHint() );
769        playlistVisible = !playlistVisible;
770     }
771 }
772
773 /* Function called from the menu to undock the playlist */
774 void MainInterface::undockPlaylist()
775 {
776     dockPL->setFloating( true );
777     resize( sizeHint() );
778 }
779
780 void MainInterface::toggleMinimalView()
781 {
782     TOGGLEV( menuBar() );
783     TOGGLEV( controls );
784     TOGGLEV( statusBar() );
785     doComponentsUpdate();
786 }
787
788 /* Video widget cannot do this synchronously as it runs in another thread */
789 /* Well, could it, actually ? Probably dangerous ... */
790 void MainInterface::doComponentsUpdate()
791 {
792     resize( sizeHint() );
793     msg_Dbg( p_intf, "Updating the geometry" );
794     updateGeometry();
795 }
796
797 /* toggling advanced controls buttons */
798 void MainInterface::toggleAdvanced()
799 {
800     controls->toggleAdvanced();
801 }
802
803 /* Get the visibility status of the controls (hidden or not, advanced or not) */
804 int MainInterface::getControlsVisibilityStatus()
805 {
806     return( (controls->isVisible() ? CONTROLS_VISIBLE : CONTROLS_HIDDEN )
807                 + CONTROLS_ADVANCED * controls->b_advancedVisible );
808 }
809
810 #if 0
811 void MainInterface::visual()
812 {
813     if( !VISIBLE( visualSelector) )
814     {
815         visualSelector->show();
816         if( !THEMIM->getIM()->hasVideo() )
817         {
818             /* Show the background widget */
819         }
820         visualSelectorEnabled = true;
821     }
822     else
823     {
824         /* Stop any currently running visualization */
825         visualSelector->hide();
826         visualSelectorEnabled = false;
827     }
828     doComponentsUpdate();
829 }
830 #endif
831
832 /************************************************************************
833  * Other stuff
834  ************************************************************************/
835 void MainInterface::setDisplayPosition( float pos, int time, int length )
836 {
837     char psz_length[MSTRTIME_MAX_SIZE], psz_time[MSTRTIME_MAX_SIZE];
838     secstotimestr( psz_length, length );
839     secstotimestr( psz_time, ( b_remainingTime && length ) ? length - time
840                                                            : time );
841
842     QString timestr;
843     timestr.sprintf( "%s/%s", psz_time,
844                             ( !length && time ) ? "--:--" : psz_length );
845
846     /* Add a minus to remaining time*/
847     if( b_remainingTime && length ) timeLabel->setText( " -"+timestr+" " );
848     else timeLabel->setText( " "+timestr+" " );
849 }
850
851 void MainInterface::toggleTimeDisplay()
852 {
853     b_remainingTime = !b_remainingTime;
854 }
855
856 void MainInterface::setName( QString name )
857 {
858     input_name = name; /* store it for the QSystray use */
859     /* Display it in the status bar, but also as a Tooltip in case it doesn't
860        fit in the label */
861     nameLabel->setText( " " + name + " " );
862     nameLabel->setToolTip( " " + name +" " );
863 }
864
865 void MainInterface::setStatus( int status )
866 {
867     /* Forward the status to the controls to toggle Play/Pause */
868     controls->setStatus( status );
869     /* And in the systray for the menu */
870     if( sysTray )
871         QVLCMenu::updateSystrayMenu( this, p_intf );
872 }
873
874 void MainInterface::setRate( int rate )
875 {
876     QString str;
877     str.setNum( ( 1000 / (double)rate ), 'f', 2 );
878     str.append( "x" );
879     speedLabel->setText( str );
880     speedControl->updateControls( rate );
881 }
882
883 void MainInterface::updateOnTimer()
884 {
885     if( intf_ShouldDie( p_intf ) )
886     {
887         QApplication::closeAllWindows();
888         QApplication::quit();
889     }
890 #if 0
891     if( need_components_update )
892     {
893         doComponentsUpdate();
894         need_components_update = false;
895     }
896 #endif
897 }
898
899 /*****************************************************************************
900  * Systray Icon and Systray Menu
901  *****************************************************************************/
902
903 /**
904  * Create a SystemTray icon and a menu that would go with it.
905  * Connects to a click handler on the icon.
906  **/
907 void MainInterface::createSystray()
908 {
909     QIcon iconVLC;
910     if( QDate::currentDate().dayOfYear() >= 354 )
911         iconVLC =  QIcon( QPixmap( ":/vlc128-christmas.png" ) );
912     else
913         iconVLC =  QIcon( QPixmap( ":/vlc128.png" ) );
914     sysTray = new QSystemTrayIcon( iconVLC, this );
915     sysTray->setToolTip( qtr( "VLC media player" ));
916
917     systrayMenu = new QMenu( qtr( "VLC media player" ), this );
918     systrayMenu->setIcon( iconVLC );
919
920     QVLCMenu::updateSystrayMenu( this, p_intf, true );
921     sysTray->show();
922
923     CONNECT( sysTray, activated( QSystemTrayIcon::ActivationReason ),
924             this, handleSystrayClick( QSystemTrayIcon::ActivationReason ) );
925 }
926
927 /**
928  * Updates the Systray Icon's menu and toggle the main interface
929  */
930 void MainInterface::toggleUpdateSystrayMenu()
931 {
932     /* If hidden, show it */
933     if( isHidden() )
934     {
935         show();
936         activateWindow();
937     }
938     else if( isMinimized() )
939     {
940         /* Minimized */
941         showNormal();
942         activateWindow();
943     }
944     else
945     {
946         /* Visible */
947 #ifdef WIN32
948         /* check if any visible window is above vlc in the z-order,
949          * but ignore the ones always on top */
950         WINDOWINFO wi;
951         HWND hwnd;
952         wi.cbSize = sizeof( WINDOWINFO );
953         for( hwnd = GetNextWindow( internalWinId(), GW_HWNDPREV );
954                 hwnd && !IsWindowVisible( hwnd );
955                 hwnd = GetNextWindow( hwnd, GW_HWNDPREV ) );
956         if( !hwnd || !GetWindowInfo( hwnd, &wi ) ||
957                 (wi.dwExStyle&WS_EX_TOPMOST) )
958 #else
959         if( isActiveWindow() )
960 #endif
961         {
962             hide();
963         }
964         else
965         {
966             activateWindow();
967         }
968     }
969     QVLCMenu::updateSystrayMenu( this, p_intf );
970 }
971
972 void MainInterface::handleSystrayClick(
973                                     QSystemTrayIcon::ActivationReason reason )
974 {
975     switch( reason )
976     {
977         case QSystemTrayIcon::Trigger:
978             toggleUpdateSystrayMenu();
979             break;
980         case QSystemTrayIcon::MiddleClick:
981             sysTray->showMessage( qtr( "VLC media player" ),
982                     qtr( "Control menu for the player" ),
983                     QSystemTrayIcon::Information, 3000 );
984             break;
985     }
986 }
987
988 /**
989  * Updates the name of the systray Icon tooltip.
990  * Doesn't check if the systray exists, check before you call it.
991  **/
992 void MainInterface::updateSystrayTooltipName( QString name )
993 {
994     if( name.isEmpty() )
995     {
996         sysTray->setToolTip( qtr( "VLC media player" ) );
997     }
998     else
999     {
1000         sysTray->setToolTip( name );
1001         if( notificationEnabled && ( isHidden() || isMinimized() ) )
1002         {
1003             sysTray->showMessage( qtr( "VLC media player" ), name,
1004                     QSystemTrayIcon::NoIcon, 3000 );
1005         }
1006     }
1007 }
1008
1009 /**
1010  * Updates the status of the systray Icon tooltip.
1011  * Doesn't check if the systray exists, check before you call it.
1012  **/
1013 void MainInterface::updateSystrayTooltipStatus( int i_status )
1014 {
1015     switch( i_status )
1016     {
1017         case  0:
1018         case  END_S:
1019             {
1020                 sysTray->setToolTip( qtr( "VLC media player" ) );
1021                 break;
1022             }
1023         case PLAYING_S:
1024             {
1025                 sysTray->setToolTip( input_name );
1026                 break;
1027             }
1028         case PAUSE_S:
1029             {
1030                 sysTray->setToolTip( input_name + " - "
1031                         + qtr( "Paused") );
1032                 break;
1033             }
1034     }
1035 }
1036
1037 /************************************************************************
1038  * D&D Events
1039  ************************************************************************/
1040 void MainInterface::dropEvent(QDropEvent *event)
1041 {
1042      const QMimeData *mimeData = event->mimeData();
1043
1044      /* D&D of a subtitles file, add it on the fly */
1045      if( mimeData->urls().size() == 1 )
1046      {
1047         if( THEMIM->getIM()->hasInput() )
1048         {
1049             if( input_AddSubtitles( THEMIM->getInput(),
1050                                     qtu( mimeData->urls()[0].toString() ),
1051                                     VLC_TRUE ) )
1052             {
1053                 event->acceptProposedAction();
1054                 return;
1055             }
1056         }
1057      }
1058      bool first = true;
1059      foreach( QUrl url, mimeData->urls() ) {
1060         QString s = url.toString();
1061         if( s.length() > 0 ) {
1062             playlist_Add( THEPL, qtu(s), NULL,
1063                           PLAYLIST_APPEND | (first ? PLAYLIST_GO:0),
1064                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
1065             first = false;
1066         }
1067      }
1068      event->acceptProposedAction();
1069 }
1070 void MainInterface::dragEnterEvent(QDragEnterEvent *event)
1071 {
1072      event->acceptProposedAction();
1073 }
1074 void MainInterface::dragMoveEvent(QDragMoveEvent *event)
1075 {
1076      event->acceptProposedAction();
1077 }
1078 void MainInterface::dragLeaveEvent(QDragLeaveEvent *event)
1079 {
1080      event->accept();
1081 }
1082
1083 /************************************************************************
1084  * Events stuff
1085  ************************************************************************/
1086 void MainInterface::customEvent( QEvent *event )
1087 {
1088 #if 0
1089     if( event->type() == PLDockEvent_Type )
1090     {
1091         PlaylistDialog::killInstance();
1092         playlistEmbeddedFlag = true;
1093         menuBar()->clear();
1094         QVLCMenu::createMenuBar(this, p_intf, true, visualSelectorEnabled);
1095         togglePlaylist();
1096     }
1097 #endif
1098     /*else */
1099     if ( event->type() == SetVideoOnTopEvent_Type )
1100     {
1101         SetVideoOnTopQtEvent* p_event = (SetVideoOnTopQtEvent*)event;
1102         if( p_event->OnTop() )
1103             setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
1104         else
1105             setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
1106         show(); /* necessary to apply window flags?? */
1107     }
1108 }
1109
1110 void MainInterface::keyPressEvent( QKeyEvent *e )
1111 {
1112     if( ( e->modifiers() &  Qt::ControlModifier ) && ( e->key() & Qt::Key_H )
1113           && menuBar()->isHidden() )
1114     {
1115         toggleMinimalView();
1116         e->accept();
1117     }
1118
1119     int i_vlck = qtEventToVLCKey( e );
1120     if( i_vlck > 0 )
1121     {
1122         var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck );
1123         e->accept();
1124     }
1125     else
1126         e->ignore();
1127 }
1128
1129 void MainInterface::wheelEvent( QWheelEvent *e )
1130 {
1131     int i_vlckey = qtWheelEventToVLCKey( e );
1132     var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlckey );
1133     e->accept();
1134 }
1135
1136 void MainInterface::closeEvent( QCloseEvent *e )
1137 {
1138     hide();
1139     THEDP->quit();
1140 }
1141
1142 void MainInterface::toggleFullScreen( void )
1143 {
1144     if( isFullScreen() )
1145         showNormal();
1146     else
1147         showFullScreen();
1148 }
1149
1150 /*****************************************************************************
1151  * Callbacks
1152  *****************************************************************************/
1153 static int InteractCallback( vlc_object_t *p_this,
1154                              const char *psz_var, vlc_value_t old_val,
1155                              vlc_value_t new_val, void *param )
1156 {
1157     intf_dialog_args_t *p_arg = new intf_dialog_args_t;
1158     p_arg->p_dialog = (interaction_dialog_t *)(new_val.p_address);
1159     DialogEvent *event = new DialogEvent( INTF_DIALOG_INTERACTION, 0, p_arg );
1160     QApplication::postEvent( THEDP, static_cast<QEvent*>(event) );
1161     return VLC_SUCCESS;
1162 }
1163
1164 /*****************************************************************************
1165  * PopupMenuCB: callback triggered by the intf-popupmenu playlist variable.
1166  *  We don't show the menu directly here because we don't want the
1167  *  caller to block for a too long time.
1168  *****************************************************************************/
1169 static int PopupMenuCB( vlc_object_t *p_this, const char *psz_variable,
1170                         vlc_value_t old_val, vlc_value_t new_val, void *param )
1171 {
1172     intf_thread_t *p_intf = (intf_thread_t *)param;
1173     msg_Dbg( p_this, "Menu Requested" ); // DEBUG to track the non disparition of the menu...
1174
1175     if( p_intf->pf_show_dialog )
1176     {
1177         p_intf->pf_show_dialog( p_intf, INTF_DIALOG_POPUPMENU,
1178                                 new_val.b_bool, 0 );
1179     }
1180
1181     return VLC_SUCCESS;
1182 }
1183
1184 /*****************************************************************************
1185  * IntfShowCB: callback triggered by the intf-show playlist variable.
1186  *****************************************************************************/
1187 static int IntfShowCB( vlc_object_t *p_this, const char *psz_variable,
1188                        vlc_value_t old_val, vlc_value_t new_val, void *param )
1189 {
1190     intf_thread_t *p_intf = (intf_thread_t *)param;
1191     msg_Dbg( p_this, "Intf Show Requested" ); // DEBUG to track the non disparition of the menu...
1192     //p_intf->p_sys->b_intf_show = VLC_TRUE;
1193
1194     return VLC_SUCCESS;
1195 }