summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/webgl/qwebglwebsocketserver.cpp
blob: 959287de9ff10c14632475ac1aea86cf166e98b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt WebGL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qwebglwebsocketserver.h"

#include "qwebglcontext.h"
#include "qwebglfunctioncall.h"
#include "qwebglintegration.h"
#include "qwebglintegration_p.h"
#include "qwebglwindow.h"
#include "qwebglwindow_p.h"

#include <QtCore/private/qobject_p.h>
#include <QtCore/qcoreevent.h>
#include <QtCore/qdebug.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qwaitcondition.h>
#include <QtGui/qevent.h>
#include <QtGui/qguiapplication.h>
#include <QtGui/qpa/qwindowsysteminterface.h>
#include <QtWebSockets/qwebsocket.h>
#include <QtWebSockets/qwebsocketserver.h>

#include <cstring>

QT_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(lc, "qt.qpa.webgl.websocketserver")

inline QWebGLIntegration *webGLIntegration()
{
#ifdef QT_DEBUG
    auto nativeInterface = dynamic_cast<QWebGLIntegration *>(qGuiApp->platformNativeInterface());
    Q_ASSERT(nativeInterface);
#else
    auto nativeInterface = static_cast<QWebGLIntegration *>(qGuiApp->platformNativeInterface());
#endif // QT_DEBUG
    return nativeInterface;
}

class QWebGLWebSocketServerPrivate
{
public:
    QWebSocketServer *server = nullptr;
};

QWebGLWebSocketServer::QWebGLWebSocketServer(QObject *parent) :
    QObject(parent),
    d_ptr(new QWebGLWebSocketServerPrivate)
{}

QWebGLWebSocketServer::~QWebGLWebSocketServer()
{}

quint16 QWebGLWebSocketServer::port() const
{
    Q_D(const QWebGLWebSocketServer);
    return d->server->serverPort();
}

QMutex *QWebGLWebSocketServer::mutex()
{
    return &QWebGLIntegrationPrivate::instance()->waitMutex;
}

QWaitCondition *QWebGLWebSocketServer::waitCondition()
{
    return &QWebGLIntegrationPrivate::instance()->waitCondition;
}

QVariant QWebGLWebSocketServer::queryValue(int id)
{
    QMutexLocker locker(&QWebGLIntegrationPrivate::instance()->waitMutex);
    if (QWebGLIntegrationPrivate::instance()->receivedResponses.contains(id))
        return QWebGLIntegrationPrivate::instance()->receivedResponses.take(id);
    return QVariant();
}

void QWebGLWebSocketServer::create()
{
    Q_D(QWebGLWebSocketServer);
    d->server = new QWebSocketServer(QLatin1String("qtwebgl"), QWebSocketServer::NonSecureMode);
    if (d->server->listen(QHostAddress::Any)) {
        connect(d->server, &QWebSocketServer::newConnection,
                this, &QWebGLWebSocketServer::onNewConnection);
    } else {
        qCCritical(lc, "The WebSocket Server cannot start: %s",
                   qPrintable(d->server->errorString()));
    }

    QMutexLocker lock(&QWebGLIntegrationPrivate::instance()->waitMutex);
    QWebGLIntegrationPrivate::instance()->waitCondition.wakeAll();
}

void QWebGLWebSocketServer::sendMessage(QWebSocket *socket,
                                        MessageType type,
                                        const QVariantMap &values)
{
    if (!socket)
        return;
    QString typeString;
    switch (type) {
    case MessageType::Connect:
        typeString = QStringLiteral("connect");
        qCDebug(lc) << "Sending connect to " << socket << values;
        break;
    case MessageType::GlCommand: {
        const auto functionName = values["function"].toString().toUtf8();
        const auto parameters = values["parameters"].toList();
        const quint32 parameterCount = parameters.size();
        qCDebug(lc, "Sending gl_command %s to %p with %d parameters",
                qPrintable(functionName), socket, parameterCount);
        QByteArray data;
        {
            QDataStream stream(&data, QIODevice::WriteOnly);
            stream << QWebGLContext::functionIndex(functionName);
            if (values.contains("id")) {
                auto ok = false;
                stream << quint32(values["id"].toUInt(&ok));
                Q_ASSERT(ok);
            }
            const std::function<void(const QVariantList &)> serialize = [&stream, &serialize](
                    const QVariantList &parameters) {
                for (const auto &value : parameters) {
                    if (value.isNull()) {
                        stream << (quint8)'n';
                    } else switch (value.type()) {
                    case QVariant::Int:
                        stream << (quint8)'i' << value.toInt();
                        break;
                    case QVariant::UInt:
                        stream << (quint8)'u' << value.toUInt();
                        break;
                    case QVariant::Bool:
                        stream << (quint8)'b' << (quint8)value.toBool();
                        break;
                    case QVariant::Double:
                        stream << (quint8)'d' << value.toDouble();
                        break;
                    case QVariant::String:
                        stream << (quint8)'s' << value.toString().toUtf8();
                        break;
                    case QVariant::ByteArray: {
                        const auto byteArray = value.toByteArray();
                        if (byteArray.isNull())
                            stream << (quint8)'n';
                        else
                            stream << (quint8)'x' << byteArray;
                        break;
                    }
                    case QVariant::List: {
                        const auto list = value.toList();
                        stream << quint8('a') << quint8(list.size());
                        serialize(list);
                        break;
                    }
                    default:
                        qCCritical(lc, "Unsupported type: %d", value.type());
                        break;
                    }
                }
            };
            serialize(parameters);
            stream << (quint32)0xbaadf00d;
        }
        const quint32 totalMessageSize = data.size();
        const quint32 maxMessageSize = 1024;
        for (quint32 i = 0; i <= data.size() / maxMessageSize; ++i) {
            const quint32 offset = i * maxMessageSize;
            const quint32 size = qMin(totalMessageSize - offset, maxMessageSize);
            const auto chunk = QByteArray::fromRawData(data.constData() + offset, size);
            socket->sendBinaryMessage(chunk);
        }
        return;
    }
    case MessageType::CreateCanvas:
        qCDebug(lc) << "Sending create_canvas to " << socket << values;
        typeString = QStringLiteral("create_canvas");
        break;
    case MessageType::DestroyCanvas:
        return; // TODO: In current implementation the canvas is not destroyed
        qCDebug(lc) << "Sending destroy_canvas to " << socket << values;
        typeString = QStringLiteral("destroy_canvas");
        break;
    case MessageType::OpenUrl:
        qCDebug(lc) << "Sending open_url to " << socket << values;
        typeString = QStringLiteral("open_url");
        break;
    case MessageType::ChangeTitle:
        qCDebug(lc) << "Sending change_title to " << socket << values;
        typeString = QStringLiteral("changle_title");
        break;
    }
    QJsonDocument document;
    auto commandObject = QJsonObject::fromVariantMap(values);
    commandObject["type"] = typeString;
    document.setObject(commandObject);
    auto data = document.toJson(QJsonDocument::Compact);
    socket->sendTextMessage(data);
}

bool QWebGLWebSocketServer::event(QEvent *event)
{
    int type = event->type();
    if (type == QWebGLFunctionCall::type()) {
        auto e = static_cast<QWebGLFunctionCall *>(event);
        QVariantMap values {
           { "function", e->functionName() },
           { "parameters", e->parameters() }
        };
        if (e->id() != -1)
            values.insert("id", e->id());
        auto integrationPrivate = QWebGLIntegrationPrivate::instance();
        auto clientData = integrationPrivate->findClientData(e->surface());
        if (clientData && clientData->socket) {
            sendMessage(clientData->socket, MessageType::GlCommand, values);
            if (e->isBlocking())
                integrationPrivate->pendingResponses.append(e->id());
            return true;
        }
        return false;
    }
    return QObject::event(event);
}

void QWebGLWebSocketServer::onNewConnection()
{
    Q_D(QWebGLWebSocketServer);
    QWebSocket *socket = d->server->nextPendingConnection();
    if (socket) {
        connect(socket, &QWebSocket::disconnected, this, &QWebGLWebSocketServer::onDisconnect);
        connect(socket, &QWebSocket::textMessageReceived, this,
                &QWebGLWebSocketServer::onTextMessageReceived);

        const QVariantMap values{
            {
                QStringLiteral("debug"),
#ifdef QT_DEBUG
                true
#else
                false
#endif
            },
            { QStringLiteral("loadingScreen"), qgetenv("QT_WEBGL_LOADINGSCREEN") },
            { QStringLiteral("supportedFunctions"),
              QVariant::fromValue(QWebGLContext::supportedFunctions()) },
            { "sysinfo",
                QVariantMap {
                    { QStringLiteral("buildAbi"), QSysInfo::buildAbi() },
                    { QStringLiteral("buildCpuArchitecture"), QSysInfo::buildCpuArchitecture() },
                    { QStringLiteral("currentCpuArchitecture"),
                      QSysInfo::currentCpuArchitecture() },
                    { QStringLiteral("kernelType"), QSysInfo::kernelType() },
                    { QStringLiteral("machineHostName"), QSysInfo::machineHostName() },
                    { QStringLiteral("prettyProductName"), QSysInfo::prettyProductName() },
                    { QStringLiteral("productType"), QSysInfo::productType() },
                    { QStringLiteral("productVersion"), QSysInfo::productVersion() }
                }
            }
        };

        sendMessage(socket, MessageType::Connect, values);
    }
}

void QWebGLWebSocketServer::onDisconnect()
{
    QWebSocket *socket = qobject_cast<QWebSocket *>(sender());
    Q_ASSERT(socket);
    QWebGLIntegrationPrivate::instance()->clientDisconnected(socket);
    socket->deleteLater();
}

void QWebGLWebSocketServer::onTextMessageReceived(const QString &message)
{
    const auto socket = qobject_cast<QWebSocket *>(sender());
    QWebGLIntegrationPrivate::instance()->onTextMessageReceived(socket, message);
}

QT_END_NAMESPACE