fix: VirtIO keyboard input latency and direction accuracy

- Driver now drains SYN/repeat/unknown events in a loop instead of
  returning None on the first non-key event
- Snake drains all pending input events per tick, applies only the
  latest direction change (prevents stale event accumulation)
This commit is contained in:
2026-10-19 00:34:13 +02:00
parent 0a47b5489a
commit 2792387348
2 changed files with 30 additions and 26 deletions

View File

@@ -65,9 +65,11 @@ extern "C" fn main() {
} }
fn handle_input(snake: &mut Snake) { fn handle_input(snake: &mut Snake) {
// Drain all pending events, apply the latest direction
loop {
let _ = syscall::send(INPUT_EP, 0, 0, 0, 0); let _ = syscall::send(INPUT_EP, 0, 0, 0, 0);
let (tag, kind, _, _) = syscall::receive(INPUT_EP); let (tag, kind, _, _) = syscall::receive(INPUT_EP);
if tag == 0 { break; } // NoEvent — queue drained
if tag == 1 { if tag == 1 {
match kind { match kind {
0 => snake.set_direction(Direction::Up), 0 => snake.set_direction(Direction::Up),
@@ -78,6 +80,7 @@ fn handle_input(snake: &mut Snake) {
} }
} }
} }
}
fn wait_tick() { fn wait_tick() {
let start = syscall::get_time(); let start = syscall::get_time();

View File

@@ -129,6 +129,7 @@ fn linux_keycode_to_key(code: u16) -> Option<Key> {
impl InputDevice for VirtioInputDevice { impl InputDevice for VirtioInputDevice {
fn poll(&mut self) -> Option<InputEvent> { fn poll(&mut self) -> Option<InputEvent> {
loop {
let current_used = self.used_idx(); let current_used = self.used_idx();
if current_used == self.last_used { if current_used == self.last_used {
return None; return None;
@@ -149,8 +150,8 @@ impl InputDevice for VirtioInputDevice {
} }
} }
} }
// Skip SYN events, repeats, and unknown keys — keep draining
None }
} }
fn has_event(&self) -> bool { fn has_event(&self) -> bool {