summaryrefslogtreecommitdiff
path: root/src/devices/input.rs
blob: f3191dd8cfb9a511be6752dec9c708732145436a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;
        }

    }
}