File indexing completed on 2024-04-21 14:56:38

0001 /*
0002     SPDX-FileCopyrightText: 2019 Aleix Pol Gonzalez <aleixpol@kde.org>
0003     SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "kmodifierkeyinfoprovider_wayland.h"
0009 #include <QDebug>
0010 
0011 #include <QtWaylandClient/qwaylandclientextension.h>
0012 #include <wayland-client-core.h>
0013 
0014 #include "qwayland-keystate.h"
0015 
0016 class KeyState : public QWaylandClientExtensionTemplate<KeyState>, public QtWayland::org_kde_kwin_keystate
0017 {
0018     Q_OBJECT
0019 public:
0020     KeyState()
0021         : QWaylandClientExtensionTemplate<KeyState>(3)
0022     {
0023     }
0024 
0025     ~KeyState()
0026     {
0027         if (isInitialized()) {
0028             wl_proxy_destroy(reinterpret_cast<struct wl_proxy *>(object()));
0029         }
0030     }
0031 
0032     void org_kde_kwin_keystate_stateChanged(uint32_t key, uint32_t state) override
0033     {
0034         Q_EMIT stateChanged(toKey(static_cast<KeyState::key>(key)), toState(static_cast<KeyState::state>(state)));
0035     }
0036 
0037     KModifierKeyInfoProvider::ModifierState toState(KeyState::state state)
0038     {
0039         switch (state) {
0040         case KeyState::state::state_unlocked:
0041             return KModifierKeyInfoProvider::Nothing;
0042         case KeyState::state::state_locked:
0043             return KModifierKeyInfoProvider::Locked;
0044         case KeyState::state::state_latched:
0045             return KModifierKeyInfoProvider::Latched;
0046         }
0047         Q_UNREACHABLE();
0048         return KModifierKeyInfoProvider::Nothing;
0049     }
0050 
0051     Qt::Key toKey(KeyState::key key)
0052     {
0053         switch (key) {
0054         case KeyState::key::key_capslock:
0055             return Qt::Key_CapsLock;
0056         case KeyState::key::key_numlock:
0057             return Qt::Key_NumLock;
0058         case KeyState::key::key_scrolllock:
0059             return Qt::Key_ScrollLock;
0060         }
0061         Q_UNREACHABLE();
0062         return {};
0063     }
0064 
0065     Q_SIGNAL void stateChanged(Qt::Key key, KModifierKeyInfoProvider::ModifierState state);
0066 };
0067 
0068 KModifierKeyInfoProviderWayland::KModifierKeyInfoProviderWayland()
0069 {
0070     m_keystate = new KeyState;
0071 
0072     QObject::connect(m_keystate, &KeyState::activeChanged, this, [this]() {
0073         if (m_keystate->isActive()) {
0074             m_keystate->fetchStates();
0075         }
0076     });
0077 
0078     connect(m_keystate, &KeyState::stateChanged, this, &KModifierKeyInfoProviderWayland::stateUpdated);
0079 
0080     stateUpdated(Qt::Key_CapsLock, KModifierKeyInfoProvider::Nothing);
0081     stateUpdated(Qt::Key_NumLock, KModifierKeyInfoProvider::Nothing);
0082     stateUpdated(Qt::Key_ScrollLock, KModifierKeyInfoProvider::Nothing);
0083 }
0084 
0085 KModifierKeyInfoProviderWayland::~KModifierKeyInfoProviderWayland()
0086 {
0087     delete m_keystate;
0088 }
0089 
0090 bool KModifierKeyInfoProviderWayland::setKeyLatched(Qt::Key /*key*/, bool /*latched*/)
0091 {
0092     return false;
0093 }
0094 
0095 bool KModifierKeyInfoProviderWayland::setKeyLocked(Qt::Key /*key*/, bool /*locked*/)
0096 {
0097     return false;
0098 }
0099 
0100 #include "kmodifierkeyinfoprovider_wayland.moc"
0101 #include "moc_kmodifierkeyinfoprovider_wayland.cpp"