summaryrefslogtreecommitdiff
path: root/src/devices/input.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/devices/input.rs')
-rw-r--r--src/devices/input.rs128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/devices/input.rs b/src/devices/input.rs
new file mode 100644
index 0000000..f3191dd
--- /dev/null
+++ b/src/devices/input.rs
@@ -0,0 +1,128 @@
+use crate::*;
+
+use std::collections::VecDeque;
+
+pub struct InputDevice {
+ pub wake_flag: bool,
+
+ pub mouse_position: ScreenPosition,
+ pub mouse_button_state: u8,
+
+ pub horizontal_scroll_value: u16,
+ pub vertical_scroll_value: u16,
+ pub horizontal_scroll_value_delta: f64,
+ pub vertical_scroll_value_delta: f64,
+
+ pub character_queue: VecDeque<u8>,
+ pub modifier_state: u8,
+ pub navigation_state: u8,
+}
+
+impl InputDevice {
+ pub fn new() -> Self {
+ Self {
+ wake_flag: false,
+
+ mouse_position: ScreenPosition::ZERO,
+ mouse_button_state: 0x00,
+
+ horizontal_scroll_value: 0x0000,
+ vertical_scroll_value: 0x0000,
+ horizontal_scroll_value_delta: 0.0,
+ vertical_scroll_value_delta: 0.0,
+
+ character_queue: VecDeque::new(),
+ modifier_state: 0x00,
+ navigation_state: 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,
+ };
+ if new_button_state != self.mouse_button_state {
+ self.mouse_button_state = new_button_state;
+ 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;
+ 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.wake_flag = true;
+ }
+ while self.horizontal_scroll_value_delta < -20.0 {
+ self.horizontal_scroll_value -= 1;
+ self.horizontal_scroll_value_delta += 20.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.wake_flag = true;
+ }
+ while self.vertical_scroll_value_delta < -20.0 {
+ self.vertical_scroll_value -= 1;
+ self.vertical_scroll_value_delta += 20.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);
+ self.wake_flag = true;
+ }
+ }
+
+ pub fn on_keyboard_input(&mut self, input: phosphor::KeyboardInput) {
+ let tab = self.modifier_state & 0x40 != 0;
+ 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,
+ _ => return,
+ };
+ let new_navigation_state = match input.action {
+ phosphor::Action::Pressed => self.navigation_state | mask,
+ phosphor::Action::Released => self.navigation_state & !mask,
+ };
+ if new_navigation_state != self.navigation_state {
+ self.navigation_state = new_navigation_state;
+ 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;
+ self.wake_flag = true;
+ }
+
+ }
+}