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
10#include "qwasmtheme.h"
11#if QT_CONFIG(clipboard)
12#include "qwasmclipboard.h"
13#endif
15#include "qwasmservices.h"
17#include "qwasmplatform.h"
18#include "qwasmwindow.h"
21#if QT_CONFIG(draganddrop)
22#include "qwasmdrag.h"
23#endif
24
25#include <qpa/qplatformwindow.h>
26#include <QtGui/qscreen.h>
27#include <qpa/qwindowsysteminterface.h>
28#include <QtCore/qcoreapplication.h>
29#include <qpa/qplatforminputcontextfactory_p.h>
30#include <qpa/qwindowsysteminterface_p.h>
31#include "private/qwasmsuspendresumecontrol_p.h"
32
33#include <emscripten/bind.h>
34#include <emscripten/val.h>
35
36// this is where EGL headers are pulled in, make sure it is last
37#include "qwasmscreen.h"
38#if QT_CONFIG(draganddrop)
39#include <private/qsimpledrag_p.h>
40#endif
41
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_fontDb(nullptr)
99 , m_desktopServices(nullptr)
100#if QT_CONFIG(clipboard)
101 , m_clipboard(new QWasmClipboard)
102#endif
103#if QT_CONFIG(accessibility)
104 , m_accessibility(new QWasmAccessibility)
105#endif
106{
107 s_instance = this;
108
109 touchPoints = emscripten::val::global("navigator")["maxTouchPoints"].as<int>();
110 QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(false);
111
112 // Create screens for container elements. Each container element will ultimately become a
113 // div element. Qt historically supported supplying canvas for screen elements - these elements
114 // will be transformed into divs and warnings about deprecation will be printed. See
115 // QWasmScreen ctor.
116 emscripten::val filtered = emscripten::val::array();
117 emscripten::val qtContainerElements = val::module_property("qtContainerElements");
118 if (qtContainerElements.isArray()) {
119 for (int i = 0; i < qtContainerElements["length"].as<int>(); ++i) {
120 emscripten::val element = qtContainerElements[i].as<emscripten::val>();
121 if (element.isNull() || element.isUndefined())
122 qWarning() << "Skipping null or undefined element in qtContainerElements";
123 else
124 filtered.call<void>("push", element);
125 }
126 } else {
127 // No screens, which may or may not be intended
128 qWarning() << "The qtContainerElements module property was not set or is invalid. "
129 "Proceeding with no screens.";
130 }
132
133 // install browser window resize handler
134 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, EM_TRUE,
135 [](int, const EmscriptenUiEvent *, void *) -> EM_BOOL {
136 // This resize event is called when the HTML window is
137 // resized. Depending on the page layout the elements might
138 // also have been resized, so we update the Qt screen sizes
139 // (and canvas render sizes).
140 if (QWasmIntegration *integration = QWasmIntegration::get())
141 integration->resizeAllScreens();
142 return EM_FALSE;
143 });
144
145 // install visualViewport resize handler which picks up size and scale change on mobile.
146 emscripten::val visualViewport = emscripten::val::global("window")["visualViewport"];
147 if (!visualViewport.isUndefined()) {
148 visualViewport.call<void>("addEventListener", val("resize"),
149 val::module_property("qtResizeAllScreens"));
150 }
151#if QT_CONFIG(draganddrop)
152 m_drag = std::make_unique<QWasmDrag>();
153#endif
154}
155
157{
158 // Remove event listener
159 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, EM_TRUE, nullptr);
160 emscripten::val visualViewport = emscripten::val::global("window")["visualViewport"];
161 if (!visualViewport.isUndefined()) {
162 visualViewport.call<void>("removeEventListener", val("resize"),
163 val::module_property("qtResizeAllScreens"));
164 }
165
166 delete m_fontDb;
167 delete m_desktopServices;
168#if QT_CONFIG(accessibility)
169 delete m_accessibility;
170#endif
171
172 for (const auto &elementAndScreen : m_screens)
173 elementAndScreen.wasmScreen->deleteScreen();
174
175 m_screens.clear();
176 delete m_clipboard;
177 s_instance = nullptr;
178}
179
180bool QWasmIntegration::hasCapability(QPlatformIntegration::Capability cap) const
181{
182 switch (cap) {
183 case ThreadedPixmaps: return true;
184 case OpenGL: return true;
185 case ThreadedOpenGL: return false;
186 case MultipleWindows: return true;
187 case WindowManagement: return true;
188 case ForeignWindows: return true;
189 case ChildWindows: return true;
190 case OpenGLOnRasterSurface: return true;
191 case OffscreenSurface: return true;
192 case ApplicationState: return true;
193 default: return QPlatformIntegration::hasCapability(cap);
194 }
195}
196
197QWasmWindow *QWasmIntegration::createWindow(QWindow *window, WId nativeHandle) const
198{
199 auto *wasmScreen = QWasmScreen::get(window->screen());
200 QWasmCompositor *compositor = wasmScreen->compositor();
201 return new QWasmWindow(window, compositor,
202 m_backingStores.value(window), nativeHandle);
203}
204
206{
207 return createWindow(window, 0);
208}
209
210QPlatformWindow *QWasmIntegration::createForeignWindow(QWindow *window, WId nativeHandle) const
211{
212 return createWindow(window, nativeHandle);
213}
214
216{
217 QWasmCompositor *compositor = QWasmScreen::get(window->screen())->compositor();
218 QWasmBackingStore *backingStore = new QWasmBackingStore(compositor, window);
219 m_backingStores.insert(window, backingStore);
220 return backingStore;
221}
222
224{
225 m_backingStores.remove(window);
226}
227
228#ifndef QT_NO_OPENGL
229QPlatformOpenGLContext *QWasmIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
230{
231 return new QWasmOpenGLContext(context);
232}
233#endif
234
236{
237 auto icStrs = QPlatformInputContextFactory::requested();
238 if (!icStrs.isEmpty()) {
239 m_inputContext.reset(QPlatformInputContextFactory::create(icStrs));
240 m_wasmInputContext = nullptr;
241 } else {
242 m_inputContext.reset(new QWasmInputContext());
243 m_wasmInputContext = static_cast<QWasmInputContext *>(m_inputContext.data());
244 }
245}
246
248{
249 return m_inputContext.data();
250}
251
252QPlatformOffscreenSurface *QWasmIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const
253{
254 return new QWasmOffscreenSurface(surface);
255}
256
258{
259 if (m_fontDb == nullptr)
260 m_fontDb = new QWasmFontDatabase;
261
262 return m_fontDb;
263}
264
269
270QVariant QWasmIntegration::styleHint(QPlatformIntegration::StyleHint hint) const
271{
272 switch (hint) {
273 case ShowIsFullScreen:
274 return true;
275 case UnderlineShortcut:
276 return platform() != Platform::MacOS;
277 default:
278 return QPlatformIntegration::styleHint(hint);
279 }
280}
281
282Qt::WindowState QWasmIntegration::defaultWindowState(Qt::WindowFlags flags) const
283{
284 // Don't maximize dialogs or popups
285 if (flags.testFlag(Qt::Dialog) || flags.testFlag(Qt::Popup))
286 return Qt::WindowNoState;
287
288 return QPlatformIntegration::defaultWindowState(flags);
289}
290
292{
293 return QStringList() << "webassembly"_L1;
294}
295
297{
298 if (name == "webassembly"_L1)
299 return new QWasmTheme;
300 return QPlatformIntegration::createPlatformTheme(name);
301}
302
304{
305 if (m_desktopServices == nullptr)
306 m_desktopServices = new QWasmServices();
307 return m_desktopServices;
308}
309
310#if QT_CONFIG(clipboard)
311QPlatformClipboard* QWasmIntegration::clipboard() const
312{
313 return m_clipboard;
314}
315#endif
316
317#ifndef QT_NO_ACCESSIBILITY
319{
320 return m_accessibility;
321}
322#endif
323
325{
326 const auto *primaryScreenBefore = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
327 QList<ScreenMapping> newScreens;
328
329 QList<QWasmScreen *> screensToDelete;
330 std::transform(m_screens.begin(), m_screens.end(), std::back_inserter(screensToDelete),
331 [](const ScreenMapping &mapping) { return mapping.wasmScreen; });
332
333 for (int i = 0; i < elementArray["length"].as<int>(); ++i) {
334 const auto element = elementArray[i];
335 const auto it = std::find_if(
336 m_screens.begin(), m_screens.end(),
337 [&element](const ScreenMapping &screen) { return screen.emscriptenVal == element; });
338 QWasmScreen *screen;
339 if (it != m_screens.end()) {
340 screen = it->wasmScreen;
341 screensToDelete.erase(std::remove_if(screensToDelete.begin(), screensToDelete.end(),
342 [screen](const QWasmScreen *removedScreen) {
343 return removedScreen == screen;
344 }),
345 screensToDelete.end());
346 } else {
347 screen = new QWasmScreen(element);
348 QWindowSystemInterface::handleScreenAdded(screen);
349 }
350 newScreens.push_back({element, screen});
351 }
352
353 std::for_each(screensToDelete.begin(), screensToDelete.end(),
354 [](QWasmScreen *removed) { removed->deleteScreen(); });
355
356 m_screens = newScreens;
357 auto *primaryScreenAfter = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
358 if (primaryScreenAfter && primaryScreenAfter != primaryScreenBefore)
359 QWindowSystemInterface::handlePrimaryScreenChanged(primaryScreenAfter);
360}
361
363{
364 Q_ASSERT_X(m_screens.end()
365 == std::find_if(m_screens.begin(), m_screens.end(),
366 [&element](const ScreenMapping &screen) {
367 return screen.emscriptenVal == element;
368 }),
369 Q_FUNC_INFO, "Double-add of an element");
370
371 QWasmScreen *screen = new QWasmScreen(element);
372 QWindowSystemInterface::handleScreenAdded(screen);
373 m_screens.push_back({element, screen});
374}
375
377{
378 const auto *primaryScreenBefore = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
379
380 const auto it =
381 std::find_if(m_screens.begin(), m_screens.end(),
382 [&element](const ScreenMapping &screen) { return screen.emscriptenVal == element; });
383 if (it == m_screens.end()) {
384 qWarning() << "Attempt to remove a nonexistent screen.";
385 return;
386 }
387
388 QWasmScreen *removedScreen = it->wasmScreen;
389 removedScreen->deleteScreen();
390
391 m_screens.erase(std::remove_if(m_screens.begin(), m_screens.end(),
392 [removedScreen](const ScreenMapping &mapping) {
393 return removedScreen == mapping.wasmScreen;
394 }),
395 m_screens.end());
396 auto *primaryScreenAfter = m_screens.isEmpty() ? nullptr : m_screens[0].wasmScreen;
397 if (primaryScreenAfter && primaryScreenAfter != primaryScreenBefore)
398 QWindowSystemInterface::handlePrimaryScreenChanged(primaryScreenAfter);
399}
400
401void QWasmIntegration::resizeScreen(const emscripten::val &element)
402{
403 auto it = std::find_if(m_screens.begin(), m_screens.end(),
404 [&] (const ScreenMapping &candidate) { return candidate.emscriptenVal.equals(element); });
405 if (it == m_screens.end()) {
406 qWarning() << "Attempting to resize non-existing screen for element"
407 << QString::fromEcmaString(element["id"]);
408 return;
409 }
410 it->wasmScreen->updateQScreenSize();
411}
412
414{
415 emscripten::val dpi = emscripten::val::module_property("qtFontDpi");
416 if (dpi.isUndefined())
417 return;
418 qreal dpiValue = dpi.as<qreal>();
419 for (const auto &elementAndScreen : m_screens)
420 QWindowSystemInterface::handleScreenLogicalDotsPerInchChange(elementAndScreen.wasmScreen->screen(), dpiValue, dpiValue);
421}
422
424{
425 for (const auto &elementAndScreen : m_screens)
426 elementAndScreen.wasmScreen->updateQScreenSize();
427}
428
430{
431 m_fontDb->populateLocalFontFamilies(families);
432}
433
434quint64 QWasmIntegration::getTimestamp()
435{
436 return emscripten_performance_now();
437}
438
439#if QT_CONFIG(draganddrop)
440QPlatformDrag *QWasmIntegration::drag() const
441{
442 return m_drag.get();
443}
444#endif // QT_CONFIG(draganddrop)
445
446QT_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)
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()