1 /*****************************************************************************
2  * qt.cpp : Qt interface
3  ****************************************************************************
4  * Copyright © 2006-2009 the VideoLAN team
5  * $Id: eda27528978e4a357c515d49fd00824790ce97f4 $
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Jean-Baptiste Kempf <jb@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 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
30 
31 #include <QApplication>
32 #include <QDate>
33 #include <QMutex>
34 
35 #include "qt.hpp"
36 
37 #include "input_manager.hpp"    /* THEMIM destruction */
38 #include "dialogs_provider.hpp" /* THEDP creation */
39 #ifdef _WIN32
40 # include "main_interface_win32.hpp"
41 #else
42 # include "main_interface.hpp"   /* MainInterface creation */
43 #endif
44 #include "extensions_manager.hpp" /* Extensions manager */
45 #include "managers/addons_manager.hpp" /* Addons manager */
46 #include "dialogs/help.hpp"     /* Launch Update */
47 #include "recents.hpp"          /* Recents Item destruction */
48 #include "util/qvlcapp.hpp"     /* QVLCApplication definition */
49 #include "components/playlist/playlist_model.hpp" /* for ~PLModel() */
50 
51 #include <vlc_plugin.h>
52 #include <vlc_vout_window.h>
53 
54 #ifdef _WIN32 /* For static builds */
55  #include <QtPlugin>
56 
57  #ifdef QT_STATICPLUGIN
58   Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
59   Q_IMPORT_PLUGIN(QSvgIconPlugin)
60   Q_IMPORT_PLUGIN(QSvgPlugin)
61   #if !HAS_QT56
62    Q_IMPORT_PLUGIN(AccessibleFactory)
63   #endif
64  #endif
65 #endif
66 
67 /*****************************************************************************
68  * Local prototypes.
69  *****************************************************************************/
70 static int  OpenIntf     ( vlc_object_t * );
71 static int  OpenDialogs  ( vlc_object_t * );
72 static int  Open         ( vlc_object_t *, bool );
73 static void Close        ( vlc_object_t * );
74 static int  WindowOpen   ( vout_window_t *, const vout_window_cfg_t * );
75 static void WindowClose  ( vout_window_t * );
76 static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );
77 
78 /*****************************************************************************
79  * Module descriptor
80  *****************************************************************************/
81 #define ADVANCED_PREFS_TEXT N_( "Show advanced preferences over simple ones" )
82 #define ADVANCED_PREFS_LONGTEXT N_( "Show advanced preferences and not simple "\
83                                     "preferences when opening the preferences "\
84                                     "dialog." )
85 
86 #define SYSTRAY_TEXT N_( "Systray icon" )
87 #define SYSTRAY_LONGTEXT N_( "Show an icon in the systray " \
88                              "allowing you to control VLC media player " \
89                              "for basic actions." )
90 
91 #define MINIMIZED_TEXT N_( "Start VLC with only a systray icon" )
92 #define MINIMIZED_LONGTEXT N_( "VLC will start with just an icon in " \
93                                "your taskbar." )
94 
95 #define KEEPSIZE_TEXT N_( "Resize interface to the native video size" )
96 #define KEEPSIZE_LONGTEXT N_( "You have two choices:\n" \
97             " - The interface will resize to the native video size\n" \
98             " - The video will fit to the interface size\n " \
99             "By default, interface resize to the native video size." )
100 
101 #define TITLE_TEXT N_( "Show playing item name in window title" )
102 #define TITLE_LONGTEXT N_( "Show the name of the song or video in the " \
103                            "controller window title." )
104 
105 #define NOTIFICATION_TEXT N_( "Show notification popup on track change" )
106 #define NOTIFICATION_LONGTEXT N_( \
107     "Show a notification popup with the artist and track name when " \
108     "the current playlist item changes, when VLC is minimized or hidden." )
109 
110 #define OPACITY_TEXT N_( "Windows opacity between 0.1 and 1" )
111 #define OPACITY_LONGTEXT N_( "Sets the windows opacity between 0.1 and 1 " \
112                              "for main interface, playlist and extended panel."\
113                              " This option only works with Windows and " \
114                              "X11 with composite extensions." )
115 
116 #define OPACITY_FS_TEXT N_( "Fullscreen controller opacity between 0.1 and 1" )
117 #define OPACITY_FS_LONGTEXT N_( "Sets the fullscreen controller opacity between 0.1 and 1 " \
118                              "for main interface, playlist and extended panel."\
119                              " This option only works with Windows and " \
120                              "X11 with composite extensions." )
121 
122 #define ERROR_TEXT N_( "Show unimportant error and warnings dialogs" )
123 
124 #define UPDATER_TEXT N_( "Activate the updates availability notification" )
125 #define UPDATER_LONGTEXT N_( "Activate the automatic notification of new " \
126                             "versions of the software. It runs once every " \
127                             "two weeks." )
128 #define UPDATER_DAYS_TEXT N_("Number of days between two update checks")
129 
130 #define PRIVACY_TEXT N_( "Ask for network policy at start" )
131 
132 #define RECENTPLAY_TEXT N_( "Save the recently played items in the menu" )
133 
134 #define RECENTPLAY_FILTER_TEXT N_( "List of words separated by | to filter" )
135 #define RECENTPLAY_FILTER_LONGTEXT N_( "Regular expression used to filter " \
136         "the recent items played in the player." )
137 
138 #define SLIDERCOL_TEXT N_( "Define the colors of the volume slider" )
139 #define SLIDERCOL_LONGTEXT N_( "Define the colors of the volume slider\n" \
140                        "By specifying the 12 numbers separated by a ';'\n" \
141             "Default is '255;255;255;20;226;20;255;176;15;235;30;20'\n" \
142             "An alternative can be '30;30;50;40;40;100;50;50;160;150;150;255'")
143 
144 #define QT_MODE_TEXT N_( "Selection of the starting mode and look" )
145 #define QT_MODE_LONGTEXT N_( "Start VLC with:\n" \
146                              " - normal mode\n"  \
147                              " - a zone always present to show information " \
148                                   "as lyrics, album arts...\n" \
149                              " - minimal mode with limited controls" )
150 
151 #define QT_FULLSCREEN_TEXT N_( "Show a controller in fullscreen mode" )
152 #define QT_NATIVEOPEN_TEXT N_( "Embed the file browser in open dialog" )
153 
154 #define FULLSCREEN_NUMBER_TEXT N_( "Define which screen fullscreen goes" )
155 #define FULLSCREEN_NUMBER_LONGTEXT N_( "Screennumber of fullscreen, instead of " \
156                                        "same screen where interface is." )
157 
158 #define QT_AUTOLOAD_EXTENSIONS_TEXT N_( "Load extensions on startup" )
159 #define QT_AUTOLOAD_EXTENSIONS_LONGTEXT N_( "Automatically load the "\
160                                             "extensions module on startup." )
161 
162 #define QT_MINIMAL_MODE_TEXT N_("Start in minimal view (without menus)" )
163 
164 #define QT_BGCONE_TEXT N_( "Display background cone or art" )
165 #define QT_BGCONE_LONGTEXT N_( "Display background cone or current album art " \
166                             "when not playing. " \
167                             "Can be disabled to prevent burning screen." )
168 #define QT_BGCONE_EXPANDS_TEXT N_( "Expanding background cone or art" )
169 #define QT_BGCONE_EXPANDS_LONGTEXT N_( "Background art fits window's size." )
170 
171 #define QT_DISABLE_VOLUME_KEYS_TEXT N_( "Ignore keyboard volume buttons." )
172 #define QT_DISABLE_VOLUME_KEYS_LONGTEXT N_(                                             \
173     "With this option checked, the volume up, volume down and mute buttons on your "    \
174     "keyboard will always change your system volume. With this option unchecked, the "  \
175     "volume buttons will change VLC's volume when VLC is selected and change the "      \
176     "system volume when VLC is not selected." )
177 
178 #define QT_PAUSE_MINIMIZED_TEXT N_( "Pause the video playback when minimized" )
179 #define QT_PAUSE_MINIMIZED_LONGTEXT N_( \
180     "With this option enabled, the playback will be automatically paused when minimizing the window." )
181 
182 #define ICONCHANGE_TEXT N_( "Allow automatic icon changes")
183 #define ICONCHANGE_LONGTEXT N_( \
184     "This option allows the interface to change its icon on various occasions.")
185 
186 #define VOLUME_MAX_TEXT N_( "Maximum Volume displayed" )
187 
188 #define AUTORAISE_ON_PLAYBACK_TEXT N_( "When to raise the interface" )
189 #define AUTORAISE_ON_PLAYBACK_LONGTEXT N_( "This option allows the interface to be raised automatically " \
190     "when a video/audio playback starts, or never." )
191 
192 #define FULLSCREEN_CONTROL_PIXELS N_( "Fullscreen controller mouse sensitivity" )
193 
194 #define CONTINUE_PLAYBACK_TEXT N_("Continue playback?")
195 
196 static const int i_notification_list[] =
197     { NOTIFICATION_NEVER, NOTIFICATION_MINIMIZED, NOTIFICATION_ALWAYS };
198 
199 static const char *const psz_notification_list_text[] =
200     { N_("Never"), N_("When minimized"), N_("Always") };
201 
202 static const int i_continue_list[] =
203     { 0, 1, 2 };
204 
205 static const char *const psz_continue_list_text[] =
206     { N_("Never"), N_("Ask"), N_("Always") };
207 
208 static const int i_raise_list[] =
209     { MainInterface::RAISE_NEVER, MainInterface::RAISE_VIDEO, \
210       MainInterface::RAISE_AUDIO, MainInterface::RAISE_AUDIOVIDEO,  };
211 
212 static const char *const psz_raise_list_text[] =
213     { N_( "Never" ), N_( "Video" ), N_( "Audio" ), _( "Audio/Video" ) };
214 
215 /**********************************************************************/
216 vlc_module_begin ()
217     set_shortname( "Qt" )
218     set_description( N_("Qt interface") )
219     set_category( CAT_INTERFACE )
220     set_subcategory( SUBCAT_INTERFACE_MAIN )
221     set_capability( "interface", 151 )
222     set_callbacks( OpenIntf, Close )
223 
224     add_shortcut("qt")
225 
226     add_bool( "qt-minimal-view", false, QT_MINIMAL_MODE_TEXT,
227               QT_MINIMAL_MODE_TEXT, false );
228 
229     add_bool( "qt-system-tray", true, SYSTRAY_TEXT, SYSTRAY_LONGTEXT, false)
230 
231     add_integer( "qt-notification", NOTIFICATION_MINIMIZED,
232                  NOTIFICATION_TEXT,
233                  NOTIFICATION_LONGTEXT, false )
234             change_integer_list( i_notification_list, psz_notification_list_text )
235 
236     add_bool( "qt-start-minimized", false, MINIMIZED_TEXT,
237               MINIMIZED_LONGTEXT, true)
238     add_bool( "qt-pause-minimized", false, QT_PAUSE_MINIMIZED_TEXT,
239               QT_PAUSE_MINIMIZED_LONGTEXT, false )
240 
241     add_float_with_range( "qt-opacity", 1., 0.1, 1., OPACITY_TEXT,
242                           OPACITY_LONGTEXT, false )
243     add_float_with_range( "qt-fs-opacity", 0.8, 0.1, 1., OPACITY_FS_TEXT,
244                           OPACITY_FS_LONGTEXT, false )
245 
246     add_bool( "qt-video-autoresize", true, KEEPSIZE_TEXT,
247               KEEPSIZE_LONGTEXT, false )
248     add_bool( "qt-name-in-title", true, TITLE_TEXT,
249               TITLE_LONGTEXT, false )
250     add_bool( "qt-fs-controller", true, QT_FULLSCREEN_TEXT,
251               QT_FULLSCREEN_TEXT, false )
252 
253     add_bool( "qt-recentplay", true, RECENTPLAY_TEXT,
254               RECENTPLAY_TEXT, false )
255     add_string( "qt-recentplay-filter", "",
256                 RECENTPLAY_FILTER_TEXT, RECENTPLAY_FILTER_LONGTEXT, false )
257     add_integer( "qt-continue", 1, CONTINUE_PLAYBACK_TEXT, CONTINUE_PLAYBACK_TEXT, false )
258             change_integer_list(i_continue_list, psz_continue_list_text )
259 
260 #ifdef UPDATE_CHECK
261     add_bool( "qt-updates-notif", true, UPDATER_TEXT,
262               UPDATER_LONGTEXT, false )
263     add_integer_with_range( "qt-updates-days", 3, 0, 180,
264               UPDATER_DAYS_TEXT, UPDATER_DAYS_TEXT, false )
265 #endif
266 
267 #ifdef _WIN32
268     add_bool( "qt-disable-volume-keys"             /* name */,
269               true                                 /* default value */,
270               QT_DISABLE_VOLUME_KEYS_TEXT          /* text */,
271               QT_DISABLE_VOLUME_KEYS_LONGTEXT      /* longtext */,
272               false                                /* advanced mode only */)
273 #endif
274 
275     add_bool( "qt-embedded-open", false, QT_NATIVEOPEN_TEXT,
276                QT_NATIVEOPEN_TEXT, false )
277 
278 
279     add_bool( "qt-advanced-pref", false, ADVANCED_PREFS_TEXT,
280               ADVANCED_PREFS_LONGTEXT, false )
281     add_bool( "qt-error-dialogs", true, ERROR_TEXT,
282               ERROR_TEXT, false )
283 
284     add_string( "qt-slider-colours", "153;210;153;20;210;20;255;199;15;245;39;29",
285                 SLIDERCOL_TEXT, SLIDERCOL_LONGTEXT, false )
286 
287     add_bool( "qt-privacy-ask", true, PRIVACY_TEXT, PRIVACY_TEXT,
288               false )
289         change_private ()
290 
291     add_integer( "qt-fullscreen-screennumber", -1, FULLSCREEN_NUMBER_TEXT,
292                FULLSCREEN_NUMBER_LONGTEXT, false );
293 
294     add_bool( "qt-autoload-extensions", true,
295               QT_AUTOLOAD_EXTENSIONS_TEXT, QT_AUTOLOAD_EXTENSIONS_LONGTEXT,
296               false )
297 
298     add_bool( "qt-bgcone", true, QT_BGCONE_TEXT, QT_BGCONE_LONGTEXT, true )
299     add_bool( "qt-bgcone-expands", false, QT_BGCONE_EXPANDS_TEXT,
300               QT_BGCONE_EXPANDS_LONGTEXT, true )
301 
302     add_bool( "qt-icon-change", true, ICONCHANGE_TEXT, ICONCHANGE_LONGTEXT, true )
303 
304     add_integer_with_range( "qt-max-volume", 125, 60, 300, VOLUME_MAX_TEXT, VOLUME_MAX_TEXT, true)
305 
306     add_integer_with_range( "qt-fs-sensitivity", 3, 0, 4000, FULLSCREEN_CONTROL_PIXELS,
307             FULLSCREEN_CONTROL_PIXELS, true)
308 
309     add_obsolete_bool( "qt-blingbling" )      /* Suppressed since 1.0.0 */
310     add_obsolete_integer( "qt-display-mode" ) /* Suppressed since 1.1.0 */
311 
312     add_obsolete_bool( "qt-adv-options" )     /* Since 2.0.0 */
313     add_obsolete_bool( "qt-volume-complete" ) /* Since 2.0.0 */
314     add_obsolete_integer( "qt-startvolume" )  /* Since 2.0.0 */
315 
316     add_integer( "qt-auto-raise", MainInterface::RAISE_VIDEO, AUTORAISE_ON_PLAYBACK_TEXT,
317                  AUTORAISE_ON_PLAYBACK_LONGTEXT, false )
318             change_integer_list( i_raise_list, psz_raise_list_text )
319 
320     cannot_unload_broken_library()
321 
322     add_submodule ()
323         set_description( "Dialogs provider" )
324         set_capability( "dialogs provider", 51 )
325 
326         set_callbacks( OpenDialogs, Close )
327 
328     add_submodule ()
329         set_capability( "vout window", 0 )
330         set_callbacks( WindowOpen, WindowClose )
331 
332 vlc_module_end ()
333 
334 /*****************************************/
335 
336 /* Ugly, but the Qt interface assumes single instance anyway */
337 static vlc_sem_t ready;
338 static QMutex lock;
339 static bool busy = false;
340 static bool active = false;
341 
342 /*****************************************************************************
343  * Module callbacks
344  *****************************************************************************/
345 
346 static void *ThreadPlatform( void *, char * );
347 
Thread(void * data)348 static void *Thread( void *data )
349 {
350     return ThreadPlatform( data, NULL );
351 }
352 
353 #ifdef Q_OS_MAC
354 /* Used to abort the app.exec() on OSX after libvlc_Quit is called */
355 #include "../../../lib/libvlc_internal.h" /* libvlc_SetExitHandler */
Abort(void * obj)356 static void Abort( void *obj )
357 {
358     QVLCApp::triggerQuit();
359 }
360 #endif
361 
362 #if defined (QT5_HAS_X11)
363 # include <vlc_xlib.h>
364 # include <QX11Info>
365 
ThreadXCB(void * data)366 static void *ThreadXCB( void *data )
367 {
368     char platform_name[] = "xcb";
369     return ThreadPlatform( data, platform_name );
370 }
371 
HasX11(vlc_object_t * obj)372 static bool HasX11( vlc_object_t *obj )
373 {
374     if( !vlc_xlib_init( obj ) )
375         return false;
376 
377     Display *dpy = XOpenDisplay( NULL );
378     if( dpy == NULL )
379         return false;
380 
381     XCloseDisplay( dpy );
382     return true;
383 }
384 #endif
385 
386 #ifdef QT5_HAS_WAYLAND
387 # include <wayland-client.h>
388 
ThreadWayland(void * data)389 static void *ThreadWayland( void *data )
390 {
391     char platform_name[] = "wayland";
392     return ThreadPlatform( data, platform_name );
393 }
394 
HasWayland(void)395 static bool HasWayland( void )
396 {
397     struct wl_display *dpy = wl_display_connect( NULL );
398     if( dpy == NULL )
399         return false;
400 
401     wl_display_disconnect( dpy );
402     return true;
403 }
404 #endif
405 
406 /* Open Interface */
Open(vlc_object_t * p_this,bool isDialogProvider)407 static int Open( vlc_object_t *p_this, bool isDialogProvider )
408 {
409     intf_thread_t *p_intf = (intf_thread_t *)p_this;
410     void *(*thread)(void *) = Thread;
411 
412 #ifdef QT5_HAS_X11
413     if( HasX11( p_this ) )
414         thread = ThreadXCB;
415     else
416 #endif
417 #ifdef QT5_HAS_WAYLAND
418     if( HasWayland() )
419         thread = ThreadWayland;
420     else
421 #endif
422 #if defined (QT5_HAS_X11) || defined (QT5_HAS_WAYLAND)
423         return VLC_EGENERIC;
424 #endif
425 
426     QMutexLocker locker (&lock);
427     if (busy)
428     {
429         msg_Err (p_this, "cannot start Qt multiple times");
430         return VLC_EGENERIC;
431     }
432 
433     /* Allocations of p_sys */
434     intf_sys_t *p_sys = p_intf->p_sys = new intf_sys_t;
435     p_sys->b_isDialogProvider = isDialogProvider;
436     p_sys->p_mi = NULL;
437     p_sys->pl_model = NULL;
438 
439     /* set up the playlist to work on */
440     if( isDialogProvider )
441         p_sys->p_playlist = pl_Get( (intf_thread_t *)p_intf->obj.parent );
442     else
443         p_sys->p_playlist = pl_Get( p_intf );
444 
445     /* */
446     vlc_sem_init (&ready, 0);
447 #ifdef Q_OS_MAC
448     /* Run mainloop on the main thread as Cocoa requires */
449     libvlc_SetExitHandler( p_intf->obj.libvlc, Abort, p_intf );
450     thread( (void *)p_intf );
451 #else
452     if( vlc_clone( &p_sys->thread, thread, p_intf, VLC_THREAD_PRIORITY_LOW ) )
453     {
454         delete p_sys;
455         return VLC_ENOMEM;
456     }
457 #endif
458 
459     /* Wait for the interface to be ready. This prevents the main
460      * LibVLC thread from starting video playback before we can create
461      * an embedded video window. */
462     vlc_sem_wait (&ready);
463     vlc_sem_destroy (&ready);
464     busy = active = true;
465 
466     return VLC_SUCCESS;
467 }
468 
469 /* Open Qt interface */
OpenIntf(vlc_object_t * p_this)470 static int OpenIntf( vlc_object_t *p_this )
471 {
472     return Open( p_this, false );
473 }
474 
475 /* Open Dialog Provider */
OpenDialogs(vlc_object_t * p_this)476 static int OpenDialogs( vlc_object_t *p_this )
477 {
478     return Open( p_this, true );
479 }
480 
Close(vlc_object_t * p_this)481 static void Close( vlc_object_t *p_this )
482 {
483     intf_thread_t *p_intf = (intf_thread_t *)p_this;
484     intf_sys_t *p_sys = p_intf->p_sys;
485 
486     if( !p_sys->b_isDialogProvider )
487     {
488         playlist_t *pl = THEPL;
489 
490         playlist_Deactivate (pl); /* release window provider if needed */
491     }
492 
493     /* And quit */
494     msg_Dbg( p_this, "requesting exit..." );
495     QVLCApp::triggerQuit();
496 
497     msg_Dbg( p_this, "waiting for UI thread..." );
498 #ifndef Q_OS_MAC
499     vlc_join (p_sys->thread, NULL);
500 #endif
501     delete p_sys;
502 
503     QMutexLocker locker (&lock);
504     assert (busy);
505     busy = false;
506 }
507 
ThreadPlatform(void * obj,char * platform_name)508 static void *ThreadPlatform( void *obj, char *platform_name )
509 {
510     intf_thread_t *p_intf = (intf_thread_t *)obj;
511     intf_sys_t *p_sys = p_intf->p_sys;
512     char vlc_name[] = "vlc"; /* for WM_CLASS */
513     char platform_parm[] = "-platform";
514     char *argv[4];
515     int argc = 0;
516 
517     argv[argc++] = vlc_name;
518     if( platform_name != NULL )
519     {
520         argv[argc++] = platform_parm;
521         argv[argc++] = platform_name;
522     }
523     argv[argc] = NULL;
524 
525     Q_INIT_RESOURCE( vlc );
526 
527 #if HAS_QT56
528     QApplication::setAttribute( Qt::AA_EnableHighDpiScaling );
529     QApplication::setAttribute( Qt::AA_UseHighDpiPixmaps );
530 #endif
531 
532     /* Start the QApplication here */
533     QVLCApp app( argc, argv );
534 
535     /* Set application direction to locale direction,
536      * necessary for  RTL locales */
537     app.setLayoutDirection(QLocale().textDirection());
538 
539     p_sys->p_app = &app;
540 
541 
542     /* All the settings are in the .conf/.ini style */
543 #ifdef _WIN32
544     char *cConfigDir = config_GetUserDir( VLC_CONFIG_DIR );
545     QString configDir = cConfigDir;
546     free( cConfigDir );
547     if( configDir.endsWith( "\\vlc" ) )
548         configDir.chop( 4 ); /* the "\vlc" dir is added again by QSettings */
549     QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, configDir );
550 #endif
551 
552     p_sys->mainSettings = new QSettings(
553 #ifdef _WIN32
554             QSettings::IniFormat,
555 #else
556             QSettings::NativeFormat,
557 #endif
558             QSettings::UserScope, "vlc", "vlc-qt-interface" );
559 
560     if( QDate::currentDate().dayOfYear() >= QT_XMAS_JOKE_DAY && var_InheritBool( p_intf, "qt-icon-change" ) )
561         app.setWindowIcon( QIcon::fromTheme( "vlc-xmas", QIcon( ":/logo/vlc128-xmas.png" ) ) );
562     else
563         app.setWindowIcon( QIcon::fromTheme( "vlc", QIcon( ":/logo/vlc256.png" ) ) );
564 
565     /* Initialize the Dialog Provider and the Main Input Manager */
566     DialogsProvider::getInstance( p_intf );
567     MainInputManager* mim = MainInputManager::getInstance( p_intf );
568     mim->probeCurrentInput();
569 
570 #ifdef UPDATE_CHECK
571     /* Checking for VLC updates */
572     if( var_InheritBool( p_intf, "qt-updates-notif" ) &&
573         !var_InheritBool( p_intf, "qt-privacy-ask" ) )
574     {
575         int interval = var_InheritInteger( p_intf, "qt-updates-days" );
576         if( QDate::currentDate() >
577              getSettings()->value( "updatedate" ).toDate().addDays( interval ) )
578         {
579             /* The constructor of the update Dialog will do the 1st request */
580             UpdateDialog::getInstance( p_intf );
581             getSettings()->setValue( "updatedate", QDate::currentDate() );
582         }
583     }
584 #endif
585 
586     /* Create the normal interface in non-DP mode */
587     MainInterface *p_mi = NULL;
588 
589     if( !p_sys->b_isDialogProvider )
590     {
591 #ifdef _WIN32
592         p_mi = new MainInterfaceWin32( p_intf );
593 #else
594         p_mi = new MainInterface( p_intf );
595 #endif
596         p_sys->p_mi = p_mi;
597 
598         /* Check window type from the Qt platform back-end */
599         p_sys->voutWindowType = VOUT_WINDOW_TYPE_INVALID;
600         QString platform = app.platformName();
601         if( platform == qfu("xcb") )
602             p_sys->voutWindowType = VOUT_WINDOW_TYPE_XID;
603         else if( platform == qfu("wayland") )
604             p_sys->voutWindowType = VOUT_WINDOW_TYPE_WAYLAND;
605         else if( platform == qfu("windows") )
606             p_sys->voutWindowType = VOUT_WINDOW_TYPE_HWND;
607         else if( platform == qfu("cocoa" ) )
608             p_sys->voutWindowType = VOUT_WINDOW_TYPE_NSOBJECT;
609         else
610             msg_Err( p_intf, "unknown Qt platform: %s", qtu(platform) );
611 
612         var_Create( THEPL, "qt4-iface", VLC_VAR_ADDRESS );
613         var_SetAddress( THEPL, "qt4-iface", p_intf );
614         var_Create( THEPL, "window", VLC_VAR_STRING );
615         if( p_sys->voutWindowType != VOUT_WINDOW_TYPE_INVALID )
616             var_SetString( THEPL, "window", "qt,any" );
617     }
618 
619     /* Explain how to show a dialog :D */
620     p_intf->pf_show_dialog = ShowDialog;
621 
622     /* Tell the main LibVLC thread we are ready */
623     vlc_sem_post (&ready);
624 
625 #ifdef Q_OS_MAC
626     /* We took over main thread, register and start here */
627     if( !p_sys->b_isDialogProvider )
628         playlist_Play( THEPL );
629 #endif
630 
631     /* Last settings */
632     app.setQuitOnLastWindowClosed( false );
633 
634     /* Retrieve last known path used in file browsing */
635     p_sys->filepath =
636          getSettings()->value( "filedialog-path", QVLCUserDir( VLC_HOME_DIR ) ).toString();
637 
638     /* Loads and tries to apply the preferred QStyle */
639     QString s_style = getSettings()->value( "MainWindow/QtStyle", "" ).toString();
640     if( s_style.compare("") != 0 )
641         QApplication::setStyle( s_style );
642 
643     /* Launch */
644     app.exec();
645 
646     msg_Dbg( p_intf, "QApp exec() finished" );
647     if (p_mi != NULL)
648     {
649         var_Destroy( THEPL, "window" );
650         var_Destroy( THEPL, "qt4-iface" );
651 
652         QMutexLocker locker (&lock);
653         active = false;
654 
655         p_sys->p_mi = NULL;
656         /* Destroy first the main interface because it is connected to some
657            slots in the MainInputManager */
658         delete p_mi;
659     }
660 
661     /* */
662     ExtensionsManager::killInstance();
663     AddonsManager::killInstance();
664 
665     /* Destroy all remaining windows,
666        because some are connected to some slots
667        in the MainInputManager
668        Settings must be destroyed after that.
669      */
670     DialogsProvider::killInstance();
671 
672     /* Delete the recentsMRL object before the configuration */
673     RecentsMRL::killInstance();
674 
675     /* Save the path or delete if recent play are disabled */
676     if( var_InheritBool( p_intf, "qt-recentplay" ) )
677         getSettings()->setValue( "filedialog-path", p_sys->filepath );
678     else
679         getSettings()->remove( "filedialog-path" );
680 
681     /* */
682     delete p_sys->pl_model;
683 
684     /* Destroy the MainInputManager */
685     MainInputManager::killInstance();
686 
687     /* Delete the configuration. Application has to be deleted after that. */
688     delete p_sys->mainSettings;
689 
690     /* Delete the application automatically */
691     return NULL;
692 }
693 
694 /*****************************************************************************
695  * Callback to show a dialog
696  *****************************************************************************/
ShowDialog(intf_thread_t * p_intf,int i_dialog_event,int i_arg,intf_dialog_args_t * p_arg)697 static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
698                         intf_dialog_args_t *p_arg )
699 {
700     VLC_UNUSED( p_intf );
701     DialogEvent *event = new DialogEvent( i_dialog_event, i_arg, p_arg );
702     QApplication::postEvent( THEDP, event );
703 }
704 
705 /**
706  * Video output window provider
707  *
708  * TODO move it out of here ?
709  */
710 static int WindowControl( vout_window_t *, int i_query, va_list );
711 
712 typedef struct {
713     MainInterface *mi;
714 #ifdef QT5_HAS_X11
715     Display *dpy;
716 #endif
717     bool orphaned;
718     QMutex lock;
719 } vout_window_qt_t;
720 
WindowOpen(vout_window_t * p_wnd,const vout_window_cfg_t * cfg)721 static int WindowOpen( vout_window_t *p_wnd, const vout_window_cfg_t *cfg )
722 {
723     if( cfg->is_standalone )
724         return VLC_EGENERIC;
725 
726     intf_thread_t *p_intf =
727         (intf_thread_t *)var_InheritAddress( p_wnd, "qt4-iface" );
728     if( !p_intf )
729     {   /* If another interface is used, this plugin cannot work */
730         msg_Dbg( p_wnd, "Qt interface not found" );
731         return VLC_EGENERIC;
732     }
733 
734     if( cfg->type != VOUT_WINDOW_TYPE_INVALID
735      && cfg->type != p_intf->p_sys->voutWindowType )
736         return VLC_EGENERIC;
737 
738     switch( p_intf->p_sys->voutWindowType )
739     {
740         case VOUT_WINDOW_TYPE_XID:
741             if( var_InheritBool( p_wnd, "video-wallpaper" ) )
742                 return VLC_EGENERIC;
743             break;
744     }
745 
746     QMutexLocker locker (&lock);
747     if (unlikely(!active))
748         return VLC_EGENERIC;
749 
750     vout_window_qt_t *sys = new vout_window_qt_t;
751 
752     sys->mi = p_intf->p_sys->p_mi;
753     sys->orphaned = false;
754     p_wnd->sys = (vout_window_sys_t *)sys;
755     msg_Dbg( p_wnd, "requesting video window..." );
756 
757 #ifdef QT5_HAS_X11
758     Window xid;
759 
760     if (QX11Info::isPlatformX11())
761     {
762         sys->dpy = XOpenDisplay(NULL);
763         if (unlikely(sys->dpy == NULL))
764         {
765             delete sys;
766             return VLC_EGENERIC;
767         }
768 
769         int snum = DefaultScreen(sys->dpy);
770         unsigned long black = BlackPixel(sys->dpy, snum);
771 
772         xid = XCreateSimpleWindow(sys->dpy, RootWindow(sys->dpy, snum),
773                                   0, 0, cfg->width, cfg->height,
774                                   0, black, black);
775     }
776 #endif
777 
778     if (!sys->mi->getVideo(p_wnd, cfg->width, cfg->height, cfg->is_fullscreen))
779     {
780 #ifdef QT5_HAS_X11
781         if (QX11Info::isPlatformX11())
782             XCloseDisplay(sys->dpy);
783 #endif
784         delete sys;
785         return VLC_EGENERIC;
786     }
787 
788 #ifdef QT5_HAS_X11
789     if (QX11Info::isPlatformX11())
790     {
791         QMutexLocker locker2(&sys->lock);
792 
793         XReparentWindow(sys->dpy, xid, p_wnd->handle.xid, 0, 0);
794         XMapWindow(sys->dpy, xid);
795         XSync(sys->dpy, True);
796         p_wnd->handle.xid = xid;
797     }
798 #endif
799     p_wnd->info.has_double_click = true;
800     p_wnd->control = WindowControl;
801     return VLC_SUCCESS;
802 }
803 
WindowResized(vout_window_t * wnd,const QSize & size)804 void WindowResized(vout_window_t *wnd, const QSize& size)
805 {
806 #ifdef QT5_HAS_X11
807     vout_window_qt_t *sys = (vout_window_qt_t *)wnd->sys;
808 
809     if (QX11Info::isPlatformX11())
810     {
811         XResizeWindow(sys->dpy, wnd->handle.xid, size.width(), size.height());
812         XClearWindow(sys->dpy, wnd->handle.xid);
813         XSync(sys->dpy, True);
814     }
815 #endif
816     vout_window_ReportSize(wnd, size.width(), size.height());
817 }
818 
WindowControl(vout_window_t * p_wnd,int i_query,va_list args)819 static int WindowControl( vout_window_t *p_wnd, int i_query, va_list args )
820 {
821     vout_window_qt_t *sys = (vout_window_qt_t *)p_wnd->sys;
822     QMutexLocker locker (&lock);
823 
824     if (unlikely(!active))
825     {
826         msg_Warn (p_wnd, "video already released before control");
827         return VLC_EGENERIC;
828     }
829     return sys->mi->controlVideo(i_query, args);
830 }
831 
WindowReleased(vout_window_t * wnd)832 void WindowReleased(vout_window_t *wnd)
833 {
834     vout_window_qt_t *sys = (vout_window_qt_t *)wnd->sys;
835     QMutexLocker locker(&sys->lock);
836 
837     msg_Warn(wnd, "orphaned video window");
838     sys->orphaned = true;
839 #if defined (QT5_HAS_X11)
840     if (QX11Info::isPlatformX11())
841     {   /* In the unlikely event that WindowOpen() has not yet reparented the
842          * window, WindowOpen() will skip reparenting. Then this call will be
843          * a no-op.
844          */
845         XReparentWindow(sys->dpy, wnd->handle.xid,
846                         RootWindow(sys->dpy, DefaultScreen(sys->dpy)), 0, 0);
847         XSync(sys->dpy, True);
848     }
849 #endif
850 }
851 
WindowClose(vout_window_t * p_wnd)852 static void WindowClose( vout_window_t *p_wnd )
853 {
854     vout_window_qt_t *sys = (vout_window_qt_t *)p_wnd->sys;
855     QMutexLocker locker (&lock);
856 
857     /* Normally, the interface terminates after the video. In the contrary, the
858      * Qt main loop is gone, so we cannot send any event to the user interface
859      * widgets. Ideally, we would keep the Qt main loop running until after
860      * the video window is released. But it is far simpler to just have the Qt
861      * thread destroy the window early, and to turn this function into a stub.
862      *
863      * That assumes the video output will behave sanely if it window is
864      * destroyed asynchronously.
865      * XCB and Xlib-XCB are fine with that. Plain Xlib wouldn't, */
866     if (likely(active))
867     {
868         msg_Dbg(p_wnd, "releasing video...");
869         sys->mi->releaseVideo();
870     }
871     else
872         msg_Warn (p_wnd, "video already released");
873 
874 #if defined (QT5_HAS_X11)
875     if (QX11Info::isPlatformX11())
876         XCloseDisplay(sys->dpy);
877 #endif
878     delete sys;
879 }
880