From b05bdd6b1b5f99327f0e7779b54dfc03725767e5 Mon Sep 17 00:00:00 2001 From: Maxwell Beck Date: Sat, 28 Jun 2025 21:37:10 -0500 Subject: Input improvements --- src/main.rs | 74 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 56586ee..2f9b232 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,11 @@ use std::{collections::HashSet, pin::pin, time::Duration}; use bluer::{ Adapter, AdapterEvent, Device, DiscoveryFilter, DiscoveryTransport, Session, Uuid, - gatt::{local::Profile, remote::Characteristic}, + gatt::remote::Characteristic, }; use evdev::{ - AbsInfo, AbsoluteAxisCode, AttributeSet, EventType, InputEvent, KeyCode, MiscCode, PropType, - SynchronizationCode, UinputAbsSetup, - uinput::{VirtualDevice, VirtualDeviceBuilder}, + AbsInfo, AbsoluteAxisCode, AttributeSet, EventType, InputEvent, KeyCode, PropType, + SynchronizationCode, UinputAbsSetup, uinput::VirtualDevice, }; use tokio::{select, time::interval}; use tokio_stream::StreamExt; @@ -18,7 +17,7 @@ const HID_SERVICE: Result = const REPORT_REF: Result = Uuid::try_parse("00002908-0000-1000-8000-00805f9b34fb"); -const MAGIC_REPORT: u8 = 240; +// const MAGIC_REPORT: u8 = 240; const BUTTON_REPORT: u8 = 251; const TOUCH_REPORT: u8 = 252; @@ -97,14 +96,13 @@ async fn run(adapter: &Adapter) -> bluer::Result<()> { let chars = RemoteCharateristics::get(&remote).await?; let mut touch_stream = pin!(chars.touch.notify().await?); + let mut button_stream = pin!(chars.button.notify().await?); eprintln!("DBG starting"); let mut keys = AttributeSet::new(); keys.insert(KeyCode::BTN_TOUCH); keys.insert(KeyCode::BTN_TOOL_FINGER); - - let mut msc = AttributeSet::new(); - msc.insert(MiscCode::MSC_TIMESTAMP); + keys.insert(KeyCode::BTN_LEFT); let mut props = AttributeSet::new(); props.insert(PropType::POINTER); @@ -126,8 +124,6 @@ async fn run(adapter: &Adapter) -> bluer::Result<()> { AbsInfo::new(0, -2048, 2048, 32, 0, 16), )) .unwrap() - .with_msc(&msc) - .unwrap() .build() .unwrap(); @@ -141,21 +137,57 @@ async fn run(adapter: &Adapter) -> bluer::Result<()> { select! { ev = touch_stream.next() => { let ev = ev.unwrap(); - tx = ((((ev[5] & 15) as u16) << 8) + ev[4] as u16) as f32; - ty = ((((ev[5] & !15) as u16) >> 4) + ((ev[6] as u16) << 4)) as f32; - if tx >= 2048.0 { - tx -= 4096.0; - } - if ty >= 2048.0 { - ty -= 4096.0; + let touch_next = if ev[3] > 0 { 1 } else { 0 }; + if touch_next > 0 { + tx = ((((ev[5] & 15) as u16) << 8) + ev[4] as u16) as f32; + ty = ((((ev[5] & !15) as u16) >> 4) + ((ev[6] as u16) << 4)) as f32; + if tx >= 2048.0 { + tx -= 4096.0; + } + if ty >= 2048.0 { + ty -= 4096.0; + } + if touch == 0 { + x = tx; + y = ty; + } } - - touch = if ev[3] > 0 { 1 } else { 0 }; + touch = touch_next; // ts = (((ev[2] as u16) << 8) + (ev[1] as u16)) as i32; }, + ev = button_stream.next() => { + let ev = ev.unwrap(); + match (ev[0], ev[1]) { + (8, 0) => { + uinput + .emit(&[ + InputEvent::new(EventType::KEY.0, KeyCode::BTN_LEFT.0, 1), + InputEvent::new( + EventType::SYNCHRONIZATION.0, + SynchronizationCode::SYN_REPORT.0, + 0, + ), + ]) + .unwrap(); + }, + (0, 0) => { + uinput + .emit(&[ + InputEvent::new(EventType::KEY.0, KeyCode::BTN_LEFT.0, 0), + InputEvent::new( + EventType::SYNCHRONIZATION.0, + SynchronizationCode::SYN_REPORT.0, + 0, + ), + ]) + .unwrap(); + }, + (_, _) => (), + } + } _ = timer.tick() => { - x = 0.5 * x + 0.5 * tx; - y = 0.5 * y + 0.5 * ty; + x = 0.95 * x + 0.05 * tx; + y = 0.95 * y + 0.05 * ty; uinput .emit(&[ -- cgit 1.4.1