diff --git a/src/device/mod.rs b/src/device/mod.rs index 692a5b7..4cb4c1d 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -20,6 +20,8 @@ pub struct DeviceProfile { pub pen_y_max: i32, pub pen_pressure_max: i32, pub pen_distance_max: i32, + // Whether ABS_DISTANCE grows as the pen approaches (opposite of the evdev convention) + pub pen_distance_inverted: bool, pub pen_tilt_range: i32, // Touch screen dimensions diff --git a/src/device/rm2.rs b/src/device/rm2.rs index 1a582b2..4c1b2bf 100644 --- a/src/device/rm2.rs +++ b/src/device/rm2.rs @@ -11,6 +11,7 @@ pub const RM2: DeviceProfile = DeviceProfile { pen_y_max: 15725, pen_pressure_max: 4095, pen_distance_max: 255, + pen_distance_inverted: false, pen_tilt_range: 6400, // Touch screen: 1872×1404 display, ~210×158 mm → ~9 units/mm diff --git a/src/device/rmpp.rs b/src/device/rmpp.rs index be99029..fa8c8f7 100644 --- a/src/device/rmpp.rs +++ b/src/device/rmpp.rs @@ -17,6 +17,8 @@ pub const RMPP: DeviceProfile = DeviceProfile { pen_y_max: 15340, pen_pressure_max: 4096, pen_distance_max: 65535, + // Elan digitizer reports ~0 on proximity-in, rising to ~19000 near contact + pen_distance_inverted: true, pen_tilt_range: 9000, touch_x_max: 2064, diff --git a/src/input/pen.rs b/src/input/pen.rs index f38a86d..b3ddbaf 100644 --- a/src/input/pen.rs +++ b/src/input/pen.rs @@ -11,10 +11,11 @@ use crate::orientation::Orientation; use crate::palm::SharedPalmState; use crate::ssh; -use super::event::{key_event, parse_input_event, ABS_PRESSURE, EV_ABS, EV_SYN, SYN_REPORT}; +use super::event::{key_event, parse_input_event, ABS_PRESSURE, EV_ABS, EV_KEY, EV_SYN, SYN_REPORT}; const ABS_X: u16 = 0x00; const ABS_Y: u16 = 0x01; +const ABS_DISTANCE: u16 = 0x19; const ABS_TILT_X: u16 = 0x1a; const ABS_TILT_Y: u16 = 0x1b; @@ -61,6 +62,8 @@ pub fn run_pen( let mut buf = vec![0u8; device_profile.input_event_size]; let mut batch: Vec = Vec::with_capacity(32); let mut touch_down = false; + // evdev only reports changed values, so pressure persists across frames + let mut pressure: i32 = 0; let mut frame_count: u64 = 0; // For collecting X/Y/tilt values within a frame @@ -81,6 +84,17 @@ pub fn run_pen( let code = ev.raw_code(); let value = ev.raw_value(); + // BTN_TOUCH is derived from pressure below; drop the device's own + if ty == EV_KEY && code == btn_touch_code { + continue; + } + + if ty == EV_ABS && code == ABS_DISTANCE && device_profile.pen_distance_inverted { + let distance = device_profile.pen_distance_max - value.clamp(0, device_profile.pen_distance_max); + batch.push(InputEvent::new(ev.event_type(), code, distance)); + continue; + } + // Collect position and tilt values, defer transformation until SYN_REPORT if ty == EV_ABS { match code { @@ -128,11 +142,12 @@ pub fn run_pen( batch.insert(1, InputEvent::new(evdevil::event::EventType::from_raw(EV_ABS), Abs::TILT_Y.raw(), out_ty)); } - let pressure = batch + if let Some(p) = batch .iter() .rfind(|e| e.event_type().raw() == EV_ABS && e.raw_code() == ABS_PRESSURE) - .map(|e| e.raw_value()) - .unwrap_or(0); + { + pressure = p.raw_value(); + } let now_touching = pressure > 0; update_palm_state(&palm, now_touching);