File indexing completed on 2024-04-28 16:54:58

0001 /*
0002     SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractNativeEventFilter>
0010 #include <QObject>
0011 
0012 #include <config-plasma.h>
0013 
0014 #if HAVE_X11
0015 #include <xcb/xcb.h>
0016 #endif
0017 
0018 class QScreen;
0019 class QTimer;
0020 
0021 /**
0022  * This class watches for output ordering changes from
0023  * the relevant backend
0024  */
0025 class OutputOrderWatcher : public QObject
0026 {
0027     Q_OBJECT
0028 public:
0029     /**
0030      * Create the correct OutputOrderWatcher
0031      */
0032     static OutputOrderWatcher *instance(QObject *parent);
0033 
0034     /**
0035      * Returns the list of outputs in order
0036      *
0037      * At the time of change signalling all Screens are
0038      * guaranteed to exist in QGuiApplication.
0039      *
0040      * After screen removal this can temporarily contain
0041      * dead entries.
0042      */
0043     QStringList outputOrder() const;
0044 
0045     /**
0046      * @internal
0047      * For X11 we know libkscreen takes a server grab whilst changing properties.
0048      * This means we know at the time of any runtime screen addition and removal the priorities will
0049      * be up-to-date when we poll them.
0050      *
0051      * For dynamic ordering changes without screen changes we will get the change property multiple times.
0052      * A timer is used to batch them.
0053      *
0054      *  A caveat on X11 is the initial startup where plasmashell may query screen information before
0055      * kscreen has set properties.
0056      * This should resolve itself as a dynamic re-ordering when kscreen does start.
0057      *
0058      * For wayland we know kwin sends the priority order whenever screen changes are made.
0059      * As creating screens requires an extra async call to kwin (to bind to the output)
0060      * we always get the new priority ordering before and screen additions.
0061      * We should always have correct values on startup.
0062      */
0063     virtual void refresh();
0064 Q_SIGNALS:
0065     void outputOrderChanged(const QStringList &outputOrder);
0066 
0067 protected:
0068     OutputOrderWatcher(QObject *parent);
0069     /**
0070      * Backend failed, use QScreen based implementaion
0071      */
0072     void useFallback(bool fallback);
0073 
0074     QStringList m_outputOrder;
0075     bool m_orderProtocolPresent = false;
0076 
0077 private:
0078 };
0079 
0080 #if HAVE_X11
0081 class X11OutputOrderWatcher : public OutputOrderWatcher, public QAbstractNativeEventFilter
0082 {
0083     Q_OBJECT
0084 public:
0085     X11OutputOrderWatcher(QObject *parent);
0086     void refresh() override;
0087 
0088 protected:
0089 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0090     bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override;
0091 #else
0092     bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result) override;
0093 #endif
0094 private:
0095     QTimer *m_delayTimer = nullptr; // This is just to simulate the protocol that will be guaranteed to always arrive after screens additions and removals
0096     // Xrandr
0097     int m_xrandrExtensionOffset;
0098     xcb_atom_t m_kdeScreenAtom = XCB_ATOM_NONE;
0099 };
0100 #endif
0101 
0102 class WaylandOutputOrderWatcher : public OutputOrderWatcher
0103 {
0104     Q_OBJECT
0105 public:
0106     WaylandOutputOrderWatcher(QObject *parent);
0107     void refresh() override;
0108 
0109 private:
0110     bool hasAllScreens() const;
0111     QStringList m_pendingOutputOrder;
0112 };