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 | |
17 | class KeyState : public QWaylandClientExtensionTemplate<KeyState>, public QtWayland::org_kde_kwin_keystate |
18 | { |
19 | Q_OBJECT |
20 | public: |
21 | KeyState() |
22 | : QWaylandClientExtensionTemplate<KeyState>(5) |
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 | case KeyState::state::state_pressed: |
52 | return KModifierKeyInfoProvider::Pressed; |
53 | } |
54 | Q_UNREACHABLE(); |
55 | return KModifierKeyInfoProvider::Nothing; |
56 | } |
57 | |
58 | Qt::Key toKey(KeyState::key key) |
59 | { |
60 | switch (key) { |
61 | case KeyState::key::key_capslock: |
62 | return Qt::Key_CapsLock; |
63 | case KeyState::key::key_numlock: |
64 | return Qt::Key_NumLock; |
65 | case KeyState::key::key_scrolllock: |
66 | return Qt::Key_ScrollLock; |
67 | case KeyState::key_alt: |
68 | return Qt::Key_Alt; |
69 | case KeyState::key_shift: |
70 | return Qt::Key_Shift; |
71 | case KeyState::key_control: |
72 | return Qt::Key_Control; |
73 | case KeyState::key_meta: |
74 | return Qt::Key_Meta; |
75 | case KeyState::key_altgr: |
76 | return Qt::Key_AltGr; |
77 | } |
78 | Q_UNREACHABLE(); |
79 | return {}; |
80 | } |
81 | |
82 | Q_SIGNAL void stateChanged(Qt::Key key, KModifierKeyInfoProvider::ModifierState state); |
83 | }; |
84 | |
85 | KModifierKeyInfoProviderWayland::KModifierKeyInfoProviderWayland() |
86 | { |
87 | m_keystate = new KeyState; |
88 | |
89 | QObject::connect(sender: m_keystate, signal: &KeyState::activeChanged, context: this, slot: [this]() { |
90 | if (m_keystate->isActive()) { |
91 | m_keystate->fetchStates(); |
92 | } |
93 | }); |
94 | |
95 | connect(sender: m_keystate, signal: &KeyState::stateChanged, context: this, slot: &KModifierKeyInfoProviderWayland::stateUpdated); |
96 | |
97 | stateUpdated(key: Qt::Key_CapsLock, state: KModifierKeyInfoProvider::Nothing); |
98 | stateUpdated(key: Qt::Key_NumLock, state: KModifierKeyInfoProvider::Nothing); |
99 | stateUpdated(key: Qt::Key_ScrollLock, state: KModifierKeyInfoProvider::Nothing); |
100 | stateUpdated(key: Qt::Key_Alt, state: KModifierKeyInfoProvider::Nothing); |
101 | stateUpdated(key: Qt::Key_Shift, state: KModifierKeyInfoProvider::Nothing); |
102 | stateUpdated(key: Qt::Key_Control, state: KModifierKeyInfoProvider::Nothing); |
103 | stateUpdated(key: Qt::Key_Meta, state: KModifierKeyInfoProvider::Nothing); |
104 | stateUpdated(key: Qt::Key_AltGr, state: KModifierKeyInfoProvider::Nothing); |
105 | } |
106 | |
107 | KModifierKeyInfoProviderWayland::~KModifierKeyInfoProviderWayland() |
108 | { |
109 | delete m_keystate; |
110 | } |
111 | |
112 | bool KModifierKeyInfoProviderWayland::setKeyLatched(Qt::Key /*key*/, bool /*latched*/) |
113 | { |
114 | return false; |
115 | } |
116 | |
117 | bool KModifierKeyInfoProviderWayland::setKeyLocked(Qt::Key /*key*/, bool /*locked*/) |
118 | { |
119 | return false; |
120 | } |
121 | |
122 | #include "kmodifierkeyinfoprovider_wayland.moc" |
123 | #include "moc_kmodifierkeyinfoprovider_wayland.cpp" |
124 | |