qt.cpp 26.2 KB
Newer Older
Clément Stenac's avatar
Clément Stenac committed
1
/*****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
2
 * qt.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 30
#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS

Clément Stenac's avatar
Clément Stenac committed
31
#include <QApplication>
32
#include <QDate>
33
#include <QMutex>
Clément Stenac's avatar
Clément Stenac committed
34

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
35
#include "qt.hpp"
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
36

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

47
#if defined (QT5_HAS_X11) || defined (Q_WS_X11)
48
 #include <vlc_xlib.h>
Rafaël Carré's avatar
Rafaël Carré committed
49 50
#endif

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

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

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

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
82
#define ADVANCED_PREFS_TEXT N_( "Show advanced preferences over simple ones" )
83 84
#define ADVANCED_PREFS_LONGTEXT N_( "Show advanced preferences and not simple "\
                                    "preferences when opening the preferences "\
85
                                    "dialog." )
86

87 88 89
#define SYSTRAY_TEXT N_( "Systray icon" )
#define SYSTRAY_LONGTEXT N_( "Show an icon in the systray " \
                             "allowing you to control VLC media player " \
90
                             "for basic actions." )
91

92
#define MINIMIZED_TEXT N_( "Start VLC with only a systray icon" )
93 94
#define MINIMIZED_LONGTEXT N_( "VLC will start with just an icon in " \
                               "your taskbar" )
95

96 97 98 99 100
#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." )
101

102 103
#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 " \
Kaarlo Raiha's avatar
Kaarlo Raiha committed
104
                           "controller window title." )
105

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

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

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


124
#define ERROR_TEXT N_( "Show unimportant error and warnings dialogs" )
125

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

132
#define PRIVACY_TEXT N_( "Ask for network policy at start" )
133

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

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

140 141 142 143
#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" \
144
            "An alternative can be '30;30;50;40;40;100;50;50;160;150;150;255' ")
145 146 147 148

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

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

156
#define FULLSCREEN_NUMBER_TEXT N_( "Define which screen fullscreen goes" )
Kaarlo Raiha's avatar
Kaarlo Raiha committed
157
#define FULLSCREEN_NUMBER_LONGTEXT N_( "Screennumber of fullscreen, instead of " \
158 159
                                       "same screen where interface is" )

160 161 162 163
#define QT_AUTOLOAD_EXTENSIONS_TEXT N_( "Load extensions on startup" )
#define QT_AUTOLOAD_EXTENSIONS_LONGTEXT N_( "Automatically load the "\
                                            "extensions module on startup" )

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

166 167
#define QT_BGCONE_TEXT N_( "Display background cone or art" )
#define QT_BGCONE_LONGTEXT N_( "Display background cone or current album art " \
168
                            "when not playing. " \
169 170 171
                            "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" )
172 173 174 175 176 177 178

#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." )
179 180 181 182 183

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

184 185 186 187
#define ICONCHANGE_TEXT N_( "Allow automatic icon changes")
#define ICONCHANGE_LONGTEXT N_( \
    "This option allows the interface to change its icon on various occasions.")

188 189
#define VOLUME_MAX_TEXT N_( "Maximum Volume displayed" )

190 191
#define FULLSCREEN_CONTROL_PIXELS N_( "Fullscreen controller mouse sensitivity" )

192 193
#define CONTINUE_PLAYBACK_TEXT N_("Continue playback?")

194 195 196 197 198 199
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") };

200 201 202 203 204 205 206
static const int i_continue_list[] =
    { 0, 1, 2 };

static const char *const psz_continue_list_text[] =
    { N_("Never"), N_("Ask"), N_("Always") };


Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
207
/**********************************************************************/
208 209 210
vlc_module_begin ()
    set_shortname( "Qt" )
    set_description( N_("Qt interface") )
211
    set_category( CAT_INTERFACE )
212 213
    set_subcategory( SUBCAT_INTERFACE_MAIN )
    set_capability( "interface", 151 )
214
    set_callbacks( OpenIntf, Close )
215 216

    add_shortcut("qt")
217

218
    add_bool( "qt-minimal-view", false, QT_MINIMAL_MODE_TEXT,
219
              QT_MINIMAL_MODE_TEXT, false );
220

221
    add_bool( "qt-system-tray", true, SYSTRAY_TEXT, SYSTRAY_LONGTEXT, false)
222 223 224 225 226 227

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

228 229
    add_bool( "qt-start-minimized", false, MINIMIZED_TEXT,
              MINIMIZED_LONGTEXT, true)
230
    add_bool( "qt-pause-minimized", false, QT_PAUSE_MINIMIZED_TEXT,
231
              QT_PAUSE_MINIMIZED_LONGTEXT, false )
232

233
    add_float_with_range( "qt-opacity", 1., 0.1, 1., OPACITY_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
234
                          OPACITY_LONGTEXT, false )
235
    add_float_with_range( "qt-fs-opacity", 0.8, 0.1, 1., OPACITY_FS_TEXT,
236
                          OPACITY_FS_LONGTEXT, false )
237

238
    add_bool( "qt-video-autoresize", true, KEEPSIZE_TEXT,
239
              KEEPSIZE_LONGTEXT, false )
240
    add_bool( "qt-name-in-title", true, TITLE_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
241
              TITLE_LONGTEXT, false )
242
    add_bool( "qt-fs-controller", true, QT_FULLSCREEN_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
243
              QT_FULLSCREEN_TEXT, false )
244

245 246 247 248
    add_bool( "qt-recentplay", true, RECENTPLAY_TEXT,
              RECENTPLAY_TEXT, false )
    add_string( "qt-recentplay-filter", "",
                RECENTPLAY_FILTER_TEXT, RECENTPLAY_FILTER_LONGTEXT, false )
249 250
    add_integer( "qt-continue", 1, CONTINUE_PLAYBACK_TEXT, CONTINUE_PLAYBACK_TEXT, false )
            change_integer_list(i_continue_list, psz_continue_list_text )
251 252 253 254

#ifdef UPDATE_CHECK
    add_bool( "qt-updates-notif", true, UPDATER_TEXT,
              UPDATER_LONGTEXT, false )
255 256
    add_integer_with_range( "qt-updates-days", 3, 0, 180,
              UPDATER_DAYS_TEXT, UPDATER_DAYS_TEXT, false )
257 258
#endif

259
#ifdef _WIN32
260 261 262 263 264 265 266
    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

267
    add_bool( "qt-embedded-open", false, QT_NATIVEOPEN_TEXT,
268
               QT_NATIVEOPEN_TEXT, false )
269

270

271
    add_bool( "qt-advanced-pref", false, ADVANCED_PREFS_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
272
              ADVANCED_PREFS_LONGTEXT, false )
273
    add_bool( "qt-error-dialogs", true, ERROR_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
274
              ERROR_TEXT, false )
275

276
    add_string( "qt-slider-colours", "153;210;153;20;210;20;255;199;15;245;39;29",
277
                SLIDERCOL_TEXT, SLIDERCOL_LONGTEXT, false )
278

279
    add_bool( "qt-privacy-ask", true, PRIVACY_TEXT, PRIVACY_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
280
              false )
281
        change_private ()
282

283
    add_integer( "qt-fullscreen-screennumber", -1, FULLSCREEN_NUMBER_TEXT,
284 285
               FULLSCREEN_NUMBER_LONGTEXT, false );

286
    add_bool( "qt-autoload-extensions", true,
287 288 289
              QT_AUTOLOAD_EXTENSIONS_TEXT, QT_AUTOLOAD_EXTENSIONS_LONGTEXT,
              false )

290 291 292 293
    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 )

294
    add_bool( "qt-icon-change", true, ICONCHANGE_TEXT, ICONCHANGE_LONGTEXT, true )
295

296 297
    add_integer_with_range( "qt-max-volume", 125, 60, 300, VOLUME_MAX_TEXT, VOLUME_MAX_TEXT, true)

298 299 300
    add_integer_with_range( "qt-fs-sensitivity", 3, 0, 4000, FULLSCREEN_CONTROL_PIXELS,
            FULLSCREEN_CONTROL_PIXELS, true)

301
    add_obsolete_bool( "qt-blingbling" )      /* Suppressed since 1.0.0 */
302
    add_obsolete_integer( "qt-display-mode" ) /* Suppressed since 1.1.0 */
303

304 305 306
    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 */
307

308
    cannot_unload_broken_library()
309

310 311 312
    add_submodule ()
        set_description( "Dialogs provider" )
        set_capability( "dialogs provider", 51 )
313

314
        set_callbacks( OpenDialogs, Close )
315

316
    add_submodule ()
317
        set_capability( "vout window", 0 )
318
        set_callbacks( WindowOpen, WindowClose )
319

320
vlc_module_end ()
Clément Stenac's avatar
Clément Stenac committed
321

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
322 323
/*****************************************/

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

Clément Stenac's avatar
Clément Stenac committed
330 331 332
/*****************************************************************************
 * Module callbacks
 *****************************************************************************/
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
333

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

343
static void RegisterIntf( intf_thread_t *p_this )
344
{
345
    playlist_t *pl = p_this->p_sys->p_playlist;
346 347 348 349 350 351
    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
352
/* Open Interface */
353
static int Open( vlc_object_t *p_this, bool isDialogProvider )
Clément Stenac's avatar
Clément Stenac committed
354 355
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
356

357
#if defined (QT5_HAS_X11) || defined (Q_WS_X11)
358
    if( !vlc_xlib_init( p_this ) )
359
        return VLC_EGENERIC;
360

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

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

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

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

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

410
#ifndef Q_OS_MAC
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
411
    if( !isDialogProvider )
412
        RegisterIntf( p_intf );
413 414
#endif

Clément Stenac's avatar
Clément Stenac committed
415 416 417
    return VLC_SUCCESS;
}

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

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

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

435
    if( !p_sys->b_isDialogProvider )
436
    {
437
        playlist_t *pl = THEPL;
438 439 440 441

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

444
    /* And quit */
445
    msg_Dbg( p_this, "requesting exit..." );
446
    QVLCApp::triggerQuit();
447

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

454 455 456
    QMutexLocker locker (&lock);
    assert (busy);
    busy = false;
Clément Stenac's avatar
Clément Stenac committed
457 458
}

459
static void *Thread( void *obj )
Clément Stenac's avatar
Clément Stenac committed
460
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
461
    intf_thread_t *p_intf = (intf_thread_t *)obj;
462
    MainInterface *p_mi;
463 464 465 466 467 468 469 470 471 472 473 474
    char vlc_name[] = "vlc"; /* for WM_CLASS */
#ifdef QT5_HAS_X11
    char platform_parm[] = "-platform";
    char platform_value[] = "xcb";
#endif
    char *argv[] = {
        vlc_name,
#ifdef QT5_HAS_X11
        platform_parm, platform_value,
#endif
        NULL,
    };
475
    int argc = sizeof(argv) / sizeof(argv[0]) - 1;
476

Rafaël Carré's avatar
Rafaël Carré committed
477
    Q_INIT_RESOURCE( vlc );
478 479

    /* Start the QApplication here */
480
    QVLCApp app( argc, argv );
481

Fahad Al-Saidi's avatar
Fahad Al-Saidi committed
482 483 484 485
    /* Set application direction to locale direction,
     * necessary for  RTL locales */
    app.setLayoutDirection(QLocale().textDirection());

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

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
488 489

    /* All the settings are in the .conf/.ini style */
Hannes Domani's avatar
Hannes Domani committed
490 491 492 493 494 495 496 497 498
#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

499
    p_intf->p_sys->mainSettings = new QSettings(
500
#ifdef _WIN32
501 502 503 504 505 506
            QSettings::IniFormat,
#else
            QSettings::NativeFormat,
#endif
            QSettings::UserScope, "vlc", "vlc-qt-interface" );

507
    /* Icon setting, Mac uses icon from .icns */
508
#ifndef Q_OS_MAC
509
    if( QDate::currentDate().dayOfYear() >= QT_XMAS_JOKE_DAY && var_InheritBool( p_intf, "qt-icon-change" ) )
510
        app.setWindowIcon( QIcon::fromTheme( "vlc-xmas", QIcon( ":/logo/vlc128-xmas.png" ) ) );
511
    else
512
        app.setWindowIcon( QIcon::fromTheme( "vlc", QIcon( ":/logo/vlc256.png" ) ) );
513
#endif
Rafaël Carré's avatar
Rafaël Carré committed
514

515
    /* Initialize the Dialog Provider and the Main Input Manager */
516
    DialogsProvider::getInstance( p_intf );
517 518
    MainInputManager* mim = MainInputManager::getInstance( p_intf );
    mim->probeCurrentInput();
Clément Stenac's avatar
Clément Stenac committed
519

520 521
#ifdef UPDATE_CHECK
    /* Checking for VLC updates */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
522 523
    if( var_InheritBool( p_intf, "qt-updates-notif" ) &&
        !var_InheritBool( p_intf, "qt-privacy-ask" ) )
524
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
525
        int interval = var_InheritInteger( p_intf, "qt-updates-days" );
526 527 528 529 530 531 532 533 534 535
        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
536
    /* Create the normal interface in non-DP mode */
537
    if( !p_intf->p_sys->b_isDialogProvider )
538
    {
539
        p_mi = new MainInterface( p_intf );
540 541
        p_intf->p_sys->p_mi = p_mi;
    }
542
    else
543
        p_mi = NULL;
544

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

548 549
    /* Check window type from the Qt platform back-end */
    p_intf->p_sys->voutWindowType = VOUT_WINDOW_TYPE_INVALID;
550
#if HAS_QT5 || defined (Q_WS_QPA)
551 552 553
    QString platform = app.platformName();
    if( platform == qfu("xcb") )
        p_intf->p_sys->voutWindowType = VOUT_WINDOW_TYPE_XID;
554 555 556 557
    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;
558 559 560 561 562 563 564 565 566 567
    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

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

571
#ifdef Q_OS_MAC
572 573 574
    /* We took over main thread, register and start here */
    if( !p_intf->p_sys->b_isDialogProvider )
    {
575
        RegisterIntf( p_intf );
576
        playlist_Play( THEPL );
577 578 579
    }
#endif

580
    /* Last settings */
581
    app.setQuitOnLastWindowClosed( false );
582

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

587 588 589 590 591
    /* 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 );

592
    /* Launch */
593
    app.exec();
594

595
    msg_Dbg( p_intf, "QApp exec() finished" );
596
    if (p_mi != NULL)
597
    {
598 599
        QMutexLocker locker (&lock);
        active = false;
600

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
601
        p_intf->p_sys->p_mi = NULL;
602 603
        /* Destroy first the main interface because it is connected to some
           slots in the MainInputManager */
604
        delete p_mi;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
605
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
606

607 608
    /* */
    ExtensionsManager::killInstance();
609
    AddonsManager::killInstance();
610

611 612 613 614 615
    /* 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
616
    DialogsProvider::killInstance();
617

Ludovic Fauvet's avatar
Ludovic Fauvet committed
618 619 620
    /* Delete the recentsMRL object before the configuration */
    RecentsMRL::killInstance();

621 622 623 624 625
    /* 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" );
626

627 628 629
    /* */
    delete p_intf->p_sys->pl_model;

630 631 632
    /* Delete the configuration. Application has to be deleted after that. */
    delete p_intf->p_sys->mainSettings;

633 634 635
    /* Destroy the MainInputManager */
    MainInputManager::killInstance();

636
    /* Delete the application automatically */
637
    return NULL;
Clément Stenac's avatar
Clément Stenac committed
638 639 640 641 642 643 644 645
}

/*****************************************************************************
 * 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
646
    VLC_UNUSED( p_intf );
Clément Stenac's avatar
Clément Stenac committed
647
    DialogEvent *event = new DialogEvent( i_dialog_event, i_arg, p_arg );
648
    QApplication::postEvent( THEDP, event );
Clément Stenac's avatar
Clément Stenac committed
649
}
650

651 652
/**
 * Video output window provider
653 654
 *
 * TODO move it out of here ?
655
 */
656
static int WindowControl( vout_window_t *, int i_query, va_list );
657

658
static int WindowOpen( vout_window_t *p_wnd, const vout_window_cfg_t *cfg )
659
{
660
    if( cfg->is_standalone )
661 662
        return VLC_EGENERIC;

663 664
    intf_thread_t *p_intf =
        (intf_thread_t *)var_InheritAddress( p_wnd, "qt4-iface" );
665
    if( !p_intf )
666
    {   /* If another interface is used, this plugin cannot work */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
667
        msg_Dbg( p_wnd, "Qt interface not found" );
668 669
        return VLC_EGENERIC;
    }
670 671 672

    if( cfg->type != VOUT_WINDOW_TYPE_INVALID
     && cfg->type != p_intf->p_sys->voutWindowType )
673
        return VLC_EGENERIC;
674 675

    switch( p_intf->p_sys->voutWindowType )
676 677 678 679 680 681
    {
        case VOUT_WINDOW_TYPE_XID:
            if( var_InheritBool( p_wnd, "video-wallpaper" ) )
                return VLC_EGENERIC;
            break;
    }
682

683 684 685 686
    QMutexLocker locker (&lock);
    if (unlikely(!active))
        return VLC_EGENERIC;

687
    MainInterface *p_mi = p_intf->p_sys->p_mi;
688
    msg_Dbg( p_wnd, "requesting video window..." );
689

690 691
    unsigned i_width = cfg->width;
    unsigned i_height = cfg->height;
692

693
    WId wid = p_mi->getVideo( p_wnd, &i_width, &i_height, cfg->is_fullscreen );
694
    if( !wid )
695
        return VLC_EGENERIC;
696

697 698 699
    p_wnd->type = p_intf->p_sys->voutWindowType;

    switch( p_wnd->type )
700 701
    {
        case VOUT_WINDOW_TYPE_XID:
702
            p_wnd->handle.xid = (uintptr_t)wid;
703 704 705 706 707 708 709 710
            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;
711
        default:
712
            vlc_assert_unreachable();
713 714
    }

715 716
    p_wnd->control = WindowControl;
    p_wnd->sys = (vout_window_sys_t*)p_mi;
717 718 719
    return VLC_SUCCESS;
}

720
static int WindowControl( vout_window_t *p_wnd, int i_query, va_list args )
721
{
722
    MainInterface *p_mi = (MainInterface *)p_wnd->sys;
723 724 725 726 727 728 729
    QMutexLocker locker (&lock);

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

733
static void WindowClose( vout_window_t *p_wnd )
734
{
735
    MainInterface *p_mi = (MainInterface *)p_wnd->sys;
736 737 738
    QMutexLocker locker (&lock);

    /* Normally, the interface terminates after the video. In the contrary, the
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
739 740 741
     * 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
742 743 744 745 746 747 748
     * 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))
    {
749
        msg_Warn (p_wnd, "video already released");
750 751
        return;
    }
752
    msg_Dbg (p_wnd, "releasing video...");
753
    p_mi->releaseVideo();
754
}