From 642b04701d2c3b532ce48c27cad6c7f0877761b5 Mon Sep 17 00:00:00 2001
From: Johannes Pointner <johannes.pointner@br-automation.com>
Date: Wed, 29 Jun 2022 11:22:16 +0200
Subject: [PATCH] modifications

---
 src/plugins/platforms/webgl/CMakeLists.txt    |  4 +--
 .../platforms/webgl/qwebglintegration.cpp     | 30 ++++++++-----------
 .../platforms/webgl/qwebglintegration.h       |  1 -
 .../platforms/webgl/qwebglwebsocketserver.h   |  1 +
 4 files changed, 16 insertions(+), 20 deletions(-)

diff --git a/src/plugins/platforms/webgl/CMakeLists.txt b/src/plugins/platforms/webgl/CMakeLists.txt
index 3ce925e..bceaffb 100644
--- a/src/plugins/platforms/webgl/CMakeLists.txt
+++ b/src/plugins/platforms/webgl/CMakeLists.txt
@@ -19,11 +19,11 @@ qt_internal_add_plugin(QWebGLIntegrationPlugin
         qwebglwindow.cpp qwebglwindow.h qwebglwindow_p.h
     LIBRARIES
         Qt::Core
-	Qt::CorePrivate
+        Qt::CorePrivate
         Qt::Gui
         Qt::GuiPrivate
         Qt::WebSockets
-	Qt::Quick
+        Qt::Quick
 )
 
 # Resources:
diff --git a/src/plugins/platforms/webgl/qwebglintegration.cpp b/src/plugins/platforms/webgl/qwebglintegration.cpp
index e6defe2..8fbdd07 100644
--- a/src/plugins/platforms/webgl/qwebglintegration.cpp
+++ b/src/plugins/platforms/webgl/qwebglintegration.cpp
@@ -32,7 +32,6 @@
 
 #include "qwebglwindow.h"
 #include "qwebglcontext.h"
-#include "qwebglcontext.h"
 #include "qwebglhttpserver.h"
 #include "qwebglwebsocketserver.h"
 #include "qwebglplatformservices.h"
@@ -85,11 +84,6 @@ QWebGLIntegration::QWebGLIntegration(quint16 port, quint16 wssport) :
     qRegisterMetaType<QWebGLWebSocketServer::MessageType>("QWebGLWebSocketServer::MessageType");
 }
 
-QWebGLIntegration::~QWebGLIntegration()
-{
-    Q_D(QWebGLIntegration);
-}
-
 QWebGLIntegration *QWebGLIntegration::instance()
 {
     return static_cast<QWebGLIntegration *>(qGuiApp->platformNativeInterface());
@@ -191,7 +185,6 @@ QPlatformWindow *QWebGLIntegration::createPlatformWindow(QWindow *window) const
     Q_D(const QWebGLIntegration);
     qCDebug(lcWebGL, "Creating platform window for: %p", window);
 
-
     if (window->inherits("QQuickWindow")) {
         auto quickWindow = (QQuickWindow *)window;
         quickWindow->setPersistentSceneGraph(false);
@@ -256,12 +249,10 @@ QPlatformOpenGLContext *QWebGLIntegration::createPlatformOpenGLContext(QOpenGLCo
     const
 {
     qCDebug(lcWebGL, "%p", context);
-    // ### If this is needed still then this part needs tweaking (the code does not make sense)
-    //QVariant nativeHandle = context->nativeHandle();
 
     const QSurfaceFormat adjustedFormat = context->format();
     QWebGLContext *ctx = new QWebGLContext(adjustedFormat);
-    //context->setNativeHandle(nativeHandle);
+
     return ctx;
 }
 
@@ -481,27 +472,32 @@ void QWebGLIntegrationPrivate::handleCanvasResize(const ClientData &clientData,
 
 void QWebGLIntegrationPrivate::handleMouse(const ClientData &clientData, const QJsonObject &object)
 {
+    static int buttonState = Qt::NoButton;
     const auto winId = object.value("name").toInt(-1);
     Q_ASSERT(winId != -1);
     QPointF localPos(object.value("layerX").toDouble(),
                      object.value("layerY").toDouble());
     QPointF globalPos(object.value("clientX").toDouble(),
                       object.value("clientY").toDouble());
-    auto buttons = static_cast<Qt::MouseButtons>(object.value("buttons").toInt());
+    int buttons = object.value("buttons").toInt();
+    int buttonStateChange = buttonState ^ buttons;
+    QEvent::Type type = QEvent::MouseMove;
+    if (buttons > buttonState)
+        type = QEvent::MouseButtonPress;
+    else if (buttons < buttonState)
+        type = QEvent::MouseButtonRelease;
     auto time = object.value("time").toString();
     auto platformWindow = findWindow(clientData, winId);
-    bool hasButton = QGuiApplication::mouseButtons() != Qt::NoButton;
-    bool mouseMove = QGuiApplicationPrivate::instance()->lastCursorPosition != globalPos;
     QWindowSystemInterface::handleMouseEvent(platformWindow->window(),
                                              time.toULong(),
                                              localPos,
                                              globalPos,
                                              Qt::MouseButtons(buttons),
-                                             hasButton ? (!mouseMove ? Qt::LeftButton : Qt::NoButton) : Qt::LeftButton,
-                                             hasButton ? (buttons && !mouseMove ? QEvent::MouseButtonRelease : QEvent::None) :
-                                                buttons ? QEvent::MouseButtonPress : QEvent::None,
-                                             Qt::NoModifier,
+                                             Qt::MouseButton(buttonStateChange),
+                                             type,
+                                             QGuiApplication::keyboardModifiers(),
                                              Qt::MouseEventNotSynthesized);
+    buttonState = buttons;
 }
 
 void QWebGLIntegrationPrivate::handleWheel(const ClientData &clientData, const QJsonObject &object)
diff --git a/src/plugins/platforms/webgl/qwebglintegration.h b/src/plugins/platforms/webgl/qwebglintegration.h
index d2716a8..a2458d6 100644
--- a/src/plugins/platforms/webgl/qwebglintegration.h
+++ b/src/plugins/platforms/webgl/qwebglintegration.h
@@ -46,7 +46,6 @@ class QWebGLIntegration : public QPlatformIntegration, public QPlatformNativeInt
 {
 public:
     QWebGLIntegration(quint16 port, quint16 wssport);
-    ~QWebGLIntegration();
 
     static QWebGLIntegration *instance();
 
diff --git a/src/plugins/platforms/webgl/qwebglwebsocketserver.h b/src/plugins/platforms/webgl/qwebglwebsocketserver.h
index bbbb9f6..f265856 100644
--- a/src/plugins/platforms/webgl/qwebglwebsocketserver.h
+++ b/src/plugins/platforms/webgl/qwebglwebsocketserver.h
@@ -33,6 +33,7 @@
 #include <QtCore/qobject.h>
 #include <QtCore/qscopedpointer.h>
 #include <QtCore/qvariant.h>
+
 QT_BEGIN_NAMESPACE
 
 class QMutex;
