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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QQuickView>
#include <QWaylandOutput>
#include <QWaylandSeat>
#include <QWaylandKeymap>
#include <wayland-server.h>
#include "global.h"
#include "logging.h"
#include "application.h"
#include "applicationmanager.h"
#include "waylandcompositor.h"
#include "waylandxdgwatchdog.h"
#include <QWaylandWlShell>
#include <QWaylandXdgShell>
#include <QWaylandQuickOutput>
#include <QWaylandTextInputManager>
#include <QWaylandQtTextInputMethodManager>
#include <QWaylandQtWindowManager>
#include "waylandqtamserverextension_p.h"
QT_BEGIN_NAMESPACE_AM
WindowSurface::WindowSurface(WaylandCompositor *comp, QWaylandClient *client, uint id, int version)
: QWaylandQuickSurface(comp, client, id, version)
, m_surface(this)
, m_compositor(comp)
{ }
void WindowSurface::setShellSurface(QWaylandWlShellSurface *shellSurface)
{
Q_ASSERT(!m_xdgSurface);
m_wlSurface = shellSurface;
connect(m_wlSurface, &QWaylandWlShellSurface::pong,
this, &WindowSurface::pong);
}
void WindowSurface::sendResizing(const QSize &size)
{
if (m_topLevel && m_xdgSurface)
m_topLevel->sendResizing(size);
else if (m_popup && m_xdgSurface)
; // do nothing
else if (m_wlSurface)
m_wlSurface->sendConfigure(size, QWaylandWlShellSurface::NoneEdge);
}
QWaylandXdgSurface *WindowSurface::xdgSurface() const
{
return m_xdgSurface;
}
WaylandCompositor *WindowSurface::compositor() const
{
return m_compositor;
}
QString WindowSurface::applicationId() const
{
if (m_xdgSurface && m_xdgSurface->toplevel())
return m_xdgSurface->toplevel()->appId();
return { };
}
bool WindowSurface::isPopup() const
{
return m_popup;
}
QRect WindowSurface::popupGeometry() const
{
return m_popup ? m_popup->configuredGeometry() : QRect();
}
QWaylandSurface *WindowSurface::surface() const
{
return m_surface;
}
qint64 WindowSurface::processId() const
{
return m_surface->client()->processId();
}
QWindow *WindowSurface::outputWindow() const
{
if (QWaylandView *v = m_surface->primaryView())
return v->output()->window();
return nullptr;
}
void WindowSurface::close()
{
if (m_topLevel) {
m_topLevel->sendClose();
} else if (m_popup) {
m_popup->sendPopupDone();
} else {
qCWarning(LogWayland) << "The Wayland surface" << this << "is not using the XDG Shell extension. Unable to send close signal.";
}
}
bool WaylandCompositor::s_nestedLogging = false;
QObject *WaylandCompositor::preConstructor(QObject *parent)
{
static bool once = false;
if (!once) {
s_nestedLogging = (qEnvironmentVariable("WAYLAND_DEBUG") == u"1")
&& (qApp->platformName().startsWith(u"wayland"));
if (s_nestedLogging) {
// we are a nested compositor and logging with WAYLAND_DEBUG=1 just creates a mess,
// as both the server and client side output is intermixed without any tagging.
qunsetenv("WAYLAND_DEBUG");
qCWarning(LogWayland) << "The application manager is running as a nested compositor and WAYLAND_DEBUG=1 is set.\n"
"This would produce a mixed client/server log that is not readable.\n"
"All server side logs will be prefixed with <QtAM> to make them distinguishable.\n"
"You can use https://github.com/rgriebl/wlanalyze to analyze these log files.";
}
once = true;
}
return parent;
}
WaylandCompositor::WaylandCompositor(QQuickWindow *window, const QString &waylandSocketName)
: QWaylandQuickCompositor(preConstructor(nullptr))
, m_wlShell(new QWaylandWlShell(this))
, m_xdgShell(new QWaylandXdgShell(this))
, m_amExtension(new WaylandQtAMServerExtension(this))
, m_qtTextInputMethodManager(new QWaylandQtTextInputMethodManager(this))
, m_textInputManager(new QWaylandTextInputManager(this))
, m_xdgWatchdog(new WaylandXdgWatchdog(m_xdgShell))
{
setupLogging();
// We are instantiating both the semi-official TextInputManager protocol (which has some
// traction upstream, but also has known defects) and our own QtTextInputMethodManager
// (which was added in Qt 6 to mimic our internal C++ interfaces and works perfectly stable).
// Clients can then choose which protocol they want to use. QtVirtualKeyboard will use the
// QtTextInputMethodManager automatically.
//TODO: find out, why all 6.4 based clients are crashing at startup, if we instantiate the two
// extensions in the opposite order.
m_wlShell->setParent(this);
m_xdgShell->setParent(this);
m_amExtension->setParent(this);
m_textInputManager->setParent(this);
m_qtTextInputMethodManager->setParent(this);
setSocketName(waylandSocketName.toUtf8());
registerOutputWindow(window);
connect(this, &QWaylandCompositor::surfaceRequested, this, &WaylandCompositor::doCreateSurface);
connect(m_wlShell, &QWaylandWlShell::wlShellSurfaceRequested, this, &WaylandCompositor::createWlSurface);
connect(m_xdgShell, &QWaylandXdgShell::xdgSurfaceCreated, this, &WaylandCompositor::onXdgSurfaceCreated);
connect(m_xdgShell, &QWaylandXdgShell::toplevelCreated, this, &WaylandCompositor::onTopLevelCreated);
connect(m_xdgShell, &QWaylandXdgShell::popupCreated, this, &WaylandCompositor::onPopupCreated);
auto wmext = new QWaylandQtWindowManager(this);
wmext->setParent(this);
connect(wmext, &QWaylandQtWindowManager::openUrl, this, [](QWaylandClient *client, const QUrl &url) {
if (!ApplicationManager::instance()->fromProcessId(client->processId()).isEmpty())
ApplicationManager::instance()->openUrl(url.toString());
});
create();
// set a sensible default keymap
defaultSeat()->keymap()->setLayout(QLocale::system().name().section(u'_', 1, 1).toLower());
}
WaylandCompositor::~WaylandCompositor()
{
// QWayland leaks like sieve everywhere, but we need this explicit delete to be able
// to suppress the rest via LSAN leak suppression files
delete defaultSeat();
}
void WaylandCompositor::setupLogging()
{
// only need custom logging, if we are running nested and want to log everything
if (!s_nestedLogging)
return;
// The actual logging code below is mostly a verbatim copy of the code in libwayland-server
// in order to keep the log format consistent.
wl_display_add_protocol_logger(display(), [](void *user_data,
enum wl_protocol_logger_type direction,
const struct wl_protocol_logger_message *message) {
bool send = (direction == WL_PROTOCOL_LOGGER_EVENT);
auto target = message->resource ? &message->resource->object : nullptr;
if (!target)
return;
char *buffer;
size_t buffer_length;
auto f = ::open_memstream(&buffer, &buffer_length);
if (!f)
return;
::timespec tp;
::clock_gettime(CLOCK_REALTIME, &tp);
// Wayland bug -- this will overflow unsigned int, but we need to stay compatible with
// the timestamps generated by the libwayland-client logger.
unsigned int time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
::fprintf(f, "<%s> [%7u.%03u] %s%s#%u.%s(",
static_cast<const char *>(user_data),
time / 1000, time % 1000,
send ? " -> " : "",
message->resource->object.interface->name, target->id,
message->message->name);
const char *signature = message->message->signature;
char *realSignatureStart = nullptr;
auto version = ::strtoul(signature, &realSignatureStart, 10);
if (version && realSignatureStart)
signature = realSignatureStart;
for (int i = 0; i < message->arguments_count; ++i, ++signature) {
if (i > 0)
fprintf(f, ", ");
while (signature && (*signature == '?'))
++signature; // ignore nullable
switch (signature ? *signature : '\0') {
case 'u':
fprintf(f, "%u", message->arguments[i].u);
break;
case 'i':
fprintf(f, "%d", message->arguments[i].i);
break;
case 'f':
// The magic number 390625 is 1e8 / 256
if (message->arguments[i].f >= 0) {
fprintf(f, "%d.%08d",
message->arguments[i].f / 256,
390625 * (message->arguments[i].f % 256));
} else {
fprintf(f, "-%d.%08d",
message->arguments[i].f / -256,
-390625 * (message->arguments[i].f % 256));
}
break;
case 's':
if (message->arguments[i].s)
fprintf(f, "\"%s\"", message->arguments[i].s);
else
fprintf(f, "nil");
break;
case 'o':
if (message->arguments[i].o)
fprintf(f, "%s#%u",
message->arguments[i].o->interface->name,
message->arguments[i].o->id);
else
fprintf(f, "nil");
break;
case 'n': {
uint32_t nval;
nval = message->arguments[i].n;
fprintf(f, "new id %s#",
(message->message->types[i]) ?
message->message->types[i]->name :
"[unknown]");
if (nval != 0)
fprintf(f, "%u", nval);
else
fprintf(f, "nil");
break;
}
case 'a':
fprintf(f, "array[%zu]", message->arguments[i].a->size);
break;
case 'h':
fprintf(f, "fd %d", message->arguments[i].h);
break;
default:
qWarning() << "Unknown Wayland signature byte:" << (signature ? *signature : '\0');
Q_ASSERT(false);
break;
}
}
::fprintf(f, ")\n");
if (::fclose(f) == 0) {
::fprintf(stderr, "%s", buffer);
::free(buffer);
}
}, const_cast<char *>("QtAM"));
}
void WaylandCompositor::registerOutputWindow(QQuickWindow* window)
{
auto output = new QWaylandQuickOutput(this, window);
output->setSizeFollowsWindow(true);
m_outputs.append(output);
window->winId();
// Qt doesn't automatically de-focus a Wayland client, if a control in the Sys-UI gets focus
//TODO: check if upstreaming to QtWaylandCompositor is an option
connect(window, &QQuickWindow::activeFocusItemChanged,
this, [this, window]() {
QQuickItem *lastFocusItem = window->property("_am_lastFocusItem").value<QQuickItem *>();
QQuickItem *currentFocusItem = window->activeFocusItem();
window->setProperty("_am_lastFocusItem", QVariant::fromValue(currentFocusItem));
if ((lastFocusItem != currentFocusItem)
&& qobject_cast<QWaylandQuickItem *>(lastFocusItem)
&& !qobject_cast<QWaylandQuickItem *>(currentFocusItem)) {
if (QWaylandSeat *target = defaultSeat())
target->setKeyboardFocus(nullptr);
}
});
}
void WaylandCompositor::setWatchdogTimeouts(std::chrono::milliseconds checkInterval,
std::chrono::milliseconds warnTimeout,
std::chrono::milliseconds killTimeout)
{
m_xdgWatchdog->setTimeouts(checkInterval, warnTimeout, killTimeout);
}
WaylandQtAMServerExtension *WaylandCompositor::amExtension()
{
return m_amExtension;
}
void WaylandCompositor::doCreateSurface(QWaylandClient *client, uint id, int version)
{
(void) new WindowSurface(this, client, id, version);
}
void WaylandCompositor::createWlSurface(QWaylandSurface *surface, const QWaylandResource &resource)
{
WindowSurface *windowSurface = static_cast<WindowSurface *>(surface);
QWaylandWlShellSurface *ss = new QWaylandWlShellSurface(m_wlShell, windowSurface, resource);
windowSurface->setShellSurface(ss);
connect(windowSurface, &QWaylandSurface::hasContentChanged, this, [this, windowSurface]() {
if (windowSurface->hasContent())
emit surfaceMapped(windowSurface);
});
}
void WaylandCompositor::onXdgSurfaceCreated(QWaylandXdgSurface *xdgSurface)
{
WindowSurface *windowSurface = static_cast<WindowSurface*>(xdgSurface->surface());
Q_ASSERT(!windowSurface->m_wlSurface);
windowSurface->m_xdgSurface = xdgSurface;
connect(windowSurface, &QWaylandSurface::hasContentChanged, this, [this, windowSurface]() {
if (windowSurface->hasContent())
emit this->surfaceMapped(windowSurface);
});
emit windowSurface->xdgSurfaceChanged();
}
void WaylandCompositor::onTopLevelCreated(QWaylandXdgToplevel *topLevel, QWaylandXdgSurface *xdgSurface)
{
WindowSurface *windowSurface = static_cast<WindowSurface*>(xdgSurface->surface());
Q_ASSERT(!windowSurface->m_wlSurface);
Q_ASSERT(windowSurface->m_xdgSurface == xdgSurface);
windowSurface->m_topLevel = topLevel;
}
void WaylandCompositor::onPopupCreated(QWaylandXdgPopup *popup, QWaylandXdgSurface *xdgSurface)
{
WindowSurface *windowSurface = static_cast<WindowSurface*>(xdgSurface->surface());
Q_ASSERT(!windowSurface->m_wlSurface);
Q_ASSERT(windowSurface->m_xdgSurface == xdgSurface);
windowSurface->m_popup = popup;
connect(popup, &QWaylandXdgPopup::configuredGeometryChanged,
windowSurface, &WindowSurface::popupGeometryChanged);
}
QT_END_NAMESPACE_AM
#include "moc_waylandcompositor.cpp"
|