diff --git a/src/gadgets/keyboard.rs b/src/gadgets/keyboard.rs index d6a8e0b..cc28faa 100644 --- a/src/gadgets/keyboard.rs +++ b/src/gadgets/keyboard.rs @@ -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]; @@ -543,15 +544,17 @@ 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); } } } @@ -559,16 +562,20 @@ pub fn attempt_read( 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; } } diff --git a/src/lib.rs b/src/lib.rs index 99bd41e..c107250 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ pub struct HidSpecification { pub struct HidMouse { pub mouse_path: String, pub mouse_poll_rate: Option, - pub mouse_side_buttons: bool + pub mouse_side_buttons: bool, } static mut HID_SPEC: Option = None; @@ -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 = 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 = 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 = Vec::new(); + loop { + if !KEYBOARD_READING { + break; + } - static mut KEYBOARD_THREADS: Vec = 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(())