]> git.gir.st - tmk_keyboard.git/blob - common/host.c
Add keycode.h and remove usb_keycodes.h.
[tmk_keyboard.git] / common / host.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdint.h>
19 #include <avr/interrupt.h>
20 #include "keycode.h"
21 #include "host.h"
22 #include "util.h"
23 #include "debug.h"
24
25
26 #ifdef NKRO_ENABLE
27 bool keyboard_nkro = false;
28 #endif
29
30 static host_driver_t *driver;
31 static report_keyboard_t report0;
32 static report_keyboard_t report1;
33 report_keyboard_t *keyboard_report = &report0;
34 report_keyboard_t *keyboard_report_prev = &report1;
35
36
37 static inline void add_key_byte(uint8_t code);
38 static inline void del_key_byte(uint8_t code);
39 static inline void add_key_bit(uint8_t code);
40 static inline void del_key_bit(uint8_t code);
41
42
43 void host_set_driver(host_driver_t *d)
44 {
45 driver = d;
46 }
47
48 host_driver_t *host_get_driver(void)
49 {
50 return driver;
51 }
52
53 uint8_t host_keyboard_leds(void)
54 {
55 if (!driver) return 0;
56 return (*driver->keyboard_leds)();
57 }
58
59 /* new interface */
60 void host_register_key(uint8_t key)
61 {
62 host_add_key(key);
63 host_send_keyboard_report();
64 }
65
66 void host_unregister_key(uint8_t key)
67 {
68 host_del_key(key);
69 host_send_keyboard_report();
70 }
71
72 void host_clear_all_keys_but_mods(void)
73 {
74 for (int8_t i = 0; i < REPORT_KEYS; i++) {
75 keyboard_report->keys[i] = 0;
76 }
77 host_send_keyboard_report();
78 }
79
80 /* keyboard report operations */
81 void host_add_key(uint8_t key)
82 {
83 #ifdef NKRO_ENABLE
84 if (keyboard_nkro) {
85 add_key_bit(key);
86 return;
87 }
88 #endif
89 add_key_byte(key);
90 }
91
92 void host_del_key(uint8_t key)
93 {
94 #ifdef NKRO_ENABLE
95 if (keyboard_nkro) {
96 del_key_bit(key);
97 return;
98 }
99 #endif
100 del_key_byte(key);
101 }
102
103 void host_add_mod_bit(uint8_t mod)
104 {
105 keyboard_report->mods |= mod;
106 }
107
108 void host_del_mod_bit(uint8_t mod)
109 {
110 keyboard_report->mods &= ~mod;
111 }
112
113 void host_set_mods(uint8_t mods)
114 {
115 keyboard_report->mods = mods;
116 }
117
118 void host_add_code(uint8_t code)
119 {
120 if (IS_MOD(code)) {
121 host_add_mod_bit(MOD_BIT(code));
122 } else {
123 host_add_key(code);
124 }
125 }
126
127 void host_del_code(uint8_t code)
128 {
129 if (IS_MOD(code)) {
130 host_del_mod_bit(MOD_BIT(code));
131 } else {
132 host_del_key(code);
133 }
134 }
135
136 void host_swap_keyboard_report(void)
137 {
138 uint8_t sreg = SREG;
139 cli();
140 report_keyboard_t *tmp = keyboard_report_prev;
141 keyboard_report_prev = keyboard_report;
142 keyboard_report = tmp;
143 SREG = sreg;
144 }
145
146 void host_clear_keyboard_report(void)
147 {
148 keyboard_report->mods = 0;
149 for (int8_t i = 0; i < REPORT_KEYS; i++) {
150 keyboard_report->keys[i] = 0;
151 }
152 }
153
154 uint8_t host_has_anykey(void)
155 {
156 uint8_t cnt = 0;
157 for (int i = 0; i < REPORT_KEYS; i++) {
158 if (keyboard_report->keys[i])
159 cnt++;
160 }
161 return cnt;
162 }
163
164 uint8_t host_get_first_key(void)
165 {
166 #ifdef NKRO_ENABLE
167 if (keyboard_nkro) {
168 uint8_t i = 0;
169 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
170 ;
171 return i<<3 | biton(keyboard_report->keys[i]);
172 }
173 #endif
174 return keyboard_report->keys[0];
175 }
176
177
178 void host_send_keyboard_report(void)
179 {
180 if (!driver) return;
181 (*driver->send_keyboard)(keyboard_report);
182
183 if (debug_keyboard) {
184 print("keys: ");
185 for (int i = 0; i < REPORT_KEYS; i++) {
186 phex(keyboard_report->keys[i]); print(" ");
187 }
188 print(" mods: "); phex(keyboard_report->mods); print("\n");
189 }
190 }
191
192 void host_mouse_send(report_mouse_t *report)
193 {
194 if (!driver) return;
195 (*driver->send_mouse)(report);
196 }
197
198 void host_system_send(uint16_t data)
199 {
200 static uint16_t last_data = 0;
201 if (data == last_data) return;
202 last_data = data;
203
204 if (!driver) return;
205 (*driver->send_system)(data);
206 }
207
208 void host_consumer_send(uint16_t data)
209 {
210 static uint16_t last_data = 0;
211 if (data == last_data) return;
212 last_data = data;
213
214 if (!driver) return;
215 (*driver->send_consumer)(data);
216 }
217
218
219 static inline void add_key_byte(uint8_t code)
220 {
221 // TODO: fix ugly code
222 int8_t i = 0;
223 int8_t empty = -1;
224 for (; i < REPORT_KEYS; i++) {
225 if (keyboard_report_prev->keys[i] == code) {
226 keyboard_report->keys[i] = code;
227 break;
228 }
229 if (empty == -1 &&
230 keyboard_report_prev->keys[i] == 0 &&
231 keyboard_report->keys[i] == 0) {
232 empty = i;
233 }
234 }
235 if (i == REPORT_KEYS) {
236 if (empty != -1) {
237 keyboard_report->keys[empty] = code;
238 }
239 }
240 }
241
242 static inline void del_key_byte(uint8_t code)
243 {
244 int i = 0;
245 for (; i < REPORT_KEYS; i++) {
246 if (keyboard_report->keys[i] == code) {
247 keyboard_report->keys[i] = 0;
248 }
249 }
250 }
251
252 static inline void add_key_bit(uint8_t code)
253 {
254 if ((code>>3) < REPORT_KEYS) {
255 keyboard_report->keys[code>>3] |= 1<<(code&7);
256 } else {
257 debug("add_key_bit: can't add: "); phex(code); debug("\n");
258 }
259 }
260
261 static inline void del_key_bit(uint8_t code)
262 {
263 if ((code>>3) < REPORT_KEYS) {
264 keyboard_report->keys[code>>3] &= ~(1<<(code&7));
265 } else {
266 debug("del_key_bit: can't del: "); phex(code); debug("\n");
267 }
268 }
Imprint / Impressum