Skip to content

Commit

Permalink
Merge pull request #72 from mahkoh/jorth/natural-scrolling
Browse files Browse the repository at this point in the history
input: add support for natural scrolling
  • Loading branch information
mahkoh authored Feb 4, 2024
2 parents b4d7306 + 78b557b commit 3d7bf39
Show file tree
Hide file tree
Showing 20 changed files with 165 additions and 17 deletions.
4 changes: 4 additions & 0 deletions jay-config/src/_private/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ impl Client {
self.send(&ClientMessage::SetTapEnabled { device, enabled })
}

pub fn set_input_natural_scrolling_enabled(&self, device: InputDevice, enabled: bool) {
self.send(&ClientMessage::SetNaturalScrollingEnabled { device, enabled })
}

pub fn set_input_drag_enabled(&self, device: InputDevice, enabled: bool) {
self.send(&ClientMessage::SetDragEnabled { device, enabled })
}
Expand Down
4 changes: 4 additions & 0 deletions jay-config/src/_private/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ pub enum ClientMessage<'a> {
GetWorkspaceCapture {
workspace: Workspace,
},
SetNaturalScrollingEnabled {
device: InputDevice,
enabled: bool,
},
}

#[derive(Encode, Decode, Debug)]
Expand Down
7 changes: 7 additions & 0 deletions jay-config/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ impl InputDevice {
pub fn set_drag_lock_enabled(self, enabled: bool) {
get!().set_input_drag_lock_enabled(self, enabled);
}

/// Sets whether natural scrolling is enabled for this device.
///
/// See <https://wayland.freedesktop.org/libinput/doc/latest/scrolling.html>
pub fn set_natural_scrolling_enabled(self, enabled: bool) {
get!().set_input_natural_scrolling_enabled(self, enabled);
}
}

/// A seat.
Expand Down
3 changes: 3 additions & 0 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub trait InputDevice {
fn set_tap_enabled(&self, enabled: bool);
fn set_drag_enabled(&self, enabled: bool);
fn set_drag_lock_enabled(&self, enabled: bool);
fn set_natural_scrolling_enabled(&self, enabled: bool);
}

#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
Expand Down Expand Up @@ -195,6 +196,7 @@ pub enum InputEvent {
AxisPx {
dist: Fixed,
axis: ScrollAxis,
inverted: bool,
},
AxisSource {
source: AxisSource,
Expand All @@ -205,6 +207,7 @@ pub enum InputEvent {
Axis120 {
dist: i32,
axis: ScrollAxis,
inverted: bool,
},
AxisFrame {
time_usec: u64,
Expand Down
18 changes: 18 additions & 0 deletions src/backends/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ struct MetalInputDevice {
events: SyncQueue<InputEvent>,
cb: CloneCell<Option<Rc<dyn Fn()>>>,
name: CloneCell<Rc<String>>,
natural_scrolling: Cell<bool>,

// state
pressed_keys: SmallMap<u32, (), 5>,
Expand All @@ -296,6 +297,7 @@ struct MetalInputDevice {
tap_enabled: Cell<Option<bool>>,
drag_enabled: Cell<Option<bool>>,
drag_lock_enabled: Cell<Option<bool>>,
natural_scrolling_enabled: Cell<Option<bool>>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -354,6 +356,9 @@ impl MetalInputDevice {
if let Some(enabled) = self.drag_lock_enabled.get() {
dev.device().set_drag_lock_enabled(enabled);
}
if let Some(enabled) = self.natural_scrolling_enabled.get() {
self.do_set_natural_scrolling_enabled(&dev, enabled);
}
}

fn pre_pause(&self) {
Expand All @@ -373,6 +378,12 @@ impl MetalInputDevice {
});
}
}

fn do_set_natural_scrolling_enabled(&self, dev: &RegisteredDevice, enabled: bool) {
dev.device().set_natural_scrolling_enabled(enabled);
self.natural_scrolling
.set(dev.device().natural_scrolling_enabled());
}
}

impl InputDevice for MetalInputDevice {
Expand Down Expand Up @@ -465,6 +476,13 @@ impl InputDevice for MetalInputDevice {
dev.device().set_drag_lock_enabled(enabled);
}
}

fn set_natural_scrolling_enabled(&self, enabled: bool) {
self.natural_scrolling_enabled.set(Some(enabled));
if let Some(dev) = self.inputdev.get() {
self.do_set_natural_scrolling_enabled(&dev, enabled);
}
}
}

impl MetalInputDevice {
Expand Down
2 changes: 2 additions & 0 deletions src/backends/metal/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ impl MetalBackend {
InputEvent::Axis120 {
dist: scroll as _,
axis,
inverted: dev.natural_scrolling.get(),
}
} else {
InputEvent::AxisPx {
dist: Fixed::from_f64(scroll),
axis,
inverted: dev.natural_scrolling.get(),
}
};
dev.event(ie);
Expand Down
4 changes: 4 additions & 0 deletions src/backends/metal/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ impl MetalBackend {
events: Default::default(),
cb: Default::default(),
name: Default::default(),
natural_scrolling: Default::default(),
pressed_keys: Default::default(),
pressed_buttons: Default::default(),
left_handed: Default::default(),
Expand All @@ -296,6 +297,7 @@ impl MetalBackend {
tap_enabled: Default::default(),
drag_enabled: Default::default(),
drag_lock_enabled: Default::default(),
natural_scrolling_enabled: Default::default(),
});
slots[slot] = Some(dev.clone());
self.device_holder
Expand Down Expand Up @@ -335,6 +337,8 @@ impl MetalBackend {
};
inputdev.device().set_slot(slot);
dev.name.set(Rc::new(inputdev.device().name()));
dev.natural_scrolling
.set(inputdev.device().natural_scrolling_enabled());
dev.inputdev.set(Some(inputdev));
dev.apply_config();
slf.state
Expand Down
9 changes: 9 additions & 0 deletions src/backends/x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ impl XBackend {
seat.mouse_event(InputEvent::Axis120 {
dist: val * AXIS_120,
axis,
inverted: false,
});
seat.mouse_event(InputEvent::AxisFrame {
time_usec: now_usec(),
Expand Down Expand Up @@ -1162,6 +1163,10 @@ impl InputDevice for XSeatKeyboard {
fn set_drag_lock_enabled(&self, enabled: bool) {
let _ = enabled;
}

fn set_natural_scrolling_enabled(&self, enabled: bool) {
let _ = enabled;
}
}

impl InputDevice for XSeatMouse {
Expand Down Expand Up @@ -1223,4 +1228,8 @@ impl InputDevice for XSeatMouse {
fn set_drag_lock_enabled(&self, enabled: bool) {
let _ = enabled;
}

fn set_natural_scrolling_enabled(&self, enabled: bool) {
let _ = enabled;
}
}
20 changes: 15 additions & 5 deletions src/cli/seat_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use {
wire::{
jay_compositor::{GetSeats, Seat, SeatEvents},
jay_seat_events::{
Axis120, AxisFrame, AxisPx, AxisSource, AxisStop, Button, Key, Modifiers,
PointerAbs, PointerRel,
Axis120, AxisFrame, AxisInverted, AxisPx, AxisSource, AxisStop, Button, Key,
Modifiers, PointerAbs, PointerRel,
},
},
},
Expand Down Expand Up @@ -134,6 +134,9 @@ async fn run(seat_test: Rc<SeatTest>) {
AxisSource::handle(tc, se, ps.clone(), move |ps, ev| {
ps.source.set(Some(ev.source));
});
AxisInverted::handle(tc, se, ps.clone(), move |ps, ev| {
ps.inverted[ev.axis as usize].set(ev.inverted != 0);
});
AxisPx::handle(tc, se, ps.clone(), move |ps, ev| {
ps.px[ev.axis as usize].set(Some(ev.dist));
});
Expand All @@ -152,6 +155,8 @@ async fn run(seat_test: Rc<SeatTest>) {
let stop_y = ps.stop[1].take();
let v120_x = ps.v120[0].take();
let v120_y = ps.v120[1].take();
let inverted_x = ps.inverted[0].get();
let inverted_y = ps.inverted[1].get();
if all || ev.seat == seat {
if all {
print!("Seat: {}, ", st.name(ev.seat));
Expand All @@ -175,9 +180,9 @@ async fn run(seat_test: Rc<SeatTest>) {
print!("Source: {}", source);
need_comma = true;
}
for (axis, px, steps, stop) in [
("horizontal", px_x, v120_x, stop_x),
("vertical", px_y, v120_y, stop_y),
for (axis, px, steps, stop, inverted) in [
("horizontal", px_x, v120_x, stop_x, inverted_x),
("vertical", px_y, v120_y, stop_y, inverted_y),
] {
if px.is_some() || steps.is_some() || stop {
comma!();
Expand All @@ -197,6 +202,11 @@ async fn run(seat_test: Rc<SeatTest>) {
print!("stop");
need_comma = true;
}
if inverted {
comma!();
print!("natural scrolling");
need_comma = true;
}
}
println!();
}
Expand Down
13 changes: 13 additions & 0 deletions src/config/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,16 @@ impl ConfigProxyHandler {
Ok(())
}

fn handle_set_natural_scrolling_enabled(
&self,
device: InputDevice,
enabled: bool,
) -> Result<(), CphError> {
let dev = self.get_device_handler_data(device)?;
dev.device.set_natural_scrolling_enabled(enabled);
Ok(())
}

fn handle_set_drag_lock_enabled(
&self,
device: InputDevice,
Expand Down Expand Up @@ -1296,6 +1306,9 @@ impl ConfigProxyHandler {
ClientMessage::GetWorkspaceCapture { workspace } => self
.handle_get_workspace_capture(workspace)
.wrn("get_workspace_capture")?,
ClientMessage::SetNaturalScrollingEnabled { device, enabled } => self
.handle_set_natural_scrolling_enabled(device, enabled)
.wrn("set_natural_scrolling_enabled")?,
}
Ok(())
}
Expand Down
5 changes: 5 additions & 0 deletions src/ifs/jay_seat_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ impl JaySeatEvents {
});
}
if let Some(dist) = ps.px[axis].get() {
self.client.event(AxisInverted {
self_id: self.id,
axis: axis as _,
inverted: ps.inverted[axis].get() as _,
});
self.client.event(AxisPx {
self_id: self.id,
dist,
Expand Down
2 changes: 1 addition & 1 deletion src/ifs/wl_seat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ impl Global for WlSeatGlobal {
}

fn version(&self) -> u32 {
8
9
}

fn break_loops(&self) {
Expand Down
25 changes: 20 additions & 5 deletions src/ifs/wl_seat/event_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use {
wl_keyboard::{self, WlKeyboard},
wl_pointer::{
self, PendingScroll, WlPointer, AXIS_DISCRETE_SINCE_VERSION,
AXIS_SOURCE_SINCE_VERSION, AXIS_STOP_SINCE_VERSION,
AXIS_VALUE120_SINCE_VERSION, POINTER_FRAME_SINCE_VERSION, WHEEL_TILT,
WHEEL_TILT_SINCE_VERSION,
AXIS_RELATIVE_DIRECTION_SINCE_VERSION, AXIS_SOURCE_SINCE_VERSION,
AXIS_STOP_SINCE_VERSION, AXIS_VALUE120_SINCE_VERSION, IDENTICAL, INVERTED,
POINTER_FRAME_SINCE_VERSION, WHEEL_TILT, WHEEL_TILT_SINCE_VERSION,
},
zwp_pointer_constraints_v1::{ConstraintType, SeatConstraintStatus},
zwp_relative_pointer_v1::ZwpRelativePointerV1,
Expand Down Expand Up @@ -199,8 +199,16 @@ impl WlSeatGlobal {
} => self.button_event(time_usec, button, state),

InputEvent::AxisSource { source } => self.pointer_owner.axis_source(source),
InputEvent::Axis120 { dist, axis } => self.pointer_owner.axis_120(dist, axis),
InputEvent::AxisPx { dist, axis } => self.pointer_owner.axis_px(dist, axis),
InputEvent::Axis120 {
dist,
axis,
inverted,
} => self.pointer_owner.axis_120(dist, axis, inverted),
InputEvent::AxisPx {
dist,
axis,
inverted,
} => self.pointer_owner.axis_px(dist, axis, inverted),
InputEvent::AxisStop { axis } => self.pointer_owner.axis_stop(axis),
InputEvent::AxisFrame { time_usec } => self.pointer_owner.frame(dev, self, time_usec),
}
Expand Down Expand Up @@ -613,6 +621,13 @@ impl WlSeatGlobal {
}
}
if let Some(delta) = event.px[i].get() {
if p.seat.version >= AXIS_RELATIVE_DIRECTION_SINCE_VERSION {
let direction = match event.inverted[i].get() {
false => IDENTICAL,
true => INVERTED,
};
p.send_axis_relative_direction(axis, direction);
}
p.send_axis(time, axis, delta);
}
if p.seat.version >= AXIS_STOP_SINCE_VERSION && event.stop[i].get() {
Expand Down
6 changes: 4 additions & 2 deletions src/ifs/wl_seat/pointer_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ impl PointerOwnerHolder {
self.pending_scroll.source.set(Some(axis_source as _));
}

pub fn axis_120(&self, delta: i32, axis: ScrollAxis) {
pub fn axis_120(&self, delta: i32, axis: ScrollAxis, inverted: bool) {
self.pending_scroll.v120[axis as usize].set(Some(delta));
self.pending_scroll.inverted[axis as usize].set(inverted);
}

pub fn axis_px(&self, delta: Fixed, axis: ScrollAxis) {
pub fn axis_px(&self, delta: Fixed, axis: ScrollAxis, inverted: bool) {
self.pending_scroll.px[axis as usize].set(Some(delta));
self.pending_scroll.inverted[axis as usize].set(inverted);
}

pub fn axis_stop(&self, axis: ScrollAxis) {
Expand Down
Loading

0 comments on commit 3d7bf39

Please sign in to comment.