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 <QDebug>
9#include <QGuiApplication>
10#include <QSharedPointer>
11#include <QWaylandClientExtensionTemplate>
12#include <QWindow>
13#include <qpa/qplatformwindow_p.h>
14
15#include "qwayland-keyboard-shortcuts-inhibit-unstable-v1.h"
16
17class ShortcutsInhibitor : public QtWayland::zwp_keyboard_shortcuts_inhibitor_v1
18{
19public:
20 ShortcutsInhibitor(::zwp_keyboard_shortcuts_inhibitor_v1 *id)
21 : QtWayland::zwp_keyboard_shortcuts_inhibitor_v1(id)
22 {
23 }
24
25 ~ShortcutsInhibitor() override
26 {
27 destroy();
28 }
29
30 void zwp_keyboard_shortcuts_inhibitor_v1_active() override
31 {
32 m_active = true;
33 }
34
35 void zwp_keyboard_shortcuts_inhibitor_v1_inactive() override
36 {
37 m_active = false;
38 }
39
40 bool isActive() const
41 {
42 return m_active;
43 }
44
45private:
46 bool m_active = false;
47};
48
49class ShortcutsInhibitManager : public QWaylandClientExtensionTemplate<ShortcutsInhibitManager>, public QtWayland::zwp_keyboard_shortcuts_inhibit_manager_v1
50{
51public:
52 ShortcutsInhibitManager()
53 : QWaylandClientExtensionTemplate<ShortcutsInhibitManager>(1)
54 {
55 initialize();
56 }
57 ~ShortcutsInhibitManager() override
58 {
59 if (isInitialized()) {
60 destroy();
61 }
62 }
63
64 void startInhibition(QWindow *window)
65 {
66 if (m_inhibitions.contains(window)) {
67 return;
68 }
69 auto waylandApp = qGuiApp->nativeInterface<QNativeInterface::QWaylandApplication>();
70 auto waylandWindow = window->nativeInterface<QNativeInterface::Private::QWaylandWindow>();
71 if (!waylandApp || !waylandWindow) {
72 return;
73 }
74
75 auto seat = waylandApp->lastInputSeat();
76 auto surface = waylandWindow->surface();
77
78 if (!seat || !surface) {
79 return;
80 }
81 m_inhibitions[window].reset(new ShortcutsInhibitor(inhibit_shortcuts(surface, seat)));
82 }
83
84 bool isInhibited(QWindow *window) const
85 {
86 return m_inhibitions.contains(window);
87 }
88
89 void stopInhibition(QWindow *window)
90 {
91 m_inhibitions.remove(window);
92 }
93
95};
96
97static std::shared_ptr<ShortcutsInhibitManager> theManager()
98{
99 static std::weak_ptr<ShortcutsInhibitManager> managerInstance;
100 std::shared_ptr<ShortcutsInhibitManager> ret = managerInstance.lock();
101 if (!ret) {
102 ret = std::make_shared<ShortcutsInhibitManager>();
103 managerInstance = ret;
104 }
105 return ret;
106}
107
108WaylandInhibition::WaylandInhibition(QWindow *window)
109 : ShortcutInhibition()
110 , m_window(window)
111 , m_manager(theManager())
112{
113}
114
115WaylandInhibition::~WaylandInhibition() = default;
116
117bool WaylandInhibition::shortcutsAreInhibited() const
118{
119 return m_manager->isInhibited(m_window);
120}
121
122void WaylandInhibition::enableInhibition()
123{
124 m_manager->startInhibition(m_window);
125}
126
127void WaylandInhibition::disableInhibition()
128{
129 m_manager->stopInhibition(m_window);
130}
QWidget * window(QObject *job)
void initialize(StandardShortcut id)
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 Mon Nov 18 2024 12:09:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.