2 Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
19 #include <avr/interrupt.h>
27 bool keyboard_nkro = false;
30 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
31 report_mouse_t mouse_report = {};
34 static host_driver_t *driver;
35 static uint16_t last_system_report = 0;
36 static uint16_t last_consumer_report = 0;
38 static inline void add_key_byte(uint8_t code);
39 static inline void del_key_byte(uint8_t code);
41 static inline void add_key_bit(uint8_t code);
42 static inline void del_key_bit(uint8_t code);
46 void host_set_driver(host_driver_t *d)
51 host_driver_t *host_get_driver(void)
56 uint8_t host_keyboard_leds(void)
58 if (!driver) return 0;
59 return (*driver->keyboard_leds)();
62 void host_keyboard_send(report_keyboard_t *report)
65 (*driver->send_keyboard)(report);
68 dprint("keyboard_report: ");
69 for (uint8_t i = 0; i < REPORT_SIZE; i++) {
70 dprintf("%02X ", keyboard_report->raw[i]);
76 void host_mouse_send(report_mouse_t *report)
79 (*driver->send_mouse)(report);
82 void host_system_send(uint16_t report)
84 if (report == last_system_report) return;
85 last_system_report = report;
88 (*driver->send_system)(report);
91 void host_consumer_send(uint16_t report)
93 if (report == last_consumer_report) return;
94 last_consumer_report = report;
97 (*driver->send_consumer)(report);
102 /* keyboard report utils */
103 void host_add_key(uint8_t key)
114 void host_del_key(uint8_t key)
125 void host_clear_keys(void)
128 for (int8_t i = 1; i < REPORT_SIZE; i++) {
129 keyboard_report->raw[i] = 0;
133 uint8_t host_get_mods(void)
135 return keyboard_report->mods;
138 void host_add_mods(uint8_t mods)
140 keyboard_report->mods |= mods;
143 void host_del_mods(uint8_t mods)
145 keyboard_report->mods &= ~mods;
148 void host_set_mods(uint8_t mods)
150 keyboard_report->mods = mods;
153 void host_clear_mods(void)
155 keyboard_report->mods = 0;
158 uint8_t host_has_anykey(void)
161 for (uint8_t i = 1; i < REPORT_SIZE; i++) {
162 if (keyboard_report->raw[i])
168 uint8_t host_has_anymod(void)
170 return bitpop(keyboard_report->mods);
173 uint8_t host_get_first_key(void)
178 for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
180 return i<<3 | biton(keyboard_report->nkro.bits[i]);
183 return keyboard_report->keys[0];
186 void host_send_keyboard_report(void)
189 host_keyboard_send(keyboard_report);
192 uint8_t host_mouse_in_use(void)
194 return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h);
197 uint16_t host_last_sysytem_report(void)
199 return last_system_report;
202 uint16_t host_last_consumer_report(void)
204 return last_consumer_report;
207 static inline void add_key_byte(uint8_t code)
211 for (; i < REPORT_KEYS; i++) {
212 if (keyboard_report->keys[i] == code) {
215 if (empty == -1 && keyboard_report->keys[i] == 0) {
219 if (i == REPORT_KEYS) {
221 keyboard_report->keys[empty] = code;
226 static inline void del_key_byte(uint8_t code)
228 for (uint8_t i = 0; i < REPORT_KEYS; i++) {
229 if (keyboard_report->keys[i] == code) {
230 keyboard_report->keys[i] = 0;
236 static inline void add_key_bit(uint8_t code)
238 if ((code>>3) < REPORT_BITS) {
239 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
241 dprintf("add_key_bit: can't add: %02X\n", code);
245 static inline void del_key_bit(uint8_t code)
247 if ((code>>3) < REPORT_BITS) {
248 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
250 dprintf("del_key_bit: can't del: %02X\n", code);