19#include <qpa/qplatformwindow.h>
20#include <QtGui/qscreen.h>
21#include <qpa/qwindowsysteminterface.h>
22#include <QtCore/qcoreapplication.h>
23#include <qpa/qplatforminputcontextfactory_p.h>
24#include <qpa/qwindowsysteminterface_p.h>
25#include "private/qwasmsuspendresumecontrol_p.h"
27#include <emscripten/bind.h>
28#include <emscripten/val.h>
32#include <private/qsimpledrag_p.h>
40using namespace Qt::StringLiterals;
80 function(
"qtSetContainerElements", &setContainerElements);
93 , m_desktopServices(
nullptr)
95#if QT_CONFIG(accessibility)
96 , m_accessibility(
new QWasmAccessibility)
98 , m_suspendResume(std::make_shared<QWasmSuspendResumeControl>())
105 touchPoints = emscripten::val::global(
"navigator")[
"maxTouchPoints"].as<
int>();
106 QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(
false);
112 emscripten::val filtered = emscripten::val::array();
113 emscripten::val qtContainerElements = val::module_property(
"qtContainerElements");
114 if (qtContainerElements.isArray()) {
115 for (
int i = 0; i < qtContainerElements[
"length"].as<
int>(); ++i) {
116 emscripten::val element = qtContainerElements[i].as<emscripten::val>();
117 if (element.isNull() || element.isUndefined())
118 qWarning() <<
"Skipping null or undefined element in qtContainerElements";
120 filtered.call<
void>(
"push", element);
124 qWarning() <<
"The qtContainerElements module property was not set or is invalid. "
125 "Proceeding with no screens.";
130 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW,
nullptr, EM_TRUE,
131 [](
int,
const EmscriptenUiEvent *,
void *) -> EM_BOOL {
136 if (QWasmIntegration *integration = QWasmIntegration::get())
137 integration->resizeAllScreens();
142 emscripten::val visualViewport = emscripten::val::global(
"window")[
"visualViewport"];
143 if (!visualViewport.isUndefined()) {
144 visualViewport.call<
void>(
"addEventListener", val(
"resize"),
145 val::module_property(
"qtResizeAllScreens"));
147 m_drag = std::make_unique<QWasmDrag>();
153 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW,
nullptr, EM_TRUE,
nullptr);
154 emscripten::val visualViewport = emscripten::val::global(
"window")[
"visualViewport"];
155 if (!visualViewport.isUndefined()) {
156 visualViewport.call<
void>(
"removeEventListener", val(
"resize"),
157 val::module_property(
"qtResizeAllScreens"));
161 delete m_desktopServices;
162#if QT_CONFIG(accessibility)
163 delete m_accessibility;
166 for (
const auto &elementAndScreen : m_screens)
167 elementAndScreen.wasmScreen->deleteScreen();
171 s_instance =
nullptr;
177 case ThreadedPixmaps:
return true;
178 case OpenGL:
return true;
179 case ThreadedOpenGL:
return false;
180 case RasterGLSurface:
return false;
181 case MultipleWindows:
return true;
182 case WindowManagement:
return true;
183 case ForeignWindows:
return true;
184 case OpenGLOnRasterSurface:
return true;
185 default:
return QPlatformIntegration::hasCapability(cap);
191 auto *wasmScreen = QWasmScreen::get(window->screen());
193 return new QWasmWindow(window, wasmScreen->deadKeySupport(), compositor,
194 m_backingStores.value(window), nativeHandle);
199 return createWindow(window, 0);
204 return createWindow(window, nativeHandle);
209 QWasmCompositor *compositor = QWasmScreen::get(window->screen())->compositor();
211 m_backingStores.insert(window, backingStore);
217 m_backingStores.remove(window);
224 for (
const auto &elementAndScreen : m_screens) {
225 elementAndScreen.wasmScreen->compositor()->requestUpdate();
239 auto icStrs = QPlatformInputContextFactory::requested();
240 if (!icStrs.isEmpty()) {
241 m_inputContext.reset(QPlatformInputContextFactory::create(icStrs));
242 m_wasmInputContext =
nullptr;
244 m_inputContext.reset(
new QWasmInputContext());
245 m_wasmInputContext =
static_cast<QWasmInputContext *>(m_inputContext.data());
251 return m_inputContext.data();
254QPlatformOffscreenSurface *
QWasmIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface)
const
256 return new QWasmOffscreenSurface
(surface
);
261 if (m_fontDb ==
nullptr)
262 m_fontDb =
new QWasmFontDatabase;
269 return new QWasmEventDispatcher(m_suspendResume);
275 case ShowIsFullScreen:
277 case UnderlineShortcut:
280 return QPlatformIntegration::styleHint(hint);
287 if (flags.testFlag(Qt::Dialog) || flags.testFlag(Qt::Popup))
288 return Qt::WindowNoState;
290 return QPlatformIntegration::defaultWindowState(flags);
295 return QStringList() <<
"webassembly"_L1;
300 if (name ==
"webassembly"_L1)
302 return QPlatformIntegration::createPlatformTheme(name);
307 if (m_desktopServices ==
nullptr)
309 return m_desktopServices;
317#ifndef QT_NO_ACCESSIBILITY
320 return m_accessibility;
326 const auto *primaryScreenBefore = m_screens.isEmpty() ?
nullptr : m_screens[0].wasmScreen;
327 QList<ScreenMapping> newScreens;
329 QList<QWasmScreen *> screensToDelete;
330 std::transform(m_screens.begin(), m_screens.end(), std::back_inserter(screensToDelete),
331 [](
const ScreenMapping &mapping) {
return mapping.wasmScreen; });
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; });
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;
345 screensToDelete.end());
348 QWindowSystemInterface::handleScreenAdded(screen);
350 newScreens.push_back({element, screen});
353 std::for_each(screensToDelete.begin(), screensToDelete.end(),
354 [](QWasmScreen *removed) { removed->deleteScreen(); });
356 m_screens = newScreens;
357 auto *primaryScreenAfter = m_screens.isEmpty() ?
nullptr : m_screens[0].wasmScreen;
358 if (primaryScreenAfter && primaryScreenAfter != primaryScreenBefore)
359 QWindowSystemInterface::handlePrimaryScreenChanged(primaryScreenAfter);
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;
369 Q_FUNC_INFO,
"Double-add of an element");
372 QWindowSystemInterface::handleScreenAdded(screen);
373 m_screens.push_back({element, screen});
378 const auto *primaryScreenBefore = m_screens.isEmpty() ?
nullptr : m_screens[0].wasmScreen;
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.";
391 m_screens.erase(std::remove_if(m_screens.begin(), m_screens.end(),
392 [removedScreen](
const ScreenMapping &mapping) {
393 return removedScreen == mapping.wasmScreen;
396 auto *primaryScreenAfter = m_screens.isEmpty() ?
nullptr : m_screens[0].wasmScreen;
397 if (primaryScreenAfter && primaryScreenAfter != primaryScreenBefore)
398 QWindowSystemInterface::handlePrimaryScreenChanged(primaryScreenAfter);
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"]);
410 it->wasmScreen->updateQScreenSize();
415 emscripten::val dpi = emscripten::val::module_property(
"qtFontDpi");
416 if (dpi.isUndefined())
418 qreal dpiValue = dpi.as<qreal>();
419 for (
const auto &elementAndScreen : m_screens)
420 QWindowSystemInterface::handleScreenLogicalDotsPerInchChange(elementAndScreen.wasmScreen->screen(), dpiValue, dpiValue);
425 for (
const auto &elementAndScreen : m_screens)
426 elementAndScreen.wasmScreen->updateQScreenSize();
431 m_fontDb->populateLocalFontFamilies(families);
436 return emscripten_performance_now();
439#if QT_CONFIG(draganddrop)
440QPlatformDrag *QWasmIntegration::drag()
const
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 releaseRequesetUpdateHold()
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
QPlatformClipboard * clipboard() const override
Accessor for the platform integration's clipboard.
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)
friend class QWasmCompositor
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()