Plasma-workspace

outputorderwatcher.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
3 SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "outputorderwatcher.h"
9
10#include <ranges>
11
12#include <QScreen>
13#include <QTimer>
14
15#include <KWindowSystem>
16
17#include "qwayland-kde-output-order-v1.h"
18#include <QtWaylandClient/QWaylandClientExtension>
19#include <QtWaylandClient/QtWaylandClientVersion>
20
21#if HAVE_X11
22#include <X11/Xlib.h>
23#include <xcb/randr.h>
24#include <xcb/xcb_event.h>
25#endif // HAVE_X11
26
27template<typename T>
29
30class WaylandOutputOrder : public QWaylandClientExtensionTemplate<WaylandOutputOrder, &QtWayland::kde_output_order_v1::destroy>,
31 public QtWayland::kde_output_order_v1
32{
33 Q_OBJECT
34public:
35 WaylandOutputOrder(QObject *parent)
36 : QWaylandClientExtensionTemplate(1)
37 {
38 setParent(parent);
39 initialize();
40 }
41
42protected:
43 void kde_output_order_v1_output(const QString &outputName) override
44 {
45 if (m_done) {
46 m_outputOrder.clear();
47 m_done = false;
48 }
49 m_outputOrder.append(outputName);
50 }
51
52 void kde_output_order_v1_done() override
53 {
54 // If no output arrived it means we don't have *any* usable output
55 if (m_done) {
56 m_outputOrder.clear();
57 }
58 m_done = true;
59 Q_EMIT outputOrderChanged(m_outputOrder);
60 }
61
62Q_SIGNALS:
63 void outputOrderChanged(const QStringList &outputName);
64
65private:
66 QStringList m_outputOrder;
67 bool m_done = true;
68};
69
70OutputOrderWatcher::OutputOrderWatcher(QObject *parent)
71 : QObject(parent)
72{
75}
76
77void OutputOrderWatcher::useFallback(bool fallback, const char *reason)
78{
79 m_orderProtocolPresent = !fallback;
80 if (fallback) {
81 if (reason) {
82 qCritical() << "OutputOrderWatcher may not work as expected. Reason:" << reason;
83 }
85 refresh();
86 }
87}
88
90{
91#if HAVE_X11
93 return new X11OutputOrderWatcher(parent);
94 } else
95#endif
97 return new WaylandOutputOrderWatcher(parent);
98 }
99 // return default impl that does something at least
100 return new OutputOrderWatcher(parent);
101}
102
104{
105 Q_ASSERT(!m_orderProtocolPresent);
106
107 QStringList pendingOutputOrder;
108
109 pendingOutputOrder.clear();
110 for (auto *s : qApp->screens()) {
111 pendingOutputOrder.append(s->name());
112 }
113
114 auto outputLess = [](const QString &c1, const QString &c2) {
115 if (c1 == qApp->primaryScreen()->name()) {
116 return true;
117 } else if (c2 == qApp->primaryScreen()->name()) {
118 return false;
119 } else {
120 return c1 < c2;
121 }
122 };
123 std::sort(pendingOutputOrder.begin(), pendingOutputOrder.end(), outputLess);
124
125 if (m_outputOrder != pendingOutputOrder) {
126 m_outputOrder = pendingOutputOrder;
127 Q_EMIT outputOrderChanged(m_outputOrder);
128 }
129 return;
130}
131
133{
134 return m_outputOrder;
135}
136
137X11OutputOrderWatcher::X11OutputOrderWatcher(QObject *parent)
138 : OutputOrderWatcher(parent)
139 , m_x11Interface(qGuiApp->nativeInterface<QNativeInterface::QX11Application>())
140{
141 if (!m_x11Interface) [[unlikely]] {
142 Q_ASSERT(false);
143 return;
144 }
145 // This timer is used to signal only when a qscreen for every output is already created, perhaps by monitoring
146 // screenadded/screenremoved and tracking the outputs still missing
147 m_delayTimer = new QTimer(this);
148 m_delayTimer->setSingleShot(true);
149 m_delayTimer->setInterval(0);
150 connect(m_delayTimer, &QTimer::timeout, this, [this]() {
151 refresh();
152 });
153
154 // By default try to use the protocol on x11
155 m_orderProtocolPresent = true;
156
157 qGuiApp->installNativeEventFilter(this);
158 const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_x11Interface->connection(), &xcb_randr_id);
159 if (!reply || !reply->present) { // SENTRY PLASMA-WORKSPACE-1MMC: XRandr extension is not initialized when using vncserver
160 useFallback(true, "XRandr extension is not initialized");
161 return;
162 }
163
164 m_xrandrExtensionOffset = reply->first_event;
165
166 constexpr const char *effectName = "_KDE_SCREEN_INDEX";
167 xcb_intern_atom_cookie_t atomCookie =
168 xcb_intern_atom_unchecked(m_x11Interface->connection(), false, std::char_traits<char>::length(effectName), effectName);
169 xcb_intern_atom_reply_t *atom(xcb_intern_atom_reply(m_x11Interface->connection(), atomCookie, nullptr));
170 if (!atom) {
171 useFallback(true);
172 return;
173 }
174
175 m_kdeScreenAtom = atom->atom;
176 m_delayTimer->start();
177}
178
179void X11OutputOrderWatcher::refresh()
180{
181 if (!m_orderProtocolPresent) {
183 return;
184 }
186
187 ScopedPointer<xcb_randr_get_screen_resources_current_reply_t> reply(xcb_randr_get_screen_resources_current_reply(
188 m_x11Interface->connection(),
189 xcb_randr_get_screen_resources_current(m_x11Interface->connection(), DefaultRootWindow(m_x11Interface->display())),
190 NULL));
191
192 xcb_timestamp_t timestamp = reply->config_timestamp;
193 int len = xcb_randr_get_screen_resources_current_outputs_length(reply.data());
194 xcb_randr_output_t *randr_outputs = xcb_randr_get_screen_resources_current_outputs(reply.data());
195
196 for (int i = 0; i < len; i++) {
197 ScopedPointer<xcb_randr_get_output_info_reply_t> output(
198 xcb_randr_get_output_info_reply(m_x11Interface->connection(),
199 xcb_randr_get_output_info(m_x11Interface->connection(), randr_outputs[i], timestamp),
200 NULL));
201
202 if (output == NULL || output->connection == XCB_RANDR_CONNECTION_DISCONNECTED || output->crtc == 0) {
203 continue;
204 }
205
206 auto orderCookie = xcb_randr_get_output_property(m_x11Interface->connection(), randr_outputs[i], m_kdeScreenAtom, XCB_ATOM_ANY, 0, 100, false, false);
207 ScopedPointer<xcb_randr_get_output_property_reply_t> orderReply(
208 xcb_randr_get_output_property_reply(m_x11Interface->connection(), orderCookie, nullptr));
209 // If there is even a single screen without _KDE_SCREEN_INDEX info, fall back to alphabetical ordering
210 if (!orderReply) {
211 useFallback(true);
212 return;
213 }
214
215 if (!(orderReply->type == XCB_ATOM_INTEGER && orderReply->format == 32 && orderReply->num_items == 1)) {
216 useFallback(true);
217 return;
218 }
219
220 const uint32_t order = *xcb_randr_get_output_property_data(orderReply.data());
221
222 if (order > 0) { // 0 is the special case for disabled, so we ignore it
223 orderMap.emplace_back(order,
224 QString::fromUtf8(reinterpret_cast<const char *>(xcb_randr_get_output_info_name(output.get())),
225 xcb_randr_get_output_info_name_length(output.get())));
226 }
227 }
228
229 const auto screens = qGuiApp->screens();
230 std::vector<QString> screenNames;
231 screenNames.reserve(screens.size());
232 std::transform(screens.begin(), screens.end(), std::back_inserter(screenNames), [](const QScreen *screen) {
233 return screen->name();
234 });
235 const bool isScreenPresent = std::all_of(orderMap.cbegin(), orderMap.cend(), [&screenNames](const auto &pr) {
236 return std::ranges::find(screenNames, std::get<QString>(pr)) != screenNames.end();
237 });
238 if (!isScreenPresent) [[unlikely]] {
239 // if the pending output order refers to screens
240 // we don't know of yet, try again next time a screen is added
241 // this seems unlikely given we have the server lock and the timing thing
242 m_delayTimer->start();
243 return;
244 }
245
246 std::sort(orderMap.begin(), orderMap.end());
247
248 // Rather verbose ifdef due to clang support of ranges API
249#if defined(__clang__) && __clang_major__ < 16
250 const auto getAllValues = [](const QList<std::pair<uint, QString>> &orderMap) -> QList<QString> {
251 QList<QString> values;
252 values.reserve(orderMap.size());
253 std::transform(orderMap.begin(), orderMap.end(), std::back_inserter(values), [](const auto &pair) {
254 return pair.second;
255 });
256 return values;
257 };
258 if (const auto pendingOutputs = getAllValues(orderMap); pendingOutputs != m_outputOrder) {
259 m_outputOrder = pendingOutputs;
260#else
261 if (const auto pendingOutputs = std::views::values(std::as_const(orderMap)); !std::ranges::equal(pendingOutputs, std::as_const(m_outputOrder))) {
262 m_outputOrder = QStringList{pendingOutputs.begin(), pendingOutputs.end()};
263#endif
264 Q_EMIT outputOrderChanged(m_outputOrder);
265 }
266}
267
268bool X11OutputOrderWatcher::nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result)
269{
270 Q_UNUSED(result);
271 // a particular edge case: when we switch the only enabled screen
272 // we don't have any signal about it, the primary screen changes but we have the same old QScreen* getting recycled
273 // see https://bugs.kde.org/show_bug.cgi?id=373880
274 // if this slot will be invoked many times, their//second time on will do nothing as name and primaryOutputName will be the same by then
275 if (eventType[0] != 'x') {
276 return false;
277 }
278
279 xcb_generic_event_t *ev = static_cast<xcb_generic_event_t *>(message);
280
281 const auto responseType = XCB_EVENT_RESPONSE_TYPE(ev);
282
283 if (responseType == m_xrandrExtensionOffset + XCB_RANDR_NOTIFY) {
284 auto *randrEvent = reinterpret_cast<xcb_randr_notify_event_t *>(ev);
285 if (randrEvent->subCode == XCB_RANDR_NOTIFY_OUTPUT_PROPERTY) {
286 xcb_randr_output_property_t property = randrEvent->u.op;
287
288 if (property.atom == m_kdeScreenAtom) {
289 // Force an X11 roundtrip to make sure we have all other
290 // screen events in the buffer when we process the deferred refresh
291 useFallback(false);
292 roundtrip();
293 m_delayTimer->start();
294 }
295 } else if (randrEvent->subCode == XCB_RANDR_NOTIFY_OUTPUT_CHANGE) {
296 // When the ast screen is removed, its qscreen becomes name ":0.0" as the fake screen, but nothing happens really,
297 // screenpool doesn't notice (and looking at the assert_x there are, that was expected"
298 // then the screen gets connected again, a new screen gets conencted, the old 0.0 one
299 // gets disconnected, but the screen order stuff doesn't say anything as it's still
300 // the same connector name as before
301 // so screenpool finds itself with an empty screenorder
302 if (randrEvent->u.oc.connection == XCB_RANDR_CONNECTION_DISCONNECTED) {
303 // Cause ScreenPool to reevaluate screenorder again, so the screen :0.0 will
304 // be correctly moved to fakeScreens
305 m_delayTimer->start();
306 }
307 }
308 }
309 return false;
310}
311
312void X11OutputOrderWatcher::roundtrip() const
313{
314 const auto cookie = xcb_get_input_focus(m_x11Interface->connection());
315 xcb_generic_error_t *error = nullptr;
316 ScopedPointer<xcb_get_input_focus_reply_t> sync(xcb_get_input_focus_reply(m_x11Interface->connection(), cookie, &error));
317 if (error) {
318 free(error);
319 }
320}
321
322WaylandOutputOrderWatcher::WaylandOutputOrderWatcher(QObject *parent)
323 : OutputOrderWatcher(parent)
324{
325 // Asking for primaryOutputName() before this happened, will return qGuiApp->primaryScreen()->name() anyways, so set it so the outputOrderChanged will
326 // have parameters that are coherent
328
329 auto outputListManagement = new WaylandOutputOrder(this);
330 m_orderProtocolPresent = outputListManagement->isActive();
331 if (!m_orderProtocolPresent) {
332 useFallback(true, "kde_output_order_v1 protocol is not available");
333 return;
334 }
335 connect(outputListManagement, &WaylandOutputOrder::outputOrderChanged, this, [this](const QStringList &order) {
336 m_pendingOutputOrder = order;
337
338 if (hasAllScreens()) {
339 if (m_pendingOutputOrder != m_outputOrder) {
340 m_outputOrder = m_pendingOutputOrder;
341 Q_EMIT outputOrderChanged(m_outputOrder);
342 }
343 }
344 // otherwise wait for next QGuiApp screenAdded/removal
345 // to keep things in sync
346 });
347}
348
349bool WaylandOutputOrderWatcher::hasAllScreens() const
350{
351 // for each name in our ordered list, find a screen with that name
352 for (const auto &name : std::as_const(m_pendingOutputOrder)) {
353 bool present = false;
354 for (auto *s : qApp->screens()) {
355 if (s->name() == name) {
356 present = true;
357 break;
358 }
359 }
360 if (!present) {
361 return false;
362 }
363 }
364 return true;
365}
366
367void WaylandOutputOrderWatcher::refresh()
368{
369 if (!m_orderProtocolPresent) {
371 return;
372 }
373
374 if (!hasAllScreens()) {
375 return;
376 }
377
378 if (m_outputOrder != m_pendingOutputOrder) {
379 m_outputOrder = m_pendingOutputOrder;
380 Q_EMIT outputOrderChanged(m_outputOrder);
381 }
382}
383
384#include "outputorderwatcher.moc"
385
386#include "moc_outputorderwatcher.cpp"
static bool isPlatformX11()
static bool isPlatformWayland()
This class watches for output ordering changes from the relevant backend.
static OutputOrderWatcher * instance(QObject *parent)
Create the correct OutputOrderWatcher.
QStringList outputOrder() const
Returns the list of outputs in order.
void useFallback(bool fallback, const char *reason=nullptr)
Backend failed, use QScreen based implementaion.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
void initialize(StandardShortcut id)
void primaryScreenChanged(QScreen *screen)
void screenAdded(QScreen *screen)
void screenRemoved(QScreen *screen)
void append(QList< T > &&value)
iterator begin()
const_iterator cbegin() const const
const_iterator cend() const const
void clear()
reference emplace_back(Args &&... args)
iterator end()
void reserve(qsizetype size)
qsizetype size() const const
QObject(QObject *parent)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QString fromUtf8(QByteArrayView str)
UniqueConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 21 2025 11:51:10 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.