-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.c
106 lines (83 loc) · 2.01 KB
/
keyboard.c
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
#include "keyboard.h"
/*sublime-c-static-fn-hoist-start*/
static inline bool key_is_valid(uint8_t key_code);
/*sublime-c-static-fn-hoist-end*/
static struct {
uint8_t keys_down[KEYS_DOWN_MAX];
uint8_t key_modifiers;
bool is_locked;
} self;
void keyboard_reset(void) {
memset(&self, 0, sizeof(self));
}
void key_press(uint8_t key_code) {
if (not key_is_valid(key_code)) {
return;
}
for (uint_fast8_t i = 0; i < KEYS_DOWN_MAX; i++) {
if (key_is_valid(self.keys_down[i])) {
continue;
}
if (self.keys_down[i] == key_code) {
break;
}
self.keys_down[i] = key_code;
break;
}
}
bool key_is_down(uint8_t key_code) {
if (not key_is_valid(key_code)) {
return false;
}
for (uint_fast8_t i = 0; i < KEYS_DOWN_MAX; i++) {
if (self.keys_down[i] == key_code) {
return true;
}
}
return false;
}
void key_release(uint8_t key_code) {
if (not key_is_valid(key_code)) {
return;
}
for (uint_fast8_t i = 0; i < KEYS_DOWN_MAX; i++) {
if (self.keys_down[i] == key_code) {
self.keys_down[i] = 0;
}
}
}
static inline bool key_is_valid(uint8_t key_code) {
return key_code > 3;
}
void keyboard_get_state(struct keyboard_state_s *dst) {
dst->key_count = 0;
for (uint_fast8_t i = 0; i < KEYS_DOWN_MAX; i++) {
if (key_is_valid(self.keys_down[i])) {
dst->keys[dst->key_count] = self.keys_down[i];
dst->key_count += 1;
}
}
dst->modifiers = self.key_modifiers;
}
void keyboard_set_state(struct keyboard_state_s const *src) {
memset(self.keys_down, 0, sizeof(self.keys_down));
for (uint_fast8_t i = 0; i < src->key_count; i++) {
if (key_is_valid(src->keys[i])) {
self.keys_down[i] = src->keys[i];
}
}
self.key_modifiers = src->modifiers;
}
void keyboard_set_modifiers(uint8_t modifiers) {
self.key_modifiers = modifiers;
}
bool keyboard_state_try_lock(void) {
if (self.is_locked) {
return false;
}
self.is_locked = true;
return true;
}
void keyboard_state_unlock(void) {
self.is_locked = false;
}