|
| 1 | +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +#include "keyboardstatenotify.h" |
| 6 | + |
| 7 | +#include <QGuiApplication> |
| 8 | +#include <QLoggingCategory> |
| 9 | +#include <wayland-client.h> |
| 10 | + |
| 11 | +namespace DCC_NAMESPACE { |
| 12 | + |
| 13 | +Q_LOGGING_CATEGORY(lcKbdStateNotify, "dde.dcc.keyboard.statenotify") |
| 14 | + |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | +// Watcher |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | + |
| 19 | +KeyboardStateNotify::Watcher::Watcher(KeyboardStateNotify *parent, |
| 20 | + struct ::treeland_keyboard_state_watcher_v1 *obj) |
| 21 | + : QObject(parent) |
| 22 | + , QtWayland::treeland_keyboard_state_watcher_v1(obj) |
| 23 | +{ |
| 24 | + qCDebug(lcKbdStateNotify) << "Watcher created, configuring to watch num_lock with all flags"; |
| 25 | + |
| 26 | + // Watch num_lock modifier |
| 27 | + set_modifiers(modifier_num_lock); |
| 28 | + |
| 29 | + // Receive both locked and unlocked events |
| 30 | + set_flags(watch_flag_locked | watch_flag_unlocked); |
| 31 | + |
| 32 | + apply(); |
| 33 | +} |
| 34 | + |
| 35 | +KeyboardStateNotify::Watcher::~Watcher() |
| 36 | +{ |
| 37 | + if (isInitialized()) |
| 38 | + destroy(); |
| 39 | +} |
| 40 | + |
| 41 | +void KeyboardStateNotify::Watcher::treeland_keyboard_state_watcher_v1_current_state( |
| 42 | + uint32_t modifier, uint32_t state) |
| 43 | +{ |
| 44 | + qCDebug(lcKbdStateNotify) << "current_state: modifier=" << modifier << "state=" << state; |
| 45 | + |
| 46 | + if (modifier == modifier_num_lock) { |
| 47 | + bool on = (state == modifier_state_locked); |
| 48 | + Q_EMIT numLockChanged(on); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +void KeyboardStateNotify::Watcher::treeland_keyboard_state_watcher_v1_state_changed( |
| 53 | + uint32_t modifier, uint32_t state) |
| 54 | +{ |
| 55 | + qCDebug(lcKbdStateNotify) << "state_changed: modifier=" << modifier << "state=" << state; |
| 56 | + |
| 57 | + if (modifier == modifier_num_lock) { |
| 58 | + bool on = (state == modifier_state_locked); |
| 59 | + Q_EMIT numLockChanged(on); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// --------------------------------------------------------------------------- |
| 64 | +// KeyboardStateNotify |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | + |
| 67 | +KeyboardStateNotify::KeyboardStateNotify(QObject *parent) |
| 68 | + : QWaylandClientExtensionTemplate<KeyboardStateNotify>(1) |
| 69 | + , QtWayland::treeland_keyboard_state_notify_manager_v1() |
| 70 | +{ |
| 71 | + setParent(parent); |
| 72 | + |
| 73 | + qCDebug(lcKbdStateNotify) << "KeyboardStateNotify initializing"; |
| 74 | + |
| 75 | + connect(this, &QWaylandClientExtension::activeChanged, this, [this]() { |
| 76 | + const bool avail = isActive(); |
| 77 | + qCDebug(lcKbdStateNotify) << "active changed:" << avail; |
| 78 | + |
| 79 | + if (m_available != avail) { |
| 80 | + m_available = avail; |
| 81 | + Q_EMIT availableChanged(avail); |
| 82 | + } |
| 83 | + |
| 84 | + if (avail) { |
| 85 | + auto *waylandApp = qGuiApp->nativeInterface<QNativeInterface::QWaylandApplication>(); |
| 86 | + if (waylandApp) { |
| 87 | + createWatcher(nullptr); |
| 88 | + } |
| 89 | + } else { |
| 90 | + delete m_watcher; |
| 91 | + m_watcher = nullptr; |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +KeyboardStateNotify::~KeyboardStateNotify() |
| 99 | +{ |
| 100 | + delete m_watcher; |
| 101 | + m_watcher = nullptr; |
| 102 | +} |
| 103 | + |
| 104 | +bool KeyboardStateNotify::available() const |
| 105 | +{ |
| 106 | + return m_available; |
| 107 | +} |
| 108 | + |
| 109 | +void KeyboardStateNotify::createWatcher(wl_seat *seat) |
| 110 | +{ |
| 111 | + if (m_watcher) { |
| 112 | + delete m_watcher; |
| 113 | + m_watcher = nullptr; |
| 114 | + } |
| 115 | + |
| 116 | + if (!isActive() || !QtWayland::treeland_keyboard_state_notify_manager_v1::isInitialized()) |
| 117 | + return; |
| 118 | + |
| 119 | + // Pass null seat to monitor all seats |
| 120 | + auto *watcherObj = get_keyboard_state_watcher(seat); |
| 121 | + m_watcher = new Watcher(this, watcherObj); |
| 122 | + |
| 123 | + connect(m_watcher, &Watcher::numLockChanged, this, &KeyboardStateNotify::numLockChanged); |
| 124 | + |
| 125 | + // Flush the Wayland display to ensure the request is sent |
| 126 | + auto *waylandApp = qGuiApp->nativeInterface<QNativeInterface::QWaylandApplication>(); |
| 127 | + if (waylandApp && waylandApp->display()) |
| 128 | + wl_display_flush(waylandApp->display()); |
| 129 | + |
| 130 | + qCDebug(lcKbdStateNotify) << "Watcher created and configured"; |
| 131 | +} |
| 132 | + |
| 133 | +} // namespace DCC_NAMESPACE |
0 commit comments