|
| 1 | +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +#include "treelandkeynotify.h" |
| 6 | + |
| 7 | +#include "wayland-treeland-keyboard-state-notify-unstable-v1-client-protocol.h" |
| 8 | + |
| 9 | +DS_BEGIN_NAMESPACE |
| 10 | +namespace keynotify |
| 11 | +{ |
| 12 | + |
| 13 | +TreelandKeyNotify::TreelandKeyNotify(QObject *parent) |
| 14 | + : QWaylandClientExtensionTemplate<TreelandKeyNotify>(treeland_keyboard_state_notify_manager_v1_interface.version) |
| 15 | +{ |
| 16 | + setParent(parent); |
| 17 | + connect(this, &TreelandKeyNotify::activeChanged, this, &TreelandKeyNotify::updateWatcher); |
| 18 | +} |
| 19 | + |
| 20 | +void TreelandKeyNotify::setCapsLockEnabled(bool enabled) |
| 21 | +{ |
| 22 | + if (m_capsLockEnabled == enabled) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + m_capsLockEnabled = enabled; |
| 27 | + updateWatcher(); |
| 28 | +} |
| 29 | + |
| 30 | +void TreelandKeyNotify::updateWatcher() |
| 31 | +{ |
| 32 | + if (!isActive()) { |
| 33 | + if (m_watcher) { |
| 34 | + m_watcher->deleteLater(); |
| 35 | + m_watcher = nullptr; |
| 36 | + } |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (!m_watcher) { |
| 41 | + m_watcher = createWatcher(this); |
| 42 | + if (!m_watcher) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + connect(m_watcher, &TreelandKeyWatcher::stateChanged, this, [this](uint32_t modifier, uint32_t state) { |
| 47 | + if (modifier == TreelandKeyWatcher::modifier_caps_lock) { |
| 48 | + if (state == TreelandKeyWatcher::modifier_state_locked) { |
| 49 | + Q_EMIT capsLockChanged(true); |
| 50 | + } else if (state == TreelandKeyWatcher::modifier_state_unlocked) { |
| 51 | + Q_EMIT capsLockChanged(false); |
| 52 | + } |
| 53 | + } else if (modifier == TreelandKeyWatcher::modifier_num_lock) { |
| 54 | + if (state == TreelandKeyWatcher::modifier_state_locked) { |
| 55 | + Q_EMIT numLockChanged(true); |
| 56 | + } else if (state == TreelandKeyWatcher::modifier_state_unlocked) { |
| 57 | + Q_EMIT numLockChanged(false); |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + m_watcher->watchLocks(m_capsLockEnabled); |
| 64 | +} |
| 65 | + |
| 66 | +TreelandKeyWatcher *TreelandKeyNotify::createWatcher(QObject *parent) |
| 67 | +{ |
| 68 | + if (!isActive()) { |
| 69 | + return nullptr; |
| 70 | + } |
| 71 | + |
| 72 | + auto *watcher = get_keyboard_state_watcher(nullptr); |
| 73 | + if (!watcher) { |
| 74 | + return nullptr; |
| 75 | + } |
| 76 | + |
| 77 | + return new TreelandKeyWatcher(watcher, parent); |
| 78 | +} |
| 79 | + |
| 80 | +TreelandKeyWatcher::TreelandKeyWatcher(struct ::treeland_keyboard_state_watcher_v1 *object, QObject *parent) |
| 81 | + : QObject(parent) |
| 82 | + , QtWayland::treeland_keyboard_state_watcher_v1(object) |
| 83 | +{ |
| 84 | +} |
| 85 | + |
| 86 | +void TreelandKeyWatcher::watchLocks(bool watchCapsLock) |
| 87 | +{ |
| 88 | + set_modifiers(watchCapsLock ? modifier_caps_lock | modifier_num_lock : modifier_num_lock); |
| 89 | + set_flags(watch_flag_locked | watch_flag_unlocked); |
| 90 | + apply(); |
| 91 | +} |
| 92 | + |
| 93 | +void TreelandKeyWatcher::treeland_keyboard_state_watcher_v1_state_changed(uint32_t modifier, uint32_t state) |
| 94 | +{ |
| 95 | + Q_EMIT stateChanged(modifier, state); |
| 96 | +} |
| 97 | + |
| 98 | +} |
| 99 | +DS_END_NAMESPACE |
0 commit comments