Merge main into feat/proof-setup-save-load

This commit is contained in:
dotCooCoo
2026-11-18 23:51:32 -08:00
103 changed files with 3678 additions and 355 deletions
+70 -25
View File
@@ -1,4 +1,5 @@
//! macOS: a drawing tablet's pen pressure (#852), tilt and barrel rotation (#764). winit 0.30
//! macOS: a drawing tablet's pen pressure (#852), tilt and barrel rotation (#764), and an airbrush
//! pen's finger wheel. winit 0.30
//! hands a pen to the window there as a mouse, without them, so the app reads them from AppKit
//! itself and writes them into the UI's [`StylusFeed`] (`Services::stylus`), which the canvas
//! reads, as on X11 ([`crate::tablet`]).
@@ -14,8 +15,9 @@
//! (−1..1 on each axis, ±[`TILT_DEGREES`] at the ends) and its `rotation` go into the feed as the
//! pen's pose, kept for that move too. AppKit gives a rotation of 0 for a pen that does not read
//! one and a tilt of 0 for an upright pen, so one event cannot tell a pen without tilt or rotation
//! from one held upright and unturned. Each reads as none until the pen reports a value other
//! than 0 after it comes near ([`Pen`]).
//! from one held upright and unturned. Its `tangentialPressure` (−1..1, an airbrush pen's finger
//! wheel) goes into the pose as the wheel, 0 for a pen without one as well. Each reads as none
//! until the pen reports a value other than 0 after it comes near ([`Pen`]).
//! A mouse event of any other subtype is a mouse's or a trackpad's: the feed is set to `None`.
//! - An `NSEventTypeTabletPoint` event carries the same pressure, tilt and rotation without a move.
//! - A tablet proximity event (`NSEventTypeTabletProximity`, or a mouse event of that subtype) says
@@ -68,6 +70,8 @@ struct RawEvent {
tilt: (f64, f64),
/// `rotation`, in degrees (the same events as `pressure`).
rotation: f32,
/// `tangentialPressure`, −1..1 (the same events as `pressure`).
tangential: f32,
/// `pointingDeviceType` (proximity events only).
device: usize,
/// `isEnteringProximity` (proximity events only).
@@ -107,18 +111,42 @@ impl RawEvent {
/// and the pen's rotation is taken the same way. [`PenPose`]'s rotation runs clockwise, so the
/// angle is negated and brought into 0..360.
///
/// The tilt is left out unless `tilts`, and the rotation unless `turns` ([`Pen`]).
fn pose(&self, tilts: bool, turns: bool) -> PenPose {
/// AppKit's `tangentialPressure` is −1.0..1.0, the finger wheel of an airbrush pen on the
/// devices that have one (<https://developer.apple.com/documentation/appkit/nsevent/tangentialpressure>).
/// It becomes [`PenPose`]'s wheel of 0..1, −1.0 at 0 and 1.0 at 1; a value past either end is
/// clamped.
///
/// The tilt is left out unless `read.tilts`, the rotation unless `read.turns`, and the wheel
/// unless `read.wheels` ([`Pen`]).
fn pose(&self, read: Reads) -> PenPose {
let degrees = |v: f64| (v.clamp(-1.0, 1.0) * TILT_DEGREES) as f32;
// `0.0 - r` rather than `-r`: a rotation of 0 gives 0, not −0.
let rotation = turns.then(|| (0.0 - self.rotation).rem_euclid(360.0));
PenPose { tilt: tilts.then(|| [degrees(self.tilt.0), degrees(self.tilt.1)]), rotation }
let rotation = read.turns.then(|| (0.0 - self.rotation).rem_euclid(360.0));
let wheel = read.wheels.then(|| (self.tangential.clamp(-1.0, 1.0) + 1.0) / 2.0);
PenPose { tilt: read.tilts.then(|| [degrees(self.tilt.0), degrees(self.tilt.1)]), rotation, wheel }
}
/// Whether the event reports a tilt other than 0, and a rotation other than 0.
fn reads(&self) -> (bool, bool) {
/// Whether the event reports a tilt other than 0, a rotation other than 0, and a tangential
/// pressure other than 0.
fn reads(&self) -> Reads {
let read = |v: f64| v.is_finite() && v != 0.0;
(read(self.tilt.0) || read(self.tilt.1), read(f64::from(self.rotation)))
Reads { tilts: read(self.tilt.0) || read(self.tilt.1), turns: read(f64::from(self.rotation)), wheels: read(f64::from(self.tangential)) }
}
}
/// Which of a pen's tilt, rotation and finger wheel it has reported.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Reads {
tilts: bool,
turns: bool,
wheels: bool,
}
impl Reads {
fn add(&mut self, other: Reads) {
self.tilts |= other.tilts;
self.turns |= other.turns;
self.wheels |= other.wheels;
}
}
@@ -148,14 +176,14 @@ impl Update {
}
}
/// Which end of the pen is near the tablet, and whether it has reported a tilt or a rotation other
/// than 0 since it came near: until it has, its tilt or rotation reads as none, since AppKit gives
/// 0 for a pen without one and for one held upright or unturned.
/// Which end of the pen is near the tablet, and whether it has reported a tilt, a rotation or a
/// tangential pressure other than 0 since it came near: until it has, that value reads as none,
/// since AppKit gives 0 for a pen without one and for one held upright, unturned or with its
/// wheel half way.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Pen {
eraser: bool,
tilts: bool,
turns: bool,
read: Reads,
}
impl Pen {
@@ -163,16 +191,14 @@ impl Pen {
if e.proximity() {
// Entering with the tip or the eraser end: it hovers. A puck, or leaving, is a mouse.
self.eraser = e.entering && e.device == device::ERASER;
(self.tilts, self.turns) = (false, false);
self.read = Reads::default();
let pen = e.entering && e.device != device::CURSOR;
return Update::Set(pen.then_some(PenSample { pressure: 0.0, eraser: self.eraser, ..PenSample::default() }));
}
if e.point() {
let (tilts, turns) = e.reads();
self.tilts |= tilts;
self.turns |= turns;
self.read.add(e.reads());
}
let sample = PenSample { pressure: e.pressure, eraser: self.eraser, pose: e.pose(self.tilts, self.turns) };
let sample = PenSample { pressure: e.pressure, eraser: self.eraser, pose: e.pose(self.read) };
match (e.point(), e.mouse()) {
(true, true) => Update::Move(sample),
(true, false) => Update::Set(Some(sample)),
@@ -246,6 +272,7 @@ mod monitor {
let tilt = e.tilt();
raw.tilt = (tilt.x, tilt.y);
raw.rotation = e.rotation();
raw.tangential = e.tangentialPressure();
}
if raw.proximity() {
raw.device = e.pointingDeviceType().0;
@@ -326,26 +353,44 @@ mod tests {
let sample = |pressure, pose| PenSample { pressure, eraser: false, pose };
assert_eq!(p.handle(&pen(DRAG, 0.5)), Update::Move(tip(0.5)), "no tilt or rotation reported yet");
let leaning = RawEvent { tilt: (0.5, -0.25), rotation: 30.0, ..pen(DRAG, 0.5) };
let pose = PenPose { tilt: Some([30.0, -15.0]), rotation: Some(330.0) };
let pose = PenPose { tilt: Some([30.0, -15.0]), rotation: Some(330.0), wheel: None };
assert_eq!(p.handle(&leaning), Update::Move(sample(0.5, pose)));
let past_the_ends = RawEvent { tilt: (-4.0, 1.5), rotation: 270.0, ..pen(event_type::TABLET_POINT, 0.8) };
let pose = PenPose { tilt: Some([-60.0, 60.0]), rotation: Some(90.0) };
let pose = PenPose { tilt: Some([-60.0, 60.0]), rotation: Some(90.0), wheel: None };
assert_eq!(p.handle(&past_the_ends), Update::Set(Some(sample(0.8, pose))), "clamped");
let pose = PenPose { tilt: Some([0.0, 0.0]), rotation: Some(0.0) };
let pose = PenPose { tilt: Some([0.0, 0.0]), rotation: Some(0.0), wheel: None };
let Update::Move(upright) = p.handle(&pen(DRAG, 0.6)) else { panic!("a pen's drag is a move") };
assert_eq!(upright, sample(0.6, pose), "once reported, 0 is upright and unturned");
assert!(upright.pose.rotation.is_some_and(f32::is_sign_positive), "{upright:?}");
let feed = StylusFeed::default();
p.handle(&RawEvent { tilt: (f64::NAN, 0.0), rotation: -90.0, ..pen(DRAG, 0.3) }).apply(&feed);
assert_eq!(feed.pose(), Some(PenPose { tilt: None, rotation: Some(90.0) }), "a tilt that is not a number is left out");
assert_eq!(feed.pose(), Some(PenPose { rotation: Some(90.0), ..PenPose::default() }), "a tilt that is not a number is left out");
p.handle(&proximity(1, true));
let tilted = RawEvent { tilt: (0.0, 0.5), ..pen(event_type::TABLET_POINT, 0.8) };
let pose = PenPose { tilt: Some([0.0, 30.0]), rotation: None };
let pose = PenPose { tilt: Some([0.0, 30.0]), ..PenPose::default() };
assert_eq!(p.handle(&tilted), Update::Set(Some(sample(0.8, pose))), "no rotation reported since the pen came near");
p.handle(&RawEvent { kind: DRAG, ..Default::default() }).apply(&feed);
assert_eq!(feed.pose(), None, "a mouse moved");
}
/// An airbrush pen's finger wheel (AppKit's `tangentialPressure`, −1..1) reaches the feed as
/// 0..1, once the pen has reported a value other than 0 since it came near.
#[test]
fn an_airbrush_wheel_goes_into_the_feed() {
let mut p = Pen::default();
let wheel = |p: &mut Pen, tangential: f32| match p.handle(&RawEvent { tangential, ..pen(DRAG, 0.5) }) {
Update::Move(s) => s.pose.wheel,
u => panic!("a pen's drag is a move: {u:?}"),
};
assert_eq!(wheel(&mut p, 0.0), None, "no wheel reported yet");
assert_eq!(wheel(&mut p, -1.0), Some(0.0));
assert_eq!(wheel(&mut p, 0.5), Some(0.75));
assert_eq!(wheel(&mut p, 0.0), Some(0.5), "once reported, 0 is half way");
assert_eq!(wheel(&mut p, 3.0), Some(1.0), "clamped");
p.handle(&proximity(1, true));
assert_eq!(wheel(&mut p, 0.0), None, "not reported since the pen came near");
}
#[test]
fn the_feed_gets_each_moves_pressure_within_0_to_1() {
let feed = StylusFeed::default();
+56 -14
View File
@@ -1,4 +1,5 @@
//! Linux: a drawing tablet's pen pressure, tilt and barrel rotation under X11, XWayland included
//! Linux: a drawing tablet's pen pressure, tilt, barrel rotation and airbrush finger wheel under
//! X11, XWayland included
//! (#764). winit 0.30 hands a pen to the window there as a mouse, without any of them, so the app
//! reads them from XInput2 itself and writes them into the UI's [`StylusFeed`]
//! (`Services::stylus`), which the canvas reads.
@@ -14,11 +15,17 @@
//! - "Abs Tilt X" and "Abs Tilt Y" (a pen that has both), in degrees as they are when the range
//! fits in ±90, otherwise scaled to ±[`TILT_DEGREES`];
//! - "Abs Rotary Z" (the barrel rotation of an Art Pen), scaled to a turn with the middle of the
//! range at 0°.
//! range at 0°;
//! - "Abs Wheel" (the finger wheel of an airbrush pen), scaled from the valuator's range to 0..1.
//! XWayland also sends an Art Pen's barrel rotation on this valuator
//! (<https://github.com/mirror/xserver/blob/master/hw/xwayland/xwayland-input.c>), so under
//! XWayland it reads as the wheel. A tablet pad's wheel is on a device without a pressure axis,
//! which is not read.
//!
//! Any other pointer device moving sets the feed to `None`. Raw events carry only the valuators
//! that changed, so the last value of each axis of each pen is kept; an axis the pen has that no
//! event has carried yet reads 0 (upright, unturned), and an axis it lacks reads nothing.
//! event has carried yet reads 0 (upright, unturned), and an axis it lacks reads nothing. The
//! wheel reads nothing until an event carries it.
//!
//! The X server lists a pen's eraser end as a device of its own, whose name or XInput type contains
//! "eraser" (xf86-input-wacom's "… Pen eraser" of type `ERASER`, xf86-input-libinput's
@@ -60,6 +67,7 @@ enum AxisKind {
TiltX,
TiltY,
Rotation,
Wheel,
}
impl AxisKind {
@@ -70,6 +78,7 @@ impl AxisKind {
"Abs Tilt X" => Some(Self::TiltX),
"Abs Tilt Y" => Some(Self::TiltY),
"Abs Rotary Z" => Some(Self::Rotation),
"Abs Wheel" => Some(Self::Wheel),
_ => None,
}
}
@@ -81,6 +90,7 @@ impl AxisKind {
Self::TiltX => 1,
Self::TiltY => 2,
Self::Rotation => 3,
Self::Wheel => 4,
}
}
}
@@ -95,18 +105,22 @@ struct Axis {
}
impl Axis {
/// A raw value of this valuator as a pressure of 0..1, a tilt in degrees (−90..90) or a
/// rotation in degrees (−180..180, the middle of the range at 0). A value that is not a number
/// is a mouse's pressure, 1, or a tilt or rotation of 0.
/// A raw value of this valuator as a pressure of 0..1, a tilt in degrees (−90..90), a rotation
/// in degrees (−180..180, the middle of the range at 0) or a wheel of 0..1. A value that is not
/// a number is a mouse's pressure, 1, a tilt or rotation of 0, or a wheel half way.
fn map(&self, v: f64) -> f32 {
if !v.is_finite() {
return if self.kind == AxisKind::Pressure { 1.0 } else { 0.0 };
return match self.kind {
AxisKind::Pressure => 1.0,
AxisKind::Wheel => 0.5,
_ => 0.0,
};
}
let span = self.max - self.min;
let unit = if span.is_finite() && span > 0.0 { (v - self.min) / span } else { v };
let unit = unit.clamp(0.0, 1.0);
match self.kind {
AxisKind::Pressure => unit as f32,
AxisKind::Pressure | AxisKind::Wheel => unit as f32,
AxisKind::TiltX | AxisKind::TiltY if self.min >= -90.0 && self.max <= 90.0 && span > 0.0 => v.clamp(-90.0, 90.0) as f32,
AxisKind::TiltX | AxisKind::TiltY => ((unit * 2.0 - 1.0) * TILT_DEGREES) as f32,
AxisKind::Rotation => ((unit - 0.5) * 360.0) as f32,
@@ -173,7 +187,7 @@ enum Update {
#[derive(Debug, Default)]
struct Pens {
devices: HashMap<u16, Device>,
last: HashMap<u16, [Option<f32>; 4]>,
last: HashMap<u16, [Option<f32>; 5]>,
}
impl Pens {
@@ -200,10 +214,12 @@ impl Pens {
}
}
}
// An axis the pen has reads 0 until an event carries it; one it lacks reads nothing.
// An axis the pen has reads 0 until an event carries it; one it lacks reads nothing. The
// wheel reads nothing until an event carries it: XWayland lists the axis for every pen.
let value = |kind: AxisKind| device.axis(kind).map(|_| last.get(kind.slot()).copied().flatten().unwrap_or(0.0));
let tilt = value(AxisKind::TiltX).zip(value(AxisKind::TiltY)).map(|(x, y)| [x, y]);
let pose = PenPose { tilt, rotation: value(AxisKind::Rotation) };
let wheel = device.axis(AxisKind::Wheel).and_then(|_| last.get(AxisKind::Wheel.slot()).copied().flatten());
let pose = PenPose { tilt, rotation: value(AxisKind::Rotation), wheel };
Update::Set(Some(PenSample { pressure: value(AxisKind::Pressure).unwrap_or(0.0), eraser: device.eraser, pose }))
}
@@ -430,7 +446,7 @@ mod tests {
let mut pens = Pens::default();
pens.set_devices([pen(9), Device::new(10, "Wacom Intuos Pro M Pen eraser", Some("ERASER"), pen_valuators()), mouse(12)]);
let feed = StylusFeed::default();
let pose = PenPose { tilt: Some([0.0, 0.0]), rotation: Some(0.0) };
let pose = PenPose { tilt: Some([0.0, 0.0]), rotation: Some(0.0), wheel: None };
pens.raw_event(&feed, 9, &[0b100], &[32767.5]);
assert_eq!(pens.handle(9, []), Update::Set(Some(PenSample { pressure: 0.5, eraser: false, pose })));
assert_eq!(feed.eraser(), Some(false));
@@ -479,10 +495,36 @@ mod tests {
// A pen whose tilt and rotation no event has carried yet is upright and unturned.
let mut fresh = Pens::default();
fresh.set_devices([pen(9)]);
let upright = PenPose { tilt: Some([0.0, 0.0]), rotation: Some(0.0) };
let upright = PenPose { tilt: Some([0.0, 0.0]), rotation: Some(0.0), wheel: None };
assert_eq!(fresh.handle(9, [(2, 0.0)]), Update::Set(Some(PenSample { pressure: 0.0, eraser: false, pose: upright })));
}
/// An airbrush pen's "Abs Wheel" valuator goes into the feed as 0..1 of its range, once an
/// event carries it; a pen that lists it but has not moved it reads no wheel, and a device
/// without a pressure axis (a tablet pad's wheel) is not a pen.
#[test]
fn an_airbrush_wheel_goes_into_the_feed() {
let mut valuators = pen_valuators();
valuators.push((6, "Abs Wheel".to_string(), 0.0, 1023.0));
let airbrush = Device::new(9, "Wacom Intuos Pro M Pen stylus", Some("STYLUS"), valuators);
let pad = Device::new(13, "Wacom Intuos Pro M Pad pad", Some("PAD"), [(6, "Abs Wheel".to_string(), 0.0, 71.0)]);
let mut pens = Pens::default();
pens.set_devices([airbrush, pad, pen(10)]);
let feed = StylusFeed::default();
pens.raw_event(&feed, 9, &[0b100], &[32767.5]);
assert_eq!(feed.pose().and_then(|q| q.wheel), None, "no event has carried the wheel yet");
pens.raw_event(&feed, 9, &[0b100_0000], &[255.75]);
assert_eq!(feed.pose().and_then(|q| q.wheel), Some(0.25));
pens.raw_event(&feed, 9, &[0b100_0000], &[5000.0]);
assert_eq!(feed.pose().and_then(|q| q.wheel), Some(1.0), "clamped to the range");
pens.raw_event(&feed, 9, &[0b100], &[100.0]);
assert_eq!(feed.pose().and_then(|q| q.wheel), Some(1.0), "kept while other valuators move");
pens.raw_event(&feed, 13, &[0b100_0000], &[30.0]);
assert_eq!((feed.held(), feed.pose()), (None, None), "the pad is not a pen");
pens.raw_event(&feed, 10, &[0b100_0000], &[512.0]);
assert_eq!(feed.pose().and_then(|q| q.wheel), None, "a pen without the valuator");
}
/// A device listed again unchanged keeps its last tilt and rotation; another device listed
/// under the same id starts upright and unturned.
#[test]
@@ -495,7 +537,7 @@ mod tests {
assert_eq!(pose.tilt, Some([-30.0, 45.0]));
let other = Device::new(9, "HUION Kamvas Pen Pen (0x1)", Some("STYLUS"), pen_valuators());
pens.set_devices([other]);
let upright = PenPose { tilt: Some([0.0, 0.0]), rotation: Some(0.0) };
let upright = PenPose { tilt: Some([0.0, 0.0]), rotation: Some(0.0), wheel: None };
assert_eq!(pens.handle(9, [(2, 0.0)]), Update::Set(Some(PenSample { pressure: 0.0, eraser: false, pose: upright })));
}
+7 -6
View File
@@ -133,12 +133,13 @@ fn pen_sample(pressure: u32, pen_mask: u32, pen_flags: u32, in_contact: bool, po
/// How a pen whose `POINTER_PEN_INFO` holds `pen_mask`, `tilt_x`, `tilt_y` and `rotation` is
/// held: the tilt when the pen reports it along either axis (0 along an axis it doesn't), the
/// rotation when it reports one, each kept within [`PenPose`]'s ranges.
/// rotation when it reports one, each kept within [`PenPose`]'s ranges. `POINTER_PEN_INFO` has no
/// field for an airbrush pen's finger wheel, so the pose has none.
fn pen_pose(pen_mask: u32, tilt_x: i32, tilt_y: i32, rotation: u32) -> PenPose {
let along = |mask: u32, t: i32| if pen_mask & mask != 0 { t as f32 } else { 0.0 };
let tilt = (pen_mask & (PEN_MASK_TILT_X | PEN_MASK_TILT_Y) != 0).then(|| [along(PEN_MASK_TILT_X, tilt_x), along(PEN_MASK_TILT_Y, tilt_y)]);
let rotation = (pen_mask & PEN_MASK_ROTATION != 0).then_some(rotation as f32);
PenPose { tilt, rotation }.sanitized()
PenPose { tilt, rotation, wheel: None }.sanitized()
}
/// Whether a mouse message with this extra information (`GetMessageExtraInfo`) comes from a mouse,
@@ -426,12 +427,12 @@ mod tests {
#[test]
fn a_pens_tilt_and_rotation_come_where_it_reports_them() {
let all = PEN_MASK_TILT_X | PEN_MASK_TILT_Y | PEN_MASK_ROTATION;
assert_eq!(pen_pose(all, 30, -45, 90), PenPose { tilt: Some([30.0, -45.0]), rotation: Some(90.0) });
assert_eq!(pen_pose(all, 30, -45, 90), PenPose { tilt: Some([30.0, -45.0]), rotation: Some(90.0), wheel: None });
assert_eq!(pen_pose(PEN_MASK_PRESSURE, 30, -45, 90), PenPose::default(), "a pen that reports neither");
assert_eq!(pen_pose(PEN_MASK_TILT_X, 30, -45, 90), PenPose { tilt: Some([30.0, 0.0]), rotation: None }, "one axis");
assert_eq!(pen_pose(PEN_MASK_ROTATION, 0, 0, 359), PenPose { tilt: None, rotation: Some(359.0) }, "a 6D Art Pen's barrel");
assert_eq!(pen_pose(PEN_MASK_TILT_X, 30, -45, 90), PenPose { tilt: Some([30.0, 0.0]), ..PenPose::default() }, "one axis");
assert_eq!(pen_pose(PEN_MASK_ROTATION, 0, 0, 359), PenPose { rotation: Some(359.0), ..PenPose::default() }, "a 6D Art Pen's barrel");
let wild = pen_pose(all, 400, i32::MIN, 720);
assert_eq!(wild, PenPose { tilt: Some([90.0, -90.0]), rotation: Some(0.0) }, "kept within the ranges");
assert_eq!(wild, PenPose { tilt: Some([90.0, -90.0]), rotation: Some(0.0), wheel: None }, "kept within the ranges");
let pose = pen_pose(all, 10, 20, 180);
assert_eq!(pen_sample(512, PEN_MASK_PRESSURE | all, 0, true, pose).pose, pose);
}