Skip to content
Open
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Per-application profiles switch with the focused app. An `[apps]` alias lets one
<td width="50%"><b>SmartShift</b><ul><li>Wheel mode and sensitivity</li><li>Scrolling force on SmartShift Enhanced mice</li><li>A permanent-ratchet switch</li></ul></td>
</tr>
<tr>
<td colspan="2"><img src="https://assets.openlogi.org/screenshots/macos/light/cards/scrolling.png" width="820" alt="Scrolling card with inversion, wheel resolution, and smooth wheel"><br><b>Scrolling</b><ul><li>Scroll inversion per device, natively or in software</li><li>Wheel resolution and smooth wheel scrolling</li><li>Hold Back or Forward and roll the wheel to scroll sideways on mice without a thumb wheel</li><li>Any action on each thumb-wheel direction, per app</li></ul></td>
<td colspan="2"><img src="https://assets.openlogi.org/screenshots/macos/light/cards/scrolling.png" width="820" alt="Scrolling card with vertical and horizontal scroll inversion, wheel resolution, and smooth wheel"><br><b>Scrolling</b><ul><li>Vertical and horizontal (thumb-wheel) scroll inversion per device, natively or in software</li><li>Wheel resolution, smooth wheel scrolling, and per-device thumb-wheel sensitivity</li><li>Hold Back or Forward and roll the wheel to scroll sideways on mice without a thumb wheel</li><li>Any action on each thumb-wheel direction, per app</li></ul></td>
</tr>
</table>

Expand Down Expand Up @@ -162,6 +162,7 @@ Feature coverage depends on what each device reports; the app shows only the con
| Feature | macOS | Windows | Linux |
| --- | --- | --- | --- |
| Mouse button remapping and gestures | ✓ | ✓ | ✓ |
| Per-device vertical and horizontal (thumb-wheel) scroll inversion | ✓ | ✓ | ✓ |
| Dedicated keyboard keys (media, emoji, screenshot, …) | ✓ | ✓ | ✓ |
| Keyboard F-key (F1–F12) remapping | ✓ | ✓ | — |
| Per-application profiles | ✓ | ✓ | X11, wlroots (sway, Hyprland), GNOME with the [companion extension](crates/openlogi-hook/gnome-shell-extension/README.md), KDE Plasma with the [companion KWin script](crates/openlogi-hook/kwin-script/README.md) |
Expand Down
110 changes: 94 additions & 16 deletions crates/openlogi-agent-core/src/capture_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use openlogi_hid::DeviceRoute;
use openlogi_hid::reprog_controls::DPI_MODE_SHIFT_CIDS;
use openlogi_hid::session::gesture::{
CaptureSpec, DIVERTABLE_STANDARD_BUTTONS, GESTURE_SOURCE_BUTTONS, HostMode, OnboardTarget,
SlotRoute,
SlotRoute, ThumbwheelCapture,
};
use tokio::sync::watch;

Expand Down Expand Up @@ -70,6 +70,8 @@ pub struct DispatchPlan {
/// This device's effective thumb-wheel sensitivity (device override or the
/// app-wide default).
pub thumbwheel_sensitivity: ThumbwheelSensitivity,
/// Whether this device's horizontal thumb wheel is inverted.
pub invert_thumbwheel: bool,
/// Whether this device's wheel scrolls inverted, so the scrolling the
/// agent posts for it — a wheel-hold button's horizontal scroll, and the
/// diverted smooth wheel, whose native invert bit has no effect — follows
Expand Down Expand Up @@ -178,6 +180,15 @@ impl DeviceCapturePlan {
}
}

/// Configured native `0x2150` thumb-wheel inversion target for an online device
/// whose thumb wheel stays in native reporting under the current plan.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct NativeThumbwheelTarget {
pub(crate) route: DeviceRoute,
pub(crate) unit_id: Option<[u8; 4]>,
pub(crate) inverted: bool,
}

/// Armed capture plans plus the latest route of every online physical device.
///
/// Plans omit a disabled device so capture leaves it native. Its route stays
Expand All @@ -186,6 +197,7 @@ impl DeviceCapturePlan {
pub struct CapturePublication {
plans: Vec<DeviceCapturePlan>,
routes: BTreeMap<PhysicalDeviceKey, DeviceRoute>,
native_thumbwheel: BTreeMap<PhysicalDeviceKey, NativeThumbwheelTarget>,
}

impl CapturePublication {
Expand All @@ -195,7 +207,22 @@ impl CapturePublication {
plans: Vec<DeviceCapturePlan>,
routes: BTreeMap<PhysicalDeviceKey, DeviceRoute>,
) -> Self {
Self { plans, routes }
Self {
plans,
routes,
native_thumbwheel: BTreeMap::new(),
}
}

/// Attach the native thumb-wheel inversion targets from the same rebuild so
/// post-teardown reconciliation sees the latest configured direction.
#[must_use]
pub(crate) fn with_native_thumbwheel(
mut self,
native_thumbwheel: BTreeMap<PhysicalDeviceKey, NativeThumbwheelTarget>,
) -> Self {
self.native_thumbwheel = native_thumbwheel;
self
}

/// A publication whose routes are the routes of `plans`.
Expand All @@ -208,7 +235,11 @@ impl CapturePublication {
.entry(plan.target.physical_key.clone())
.or_insert_with(|| plan.target.route.clone());
}
Self { plans, routes }
Self {
plans,
routes,
native_thumbwheel: BTreeMap::new(),
}
}

/// Capture sessions the watcher may arm.
Expand All @@ -223,6 +254,16 @@ impl CapturePublication {
self.routes.get(key)
}

/// Native thumb-wheel target for `key` when the latest config configures
/// `0x2150` inversion natively and no capture session diverts it.
#[must_use]
pub(crate) fn native_thumbwheel(
&self,
key: &PhysicalDeviceKey,
) -> Option<&NativeThumbwheelTarget> {
self.native_thumbwheel.get(key)
}

/// Whether a plan here hands the main wheel at `route` to its capture
/// session, which then owns that wheel's reporting mode.
#[must_use]
Expand All @@ -231,6 +272,15 @@ impl CapturePublication {
.iter()
.any(|plan| plan.target.route == *route && plan.target.spec.divert_main_wheel.is_some())
}

/// Whether a plan here hands the horizontal thumb wheel at `route` to its
/// capture session, which then owns that wheel's reporting mode.
#[must_use]
pub(crate) fn diverts_thumbwheel(&self, route: &DeviceRoute) -> bool {
self.plans
.iter()
.any(|plan| plan.target.route == *route && plan.target.spec.thumbwheel.is_captured())
}
}

/// Read-only, lossless, coalescing view of the latest capture publication.
Expand Down Expand Up @@ -402,25 +452,14 @@ pub fn plan_for_device(
})
})
.collect();
let thumbwheel_bindings_nondefault = [
ButtonId::Thumbwheel,
ButtonId::ThumbwheelScrollUp,
ButtonId::ThumbwheelScrollDown,
]
.iter()
.any(|button| {
bindings
.get(button)
.is_some_and(|binding| binding.click_action() != default_binding(*button))
});
let thumbwheel_sensitivity = config.thumbwheel_sensitivity(config_key);
let thumbwheel = thumbwheel_capture_spec(&bindings, thumbwheel_sensitivity);
DeviceCapturePlan {
target: CaptureTarget {
physical_key,
route,
spec: CaptureSpec {
capture_thumbwheel: thumbwheel_sensitivity != ThumbwheelSensitivity::DEFAULT
|| thumbwheel_bindings_nondefault,
thumbwheel,
divert_gesture_sources: GESTURE_SOURCE_BUTTONS
.into_iter()
.filter(|(_, button)| gesture_bindings.contains_key(button))
Expand All @@ -442,6 +481,7 @@ pub fn plan_for_device(
gesture_bindings,
side_gesture_bindings,
thumbwheel_sensitivity,
invert_thumbwheel: false,
invert_wheel: false,
momentum: config.momentum_scrolling(config_key),
pointer: None,
Expand All @@ -450,6 +490,28 @@ pub fn plan_for_device(
}
}

fn thumbwheel_capture_spec(
bindings: &BTreeMap<ButtonId, Binding>,
thumbwheel_sensitivity: ThumbwheelSensitivity,
) -> ThumbwheelCapture {
let thumbwheel_bindings_nondefault = [
ButtonId::Thumbwheel,
ButtonId::ThumbwheelScrollUp,
ButtonId::ThumbwheelScrollDown,
]
.iter()
.any(|button| {
bindings
.get(button)
.is_some_and(|binding| binding.click_action() != default_binding(*button))
});
if thumbwheel_sensitivity != ThumbwheelSensitivity::DEFAULT || thumbwheel_bindings_nondefault {
ThumbwheelCapture::CaptureOriginal
} else {
ThumbwheelCapture::Native
}
}

#[cfg(test)]
mod tests {
use openlogi_core::binding::{Binding, LongPressBinding, WheelHoldBinding};
Expand Down Expand Up @@ -489,6 +551,22 @@ mod tests {
assert!(!CapturePublication::default().diverts_main_wheel(&route()));
}

#[test]
fn a_publication_diverts_only_the_thumbwheel_its_plan_hands_to_capture() {
let other = DeviceRoute::Bolt {
receiver_uid: "cafe".into(),
slot: 3,
};
let mut plan = plan_for_device(&Config::default(), "mouse", route(), None, 0, true);
plan.target.spec.thumbwheel = ThumbwheelCapture::CaptureOriginal;
let diverting = CapturePublication::from_plans(vec![plan.clone()]);
assert!(diverting.diverts_thumbwheel(&route()));
assert!(!diverting.diverts_thumbwheel(&other));
plan.target.spec.thumbwheel = ThumbwheelCapture::Native;
assert!(!CapturePublication::from_plans(vec![plan]).diverts_thumbwheel(&route()));
assert!(!CapturePublication::default().diverts_thumbwheel(&route()));
}

/// A device's dispatch carries its own momentum-scrolling settings, and
/// a device without them the app-wide default.
#[test]
Expand Down
Loading
Loading