qt.cpp 27 KB
Newer Older
Clément Stenac's avatar
Clément Stenac committed
1
/*****************************************************************************
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>
34

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

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

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 57 58 59 60 61 62

 #ifdef QT_STATICPLUGIN
  Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
  Q_IMPORT_PLUGIN(QSvgIconPlugin)
  Q_IMPORT_PLUGIN(QSvgPlugin)
  #if !HAS_QT56
   Q_IMPORT_PLUGIN(AccessibleFactory)
63
  #endif
64
 #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 * );
Clément Stenac's avatar
Clément Stenac committed
76 77 78 79 80
static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );

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

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

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

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

101 102
#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 " \
103
                           "controller window title." )
104

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

189 190 191 192
#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 " \
    "when a video/audio playback starts, or never" )

193 194
#define FULLSCREEN_CONTROL_PIXELS N_( "Fullscreen controller mouse sensitivity" )

195 196
#define CONTINUE_PLAYBACK_TEXT N_("Continue playback?")

197 198 199 200 201 202
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") };

203 204 205 206 207 208
static const int i_continue_list[] =
    { 0, 1, 2 };

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

209 210 211 212 213 214
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[] =
    { N_( "Never" ), N_( "Video" ), N_( "Audio" ), _( "Both" ) };
215

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
216
/**********************************************************************/
217 218 219
vlc_module_begin ()
    set_shortname( "Qt" )
    set_description( N_("Qt interface") )
220
    set_category( CAT_INTERFACE )
221 222
    set_subcategory( SUBCAT_INTERFACE_MAIN )
    set_capability( "interface", 151 )
223
    set_callbacks( OpenIntf, Close )
224 225

    add_shortcut("qt")
226

227
    add_bool( "qt-minimal-view", false, QT_MINIMAL_MODE_TEXT,
228
              QT_MINIMAL_MODE_TEXT, false );
229

230
    add_bool( "qt-system-tray", true, SYSTRAY_TEXT, SYSTRAY_LONGTEXT, false)
231 232 233 234 235 236

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

237 238
    add_bool( "qt-start-minimized", false, MINIMIZED_TEXT,
              MINIMIZED_LONGTEXT, true)
239
    add_bool( "qt-pause-minimized", false, QT_PAUSE_MINIMIZED_TEXT,
240
              QT_PAUSE_MINIMIZED_LONGTEXT, false )
241

242
    add_float_with_range( "qt-opacity", 1., 0.1, 1., OPACITY_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
243
                          OPACITY_LONGTEXT, false )
244
    add_float_with_range( "qt-fs-opacity", 0.8, 0.1, 1., OPACITY_FS_TEXT,
245
                          OPACITY_FS_LONGTEXT, false )
246

247
    add_bool( "qt-video-autoresize", true, KEEPSIZE_TEXT,
248
              KEEPSIZE_LONGTEXT, false )
249
    add_bool( "qt-name-in-title", true, TITLE_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
250
              TITLE_LONGTEXT, false )
251
    add_bool( "qt-fs-controller", true, QT_FULLSCREEN_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
252
              QT_FULLSCREEN_TEXT, false )
253

254 255 256 257
    add_bool( "qt-recentplay", true, RECENTPLAY_TEXT,
              RECENTPLAY_TEXT, false )
    add_string( "qt-recentplay-filter", "",
                RECENTPLAY_FILTER_TEXT, RECENTPLAY_FILTER_LONGTEXT, false )
258 259
    add_integer( "qt-continue", 1, CONTINUE_PLAYBACK_TEXT, CONTINUE_PLAYBACK_TEXT, false )
            change_integer_list(i_continue_list, psz_continue_list_text )
260 261 262 263

#ifdef UPDATE_CHECK
    add_bool( "qt-updates-notif", true, UPDATER_TEXT,
              UPDATER_LONGTEXT, false )
264 265
    add_integer_with_range( "qt-updates-days", 3, 0, 180,
              UPDATER_DAYS_TEXT, UPDATER_DAYS_TEXT, false )
266 267
#endif

268
#ifdef _WIN32
269 270 271 272 273 274 275
    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

276
    add_bool( "qt-embedded-open", false, QT_NATIVEOPEN_TEXT,
277
               QT_NATIVEOPEN_TEXT, false )
278

279

280
    add_bool( "qt-advanced-pref", false, ADVANCED_PREFS_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
281
              ADVANCED_PREFS_LONGTEXT, false )
282
    add_bool( "qt-error-dialogs", true, ERROR_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
283
              ERROR_TEXT, false )
284

285
    add_string( "qt-slider-colours", "153;210;153;20;210;20;255;199;15;245;39;29",
286
                SLIDERCOL_TEXT, SLIDERCOL_LONGTEXT, false )
287

288
    add_bool( "qt-privacy-ask", true, PRIVACY_TEXT, PRIVACY_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
289
              false )
290
        change_private ()
291

292
    add_integer( "qt-fullscreen-screennumber", -1, FULLSCREEN_NUMBER_TEXT,
293 294
               FULLSCREEN_NUMBER_LONGTEXT, false );

295
    add_bool( "qt-autoload-extensions", true,
296 297 298
              QT_AUTOLOAD_EXTENSIONS_TEXT, QT_AUTOLOAD_EXTENSIONS_LONGTEXT,
              false )

299 300 301 302
    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 )

303
    add_bool( "qt-icon-change", true, ICONCHANGE_TEXT, ICONCHANGE_LONGTEXT, true )
304

305 306
    add_integer_with_range( "qt-max-volume", 125, 60, 300, VOLUME_MAX_TEXT, VOLUME_MAX_TEXT, true)

307 308 309
    add_integer_with_range( "qt-fs-sensitivity", 3, 0, 4000, FULLSCREEN_CONTROL_PIXELS,
            FULLSCREEN_CONTROL_PIXELS, true)

310
    add_obsolete_bool( "qt-blingbling" )      /* Suppressed since 1.0.0 */
311
    add_obsolete_integer( "qt-display-mode" ) /* Suppressed since 1.1.0 */
312

313 314 315
    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 */
316

317 318 319 320
    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 )

321
    cannot_unload_broken_library()
322

323 324 325
    add_submodule ()
        set_description( "Dialogs provider" )
        set_capability( "dialogs provider", 51 )
326

327
        set_callbacks( OpenDialogs, Close )
328

329
    add_submodule ()
330
        set_capability( "vout window", 0 )
331
        set_callbacks( WindowOpen, WindowClose )
332

333
vlc_module_end ()
Clément Stenac's avatar
Clément Stenac committed
334

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
335 336
/*****************************************/

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
337
/* Ugly, but the Qt interface assumes single instance anyway */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
338
static vlc_sem_t ready;
339 340 341
static QMutex lock;
static bool busy = false;
static bool active = false;
342

Clément Stenac's avatar
Clément Stenac committed
343 344 345
/*****************************************************************************
 * Module callbacks
 *****************************************************************************/
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
346

347 348 349 350 351 352 353
static void *ThreadPlatform( void *, char * );

static void *Thread( void *data )
{
    return ThreadPlatform( data, NULL );
}

354
#ifdef Q_OS_MAC
355
/* Used to abort the app.exec() on OSX after libvlc_Quit is called */
356
#include "../../../lib/libvlc_internal.h" /* libvlc_SetExitHandler */
357 358 359 360 361 362
static void Abort( void *obj )
{
    QVLCApp::triggerQuit();
}
#endif

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
#if defined (QT5_HAS_X11)
# include <vlc_xlib.h>

static void *ThreadXCB( void *data )
{
    char platform_name[] = "xcb";
    return ThreadPlatform( data, platform_name );
}

static bool HasX11( vlc_object_t *obj )
{
    if( !vlc_xlib_init( obj ) )
        return false;

    Display *dpy = XOpenDisplay( NULL );
    if( dpy == NULL )
        return false;

    XCloseDisplay( dpy );
    return true;
}
#endif

#ifdef QT5_HAS_WAYLAND
# include <wayland-client.h>

static void *ThreadWayland( void *data )
{
    char platform_name[] = "wayland";
    return ThreadPlatform( data, platform_name );
}

static bool HasWayland( void )
{
    struct wl_display *dpy = wl_display_connect( NULL );
    if( dpy == NULL )
        return false;

    wl_display_disconnect( dpy );
    return true;
}
#endif

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
406
/* Open Interface */
407
static int Open( vlc_object_t *p_this, bool isDialogProvider )
Clément Stenac's avatar
Clément Stenac committed
408 409
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
410
    void *(*thread)(void *) = Thread;
411

412 413 414 415 416 417 418 419 420 421 422
#ifdef QT5_HAS_WAYLAND
    if( HasWayland() )
        thread = ThreadWayland;
    else
#endif
#ifdef QT5_HAS_X11
    if( HasX11( p_this ) )
        thread = ThreadXCB;
    else
#endif
#if defined (QT5_HAS_X11) || defined (QT5_HAS_WAYLAND)
423
        return VLC_EGENERIC;
424
#endif
425

426
    QMutexLocker locker (&lock);
427
    if (busy)
428
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
429
        msg_Err (p_this, "cannot start Qt multiple times");
430 431 432
        return VLC_EGENERIC;
    }

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
433 434
    /* Allocations of p_sys */
    intf_sys_t *p_sys = p_intf->p_sys = new intf_sys_t;
435
    p_sys->b_isDialogProvider = isDialogProvider;
436
    p_sys->p_mi = NULL;
437
    p_sys->pl_model = NULL;
438

439 440
    /* set up the playlist to work on */
    if( isDialogProvider )
441
        p_sys->p_playlist = pl_Get( (intf_thread_t *)p_intf->obj.parent );
442
    else
443
        p_sys->p_playlist = pl_Get( p_intf );
444

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
445
    /* */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
446
    vlc_sem_init (&ready, 0);
447
#ifdef Q_OS_MAC
448
    /* Run mainloop on the main thread as Cocoa requires */
449
    libvlc_SetExitHandler( p_intf->obj.libvlc, Abort, p_intf );
450
    thread( (void *)p_intf );
451
#else
452
    if( vlc_clone( &p_sys->thread, thread, p_intf, VLC_THREAD_PRIORITY_LOW ) )
453 454
    {
        delete p_sys;
Christophe Mutricy's avatar
Christophe Mutricy committed
455
        return VLC_ENOMEM;
456
    }
457
#endif
458

459 460 461
    /* 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
462 463
    vlc_sem_wait (&ready);
    vlc_sem_destroy (&ready);
464
    busy = active = true;
465

Clément Stenac's avatar
Clément Stenac committed
466 467 468
    return VLC_SUCCESS;
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
469
/* Open Qt interface */
470 471 472 473 474
static int OpenIntf( vlc_object_t *p_this )
{
    return Open( p_this, false );
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
475
/* Open Dialog Provider */
Clément Stenac's avatar
Clément Stenac committed
476 477
static int OpenDialogs( vlc_object_t *p_this )
{
478
    return Open( p_this, true );
Clément Stenac's avatar
Clément Stenac committed
479 480 481 482 483
}

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

486
    if( !p_sys->b_isDialogProvider )
487
    {
488
        playlist_t *pl = THEPL;
489 490

        playlist_Deactivate (pl); /* release window provider if needed */
491
    }
492

493
    /* And quit */
494
    msg_Dbg( p_this, "requesting exit..." );
495
    QVLCApp::triggerQuit();
496

497
    msg_Dbg( p_this, "waiting for UI thread..." );
498
#ifndef Q_OS_MAC
499
    vlc_join (p_sys->thread, NULL);
500
#endif
501
    delete p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
502

503 504 505
    QMutexLocker locker (&lock);
    assert (busy);
    busy = false;
Clément Stenac's avatar
Clément Stenac committed
506 507
}

508
static void *ThreadPlatform( void *obj, char *platform_name )
Clément Stenac's avatar
Clément Stenac committed
509
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
510
    intf_thread_t *p_intf = (intf_thread_t *)obj;
511
    intf_sys_t *p_sys = p_intf->p_sys;
512 513
    char vlc_name[] = "vlc"; /* for WM_CLASS */
    char platform_parm[] = "-platform";
514 515 516 517 518 519 520 521 522 523
    char *argv[4];
    int argc = 0;

    argv[argc++] = vlc_name;
    if( platform_name != NULL )
    {
        argv[argc++] = platform_parm;
        argv[argc++] = platform_name;
    }
    argv[argc] = NULL;
524

Rafaël Carré's avatar
Rafaël Carré committed
525
    Q_INIT_RESOURCE( vlc );
526

527 528
#if HAS_QT56
    QApplication::setAttribute( Qt::AA_EnableHighDpiScaling );
529
    QApplication::setAttribute( Qt::AA_UseHighDpiPixmaps );
530 531
#endif

532
    /* Start the QApplication here */
533
    QVLCApp app( argc, argv );
534

Fahad Al-Saidi's avatar
Fahad Al-Saidi committed
535 536 537 538
    /* Set application direction to locale direction,
     * necessary for  RTL locales */
    app.setLayoutDirection(QLocale().textDirection());

539
    p_sys->p_app = &app;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
540

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
541 542

    /* All the settings are in the .conf/.ini style */
Hannes Domani's avatar
Hannes Domani committed
543 544 545 546 547 548 549 550 551
#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

552
    p_sys->mainSettings = new QSettings(
553
#ifdef _WIN32
554 555 556 557 558 559
            QSettings::IniFormat,
#else
            QSettings::NativeFormat,
#endif
            QSettings::UserScope, "vlc", "vlc-qt-interface" );

560
    /* Icon setting, Mac uses icon from .icns */
561
#ifndef Q_OS_MAC
562
    if( QDate::currentDate().dayOfYear() >= QT_XMAS_JOKE_DAY && var_InheritBool( p_intf, "qt-icon-change" ) )
563
        app.setWindowIcon( QIcon::fromTheme( "vlc-xmas", QIcon( ":/logo/vlc128-xmas.png" ) ) );
564
    else
565
        app.setWindowIcon( QIcon::fromTheme( "vlc", QIcon( ":/logo/vlc256.png" ) ) );
566
#endif
Rafaël Carré's avatar
Rafaël Carré committed
567

568
    /* Initialize the Dialog Provider and the Main Input Manager */
569
    DialogsProvider::getInstance( p_intf );
570 571
    MainInputManager* mim = MainInputManager::getInstance( p_intf );
    mim->probeCurrentInput();
Clément Stenac's avatar
Clément Stenac committed
572

573 574
#ifdef UPDATE_CHECK
    /* Checking for VLC updates */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
575 576
    if( var_InheritBool( p_intf, "qt-updates-notif" ) &&
        !var_InheritBool( p_intf, "qt-privacy-ask" ) )
577
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
578
        int interval = var_InheritInteger( p_intf, "qt-updates-days" );
579 580 581 582 583 584 585 586 587 588
        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
589
    /* Create the normal interface in non-DP mode */
590 591 592
    MainInterface *p_mi = NULL;

    if( !p_sys->b_isDialogProvider )
593
    {
594 595 596
#ifdef _WIN32
        p_mi = new MainInterfaceWin32( p_intf );
#else
597
        p_mi = new MainInterface( p_intf );
598
#endif
599
        p_sys->p_mi = p_mi;
600

601 602 603 604 605
        /* Check window type from the Qt platform back-end */
        p_sys->voutWindowType = VOUT_WINDOW_TYPE_INVALID;
        QString platform = app.platformName();
        if( platform == qfu("xcb") )
            p_sys->voutWindowType = VOUT_WINDOW_TYPE_XID;
606 607
        else if( platform == qfu("wayland") )
            p_sys->voutWindowType = VOUT_WINDOW_TYPE_WAYLAND;
608 609 610 611 612 613
        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
            msg_Err( p_intf, "unknown Qt platform: %s", qtu(platform) );
614

615 616 617 618 619 620 621 622 623 624
        var_Create( THEPL, "qt4-iface", VLC_VAR_ADDRESS );
        var_SetAddress( THEPL, "qt4-iface", p_intf );
        var_Create( THEPL, "window", VLC_VAR_STRING );
        if( p_sys->voutWindowType != VOUT_WINDOW_TYPE_INVALID )
            var_SetString( THEPL, "window", "qt,any" );
    }

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

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

628
#ifdef Q_OS_MAC
629
    /* We took over main thread, register and start here */
630
    if( !p_sys->b_isDialogProvider )
631
        playlist_Play( THEPL );
632 633
#endif

634
    /* Last settings */
635
    app.setQuitOnLastWindowClosed( false );
636

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

641 642 643 644 645
    /* 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 );

646
    /* Launch */
647
    app.exec();
648

649
    msg_Dbg( p_intf, "QApp exec() finished" );
650
    if (p_mi != NULL)
651
    {
652 653 654
        var_Destroy( THEPL, "window" );
        var_Destroy( THEPL, "qt4-iface" );

655 656
        QMutexLocker locker (&lock);
        active = false;
657

658
        p_sys->p_mi = NULL;
659 660
        /* Destroy first the main interface because it is connected to some
           slots in the MainInputManager */
661
        delete p_mi;
662
    }
663

664 665
    /* */
    ExtensionsManager::killInstance();
666
    AddonsManager::killInstance();
667

668 669 670 671 672
    /* 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
673
    DialogsProvider::killInstance();
674

Ludovic Fauvet's avatar
Ludovic Fauvet committed
675 676 677
    /* Delete the recentsMRL object before the configuration */
    RecentsMRL::killInstance();

678 679
    /* Save the path or delete if recent play are disabled */
    if( var_InheritBool( p_intf, "qt-recentplay" ) )
680
        getSettings()->setValue( "filedialog-path", p_sys->filepath );
681 682
    else
        getSettings()->remove( "filedialog-path" );
683

684
    /* */
685
    delete p_sys->pl_model;
686

687 688 689
    /* Destroy the MainInputManager */
    MainInputManager::killInstance();

690
    /* Delete the configuration. Application has to be deleted after that. */
691
    delete p_sys->mainSettings;
692

693
    /* Delete the application automatically */
694
    return NULL;
Clément Stenac's avatar
Clément Stenac committed
695 696 697 698 699 700 701 702
}

/*****************************************************************************
 * 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 )
{
703
    VLC_UNUSED( p_intf );
Clément Stenac's avatar
Clément Stenac committed
704
    DialogEvent *event = new DialogEvent( i_dialog_event, i_arg, p_arg );
705
    QApplication::postEvent( THEDP, event );
Clément Stenac's avatar
Clément Stenac committed
706
}
707

708 709
/**
 * Video output window provider
710 711
 *
 * TODO move it out of here ?
712
 */
713
static int WindowControl( vout_window_t *, int i_query, va_list );
714

715
static int WindowOpen( vout_window_t *p_wnd, const vout_window_cfg_t *cfg )
716
{
717
    if( cfg->is_standalone )
718 719
        return VLC_EGENERIC;

720 721
    intf_thread_t *p_intf =
        (intf_thread_t *)var_InheritAddress( p_wnd, "qt4-iface" );
722
    if( !p_intf )
723
    {   /* If another interface is used, this plugin cannot work */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
724
        msg_Dbg( p_wnd, "Qt interface not found" );
725 726
        return VLC_EGENERIC;
    }
727 728 729

    if( cfg->type != VOUT_WINDOW_TYPE_INVALID
     && cfg->type != p_intf->p_sys->voutWindowType )
730
        return VLC_EGENERIC;
731 732

    switch( p_intf->p_sys->voutWindowType )
733 734 735 736 737 738
    {
        case VOUT_WINDOW_TYPE_XID:
            if( var_InheritBool( p_wnd, "video-wallpaper" ) )
                return VLC_EGENERIC;
            break;
    }
739

740 741 742 743
    QMutexLocker locker (&lock);
    if (unlikely(!active))
        return VLC_EGENERIC;

744
    MainInterface *p_mi = p_intf->p_sys->p_mi;
745
    msg_Dbg( p_wnd, "requesting video window..." );
746

747
    if( !p_mi->getVideo( p_wnd, cfg->width, cfg->height, cfg->is_fullscreen ) )
748
        return VLC_EGENERIC;
749

750
    p_wnd->info.has_double_click = true;
751 752
    p_wnd->control = WindowControl;
    p_wnd->sys = (vout_window_sys_t*)p_mi;
753 754 755
    return VLC_SUCCESS;
}

756
static int WindowControl( vout_window_t *p_wnd, int i_query, va_list args )
757
{
758
    MainInterface *p_mi = (MainInterface *)p_wnd->sys;
759 760 761 762 763 764 765
    QMutexLocker locker (&lock);

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

769
static void WindowClose( vout_window_t *p_wnd )
770
{
771
    MainInterface *p_mi = (MainInterface *)p_wnd->sys;
772 773 774
    QMutexLocker locker (&lock);

    /* Normally, the interface terminates after the video. In the contrary, the
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
775 776 777
     * 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
778 779 780 781 782 783 784
     * 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))
    {
785
        msg_Warn (p_wnd, "video already released");
786 787
        return;
    }
788
    msg_Dbg (p_wnd, "releasing video...");
789
    p_mi->releaseVideo();
790
}