KGuiAddons

kmodifierkeyinfoprovider_wayland.cpp
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>(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(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(toKey(static_cast<KeyState::key>(key)), toState(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
85KModifierKeyInfoProviderWayland::KModifierKeyInfoProviderWayland()
86{
87 m_keystate = new KeyState;
88
89 QObject::connect(m_keystate, &KeyState::activeChanged, this, [this]() {
90 if (m_keystate->isActive()) {
91 m_keystate->fetchStates();
92 }
93 });
94
95 connect(m_keystate, &KeyState::stateChanged, this, &KModifierKeyInfoProviderWayland::stateUpdated);
96
97 stateUpdated(Qt::Key_CapsLock, KModifierKeyInfoProvider::Nothing);
98 stateUpdated(Qt::Key_NumLock, KModifierKeyInfoProvider::Nothing);
99 stateUpdated(Qt::Key_ScrollLock, KModifierKeyInfoProvider::Nothing);
100 stateUpdated(Qt::Key_Alt, KModifierKeyInfoProvider::Nothing);
101 stateUpdated(Qt::Key_Shift, KModifierKeyInfoProvider::Nothing);
102 stateUpdated(Qt::Key_Control, KModifierKeyInfoProvider::Nothing);
103 stateUpdated(Qt::Key_Meta, KModifierKeyInfoProvider::Nothing);
104 stateUpdated(Qt::Key_AltGr, KModifierKeyInfoProvider::Nothing);
105}
106
107KModifierKeyInfoProviderWayland::~KModifierKeyInfoProviderWayland()
108{
109 delete m_keystate;
110}
111
112bool KModifierKeyInfoProviderWayland::setKeyLatched(Qt::Key /*key*/, bool /*latched*/)
113{
114 return false;
115}
116
117bool KModifierKeyInfoProviderWayland::setKeyLocked(Qt::Key /*key*/, bool /*locked*/)
118{
119 return false;
120}
121
122#include "kmodifierkeyinfoprovider_wayland.moc"
123#include "moc_kmodifierkeyinfoprovider_wayland.cpp"
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
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.