1/*
2 SPDX-FileCopyrightText: 2019 Aleix Pol Gonzalez <aleixpol@kde.org>
3 SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "kmodifierkeyinfoprovider_wayland.h"
9#include <QDebug>
10#include <QGuiApplication>
11
12#include <QWaylandClientExtensionTemplate>
13#include <wayland-client-core.h>
14
15#include "qwayland-keystate.h"
16
17class KeyState : public QWaylandClientExtensionTemplate<KeyState>, public QtWayland::org_kde_kwin_keystate
18{
19 Q_OBJECT
20public:
21 KeyState()
22 : QWaylandClientExtensionTemplate<KeyState>(4)
23 {
24 }
25
26 ~KeyState()
27 {
28 if (isInitialized() && qGuiApp) {
29 if (QtWayland::org_kde_kwin_keystate::version() >= ORG_KDE_KWIN_KEYSTATE_DESTROY_SINCE_VERSION) {
30 destroy();
31 } else {
32 wl_proxy_destroy(proxy: reinterpret_cast<struct wl_proxy *>(object()));
33 }
34 }
35 }
36
37 void org_kde_kwin_keystate_stateChanged(uint32_t key, uint32_t state) override
38 {
39 Q_EMIT stateChanged(key: toKey(key: static_cast<KeyState::key>(key)), state: toState(state: static_cast<KeyState::state>(state)));
40 }
41
42 KModifierKeyInfoProvider::ModifierState toState(KeyState::state state)
43 {
44 switch (state) {
45 case KeyState::state::state_unlocked:
46 return KModifierKeyInfoProvider::Nothing;
47 case KeyState::state::state_locked:
48 return KModifierKeyInfoProvider::Locked;
49 case KeyState::state::state_latched:
50 return KModifierKeyInfoProvider::Latched;
51 }
52 Q_UNREACHABLE();
53 return KModifierKeyInfoProvider::Nothing;
54 }
55
56 Qt::Key toKey(KeyState::key key)
57 {
58 switch (key) {
59 case KeyState::key::key_capslock:
60 return Qt::Key_CapsLock;
61 case KeyState::key::key_numlock:
62 return Qt::Key_NumLock;
63 case KeyState::key::key_scrolllock:
64 return Qt::Key_ScrollLock;
65 }
66 Q_UNREACHABLE();
67 return {};
68 }
69
70 Q_SIGNAL void stateChanged(Qt::Key key, KModifierKeyInfoProvider::ModifierState state);
71};
72
73KModifierKeyInfoProviderWayland::KModifierKeyInfoProviderWayland()
74{
75 m_keystate = new KeyState;
76
77 QObject::connect(sender: m_keystate, signal: &KeyState::activeChanged, context: this, slot: [this]() {
78 if (m_keystate->isActive()) {
79 m_keystate->fetchStates();
80 }
81 });
82
83 connect(sender: m_keystate, signal: &KeyState::stateChanged, context: this, slot: &KModifierKeyInfoProviderWayland::stateUpdated);
84
85 stateUpdated(key: Qt::Key_CapsLock, state: KModifierKeyInfoProvider::Nothing);
86 stateUpdated(key: Qt::Key_NumLock, state: KModifierKeyInfoProvider::Nothing);
87 stateUpdated(key: Qt::Key_ScrollLock, state: KModifierKeyInfoProvider::Nothing);
88}
89
90KModifierKeyInfoProviderWayland::~KModifierKeyInfoProviderWayland()
91{
92 delete m_keystate;
93}
94
95bool KModifierKeyInfoProviderWayland::setKeyLatched(Qt::Key /*key*/, bool /*latched*/)
96{
97 return false;
98}
99
100bool KModifierKeyInfoProviderWayland::setKeyLocked(Qt::Key /*key*/, bool /*locked*/)
101{
102 return false;
103}
104
105#include "kmodifierkeyinfoprovider_wayland.moc"
106#include "moc_kmodifierkeyinfoprovider_wayland.cpp"
107

source code of kguiaddons/src/util/kmodifierkeyinfoprovider_wayland.cpp