Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwasmintegration.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
8#include "qwasmtheme.h"
9#if QT_CONFIG(clipboard)
10#include "qwasmclipboard.h"
11#endif
13#include "qwasmservices.h"
15#include "qwasmplatform.h"
16#include "qwasmwindow.h"
19#if QT_CONFIG(draganddrop)
20#include "qwasmdrag.h"
21#endif
22
23#include <qpa/qplatformwindow.h>
24#include <QtGui/qscreen.h>
25#include <qpa/qwindowsysteminterface.h>
26#include <QtCore/qcoreapplication.h>
27#include <qpa/qplatforminputcontextfactory_p.h>
28#include <qpa/qwindowsysteminterface_p.h>
29#include "private/qwasmsuspendresumecontrol_p.h"
30
31#include <emscripten/bind.h>
32#include <emscripten/val.h>
33
34// this is where EGL headers are pulled in, make sure it is last
35#include "qwasmscreen.h"
36#if QT_CONFIG(draganddrop)
37#include <private/qsimpledrag_p.h>
38#endif
39
41
42extern void qt_set_sequence_auto_mnemonic(bool);
43
44using namespace emscripten;
45
46using namespace Qt::StringLiterals;
47
48static void setContainerElements(emscripten::val elementArray)
49{
51}
52
57
62
67
72
73static void resizeAllScreens(emscripten::val event)
74{
75 Q_UNUSED(event);
77}
78
83
85{
86 function("qtSetContainerElements", &setContainerElements);
87 function("qtAddContainerElement", &addContainerElement);
88 function("qtRemoveContainerElement", &removeContainerElement);
89 function("qtResizeContainerElement", &resizeContainerElement);
90 function("qtUpdateDpi", &qtUpdateDpi);
91 function("qtResizeAllScreens", &resizeAllScreens);
92 function("qtLoadLocalFontFamilies", &loadLocalFontFamilies);
93}
94
96
97QWasmIntegration::QWasmIntegration()
98 : m_suspendResume(std::make_shared<QWasmSuspendResumeControl>()) // create early in order to register event handlers at startup
99 , m_fontDb(nullptr)
100 , m_desktopServices(nullptr)
101#if QT_CONFIG(clipboard)
102 , m_clipboard(new QWasmClipboard)
103#endif
104#if QT_CONFIG(accessibility)
105 , m_accessibility(new QWasmAccessibility)
106#endif
107{
108 s_instance = this;
109
110 if (platform() == Platform::MacOS)
112
113 touchPoints = emscripten::val::global("navigator")["maxTouchPoints"].as<int>();
114 QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(false);
115
116 // Create screens for container elements. Each container element will ultimately become a
117 // div element. Qt historically supported supplying canvas for screen elements - these elements
118 // will be transformed into divs and warnings about deprecation will be printed. See
119 // QWasmScreen ctor.
120 emscripten::val filtered = emscripten::val::array();
121 emscripten::val qtContainerElements = val::module_property("qtContainerElements");
122 if (qtContainerElements.isArray()) {
123 for (int i = 0; i < qtContainerElements["length"].as<int>(); ++i) {
124 emscripten::val element = qtContainerElements[i].as<emscripten::val>();
125 if (element.isNull() || element.isUndefined())
126 qWarning() << "Skipping null or undefined element in qtContainerElements";
127 else
128 filtered.call<void>("push", element);
129 }
130 } else {
131 // No screens, which may or may not be intended
132 qWarning() << "The qtContainerElements module property was not set or is invalid. "
133 "Proceeding with no screens.";
134 }
136
137 // install browser window resize handler
138 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, EM_TRUE,
139 [](int, const EmscriptenUiEvent *, void *) -> EM_BOOL {
140 // This resize event is called when the HTML window is
141 // resized. Depending on the page layout the elements might
142 // also have been resized, so we update the Qt screen sizes
143 // (and canvas render sizes).
144 if (QWasmIntegration *integration = QWasmIntegration::get())
145 integration->resizeAllScreens();
146 return EM_FALSE;
147 });
148
149 // install visualViewport resize handler which picks up size and scale change on mobile.
150 emscripten::val visualViewport = emscripten::val::global("window")["visualViewport"];
151 if (!visualViewport.isUndefined()) {
152 visualViewport.call<void>("addEventListener", val("resize"),
153 val::module_property("qtResizeAllScreens"));
154 }
155#if QT_CONFIG(draganddrop)
156 m_drag = std::make_unique<QWasmDrag>();
157#endif
158}
159
161{
162 // Remove event listener
163 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, EM_TRUE, nullptr);
164 emscripten::val visualViewport = emscripten::val::global("window")["visualViewport"];
165 if (!visualViewport.isUndefined()) {
166 visualViewport.call<void>("removeEventListener", val("resize"),
167 val::module_property("qtResizeAllScreens"));
168 }
169
170 delete m_fontDb;
171 delete m_desktopServices;
172#if QT_CONFIG(accessibility)
173 delete m_accessibility;
174#endif
175
176 for (const auto &elementAndScreen : m_screens)
177 elementAndScreen.wasmScreen->deleteScreen();
178
179 m_screens.clear();
180
181 s_instance = nullptr;
182}
183
184bool QWasmIntegration::hasCapability(QPlatformIntegration::Capability cap) const
185{
186 switch (cap) {
187 case ThreadedPixmaps: return true;
188 case OpenGL: return true;
189 case ThreadedOpenGL: return false;
190 case MultipleWindows: return true;
191 case WindowManagement: return true;
192 case ForeignWindows: return true;
193 case OpenGLOnRasterSurface: return true;
194 default: return QPlatformIntegration::hasCapability(cap);
195 }
196}
197
198QWasmWindow *QWasmIntegration::createWindow(QWindow *window, WId nativeHandle) const
199{
200 auto *wasmScreen = QWasmScreen::get(window->screen());
201 QWasmCompositor *compositor = wasmScreen->compositor();
202 return new QWasmWindow(window, wasmScreen->deadKeySupport(), compositor,
203 m_backingStores.value(window), nativeHandle);
204}
205
207{
208 return createWindow(window, 0);
209}
210
211QPlatformWindow *QWasmIntegration::createForeignWindow(QWindow *window, WId nativeHandle) const
212{
213 return createWindow(window, nativeHandle);
214}
215
217{
218 QWasmCompositor *compositor = QWasmScreen::get(window->screen())->compositor();
219 QWasmBackingStore *backingStore = new QWasmBackingStore(compositor, window);
220 m_backingStores.insert(window, backingStore);
221 return backingStore;
222}
223
225{
226 m_backingStores.remove(window);
227}
228
230{
232 {
233 for (const auto &elementAndScreen : m_screens) {
234 elementAndScreen.wasmScreen->compositor()->requestUpdate();
235 }
236 }
237}
238
239#ifndef QT_NO_OPENGL
240QPlatformOpenGLContext *QWasmIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
241{
242 return new QWasmOpenGLContext(context);
243}
244#endif
245
247{
248 auto icStrs = QPlatformInputContextFactory::requested();
249 if (!icStrs.isEmpty()) {
250 m_inputContext.reset(QPlatformInputContextFactory::create(icStrs));
251 m_wasmInputContext = nullptr;
252 } else {
253 m_inputContext.reset(new QWasmInputContext());
254 m_wasmInputContext = static_cast<QWasmInputContext *>(m_inputContext.data());
255 }
256}
257
259{
260 return m_inputContext.data();
261}
262
263QPlatformOffscreenSurface *QWasmIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const
264{
265 return new QWasmOffscreenSurface(surface);
266}
267
269{
270 if (m_fontDb == nullptr)
271 m_fontDb = new QWasmFontDatabase;
272
273 return m_fontDb;
274}
275
277{
278 return new QWasmEventDispatcher(m_suspendResume);
279}
280
281QVariant QWasmIntegration::styleHint(QPlatformIntegration::StyleHint hint) const
282{
283 switch (hint) {
284 case ShowIsFullScreen:
285 return true;
286 case UnderlineShortcut:
287 return platform() != Platform::MacOS;
288 default:
289 return QPlatformIntegration::styleHint(hint);
290 }
291}
292
293Qt::WindowState QWasmIntegration::defaultWindowState(Qt::WindowFlags flags) const
294{
295 // Don't maximize dialogs or popups
296 if (flags.testFlag(Qt::Dialog) || flags.testFlag(Qt::Popup))
297 return Qt::WindowNoState;
298
299 return QPlatformIntegration::defaultWindowState(flags);
300}
301
303{
304 return QStringList() << "webassembly"_L1;
305}
306
308{
309 if (name == "webassembly"_L1)
310 return new QWasmTheme;
311 return QPlatformIntegration::createPlatformTheme(name);
312}
313
315{
316 if (m_desktopServices == nullptr)
317 m_desktopServices = new QWasmServices();
318 return m_desktopServices;
319}
320
321#if QT_CONFIG(clipboard)
322QPlatformClipboard* QWasmIntegration::clipboard() const
323{
324 return m_clipboard;
325}
326#endif
327
328#ifndef QT_NO_ACCESSIBILITY
330{
331 return m_accessibility;
332}
333#endif
334
336{
337 const auto *primaryScreenBefore = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
338 QList<ScreenMapping> newScreens;
339
340 QList<QWasmScreen *> screensToDelete;
341 std::transform(m_screens.begin(), m_screens.end(), std::back_inserter(screensToDelete),
342 [](const ScreenMapping &mapping) { return mapping.wasmScreen; });
343
344 for (int i = 0; i < elementArray["length"].as<int>(); ++i) {
345 const auto element = elementArray[i];
346 const auto it = std::find_if(
347 m_screens.begin(), m_screens.end(),
348 [&element](const ScreenMapping &screen) { return screen.emscriptenVal == element; });
349 QWasmScreen *screen;
350 if (it != m_screens.end()) {
351 screen = it->wasmScreen;
352 screensToDelete.erase(std::remove_if(screensToDelete.begin(), screensToDelete.end(),
353 [screen](const QWasmScreen *removedScreen) {
354 return removedScreen == screen;
355 }),
356 screensToDelete.end());
357 } else {
358 screen = new QWasmScreen(element);
359 QWindowSystemInterface::handleScreenAdded(screen);
360 }
361 newScreens.push_back({element, screen});
362 }
363
364 std::for_each(screensToDelete.begin(), screensToDelete.end(),
365 [](QWasmScreen *removed) { removed->deleteScreen(); });
366
367 m_screens = newScreens;
368 auto *primaryScreenAfter = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
369 if (primaryScreenAfter && primaryScreenAfter != primaryScreenBefore)
370 QWindowSystemInterface::handlePrimaryScreenChanged(primaryScreenAfter);
371}
372
374{
375 Q_ASSERT_X(m_screens.end()
376 == std::find_if(m_screens.begin(), m_screens.end(),
377 [&element](const ScreenMapping &screen) {
378 return screen.emscriptenVal == element;
379 }),
380 Q_FUNC_INFO, "Double-add of an element");
381
382 QWasmScreen *screen = new QWasmScreen(element);
383 QWindowSystemInterface::handleScreenAdded(screen);
384 m_screens.push_back({element, screen});
385}
386
388{
389 const auto *primaryScreenBefore = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
390
391 const auto it =
392 std::find_if(m_screens.begin(), m_screens.end(),
393 [&element](const ScreenMapping &screen) { return screen.emscriptenVal == element; });
394 if (it == m_screens.end()) {
395 qWarning() << "Attempt to remove a nonexistent screen.";
396 return;
397 }
398
399 QWasmScreen *removedScreen = it->wasmScreen;
400 removedScreen->deleteScreen();
401
402 m_screens.erase(std::remove_if(m_screens.begin(), m_screens.end(),
403 [removedScreen](const ScreenMapping &mapping) {
404 return removedScreen == mapping.wasmScreen;
405 }),
406 m_screens.end());
407 auto *primaryScreenAfter = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
408 if (primaryScreenAfter && primaryScreenAfter != primaryScreenBefore)
409 QWindowSystemInterface::handlePrimaryScreenChanged(primaryScreenAfter);
410}
411
412void QWasmIntegration::resizeScreen(const emscripten::val &element)
413{
414 auto it = std::find_if(m_screens.begin(), m_screens.end(),
415 [&] (const ScreenMapping &candidate) { return candidate.emscriptenVal.equals(element); });
416 if (it == m_screens.end()) {
417 qWarning() << "Attempting to resize non-existing screen for element"
418 << QString::fromEcmaString(element["id"]);
419 return;
420 }
421 it->wasmScreen->updateQScreenSize();
422}
423
425{
426 emscripten::val dpi = emscripten::val::module_property("qtFontDpi");
427 if (dpi.isUndefined())
428 return;
429 qreal dpiValue = dpi.as<qreal>();
430 for (const auto &elementAndScreen : m_screens)
431 QWindowSystemInterface::handleScreenLogicalDotsPerInchChange(elementAndScreen.wasmScreen->screen(), dpiValue, dpiValue);
432}
433
435{
436 for (const auto &elementAndScreen : m_screens)
437 elementAndScreen.wasmScreen->updateQScreenSize();
438}
439
441{
442 m_fontDb->populateLocalFontFamilies(families);
443}
444
445quint64 QWasmIntegration::getTimestamp()
446{
447 return emscripten_performance_now();
448}
449
450#if QT_CONFIG(draganddrop)
451QPlatformDrag *QWasmIntegration::drag() const
452{
453 return m_drag.get();
454}
455#endif // QT_CONFIG(draganddrop)
456
457QT_END_NAMESPACE
static bool releaseRequestUpdateHold()
QWasmCompositor(QWasmScreen *screen)
QPlatformInputContext * inputContext() const override
Returns the platforms input context.
void setContainerElements(emscripten::val elementArray)
static QWasmIntegration * get()
Qt::WindowState defaultWindowState(Qt::WindowFlags flags) const override
QPlatformFontDatabase * fontDatabase() const override
Accessor for the platform integration's fontdatabase.
void addContainerElement(emscripten::val elementArray)
void initialize() override
Performs initialization steps that depend on having an event dispatcher available.
void removeContainerElement(emscripten::val elementArray)
QAbstractEventDispatcher * createEventDispatcher() const override
Factory function for the GUI event dispatcher.
QStringList themeNames() const override
void loadLocalFontFamilies(emscripten::val families)
QPlatformOpenGLContext * createPlatformOpenGLContext(QOpenGLContext *context) const override
Factory function for QPlatformOpenGLContext.
void removeBackingStore(QWindow *window)
QPlatformBackingStore * createPlatformBackingStore(QWindow *window) const override
Factory function for QPlatformBackingStore.
QPlatformServices * services() const override
QPlatformWindow * createForeignWindow(QWindow *window, WId nativeHandle) const override
QPlatformTheme * createPlatformTheme(const QString &name) const override
void resizeScreen(const emscripten::val &canvas)
QVariant styleHint(QPlatformIntegration::StyleHint hint) const override
bool hasCapability(QPlatformIntegration::Capability cap) const override
QPlatformWindow * createPlatformWindow(QWindow *window) const override
Factory function for QPlatformWindow.
QPlatformAccessibility * accessibility() const override
QWasmOffscreenSurface(QOffscreenSurface *offscreenSurface)
QWasmOpenGLContext(QOpenGLContext *context)
void deleteScreen()
friend class QWasmCompositor
QJSValue function
Definition qjsengine.cpp:9
EMSCRIPTEN_BINDINGS(qtQWasmIntegraton)
static void addContainerElement(emscripten::val element)
static void removeContainerElement(emscripten::val element)
QT_BEGIN_NAMESPACE void qt_set_sequence_auto_mnemonic(bool)
static void loadLocalFontFamilies(emscripten::val event)
static void resizeAllScreens(emscripten::val event)
static void resizeContainerElement(emscripten::val element)
static void setContainerElements(emscripten::val elementArray)
static void qtUpdateDpi()
QT_BEGIN_NAMESPACE Platform platform()