xdgoutput_v1_interface.cpp 5.71 KB
Newer Older
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
/*
    SPDX-FileCopyrightText: 2018 David Edmundson <kde@davidedmundson.co.uk>
    SPDX-FileCopyrightText: 2020 David Edmundson <kde@davidedmundson.co.uk>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "xdgoutput_v1_interface.h"
#include "display.h"
#include "output_interface.h"

#include "qwayland-server-xdg-output-unstable-v1.h"

#include <QHash>
#include <QDebug>

namespace KWaylandServer
{

static const quint32 s_version = 2;

class XdgOutputManagerV1InterfacePrivate : public QtWaylandServer::zxdg_output_manager_v1
{
public:
    XdgOutputManagerV1InterfacePrivate(XdgOutputManagerV1Interface *q, Display *display);
    QHash<OutputInterface *, XdgOutputV1Interface *> outputs;

    XdgOutputManagerV1Interface *q;

protected:
    void zxdg_output_manager_v1_destroy(Resource *resource) override;
    void zxdg_output_manager_v1_get_xdg_output(Resource *resource, uint32_t id, wl_resource *output) override;
};

class XdgOutputV1InterfacePrivate : public QtWaylandServer::zxdg_output_v1
{
public:
    QPoint pos;
    QSize size;
    QString name;
    QString description;
    bool dirty = false;
    bool doneOnce = false;

protected:
    void zxdg_output_v1_bind_resource(Resource *resource) override;
    void zxdg_output_v1_destroy(Resource *resource) override;
};


XdgOutputManagerV1Interface::XdgOutputManagerV1Interface(Display *display, QObject *parent)
    : QObject(parent)
    , d(new XdgOutputManagerV1InterfacePrivate(this, display))
{
}

XdgOutputManagerV1Interface::~XdgOutputManagerV1Interface()
{}

XdgOutputV1Interface * XdgOutputManagerV1Interface::createXdgOutput(OutputInterface *output, QObject *parent)
{
    Q_ASSERT_X(!d->outputs.contains(output), "createXdgOutput", "An XdgOuputInterface already exists for this output");

    auto xdgOutput = new XdgOutputV1Interface(parent);
    d->outputs[output] = xdgOutput;

    //as XdgOutput lifespan is managed by user, delete our mapping when either
    //it or the relevant Output gets deleted
    connect(output, &QObject::destroyed, this, [this, output]() {
        d->outputs.remove(output);
    });
    connect(xdgOutput, &QObject::destroyed, this, [this, output]() {
        d->outputs.remove(output);
    });

    return xdgOutput;
}


XdgOutputManagerV1InterfacePrivate::XdgOutputManagerV1InterfacePrivate(XdgOutputManagerV1Interface *qptr, Display *d)
    : QtWaylandServer::zxdg_output_manager_v1(*d, s_version)
    , q(qptr)
{
}

void XdgOutputManagerV1InterfacePrivate::zxdg_output_manager_v1_get_xdg_output(Resource *resource, uint32_t id, wl_resource *outputResource)
{
    auto output = OutputInterface::get(outputResource);
    if (!output) { // output client is requesting XdgOutput for an Output that doesn't exist
        return;
    }
    auto xdgOutput = outputs.value(output);
    if (!xdgOutput) {
        return;  // client is requesting XdgOutput for an Output that doesn't exist
    }
    xdgOutput->d->add(resource->client(), id, resource->version());
}

void XdgOutputManagerV1InterfacePrivate::zxdg_output_manager_v1_destroy(Resource *resource)
{
    wl_resource_destroy(resource->handle);
}

XdgOutputV1Interface::XdgOutputV1Interface(QObject *parent)
    : QObject(parent)
    , d(new XdgOutputV1InterfacePrivate())
{
}

XdgOutputV1Interface::~XdgOutputV1Interface()
110
111
112
113
114
115
116
117
118
119
120
121
122
{
    // Generated code typically cleans up resource objects when the client calls the relevant destructor
    // In multi-cast mode our wrapper can be deleted at any time whilst a client resource exists.
    // any existing resources that have a pending message will then crash the compositor.

    // Deleting resources ahead of time also resolves this. calls to this resource will no-op
    const QMultiMap<struct ::wl_client*, QtWaylandServer::zxdg_output_v1::Resource*> resourceMap = d->resourceMap();
    for (auto resource : resourceMap)
    {
        wl_resource_destroy(resource->handle);
    }

}
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

void XdgOutputV1Interface::setLogicalSize(const QSize &size)
{
    if (size == d->size) {
        return;
    }
    d->size = size;
    d->dirty = true;
    for (auto resource : d->resourceMap()) {
        d->send_logical_size(resource->handle, size.width(), size.height());
    }
}

QSize XdgOutputV1Interface::logicalSize() const
{
    return d->size;
}

void XdgOutputV1Interface::setLogicalPosition(const QPoint &pos)
{
    if (pos == d->pos) {
        return;
    }
    d->pos = pos;
    d->dirty = true;
    for (auto resource : d->resourceMap()) {
        d->send_logical_position(resource->handle, pos.x(), pos.y());
    }
}

QPoint XdgOutputV1Interface::logicalPosition() const
{
    return d->pos;
}

void XdgOutputV1Interface::setName(const QString &name)
{
    d->name = name;
    // this can only be set once before the client connects
}

void XdgOutputV1Interface::setDescription(const QString &description)
{
    d->description = description;
    // this can only be set once before the client connects
}

void XdgOutputV1Interface::done()
{
    d->doneOnce = true;
    if (!d->dirty) {
        return;
    }
    d->dirty = false;
    for (auto resource : d->resourceMap()) {
        d->send_done(resource->handle);
    }
}

void XdgOutputV1InterfacePrivate::zxdg_output_v1_destroy(Resource *resource)
{
    wl_resource_destroy(resource->handle);
}

void XdgOutputV1InterfacePrivate::zxdg_output_v1_bind_resource(Resource *resource)
{
    send_logical_position(resource->handle, pos.x(), pos.y());
    send_logical_size(resource->handle, size.width(), size.height());
    if (resource->version() >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) {
        send_name(resource->handle, name);
    }
    if (resource->version() >= ZXDG_OUTPUT_V1_DESCRIPTION_SINCE_VERSION) {
        send_description(resource->handle, description);
    }
    if (doneOnce) {
        send_done(resource->handle);
    }
}

}