From 6b3796c9a0d3a2f1422bcbde4790c43417659722 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 16 Apr 2024 10:51:13 +1200 Subject: Update devices to match new specifications --- src/devices/input.rs | 176 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 109 insertions(+), 67 deletions(-) (limited to 'src/devices/input.rs') diff --git a/src/devices/input.rs b/src/devices/input.rs index f3191dd..f23d902 100644 --- a/src/devices/input.rs +++ b/src/devices/input.rs @@ -1,21 +1,45 @@ use crate::*; - +use phosphor::*; use std::collections::VecDeque; +const CONTROL: u8 = 0x80; +const ALT: u8 = 0x40; +const SHIFT: u8 = 0x20; + +const UP: u8 = 0x80; +const DOWN: u8 = 0x40; +const LEFT: u8 = 0x20; +const RIGHT: u8 = 0x10; +const CONFIRM: u8 = 0x08; +const CANCEL: u8 = 0x04; +const NEXT: u8 = 0x02; +const PREVIOUS: u8 = 0x01; +const AUX1: u8 = 0x02; +const AUX2: u8 = 0x01; + +macro_rules! test { ($value:expr, $mask:expr) => { $value & $mask != 0 }; } + + pub struct InputDevice { pub wake_flag: bool, - pub mouse_position: ScreenPosition, - pub mouse_button_state: u8, + pub pointer_position: ScreenPosition, + pub pointer_buttons: u8, + pub pointer_active: bool, + + pub horizontal_scroll: i8, + pub vertical_scroll: i8, + pub horizontal_scroll_delta: f64, + pub vertical_scroll_delta: f64, - pub horizontal_scroll_value: u16, - pub vertical_scroll_value: u16, - pub horizontal_scroll_value_delta: f64, - pub vertical_scroll_value_delta: f64, + pub text_queue: VecDeque, + pub modifiers: u8, + pub navigation: u8, - pub character_queue: VecDeque, - pub modifier_state: u8, - pub navigation_state: u8, + pub controller_1: u8, + pub controller_2: u8, + pub controller_3: u8, + pub controller_4: u8, } impl InputDevice { @@ -23,104 +47,122 @@ impl InputDevice { Self { wake_flag: false, - mouse_position: ScreenPosition::ZERO, - mouse_button_state: 0x00, + pointer_position: ScreenPosition::ZERO, + pointer_buttons: 0x00, + pointer_active: false, - horizontal_scroll_value: 0x0000, - vertical_scroll_value: 0x0000, - horizontal_scroll_value_delta: 0.0, - vertical_scroll_value_delta: 0.0, + horizontal_scroll: 0x0000, + vertical_scroll: 0x0000, + horizontal_scroll_delta: 0.0, + vertical_scroll_delta: 0.0, - character_queue: VecDeque::new(), - modifier_state: 0x00, - navigation_state: 0x00, + text_queue: VecDeque::new(), + modifiers: 0x00, + navigation: 0x00, + + controller_1: 0x00, + controller_2: 0x00, + controller_3: 0x00, + controller_4: 0x00, } } - pub fn mouse_button_action(&mut self, mask: u8, action: phosphor::Action) { - let new_button_state = match action { - phosphor::Action::Pressed => self.mouse_button_state | mask, - phosphor::Action::Released => self.mouse_button_state & !mask, + pub fn read_horizontal_scroll(&mut self) -> u8 { + let output_value = self.horizontal_scroll; + self.horizontal_scroll = 0; + return output_value as u8; + } + + pub fn read_vertical_scroll(&mut self) -> u8 { + let output_value = self.vertical_scroll; + self.vertical_scroll = 0; + return output_value as u8; + } + + pub fn on_pointer_button(&mut self, mask: u8, action: Action) { + let new_buttons = match action { + Action::Pressed => self.pointer_buttons | mask, + Action::Released => self.pointer_buttons & !mask, }; - if new_button_state != self.mouse_button_state { - self.mouse_button_state = new_button_state; + if new_buttons != self.pointer_buttons { + self.pointer_buttons = new_buttons; self.wake_flag = true; } } - pub fn move_mouse(&mut self, new_mouse_position: ScreenPosition) { - let old_mouse_position = self.mouse_position; - if new_mouse_position != old_mouse_position { - self.mouse_position = new_mouse_position; + pub fn on_pointer_move(&mut self, position: ScreenPosition) { + if position != self.pointer_position { + self.pointer_position = position; self.wake_flag = true; } } pub fn on_scroll_horizontal(&mut self, delta: f64) { - self.horizontal_scroll_value_delta += delta; - while self.horizontal_scroll_value_delta > 20.0 { - self.horizontal_scroll_value += 1; - self.horizontal_scroll_value_delta -= 20.0; + self.horizontal_scroll_delta += delta; + while self.horizontal_scroll_delta > 1.0 { + self.horizontal_scroll = self.horizontal_scroll.saturating_add(1); + self.horizontal_scroll_delta -= 1.0; self.wake_flag = true; } - while self.horizontal_scroll_value_delta < -20.0 { - self.horizontal_scroll_value -= 1; - self.horizontal_scroll_value_delta += 20.0; + while self.horizontal_scroll_delta < -1.0 { + self.horizontal_scroll = self.horizontal_scroll.saturating_sub(1); + self.horizontal_scroll_delta += 1.0; self.wake_flag = true; } } pub fn on_scroll_vertical(&mut self, delta: f64) { - self.vertical_scroll_value_delta += delta; - while self.vertical_scroll_value_delta > 20.0 { - self.vertical_scroll_value += 1; - self.vertical_scroll_value_delta -= 20.0; + self.vertical_scroll_delta += delta; + while self.vertical_scroll_delta > 1.0 { + self.vertical_scroll = self.vertical_scroll.saturating_add(1); + self.vertical_scroll_delta -= 1.0; self.wake_flag = true; } - while self.vertical_scroll_value_delta < -20.0 { - self.vertical_scroll_value -= 1; - self.vertical_scroll_value_delta += 20.0; + while self.vertical_scroll_delta < -1.0 { + self.vertical_scroll = self.vertical_scroll.saturating_sub(1); + self.vertical_scroll_delta += 1.0; self.wake_flag = true; } } pub fn on_character_input(&mut self, input: char) { - if let Ok(ascii) = u8::try_from(u32::from(input)) { - self.character_queue.push_back(ascii); + if let Ok(byte) = u8::try_from(u32::from(input)) { + self.text_queue.push_back(byte); self.wake_flag = true; } } - pub fn on_keyboard_input(&mut self, input: phosphor::KeyboardInput) { - let tab = self.modifier_state & 0x40 != 0; + pub fn on_keyboard_input(&mut self, input: KeyboardInput) { let mask = match input.key { - phosphor::KeyCode::Up => 0x80, - phosphor::KeyCode::Down => 0x40, - phosphor::KeyCode::Left => 0x20, - phosphor::KeyCode::Right => 0x10, - phosphor::KeyCode::Tab => match tab { false => 0x08, true => 0x04 }, - phosphor::KeyCode::Return => 0x02, - phosphor::KeyCode::Escape => 0x01, + KeyCode::Up => UP, + KeyCode::Down => DOWN, + KeyCode::Left => LEFT, + KeyCode::Right => RIGHT, + KeyCode::Return => CONFIRM, + KeyCode::Escape => CANCEL, + KeyCode::Tab => match test!(self.modifiers, SHIFT) { + false => NEXT, + true => PREVIOUS + }, _ => return, }; - let new_navigation_state = match input.action { - phosphor::Action::Pressed => self.navigation_state | mask, - phosphor::Action::Released => self.navigation_state & !mask, + let navigation = match input.action { + Action::Pressed => self.navigation | mask, + Action::Released => self.navigation & !mask, }; - if new_navigation_state != self.navigation_state { - self.navigation_state = new_navigation_state; + if navigation != self.navigation { + self.navigation = navigation; self.wake_flag = true; } } - pub fn on_modifier_change(&mut self, modifiers: phosphor::ModifiersState) { - let mut new_modifier_state = 0x00; - if modifiers.ctrl() { new_modifier_state |= 0x80 } - if modifiers.shift() { new_modifier_state |= 0x40 } - if modifiers.alt() { new_modifier_state |= 0x20 } - if modifiers.logo() { new_modifier_state |= 0x10 } - if new_modifier_state != self.modifier_state { - self.modifier_state = new_modifier_state; + pub fn on_modifier_change(&mut self, modifiers: ModifiersState) { + let mut new_modifiers = 0x00; + if modifiers.ctrl() { new_modifiers |= CONTROL } + if modifiers.alt() { new_modifiers |= ALT } + if modifiers.shift() { new_modifiers |= SHIFT } + if new_modifiers != self.modifiers { + self.modifiers = new_modifiers; self.wake_flag = true; } -- cgit v1.2.3-70-g09d2