Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/logid/InputDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <InputDevice.h>
#include <system_error>
#include <mutex>
#include <util/log.h>

extern "C"
{
Expand Down Expand Up @@ -99,6 +100,7 @@ void InputDevice::registerAxis(uint axis) {
}

void InputDevice::moveAxis(uint axis, int movement) {
logPrintf(WARN, "InputDevice Debug: Sending EV_REL axis=%u value=%d", axis, movement);
_sendEvent(EV_REL, axis, movement);
}

Expand Down
27 changes: 14 additions & 13 deletions src/logid/actions/gesture/AxisGesture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ void AxisGesture::press(bool init_threshold) {
}

void AxisGesture::release(bool primary) {
// Do nothing
_axis = 0;
(void) primary; // Suppress unused warning
}

void AxisGesture::move(int16_t axis) {
std::shared_lock lock(_config_mutex);
logPrintf(WARN, "AxisGesture Debug: Input axis=%d, input_axis_configured=%d", axis, _input_axis.has_value());
if (!_input_axis.has_value())
return;

Expand All @@ -82,15 +83,17 @@ void AxisGesture::move(int16_t axis) {
int low_res_axis = InputDevice::getLowResAxis(axis);
int hires_remainder = _hires_remainder;

if (new_axis > threshold) {
if (std::abs(new_axis) > threshold) {
double move = axis;
if (_axis < threshold)
move = new_axis - threshold;
bool negative_multiplier = _config.axis_multiplier.value_or(1) < 0;
if (negative_multiplier)
move *= -_config.axis_multiplier.value_or(1);
else
move *= _config.axis_multiplier.value_or(1);
if (std::abs(_axis) < threshold) {
// crossing threshold
if (new_axis > 0)
move = new_axis - threshold;
else
move = new_axis + threshold;
}

move *= _config.axis_multiplier.value_or(1);
// Handle hi-res multiplier
move *= _multiplier;

Expand All @@ -102,10 +105,8 @@ void AxisGesture::move(int16_t axis) {
_axis_remainder -= int_remainder;
}

if (negative_multiplier)
move_floor = -move_floor;

if (low_res_axis != -1) {
logPrintf(WARN, "AxisGesture Debug: Calling moveAxis (HiRes) with %f", move_floor);
int lowres_movement, hires_movement = (int) move_floor;
_device->virtualInput()->moveAxis(_input_axis.value(), hires_movement);
hires_remainder += hires_movement;
Expand Down Expand Up @@ -138,7 +139,7 @@ void AxisGesture::setHiresMultiplier(double multiplier) {
_hires_multiplier = multiplier;
if (_input_axis.has_value()) {
if (InputDevice::getLowResAxis(_input_axis.value()) != -1)
_multiplier = _config.axis_multiplier.value_or(1) * multiplier;
_multiplier = multiplier; // Don't bake axis_multiplier here, it's applied in move()
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/logid/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,22 @@ namespace logid::config {
template<>
struct config_io<double> : public primitive_io<double,
libconfig::Setting::TypeFloat> {
static double get(const libconfig::Setting& setting) {
switch (setting.getType()) {
case libconfig::Setting::TypeInt:
return (double) (int) setting;
case libconfig::Setting::TypeInt64:
return (double) (long long) setting;
case libconfig::Setting::TypeFloat:
default:
return (double) setting;
}
}

static double get(const libconfig::Setting& parent,
const std::string& name) {
return get(parent.lookup(name));
}
};
template<>
struct config_io<std::string> : public primitive_io<std::string,
Expand Down
52 changes: 36 additions & 16 deletions src/logid/features/ThumbWheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <actions/gesture/AxisGesture.h>
#include <Device.h>
#include <util/log.h>
#include <util/task.h>
#include <ipc_defs.h>

using namespace logid::features;
Expand Down Expand Up @@ -153,6 +154,7 @@ void ThumbWheel::setProfile(config::Profile& profile) {
void ThumbWheel::_handleEvent(hidpp20::ThumbWheel::ThumbwheelEvent event) {
std::shared_lock lock(_config_mutex);
if (event.flags & hidpp20::ThumbWheel::SingleTap) {
_touch_pending = false; // Cancel pending touch on tap
auto action = _tap_action;
if (action) {
action->press();
Expand All @@ -173,36 +175,53 @@ void ThumbWheel::_handleEvent(hidpp20::ThumbWheel::ThumbwheelEvent event) {
if ((bool) (event.flags & hidpp20::ThumbWheel::Touch) != _last_touch) {
_last_touch = !_last_touch;
if (_touch_action) {
if (_last_touch)
_touch_action->press();
else
_touch_action->release();
if (_last_touch) {
// Touch started: start timer
_touch_pending = true;
auto now = std::chrono::system_clock::now();
_touch_time = now;

logid::run_task_after([this, now]() {
// Only press if still pending AND it's the same touch session
if (_touch_pending.load() && _touch_time == now) {
std::shared_lock lock(_config_mutex);
// Re-check after acquiring lock
if (_touch_pending.load() && _touch_time == now) {
if (_touch_action)
_touch_action->press();
_touch_active = true;
_touch_pending = false;
}
}
}, std::chrono::milliseconds(TOUCH_DELAY_MS));
} else {
// Touch stopped: ALWAYS release
_touch_pending = false;
if (_touch_active.exchange(false)) {
std::shared_lock lock(_config_mutex);
if (_touch_action)
_touch_action->release();
}
}
}
}

if (event.rotationStatus != hidpp20::ThumbWheel::Inactive) {
// Make right positive unless inverted
event.rotation *= _wheel_info.defaultDirection;

if (event.rotationStatus == hidpp20::ThumbWheel::Start) {
if (_right_gesture)
_right_gesture->press(true);
if (_left_gesture)
_left_gesture->press(true);
}

if (event.rotation) {
int8_t direction = event.rotation > 0 ? 1 : -1;
_touch_pending = false; // Cancel pending touch on movement
std::shared_ptr<actions::Gesture> scroll_action;

if (direction > 0)
if (event.rotation > 0)
scroll_action = _right_gesture;
else
scroll_action = _left_gesture;

if (scroll_action) {
scroll_action->press(true);
scroll_action->move((int16_t) (direction * event.rotation));
scroll_action->move(event.rotation);
}
}

Expand All @@ -223,8 +242,9 @@ void ThumbWheel::_fixGesture(const std::shared_ptr<actions::Gesture>& gesture) c
axis->setHiresMultiplier(_wheel_info.divertedRes);
} catch (std::bad_cast& e) {}

if (gesture)
gesture->press(true);
if (gesture) {
// gesture->press(true); // Removed to prevent setting initial axis to +50
}
}

ThumbWheel::IPC::IPC(ThumbWheel* parent) : ipcgull::interface(
Expand Down
8 changes: 8 additions & 0 deletions src/logid/features/ThumbWheel.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <actions/gesture/Gesture.h>
#include <backend/hidpp20/features/ThumbWheel.h>
#include <backend/hidpp/Device.h>
#include <chrono>
#include <atomic>

namespace logid::features {
class ThumbWheel : public DeviceFeature {
Expand Down Expand Up @@ -85,6 +87,12 @@ namespace logid::features {

bool _last_proxy = false;
bool _last_touch = false;

// Smart touch detection
std::chrono::system_clock::time_point _touch_time;
std::atomic<bool> _touch_pending{false};
std::atomic<bool> _touch_active{false};
static constexpr int TOUCH_DELAY_MS = 150;

mutable std::shared_mutex _config_mutex;
std::reference_wrapper<std::optional<config::ThumbWheel>> _config;
Expand Down