From cb3a3a4998046086308bf34fe1ee8536f4d6f6fb Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sat, 29 Jun 2024 18:18:19 +0200 Subject: [PATCH] switches --- src/Compositor.cpp | 8 +++++ src/debug/HyprCtl.cpp | 4 +-- src/events/Devices.cpp | 2 +- src/helpers/WLClasses.hpp | 14 ++++---- src/managers/input/InputManager.cpp | 50 +++++++++++------------------ src/managers/input/InputManager.hpp | 3 +- 6 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index a938e4b0..d95fcd8d 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -308,6 +308,14 @@ void CCompositor::initAllSignals() { g_pInputManager->updateCapabilities(); }, nullptr); + + m_pAqBackend->events.newSwitch.registerStaticListener( + [](void* data, std::any d) { + auto dev = std::any_cast>(d); + Debug::log(LOG, "New aquamarine switch with name {}", dev->getName()); + g_pInputManager->newSwitch(dev); + }, + nullptr); } void CCompositor::removeAllSignals() { diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 703ff79b..c1981740 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -654,7 +654,7 @@ std::string devicesRequest(eHyprCtlOutputFormat format, std::string request) { "address": "0x{:x}", "name": "{}" }},)#", - (uintptr_t)&d, escapeJSONStrings(d.pWlrDevice ? d.pWlrDevice->name : "")); + (uintptr_t)&d, escapeJSONStrings(d.pDevice ? d.pDevice->getName() : "")); } trimTrailingComma(result); @@ -702,7 +702,7 @@ std::string devicesRequest(eHyprCtlOutputFormat format, std::string request) { result += "\n\nSwitches:\n"; for (auto& d : g_pInputManager->m_lSwitches) { - result += std::format("\tSwitch Device at {:x}:\n\t\t{}\n", (uintptr_t)&d, d.pWlrDevice ? d.pWlrDevice->name : ""); + result += std::format("\tSwitch Device at {:x}:\n\t\t{}\n", (uintptr_t)&d, d.pDevice ? d.pDevice->getName() : ""); } } diff --git a/src/events/Devices.cpp b/src/events/Devices.cpp index 752e51f7..a7c5dd55 100644 --- a/src/events/Devices.cpp +++ b/src/events/Devices.cpp @@ -41,7 +41,7 @@ void Events::listener_newInput(wl_listener* listener, void* data) { break; case WLR_INPUT_DEVICE_SWITCH: Debug::log(LOG, "Attached a switch device with name {}", DEVICE->name); - g_pInputManager->newSwitch(DEVICE); + // g_pInputManager->newSwitch(DEVICE); break; default: Debug::log(WARN, "Unrecognized input device plugged in: {}", DEVICE->name); break; } diff --git a/src/helpers/WLClasses.hpp b/src/helpers/WLClasses.hpp index 6b25e76d..51f90166 100644 --- a/src/helpers/WLClasses.hpp +++ b/src/helpers/WLClasses.hpp @@ -15,6 +15,8 @@ class IPointer; class IKeyboard; class CWLSurfaceResource; +AQUAMARINE_FORWARD(ISwitch); + struct SRenderData { CMonitor* pMonitor; timespec* when; @@ -70,14 +72,14 @@ struct SSwipeGesture { }; struct SSwitchDevice { - wlr_input_device* pWlrDevice = nullptr; + WP pDevice; - int status = -1; // uninitialized - - DYNLISTENER(destroy); - DYNLISTENER(toggle); + struct { + CHyprSignalListener destroy; + CHyprSignalListener fire; + } listeners; bool operator==(const SSwitchDevice& other) const { - return pWlrDevice == other.pWlrDevice; + return pDevice == other.pDevice; } }; diff --git a/src/managers/input/InputManager.cpp b/src/managers/input/InputManager.cpp index 9e2a16c7..6865ebbc 100644 --- a/src/managers/input/InputManager.cpp +++ b/src/managers/input/InputManager.cpp @@ -1568,44 +1568,30 @@ void CInputManager::setTabletConfigs() { } } -void CInputManager::newSwitch(wlr_input_device* pDevice) { - const auto PNEWDEV = &m_lSwitches.emplace_back(); - PNEWDEV->pWlrDevice = pDevice; +void CInputManager::newSwitch(SP pDevice) { + const auto PNEWDEV = &m_lSwitches.emplace_back(); + PNEWDEV->pDevice = pDevice; - Debug::log(LOG, "New switch with name \"{}\" added", pDevice->name); + Debug::log(LOG, "New switch with name \"{}\" added", pDevice->getName()); - PNEWDEV->hyprListener_destroy.initCallback(&pDevice->events.destroy, [&](void* owner, void* data) { destroySwitch((SSwitchDevice*)owner); }, PNEWDEV, "SwitchDevice"); + PNEWDEV->listeners.destroy = pDevice->events.destroy.registerListener([this, PNEWDEV](std::any d) { destroySwitch(PNEWDEV); }); - const auto PSWITCH = wlr_switch_from_input_device(pDevice); + PNEWDEV->listeners.fire = pDevice->events.fire.registerListener([PNEWDEV](std::any d) { + const auto NAME = PNEWDEV->pDevice->getName(); + const auto E = std::any_cast(d); - PNEWDEV->hyprListener_toggle.initCallback( - &PSWITCH->events.toggle, - [&](void* owner, void* data) { - const auto PDEVICE = (SSwitchDevice*)owner; - const auto NAME = std::string(PDEVICE->pWlrDevice->name); - const auto E = (wlr_switch_toggle_event*)data; + Debug::log(LOG, "Switch {} fired, triggering binds.", NAME); - if (PDEVICE->status != -1 && PDEVICE->status == E->switch_state) - return; + g_pKeybindManager->onSwitchEvent(NAME); - Debug::log(LOG, "Switch {} fired, triggering binds.", NAME); - - g_pKeybindManager->onSwitchEvent(NAME); - - switch (E->switch_state) { - case WLR_SWITCH_STATE_ON: - Debug::log(LOG, "Switch {} turn on, triggering binds.", NAME); - g_pKeybindManager->onSwitchOnEvent(NAME); - break; - case WLR_SWITCH_STATE_OFF: - Debug::log(LOG, "Switch {} turn off, triggering binds.", NAME); - g_pKeybindManager->onSwitchOffEvent(NAME); - break; - } - - PDEVICE->status = E->switch_state; - }, - PNEWDEV, "SwitchDevice"); + if (E.enable) { + Debug::log(LOG, "Switch {} turn on, triggering binds.", NAME); + g_pKeybindManager->onSwitchOnEvent(NAME); + } else { + Debug::log(LOG, "Switch {} turn off, triggering binds.", NAME); + g_pKeybindManager->onSwitchOffEvent(NAME); + } + }); } void CInputManager::destroySwitch(SSwitchDevice* pDevice) { diff --git a/src/managers/input/InputManager.hpp b/src/managers/input/InputManager.hpp index 692b5c27..b24bffd7 100644 --- a/src/managers/input/InputManager.hpp +++ b/src/managers/input/InputManager.hpp @@ -21,6 +21,7 @@ class IKeyboard; AQUAMARINE_FORWARD(IPointer); AQUAMARINE_FORWARD(IKeyboard); AQUAMARINE_FORWARD(ITouch); +AQUAMARINE_FORWARD(ISwitch); enum eClickBehaviorMode { CLICKMODE_DEFAULT = 0, @@ -91,7 +92,7 @@ class CInputManager { void newMouse(SP); void newVirtualMouse(SP); void newTouchDevice(SP); - void newSwitch(wlr_input_device*); + void newSwitch(SP); void newTabletTool(wlr_tablet_tool*); void newTabletPad(wlr_input_device*); void newTablet(wlr_input_device*);