From 084aec210f42b40463bd2776dc94615691d8e38c Mon Sep 17 00:00:00 2001 From: BlueGlassBlock Date: Wed, 28 Aug 2024 14:50:58 +0800 Subject: [PATCH] feat: reduce latency using channel --- toucca-lib/src/serial/mod.rs | 4 ++-- toucca-lib/src/window.rs | 17 ++++++++++++++++- toucca/src/lib.rs | 4 +++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/toucca-lib/src/serial/mod.rs b/toucca-lib/src/serial/mod.rs index cce4150..2d51c4a 100644 --- a/toucca-lib/src/serial/mod.rs +++ b/toucca-lib/src/serial/mod.rs @@ -164,8 +164,8 @@ impl TouccaState { #[instrument(skip_all)] unsafe fn cycle(&mut self) { - use std::thread::sleep; use std::time::Duration; + let rx = init_pair(); loop { // check whether the window is still alive if let Some(handle) = self.hwnd { @@ -179,6 +179,7 @@ impl TouccaState { hook_wnd_proc(hwnd); } } + let _ = rx.recv_timeout(Duration::from_millis(100)); self.touch_areas = window::get_active_areas(); if let Err(e) = self.read_and_update() { error!("Error reading and updating: {}", e); @@ -186,7 +187,6 @@ impl TouccaState { if let Err(e) = self.update_touch() { error!("Error updating touch info to game: {}", e); } - sleep(Duration::from_micros(8)); } } } diff --git a/toucca-lib/src/window.rs b/toucca-lib/src/window.rs index 51aeef6..40cbc3c 100644 --- a/toucca-lib/src/window.rs +++ b/toucca-lib/src/window.rs @@ -15,6 +15,16 @@ use windows::Win32::UI::WindowsAndMessaging::*; use tracing::{debug, instrument}; +static mut _MSG_SENDER: Option> = None; + +pub fn init_pair() -> std::sync::mpsc::Receiver<()> { + let (sender, receiver) = std::sync::mpsc::channel(); + unsafe { + _MSG_SENDER = Some(sender); + } + receiver +} + #[instrument(skip_all)] unsafe extern "system" fn _enum_window(hwnd: HWND, param: LPARAM) -> BOOL { let hwnd_ptr_proc_id = (param.0 as *mut (Option, u32)).as_mut().unwrap(); @@ -76,6 +86,11 @@ fn handle_touch(hwnd: HWND, w_param: WPARAM, l_param: LPARAM) { } CloseTouchInputHandle(std::mem::transmute::<_, HTOUCHINPUT>(l_param)).unwrap(); } + unsafe { + if let Some(sender) = _MSG_SENDER.as_ref() { + let _ = sender.send(()); + } + } } static _WINDOW_RECT: RwLock = RwLock::new(RECT { @@ -152,7 +167,7 @@ unsafe fn init_key_map() { #[instrument(skip_all)] unsafe fn setup_window(hwnd: HWND) { - RegisterTouchWindow(hwnd, REGISTER_TOUCH_WINDOW_FLAGS(TWF_WANTPALM.0 | TWF_FINETOUCH.0)).unwrap(); + RegisterTouchWindow(hwnd, REGISTER_TOUCH_WINDOW_FLAGS(TWF_WANTPALM.0)).unwrap(); unsafe fn set_window_feedback_setting(hwnd: HWND, feedback: FEEDBACK_TYPE, value: BOOL) -> BOOL { unsafe { SetWindowFeedbackSetting(hwnd, feedback, 0, size_of::() as u32, Some(std::mem::transmute(&value))) diff --git a/toucca/src/lib.rs b/toucca/src/lib.rs index aa6904d..330e71a 100644 --- a/toucca/src/lib.rs +++ b/toucca/src/lib.rs @@ -3,6 +3,7 @@ use std::ffi::c_void; use std::ptr::{null_mut, NonNull}; use std::sync::Mutex; use std::thread::JoinHandle; +use std::time::Duration; use toucca_lib::window::*; use toucca_lib::{load_segatools_config, CONFIG}; @@ -138,13 +139,14 @@ pub extern "system" fn mercury_io_touch_start(perform_scan: extern "C" fn(*mut b } let handle = std::thread::spawn(move || { debug!("Started touch poll thread"); + let rx = init_pair(); loop { + let _ = rx.recv_timeout(Duration::from_millis(100)); let mut cell_pressed: [bool; 240] = [false; 240]; for area in get_active_areas() { cell_pressed[area] = true; } perform_scan(cell_pressed.as_mut_ptr()); - std::thread::sleep(std::time::Duration::from_millis(5)); } }); *_TOUCH_THREAD_HANDLE.lock().unwrap() = Some(handle);