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:
@@ -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),
|
||||||
@@ -77,6 +79,7 @@ fn handle_input(snake: &mut Snake) {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wait_tick() {
|
fn wait_tick() {
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user