summaryrefslogtreecommitdiffstats
path: root/src/window-lib/waylandqtamserverextension.cpp
blob: 56794db2a7ece7e70d9afcc8b7af21832e892a1b (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
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "waylandqtamserverextension_p.h"

#include <QDataStream>
#include <QCborValue>
#include <QtWaylandCompositor/QWaylandCompositor>
#include <QtWaylandCompositor/QWaylandResource>
#include <QtWaylandCompositor/QWaylandSurface>

#include <QtAppManCommon/logging.h>

QT_BEGIN_NAMESPACE_AM

WaylandQtAMServerExtension::WaylandQtAMServerExtension(QWaylandCompositor *compositor)
    : QWaylandCompositorExtensionTemplate(compositor)
    , QtWaylandServer::qtam_extension(compositor->display(), 2)
{ }

QVariantMap WaylandQtAMServerExtension::windowProperties(const QWaylandSurface *surface) const
{
    return m_windowProperties.value(surface);
}

void WaylandQtAMServerExtension::setWindowProperty(QWaylandSurface *surface, const QString &name, const QVariant &value)
{
    if (setWindowPropertyHelper(surface, name, value)) {
        if (Resource *target = resourceMap().value(surface->waylandClient())) {
            QByteArray data;

            switch (target->version()) {
            case 1: {
                QDataStream ds(&data, QDataStream::WriteOnly);
                ds << value;
                break;
            }
            case 2:
                data = QCborValue::fromVariant(value).toCbor();
                break;
            default:
                qCWarning(LogWayland) << "Unsupported qtam_extension version:" << target->version();
                return;
            }

            qCDebug(LogWayland) << "window property: server send" << surface << name << value;
            send_window_property_changed(target->handle, surface->resource(), name, data);
        }
    }
}

bool WaylandQtAMServerExtension::setWindowPropertyHelper(QWaylandSurface *surface, const QString &name, const QVariant &value)
{
    auto it = m_windowProperties.find(surface);
    if ((it == m_windowProperties.end()) || (it.value().value(name) != value)) {
        if (it == m_windowProperties.end()) {
            m_windowProperties[surface].insert(name, value);
            connect(surface, &QWaylandSurface::surfaceDestroyed, this, [this, surface]() {
                m_windowProperties.remove(surface);
            });
        } else {
            it.value().insert(name, value);
        }
        emit windowPropertyChanged(surface, name, value);
        return true;
    }
    return false;
}

void WaylandQtAMServerExtension::qtam_extension_set_window_property(QtWaylandServer::qtam_extension::Resource *resource, wl_resource *surface_resource, const QString &name, wl_array *value)
{
    QWaylandSurface *surface = QWaylandSurface::fromResource(surface_resource);
    const auto data = QByteArray::fromRawData(static_cast<const char *>(value->data), qsizetype(value->size));
    QVariant variantValue;

    switch (resource->version()) {
    case 1: {
        QDataStream ds(data);
        ds >> variantValue;
        break;
    }
    case 2:
        variantValue = QCborValue::fromCbor(data).toVariant();
        break;
    default:
        qCWarning(LogWayland) << "Unsupported qtam_extension version:" << resource->version();
        return;
    }
    qCDebug(LogWayland) << "window property: server receive" << surface << name << variantValue;
    setWindowPropertyHelper(surface, name, variantValue);
}

QT_END_NAMESPACE_AM

#include "moc_waylandqtamserverextension_p.cpp"