qt.cpp 27 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
#include <vlc_plugin.h>
48
#include <vlc_vout_window.h>
Clément Stenac's avatar
Clément Stenac committed
49

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

Clément Stenac's avatar
Clément Stenac committed
65 66 67
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
68
static int  OpenIntf     ( vlc_object_t * );
Clément Stenac's avatar
Clément Stenac committed
69
static int  OpenDialogs  ( vlc_object_t * );
70 71
static int  Open         ( vlc_object_t *, bool );
static void Close        ( vlc_object_t * );
72 73
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
74 75 76 77 78
static void ShowDialog   ( intf_thread_t *, int, int, intf_dialog_args_t * );

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

189 190
#define CONTINUE_PLAYBACK_TEXT N_("Continue playback?")

191 192 193 194 195 196
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") };

197 198 199 200 201 202 203
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
204
/**********************************************************************/
205 206 207
vlc_module_begin ()
    set_shortname( "Qt" )
    set_description( N_("Qt interface") )
208
    set_category( CAT_INTERFACE )
209 210
    set_subcategory( SUBCAT_INTERFACE_MAIN )
    set_capability( "interface", 151 )
211
    set_callbacks( OpenIntf, Close )
212 213

    add_shortcut("qt")
214

215
    add_bool( "qt-minimal-view", false, QT_MINIMAL_MODE_TEXT,
216
              QT_MINIMAL_MODE_TEXT, false );
217

218
    add_bool( "qt-system-tray", true, SYSTRAY_TEXT, SYSTRAY_LONGTEXT, false)
219 220 221 222 223 224

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

225 226
    add_bool( "qt-start-minimized", false, MINIMIZED_TEXT,
              MINIMIZED_LONGTEXT, true)
227
    add_bool( "qt-pause-minimized", false, QT_PAUSE_MINIMIZED_TEXT,
228
              QT_PAUSE_MINIMIZED_LONGTEXT, false )
229

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

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

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

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

256
#ifdef _WIN32
257 258 259 260 261 262 263
    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

264
    add_bool( "qt-embedded-open", false, QT_NATIVEOPEN_TEXT,
265
               QT_NATIVEOPEN_TEXT, false )
266

267

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

273
    add_string( "qt-slider-colours", "153;210;153;20;210;20;255;199;15;245;39;29",
274
                SLIDERCOL_TEXT, SLIDERCOL_LONGTEXT, false )
275

276
    add_bool( "qt-privacy-ask", true, PRIVACY_TEXT, PRIVACY_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
277
              false )
278
        change_private ()
279

280
    add_integer( "qt-fullscreen-screennumber", -1, FULLSCREEN_NUMBER_TEXT,
281 282
               FULLSCREEN_NUMBER_LONGTEXT, false );

283
    add_bool( "qt-autoload-extensions", true,
284 285 286
              QT_AUTOLOAD_EXTENSIONS_TEXT, QT_AUTOLOAD_EXTENSIONS_LONGTEXT,
              false )

287 288 289 290
    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 )

291
    add_bool( "qt-icon-change", true, ICONCHANGE_TEXT, ICONCHANGE_LONGTEXT, true )
292

293 294
    add_integer_with_range( "qt-max-volume", 125, 60, 300, VOLUME_MAX_TEXT, VOLUME_MAX_TEXT, true)

295 296 297
    add_integer_with_range( "qt-fs-sensitivity", 3, 0, 4000, FULLSCREEN_CONTROL_PIXELS,
            FULLSCREEN_CONTROL_PIXELS, true)

298
    add_obsolete_bool( "qt-blingbling" )      /* Suppressed since 1.0.0 */
299
    add_obsolete_integer( "qt-display-mode" ) /* Suppressed since 1.1.0 */
300

301 302 303
    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 */
304

305
    cannot_unload_broken_library()
306

307 308 309
    add_submodule ()
        set_description( "Dialogs provider" )
        set_capability( "dialogs provider", 51 )
310

311
        set_callbacks( OpenDialogs, Close )
312

313
    add_submodule ()
314
        set_capability( "vout window", 0 )
315
        set_callbacks( WindowOpen, WindowClose )
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 332 333 334 335 336 337
static void *ThreadPlatform( void *, char * );

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

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

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 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
#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

390
static void RegisterIntf( intf_thread_t *p_this )
391
{
392
    playlist_t *pl = p_this->p_sys->p_playlist;
393 394 395
    var_Create (pl, "qt4-iface", VLC_VAR_ADDRESS);
    var_SetAddress (pl, "qt4-iface", p_this);
    var_Create (pl, "window", VLC_VAR_STRING);
396
    var_SetString (pl, "window", "qt,any");
397 398
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
399
/* Open Interface */
400
static int Open( vlc_object_t *p_this, bool isDialogProvider )
Clément Stenac's avatar
Clément Stenac committed
401 402
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
403
    void *(*thread)(void *) = Thread;
404

405 406 407 408 409 410 411 412 413 414 415
#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)
416
        return VLC_EGENERIC;
417 418 419
#endif
#ifdef Q_WS_X11
    if( !HasX11( p_this ) )
420 421
        return VLC_EGENERIC;
#endif
422

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

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

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

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
456 457 458
    /* 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
459 460
    vlc_sem_wait (&ready);
    vlc_sem_destroy (&ready);
461
    busy = active = true;
462

463
#ifndef Q_OS_MAC
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
464
    if( !isDialogProvider )
465
        RegisterIntf( p_intf );
466 467
#endif

Clément Stenac's avatar
Clément Stenac committed
468 469 470
    return VLC_SUCCESS;
}

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

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

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

488
    if( !p_sys->b_isDialogProvider )
489
    {
490
        playlist_t *pl = THEPL;
491 492 493 494

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

497
    /* And quit */
498
    msg_Dbg( p_this, "requesting exit..." );
499
    QVLCApp::triggerQuit();
500

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

507 508 509
    QMutexLocker locker (&lock);
    assert (busy);
    busy = false;
Clément Stenac's avatar
Clément Stenac committed
510 511
}

512
static void *ThreadPlatform( void *obj, char *platform_name )
Clément Stenac's avatar
Clément Stenac committed
513
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
514
    intf_thread_t *p_intf = (intf_thread_t *)obj;
515
    intf_sys_t *p_sys = p_intf->p_sys;
516 517
    char vlc_name[] = "vlc"; /* for WM_CLASS */
    char platform_parm[] = "-platform";
518 519 520 521 522 523 524 525 526 527
    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;
528

Rafaël Carré's avatar
Rafaël Carré committed
529
    Q_INIT_RESOURCE( vlc );
530

531 532 533 534
#if HAS_QT56
    QApplication::setAttribute( Qt::AA_EnableHighDpiScaling );
#endif

535
    /* Start the QApplication here */
536
    QVLCApp app( argc, argv );
537

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

542
    p_sys->p_app = &app;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
543

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
544 545

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

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

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

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

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

    if( !p_sys->b_isDialogProvider )
596
    {
597
        p_mi = new MainInterface( p_intf );
598
        p_sys->p_mi = p_mi;
599
    }
600

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

604
    /* Check window type from the Qt platform back-end */
605
    p_sys->voutWindowType = VOUT_WINDOW_TYPE_INVALID;
606
#if HAS_QT5 || defined (Q_WS_QPA)
607 608
    QString platform = app.platformName();
    if( platform == qfu("xcb") )
609
        p_sys->voutWindowType = VOUT_WINDOW_TYPE_XID;
610
    else if( platform == qfu("windows") )
611
        p_sys->voutWindowType = VOUT_WINDOW_TYPE_HWND;
612
    else if( platform == qfu("cocoa" ) )
613
        p_sys->voutWindowType = VOUT_WINDOW_TYPE_NSOBJECT;
614 615 616
    else
        msg_Err( p_intf, "unknown Qt platform: %s", qtu(platform) );
#elif defined (Q_WS_X11)
617
    p_sys->voutWindowType = VOUT_WINDOW_TYPE_XID;
618
#elif defined (Q_WS_WIN) || defined (Q_WS_PM)
619
    p_sys->voutWindowType = VOUT_WINDOW_TYPE_HWND;
620
#elif defined (Q_WS_MAC)
621
    p_sys->voutWindowType = VOUT_WINDOW_TYPE_NSOBJECT;
622 623
#endif

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

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

636
    /* Last settings */
637
    app.setQuitOnLastWindowClosed( false );
638

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

643 644 645 646 647
    /* 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 );

648
    /* Launch */
649
    app.exec();
650

651
    msg_Dbg( p_intf, "QApp exec() finished" );
652
    if (p_mi != NULL)
653
    {
654 655
        QMutexLocker locker (&lock);
        active = false;
656

657
        p_sys->p_mi = NULL;
658 659
        /* Destroy first the main interface because it is connected to some
           slots in the MainInputManager */
660
        delete p_mi;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
661
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
662

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

746 747
    unsigned i_width = cfg->width;
    unsigned i_height = cfg->height;
748

749
    WId wid = p_mi->getVideo( p_wnd, &i_width, &i_height, cfg->is_fullscreen );
750
    if( !wid )
751
        return VLC_EGENERIC;
752

753 754 755
    p_wnd->type = p_intf->p_sys->voutWindowType;

    switch( p_wnd->type )
756 757
    {
        case VOUT_WINDOW_TYPE_XID:
758
            p_wnd->handle.xid = (uintptr_t)wid;
759 760 761 762 763 764 765 766
            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;
767
        default:
768
            vlc_assert_unreachable();
769 770
    }

771 772
    p_wnd->control = WindowControl;
    p_wnd->sys = (vout_window_sys_t*)p_mi;
773 774 775
    return VLC_SUCCESS;
}

776
static int WindowControl( vout_window_t *p_wnd, int i_query, va_list args )
777
{
778
    MainInterface *p_mi = (MainInterface *)p_wnd->sys;
779 780 781 782 783 784 785
    QMutexLocker locker (&lock);

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

789
static void WindowClose( vout_window_t *p_wnd )
790
{
791
    MainInterface *p_mi = (MainInterface *)p_wnd->sys;
792 793 794
    QMutexLocker locker (&lock);

    /* Normally, the interface terminates after the video. In the contrary, the
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
795 796 797
     * 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
798 799 800 801 802 803 804
     * 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))
    {
805
        msg_Warn (p_wnd, "video already released");
806 807
        return;
    }
808
    msg_Dbg (p_wnd, "releasing video...");
809
    p_mi->releaseVideo();
810
}