diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-09-24 23:48:47 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-24 23:48:47 +1000 |
| commit | 57e3989185b8209a11d79bac477479019d515180 (patch) | |
| tree | 27e94dcaef9f41bb8f294eab1a91a27ce4da03d0 /plugin/src/Caelestia/Internal/hyprdevices.cpp | |
| parent | plugin: sub namespace modules (diff) | |
| download | caelestia-shell-57e3989185b8209a11d79bac477479019d515180.tar.gz caelestia-shell-57e3989185b8209a11d79bac477479019d515180.tar.bz2 caelestia-shell-57e3989185b8209a11d79bac477479019d515180.zip | |
plugin: add hypr extras (#690)
* move machine
* works
* prevent duplicate refreshes
* use instead of hyprctl proc
Diffstat (limited to 'plugin/src/Caelestia/Internal/hyprdevices.cpp')
| -rw-r--r-- | plugin/src/Caelestia/Internal/hyprdevices.cpp | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/plugin/src/Caelestia/Internal/hyprdevices.cpp b/plugin/src/Caelestia/Internal/hyprdevices.cpp new file mode 100644 index 0000000..8f63d67 --- /dev/null +++ b/plugin/src/Caelestia/Internal/hyprdevices.cpp @@ -0,0 +1,129 @@ +#include "hyprdevices.hpp" + +#include <qjsonarray.h> + +namespace caelestia::internal::hypr { + +HyprKeyboard::HyprKeyboard(QJsonObject ipcObject, QObject* parent) + : QObject(parent) + , m_lastIpcObject(ipcObject) {} + +QVariantHash HyprKeyboard::lastIpcObject() const { + return m_lastIpcObject.toVariantHash(); +} + +QString HyprKeyboard::address() const { + return m_lastIpcObject.value("address").toString(); +} + +QString HyprKeyboard::name() const { + return m_lastIpcObject.value("name").toString(); +} + +QString HyprKeyboard::layout() const { + return m_lastIpcObject.value("layout").toString(); +} + +QString HyprKeyboard::activeKeymap() const { + return m_lastIpcObject.value("active_keymap").toString(); +} + +bool HyprKeyboard::capsLock() const { + return m_lastIpcObject.value("capsLock").toBool(); +} + +bool HyprKeyboard::numLock() const { + return m_lastIpcObject.value("numLock").toBool(); +} + +bool HyprKeyboard::main() const { + return m_lastIpcObject.value("main").toBool(); +} + +bool HyprKeyboard::updateLastIpcObject(const QJsonObject& object) { + if (m_lastIpcObject == object) { + return false; + } + + const auto last = m_lastIpcObject; + + m_lastIpcObject = object; + emit lastIpcObjectChanged(); + + bool dirty = false; + if (last.value("address") != object.value("address")) { + dirty = true; + emit addressChanged(); + } + if (last.value("name") != object.value("name")) { + dirty = true; + emit nameChanged(); + } + if (last.value("layout") != object.value("layout")) { + dirty = true; + emit layoutChanged(); + } + if (last.value("active_keymap") != object.value("active_keymap")) { + dirty = true; + emit activeKeymapChanged(); + } + if (last.value("capsLock") != object.value("capsLock")) { + dirty = true; + emit capsLockChanged(); + } + if (last.value("numLock") != object.value("numLock")) { + dirty = true; + emit numLockChanged(); + } + if (last.value("main") != object.value("main")) { + dirty = true; + emit mainChanged(); + } + return dirty; +} + +HyprDevices::HyprDevices(QObject* parent) + : QObject(parent) {} + +QList<HyprKeyboard*> HyprDevices::keyboards() const { + return m_keyboards; +} + +bool HyprDevices::updateLastIpcObject(const QJsonObject& object) { + const auto val = object.value("keyboards").toArray(); + bool dirty = false; + + for (const auto& keyboard : std::as_const(m_keyboards)) { + if (std::find_if(val.begin(), val.end(), [keyboard](const QJsonValue& object) { + return object.toObject().value("address").toString() == keyboard->address(); + }) == val.end()) { + dirty = true; + m_keyboards.removeAll(keyboard); + keyboard->deleteLater(); + } + } + + for (const auto& o : val) { + const auto obj = o.toObject(); + const auto addr = obj.value("address").toString(); + + auto it = std::find_if(m_keyboards.begin(), m_keyboards.end(), [addr](const HyprKeyboard* keyboard) { + return keyboard->address() == addr; + }); + + if (it != m_keyboards.end()) { + dirty |= (*it)->updateLastIpcObject(obj); + } else { + dirty = true; + m_keyboards << new HyprKeyboard(obj, this); + } + } + + if (dirty) { + emit keyboardsChanged(); + } + + return dirty; +} + +} // namespace caelestia::internal::hypr |