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// Qt-Security score:significant reason:default
4
9#include "qwasmtheme.h"
10#if QT_CONFIG(clipboard)
11#include "qwasmclipboard.h"
12#endif
14#include "qwasmservices.h"
16#include "qwasmplatform.h"
17#include "qwasmwindow.h"
20#if QT_CONFIG(draganddrop)
21#include "qwasmdrag.h"
22#endif
23
24#include <qpa/qplatformwindow.h>
25#include <QtGui/qscreen.h>
26#include <qpa/qwindowsysteminterface.h>
27#include <QtCore/qcoreapplication.h>
28#include <qpa/qplatforminputcontextfactory_p.h>
29#include <qpa/qwindowsysteminterface_p.h>
30#include "private/qwasmsuspendresumecontrol_p.h"
31
32#include <emscripten/bind.h>
33#include <emscripten/val.h>
34
35// this is where EGL headers are pulled in, make sure it is last
36#include "qwasmscreen.h"
37#if QT_CONFIG(draganddrop)
38#include <private/qsimpledrag_p.h>
39#endif
40
42
43extern void qt_set_sequence_auto_mnemonic(bool);
44
45using namespace emscripten;
46
47using namespace Qt::StringLiterals;
48
49static void setContainerElements(emscripten::val elementArray)
50{
52}
53
58
63
68
73
74static void resizeAllScreens(emscripten::val event)
75{
76 Q_UNUSED(event);
78}
79
84
86{
87 function("qtSetContainerElements", &setContainerElements);
88 function("qtAddContainerElement", &addContainerElement);
89 function("qtRemoveContainerElement", &removeContainerElement);
90 function("qtResizeContainerElement", &resizeContainerElement);
91 function("qtUpdateDpi", &qtUpdateDpi);
92 function("qtResizeAllScreens", &resizeAllScreens);
93 function("qtLoadLocalFontFamilies", &loadLocalFontFamilies);
94}
95
97
98QWasmIntegration::QWasmIntegration()
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 delete m_clipboard;
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 case OffscreenSurface: return true;
195 default: return QPlatformIntegration::hasCapability(cap);
196 }
197}
198
199QWasmWindow *QWasmIntegration::createWindow(QWindow *window, WId nativeHandle) const
200{
201 auto *wasmScreen = QWasmScreen::get(window->screen());
202 QWasmCompositor *compositor = wasmScreen->compositor();
203 return new QWasmWindow(window, compositor,
204 m_backingStores.value(window), nativeHandle);
205}
206
208{
209 return createWindow(window, 0);
210}
211
212QPlatformWindow *QWasmIntegration::createForeignWindow(QWindow *window, WId nativeHandle) const
213{
214 return createWindow(window, nativeHandle);
215}
216
218{
219 QWasmCompositor *compositor = QWasmScreen::get(window->screen())->compositor();
220 QWasmBackingStore *backingStore = new QWasmBackingStore(compositor, window);
221 m_backingStores.insert(window, backingStore);
222 return backingStore;
223}
224
226{
227 m_backingStores.remove(window);
228}
229
230#ifndef QT_NO_OPENGL
231QPlatformOpenGLContext *QWasmIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
232{
233 return new QWasmOpenGLContext(context);
234}
235#endif
236
238{
239 auto icStrs = QPlatformInputContextFactory::requested();
240 if (!icStrs.isEmpty()) {
241 m_inputContext.reset(QPlatformInputContextFactory::create(icStrs));
242 m_wasmInputContext = nullptr;
243 } else {
244 m_inputContext.reset(new QWasmInputContext());
245 m_wasmInputContext = static_cast<QWasmInputContext *>(m_inputContext.data());
246 }
247}
248
250{
251 return m_inputContext.data();
252}
253
254QPlatformOffscreenSurface *QWasmIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const
255{
256 return new QWasmOffscreenSurface(surface);
257}
258
260{
261 if (m_fontDb == nullptr)
262 m_fontDb = new QWasmFontDatabase;
263
264 return m_fontDb;
265}
266
271
272QVariant QWasmIntegration::styleHint(QPlatformIntegration::StyleHint hint) const
273{
274 switch (hint) {
275 case ShowIsFullScreen:
276 return true;
277 case UnderlineShortcut:
278 return platform() != Platform::MacOS;
279 default:
280 return QPlatformIntegration::styleHint(hint);
281 }
282}
283
284Qt::WindowState QWasmIntegration::defaultWindowState(Qt::WindowFlags flags) const
285{
286 // Don't maximize dialogs or popups
287 if (flags.testFlag(Qt::Dialog) || flags.testFlag(Qt::Popup))
288 return Qt::WindowNoState;
289
290 return QPlatformIntegration::defaultWindowState(flags);
291}
292
294{
295 return QStringList() << "webassembly"_L1;
296}
297
299{
300 if (name == "webassembly"_L1)
301 return new QWasmTheme;
302 return QPlatformIntegration::createPlatformTheme(name);
303}
304
306{
307 if (m_desktopServices == nullptr)
308 m_desktopServices = new QWasmServices();
309 return m_desktopServices;
310}
311
312#if QT_CONFIG(clipboard)
313QPlatformClipboard* QWasmIntegration::clipboard() const
314{
315 return m_clipboard;
316}
317#endif
318
319#ifndef QT_NO_ACCESSIBILITY
321{
322 return m_accessibility;
323}
324#endif
325
327{
328 const auto *primaryScreenBefore = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
329 QList<ScreenMapping> newScreens;
330
331 QList<QWasmScreen *> screensToDelete;
332 std::transform(m_screens.begin(), m_screens.end(), std::back_inserter(screensToDelete),
333 [](const ScreenMapping &mapping) { return mapping.wasmScreen; });
334
335 for (int i = 0; i < elementArray["length"].as<int>(); ++i) {
336 const auto element = elementArray[i];
337 const auto it = std::find_if(
338 m_screens.begin(), m_screens.end(),
339 [&element](const ScreenMapping &screen) { return screen.emscriptenVal == element; });
340 QWasmScreen *screen;
341 if (it != m_screens.end()) {
342 screen = it->wasmScreen;
343 screensToDelete.erase(std::remove_if(screensToDelete.begin(), screensToDelete.end(),
344 [screen](const QWasmScreen *removedScreen) {
345 return removedScreen == screen;
346 }),
347 screensToDelete.end());
348 } else {
349 screen = new QWasmScreen(element);
350 QWindowSystemInterface::handleScreenAdded(screen);
351 }
352 newScreens.push_back({element, screen});
353 }
354
355 std::for_each(screensToDelete.begin(), screensToDelete.end(),
356 [](QWasmScreen *removed) { removed->deleteScreen(); });
357
358 m_screens = newScreens;
359 auto *primaryScreenAfter = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
360 if (primaryScreenAfter && primaryScreenAfter != primaryScreenBefore)
361 QWindowSystemInterface::handlePrimaryScreenChanged(primaryScreenAfter);
362}
363
365{
366 Q_ASSERT_X(m_screens.end()
367 == std::find_if(m_screens.begin(), m_screens.end(),
368 [&element](const ScreenMapping &screen) {
369 return screen.emscriptenVal == element;
370 }),
371 Q_FUNC_INFO, "Double-add of an element");
372
373 QWasmScreen *screen = new QWasmScreen(element);
374 QWindowSystemInterface::handleScreenAdded(screen);
375 m_screens.push_back({element, screen});
376}
377
379{
380 const auto *primaryScreenBefore = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
381
382 const auto it =
383 std::find_if(m_screens.begin(), m_screens.end(),
384 [&element](const ScreenMapping &screen) { return screen.emscriptenVal == element; });
385 if (it == m_screens.end()) {
386 qWarning() << "Attempt to remove a nonexistent screen.";
387 return;
388 }
389
390 QWasmScreen *removedScreen = it->wasmScreen;
391 removedScreen->deleteScreen();
392
393 m_screens.erase(std::remove_if(m_screens.begin(), m_screens.end(),
394 [removedScreen](const ScreenMapping &mapping) {
395 return removedScreen == mapping.wasmScreen;
396 }),
397 m_screens.end());
398 auto *primaryScreenAfter = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
399 if (primaryScreenAfter && primaryScreenAfter != primaryScreenBefore)
400 QWindowSystemInterface::handlePrimaryScreenChanged(primaryScreenAfter);
401}
402
403void QWasmIntegration::resizeScreen(const emscripten::val &element)
404{
405 auto it = std::find_if(m_screens.begin(), m_screens.end(),
406 [&] (const ScreenMapping &candidate) { return candidate.emscriptenVal.equals(element); });
407 if (it == m_screens.end()) {
408 qWarning() << "Attempting to resize non-existing screen for element"
409 << QString::fromEcmaString(element["id"]);
410 return;
411 }
412 it->wasmScreen->updateQScreenSize();
413}
414
416{
417 emscripten::val dpi = emscripten::val::module_property("qtFontDpi");
418 if (dpi.isUndefined())
419 return;
420 qreal dpiValue = dpi.as<qreal>();
421 for (const auto &elementAndScreen : m_screens)
422 QWindowSystemInterface::handleScreenLogicalDotsPerInchChange(elementAndScreen.wasmScreen->screen(), dpiValue, dpiValue);
423}
424
426{
427 for (const auto &elementAndScreen : m_screens)
428 elementAndScreen.wasmScreen->updateQScreenSize();
429}
430
432{
433 m_fontDb->populateLocalFontFamilies(families);
434}
435
436quint64 QWasmIntegration::getTimestamp()
437{
438 return emscripten_performance_now();
439}
440
441#if QT_CONFIG(draganddrop)
442QPlatformDrag *QWasmIntegration::drag() const
443{
444 return m_drag.get();
445}
446#endif // QT_CONFIG(draganddrop)
447
448QT_END_NAMESPACE
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
Combined button and popup list for selecting options.
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()
Platform platform()