qt.cpp 29.4 KB
Newer Older
zorglub's avatar
zorglub committed
1
/*****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
2
 * qt.cpp : Qt interface
zorglub's avatar
zorglub committed
3
 ****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright © 2006-2009 the VideoLAN team
zorglub's avatar
zorglub committed
5
6
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
7
 *          Jean-Baptiste Kempf <jb@videolan.org>
zorglub's avatar
zorglub committed
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
 *****************************************************************************/
23

24
25
26
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
zorglub's avatar
zorglub committed
27

28
29
#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS

30
31
32
33
34
35
36
37
38
39
40
41
#include <stdlib.h>
#include <unistd.h>
#ifndef _POSIX_SPAWN
# define _POSIX_SPAWN (-1)
#endif
#if (_POSIX_SPAWN >= 0)
# include <spawn.h>
# include <sys/wait.h>

extern "C" char **environ;
#endif

zorglub's avatar
zorglub committed
42
#include <QApplication>
43
#include <QDate>
44
#include <QMutex>
Pierre Lamot's avatar
Pierre Lamot committed
45
#include <QtQuickControls2/QQuickStyle>
46
#include <QLoggingCategory>
zorglub's avatar
zorglub committed
47

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
48
#include "qt.hpp"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
49

50
#include "player/player_controller.hpp"    /* THEMIM destruction */
51
#include "playlist/playlist_controller.hpp" /* THEMPL creation */
52
#include "dialogs/dialogs_provider.hpp" /* THEDP creation */
53
#ifdef _WIN32
54
# include "maininterface/main_interface_win32.hpp"
55
#else
56
# include "maininterface/main_interface.hpp"   /* MainInterface creation */
57
#endif
58
#include "dialogs/extensions/extensions_manager.hpp" /* Extensions manager */
59
#include "dialogs/plugins/addons_manager.hpp" /* Addons manager */
60
#include "dialogs/help/help.hpp"     /* Launch Update */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
61
#include "util/qvlcapp.hpp"     /* QVLCApplication definition */
62
#include "maininterface/compositor.hpp"
zorglub's avatar
zorglub committed
63

64
#include <QVector>
65
#include "playlist/playlist_item.hpp"
66

67
#include <vlc_plugin.h>
68
#include <vlc_vout_window.h>
69
#include <vlc_cxx_helpers.hpp>
zorglub's avatar
zorglub committed
70

71
#ifdef QT_STATIC /* For static builds */
72
 #include <QtPlugin>
73
 #include <QQuickWindow>
74
75
76
77

 #ifdef QT_STATICPLUGIN
  Q_IMPORT_PLUGIN(QSvgIconPlugin)
  Q_IMPORT_PLUGIN(QSvgPlugin)
78
79
80
81
82
83
84
85
86
  Q_IMPORT_PLUGIN(QJpegPlugin)
  Q_IMPORT_PLUGIN(QtQuick2Plugin)
  Q_IMPORT_PLUGIN(QtQuickControls2Plugin)
  Q_IMPORT_PLUGIN(QtQuickLayoutsPlugin)
  Q_IMPORT_PLUGIN(QtQuick2WindowPlugin)
  Q_IMPORT_PLUGIN(QtQuickTemplates2Plugin)
  Q_IMPORT_PLUGIN(QtQmlModelsPlugin)
  Q_IMPORT_PLUGIN(QtGraphicalEffectsPlugin)
  Q_IMPORT_PLUGIN(QtGraphicalEffectsPrivatePlugin)
87
  Q_IMPORT_PLUGIN(QmlShapesPlugin)
88

89
90
91
92
  #if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
   Q_IMPORT_PLUGIN(QtQmlPlugin)
  #endif

93
94
  #ifdef _WIN32
   Q_IMPORT_PLUGIN(QWindowsVistaStylePlugin)
95
   Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
96
  #endif
97
 #endif
98
99
#endif

100
101
102
103
#ifndef X_DISPLAY_MISSING
# include <vlc_xlib.h>
#endif

zorglub's avatar
zorglub committed
104
105
106
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
107
static int  OpenIntf     ( vlc_object_t * );
zorglub's avatar
zorglub committed
108
static int  OpenDialogs  ( vlc_object_t * );
109
110
static int  Open         ( vlc_object_t *, bool );
static void Close        ( vlc_object_t * );
111
static int  WindowOpen   ( vout_window_t * );
zorglub's avatar
zorglub committed
112
113
114
115
116
static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
117
#define ADVANCED_PREFS_TEXT N_( "Show advanced preferences over simple ones" )
118
119
#define ADVANCED_PREFS_LONGTEXT N_( "Show advanced preferences and not simple "\
                                    "preferences when opening the preferences "\
120
                                    "dialog." )
121

122
123
124
#define SYSTRAY_TEXT N_( "Systray icon" )
#define SYSTRAY_LONGTEXT N_( "Show an icon in the systray " \
                             "allowing you to control VLC media player " \
125
                             "for basic actions." )
126

127
#define MINIMIZED_TEXT N_( "Start VLC with only a systray icon" )
128
#define MINIMIZED_LONGTEXT N_( "VLC will start with just an icon in " \
129
                               "your taskbar." )
130

131
132
133
134
135
#define KEEPSIZE_TEXT N_( "Resize interface to the native video size" )
#define KEEPSIZE_LONGTEXT N_( "You have two choices:\n" \
            " - The interface will resize to the native video size\n" \
            " - The video will fit to the interface size\n " \
            "By default, interface resize to the native video size." )
136

137
138
#define TITLE_TEXT N_( "Show playing item name in window title" )
#define TITLE_LONGTEXT N_( "Show the name of the song or video in the " \
VLC_help's avatar
VLC_help committed
139
                           "controller window title." )
140

141
#define NOTIFICATION_TEXT N_( "Show notification popup on track change" )
142
143
#define NOTIFICATION_LONGTEXT N_( \
    "Show a notification popup with the artist and track name when " \
144
    "the current playlist item changes, when VLC is minimized or hidden." )
145

146
#define OPACITY_TEXT N_( "Windows opacity between 0.1 and 1" )
147
#define OPACITY_LONGTEXT N_( "Sets the windows opacity between 0.1 and 1 " \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
148
                             "for main interface, playlist and extended panel."\
149
150
                             " This option only works with Windows and " \
                             "X11 with composite extensions." )
151

Christophe Mutricy's avatar
Typo    
Christophe Mutricy committed
152
#define OPACITY_FS_TEXT N_( "Fullscreen controller opacity between 0.1 and 1" )
153
154
155
156
157
#define OPACITY_FS_LONGTEXT N_( "Sets the fullscreen controller opacity between 0.1 and 1 " \
                             "for main interface, playlist and extended panel."\
                             " This option only works with Windows and " \
                             "X11 with composite extensions." )

Lyndon Brown's avatar
Lyndon Brown committed
158
#define INTERFACE_SCALE_TEXT N_( "Initial user scale factor for the interface, between 0.3 and 3.0" )
159

160
#define ERROR_TEXT N_( "Show unimportant error and warnings dialogs" )
161

162
#define UPDATER_TEXT N_( "Activate the updates availability notification" )
163
#define UPDATER_LONGTEXT N_( "Activate the automatic notification of new " \
164
165
                            "versions of the software. It runs once every " \
                            "two weeks." )
166
167
168
169

#define QT_QML_DEBUG_TEXT N_( "set the options for qml debugger" )
#define QT_QML_DEBUG_LONGTEXT N_( "set the options for qml debugger (see http://doc.qt.io/qt-5/qtquick-debugging.html#starting-applications)" )

dionoea's avatar
dionoea committed
170
#define UPDATER_DAYS_TEXT N_("Number of days between two update checks")
171

172
#define PRIVACY_TEXT N_( "Ask for network policy at start" )
173

Ludovic Fauvet's avatar
Ludovic Fauvet committed
174
#define RECENTPLAY_TEXT N_( "Save the recently played items in the menu" )
175

Ludovic Fauvet's avatar
Ludovic Fauvet committed
176
#define RECENTPLAY_FILTER_TEXT N_( "List of words separated by | to filter" )
177
#define RECENTPLAY_FILTER_LONGTEXT N_( "Regular expression used to filter " \
178
        "the recent items played in the player." )
Ludovic Fauvet's avatar
Ludovic Fauvet committed
179

180
#define SLIDERCOL_TEXT N_( "Define the colors of the volume slider" )
181
182
183
#define SLIDERCOL_LONGTEXT N_( "Define the colors of the volume slider\n" \
                       "By specifying the 12 numbers separated by a ';'\n" \
            "Default is '255;255;255;20;226;20;255;176;15;235;30;20'\n" \
184
            "An alternative can be '30;30;50;40;40;100;50;50;160;150;150;255'")
185

186
#define QT_MODE_TEXT N_( "Selection of the starting mode and look" )
187
188
#define QT_MODE_LONGTEXT N_( "Start VLC with:\n" \
                             " - normal mode\n"  \
189
                             " - a zone always present to show information " \
190
191
192
                                  "as lyrics, album arts...\n" \
                             " - minimal mode with limited controls" )

193
#define QT_FULLSCREEN_TEXT N_( "Show a controller in fullscreen mode" )
Rémi Denis-Courmont's avatar
English    
Rémi Denis-Courmont committed
194
#define QT_NATIVEOPEN_TEXT N_( "Embed the file browser in open dialog" )
195

196
#define FULLSCREEN_NUMBER_TEXT N_( "Define which screen fullscreen goes" )
VLC_help's avatar
VLC_help committed
197
#define FULLSCREEN_NUMBER_LONGTEXT N_( "Screennumber of fullscreen, instead of " \
198
                                       "same screen where interface is." )
199

200
201
#define QT_AUTOLOAD_EXTENSIONS_TEXT N_( "Load extensions on startup" )
#define QT_AUTOLOAD_EXTENSIONS_LONGTEXT N_( "Automatically load the "\
202
                                            "extensions module on startup." )
203

204
#define QT_MINIMAL_MODE_TEXT N_("Start in minimal view (without menus)" )
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
205

206
207
#define QT_BGCONE_TEXT N_( "Display background cone or art" )
#define QT_BGCONE_LONGTEXT N_( "Display background cone or current album art " \
208
                            "when not playing. " \
209
                            "Can be disabled to prevent burning screen." )
210
211
#define QT_BGCONE_EXPANDS_TEXT N_( "Expanding background cone or art" )
#define QT_BGCONE_EXPANDS_LONGTEXT N_( "Background art fits window's size." )
212
213
214
215
216
217
218

#define QT_DISABLE_VOLUME_KEYS_TEXT N_( "Ignore keyboard volume buttons." )
#define QT_DISABLE_VOLUME_KEYS_LONGTEXT N_(                                             \
    "With this option checked, the volume up, volume down and mute buttons on your "    \
    "keyboard will always change your system volume. With this option unchecked, the "  \
    "volume buttons will change VLC's volume when VLC is selected and change the "      \
    "system volume when VLC is not selected." )
219
220
221
222
223

#define QT_PAUSE_MINIMIZED_TEXT N_( "Pause the video playback when minimized" )
#define QT_PAUSE_MINIMIZED_LONGTEXT N_( \
    "With this option enabled, the playback will be automatically paused when minimizing the window." )

224
225
226
227
#define ICONCHANGE_TEXT N_( "Allow automatic icon changes")
#define ICONCHANGE_LONGTEXT N_( \
    "This option allows the interface to change its icon on various occasions.")

228
229
#define VOLUME_MAX_TEXT N_( "Maximum Volume displayed" )

230
231
#define AUTORAISE_ON_PLAYBACK_TEXT N_( "When to raise the interface" )
#define AUTORAISE_ON_PLAYBACK_LONGTEXT N_( "This option allows the interface to be raised automatically " \
232
    "when a video/audio playback starts, or never." )
233

234
235
236
237
#define QT_CLIENT_SIDE_DECORATION_TEXT N_( "Enable window titlebar" )
#define QT_CLIENT_SIDE_DECORATION_LONGTEXT N_( "This option enables the title bar. Disabling it will remove " \
    "the titlebar and move window buttons within the interface (Client Side Decoration)" )

238
239
240
241

#define QT_MENUBAR_TEXT N_( "Show the menu bar" )
#define QT_MENUBAR_LONGTEXT N_( "This option displays the classic menu bar" )

242
243
#define FULLSCREEN_CONTROL_PIXELS N_( "Fullscreen controller mouse sensitivity" )

244
245
246
247
248
249
static const int i_notification_list[] =
    { NOTIFICATION_NEVER, NOTIFICATION_MINIMIZED, NOTIFICATION_ALWAYS };

static const char *const psz_notification_list_text[] =
    { N_("Never"), N_("When minimized"), N_("Always") };

250
251
252
253
254
static const int i_raise_list[] =
    { MainInterface::RAISE_NEVER, MainInterface::RAISE_VIDEO, \
      MainInterface::RAISE_AUDIO, MainInterface::RAISE_AUDIOVIDEO,  };

static const char *const psz_raise_list_text[] =
255
    { N_( "Never" ), N_( "Video" ), N_( "Audio" ), _( "Audio/Video" ) };
256

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
257
/**********************************************************************/
258
259
260
vlc_module_begin ()
    set_shortname( "Qt" )
    set_description( N_("Qt interface") )
261
    set_category( CAT_INTERFACE )
262
263
    set_subcategory( SUBCAT_INTERFACE_MAIN )
    set_capability( "interface", 151 )
264
    set_callbacks( OpenIntf, Close )
265
266

    add_shortcut("qt")
267

268
    add_bool( "qt-minimal-view", false, QT_MINIMAL_MODE_TEXT,
269
              nullptr, false );
270

271
    add_bool( "qt-system-tray", true, SYSTRAY_TEXT, SYSTRAY_LONGTEXT, false)
272
273
274
275
276
277

    add_integer( "qt-notification", NOTIFICATION_MINIMIZED,
                 NOTIFICATION_TEXT,
                 NOTIFICATION_LONGTEXT, false )
            change_integer_list( i_notification_list, psz_notification_list_text )

278
279
    add_bool( "qt-start-minimized", false, MINIMIZED_TEXT,
              MINIMIZED_LONGTEXT, true)
280
    add_bool( "qt-pause-minimized", false, QT_PAUSE_MINIMIZED_TEXT,
281
              QT_PAUSE_MINIMIZED_LONGTEXT, false )
282

283
    add_float_with_range( "qt-opacity", 1., 0.1, 1., OPACITY_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
284
                          OPACITY_LONGTEXT, false )
285
    add_float_with_range( "qt-fs-opacity", 0.8, 0.1, 1., OPACITY_FS_TEXT,
286
                          OPACITY_FS_LONGTEXT, false )
287

288
289
290
    //qt-interface-scale is stored in Qt config file
    //this option is here to force an initial scale factor at startup
    add_float_with_range( "qt-interface-scale", -1.0, 0.3, 3.0, INTERFACE_SCALE_TEXT,
291
                          nullptr, false )
292
        change_volatile()
293

294
    add_bool( "qt-video-autoresize", true, KEEPSIZE_TEXT,
295
              KEEPSIZE_LONGTEXT, false )
296
    add_bool( "qt-name-in-title", true, TITLE_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
297
              TITLE_LONGTEXT, false )
298
    add_bool( "qt-fs-controller", true, QT_FULLSCREEN_TEXT,
299
              nullptr, false )
300

301
    add_bool( "qt-recentplay", true, RECENTPLAY_TEXT,
302
              nullptr, false )
303
304
    add_string( "qt-recentplay-filter", "",
                RECENTPLAY_FILTER_TEXT, RECENTPLAY_FILTER_LONGTEXT, false )
305
    add_obsolete_integer( "qt-continue" )
306
307
308
309

#ifdef UPDATE_CHECK
    add_bool( "qt-updates-notif", true, UPDATER_TEXT,
              UPDATER_LONGTEXT, false )
310
    add_integer_with_range( "qt-updates-days", 3, 0, 180,
311
              UPDATER_DAYS_TEXT, nullptr, false )
312
313
#endif

314
315
316
317
318
#ifdef QT_QML_DEBUG
    add_string( "qt-qmljsdebugger", NULL,
                QT_QML_DEBUG_TEXT, QT_QML_DEBUG_LONGTEXT, false )
#endif

319
#ifdef _WIN32
320
321
322
323
324
325
326
    add_bool( "qt-disable-volume-keys"             /* name */,
              true                                 /* default value */,
              QT_DISABLE_VOLUME_KEYS_TEXT          /* text */,
              QT_DISABLE_VOLUME_KEYS_LONGTEXT      /* longtext */,
              false                                /* advanced mode only */)
#endif

327
#if QT_CLIENT_SIDE_DECORATION_AVAILABLE
328
329
330
331
332
333
334
335
336
    add_bool( "qt-titlebar",
#ifdef _WIN32
              false                              /* use CSD by default on windows */,
#else
              true                               /* but not on linux */,
#endif
              QT_CLIENT_SIDE_DECORATION_TEXT, QT_CLIENT_SIDE_DECORATION_LONGTEXT, false )
#endif

337
338
    add_bool( "qt-menubar", false, QT_MENUBAR_TEXT, QT_MENUBAR_LONGTEXT, false )

339
    add_bool( "qt-embedded-open", false, QT_NATIVEOPEN_TEXT,
340
               nullptr, false )
341

342

343
    add_bool( "qt-advanced-pref", false, ADVANCED_PREFS_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
344
              ADVANCED_PREFS_LONGTEXT, false )
345
    add_bool( "qt-error-dialogs", true, ERROR_TEXT,
346
              nullptr, false )
347

348
    add_string( "qt-slider-colours", "153;210;153;20;210;20;255;199;15;245;39;29",
349
                SLIDERCOL_TEXT, SLIDERCOL_LONGTEXT, false )
350

351
    add_bool( "qt-privacy-ask", true, PRIVACY_TEXT, nullptr,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
352
              false )
353
        change_private ()
354

355
    add_integer( "qt-fullscreen-screennumber", -1, FULLSCREEN_NUMBER_TEXT,
356
357
               FULLSCREEN_NUMBER_LONGTEXT, false );

358
    add_bool( "qt-autoload-extensions", true,
359
360
361
              QT_AUTOLOAD_EXTENSIONS_TEXT, QT_AUTOLOAD_EXTENSIONS_LONGTEXT,
              false )

362
363
364
365
    add_bool( "qt-bgcone", true, QT_BGCONE_TEXT, QT_BGCONE_LONGTEXT, true )
    add_bool( "qt-bgcone-expands", false, QT_BGCONE_EXPANDS_TEXT,
              QT_BGCONE_EXPANDS_LONGTEXT, true )

366
    add_bool( "qt-icon-change", true, ICONCHANGE_TEXT, ICONCHANGE_LONGTEXT, true )
367

368
    add_integer_with_range( "qt-max-volume", 125, 60, 300, VOLUME_MAX_TEXT, nullptr, true)
369

370
    add_integer_with_range( "qt-fs-sensitivity", 3, 0, 4000, FULLSCREEN_CONTROL_PIXELS,
371
            nullptr, true)
372

373
374
375
376
    add_integer( "qt-auto-raise", MainInterface::RAISE_VIDEO, AUTORAISE_ON_PLAYBACK_TEXT,
                 AUTORAISE_ON_PLAYBACK_LONGTEXT, false )
            change_integer_list( i_raise_list, psz_raise_list_text )

377
    cannot_unload_broken_library()
378

379
380
381
    add_submodule ()
        set_description( "Dialogs provider" )
        set_capability( "dialogs provider", 51 )
382

383
        set_callbacks( OpenDialogs, Close )
384

385
    add_submodule ()
386
        set_capability( "vout window", 0 )
387
        set_callback( WindowOpen )
388

389
vlc_module_end ()
zorglub's avatar
zorglub committed
390

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
391
392
/*****************************************/

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
393
/* Ugly, but the Qt interface assumes single instance anyway */
394
395
static vlc::threads::condition_variable wait_ready;
static vlc::threads::mutex lock;
396
static bool busy = false;
397
398
399
static enum {
    OPEN_STATE_INIT,
    OPEN_STATE_OPENED,
Thomas Guillem's avatar
Thomas Guillem committed
400
    OPEN_STATE_ERROR,
401
} open_state = OPEN_STATE_INIT;
402

zorglub's avatar
zorglub committed
403
404
405
/*****************************************************************************
 * Module callbacks
 *****************************************************************************/
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
406

407
static void *Thread( void * );
Thomas Guillem's avatar
Thomas Guillem committed
408
static void *ThreadCleanup( intf_thread_t *p_intf, bool error );
409

410
#ifdef Q_OS_MAC
411
/* Used to abort the app.exec() on OSX after libvlc_Quit is called */
412
#include "../../../lib/libvlc_internal.h" /* libvlc_SetExitHandler */
413
414
415
416
417
418
static void Abort( void *obj )
{
    QVLCApp::triggerQuit();
}
#endif

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
419
/* Open Interface */
420
static int Open( vlc_object_t *p_this, bool isDialogProvider )
zorglub's avatar
zorglub committed
421
422
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
423

424
425
#ifndef X_DISPLAY_MISSING
    if (!vlc_xlib_init(p_this))
426
        return VLC_EGENERIC;
427
#endif
428

429
430
#if (_POSIX_SPAWN >= 0)
    /* Check if QApplication works */
431
    char *path = config_GetSysPath(VLC_PKG_LIBEXEC_DIR, "vlc-qt-check");
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    if (unlikely(path == NULL))
        return VLC_ENOMEM;

    char *argv[] = { path, NULL };
    pid_t pid;

    int val = posix_spawn(&pid, path, NULL, NULL, argv, environ);
    free(path);
    if (val)
        return VLC_ENOMEM;

    int status;
    while (waitpid(pid, &status, 0) == -1);

    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
447
    {
448
449
        msg_Dbg(p_this, "Qt check failed (%d). Skipping.", status);
        return VLC_EGENERIC;
450
451
452
    }
#endif

453
454
455
    /* Get the playlist before the lock to avoid a lock-order-inversion */
    vlc_playlist_t *playlist = vlc_intf_GetMainPlaylist(p_intf);

456
    vlc::threads::mutex_locker locker (lock);
Thomas Guillem's avatar
Thomas Guillem committed
457
    if (busy || open_state == OPEN_STATE_ERROR)
458
    {
Thomas Guillem's avatar
Thomas Guillem committed
459
460
        if (busy)
            msg_Err (p_this, "cannot start Qt multiple times");
461
462
463
        return VLC_EGENERIC;
    }

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
464
465
    /* Allocations of p_sys */
    intf_sys_t *p_sys = p_intf->p_sys = new intf_sys_t;
466
    p_sys->b_isDialogProvider = isDialogProvider;
467
468
    p_sys->p_mi = NULL;

469
    /* set up the playlist to work on */
470
    p_sys->p_playlist = playlist;
471
    p_sys->p_player = vlc_playlist_GetPlayer( p_sys->p_playlist );
472

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
473
    /* */
474
#ifdef Q_OS_MAC
475
    /* Run mainloop on the main thread as Cocoa requires */
476
    libvlc_SetExitHandler( vlc_object_instance(p_intf), Abort, p_intf );
Marvin Scholz's avatar
Marvin Scholz committed
477
    Thread( (void *)p_intf );
478
#else
479
    if( vlc_clone( &p_sys->thread, Thread, p_intf, VLC_THREAD_PRIORITY_LOW ) )
480
481
    {
        delete p_sys;
Christophe Mutricy's avatar
Christophe Mutricy committed
482
        return VLC_ENOMEM;
483
    }
484
#endif
485

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
486
487
488
    /* Wait for the interface to be ready. This prevents the main
     * LibVLC thread from starting video playback before we can create
     * an embedded video window. */
489
#ifndef Q_OS_MAC
490
    while (open_state == OPEN_STATE_INIT)
491
        wait_ready.wait(lock);
492
#endif
493

Thomas Guillem's avatar
Thomas Guillem committed
494
495
496
497
498
499
500
501
502
503
    if (open_state == OPEN_STATE_ERROR)
    {
#ifndef Q_OS_MAC
        vlc_join (p_sys->thread, NULL);
#endif
        delete p_sys;
        return VLC_EGENERIC;
    }

    busy = true;
zorglub's avatar
zorglub committed
504
505
506
    return VLC_SUCCESS;
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
507
/* Open Qt interface */
508
509
510
511
512
static int OpenIntf( vlc_object_t *p_this )
{
    return Open( p_this, false );
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
513
/* Open Dialog Provider */
zorglub's avatar
zorglub committed
514
515
static int OpenDialogs( vlc_object_t *p_this )
{
516
    return Open( p_this, true );
zorglub's avatar
zorglub committed
517
518
519
520
521
}

static void Close( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
522
523
    intf_sys_t *p_sys = p_intf->p_sys;

524
    /* And quit */
525
    msg_Dbg( p_this, "requesting exit..." );
526
    QVLCApp::triggerQuit();
527

528
    msg_Dbg( p_this, "waiting for UI thread..." );
529
#ifndef Q_OS_MAC
530
    vlc_join (p_sys->thread, NULL);
531
#endif
532
    delete p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
533

534
    vlc::threads::mutex_locker locker (lock);
535
    assert (busy);
536
    assert (open_state == OPEN_STATE_INIT);
537
    busy = false;
zorglub's avatar
zorglub committed
538
539
}

540
541
542
543
544
static inline void qRegisterMetaTypes()
{
    // register all types used by signal/slots
    qRegisterMetaType<size_t>("size_t");
    qRegisterMetaType<ssize_t>("ssize_t");
545
    qRegisterMetaType<vlc_tick_t>("vlc_tick_t");
546
547
}

548
static void *Thread( void *obj )
zorglub's avatar
zorglub committed
549
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
550
    intf_thread_t *p_intf = (intf_thread_t *)obj;
551
    intf_sys_t *p_sys = p_intf->p_sys;
552
    char vlc_name[] = "vlc"; /* for WM_CLASS */
553
    char *argv[3] = { nullptr };
554
555
    int argc = 0;

556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
    auto argvReleaser = vlc::wrap_carray<char*>(argv, [](char* ptr[]) {
        for ( int i = 0; ptr[i] != nullptr; ++i )
            free(ptr[i]);
    });
    argv[argc++] = strdup(vlc_name);

#ifdef QT_QML_DEBUG
    char* qmlJsDebugOpt = var_InheritString(p_intf, "qt-qmljsdebugger");
    if (qmlJsDebugOpt)
    {
        msg_Dbg(p_intf, "option qt-qmljsdebugger is %s", qmlJsDebugOpt);
        char* psz_debug_opt;
        if (asprintf(&psz_debug_opt, "-qmljsdebugger=%s", qmlJsDebugOpt) < 0)
        {
            free(qmlJsDebugOpt);
            return NULL;
        }
        argv[argc++] = psz_debug_opt;
        free(qmlJsDebugOpt);
    }
#endif
577
    argv[argc] = NULL;
578

Rafaël Carré's avatar
Rafaël Carré committed
579
    Q_INIT_RESOURCE( vlc );
580

581
582
    p_intf->p_sys->p_compositor = vlc::Compositor::createCompositor(p_intf);

583
    QApplication::setAttribute( Qt::AA_EnableHighDpiScaling );
584
    QApplication::setAttribute( Qt::AA_UseHighDpiPixmaps );
585

586
587
588
#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
    QApplication::setHighDpiScaleFactorRoundingPolicy( Qt::HighDpiScaleFactorRoundingPolicy::RoundPreferFloor );
#endif
589
590
591
592
    // at the moment, the vout is created in another thread than the rendering thread
    QApplication::setAttribute( Qt::AA_DontCheckOpenGLContextThreadAffinity );
    QQuickWindow::setDefaultAlphaBuffer(true);

593
    /* Start the QApplication here */
594
    QVLCApp app( argc, argv );
595

596
597
598
599
600
601
#if QT_VERSION >= QT_VERSION_CHECK(5,15,1)
    //suppress deprecation warnings about QML 'Connections' syntax
    //legacy connection syntax is required to keep compatibility with Qt <= 5.14
    QLoggingCategory::setFilterRules("qt.qml.connections.warning=false");
#endif

602
603
    //app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);

fahadalsaidi's avatar
fahadalsaidi committed
604
605
606
607
    /* Set application direction to locale direction,
     * necessary for  RTL locales */
    app.setLayoutDirection(QLocale().textDirection());

608
    p_sys->p_app = &app;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
609

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
610
611

    /* All the settings are in the .conf/.ini style */
ssbssa's avatar
ssbssa committed
612
613
614
615
616
617
618
619
620
#ifdef _WIN32
    char *cConfigDir = config_GetUserDir( VLC_CONFIG_DIR );
    QString configDir = cConfigDir;
    free( cConfigDir );
    if( configDir.endsWith( "\\vlc" ) )
        configDir.chop( 4 ); /* the "\vlc" dir is added again by QSettings */
    QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, configDir );
#endif

621
    p_sys->mainSettings = new QSettings(
622
#ifdef _WIN32
623
624
625
626
627
628
            QSettings::IniFormat,
#else
            QSettings::NativeFormat,
#endif
            QSettings::UserScope, "vlc", "vlc-qt-interface" );

629
630
    app.setApplicationDisplayName( qtr("VLC media player") );

631
    if( QDate::currentDate().dayOfYear() >= QT_XMAS_JOKE_DAY && var_InheritBool( p_intf, "qt-icon-change" ) )
632
        app.setWindowIcon( QIcon::fromTheme( "vlc-xmas", QIcon( ":/logo/vlc128-xmas.png" ) ) );
633
    else
634
        app.setWindowIcon( QIcon::fromTheme( "vlc", QIcon( ":/logo/vlc256.png" ) ) );
Rafaël Carré's avatar
Rafaël Carré committed
635

636
637
    app.setDesktopFileName( PACKAGE );

638
    /* Initialize the Dialog Provider and the Main Input Manager */
639
    DialogsProvider::getInstance( p_intf );
640
641
    p_sys->p_mainPlayerController = new PlayerController(p_intf);
    p_sys->p_mainPlaylistController = new vlc::playlist::PlaylistControllerModel(p_intf->p_sys->p_playlist);
zorglub's avatar
zorglub committed
642

643
644
#ifdef UPDATE_CHECK
    /* Checking for VLC updates */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
645
646
    if( var_InheritBool( p_intf, "qt-updates-notif" ) &&
        !var_InheritBool( p_intf, "qt-privacy-ask" ) )
647
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
648
        int interval = var_InheritInteger( p_intf, "qt-updates-days" );
649
650
651
652
653
654
655
656
657
658
        if( QDate::currentDate() >
             getSettings()->value( "updatedate" ).toDate().addDays( interval ) )
        {
            /* The constructor of the update Dialog will do the 1st request */
            UpdateDialog::getInstance( p_intf );
            getSettings()->setValue( "updatedate", QDate::currentDate() );
        }
    }
#endif

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
659
    /* Create the normal interface in non-DP mode */
660
661
662
    MainInterface *p_mi = NULL;

    if( !p_sys->b_isDialogProvider )
663
    {
664
        p_mi = p_intf->p_sys->p_compositor->makeMainInterface();
665
        p_sys->p_mi = p_mi;
666

667
        if (!p_mi)
Thomas Guillem's avatar
Thomas Guillem committed
668
        {
669
            msg_Err(p_intf, "unable to create main interface");
Thomas Guillem's avatar
Thomas Guillem committed
670
671
672
            return ThreadCleanup( p_intf, true );
        }

673
        /* Check window type from the Qt platform back-end */
674
675
        bool known_type = true;

676
677
678
        QString platform = app.platformName();
        if( platform == qfu("xcb") )
            p_sys->voutWindowType = VOUT_WINDOW_TYPE_XID;
679
        else if( platform == qfu("wayland") || platform == qfu("wayland-egl") )
680
            p_sys->voutWindowType = VOUT_WINDOW_TYPE_WAYLAND;
681
682
683
684
685
        else if( platform == qfu("windows") )
            p_sys->voutWindowType = VOUT_WINDOW_TYPE_HWND;
        else if( platform == qfu("cocoa" ) )
            p_sys->voutWindowType = VOUT_WINDOW_TYPE_NSOBJECT;
        else
686
        {
687
            msg_Err( p_intf, "unknown Qt platform: %s", qtu(platform) );
688
689
            known_type = false;
        }
690

691
692
693
694
        /* FIXME: Temporary, while waiting for a proper window provider API */
        libvlc_int_t *libvlc = vlc_object_instance( p_intf );

        var_Create( libvlc, "qt4-iface", VLC_VAR_ADDRESS );
695
696
697

        if( known_type )
        {
698
699
            var_SetAddress( libvlc, "qt4-iface", p_intf );
            var_SetString( libvlc, "window", "qt,any" );
700
        }
701
702
703
704
705
    }

    /* Explain how to show a dialog :D */
    p_intf->pf_show_dialog = ShowDialog;

706
#ifndef Q_OS_MAC
707
    /* Tell the main LibVLC thread we are ready */
708
    {
709
        vlc::threads::mutex_locker locker (lock);
710
        open_state = OPEN_STATE_OPENED;
711
        wait_ready.signal();
712
    }
713
#else
714
    /* We took over main thread, register and start here */
715
    if( !p_sys->b_isDialogProvider )
716
717
718
719
720
    {
        vlc_playlist_Lock( p_intf->p_sys->p_playlist );
        vlc_playlist_Start( p_intf->p_sys->p_playlist );
        vlc_playlist_Unlock( p_intf->p_sys->p_playlist );
    }
721
722
#endif

723
    /* Last settings */
724
    app.setQuitOnLastWindowClosed( false );
725

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
726
    /* Retrieve last known path used in file browsing */
727
    p_sys->filepath =
728
         getSettings()->value( "filedialog-path", QVLCUserDir( VLC_HOME_DIR ) ).toString();
729

730
731
732
733
734
    /* Loads and tries to apply the preferred QStyle */
    QString s_style = getSettings()->value( "MainWindow/QtStyle", "" ).toString();
    if( s_style.compare("") != 0 )
        QApplication::setStyle( s_style );

735
736
    qRegisterMetaTypes();

737
    /* Launch */
738
    app.exec();
739

740
    msg_Dbg( p_intf, "QApp exec() finished" );
741
    if (p_mi != NULL)
742
    {
743
744
        libvlc_int_t *libvlc = vlc_object_instance( p_intf );
        var_Destroy( libvlc, "qt4-iface" );
745
    }
Thomas Guillem's avatar
Thomas Guillem committed
746
    return ThreadCleanup( p_intf, false );
747
748
}

Thomas Guillem's avatar
Thomas Guillem committed
749
static void *ThreadCleanup( intf_thread_t *p_intf, bool error )
750
751
{
    intf_sys_t *p_sys = p_intf->p_sys;
752

753
    {
754
#ifndef Q_OS_MAC
755
        vlc::threads::mutex_locker locker (lock);
756
#endif
Thomas Guillem's avatar
Thomas Guillem committed
757
758
759
        if( error )
        {
            open_state = OPEN_STATE_ERROR;
760
#ifndef Q_OS_MAC
761
            wait_ready.signal();
762
#endif
Thomas Guillem's avatar
Thomas Guillem committed
763
764
765
        }
        else
            open_state = OPEN_STATE_INIT;
766
    }
767

768
    if (p_sys->p_compositor)
769
    {
770
771
772
773
774
        p_sys->p_compositor->destroyMainInterface();
        p_sys->p_mi = nullptr;

        delete p_sys->p_compositor;
        p_sys->p_compositor = nullptr;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
775
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
776

777
778
    /* */
    ExtensionsManager::killInstance();
779
    AddonsManager::killInstance();
780

781
782
783
784
785
    /* Destroy all remaining windows,
       because some are connected to some slots
       in the MainInputManager
       Settings must be destroyed after that.
     */
zorglub's avatar
zorglub committed
786
    DialogsProvider::killInstance();
Ludovic Fauvet's avatar
Ludovic Fauvet committed
787

788
789
    /* Save the path or delete if recent play are disabled */
    if( var_InheritBool( p_intf, "qt-recentplay" ) )
790
        getSettings()->setValue( "filedialog-path", p_sys->filepath );
791
792
    else
        getSettings()->remove( "filedialog-path" );
793

794
795
796
797
    /* Destroy the main playlist controller */
    delete p_sys->p_mainPlaylistController;
    /* Destroy the main InputManager */
    delete p_sys->p_mainPlayerController;
798
    /* Delete the configuration. Application has to be deleted after that. */
799
    delete p_sys->mainSettings;
800

801
    /* Delete the application automatically */
802
    return NULL;
zorglub's avatar
zorglub committed
803
804
805
806
807
808
809
810
}

/*****************************************************************************
 * Callback to show a dialog
 *****************************************************************************/
static void ShowDialog( intf_thread_t *p_intf, int i_dialog_event, int i_arg,
                        intf_dialog_args_t *p_arg )
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
811
    VLC_UNUSED( p_intf );
zorglub's avatar
zorglub committed
812
    DialogEvent *event = new DialogEvent( i_dialog_event, i_arg, p_arg );
813
    QApplication::postEvent( THEDP, event );
zorglub's avatar
zorglub committed
814
}
zorglub's avatar
zorglub committed
815

816
817
818
/**
 * Video output window provider
 */
819
static int WindowOpen( vout_window_t *p_wnd )
820
{
821
    if( !var_InheritBool( p_wnd, "embedded-video" ) )
822
823
        return VLC_EGENERIC;

824
    libvlc_int_t *libvlc = vlc_object_instance( p_wnd );
825
    intf_thread_t *p_intf =
826
        (intf_thread_t *)var_InheritAddress( libvlc, "qt4-iface" );
827
    if( !p_intf )
828
    {   /* If another interface is used, this plugin cannot work */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
829
        msg_Dbg( p_wnd, "Qt interface not found" );
830
831
        return VLC_EGENERIC;
    }
832
833

    switch( p_intf->p_sys->voutWindowType )
834
835
    {
        case VOUT_WINDOW_TYPE_XID:
836
        case VOUT_WINDOW_TYPE_HWND:
837
838
839
840
            if( var_InheritBool( p_wnd, "video-wallpaper" ) )
                return VLC_EGENERIC;
            break;
    }
841

842
    vlc::threads::mutex_locker locker (lock);
843
    if (unlikely(open_state != OPEN_STATE_OPENED))
844
845
        return VLC_EGENERIC;

846
    return p_intf->p_sys->p_compositor->setupVoutWindow( p_wnd ) ? VLC_SUCCESS : VLC_EGENERIC;
847

848
}