Skip to content
Closed
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: 1 addition & 1 deletion bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ desktop (pointer, keyboard, clipboard), so run them when nobody is using the Mac
typing and mouse in a scratch TextEdit document and, with `--enable-resize`, a
display mode switch and back.
- `input.js`: clicks, typing (symbols, accents, CJK, emoji), Command shortcuts, drag
selection, pixel and wheel scrolling, cursor shape, on a page in a temporary Chrome.
selection, pixel scrolling, cursor shape, on a page in a temporary Chrome.
- `clipimage.js`: clipboard images both ways.
- `mic.js`: a 440 Hz tone through the microphone path (see voxl's log for drops).
- `perf.js`: full-screen animation; viewer FPS, pipeline and encode times.
5 changes: 1 addition & 4 deletions bench/macos/input.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// macOS input against a page in a headed Chrome with a temporary profile: clicks,
// double/right/middle click, typing (symbols, accents, CJK, emoji), Cmd shortcuts,
// Shift selection, drag selection, pixel and wheel scrolling, cursor shape.
// Shift selection, drag selection, pixel scrolling, cursor shape.
// Usage: PORT=6960 node input.js (needs playwright-core and Google Chrome)
const { chromium } = require(process.env.PLAYWRIGHT_CORE || 'playwright-core');
const net = require('net');
Expand Down Expand Up @@ -90,9 +90,6 @@ const SHIFT = 0xffe1, SUPER = 0xffeb;
await sleep(600);
const y1 = await p.evaluate(() => scrollY);
check(Math.abs((y1 - y0) - 300 / k) < 300 / k * 0.25, `pixel scroll: ${y1 - y0}px of page for 300 desktop px (expect ~${Math.round(300 / k)})`);
c.send('s,0,-3'); await sleep(600);
const y2 = await p.evaluate(() => scrollY);
check(y2 < y1, `wheel clicks scroll up: ${y1} -> ${y2}`);
await b.close();
console.log(failed ? `${failed} failed` : 'all passed');
process.exit(failed ? 1 : 0);
Expand Down
86 changes: 42 additions & 44 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ use x11rb::rust_connection::RustConnection;
use x11rb::wrapper::ConnectionExt as _;

pub enum InputEvent {
/// A desktop pixel, in X's own 16-bit coordinates.
Move {
x: i32,
y: i32,
x: i16,
y: i16,
},
Button {
button: u8,
button: crate::server::Button,
down: bool,
},
/// Wheel ticks: positive dy scrolls down, positive dx scrolls right.
Scroll {
dx: i32,
dy: i32,
},
/// Precise scroll in desktop pixels (trackpads, smooth wheels).
ScrollPx {
dx: f64,
Expand Down Expand Up @@ -67,6 +63,20 @@ const SHIFT_L: u32 = 0xffe1;
/// Scroll units per wheel notch (the X server's scroll increment, as for hi-res wheels).
const NOTCH: f64 = 120.0;

/// The most notches one pixel scroll turns, either way, on an X server without smooth
/// scrolling; the rest of a longer scroll is dropped.
///
/// Who needs it: X turns its wheel by a press and a release of a button per notch, so
/// this bounds what one message makes the input thread send X at 40 events.
///
/// What sets it: 93ad2d4 clamps every wheel message to 20 notches on X, macOS and
/// Windows, and the pixel-scroll fallback to wheel clicks keeps that clamp. A message is
/// what pointer.ts gathered in one animation frame; at the default 180 px a notch, 20
/// notches is 3,600 desktop pixels in a sixtieth of a second. Unmeasured beyond that:
/// half of it drops the rest of a flick above 1,800 px a frame, double lets one message
/// send X 80 events.
const MAX_NOTCHES: i8 = 20;

/// Smooth scrolling through relative motion on the XTEST pointer's scroll axes.
/// Stock X servers give that device no scroll axes; Xvfb built with
/// patches/xorg-server-xtest-scroll.patch does.
Expand Down Expand Up @@ -347,48 +357,37 @@ impl Injector {
let _ = std::fs::remove_file(&self.scratch_path);
}

/// Turns the wheel by `notches`, a press and a release of one of X's wheel buttons
/// for each: the first of `(back, forth)` for a negative count (4 up, 6 left), the
/// second for a positive one (5 down, 7 right).
fn wheel(&self, notches: i8, (back, forth): (u8, u8)) {
let button = if notches < 0 { back } else { forth };
for _ in 0..notches.unsigned_abs() {
self.fake(xproto::BUTTON_PRESS_EVENT, button, 0, 0);
self.fake(xproto::BUTTON_RELEASE_EVENT, button, 0, 0);
}
}

fn handle(&mut self, ev: InputEvent) {
match ev {
InputEvent::Move { x, y } => {
self.fake(xproto::MOTION_NOTIFY_EVENT, 0, x as i16, y as i16)
}
InputEvent::Move { x, y } => self.fake(xproto::MOTION_NOTIFY_EVENT, 0, x, y),
InputEvent::Button { button, down } => {
if (button as usize) < self.buttons.len() {
if self.buttons[button as usize] == down {
return;
}
self.buttons[button as usize] = down;
let held = &mut self.buttons[usize::from(button.number())];
if *held == down {
return;
}
*held = down;
self.fake(
if down {
xproto::BUTTON_PRESS_EVENT
} else {
xproto::BUTTON_RELEASE_EVENT
},
button,
button.number(),
0,
0,
)
}
InputEvent::Scroll { dx, dy } => {
let click = |b: u8, n: i32| {
for _ in 0..n.min(20) {
self.fake(xproto::BUTTON_PRESS_EVENT, b, 0, 0);
self.fake(xproto::BUTTON_RELEASE_EVENT, b, 0, 0);
}
};
// X buttons: 4/5 scroll up/down, 6/7 left/right.
match dy.cmp(&0) {
std::cmp::Ordering::Greater => click(5, dy),
std::cmp::Ordering::Less => click(4, -dy),
std::cmp::Ordering::Equal => {}
}
match dx.cmp(&0) {
std::cmp::Ordering::Greater => click(7, dx),
std::cmp::Ordering::Less => click(6, -dx),
std::cmp::Ordering::Equal => {}
}
}
InputEvent::ScrollPx { dx, dy } => {
let k = NOTCH / self.px_per_notch;
self.scroll_acc.0 += dx * k;
Expand All @@ -402,18 +401,17 @@ impl Injector {
}
} else {
// Whole notches only; the remainder carries into the next gesture event.
// One turns at most MAX_NOTCHES each way, and the rest of a longer
// scroll is dropped.
let (nx, ny) = (
(self.scroll_acc.0 / NOTCH).trunc(),
(self.scroll_acc.1 / NOTCH).trunc(),
);
self.scroll_acc.0 -= nx * NOTCH;
self.scroll_acc.1 -= ny * NOTCH;
if nx != 0.0 || ny != 0.0 {
self.handle(InputEvent::Scroll {
dx: nx as i32,
dy: ny as i32,
});
}
let most = f64::from(MAX_NOTCHES);
self.wheel(ny.clamp(-most, most) as i8, (4, 5));
self.wheel(nx.clamp(-most, most) as i8, (6, 7));
}
}
InputEvent::Key { keysym, down } => {
Expand Down Expand Up @@ -498,9 +496,9 @@ pub fn spawn(
.name("input".into())
.spawn(move || {
while let Ok(first) = rx.recv() {
let mut pending_move: Option<(i32, i32)> = None;
let mut pending_move: Option<(i16, i16)> = None;
let handle =
|inj: &mut Injector, ev: InputEvent, pm: &mut Option<(i32, i32)>| match ev {
|inj: &mut Injector, ev: InputEvent, pm: &mut Option<(i16, i16)>| match ev {
InputEvent::Move { x, y } => *pm = Some((x, y)),
other => {
if let Some((x, y)) = pm.take() {
Expand Down
63 changes: 23 additions & 40 deletions src/mac/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ use std::sync::mpsc;
use std::time::{Duration, Instant};

pub enum InputEvent {
/// A desktop pixel, in 16 bits as X's coordinates are; a CGFloat holds every one.
Move {
x: i32,
y: i32,
x: i16,
y: i16,
},
Button {
button: u8,
button: crate::server::Button,
down: bool,
},
/// Wheel ticks: positive dy scrolls down, positive dx scrolls right.
Scroll {
dx: i32,
dy: i32,
},
/// Precise scroll in desktop pixels (trackpads, smooth wheels).
ScrollPx {
dx: f64,
Expand Down Expand Up @@ -187,14 +183,14 @@ impl Injector {
}
}

fn to_point(&self, x: i32, y: i32) -> CGPoint {
fn to_point(&self, x: i16, y: i16) -> CGPoint {
let Some(g) = *super::GEOMETRY.lock() else {
return CGPoint::default();
};
let k = 1.0 / g.scale();
CGPoint {
x: g.bounds.origin.x + (x as f64 + 0.5) * k,
y: g.bounds.origin.y + (y as f64 + 0.5) * k,
x: g.bounds.origin.x + (f64::from(x) + 0.5) * k,
y: g.bounds.origin.y + (f64::from(y) + 0.5) * k,
}
}

Expand Down Expand Up @@ -259,8 +255,9 @@ impl Injector {
self.mouse(ty, b);
}
InputEvent::Button { button, down } => {
let b = button as usize;
if b >= self.buttons.len() || self.buttons[b] == down {
let number = button.number();
let b = usize::from(number);
if self.buttons[b] == down {
return;
}
self.buttons[b] = down;
Expand All @@ -269,37 +266,23 @@ impl Injector {
let (t, p, last) = self.last_click;
let near = (p.x - self.pos.x).abs() < 4.0 && (p.y - self.pos.y).abs() < 4.0;
self.clicks =
if last == button && near && t.elapsed() < Duration::from_millis(500) {
if last == number && near && t.elapsed() < Duration::from_millis(500) {
self.clicks % 3 + 1
} else {
1
};
self.last_click = (Instant::now(), self.pos, button);
self.last_click = (Instant::now(), self.pos, number);
}
// X buttons: 1 left, 2 middle, 3 right, 8 back, 9 forward.
use crate::server::Button;
let (dn, up, mb) = match button {
1 => (kCGEventLeftMouseDown, kCGEventLeftMouseUp, 0),
3 => (kCGEventRightMouseDown, kCGEventRightMouseUp, 1),
2 => (kCGEventOtherMouseDown, kCGEventOtherMouseUp, 2),
8 => (kCGEventOtherMouseDown, kCGEventOtherMouseUp, 3),
9 => (kCGEventOtherMouseDown, kCGEventOtherMouseUp, 4),
_ => return,
Button::Left => (kCGEventLeftMouseDown, kCGEventLeftMouseUp, 0),
Button::Right => (kCGEventRightMouseDown, kCGEventRightMouseUp, 1),
Button::Middle => (kCGEventOtherMouseDown, kCGEventOtherMouseUp, 2),
Button::Back => (kCGEventOtherMouseDown, kCGEventOtherMouseUp, 3),
Button::Forward => (kCGEventOtherMouseDown, kCGEventOtherMouseUp, 4),
};
self.mouse(if down { dn } else { up }, mb);
}
InputEvent::Scroll { dx, dy } => {
let e = unsafe {
CGEventCreateScrollWheelEvent2(
self.src,
kCGScrollEventUnitLine,
2,
-dy.clamp(-20, 20),
-dx.clamp(-20, 20),
0,
)
};
self.post(e);
}
InputEvent::ScrollPx { dx, dy } => {
// Desktop pixels -> points; macOS scrolls content down for positive values.
let k = super::GEOMETRY
Expand Down Expand Up @@ -430,10 +413,10 @@ impl Injector {
for ks in self.mods.keys().copied().collect::<Vec<_>>() {
self.key(ks, false);
}
for b in 1..self.buttons.len() {
if self.buttons[b] {
for button in crate::server::Button::ALL {
if self.buttons[usize::from(button.number())] {
self.handle(InputEvent::Button {
button: b as u8,
button,
down: false,
});
}
Expand Down Expand Up @@ -470,9 +453,9 @@ pub fn spawn(
.name("input".into())
.spawn(move || {
while let Ok(first) = rx.recv() {
let mut pending_move: Option<(i32, i32)> = None;
let mut pending_move: Option<(i16, i16)> = None;
let handle =
|inj: &mut Injector, ev: InputEvent, pm: &mut Option<(i32, i32)>| match ev {
|inj: &mut Injector, ev: InputEvent, pm: &mut Option<(i16, i16)>| match ev {
InputEvent::Move { x, y } => *pm = Some((x, y)),
other => {
if let Some((x, y)) = pm.take() {
Expand Down
Loading