From c7fa62afe8546087be3b72daa475a35fe41737e4 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Sat, 29 Jun 2024 18:10:21 +0200 Subject: [PATCH] libinput: add switch support --- include/aquamarine/backend/Backend.hpp | 4 ++ include/aquamarine/backend/Session.hpp | 18 +++++++++ include/aquamarine/input/Input.hpp | 43 +++++++++++++++++---- src/backend/Session.cpp | 53 +++++++++++++++++++++++++- src/input/Input.cpp | 4 ++ 5 files changed, 113 insertions(+), 9 deletions(-) diff --git a/include/aquamarine/backend/Backend.hpp b/include/aquamarine/backend/Backend.hpp index 05a6391..7891595 100644 --- a/include/aquamarine/backend/Backend.hpp +++ b/include/aquamarine/backend/Backend.hpp @@ -109,6 +109,10 @@ namespace Aquamarine { Hyprutils::Signal::CSignal newPointer; Hyprutils::Signal::CSignal newKeyboard; Hyprutils::Signal::CSignal newTouch; + Hyprutils::Signal::CSignal newSwitch; + Hyprutils::Signal::CSignal newTablet; + Hyprutils::Signal::CSignal newTabletTool; + Hyprutils::Signal::CSignal newTabletPad; } events; Hyprutils::Memory::CSharedPointer allocator; diff --git a/include/aquamarine/backend/Session.hpp b/include/aquamarine/backend/Session.hpp index 1c79b36..2236e84 100644 --- a/include/aquamarine/backend/Session.hpp +++ b/include/aquamarine/backend/Session.hpp @@ -99,6 +99,23 @@ namespace Aquamarine { Hyprutils::Memory::CWeakPointer device; }; + class CLibinputSwitch : public ISwitch { + public: + CLibinputSwitch(Hyprutils::Memory::CSharedPointer dev); + virtual ~CLibinputSwitch() { + ; + } + + virtual libinput_device* getLibinputHandle(); + virtual const std::string& getName(); + + eSwitchType type = AQ_SWITCH_TYPE_UNKNOWN; + bool state = false; + + private: + Hyprutils::Memory::CWeakPointer device; + }; + class CLibinputDevice { public: CLibinputDevice(libinput_device* device, Hyprutils::Memory::CWeakPointer session_); @@ -114,6 +131,7 @@ namespace Aquamarine { Hyprutils::Memory::CSharedPointer keyboard; Hyprutils::Memory::CSharedPointer mouse; Hyprutils::Memory::CSharedPointer touch; + Hyprutils::Memory::CSharedPointer switchy; // :) }; class CSession { diff --git a/include/aquamarine/input/Input.hpp b/include/aquamarine/input/Input.hpp index eb165ae..146717b 100644 --- a/include/aquamarine/input/Input.hpp +++ b/include/aquamarine/input/Input.hpp @@ -13,7 +13,7 @@ namespace Aquamarine { } virtual libinput_device* getLibinputHandle(); virtual const std::string& getName() = 0; - virtual void updateLEDs(uint32_t leds); + virtual void updateLEDs(uint32_t leds); struct SKeyEvent { uint32_t timeMs = 0; @@ -79,7 +79,7 @@ namespace Aquamarine { ePointerAxis axis = AQ_POINTER_AXIS_VERTICAL; ePointerAxisSource source = AQ_POINTER_AXIS_SOURCE_WHEEL; ePointerAxisRelativeDirection direction = AQ_POINTER_AXIS_RELATIVE_IDENTICAL; - double delta = 0.0, discrete = 0.0; + double delta = 0.0, discrete = 0.0; }; struct SSwipeBeginEvent { @@ -88,8 +88,8 @@ namespace Aquamarine { }; struct SSwipeUpdateEvent { - uint32_t timeMs = 0; - uint32_t fingers = 0; + uint32_t timeMs = 0; + uint32_t fingers = 0; Hyprutils::Math::Vector2D delta; }; @@ -104,10 +104,10 @@ namespace Aquamarine { }; struct SPinchUpdateEvent { - uint32_t timeMs = 0; - uint32_t fingers = 0; + uint32_t timeMs = 0; + uint32_t fingers = 0; Hyprutils::Math::Vector2D delta; - double scale = 1.0, rotation = 0.0; + double scale = 1.0, rotation = 0.0; }; struct SPinchEndEvent { @@ -155,7 +155,7 @@ namespace Aquamarine { virtual libinput_device* getLibinputHandle(); virtual const std::string& getName() = 0; - Hyprutils::Math::Vector2D physicalSize; // in mm, 0,0 if unknown + Hyprutils::Math::Vector2D physicalSize; // in mm, 0,0 if unknown struct SDownEvent { uint32_t timeMs = 0; @@ -189,6 +189,33 @@ namespace Aquamarine { } events; }; + class ISwitch { + public: + virtual ~ISwitch() { + events.destroy.emit(); + } + + virtual libinput_device* getLibinputHandle(); + virtual const std::string& getName() = 0; + + enum eSwitchType { + AQ_SWITCH_TYPE_UNKNOWN = 0, + AQ_SWITCH_TYPE_LID, + AQ_SWITCH_TYPE_TABLET_MODE, + }; + + struct SFireEvent { + uint32_t timeMs = 0; + eSwitchType type = AQ_SWITCH_TYPE_UNKNOWN; + bool enable = false; + }; + + struct { + Hyprutils::Signal::CSignal destroy; + Hyprutils::Signal::CSignal fire; + } events; + }; + class ITablet { public: // FIXME: diff --git a/src/backend/Session.cpp b/src/backend/Session.cpp index 3887c9e..7f5e867 100644 --- a/src/backend/Session.cpp +++ b/src/backend/Session.cpp @@ -267,6 +267,8 @@ void Aquamarine::CSession::onReady() { backend->events.newPointer.emit(SP(d->mouse)); if (d->touch) backend->events.newTouch.emit(SP(d->touch)); + if (d->switchy) + backend->events.newSwitch.emit(SP(d->touch)); // FIXME: other devices. } @@ -571,7 +573,7 @@ void Aquamarine::CSession::handleLibinputEvent(libinput_event* e) { break; } - // ---------- touch + // --------- touch case LIBINPUT_EVENT_TOUCH_DOWN: { auto te = libinput_event_get_touch_event(e); @@ -612,6 +614,33 @@ void Aquamarine::CSession::handleLibinputEvent(libinput_event* e) { break; } + // --------- switch + + case LIBINPUT_EVENT_SWITCH_TOGGLE: { + auto se = libinput_event_get_switch_event(e); + + const bool ENABLED = libinput_event_switch_get_switch_state(se) == LIBINPUT_SWITCH_STATE_ON; + + if (ENABLED == hlDevice->switchy->state) + return; + + switch (libinput_event_switch_get_switch(se)) { + case LIBINPUT_SWITCH_LID: hlDevice->switchy->type = ISwitch::AQ_SWITCH_TYPE_LID; break; + case LIBINPUT_SWITCH_TABLET_MODE: hlDevice->switchy->type = ISwitch::AQ_SWITCH_TYPE_TABLET_MODE; break; + } + + hlDevice->switchy->events.fire.emit(ISwitch::SFireEvent{ + .timeMs = (uint32_t)(libinput_event_switch_get_time_usec(se) / 1000), + .type = hlDevice->switchy->type, + .enable = ENABLED, + }); + break; + } + + // --------- tbalet + + + // FIXME: other events default: break; @@ -652,6 +681,12 @@ void Aquamarine::CLibinputDevice::init() { session->backend->events.newTouch.emit(SP(touch)); } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_SWITCH)) { + switchy = makeShared(self.lock()); + if (session->backend->ready) + session->backend->events.newSwitch.emit(SP(switchy)); + } + // FIXME: other devices } @@ -709,4 +744,20 @@ const std::string& Aquamarine::CLibinputTouch::getName() { if (!device) return AQ_UNKNOWN_DEVICE_NAME; return device->name; +} + +Aquamarine::CLibinputSwitch::CLibinputSwitch(Hyprutils::Memory::CSharedPointer dev) : device(dev) { + ; +} + +libinput_device* Aquamarine::CLibinputSwitch::getLibinputHandle() { + if (!device) + return nullptr; + return device->device; +} + +const std::string& Aquamarine::CLibinputSwitch::getName() { + if (!device) + return AQ_UNKNOWN_DEVICE_NAME; + return device->name; } \ No newline at end of file diff --git a/src/input/Input.cpp b/src/input/Input.cpp index 871a2a5..e1266ad 100644 --- a/src/input/Input.cpp +++ b/src/input/Input.cpp @@ -12,6 +12,10 @@ libinput_device* Aquamarine::ITouch::getLibinputHandle() { return nullptr; } +libinput_device* Aquamarine::ISwitch::getLibinputHandle() { + return nullptr; +} + void Aquamarine::IKeyboard::updateLEDs(uint32_t leds) { ; }