KGuiAddons

waylandinhibition.cpp
1/*
2 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
3 SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
4*/
5
6#include "waylandinhibition_p.h"
7
8#include "kguiaddons_debug.h"
9
10#include <QDebug>
11#include <QGuiApplication>
12#include <QSharedPointer>
13#include <QWaylandClientExtensionTemplate>
14#include <QWindow>
15#include <qpa/qplatformwindow_p.h>
16
17#include "qwayland-keyboard-shortcuts-inhibit-unstable-v1.h"
18
19class ShortcutsInhibitor : public QtWayland::zwp_keyboard_shortcuts_inhibitor_v1
20{
21public:
22 ShortcutsInhibitor(::zwp_keyboard_shortcuts_inhibitor_v1 *id)
23 : QtWayland::zwp_keyboard_shortcuts_inhibitor_v1(id)
24 {
25 }
26
27 ~ShortcutsInhibitor() override
28 {
29 destroy();
30 }
31
32 void zwp_keyboard_shortcuts_inhibitor_v1_active() override
33 {
34 m_active = true;
35 }
36
37 void zwp_keyboard_shortcuts_inhibitor_v1_inactive() override
38 {
39 m_active = false;
40 }
41
42 bool isActive() const
43 {
44 return m_active;
45 }
46
47private:
48 bool m_active = false;
49};
50
51class ShortcutsInhibitManager : public QWaylandClientExtensionTemplate<ShortcutsInhibitManager>, public QtWayland::zwp_keyboard_shortcuts_inhibit_manager_v1
52{
53public:
54 ShortcutsInhibitManager()
55 : QWaylandClientExtensionTemplate<ShortcutsInhibitManager>(1)
56 {
57 initialize();
58 }
59 ~ShortcutsInhibitManager() override
60 {
61 if (isInitialized()) {
62 destroy();
63 }
64 }
65
66 void startInhibition(QWindow *window)
67 {
68 if (m_inhibitions.contains(window)) {
69 return;
70 }
71 auto waylandApp = qGuiApp->nativeInterface<QNativeInterface::QWaylandApplication>();
72 auto waylandWindow = window->nativeInterface<QNativeInterface::Private::QWaylandWindow>();
73 if (!waylandApp || !waylandWindow) {
74 return;
75 }
76
77 auto seat = waylandApp->lastInputSeat();
78 auto surface = waylandWindow->surface();
79
80 if (!seat || !surface) {
81 return;
82 }
83 m_inhibitions[window].reset(new ShortcutsInhibitor(inhibit_shortcuts(surface, seat)));
84 }
85
86 bool isInhibited(QWindow *window) const
87 {
88 return m_inhibitions.contains(window);
89 }
90
91 void stopInhibition(QWindow *window)
92 {
93 m_inhibitions.remove(window);
94 }
95
97};
98
99static std::shared_ptr<ShortcutsInhibitManager> theManager()
100{
101 static std::weak_ptr<ShortcutsInhibitManager> managerInstance;
102 std::shared_ptr<ShortcutsInhibitManager> ret = managerInstance.lock();
103 if (!ret) {
104 ret = std::make_shared<ShortcutsInhibitManager>();
105 managerInstance = ret;
106 }
107 return ret;
108}
109
110WaylandInhibition::WaylandInhibition(QWindow *window)
111 : ShortcutInhibition()
112 , m_window(window)
113 , m_manager(theManager())
114{
115}
116
117WaylandInhibition::~WaylandInhibition() = default;
118
119bool WaylandInhibition::shortcutsAreInhibited() const
120{
121 return m_manager->isInhibited(m_window);
122}
123
124void WaylandInhibition::enableInhibition()
125{
126 if (!m_manager->isActive()) {
127 qCInfo(KGUIADDONS_LOG) << "The compositor does not support the keyboard-shortcuts-inhibit-unstable-v1 protocol. Inhibiting shortcuts will not work.";
128 return;
129 }
130 m_manager->startInhibition(m_window);
131}
132
133void WaylandInhibition::disableInhibition()
134{
135 if (!m_manager->isActive()) {
136 return;
137 }
138 m_manager->stopInhibition(m_window);
139}
KCRASH_EXPORT void initialize()
QWidget * window(QObject *job)
bool contains(const Key &key) const const
bool remove(const Key &key)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.