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
137#if HAVE_X11
138X11OutputOrderWatcher::X11OutputOrderWatcher(QObject *parent)
139 : OutputOrderWatcher(parent)
140 , m_x11Interface(qGuiApp->nativeInterface<QNativeInterface::QX11Application>())
141{
142 if (!m_x11Interface) [[unlikely]] {
143 Q_ASSERT(false);
144 return;
145 }
146 // This timer is used to signal only when a qscreen for every output is already created, perhaps by monitoring
147 // screenadded/screenremoved and tracking the outputs still missing
148 m_delayTimer = new QTimer(this);
149 m_delayTimer->setSingleShot(true);
150 m_delayTimer->setInterval(0);
151 connect(m_delayTimer, &QTimer::timeout, this, [this]() {
152 refresh();
153 });
154
155 // By default try to use the protocol on x11
156 m_orderProtocolPresent = true;
157
158 qGuiApp->installNativeEventFilter(this);
159 const xcb_query_extension_reply_t *reply = xcb_get_extension_data(m_x11Interface->connection(), &xcb_randr_id);
160 if (!reply || !reply->present) { // SENTRY PLASMA-WORKSPACE-1MMC: XRandr extension is not initialized when using vncserver
161 useFallback(true, "XRandr extension is not initialized");
162 return;
163 }
164
165 m_xrandrExtensionOffset = reply->first_event;
166
167 constexpr const char *effectName = "_KDE_SCREEN_INDEX";
168 xcb_intern_atom_cookie_t atomCookie =
169 xcb_intern_atom_unchecked(m_x11Interface->connection(), false, std::char_traits<char>::length(effectName), effectName);
170 xcb_intern_atom_reply_t *atom(xcb_intern_atom_reply(m_x11Interface->connection(), atomCookie, nullptr));
171 if (!atom) {
172 useFallback(true);
173 return;
174 }
175
176 m_kdeScreenAtom = atom->atom;
177 m_delayTimer->start();
178}
179
180void X11OutputOrderWatcher::refresh()
181{
182 if (!m_orderProtocolPresent) {
184 return;
185 }
187
188 ScopedPointer<xcb_randr_get_screen_resources_current_reply_t> reply(xcb_randr_get_screen_resources_current_reply(
189 m_x11Interface->connection(),
190 xcb_randr_get_screen_resources_current(m_x11Interface->connection(), DefaultRootWindow(m_x11Interface->display())),
191 NULL));
192
193 xcb_timestamp_t timestamp = reply->config_timestamp;
194 int len = xcb_randr_get_screen_resources_current_outputs_length(reply.data());
195 xcb_randr_output_t *randr_outputs = xcb_randr_get_screen_resources_current_outputs(reply.data());
196
197 for (int i = 0; i < len; i++) {
198 ScopedPointer<xcb_randr_get_output_info_reply_t> output(
199 xcb_randr_get_output_info_reply(m_x11Interface->connection(),
200 xcb_randr_get_output_info(m_x11Interface->connection(), randr_outputs[i], timestamp),
201 NULL));
202
203 if (output == NULL || output->connection == XCB_RANDR_CONNECTION_DISCONNECTED || output->crtc == 0) {
204 continue;
205 }
206
207 auto orderCookie = xcb_randr_get_output_property(m_x11Interface->connection(), randr_outputs[i], m_kdeScreenAtom, XCB_ATOM_ANY, 0, 100, false, false);
208 ScopedPointer<xcb_randr_get_output_property_reply_t> orderReply(
209 xcb_randr_get_output_property_reply(m_x11Interface->connection(), orderCookie, nullptr));
210 // If there is even a single screen without _KDE_SCREEN_INDEX info, fall back to alphabetical ordering
211 if (!orderReply) {
212 useFallback(true);
213 return;
214 }
215
216 if (!(orderReply->type == XCB_ATOM_INTEGER && orderReply->format == 32 && orderReply->num_items == 1)) {
217 useFallback(true);
218 return;
219 }
220
221 const uint32_t order = *xcb_randr_get_output_property_data(orderReply.data());
222
223 if (order > 0) { // 0 is the special case for disabled, so we ignore it
224 orderMap.emplace_back(order,
225 QString::fromUtf8(reinterpret_cast<const char *>(xcb_randr_get_output_info_name(output.get())),
226 xcb_randr_get_output_info_name_length(output.get())));
227 }
228 }
229
230 const auto screens = qGuiApp->screens();
231 std::vector<QString> screenNames;
232 screenNames.reserve(screens.size());
233 std::transform(screens.begin(), screens.end(), std::back_inserter(screenNames), [](const QScreen *screen) {
234 return screen->name();
235 });
236 const bool isScreenPresent = std::all_of(orderMap.cbegin(), orderMap.cend(), [&screenNames](const auto &pr) {
237 return std::ranges::find(screenNames, std::get<QString>(pr)) != screenNames.end();
238 });
239 if (!isScreenPresent) [[unlikely]] {
240 // if the pending output order refers to screens
241 // we don't know of yet, try again next time a screen is added
242 // this seems unlikely given we have the server lock and the timing thing
243 m_delayTimer->start();
244 return;
245 }
246
247 std::sort(orderMap.begin(), orderMap.end());
248
249 // Rather verbose ifdef due to clang support of ranges API
250#if defined(__clang__) && __clang_major__ < 16
251 const auto getAllValues = [](const QList<std::pair<uint, QString>> &orderMap) -> QList<QString> {
252 QList<QString> values;
253 values.reserve(orderMap.size());
254 std::transform(orderMap.begin(), orderMap.end(), std::back_inserter(values), [](const auto &pair) {
255 return pair.second;
256 });
257 return values;
258 };
259 if (const auto pendingOutputs = getAllValues(orderMap); pendingOutputs != m_outputOrder) {
260 m_outputOrder = pendingOutputs;
261#else
262 if (const auto pendingOutputs = std::views::values(std::as_const(orderMap)); !std::ranges::equal(pendingOutputs, std::as_const(m_outputOrder))) {
263 m_outputOrder = QStringList{pendingOutputs.begin(), pendingOutputs.end()};
264#endif
265 Q_EMIT outputOrderChanged(m_outputOrder);
266 }
267}
268
269bool X11OutputOrderWatcher::nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result)
270{
271 Q_UNUSED(result);
272 // a particular edge case: when we switch the only enabled screen
273 // we don't have any signal about it, the primary screen changes but we have the same old QScreen* getting recycled
274 // see https://bugs.kde.org/show_bug.cgi?id=373880
275 // 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
276 if (eventType[0] != 'x') {
277 return false;
278 }
279
280 xcb_generic_event_t *ev = static_cast<xcb_generic_event_t *>(message);
281
282 const auto responseType = XCB_EVENT_RESPONSE_TYPE(ev);
283
284 if (responseType == m_xrandrExtensionOffset + XCB_RANDR_NOTIFY) {
285 auto *randrEvent = reinterpret_cast<xcb_randr_notify_event_t *>(ev);
286 if (randrEvent->subCode == XCB_RANDR_NOTIFY_OUTPUT_PROPERTY) {
287 xcb_randr_output_property_t property = randrEvent->u.op;
288
289 if (property.atom == m_kdeScreenAtom) {
290 // Force an X11 roundtrip to make sure we have all other
291 // screen events in the buffer when we process the deferred refresh
292 useFallback(false);
293 roundtrip();
294 m_delayTimer->start();
295 }
296 } else if (randrEvent->subCode == XCB_RANDR_NOTIFY_OUTPUT_CHANGE) {
297 // When the ast screen is removed, its qscreen becomes name ":0.0" as the fake screen, but nothing happens really,
298 // screenpool doesn't notice (and looking at the assert_x there are, that was expected"
299 // then the screen gets connected again, a new screen gets conencted, the old 0.0 one
300 // gets disconnected, but the screen order stuff doesn't say anything as it's still
301 // the same connector name as before
302 // so screenpool finds itself with an empty screenorder
303 if (randrEvent->u.oc.connection == XCB_RANDR_CONNECTION_DISCONNECTED) {
304 // Cause ScreenPool to reevaluate screenorder again, so the screen :0.0 will
305 // be correctly moved to fakeScreens
306 m_delayTimer->start();
307 }
308 }
309 }
310 return false;
311}
312
313void X11OutputOrderWatcher::roundtrip() const
314{
315 const auto cookie = xcb_get_input_focus(m_x11Interface->connection());
316 xcb_generic_error_t *error = nullptr;
317 ScopedPointer<xcb_get_input_focus_reply_t> sync(xcb_get_input_focus_reply(m_x11Interface->connection(), cookie, &error));
318 if (error) {
319 free(error);
320 }
321}
322#endif
323
324WaylandOutputOrderWatcher::WaylandOutputOrderWatcher(QObject *parent)
325 : OutputOrderWatcher(parent)
326{
327 // Asking for primaryOutputName() before this happened, will return qGuiApp->primaryScreen()->name() anyways, so set it so the outputOrderChanged will
328 // have parameters that are coherent
330
331 auto outputListManagement = new WaylandOutputOrder(this);
332 m_orderProtocolPresent = outputListManagement->isActive();
333 if (!m_orderProtocolPresent) {
334 useFallback(true, "kde_output_order_v1 protocol is not available");
335 return;
336 }
337 connect(outputListManagement, &WaylandOutputOrder::outputOrderChanged, this, [this](const QStringList &order) {
338 m_pendingOutputOrder = order;
339
340 if (hasAllScreens()) {
341 if (m_pendingOutputOrder != m_outputOrder) {
342 m_outputOrder = m_pendingOutputOrder;
343 Q_EMIT outputOrderChanged(m_outputOrder);
344 }
345 }
346 // otherwise wait for next QGuiApp screenAdded/removal
347 // to keep things in sync
348 });
349}
350
351bool WaylandOutputOrderWatcher::hasAllScreens() const
352{
353 // for each name in our ordered list, find a screen with that name
354 for (const auto &name : std::as_const(m_pendingOutputOrder)) {
355 bool present = false;
356 for (auto *s : qApp->screens()) {
357 if (s->name() == name) {
358 present = true;
359 break;
360 }
361 }
362 if (!present) {
363 return false;
364 }
365 }
366 return true;
367}
368
369void WaylandOutputOrderWatcher::refresh()
370{
371 if (!m_orderProtocolPresent) {
373 return;
374 }
375
376 if (!hasAllScreens()) {
377 return;
378 }
379
380 if (m_outputOrder != m_pendingOutputOrder) {
381 m_outputOrder = m_pendingOutputOrder;
382 Q_EMIT outputOrderChanged(m_outputOrder);
383 }
384}
385
386#include "outputorderwatcher.moc"
387
388#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 Apr 25 2025 11:55:44 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.