Add HiresScroll IPC support

This commit is contained in:
pixl 2023-04-29 14:59:00 -04:00
parent 485788a74e
commit 0b515feb17
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
4 changed files with 234 additions and 67 deletions

View File

@ -19,14 +19,19 @@
#include <actions/gesture/AxisGesture.h> #include <actions/gesture/AxisGesture.h>
#include <Device.h> #include <Device.h>
#include <InputDevice.h> #include <InputDevice.h>
#include "ipc_defs.h"
using namespace logid;
using namespace logid::features; using namespace logid::features;
using namespace logid::backend; using namespace logid::backend;
HiresScroll::HiresScroll(Device* dev) : DeviceFeature(dev), HiresScroll::HiresScroll(Device* dev) :
_config(dev->activeProfile().hiresscroll), _mode(0), DeviceFeature(dev),
_mask(0), _config(dev->activeProfile().hiresscroll), _mode(0),
_node(dev->ipcNode()->make_child("hires")) { _mask(0),
_node(dev->ipcNode()->make_child("hires_scroll")),
_up_node(_node->make_child("up")),
_down_node(_node->make_child("down")) {
if (_config.has_value()) { if (_config.has_value()) {
if (std::holds_alternative<bool>(_config.value())) { if (std::holds_alternative<bool>(_config.value())) {
config::HiresScroll conf{}; config::HiresScroll conf{};
@ -53,8 +58,8 @@ HiresScroll::HiresScroll(Device* dev) : DeviceFeature(dev),
_mode |= hidpp20::HiresScroll::Mode::Target; _mode |= hidpp20::HiresScroll::Mode::Target;
} }
_makeAction(_up_action, conf.up, "up"); _makeGesture(_up_gesture, conf.up, "up");
_makeAction(_down_action, conf.down, "down"); _makeGesture(_down_gesture, conf.down, "down");
} }
try { try {
@ -64,6 +69,8 @@ HiresScroll::HiresScroll(Device* dev) : DeviceFeature(dev),
} }
_last_scroll = std::chrono::system_clock::now(); _last_scroll = std::chrono::system_clock::now();
_ipc_interface = _node->make_interface<IPC>(this);
} }
HiresScroll::~HiresScroll() noexcept { HiresScroll::~HiresScroll() noexcept {
@ -72,6 +79,11 @@ HiresScroll::~HiresScroll() noexcept {
} }
void HiresScroll::configure() { void HiresScroll::configure() {
std::shared_lock lock(_config_mutex);
_configure();
}
void HiresScroll::_configure() {
auto mode = _hires_scroll->getMode(); auto mode = _hires_scroll->getMode();
mode &= ~_mask; mode &= ~_mask;
mode |= (_mode & _mask); mode |= (_mode & _mask);
@ -79,21 +91,19 @@ void HiresScroll::configure() {
} }
void HiresScroll::listen() { void HiresScroll::listen() {
std::shared_lock lock(_config_mutex);
if (!_ev_handler.has_value()) { if (!_ev_handler.has_value()) {
_ev_handler = _device->hidpp20().addEventHandler({ _ev_handler = _device->hidpp20().addEventHandler(
[index = _hires_scroll->featureIndex()]( {
const hidpp::Report& report) -> bool { [index = _hires_scroll->featureIndex()](
return (report.feature() == const hidpp::Report& report) -> bool {
index) && return (report.feature() == index) &&
(report.function() == (report.function() == hidpp20::HiresScroll::WheelMovement);
hidpp20::HiresScroll::WheelMovement); },
}, [this](const hidpp::Report& report) {
[this](const hidpp::Report& report) { _handleScroll(_hires_scroll->wheelMovementEvent(report));
_handleScroll( }
_hires_scroll->wheelMovementEvent( });
report));
}
});
} }
} }
@ -105,35 +115,40 @@ void HiresScroll::setMode(uint8_t mode) {
_hires_scroll->setMode(mode); _hires_scroll->setMode(mode);
} }
void HiresScroll::_makeAction(std::shared_ptr<actions::Gesture>& gesture, void HiresScroll::_makeGesture(std::shared_ptr<actions::Gesture>& gesture,
std::optional<config::Gesture>& config, std::optional<config::Gesture>& config,
const std::string& direction) { const std::string& direction) {
if (config.has_value()) { if (config.has_value()) {
gesture = actions::Gesture::makeGesture(_device, config.value(), gesture = actions::Gesture::makeGesture(_device, config.value(),
_node->make_child(direction)); _node->make_child(direction));
try {
auto axis = std::dynamic_pointer_cast<actions::AxisGesture>(gesture); _fixGesture(gesture);
if (axis)
axis->setHiresMultiplier(_hires_scroll->getCapabilities().multiplier);
} catch (std::bad_cast& e) {}
if (gesture)
gesture->press(true);
} else { } else {
gesture.reset(); gesture.reset();
} }
} }
void HiresScroll::_fixGesture(const std::shared_ptr<actions::Gesture>& gesture) {
try {
auto axis = std::dynamic_pointer_cast<actions::AxisGesture>(gesture);
if (axis)
axis->setHiresMultiplier(_hires_scroll->getCapabilities().multiplier);
} catch (std::bad_cast& e) {}
if (gesture)
gesture->press(true);
}
void HiresScroll::_handleScroll(hidpp20::HiresScroll::WheelStatus event) { void HiresScroll::_handleScroll(hidpp20::HiresScroll::WheelStatus event) {
std::shared_lock lock(_config_mutex);
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
if (std::chrono::duration_cast<std::chrono::seconds>( if (std::chrono::duration_cast<std::chrono::seconds>(now - _last_scroll).count() >= 1) {
now - _last_scroll).count() >= 1) { if (_up_gesture) {
if (_up_action) { _up_gesture->release(false);
_up_action->release(false); _up_gesture->press(true);
_up_action->press(true);
} }
if (_down_action) { if (_down_gesture) {
_down_action->release(false); _down_gesture->release(false);
_down_action->press(true); _down_gesture->press(true);
} }
_last_direction = 0; _last_direction = 0;
@ -141,25 +156,147 @@ void HiresScroll::_handleScroll(hidpp20::HiresScroll::WheelStatus event) {
if (event.deltaV > 0) { if (event.deltaV > 0) {
if (_last_direction == -1) { if (_last_direction == -1) {
if (_down_action) { if (_down_gesture) {
_down_action->release(false); _down_gesture->release(false);
_down_action->press(true); _down_gesture->press(true);
} }
} }
if (_up_action) if (_up_gesture)
_up_action->move(event.deltaV); _up_gesture->move(event.deltaV);
_last_direction = 1; _last_direction = 1;
} else if (event.deltaV < 0) { } else if (event.deltaV < 0) {
if (_last_direction == 1) { if (_last_direction == 1) {
if (_up_action) { if (_up_gesture) {
_up_action->release(false); _up_gesture->release(false);
_up_action->press(true); _up_gesture->press(true);
} }
} }
if (_down_action) if (_down_gesture)
_down_action->move((int16_t) -event.deltaV); _down_gesture->move((int16_t) -event.deltaV);
_last_direction = -1; _last_direction = -1;
} }
_last_scroll = now; _last_scroll = now;
} }
HiresScroll::IPC::IPC(HiresScroll* parent) : ipcgull::interface(
SERVICE_ROOT_NAME ".HiresScroll", {
{"GetConfig", {this, &IPC::getConfig, {"hires", "invert", "target"}}},
{"SetHires", {this, &IPC::setHires, {"hires"}}},
{"SetInvert", {this, &IPC::setInvert, {"invert"}}},
{"SetTarget", {this, &IPC::setTarget, {"target"}}},
{"SetUp", {this, &IPC::setUp, {"type"}}},
{"SetDown", {this, &IPC::setDown, {"type"}}},
}, {}, {}), _parent(*parent) {
}
std::tuple<bool, bool, bool> HiresScroll::IPC::getConfig() const {
std::shared_lock lock(_parent._config_mutex);
if (_parent._config.has_value()) {
if (std::holds_alternative<bool>(_parent._config.value())) {
return {std::get<bool>(_parent._config.value()), false, false};
} else {
const auto& config = std::get<config::HiresScroll>(_parent._config.value());
return {
config.hires.value_or(true),
config.invert.value_or(false),
config.target.value_or(false)
};
}
} else {
return {true, false, false};
}
}
config::HiresScroll& HiresScroll::IPC::_parentConfig() {
if (!_parent._config.has_value()) {
_parent._config = config::HiresScroll();
} else if (std::holds_alternative<bool>(_parent._config.value())) {
bool hires = std::get<bool>(_parent._config.value());
auto config = config::HiresScroll();
config.hires = hires;
_parent._config = hires;
}
return std::get<config::HiresScroll>(_parent._config.value());
}
void HiresScroll::IPC::setHires(bool hires) {
std::unique_lock lock(_parent._config_mutex);
_parentConfig().hires = hires;
_parent._mask |= hidpp20::HiresScroll::Mode::HiRes;
if (hires)
_parent._mode |= hidpp20::HiresScroll::Mode::HiRes;
else
_parent._mode &= ~hidpp20::HiresScroll::Mode::HiRes;
_parent._configure();
}
void HiresScroll::IPC::setInvert(bool invert) {
std::unique_lock lock(_parent._config_mutex);
_parentConfig().invert = invert;
_parent._mask |= hidpp20::HiresScroll::Mode::Inverted;
if (invert)
_parent._mode |= hidpp20::HiresScroll::Mode::Inverted;
else
_parent._mode &= ~hidpp20::HiresScroll::Mode::Inverted;
_parent._configure();
}
void HiresScroll::IPC::setTarget(bool target) {
std::unique_lock lock(_parent._config_mutex);
_parentConfig().target = target;
_parent._mask |= hidpp20::HiresScroll::Mode::Target;
if (target)
_parent._mode |= hidpp20::HiresScroll::Mode::Target;
else
_parent._mode &= ~hidpp20::HiresScroll::Mode::Target;
_parent._configure();
}
void HiresScroll::IPC::setUp(const std::string& type) {
std::unique_lock lock(_parent._config_mutex);
auto& config = _parentConfig();
if (!config.up.has_value()) {
config.up = config::NoGesture();
}
_parent._up_gesture = actions::Gesture::makeGesture(
_parent._device, type, config.up.value(), _parent._up_node);
if (!_parent._up_gesture->wheelCompatibility()) {
_parent._up_node.reset();
config.up.reset();
throw std::invalid_argument("incompatible gesture");
} else {
_parent._fixGesture(_parent._up_gesture);
}
}
void HiresScroll::IPC::setDown(const std::string& type) {
std::unique_lock lock(_parent._config_mutex);
auto& config = _parentConfig();
if (!config.down.has_value()) {
config.down = config::NoGesture();
}
_parent._down_gesture = actions::Gesture::makeGesture(
_parent._device, type, config.down.value(), _parent._down_node);
if (!_parent._down_gesture->wheelCompatibility()) {
_parent._down_node.reset();
config.down.reset();
throw std::invalid_argument("incompatible gesture");
} else {
_parent._fixGesture(_parent._down_gesture);
}
}

View File

@ -45,25 +45,55 @@ namespace logid::features {
private: private:
std::optional<backend::hidpp::Device::EvHandlerId> _ev_handler; std::optional<backend::hidpp::Device::EvHandlerId> _ev_handler;
void _makeAction(std::shared_ptr<actions::Gesture>& gesture, void _makeGesture(std::shared_ptr<actions::Gesture>& gesture,
std::optional<config::Gesture>& config, std::optional<config::Gesture>& config,
const std::string& direction); const std::string& direction);
void _configure();
void _fixGesture(const std::shared_ptr<actions::Gesture>& gesture);
void _handleScroll(backend::hidpp20::HiresScroll::WheelStatus event); void _handleScroll(backend::hidpp20::HiresScroll::WheelStatus event);
class IPC : public ipcgull::interface {
public:
explicit IPC(HiresScroll* parent);
[[nodiscard]] std::tuple<bool, bool, bool> getConfig() const;
void setHires(bool hires);
void setInvert(bool invert);
void setTarget(bool target);
void setUp(const std::string& type);
void setDown(const std::string& type);
private:
config::HiresScroll& _parentConfig();
HiresScroll& _parent;
};
std::shared_ptr<backend::hidpp20::HiresScroll> _hires_scroll; std::shared_ptr<backend::hidpp20::HiresScroll> _hires_scroll;
std::chrono::time_point<std::chrono::system_clock> _last_scroll; std::chrono::time_point<std::chrono::system_clock> _last_scroll;
int16_t _last_direction = 0; int16_t _last_direction = 0;
mutable std::shared_mutex _config_mutex;
std::optional<std::variant<bool, config::HiresScroll>>& _config; std::optional<std::variant<bool, config::HiresScroll>>& _config;
uint8_t _mode; uint8_t _mode;
uint8_t _mask; uint8_t _mask;
std::shared_ptr<actions::Gesture> _up_action; std::shared_ptr<actions::Gesture> _up_gesture;
std::shared_ptr<actions::Gesture> _down_action; std::shared_ptr<actions::Gesture> _down_gesture;
std::shared_ptr<ipcgull::node> _node; std::shared_ptr<ipcgull::node> _node;
std::shared_ptr<ipcgull::node> _up_node;
std::shared_ptr<ipcgull::node> _down_node;
std::shared_ptr<IPC> _ipc_interface;
}; };
} }

View File

@ -218,21 +218,21 @@ void ThumbWheel::_fixGesture(const std::shared_ptr<actions::Gesture>& gesture) c
axis->setHiresMultiplier(_wheel_info.divertedRes); axis->setHiresMultiplier(_wheel_info.divertedRes);
} catch (std::bad_cast& e) {} } catch (std::bad_cast& e) {}
gesture->press(true); if (gesture)
gesture->press(true);
} }
ThumbWheel::IPC::IPC(ThumbWheel* parent) : ThumbWheel::IPC::IPC(ThumbWheel* parent) : ipcgull::interface(
ipcgull::interface( SERVICE_ROOT_NAME ".ThumbWheel", {
SERVICE_ROOT_NAME ".ThumbWheel", { {"GetConfig", {this, &IPC::getConfig, {"divert", "invert"}}},
{"GetConfig", {this, &IPC::getConfig, {"divert", "invert"}}}, {"SetDivert", {this, &IPC::setDivert, {"divert"}}},
{"SetDivert", {this, &IPC::setDivert, {"divert"}}}, {"SetInvert", {this, &IPC::setInvert, {"invert"}}},
{"SetInvert", {this, &IPC::setInvert, {"invert"}}}, {"SetLeft", {this, &IPC::setLeft, {"type"}}},
{"SetLeft", {this, &IPC::setLeft, {"type"}}}, {"SetRight", {this, &IPC::setRight, {"type"}}},
{"SetRight", {this, &IPC::setRight, {"type"}}}, {"SetProxy", {this, &IPC::setProxy, {"type"}}},
{"SetProxy", {this, &IPC::setProxy, {"type"}}}, {"SetTap", {this, &IPC::setTap, {"type"}}},
{"SetTap", {this, &IPC::setTap, {"type"}}}, {"SetTouch", {this, &IPC::setTouch, {"type"}}},
{"SetTouch", {this, &IPC::setTouch, {"type"}}}, }, {}, {}), _parent(*parent) {
}, {}, {}), _parent(*parent) {
} }
config::ThumbWheel& ThumbWheel::IPC::_parentConfig() { config::ThumbWheel& ThumbWheel::IPC::_parentConfig() {

View File

@ -85,7 +85,7 @@ namespace logid::features {
int8_t _last_direction = 0; int8_t _last_direction = 0;
bool _last_proxy = false; bool _last_proxy = false;
bool _last_touch = false; bool _last_touch = false;
std::shared_mutex _config_mutex; mutable std::shared_mutex _config_mutex;
std::optional<config::ThumbWheel>& _config; std::optional<config::ThumbWheel>& _config;
std::optional<backend::hidpp::Device::EvHandlerId> _ev_handler; std::optional<backend::hidpp::Device::EvHandlerId> _ev_handler;