Skip to content

Commit

Permalink
Only flush keyboard when state changes
Browse files Browse the repository at this point in the history
Fixes regression with mouse low hz 1/2
  • Loading branch information
EthoIRL committed May 25, 2024
1 parent a409648 commit a8c136d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 71 deletions.
17 changes: 12 additions & 5 deletions src/gadgets/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ const BUFFER_LENGTH: usize = 12;
pub fn attempt_read(
keyboard: &mut Keyboard,
global_keyboard_state: &'static mut KeyboardState,
writer: &mut BufWriter<&mut File>
) -> Result<(), Error> {
let mut keyboard_buffer = [0u8; BUFFER_LENGTH];

Expand Down Expand Up @@ -543,32 +544,38 @@ pub fn attempt_read(
};

if event_type == EventType::EvKey {
let mut result: Result<(), Error> = Ok(());

match key_state {
KeyState::KeyDown | KeyState::KeyHold => {
match key_modifier {
Some(modifier) => {
return add_generic_down(modifier as i32, &global_keyboard_state.modifiers_down);
result = add_generic_down(modifier as i32, &global_keyboard_state.modifiers_down);
}
None => {
if let Some(code) = usb_code {
return add_generic_down(code as i32, &global_keyboard_state.keys_down);
result = add_generic_down(code as i32, &global_keyboard_state.keys_down);
}
}
}
}
KeyState::KeyUp => {
match key_modifier {
Some(modifier) => {
return remove_generic_down(modifier as i32, &global_keyboard_state.modifiers_down);
result = remove_generic_down(modifier as i32, &global_keyboard_state.modifiers_down);
}
None => {
if let Some(code) = usb_code {
return remove_generic_down(code as i32, &global_keyboard_state.keys_down);
result = remove_generic_down(code as i32, &global_keyboard_state.keys_down);
}
}
}
}
}
};

attempt_flush(global_keyboard_state, writer)?;

return result;
}
}

Expand Down
140 changes: 74 additions & 66 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct HidSpecification {
pub struct HidMouse {
pub mouse_path: String,
pub mouse_poll_rate: Option<i32>,
pub mouse_side_buttons: bool
pub mouse_side_buttons: bool,
}

static mut HID_SPEC: Option<HidSpecification> = None;
Expand All @@ -40,90 +40,98 @@ pub fn start_pass_through(specification: HidSpecification) -> Result<(), Error>
HID_SPEC = Some(specification.clone());
}

let gadget_device_keyboard = hid::open_gadget_device(specification.gadget_output.clone())?;

start_hot_reload(specification.mouse_inputs, specification.keyboard_inputs);
start_hot_reload(specification.mouse_inputs.clone(), specification.keyboard_inputs.clone());

unsafe {
thread::spawn(move || {
static mut MOUSE_THREADS: Vec<String> = Vec::new();
loop {
if !MOUSE_READING {
return;
}
if specification.mouse_inputs.is_some() {
let mouse_spec = specification.clone();
thread::spawn(move || {
static mut MOUSE_THREADS: Vec<String> = Vec::new();
loop {
if !MOUSE_READING {
return;
}

if MOUSE_INTERFACES.is_empty() {
thread::sleep(Duration::from_millis(1));
continue;
}
if MOUSE_INTERFACES.is_empty() {
thread::sleep(Duration::from_millis(1));
continue;
}

for (mouse_interface_index, mouse) in MOUSE_INTERFACES.iter_mut().enumerate() {
if !MOUSE_THREADS.contains(&mouse.mouse_path) || MOUSE_THREADS.is_empty() {
let gadget_mouse = match hid::open_gadget_device(specification.gadget_output.clone()) {
Ok(gadget_device) => gadget_device,
Err(_) => continue,
};
for (mouse_interface_index, mouse) in MOUSE_INTERFACES.iter_mut().enumerate() {
if !MOUSE_THREADS.contains(&mouse.mouse_path) || MOUSE_THREADS.is_empty() {
let gadget_mouse = match hid::open_gadget_device(mouse_spec.gadget_output.clone()) {
Ok(gadget_device) => gadget_device,
Err(_) => continue,
};

MOUSE_THREADS.push(mouse.mouse_path.clone());
MOUSE_THREADS.push(mouse.mouse_path.clone());

let mut mouse_writer = BufWriter::new(gadget_mouse);
thread::spawn(move || {
loop {
if !MOUSE_READING {
break;
}
let mut mouse_writer = BufWriter::new(gadget_mouse);
thread::spawn(move || {
loop {
if !MOUSE_READING {
break;
}

if mouse::attempt_read(mouse, &mut mouse_writer).is_err() {
MOUSE_INTERFACES.remove(mouse_interface_index);
MOUSE_THREADS.remove(mouse_interface_index);
if mouse::attempt_read(mouse, &mut mouse_writer).is_err() {
MOUSE_INTERFACES.remove(mouse_interface_index);
MOUSE_THREADS.remove(mouse_interface_index);

break;
};
}
});
break;
};
}
});
}
}

thread::sleep(Duration::from_millis(1));
}
}
});
});
}

thread::spawn(move || {
let mut keyboard_writer = BufWriter::new(gadget_device_keyboard);
if specification.keyboard_inputs.is_some() {
let keyboard_spec = specification.clone();
thread::spawn(move || {
static mut KEYBOARD_THREADS: Vec<String> = Vec::new();
loop {
if !KEYBOARD_READING {
break;
}

static mut KEYBOARD_THREADS: Vec<String> = Vec::new();
loop {
if !KEYBOARD_READING {
break;
}
if KEYBOARD_INTERFACES.is_empty() {
thread::sleep(Duration::from_millis(1));
continue;
}

if KEYBOARD_INTERFACES.is_empty() {
thread::sleep(Duration::from_millis(1));
continue;
}
for (keyboard_interface_index, keyboard) in KEYBOARD_INTERFACES.iter_mut().enumerate() {
if !KEYBOARD_THREADS.contains(&keyboard.keyboard_path) || KEYBOARD_THREADS.is_empty() {
let gadget_keyboard = match hid::open_gadget_device(keyboard_spec.gadget_output.clone()) {
Ok(gadget_device) => gadget_device,
Err(_) => continue,
};

for (keyboard_interface_index, keyboard) in KEYBOARD_INTERFACES.iter_mut().enumerate() {
if !KEYBOARD_THREADS.contains(&keyboard.keyboard_path) || KEYBOARD_THREADS.is_empty() {
KEYBOARD_THREADS.push(keyboard.keyboard_path.clone());
KEYBOARD_THREADS.push(keyboard.keyboard_path.clone());

thread::spawn(move || {
loop {
if !KEYBOARD_READING {
break;
}
let mut keyboard_writer = BufWriter::new(gadget_keyboard);
thread::spawn(move || {
loop {
if !KEYBOARD_READING {
break;
}

if keyboard::attempt_read(keyboard, &mut GLOBAL_KEYBOARD_STATE).is_err() {
KEYBOARD_INTERFACES.remove(keyboard_interface_index);
KEYBOARD_THREADS.remove(keyboard_interface_index);
if keyboard::attempt_read(keyboard, &mut GLOBAL_KEYBOARD_STATE, &mut keyboard_writer).is_err() {
KEYBOARD_INTERFACES.remove(keyboard_interface_index);
KEYBOARD_THREADS.remove(keyboard_interface_index);

break;
};
}
});
break;
};
}
});
}
}
}

_ = keyboard::attempt_flush(&mut GLOBAL_KEYBOARD_STATE, &mut keyboard_writer);
}
});
});
}
}

Ok(())
Expand Down

0 comments on commit a8c136d

Please sign in to comment.