File indexing completed on 2024-10-13 13:14:58

0001 /*
0002     SPDX-FileCopyrightText: 2018 Roman Gilg <subdiff@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 #ifndef POINTERCONSTRAINTSTEST_H
0007 #define POINTERCONSTRAINTSTEST_H
0008 
0009 #include <QObject>
0010 #include <QQuickView>
0011 
0012 #include <xcb/xcb.h>
0013 
0014 namespace KWayland
0015 {
0016 namespace Client
0017 {
0018 
0019 class ConnectionThread;
0020 class Registry;
0021 class Compositor;
0022 class Seat;
0023 class Pointer;
0024 class PointerConstraints;
0025 class LockedPointer;
0026 class ConfinedPointer;
0027 
0028 }
0029 }
0030 
0031 class MainWindow;
0032 
0033 class Backend : public QObject
0034 {
0035     Q_OBJECT
0036 public:
0037     Backend(QObject *parent = nullptr)
0038         : QObject(parent)
0039     {
0040     }
0041 
0042     Q_PROPERTY(int mode READ mode CONSTANT)
0043     Q_PROPERTY(bool lockHint MEMBER m_lockHint NOTIFY lockHintChanged)
0044     Q_PROPERTY(bool errorsAllowed READ errorsAllowed WRITE setErrorsAllowed NOTIFY errorsAllowedChanged)
0045 
0046     virtual void init(QQuickView *view)
0047     {
0048         m_view = view;
0049     }
0050     int mode() const
0051     {
0052         return (int)m_mode;
0053     }
0054 
0055     bool lockHint() const
0056     {
0057         return m_lockHint;
0058     }
0059     bool errorsAllowed() const
0060     {
0061         return m_errorsAllowed;
0062     }
0063     void setErrorsAllowed(bool set)
0064     {
0065         if (m_errorsAllowed == set) {
0066             return;
0067         }
0068         m_errorsAllowed = set;
0069         Q_EMIT errorsAllowedChanged();
0070     }
0071 
0072     Q_INVOKABLE virtual void lockRequest(bool persistent = true, QRect region = QRect())
0073     {
0074     }
0075     Q_INVOKABLE virtual void unlockRequest()
0076     {
0077     }
0078 
0079     Q_INVOKABLE virtual void confineRequest(bool persistent = true, QRect region = QRect())
0080     {
0081     }
0082     Q_INVOKABLE virtual void unconfineRequest()
0083     {
0084     }
0085     Q_INVOKABLE virtual void hideAndConfineRequest(bool confineBeforeHide = false)
0086     {
0087     }
0088     Q_INVOKABLE virtual void undoHideRequest()
0089     {
0090     }
0091 
0092 Q_SIGNALS:
0093     void confineChanged(bool confined);
0094     void lockChanged(bool locked);
0095     void lockHintChanged();
0096     void errorsAllowedChanged();
0097     void forceSurfaceCommit();
0098 
0099 protected:
0100     enum class Mode {
0101         Wayland = 0,
0102         X = 1
0103     };
0104 
0105     QQuickView *view() const
0106     {
0107         return m_view;
0108     }
0109     void setMode(Mode set)
0110     {
0111         m_mode = set;
0112     }
0113 
0114 private:
0115     QQuickView *m_view;
0116     Mode m_mode;
0117 
0118     bool m_lockHint = true;
0119     bool m_errorsAllowed = false;
0120 };
0121 
0122 class WaylandBackend : public Backend
0123 {
0124     Q_OBJECT
0125 public:
0126     WaylandBackend(QObject *parent = nullptr);
0127 
0128     void init(QQuickView *view) override;
0129 
0130     void lockRequest(bool persistent, QRect region) override;
0131     void unlockRequest() override;
0132 
0133     void confineRequest(bool persistent, QRect region) override;
0134     void unconfineRequest() override;
0135 
0136 private:
0137     void setupRegistry(KWayland::Client::Registry *registry);
0138 
0139     bool isLocked();
0140     bool isConfined();
0141 
0142     void cleanupLock();
0143     void cleanupConfine();
0144 
0145     KWayland::Client::ConnectionThread *m_connectionThreadObject;
0146     KWayland::Client::Compositor *m_compositor = nullptr;
0147     KWayland::Client::Seat *m_seat = nullptr;
0148     KWayland::Client::Pointer *m_pointer = nullptr;
0149     KWayland::Client::PointerConstraints *m_pointerConstraints = nullptr;
0150 
0151     KWayland::Client::LockedPointer *m_lockedPointer = nullptr;
0152     bool m_lockedPointerPersistent = false;
0153     KWayland::Client::ConfinedPointer *m_confinedPointer = nullptr;
0154     bool m_confinedPointerPersistent = false;
0155 };
0156 
0157 class XBackend : public Backend
0158 {
0159     Q_OBJECT
0160 public:
0161     XBackend(QObject *parent = nullptr);
0162 
0163     void init(QQuickView *view) override;
0164 
0165     void lockRequest(bool persistent, QRect region) override;
0166     void unlockRequest() override;
0167 
0168     void confineRequest(bool persistent, QRect region) override;
0169     void unconfineRequest() override;
0170 
0171     void hideAndConfineRequest(bool confineBeforeHide) override;
0172     void undoHideRequest() override;
0173 
0174 private:
0175     bool tryConfine(int &error);
0176     xcb_connection_t *m_xcbConn = nullptr;
0177 };
0178 
0179 #endif