| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the plugins of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qoffscreenintegration.h" |
| 41 | #include "qoffscreenwindow.h" |
| 42 | #include "qoffscreencommon.h" |
| 43 | |
| 44 | #if defined(Q_OS_UNIX) |
| 45 | #include <QtGui/private/qgenericunixeventdispatcher_p.h> |
| 46 | #if defined(Q_OS_MAC) |
| 47 | #include <qpa/qplatformfontdatabase.h> |
| 48 | #include <QtGui/private/qcoretextfontdatabase_p.h> |
| 49 | #else |
| 50 | #include <QtGui/private/qgenericunixfontdatabase_p.h> |
| 51 | #endif |
| 52 | #elif defined(Q_OS_WIN) |
| 53 | #include <QtGui/private/qfreetypefontdatabase_p.h> |
| 54 | #include <QtCore/private/qeventdispatcher_win_p.h> |
| 55 | #endif |
| 56 | |
| 57 | #include <QtCore/qfile.h> |
| 58 | #include <QtCore/qjsonarray.h> |
| 59 | #include <QtCore/qjsondocument.h> |
| 60 | #include <QtCore/qjsonobject.h> |
| 61 | #include <QtCore/qjsonvalue.h> |
| 62 | #include <QtGui/private/qpixmap_raster_p.h> |
| 63 | #include <QtGui/private/qguiapplication_p.h> |
| 64 | #include <qpa/qplatforminputcontextfactory_p.h> |
| 65 | #include <qpa/qplatforminputcontext.h> |
| 66 | #include <qpa/qplatformtheme.h> |
| 67 | #include <qpa/qwindowsysteminterface.h> |
| 68 | |
| 69 | #include <qpa/qplatformservices.h> |
| 70 | |
| 71 | #if QT_CONFIG(xlib) && QT_CONFIG(opengl) && !QT_CONFIG(opengles2) |
| 72 | #include "qoffscreenintegration_x11.h" |
| 73 | #endif |
| 74 | |
| 75 | QT_BEGIN_NAMESPACE |
| 76 | |
| 77 | class QCoreTextFontEngine; |
| 78 | |
| 79 | template <typename BaseEventDispatcher> |
| 80 | class QOffscreenEventDispatcher : public BaseEventDispatcher |
| 81 | { |
| 82 | public: |
| 83 | explicit QOffscreenEventDispatcher(QObject *parent = nullptr) |
| 84 | : BaseEventDispatcher(parent) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | bool processEvents(QEventLoop::ProcessEventsFlags flags) override |
| 89 | { |
| 90 | bool didSendEvents = BaseEventDispatcher::processEvents(flags); |
| 91 | |
| 92 | return QWindowSystemInterface::sendWindowSystemEvents(flags) || didSendEvents; |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | QOffscreenIntegration::QOffscreenIntegration() |
| 97 | { |
| 98 | #if defined(Q_OS_UNIX) |
| 99 | #if defined(Q_OS_MAC) |
| 100 | m_fontDatabase.reset(new QCoreTextFontDatabaseEngineFactory<QCoreTextFontEngine>); |
| 101 | #else |
| 102 | m_fontDatabase.reset(new QGenericUnixFontDatabase()); |
| 103 | #endif |
| 104 | #elif defined(Q_OS_WIN) |
| 105 | m_fontDatabase.reset(new QFreeTypeFontDatabase()); |
| 106 | #endif |
| 107 | |
| 108 | #if QT_CONFIG(draganddrop) |
| 109 | m_drag.reset(new QOffscreenDrag); |
| 110 | #endif |
| 111 | m_services.reset(new QPlatformServices); |
| 112 | } |
| 113 | |
| 114 | QOffscreenIntegration::~QOffscreenIntegration() |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | The offscren platform plugin is configurable with a JSON configuration |
| 120 | file. Write the config to disk and pass the file path as a platform argument: |
| 121 | |
| 122 | ./myapp -platform offscreen:configfile=/path/to/config.json |
| 123 | |
| 124 | The supported top-level config keys are: |
| 125 | { |
| 126 | "synchronousWindowSystemEvents": <bool> |
| 127 | "windowFrameMargins": <bool>, |
| 128 | "screens": [<screens>], |
| 129 | } |
| 130 | |
| 131 | Screen: |
| 132 | { |
| 133 | "name" : string, |
| 134 | "x": int, |
| 135 | "y": int, |
| 136 | "width": int, |
| 137 | "height": int, |
| 138 | "logicalDpi": int, |
| 139 | "logicalBaseDpi": int, |
| 140 | "dpr": double, |
| 141 | } |
| 142 | */ |
| 143 | void QOffscreenIntegration::configure(const QStringList& paramList) |
| 144 | { |
| 145 | // Use config file configuring platform plugin, if one was specified |
| 146 | bool hasConfigFile = false; |
| 147 | QString configFilePath; |
| 148 | for (const QString ¶m : paramList) { |
| 149 | // Look for "configfile=/path/to/file/" |
| 150 | QString configPrefix(QLatin1String("configfile=" )); |
| 151 | if (param.startsWith(configPrefix)) { |
| 152 | hasConfigFile = true; |
| 153 | configFilePath= param.mid(configPrefix.length()); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Create the default screen if there was no config file |
| 158 | if (!hasConfigFile) { |
| 159 | QOffscreenScreen *offscreenScreen = new QOffscreenScreen(this); |
| 160 | m_screens.append(offscreenScreen); |
| 161 | QWindowSystemInterface::handleScreenAdded(offscreenScreen); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | // Read config file |
| 166 | if (configFilePath.isEmpty()) |
| 167 | qFatal("Missing file path for -configfile platform option" ); |
| 168 | QFile configFile(configFilePath); |
| 169 | if (!configFile.exists()) |
| 170 | qFatal("Could not find platform config file %s" , qPrintable(configFilePath)); |
| 171 | if (!configFile.open(QIODevice::ReadOnly)) |
| 172 | qFatal("Could not open platform config file for reading %s, %s" , qPrintable(configFilePath), qPrintable(configFile.errorString())); |
| 173 | |
| 174 | QByteArray json = configFile.readAll(); |
| 175 | QJsonParseError error; |
| 176 | QJsonDocument config = QJsonDocument::fromJson(json, &error); |
| 177 | if (config.isNull()) |
| 178 | qFatal("Platform config file parse error: %s" , qPrintable(error.errorString())); |
| 179 | |
| 180 | // Apply configuration (create screens) |
| 181 | bool synchronousWindowSystemEvents = config["synchronousWindowSystemEvents" ].toBool(false); |
| 182 | QWindowSystemInterface::setSynchronousWindowSystemEvents(synchronousWindowSystemEvents); |
| 183 | m_windowFrameMarginsEnabled = config["windowFrameMargins" ].toBool(true); |
| 184 | QJsonArray screens = config["screens" ].toArray(); |
| 185 | for (QJsonValue screenValue : screens) { |
| 186 | QJsonObject screen = screenValue.toObject(); |
| 187 | if (screen.isEmpty()) { |
| 188 | qWarning("QOffscreenIntegration::initializeWithPlatformArguments: empty screen object" ); |
| 189 | continue; |
| 190 | } |
| 191 | QOffscreenScreen *offscreenScreen = new QOffscreenScreen(this); |
| 192 | offscreenScreen->m_name = screen["name" ].toString(); |
| 193 | offscreenScreen->m_geometry = QRect(screen["x" ].toInt(0), screen["y" ].toInt(0), |
| 194 | screen["width" ].toInt(640), screen["height" ].toInt(480)); |
| 195 | offscreenScreen->m_logicalDpi = screen["logicalDpi" ].toInt(96); |
| 196 | offscreenScreen->m_logicalBaseDpi = screen["logicalBaseDpi" ].toInt(96); |
| 197 | offscreenScreen->m_dpr = screen["dpr" ].toDouble(1.0); |
| 198 | |
| 199 | m_screens.append(offscreenScreen); |
| 200 | QWindowSystemInterface::handleScreenAdded(offscreenScreen); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void QOffscreenIntegration::initialize() |
| 205 | { |
| 206 | m_inputContext.reset(QPlatformInputContextFactory::create()); |
| 207 | } |
| 208 | |
| 209 | QPlatformInputContext *QOffscreenIntegration::inputContext() const |
| 210 | { |
| 211 | return m_inputContext.data(); |
| 212 | } |
| 213 | |
| 214 | bool QOffscreenIntegration::hasCapability(QPlatformIntegration::Capability cap) const |
| 215 | { |
| 216 | switch (cap) { |
| 217 | case ThreadedPixmaps: return true; |
| 218 | case MultipleWindows: return true; |
| 219 | default: return QPlatformIntegration::hasCapability(cap); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | QPlatformWindow *QOffscreenIntegration::createPlatformWindow(QWindow *window) const |
| 224 | { |
| 225 | Q_UNUSED(window); |
| 226 | QPlatformWindow *w = new QOffscreenWindow(window, m_windowFrameMarginsEnabled); |
| 227 | w->requestActivateWindow(); |
| 228 | return w; |
| 229 | } |
| 230 | |
| 231 | QPlatformBackingStore *QOffscreenIntegration::createPlatformBackingStore(QWindow *window) const |
| 232 | { |
| 233 | return new QOffscreenBackingStore(window); |
| 234 | } |
| 235 | |
| 236 | QAbstractEventDispatcher *QOffscreenIntegration::createEventDispatcher() const |
| 237 | { |
| 238 | #if defined(Q_OS_UNIX) |
| 239 | return createUnixEventDispatcher(); |
| 240 | #elif defined(Q_OS_WIN) |
| 241 | return new QOffscreenEventDispatcher<QEventDispatcherWin32>(); |
| 242 | #else |
| 243 | return 0; |
| 244 | #endif |
| 245 | } |
| 246 | |
| 247 | static QString themeName() { return QStringLiteral("offscreen" ); } |
| 248 | |
| 249 | QStringList QOffscreenIntegration::themeNames() const |
| 250 | { |
| 251 | return QStringList(themeName()); |
| 252 | } |
| 253 | |
| 254 | // Restrict the styles to "fusion" to prevent native styles requiring native |
| 255 | // window handles (eg Windows Vista style) from being used. |
| 256 | class OffscreenTheme : public QPlatformTheme |
| 257 | { |
| 258 | public: |
| 259 | OffscreenTheme() {} |
| 260 | |
| 261 | QVariant themeHint(ThemeHint h) const override |
| 262 | { |
| 263 | switch (h) { |
| 264 | case StyleNames: |
| 265 | return QVariant(QStringList(QStringLiteral("Fusion" ))); |
| 266 | default: |
| 267 | break; |
| 268 | } |
| 269 | return QPlatformTheme::themeHint(h); |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | QPlatformTheme *QOffscreenIntegration::createPlatformTheme(const QString &name) const |
| 274 | { |
| 275 | return name == themeName() ? new OffscreenTheme() : nullptr; |
| 276 | } |
| 277 | |
| 278 | QPlatformFontDatabase *QOffscreenIntegration::fontDatabase() const |
| 279 | { |
| 280 | return m_fontDatabase.data(); |
| 281 | } |
| 282 | |
| 283 | #if QT_CONFIG(draganddrop) |
| 284 | QPlatformDrag *QOffscreenIntegration::drag() const |
| 285 | { |
| 286 | return m_drag.data(); |
| 287 | } |
| 288 | #endif |
| 289 | |
| 290 | QPlatformServices *QOffscreenIntegration::services() const |
| 291 | { |
| 292 | return m_services.data(); |
| 293 | } |
| 294 | |
| 295 | QOffscreenIntegration *QOffscreenIntegration::createOffscreenIntegration(const QStringList& paramList) |
| 296 | { |
| 297 | QOffscreenIntegration *offscreenIntegration = nullptr; |
| 298 | |
| 299 | #if QT_CONFIG(xlib) && QT_CONFIG(opengl) && !QT_CONFIG(opengles2) |
| 300 | QByteArray glx = qgetenv("QT_QPA_OFFSCREEN_NO_GLX" ); |
| 301 | if (glx.isEmpty()) |
| 302 | offscreenIntegration = new QOffscreenX11Integration; |
| 303 | #else |
| 304 | offscreenIntegration = new QOffscreenIntegration; |
| 305 | #endif |
| 306 | |
| 307 | offscreenIntegration->configure(paramList); |
| 308 | return offscreenIntegration; |
| 309 | } |
| 310 | |
| 311 | QList<QPlatformScreen *> QOffscreenIntegration::screens() const |
| 312 | { |
| 313 | return m_screens; |
| 314 | } |
| 315 | |
| 316 | QT_END_NAMESPACE |
| 317 | |