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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use alloc::collections::VecDeque;
use crate::types::ReadableEvent;
use crate::keyboard::*;
use crate::error::Error;
use crate::syscalls;
use crate::futures::WorkQueue;
#[derive(Debug)]
pub struct InnerKeyboard {
ipc_session: StaticServiceProxy,
keys_queue: VecDeque<HidKeyboardState>
}
#[derive(Debug)]
pub struct Keyboard {
inner: InnerKeyboard,
readable_event: ReadableEvent,
}
impl Keyboard {
pub fn new() -> Result<Self, Error> {
let ipc_session = StaticServiceProxy::raw_new()?;
let readable_event = ReadableEvent(ipc_session.get_keyboard_event()?);
Ok(Keyboard {
readable_event,
inner: InnerKeyboard {
ipc_session,
keys_queue: VecDeque::new()
}
})
}
pub fn read_key(&mut self) -> char {
if let Some(key) = self.try_read_key() {
return key;
}
loop {
let handle = self.readable_event.0.as_ref();
syscalls::wait_synchronization(&[handle], None).expect("wait_synchronization returned an error");
self.readable_event.clear().expect("Cannot clear readable event");
let res = self.try_read_key();
if let Some(res) = res {
return res;
}
}
}
pub fn read_keystate_async<'a>(&'a mut self, queue: WorkQueue<'_>) -> impl core::future::Future<Output = HidKeyboardState> + Unpin + 'a {
let Keyboard { ref mut inner, ref mut readable_event } = self;
readable_event.wait_async_cb(queue, move || {
inner.try_read_keystate()
})
}
pub fn read_key_async<'a>(&'a mut self, queue: WorkQueue<'_>) -> impl core::future::Future<Output = char> + Unpin + 'a {
let Keyboard { ref mut inner, ref mut readable_event } = self;
readable_event.wait_async_cb(queue, move || {
inner.try_read_key()
})
}
pub fn try_read_key(&mut self) -> Option<char> {
self.inner.try_read_key()
}
pub fn try_read_keystate(&mut self) -> Option<HidKeyboardState> {
self.inner.try_read_keystate()
}
}
impl InnerKeyboard {
pub fn update_keys(&mut self) {
loop {
let mut states = [HidKeyboardState {
data: 0,
additional_data: 0,
state_type: HidKeyboardStateType::Unknown,
modifiers: 0
}; 0x80];
match self.ipc_session.read_keyboard_states(&mut states) {
Ok(count) => {
let states = &states[..count as usize];
for state in states {
self.keys_queue.push_back(*state);
}
},
_ => break
}
}
}
fn try_read_cached_key(&mut self) -> Option<char> {
loop {
match self.keys_queue.pop_front() {
Some(state) => {
if let HidKeyboardStateType::Ascii = state.state_type {
let lower_case = char::from(state.data);
let upper_case = char::from(state.additional_data);
let is_upper = state.modifiers & 1 == 1 || state.modifiers & (1 << 1) == (1 << 1) || state.modifiers & (1 << 2) == (1 << 2);
let is_pressed = (state.modifiers & (1 << 7)) == (1 << 7);
if is_pressed {
if is_upper {
return Some(upper_case)
} else {
return Some(lower_case)
}
}
}
},
_ => return None
}
}
}
pub fn try_read_key(&mut self) -> Option<char> {
match self.try_read_cached_key() {
None => {
self.update_keys();
self.try_read_cached_key()
}
res => res
}
}
pub fn try_read_keystate(&mut self) -> Option<HidKeyboardState> {
match self.keys_queue.pop_front() {
None => {
self.update_keys();
self.keys_queue.pop_front()
}
res => res
}
}
}