Skip to content

Commit

Permalink
feat: reduce latency using channel
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueGlassBlock committed Aug 28, 2024
1 parent 2af74d4 commit 084aec2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions toucca-lib/src/serial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -179,14 +179,14 @@ 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);
}
if let Err(e) = self.update_touch() {
error!("Error updating touch info to game: {}", e);
}
sleep(Duration::from_micros(8));
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion toucca-lib/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ use windows::Win32::UI::WindowsAndMessaging::*;

use tracing::{debug, instrument};

static mut _MSG_SENDER: Option<std::sync::mpsc::Sender<()>> = 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<HWND>, u32)).as_mut().unwrap();
Expand Down Expand Up @@ -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<RECT> = RwLock::new(RECT {
Expand Down Expand Up @@ -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::<BOOL>() as u32, Some(std::mem::transmute(&value)))
Expand Down
4 changes: 3 additions & 1 deletion toucca/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 084aec2

Please sign in to comment.