add discrete120 scroll event (#120)

* add discrete120 scroll event
This commit is contained in:
Ferdinand Schober
2024-05-04 03:34:13 +02:00
committed by GitHub
parent 973360a774
commit 1f0d386d4a
9 changed files with 167 additions and 64 deletions

View File

@@ -750,14 +750,24 @@ impl Dispatch<WlPointer, ()> for State {
}), }),
)); ));
} }
wl_pointer::Event::AxisValue120 { axis, value120 } => { wl_pointer::Event::Axis { time, axis, value } => {
let (_, client) = app.focused.as_ref().unwrap(); let (_, client) = app.focused.as_ref().unwrap();
app.pending_events.push_back(( app.pending_events.push_back((
*client, *client,
Event::Pointer(PointerEvent::Axis { Event::Pointer(PointerEvent::Axis {
time: 0, time,
axis: u32::from(axis) as u8, axis: u32::from(axis) as u8,
value: value120 as f64, value,
}),
));
}
wl_pointer::Event::AxisValue120 { axis, value120 } => {
let (_, client) = app.focused.as_ref().unwrap();
app.pending_events.push_back((
*client,
Event::Pointer(PointerEvent::AxisDiscrete120 {
axis: u32::from(axis) as u8,
value: value120,
}), }),
)); ));
} }

View File

@@ -139,10 +139,9 @@ fn to_mouse_event(wparam: WPARAM, lparam: LPARAM) -> Option<PointerEvent> {
relative_y: dy as f64, relative_y: dy as f64,
}) })
}, },
WPARAM(p) if p == WM_MOUSEWHEEL as usize => Some(PointerEvent::Axis { WPARAM(p) if p == WM_MOUSEWHEEL as usize => Some(PointerEvent::AxisDiscrete120 {
time: 0,
axis: 0, axis: 0,
value: -(mouse_low_level.mouseData as i32) as f64, value: -(mouse_low_level.mouseData as i32),
}), }),
WPARAM(p) if p == WM_XBUTTONDOWN as usize || p == WM_XBUTTONUP as usize => { WPARAM(p) if p == WM_XBUTTONDOWN as usize || p == WM_XBUTTONUP as usize => {
let hb = mouse_low_level.mouseData >> 16; let hb = mouse_low_level.mouseData >> 16;

View File

@@ -157,8 +157,20 @@ impl InputEmulation for LibeiEmulation {
} }
if let Some((d, s)) = self.scroll.as_mut() { if let Some((d, s)) = self.scroll.as_mut() {
match axis { match axis {
0 => s.scroll_discrete(0, value as i32), 0 => s.scroll(0., value as f32),
_ => s.scroll_discrete(value as i32, 0), _ => s.scroll(value as f32, 0.),
}
d.frame(self.serial, now);
}
}
crate::event::PointerEvent::AxisDiscrete120 { axis, value } => {
if !self.has_scroll {
return;
}
if let Some((d, s)) = self.scroll.as_mut() {
match axis {
0 => s.scroll_discrete(0, value),
_ => s.scroll_discrete(value, 0),
} }
d.frame(self.serial, now); d.frame(self.serial, now);
} }

View File

@@ -220,7 +220,7 @@ impl InputEmulation for MacOSEmulation {
axis, axis,
value, value,
} => { } => {
let value = value as i32 / 10; // FIXME: high precision scroll events let value = value as i32;
let (count, wheel1, wheel2, wheel3) = match axis { let (count, wheel1, wheel2, wheel3) = match axis {
0 => (1, value, 0, 0), // 0 = vertical => 1 scroll wheel device (y axis) 0 => (1, value, 0, 0), // 0 = vertical => 1 scroll wheel device (y axis)
1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x) 1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x)
@@ -231,7 +231,32 @@ impl InputEmulation for MacOSEmulation {
}; };
let event = match CGEvent::new_scroll_event( let event = match CGEvent::new_scroll_event(
self.event_source.clone(), self.event_source.clone(),
ScrollEventUnit::LINE, ScrollEventUnit::PIXEL,
count,
wheel1,
wheel2,
wheel3,
) {
Ok(e) => e,
Err(()) => {
log::warn!("scroll event creation failed!");
return;
}
};
event.post(CGEventTapLocation::HID);
}
PointerEvent::AxisDiscrete120 { axis, value } => {
let (count, wheel1, wheel2, wheel3) = match axis {
0 => (1, value, 0, 0), // 0 = vertical => 1 scroll wheel device (y axis)
1 => (2, 0, value, 0), // 1 = horizontal => 2 scroll wheel devices (y, x) -> (0, x)
_ => {
log::warn!("invalid scroll event: {axis}, {value}");
return;
}
};
let event = match CGEvent::new_scroll_event(
self.event_source.clone(),
ScrollEventUnit::PIXEL,
count, count,
wheel1, wheel1,
wheel2, wheel2,

View File

@@ -59,7 +59,8 @@ impl InputEmulation for WindowsEmulation {
time: _, time: _,
axis, axis,
value, value,
} => scroll(axis, value), } => scroll(axis, value as i32),
PointerEvent::AxisDiscrete120 { axis, value } => scroll(axis, value),
PointerEvent::Frame {} => {} PointerEvent::Frame {} => {}
}, },
Event::Keyboard(keyboard_event) => match keyboard_event { Event::Keyboard(keyboard_event) => match keyboard_event {
@@ -182,7 +183,7 @@ fn mouse_button(button: u32, state: u32) {
send_mouse_input(mi); send_mouse_input(mi);
} }
fn scroll(axis: u8, value: f64) { fn scroll(axis: u8, value: i32) {
let event_type = match axis { let event_type = match axis {
0 => MOUSEEVENTF_WHEEL, 0 => MOUSEEVENTF_WHEEL,
1 => MOUSEEVENTF_HWHEEL, 1 => MOUSEEVENTF_HWHEEL,
@@ -191,7 +192,7 @@ fn scroll(axis: u8, value: f64) {
let mi = MOUSEINPUT { let mi = MOUSEINPUT {
dx: 0, dx: 0,
dy: 0, dy: 0,
mouseData: -value as i32 as u32, mouseData: -value as u32,
dwFlags: event_type, dwFlags: event_type,
time: 0, time: 0,
dwExtraInfo: 0, dwExtraInfo: 0,

View File

@@ -180,6 +180,11 @@ impl VirtualInput {
self.pointer.axis(time, axis, value); self.pointer.axis(time, axis, value);
self.pointer.frame(); self.pointer.frame();
} }
PointerEvent::AxisDiscrete120 { axis, value } => {
let axis: Axis = (axis as u32).try_into()?;
self.pointer.axis(0, axis, (value / 15) as f64);
self.pointer.frame();
}
PointerEvent::Frame {} => self.pointer.frame(), PointerEvent::Frame {} => self.pointer.frame(),
} }
self.pointer.frame(); self.pointer.frame();

View File

@@ -125,6 +125,9 @@ impl InputEmulation for X11Emulation {
} => { } => {
self.emulate_scroll(axis, value); self.emulate_scroll(axis, value);
} }
PointerEvent::AxisDiscrete120 { axis, value } => {
self.emulate_scroll(axis, value as f64);
}
PointerEvent::Frame {} => {} PointerEvent::Frame {} => {}
}, },
Event::Keyboard(KeyboardEvent::Key { Event::Keyboard(KeyboardEvent::Key {

View File

@@ -62,8 +62,7 @@ impl<'a> DesktopPortalEmulation<'a> {
impl<'a> InputEmulation for DesktopPortalEmulation<'a> { impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
async fn consume(&mut self, event: crate::event::Event, _client: crate::client::ClientHandle) { async fn consume(&mut self, event: crate::event::Event, _client: crate::client::ClientHandle) {
match event { match event {
Pointer(p) => { Pointer(p) => match p {
match p {
PointerEvent::Motion { PointerEvent::Motion {
time: _, time: _,
relative_x, relative_x,
@@ -94,6 +93,19 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
log::warn!("{e}"); log::warn!("{e}");
} }
} }
PointerEvent::AxisDiscrete120 { axis, value } => {
let axis = match axis {
0 => Axis::Vertical,
_ => Axis::Horizontal,
};
if let Err(e) = self
.proxy
.notify_pointer_axis_discrete(&self.session, axis, value)
.await
{
log::warn!("{e}");
}
}
PointerEvent::Axis { PointerEvent::Axis {
time: _, time: _,
axis, axis,
@@ -103,18 +115,20 @@ impl<'a> InputEmulation for DesktopPortalEmulation<'a> {
0 => Axis::Vertical, 0 => Axis::Vertical,
_ => Axis::Horizontal, _ => Axis::Horizontal,
}; };
// TODO smooth scrolling let (dx, dy) = match axis {
Axis::Vertical => (0., value),
Axis::Horizontal => (value, 0.),
};
if let Err(e) = self if let Err(e) = self
.proxy .proxy
.notify_pointer_axis_discrete(&self.session, axis, value as i32) .notify_pointer_axis(&self.session, dx, dy, true)
.await .await
{ {
log::warn!("{e}"); log::warn!("{e}");
} }
} }
PointerEvent::Frame {} => {} PointerEvent::Frame {} => {}
} },
}
Keyboard(k) => { Keyboard(k) => {
match k { match k {
KeyboardEvent::Key { KeyboardEvent::Key {

View File

@@ -29,6 +29,10 @@ pub enum PointerEvent {
axis: u8, axis: u8,
value: f64, value: f64,
}, },
AxisDiscrete120 {
axis: u8,
value: i32,
},
Frame {}, Frame {},
} }
@@ -104,6 +108,9 @@ impl Display for PointerEvent {
axis, axis,
value, value,
} => write!(f, "scroll({axis}, {value})"), } => write!(f, "scroll({axis}, {value})"),
PointerEvent::AxisDiscrete120 { axis, value } => {
write!(f, "scroll-120 ({axis}, {value})")
}
PointerEvent::Frame {} => write!(f, "frame()"), PointerEvent::Frame {} => write!(f, "frame()"),
} }
} }
@@ -171,6 +178,7 @@ impl PointerEvent {
Self::Motion { .. } => PointerEventType::Motion, Self::Motion { .. } => PointerEventType::Motion,
Self::Button { .. } => PointerEventType::Button, Self::Button { .. } => PointerEventType::Button,
Self::Axis { .. } => PointerEventType::Axis, Self::Axis { .. } => PointerEventType::Axis,
Self::AxisDiscrete120 { .. } => PointerEventType::AxisDiscrete120,
Self::Frame { .. } => PointerEventType::Frame, Self::Frame { .. } => PointerEventType::Frame,
} }
} }
@@ -189,6 +197,7 @@ enum PointerEventType {
Motion, Motion,
Button, Button,
Axis, Axis,
AxisDiscrete120,
Frame, Frame,
} }
enum KeyboardEventType { enum KeyboardEventType {
@@ -213,6 +222,7 @@ impl TryFrom<u8> for PointerEventType {
x if x == Self::Motion as u8 => Ok(Self::Motion), x if x == Self::Motion as u8 => Ok(Self::Motion),
x if x == Self::Button as u8 => Ok(Self::Button), x if x == Self::Button as u8 => Ok(Self::Button),
x if x == Self::Axis as u8 => Ok(Self::Axis), x if x == Self::Axis as u8 => Ok(Self::Axis),
x if x == Self::AxisDiscrete120 as u8 => Ok(Self::AxisDiscrete120),
x if x == Self::Frame as u8 => Ok(Self::Frame), x if x == Self::Frame as u8 => Ok(Self::Frame),
_ => Err(anyhow!(ProtocolError { _ => Err(anyhow!(ProtocolError {
msg: format!("invalid pointer event type {}", value), msg: format!("invalid pointer event type {}", value),
@@ -313,6 +323,11 @@ impl From<&PointerEvent> for Vec<u8> {
let value = value.to_be_bytes(); let value = value.to_be_bytes();
[&time[..], &axis[..], &value[..]].concat() [&time[..], &axis[..], &value[..]].concat()
} }
PointerEvent::AxisDiscrete120 { axis, value } => {
let axis = axis.to_be_bytes();
let value = value.to_be_bytes();
[&axis[..], &value[..]].concat()
}
PointerEvent::Frame {} => { PointerEvent::Frame {} => {
vec![] vec![]
} }
@@ -421,6 +436,25 @@ impl TryFrom<Vec<u8>> for PointerEvent {
}; };
Ok(Self::Axis { time, axis, value }) Ok(Self::Axis { time, axis, value })
} }
PointerEventType::AxisDiscrete120 => {
let axis = match data.get(2) {
Some(d) => *d,
None => {
return Err(anyhow!(ProtocolError {
msg: "Expected 1 Byte at index 2".into(),
}));
}
};
let value = match data.get(3..7) {
Some(d) => i32::from_be_bytes(d.try_into()?),
None => {
return Err(anyhow!(ProtocolError {
msg: "Expected 4 Bytes at index 3".into(),
}));
}
};
Ok(Self::AxisDiscrete120 { axis, value })
}
PointerEventType::Frame => Ok(Self::Frame {}), PointerEventType::Frame => Ok(Self::Frame {}),
} }
} }