-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathdisplay.cpp
308 lines (261 loc) · 7.71 KB
/
display.cpp
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
/*
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "display.h"
#include "config-kwin.h"
#include "clientconnection.h"
#include "display_p.h"
#include "linuxdmabufv1clientbuffer_p.h"
#include "output.h"
#include "shmclientbuffer_p.h"
#include "utils/common.h"
#include <poll.h>
#include <string.h>
#include <sys/socket.h>
#include <QAbstractEventDispatcher>
#include <QCoreApplication>
#include <QDebug>
#include <QRect>
namespace KWin
{
DisplayPrivate *DisplayPrivate::get(Display *display)
{
return display->d.get();
}
DisplayPrivate::DisplayPrivate(Display *q)
: q(q)
{
}
void DisplayPrivate::registerSocketName(const QString &socketName)
{
socketNames.append(socketName);
Q_EMIT q->socketNamesChanged();
}
Display::Display(QObject *parent)
: QObject(parent)
, d(new DisplayPrivate(this))
{
d->display = wl_display_create();
d->loop = wl_display_get_event_loop(d->display);
}
Display::~Display()
{
wl_display_destroy_clients(d->display);
wl_display_destroy(d->display);
}
bool Display::addSocketFileDescriptor(int fileDescriptor, const QString &name)
{
if (wl_display_add_socket_fd(d->display, fileDescriptor)) {
qCWarning(KWIN_CORE, "Failed to add %d fd to display", fileDescriptor);
return false;
}
if (!name.isEmpty()) {
d->registerSocketName(name);
}
return true;
}
bool Display::addSocketName(const QString &name)
{
if (name.isEmpty()) {
const char *socket = wl_display_add_socket_auto(d->display);
if (!socket) {
qCWarning(KWIN_CORE, "Failed to find a free display socket");
return false;
}
d->registerSocketName(QString::fromUtf8(socket));
} else {
if (wl_display_add_socket(d->display, qPrintable(name))) {
qCWarning(KWIN_CORE, "Failed to add %s socket to display", qPrintable(name));
return false;
}
d->registerSocketName(name);
}
return true;
}
QStringList Display::socketNames() const
{
return d->socketNames;
}
bool Display::start()
{
if (d->running) {
return true;
}
const int fileDescriptor = wl_event_loop_get_fd(d->loop);
if (fileDescriptor == -1) {
qCWarning(KWIN_CORE) << "Did not get the file descriptor for the event loop";
return false;
}
d->socketNotifier = new QSocketNotifier(fileDescriptor, QSocketNotifier::Read, this);
connect(d->socketNotifier, &QSocketNotifier::activated, this, &Display::dispatchEvents);
QAbstractEventDispatcher *dispatcher = QCoreApplication::eventDispatcher();
connect(dispatcher, &QAbstractEventDispatcher::aboutToBlock, this, &Display::flush);
d->running = true;
Q_EMIT runningChanged(true);
return true;
}
void Display::dispatchEvents()
{
if (wl_event_loop_dispatch(d->loop, 0) != 0) {
qCWarning(KWIN_CORE) << "Error on dispatching Wayland event loop";
}
}
void Display::flush()
{
wl_display_flush_clients(d->display);
}
void Display::createShm()
{
Q_ASSERT(d->display);
new ShmClientBufferIntegration(this);
}
quint32 Display::nextSerial()
{
return wl_display_next_serial(d->display);
}
quint32 Display::serial()
{
return wl_display_get_serial(d->display);
}
bool Display::isRunning() const
{
return d->running;
}
Display::operator wl_display *()
{
return d->display;
}
Display::operator wl_display *() const
{
return d->display;
}
QList<OutputInterface *> Display::outputs() const
{
return d->outputs;
}
QList<OutputDeviceV2Interface *> Display::outputDevices() const
{
return d->outputdevicesV2;
}
QList<OutputInterface *> Display::outputsIntersecting(const QRect &rect) const
{
QList<OutputInterface *> outputs;
for (auto *output : std::as_const(d->outputs)) {
if (output->handle()->geometry().intersects(rect)) {
outputs << output;
}
}
return outputs;
}
OutputInterface *Display::largestIntersectingOutput(const QRect &rect) const
{
OutputInterface *returnOutput = nullptr;
uint64_t biggestArea = 0;
for (auto *output : std::as_const(d->outputs)) {
const QRect intersect = output->handle()->geometry().intersected(rect);
const uint64_t area = intersect.width() * intersect.height();
if (area > biggestArea) {
biggestArea = area;
returnOutput = output;
}
}
return returnOutput;
}
QList<SeatInterface *> Display::seats() const
{
return d->seats;
}
ClientConnection *Display::getConnection(wl_client *client)
{
// TODO: Use wl_client_set_user_data() when we start requiring libwayland-server that has it, and remove client lists here and in ClientConnection.
Q_ASSERT(client);
auto it = std::find_if(d->clients.constBegin(), d->clients.constEnd(), [client](ClientConnection *c) {
return c->client() == client;
});
if (it != d->clients.constEnd()) {
return *it;
}
// no ConnectionData yet, create it
auto c = new ClientConnection(client, this);
d->clients << c;
connect(c, &ClientConnection::disconnected, this, [this](ClientConnection *c) {
Q_EMIT clientDisconnected(c);
});
connect(c, &ClientConnection::destroyed, this, [this, c]() {
d->clients.removeOne(c);
});
Q_EMIT clientConnected(c);
return c;
}
ClientConnection *Display::createClient(int fd)
{
Q_ASSERT(fd != -1);
Q_ASSERT(d->display);
wl_client *c = wl_client_create(d->display, fd);
if (!c) {
return nullptr;
}
return getConnection(c);
}
GraphicsBuffer *Display::bufferForResource(wl_resource *resource)
{
if (auto buffer = LinuxDmaBufV1ClientBuffer::get(resource)) {
return buffer;
} else if (auto buffer = ShmClientBuffer::get(resource)) {
return buffer;
} else {
Q_ASSERT_X(false, Q_FUNC_INFO, "Failed to find matching GraphicsBuffer for wl_resource");
return nullptr;
}
}
void Display::setDefaultMaxBufferSize(size_t max)
{
#if HAVE_WL_DISPLAY_SET_DEFAULT_MAX_BUFFER_SIZE
wl_display_set_default_max_buffer_size(d->display, max);
#endif
}
SecurityContext::SecurityContext(Display *display, FileDescriptor &&listenFd, FileDescriptor &&closeFd, const QString &appId)
: QObject(display)
, m_display(display)
, m_listenFd(std::move(listenFd))
, m_closeFd(std::move(closeFd))
, m_appId(appId)
{
qCDebug(KWIN_CORE) << "Adding listen fd for" << appId;
auto closeSocketWatcher = new QSocketNotifier(m_closeFd.get(), QSocketNotifier::Read, this);
connect(closeSocketWatcher, &QSocketNotifier::activated, this, &SecurityContext::onCloseFdActivated);
if (m_closeFd.isClosed()) {
deleteLater();
return;
}
auto listenFdListener = new QSocketNotifier(m_listenFd.get(), QSocketNotifier::Read, this);
connect(listenFdListener, &QSocketNotifier::activated, this, &SecurityContext::onListenFdActivated);
}
SecurityContext::~SecurityContext()
{
qCDebug(KWIN_CORE) << "Removing listen fd for " << m_appId;
}
void SecurityContext::onListenFdActivated(QSocketDescriptor socketDescriptor)
{
const int clientFd = accept4(socketDescriptor, nullptr, nullptr, SOCK_CLOEXEC);
if (clientFd < 0) {
qCWarning(KWIN_CORE) << "Failed to accept client from security listen FD:" << strerror(errno);
return;
}
auto client = m_display->createClient(clientFd);
if (!client) {
close(clientFd);
return;
}
client->setSecurityContextAppId(m_appId);
}
void SecurityContext::onCloseFdActivated()
{
if (m_closeFd.isClosed()) {
deleteLater();
}
}
} // namespace KWin
#include "moc_display.cpp"