qt.cpp 26.2 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 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() */
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
  #ifdef QT_STATICPLUGIN
   Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
59 60 61
   #if !HAS_QT56
    Q_IMPORT_PLUGIN(AccessibleFactory)
   #endif
62
  #endif
63 64 65 66
 #else
  Q_IMPORT_PLUGIN(qjpeg)
  Q_IMPORT_PLUGIN(qtaccessiblewidgets)
 #endif
67 68
#endif

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

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

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

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

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

104 105
#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 " \
106
                           "controller window title." )
107

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

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

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


126
#define ERROR_TEXT N_( "Show unimportant error and warnings dialogs" )
127

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

134
#define PRIVACY_TEXT N_( "Ask for network policy at start" )
135

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

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

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

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

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

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

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

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

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

#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." )
181 182 183 184 185

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

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

190 191
#define VOLUME_MAX_TEXT N_( "Maximum Volume displayed" )

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

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

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

202 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") };


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

    add_shortcut("qt")
219

220
    add_bool( "qt-minimal-view", false, QT_MINIMAL_MODE_TEXT,
221
              QT_MINIMAL_MODE_TEXT, false );
222

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

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

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

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

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

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

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

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

269
    add_bool( "qt-embedded-open", false, QT_NATIVEOPEN_TEXT,
270
               QT_NATIVEOPEN_TEXT, false )
271

272

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

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

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

285
    add_integer( "qt-fullscreen-screennumber", -1, FULLSCREEN_NUMBER_TEXT,
286 287
               FULLSCREEN_NUMBER_LONGTEXT, false );

288
    add_bool( "qt-autoload-extensions", true,
289 290 291
              QT_AUTOLOAD_EXTENSIONS_TEXT, QT_AUTOLOAD_EXTENSIONS_LONGTEXT,
              false )

292 293 294 295
    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 )

296
    add_bool( "qt-icon-change", true, ICONCHANGE_TEXT, ICONCHANGE_LONGTEXT, true )
297

298 299
    add_integer_with_range( "qt-max-volume", 125, 60, 300, VOLUME_MAX_TEXT, VOLUME_MAX_TEXT, true)

300 301 302
    add_integer_with_range( "qt-fs-sensitivity", 3, 0, 4000, FULLSCREEN_CONTROL_PIXELS,
            FULLSCREEN_CONTROL_PIXELS, true)

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

306 307 308
    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 */
309

310
    cannot_unload_broken_library()
311

312 313 314
    add_submodule ()
        set_description( "Dialogs provider" )
        set_capability( "dialogs provider", 51 )
315

316
        set_callbacks( OpenDialogs, Close )
317

318
    add_submodule ()
319
        set_capability( "vout window", 0 )
320
        set_callbacks( WindowOpen, WindowClose )
321

322
vlc_module_end ()
Clément Stenac's avatar
Clément Stenac committed
323

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
324 325
/*****************************************/

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

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

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

345
static void RegisterIntf( intf_thread_t *p_this )
346
{
347
    playlist_t *pl = p_this->p_sys->p_playlist;
348 349 350
    var_Create (pl, "qt4-iface", VLC_VAR_ADDRESS);
    var_SetAddress (pl, "qt4-iface", p_this);
    var_Create (pl, "window", VLC_VAR_STRING);
351
    var_SetString (pl, "window", "qt,any");
352 353
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
354
/* Open Interface */
355
static int Open( vlc_object_t *p_this, bool isDialogProvider )
Clément Stenac's avatar
Clément Stenac committed
356 357
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
358

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

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

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

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

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

405 406 407
    /* 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
408 409
    vlc_sem_wait (&ready);
    vlc_sem_destroy (&ready);
410
    busy = active = true;
411

412
#ifndef Q_OS_MAC
413
    if( !isDialogProvider )
414
        RegisterIntf( p_intf );
415 416
#endif

Clément Stenac's avatar
Clément Stenac committed
417 418 419
    return VLC_SUCCESS;
}

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

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

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

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

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

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

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

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

461
static void *Thread( void *obj )
Clément Stenac's avatar
Clément Stenac committed
462
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
463
    intf_thread_t *p_intf = (intf_thread_t *)obj;
464
    MainInterface *p_mi;
465 466 467 468 469 470 471 472 473 474 475 476
    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,
    };
477
    int argc = sizeof(argv) / sizeof(argv[0]) - 1;
478

Rafaël Carré's avatar
Rafaël Carré committed
479
    Q_INIT_RESOURCE( vlc );
480 481

    /* Start the QApplication here */
482
    QVLCApp app( argc, argv );
483

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

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

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
490 491

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

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

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

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

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

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

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

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

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

582
    /* Last settings */
583
    app.setQuitOnLastWindowClosed( false );
584

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

589 590 591 592 593
    /* 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 );

594
    /* Launch */
595
    app.exec();
596

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

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

609 610
    /* */
    ExtensionsManager::killInstance();
611
    AddonsManager::killInstance();
612

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

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

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

629 630 631
    /* */
    delete p_intf->p_sys->pl_model;

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

635 636 637
    /* Destroy the MainInputManager */
    MainInputManager::killInstance();

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

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

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

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

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

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

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

685 686 687 688
    QMutexLocker locker (&lock);
    if (unlikely(!active))
        return VLC_EGENERIC;

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

692 693
    unsigned i_width = cfg->width;
    unsigned i_height = cfg->height;
694

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

699 700 701
    p_wnd->type = p_intf->p_sys->voutWindowType;

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

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

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

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

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

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