Skip to content

Commit

Permalink
input: add support for natural scrolling
Browse files Browse the repository at this point in the history
Closes #69
  • Loading branch information
mahkoh committed Feb 4, 2024
1 parent b4d7306 commit 887e2b6
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 0 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
1 change: 1 addition & 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
11 changes: 11 additions & 0 deletions src/backends/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,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 +355,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() {
dev.device().set_natural_scrolling_enabled(enabled);
}
}

fn pre_pause(&self) {
Expand Down Expand Up @@ -465,6 +469,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() {
dev.device().set_natural_scrolling_enabled(enabled);
}
}
}

impl MetalInputDevice {
Expand Down
1 change: 1 addition & 0 deletions src/backends/metal/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,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
8 changes: 8 additions & 0 deletions src/backends/x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,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 +1227,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;
}
}
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
8 changes: 8 additions & 0 deletions src/it/test_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ trait TestInputDevice: InputDevice {
fn set_drag_lock_enabled(&self, enabled: bool) {
let _ = enabled;
}

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

impl<T: TestInputDevice> InputDevice for T {
Expand Down Expand Up @@ -502,4 +506,8 @@ impl<T: TestInputDevice> InputDevice for T {
fn set_drag_lock_enabled(&self, enabled: bool) {
<Self as TestInputDevice>::set_drag_lock_enabled(self, enabled)
}

fn set_natural_scrolling_enabled(&self, enabled: bool) {
<Self as TestInputDevice>::set_natural_scrolling_enabled(self, enabled)
}
}
13 changes: 13 additions & 0 deletions src/libinput/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use {
sys::{
libinput_device, libinput_device_config_accel_set_profile,
libinput_device_config_accel_set_speed, libinput_device_config_left_handed_set,
libinput_device_config_scroll_get_natural_scroll_enabled,
libinput_device_config_scroll_set_natural_scroll_enabled,
libinput_device_config_tap_get_drag_enabled,
libinput_device_config_tap_get_drag_lock_enabled,
libinput_device_config_tap_get_enabled, libinput_device_config_tap_set_drag_enabled,
Expand Down Expand Up @@ -146,6 +148,17 @@ impl<'a> LibInputDevice<'a> {
_ => false,
}
}

pub fn set_natural_scrolling_enabled(&self, enabled: bool) {
unsafe {
libinput_device_config_scroll_set_natural_scroll_enabled(self.dev, enabled as _);
}
}

#[allow(dead_code)]
pub fn natural_scrolling_enabled(&self) -> bool {
unsafe { libinput_device_config_scroll_get_natural_scroll_enabled(self.dev) != 0 }
}
}

impl RegisteredDevice {
Expand Down
7 changes: 7 additions & 0 deletions src/libinput/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ extern "C" {
pub fn libinput_device_config_tap_get_drag_lock_enabled(
device: *mut libinput_device,
) -> libinput_config_drag_lock_state;
pub fn libinput_device_config_scroll_set_natural_scroll_enabled(
device: *mut libinput_device,
enable: c::c_int,
) -> libinput_config_status;
pub fn libinput_device_config_scroll_get_natural_scroll_enabled(
device: *mut libinput_device,
) -> c::c_int;

pub fn libinput_event_destroy(event: *mut libinput_event);
pub fn libinput_event_get_type(event: *mut libinput_event) -> libinput_event_type;
Expand Down

0 comments on commit 887e2b6

Please sign in to comment.