qt4.cpp 25.2 KB
Newer Older
Clément Stenac's avatar
Clément Stenac committed
1
/*****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
2
 * qt4.cpp : Qt interface
Clément Stenac's avatar
Clément Stenac committed
3
 ****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright © 2006-2009 the VideoLAN team
5
 * $Id$
Clément Stenac's avatar
Clément Stenac committed
6 7
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          Jean-Baptiste Kempf <jb@videolan.org>
Clément Stenac's avatar
Clément Stenac committed
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 *****************************************************************************/
24

25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
Clément Stenac's avatar
Clément Stenac committed
28 29

#include <QApplication>
30
#include <QDate>
31
#include <QMutex>
32

Clément Stenac's avatar
Clément Stenac committed
33
#include "qt4.hpp"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
34

35
#include "input_manager.hpp"    /* THEMIM destruction */
36 37
#include "dialogs_provider.hpp" /* THEDP creation */
#include "main_interface.hpp"   /* MainInterface creation */
38
#include "extensions_manager.hpp" /* Extensions manager */
39
#include "managers/addons_manager.hpp" /* Addons manager */
40 41
#include "dialogs/help.hpp"     /* Launch Update */
#include "recents.hpp"          /* Recents Item destruction */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
42
#include "util/qvlcapp.hpp"     /* QVLCApplication definition */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
43
#include "components/playlist/playlist_model.hpp" /* for ~PLModel() */
44

45
#ifdef Q_WS_X11
46
 #include <vlc_xlib.h>
Rafaël Carré's avatar
Rafaël Carré committed
47 48
#endif

49
#include <vlc_plugin.h>
50
#include <vlc_vout_window.h>
Clément Stenac's avatar
Clément Stenac committed
51

52
#ifdef _WIN32 /* For static builds */
53
 #include <QtPlugin>
54
 #if HAS_QT5
55 56 57 58 59 60
  Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
  Q_IMPORT_PLUGIN(AccessibleFactory)
 #else
  Q_IMPORT_PLUGIN(qjpeg)
  Q_IMPORT_PLUGIN(qtaccessiblewidgets)
 #endif
61 62
#endif

Clément Stenac's avatar
Clément Stenac committed
63 64 65
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
66
static int  OpenIntf     ( vlc_object_t * );
Clément Stenac's avatar
Clément Stenac committed
67
static int  OpenDialogs  ( vlc_object_t * );
68 69
static int  Open         ( vlc_object_t *, bool );
static void Close        ( vlc_object_t * );
70 71
static int  WindowOpen   ( vout_window_t *, const vout_window_cfg_t * );
static void WindowClose  ( vout_window_t * );
72
static void *Thread      ( void * );
Clément Stenac's avatar
Clément Stenac committed
73 74 75 76 77
static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
78
#define ADVANCED_PREFS_TEXT N_( "Show advanced preferences over simple ones" )
79 80
#define ADVANCED_PREFS_LONGTEXT N_( "Show advanced preferences and not simple "\
                                    "preferences when opening the preferences "\
81
                                    "dialog." )
82

83 84 85
#define SYSTRAY_TEXT N_( "Systray icon" )
#define SYSTRAY_LONGTEXT N_( "Show an icon in the systray " \
                             "allowing you to control VLC media player " \
86
                             "for basic actions." )
87

88
#define MINIMIZED_TEXT N_( "Start VLC with only a systray icon" )
89 90
#define MINIMIZED_LONGTEXT N_( "VLC will start with just an icon in " \
                               "your taskbar" )
91

92 93 94 95 96
#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." )
97

98 99
#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 " \
100
                           "controller window title." )
101

102
#define NOTIFICATION_TEXT N_( "Show notification popup on track change" )
103 104
#define NOTIFICATION_LONGTEXT N_( \
    "Show a notification popup with the artist and track name when " \
105
    "the current playlist item changes, when VLC is minimized or hidden." )
106

107
#define OPACITY_TEXT N_( "Windows opacity between 0.1 and 1" )
108
#define OPACITY_LONGTEXT N_( "Sets the windows opacity between 0.1 and 1 " \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
109
                             "for main interface, playlist and extended panel."\
110 111
                             " This option only works with Windows and " \
                             "X11 with composite extensions." )
112

Christophe Mutricy's avatar
Typo  
Christophe Mutricy committed
113
#define OPACITY_FS_TEXT N_( "Fullscreen controller opacity between 0.1 and 1" )
114 115 116 117 118 119
#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." )


120
#define ERROR_TEXT N_( "Show unimportant error and warnings dialogs" )
121

122
#define UPDATER_TEXT N_( "Activate the updates availability notification" )
123
#define UPDATER_LONGTEXT N_( "Activate the automatic notification of new " \
124 125
                            "versions of the software. It runs once every " \
                            "two weeks." )
Antoine Cellerier's avatar
Antoine Cellerier committed
126
#define UPDATER_DAYS_TEXT N_("Number of days between two update checks")
127

128
#define PRIVACY_TEXT N_( "Ask for network policy at start" )
129

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

Ludovic Fauvet's avatar
Ludovic Fauvet committed
132
#define RECENTPLAY_FILTER_TEXT N_( "List of words separated by | to filter" )
133 134
#define RECENTPLAY_FILTER_LONGTEXT N_( "Regular expression used to filter " \
        "the recent items played in the player" )
Ludovic Fauvet's avatar
Ludovic Fauvet committed
135

136 137 138 139
#define SLIDERCOL_TEXT N_( "Define the colors of the volume slider " )
#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" \
140
            "An alternative can be '30;30;50;40;40;100;50;50;160;150;150;255' ")
141 142 143 144

#define QT_MODE_TEXT N_( "Selection of the starting mode and look " )
#define QT_MODE_LONGTEXT N_( "Start VLC with:\n" \
                             " - normal mode\n"  \
145
                             " - a zone always present to show information " \
146 147 148
                                  "as lyrics, album arts...\n" \
                             " - minimal mode with limited controls" )

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

152
#define FULLSCREEN_NUMBER_TEXT N_( "Define which screen fullscreen goes" )
Kaarlo Raiha's avatar
Kaarlo Raiha committed
153
#define FULLSCREEN_NUMBER_LONGTEXT N_( "Screennumber of fullscreen, instead of " \
154 155
                                       "same screen where interface is" )

156 157 158 159
#define QT_AUTOLOAD_EXTENSIONS_TEXT N_( "Load extensions on startup" )
#define QT_AUTOLOAD_EXTENSIONS_LONGTEXT N_( "Automatically load the "\
                                            "extensions module on startup" )

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

162 163
#define QT_BGCONE_TEXT N_( "Display background cone or art" )
#define QT_BGCONE_LONGTEXT N_( "Display background cone or current album art " \
164
                            "when not playing. " \
165 166 167
                            "Can be disabled to prevent burning screen." )
#define QT_BGCONE_EXPANDS_TEXT N_( "Expanding background cone or art." )
#define QT_BGCONE_EXPANDS_LONGTEXT N_( "Background art fits window's size" )
168 169 170 171 172 173 174

#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." )
175 176 177 178 179

#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." )

180 181 182 183
#define ICONCHANGE_TEXT N_( "Allow automatic icon changes")
#define ICONCHANGE_LONGTEXT N_( \
    "This option allows the interface to change its icon on various occasions.")

184 185
#define VOLUME_MAX_TEXT N_( "Maximum Volume displayed" )

186 187
#define FULLSCREEN_CONTROL_PIXELS N_( "Fullscreen controller mouse sensitivity" )

188 189 190 191 192 193
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") };

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
194
/**********************************************************************/
195 196 197
vlc_module_begin ()
    set_shortname( "Qt" )
    set_description( N_("Qt interface") )
198
    set_category( CAT_INTERFACE )
199 200
    set_subcategory( SUBCAT_INTERFACE_MAIN )
    set_capability( "interface", 151 )
201
    set_callbacks( OpenIntf, Close )
202 203

    add_shortcut("qt")
204

205
    add_bool( "qt-minimal-view", false, QT_MINIMAL_MODE_TEXT,
206
              QT_MINIMAL_MODE_TEXT, false );
207

208
    add_bool( "qt-system-tray", true, SYSTRAY_TEXT, SYSTRAY_LONGTEXT, false)
209 210 211 212 213 214

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

215 216
    add_bool( "qt-start-minimized", false, MINIMIZED_TEXT,
              MINIMIZED_LONGTEXT, true)
217
    add_bool( "qt-pause-minimized", false, QT_PAUSE_MINIMIZED_TEXT,
218
              QT_PAUSE_MINIMIZED_LONGTEXT, false )
219

220
    add_float_with_range( "qt-opacity", 1., 0.1, 1., OPACITY_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
221
                          OPACITY_LONGTEXT, false )
222
    add_float_with_range( "qt-fs-opacity", 0.8, 0.1, 1., OPACITY_FS_TEXT,
223
                          OPACITY_FS_LONGTEXT, false )
224

225
    add_bool( "qt-video-autoresize", true, KEEPSIZE_TEXT,
226
              KEEPSIZE_LONGTEXT, false )
227
    add_bool( "qt-name-in-title", true, TITLE_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
228
              TITLE_LONGTEXT, false )
229
    add_bool( "qt-fs-controller", true, QT_FULLSCREEN_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
230
              QT_FULLSCREEN_TEXT, false )
231

232 233 234 235 236 237 238 239
    add_bool( "qt-recentplay", true, RECENTPLAY_TEXT,
              RECENTPLAY_TEXT, false )
    add_string( "qt-recentplay-filter", "",
                RECENTPLAY_FILTER_TEXT, RECENTPLAY_FILTER_LONGTEXT, false )

#ifdef UPDATE_CHECK
    add_bool( "qt-updates-notif", true, UPDATER_TEXT,
              UPDATER_LONGTEXT, false )
240 241
    add_integer_with_range( "qt-updates-days", 3, 0, 180,
              UPDATER_DAYS_TEXT, UPDATER_DAYS_TEXT, false )
242 243
#endif

244
#ifdef _WIN32
245 246 247 248 249 250 251
    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

252
    add_bool( "qt-embedded-open", false, QT_NATIVEOPEN_TEXT,
253
               QT_NATIVEOPEN_TEXT, false )
254

255

256
    add_bool( "qt-advanced-pref", false, ADVANCED_PREFS_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
257
              ADVANCED_PREFS_LONGTEXT, false )
258
    add_bool( "qt-error-dialogs", true, ERROR_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
259
              ERROR_TEXT, false )
260

261
    add_string( "qt-slider-colours", "153;210;153;20;210;20;255;199;15;245;39;29",
262
                SLIDERCOL_TEXT, SLIDERCOL_LONGTEXT, false )
263

264
    add_bool( "qt-privacy-ask", true, PRIVACY_TEXT, PRIVACY_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
265
              false )
266
        change_private ()
267

268
    add_integer( "qt-fullscreen-screennumber", -1, FULLSCREEN_NUMBER_TEXT,
269 270
               FULLSCREEN_NUMBER_LONGTEXT, false );

271
    add_bool( "qt-autoload-extensions", true,
272 273 274
              QT_AUTOLOAD_EXTENSIONS_TEXT, QT_AUTOLOAD_EXTENSIONS_LONGTEXT,
              false )

275 276 277 278
    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 )

279
    add_bool( "qt-icon-change", true, ICONCHANGE_TEXT, ICONCHANGE_LONGTEXT, true )
280

281 282
    add_integer_with_range( "qt-max-volume", 125, 60, 300, VOLUME_MAX_TEXT, VOLUME_MAX_TEXT, true)

283 284 285
    add_integer_with_range( "qt-fs-sensitivity", 3, 0, 4000, FULLSCREEN_CONTROL_PIXELS,
            FULLSCREEN_CONTROL_PIXELS, true)

286
    add_obsolete_bool( "qt-blingbling" )      /* Suppressed since 1.0.0 */
287
    add_obsolete_integer( "qt-display-mode" ) /* Suppressed since 1.1.0 */
288

289 290 291
    add_obsolete_bool( "qt-adv-options" )     /* Since 2.0.0 */
    add_obsolete_bool( "qt-volume-complete" ) /* Since 2.0.0 */
    add_obsolete_integer( "qt-startvolume" )  /* Since 2.0.0 */
292

293
    cannot_unload_broken_library()
294

295 296 297
    add_submodule ()
        set_description( "Dialogs provider" )
        set_capability( "dialogs provider", 51 )
298

299
        set_callbacks( OpenDialogs, Close )
300

301
#if defined (Q_WS_X11) || (defined (Q_WS_QPA) && defined (__unix__))
302
    add_submodule ()
303
        set_capability( "vout window xid", 0 )
304 305
        set_callbacks( WindowOpen, WindowClose )
#endif
306
#if (defined (Q_OS_WIN) && !defined (_WIN32_X11_)) || defined (Q_OS_OS2)
307
    add_submodule ()
308
        set_capability( "vout window hwnd", 0 )
309
        set_callbacks( WindowOpen, WindowClose )
310
#endif
311
#if defined (Q_OS_DARWIN)
312 313
    add_submodule ()
        set_capability( "vout window nsobject", 0 )
314
        set_callbacks( WindowOpen, WindowClose )
315
#endif
316

317
vlc_module_end ()
Clément Stenac's avatar
Clément Stenac committed
318

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
319 320
/*****************************************/

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
321
/* Ugly, but the Qt interface assumes single instance anyway */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
322
static vlc_sem_t ready;
323 324 325
static QMutex lock;
static bool busy = false;
static bool active = false;
326

Clément Stenac's avatar
Clément Stenac committed
327 328 329
/*****************************************************************************
 * Module callbacks
 *****************************************************************************/
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
330

331
#ifdef Q_OS_MAC
332
/* Used to abort the app.exec() on OSX after libvlc_Quit is called */
333
#include "../../../lib/libvlc_internal.h" /* libvlc_SetExitHandler */
334 335 336 337 338 339
static void Abort( void *obj )
{
    QVLCApp::triggerQuit();
}
#endif

340
static void RegisterIntf( intf_thread_t *p_this )
341
{
342
    playlist_t *pl = p_this->p_sys->p_playlist;
343 344 345 346 347 348
    var_Create (pl, "qt4-iface", VLC_VAR_ADDRESS);
    var_SetAddress (pl, "qt4-iface", p_this);
    var_Create (pl, "window", VLC_VAR_STRING);
    var_SetString (pl, "window", "qt4,any");
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
349
/* Open Interface */
350
static int Open( vlc_object_t *p_this, bool isDialogProvider )
Clément Stenac's avatar
Clément Stenac committed
351 352
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
353

354
#ifdef Q_WS_X11
355
    if( !vlc_xlib_init( p_this ) )
356
        return VLC_EGENERIC;
357

358
    Display *p_display = XOpenDisplay( NULL );
Rafaël Carré's avatar
Rafaël Carré committed
359 360
    if( !p_display )
    {
361
        msg_Err( p_intf, "Could not connect to X server" );
362 363
        return VLC_EGENERIC;
    }
364
    XCloseDisplay( p_display );
365
#endif
366

367
    QMutexLocker locker (&lock);
368
    if (busy)
369
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
370
        msg_Err (p_this, "cannot start Qt multiple times");
371 372 373
        return VLC_EGENERIC;
    }

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
374 375
    /* Allocations of p_sys */
    intf_sys_t *p_sys = p_intf->p_sys = new intf_sys_t;
376
    p_intf->p_sys->b_isDialogProvider = isDialogProvider;
377
    p_sys->p_mi = NULL;
378
    p_sys->pl_model = NULL;
379

380 381 382 383 384 385
    /* set up the playlist to work on */
    if( isDialogProvider )
        p_intf->p_sys->p_playlist = pl_Get( (intf_thread_t *)p_intf->p_parent );
    else
        p_intf->p_sys->p_playlist = pl_Get( p_intf );

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
386
    /* */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
387
    vlc_sem_init (&ready, 0);
388
#ifdef Q_OS_MAC
389 390 391 392
    /* Run mainloop on the main thread as Cocoa requires */
    libvlc_SetExitHandler( p_intf->p_libvlc, Abort, p_intf );
    Thread( (void *)p_intf );
#else
393
    if( vlc_clone( &p_sys->thread, Thread, p_intf, VLC_THREAD_PRIORITY_LOW ) )
394 395
    {
        delete p_sys;
Christophe Mutricy's avatar
Christophe Mutricy committed
396
        return VLC_ENOMEM;
397
    }
398
#endif
399

400 401 402
    /* 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. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
403 404
    vlc_sem_wait (&ready);
    vlc_sem_destroy (&ready);
405
    busy = active = true;
406

407
#ifndef Q_OS_MAC
408
    if( !isDialogProvider )
409
        RegisterIntf( p_intf );
410 411
#endif

Clément Stenac's avatar
Clément Stenac committed
412 413 414
    return VLC_SUCCESS;
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
415
/* Open Qt interface */
416 417 418 419 420
static int OpenIntf( vlc_object_t *p_this )
{
    return Open( p_this, false );
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
421
/* Open Dialog Provider */
Clément Stenac's avatar
Clément Stenac committed
422 423
static int OpenDialogs( vlc_object_t *p_this )
{
424
    return Open( p_this, true );
Clément Stenac's avatar
Clément Stenac committed
425 426 427 428 429
}

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

432
    if( !p_sys->b_isDialogProvider )
433
    {
434
        playlist_t *pl = THEPL;
435 436 437 438

        var_Destroy (pl, "window");
        var_Destroy (pl, "qt4-iface");
        playlist_Deactivate (pl); /* release window provider if needed */
439
    }
440

441
    /* And quit */
442
    msg_Dbg( p_this, "requesting exit..." );
443
    QVLCApp::triggerQuit();
444

445
    msg_Dbg( p_this, "waiting for UI thread..." );
446
#ifndef Q_OS_MAC
447
    vlc_join (p_sys->thread, NULL);
448
#endif
449
    delete p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
450

451 452 453
    QMutexLocker locker (&lock);
    assert (busy);
    busy = false;
Clément Stenac's avatar
Clément Stenac committed
454 455
}

456
static void *Thread( void *obj )
Clément Stenac's avatar
Clément Stenac committed
457
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
458
    intf_thread_t *p_intf = (intf_thread_t *)obj;
459
    MainInterface *p_mi;
460
    char dummy[] = "vlc"; /* for WM_CLASS */
461 462
    char *argv[] = { dummy, NULL, };
    int argc = sizeof(argv) / sizeof(argv[0]) - 1;
463

Rafaël Carré's avatar
Rafaël Carré committed
464
    Q_INIT_RESOURCE( vlc );
465 466

    /* Start the QApplication here */
467
    QVLCApp app( argc, argv );
468

469
    p_intf->p_sys->p_app = &app;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
470

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
471 472

    /* All the settings are in the .conf/.ini style */
473
    p_intf->p_sys->mainSettings = new QSettings(
474
#ifdef _WIN32
475 476 477 478 479 480
            QSettings::IniFormat,
#else
            QSettings::NativeFormat,
#endif
            QSettings::UserScope, "vlc", "vlc-qt-interface" );

481
    /* Icon setting, Mac uses icon from .icns */
482
#ifndef Q_OS_MAC
483
    if( QDate::currentDate().dayOfYear() >= QT_XMAS_JOKE_DAY && var_InheritBool( p_intf, "qt-icon-change" ) )
484
        app.setWindowIcon( QIcon::fromTheme( "vlc-xmas", QIcon( ":/logo/vlc128-xmas.png" ) ) );
485
    else
486
        app.setWindowIcon( QIcon::fromTheme( "vlc", QIcon( ":/logo/vlc256.png" ) ) );
487
#endif
Rafaël Carré's avatar
Rafaël Carré committed
488

489
    /* Initialize the Dialog Provider and the Main Input Manager */
490
    DialogsProvider::getInstance( p_intf );
491
    MainInputManager::getInstance( p_intf );
Clément Stenac's avatar
Clément Stenac committed
492

493 494
#ifdef UPDATE_CHECK
    /* Checking for VLC updates */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
495 496
    if( var_InheritBool( p_intf, "qt-updates-notif" ) &&
        !var_InheritBool( p_intf, "qt-privacy-ask" ) )
497
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
498
        int interval = var_InheritInteger( p_intf, "qt-updates-days" );
499 500 501 502 503 504 505 506 507 508
        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
509
    /* Create the normal interface in non-DP mode */
510
    if( !p_intf->p_sys->b_isDialogProvider )
511
    {
512
        p_mi = new MainInterface( p_intf );
513 514
        p_intf->p_sys->p_mi = p_mi;
    }
515
    else
516
        p_mi = NULL;
517

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

521 522
    /* Check window type from the Qt platform back-end */
    p_intf->p_sys->voutWindowType = VOUT_WINDOW_TYPE_INVALID;
523
#if defined (Q_WS_QPA) || HAS_QT5
524 525 526
    QString platform = app.platformName();
    if( platform == qfu("xcb") )
        p_intf->p_sys->voutWindowType = VOUT_WINDOW_TYPE_XID;
527 528 529 530
    else if( platform == qfu("windows") )
        p_intf->p_sys->voutWindowType = VOUT_WINDOW_TYPE_HWND;
    else if( platform == qfu("cocoa" ) )
        p_intf->p_sys->voutWindowType = VOUT_WINDOW_TYPE_NSOBJECT;
531 532 533 534 535 536 537 538 539 540
    else
        msg_Err( p_intf, "unknown Qt platform: %s", qtu(platform) );
#elif defined (Q_WS_X11)
    p_intf->p_sys->voutWindowType = VOUT_WINDOW_TYPE_XID;
#elif defined (Q_WS_WIN) || defined (Q_WS_PM)
    p_intf->p_sys->voutWindowType = VOUT_WINDOW_TYPE_HWND;
#elif defined (Q_WS_MAC)
    p_intf->p_sys->voutWindowType = VOUT_WINDOW_TYPE_NSOBJECT;
#endif

541
    /* Tell the main LibVLC thread we are ready */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
542
    vlc_sem_post (&ready);
543

544
#ifdef Q_OS_MAC
545 546 547
    /* We took over main thread, register and start here */
    if( !p_intf->p_sys->b_isDialogProvider )
    {
548
        RegisterIntf( p_intf );
549
        playlist_Play( THEPL );
550 551 552
    }
#endif

553
    /* Last settings */
554
    app.setQuitOnLastWindowClosed( false );
555

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
556
    /* Retrieve last known path used in file browsing */
557
    p_intf->p_sys->filepath =
558
         getSettings()->value( "filedialog-path", QVLCUserDir( VLC_HOME_DIR ) ).toString();
559

560 561 562 563 564
    /* 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 );

565
    /* Launch */
566
    app.exec();
567

568
    msg_Dbg( p_intf, "QApp exec() finished" );
569
    if (p_mi != NULL)
570
    {
571 572
        QMutexLocker locker (&lock);
        active = false;
573

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
574
        p_intf->p_sys->p_mi = NULL;
575 576
        /* Destroy first the main interface because it is connected to some
           slots in the MainInputManager */
577
        delete p_mi;
578
    }
579

580 581
    /* */
    ExtensionsManager::killInstance();
582
    AddonsManager::killInstance();
583

584 585 586 587 588
    /* Destroy all remaining windows,
       because some are connected to some slots
       in the MainInputManager
       Settings must be destroyed after that.
     */
Clément Stenac's avatar
Clément Stenac committed
589
    DialogsProvider::killInstance();
590

Ludovic Fauvet's avatar
Ludovic Fauvet committed
591 592 593
    /* Delete the recentsMRL object before the configuration */
    RecentsMRL::killInstance();

594 595 596 597 598
    /* Save the path or delete if recent play are disabled */
    if( var_InheritBool( p_intf, "qt-recentplay" ) )
        getSettings()->setValue( "filedialog-path", p_intf->p_sys->filepath );
    else
        getSettings()->remove( "filedialog-path" );
599

600 601 602
    /* */
    delete p_intf->p_sys->pl_model;

603 604 605
    /* Delete the configuration. Application has to be deleted after that. */
    delete p_intf->p_sys->mainSettings;

606 607 608
    /* Destroy the MainInputManager */
    MainInputManager::killInstance();

609
    /* Delete the application automatically */
610
    return NULL;
Clément Stenac's avatar
Clément Stenac committed
611 612 613 614 615 616 617 618
}

/*****************************************************************************
 * 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 )
{
619
    VLC_UNUSED( p_intf );
Clément Stenac's avatar
Clément Stenac committed
620
    DialogEvent *event = new DialogEvent( i_dialog_event, i_arg, p_arg );
621
    QApplication::postEvent( THEDP, event );
Clément Stenac's avatar
Clément Stenac committed
622
}
623

624 625
/**
 * Video output window provider
626 627
 *
 * TODO move it out of here ?
628
 */
629
static int WindowControl( vout_window_t *, int i_query, va_list );
630

631
static int WindowOpen( vout_window_t *p_wnd, const vout_window_cfg_t *cfg )
632
{
633
    if( cfg->is_standalone )
634 635
        return VLC_EGENERIC;

636 637
    intf_thread_t *p_intf =
        (intf_thread_t *)var_InheritAddress( p_wnd, "qt4-iface" );
638
    if( !p_intf )
639
    {   /* If another interface is used, this plugin cannot work */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
640
        msg_Dbg( p_wnd, "Qt interface not found" );
641 642
        return VLC_EGENERIC;
    }
643 644 645 646 647 648 649 650 651
    if( p_intf->p_sys->voutWindowType != cfg->type )
        return VLC_EGENERIC;
    switch( cfg->type )
    {
        case VOUT_WINDOW_TYPE_XID:
            if( var_InheritBool( p_wnd, "video-wallpaper" ) )
                return VLC_EGENERIC;
            break;
    }
652

653 654 655 656
    QMutexLocker locker (&lock);
    if (unlikely(!active))
        return VLC_EGENERIC;

657
    MainInterface *p_mi = p_intf->p_sys->p_mi;
658
    msg_Dbg( p_wnd, "requesting video window..." );
659

660 661 662 663
    int i_x = cfg->x;
    int i_y = cfg->y;
    unsigned i_width = cfg->width;
    unsigned i_height = cfg->height;
664

665 666
    WId wid = p_mi->getVideo( &i_x, &i_y, &i_width, &i_height );
    if( !wid )
667
        return VLC_EGENERIC;
668 669 670 671

    switch( cfg->type )
    {
        case VOUT_WINDOW_TYPE_XID:
672
            p_wnd->handle.xid = (uintptr_t)wid;
673 674 675 676 677 678 679 680 681 682
            p_wnd->display.x11 = NULL;
            break;
        case VOUT_WINDOW_TYPE_HWND:
            p_wnd->handle.hwnd = (void *)wid;
            break;
        case VOUT_WINDOW_TYPE_NSOBJECT:
            p_wnd->handle.nsobject = (void *)wid;
            break;
    }

683 684
    p_wnd->control = WindowControl;
    p_wnd->sys = (vout_window_sys_t*)p_mi;
685 686 687
    return VLC_SUCCESS;
}

688
static int WindowControl( vout_window_t *p_wnd, int i_query, va_list args )
689
{
690
    MainInterface *p_mi = (MainInterface *)p_wnd->sys;
691 692 693 694 695 696 697
    QMutexLocker locker (&lock);

    if (unlikely(!active))
    {
        msg_Warn (p_wnd, "video already released before control");
        return VLC_EGENERIC;
    }
698
    return p_mi->controlVideo( i_query, args );
699 700
}

701
static void WindowClose( vout_window_t *p_wnd )
702
{
703
    MainInterface *p_mi = (MainInterface *)p_wnd->sys;
704 705 706
    QMutexLocker locker (&lock);

    /* Normally, the interface terminates after the video. In the contrary, the
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
707 708 709
     * Qt main loop is gone, so we cannot send any event to the user interface
     * widgets. Ideally, we would keep the Qt main loop running until after
     * the video window is released. But it is far simpler to just have the Qt
710 711 712 713 714 715 716
     * thread destroy the window early, and to turn this function into a stub.
     *
     * That assumes the video output will behave sanely if it window is
     * destroyed asynchronously.
     * XCB and Xlib-XCB are fine with that. Plain Xlib wouldn't, */
    if (unlikely(!active))
    {
717
        msg_Warn (p_wnd, "video already released");
718 719
        return;
    }
720
    msg_Dbg (p_wnd, "releasing video...");
721
    p_mi->releaseVideo();
722
}