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);
40 static inline void add_key_bit(uint8_t code);
41 static inline void del_key_bit(uint8_t code);
44 void host_set_driver(host_driver_t *d)
49 host_driver_t *host_get_driver(void)
54 uint8_t host_keyboard_leds(void)
56 if (!driver) return 0;
57 return (*driver->keyboard_leds)();
60 void host_keyboard_send(report_keyboard_t *report)
63 (*driver->send_keyboard)(report);
67 for (int i = 0; i < REPORT_KEYS; i++) {
68 phex(keyboard_report->keys[i]); print(" ");
70 print(" mods: "); phex(keyboard_report->mods); print("\n");
74 void host_mouse_send(report_mouse_t *report)
77 (*driver->send_mouse)(report);
80 void host_system_send(uint16_t report)
82 if (report == last_system_report) return;
83 last_system_report = report;
86 (*driver->send_system)(report);
89 void host_consumer_send(uint16_t report)
91 if (report == last_consumer_report) return;
92 last_consumer_report = report;
95 (*driver->send_consumer)(report);
100 /* keyboard report utils */
101 void host_add_key(uint8_t key)
112 void host_del_key(uint8_t key)
123 void host_clear_keys(void)
125 for (int8_t i = 0; i < REPORT_KEYS; i++) {
126 keyboard_report->keys[i] = 0;
130 void host_add_mod_bit(uint8_t mod)
132 keyboard_report->mods |= mod;
135 void host_del_mod_bit(uint8_t mod)
137 keyboard_report->mods &= ~mod;
140 void host_set_mods(uint8_t mods)
142 keyboard_report->mods = mods;
145 void host_clear_mods(void)
147 keyboard_report->mods = 0;
150 uint8_t host_has_anykey(void)
153 for (int i = 0; i < REPORT_KEYS; i++) {
154 if (keyboard_report->keys[i])
160 uint8_t host_has_anymod(void)
162 return bitpop(keyboard_report->mods);
165 uint8_t host_get_first_key(void)
170 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
172 return i<<3 | biton(keyboard_report->keys[i]);
175 return keyboard_report->keys[0];
178 void host_send_keyboard_report(void)
181 host_keyboard_send(keyboard_report);
184 uint8_t host_mouse_in_use(void)
186 return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h);
189 uint16_t host_last_sysytem_report(void)
191 return last_system_report;
194 uint16_t host_last_consumer_report(void)
196 return last_consumer_report;
199 static inline void add_key_byte(uint8_t code)
203 for (; i < REPORT_KEYS; i++) {
204 if (keyboard_report->keys[i] == code) {
207 if (empty == -1 && keyboard_report->keys[i] == 0) {
211 if (i == REPORT_KEYS) {
213 keyboard_report->keys[empty] = code;
218 static inline void del_key_byte(uint8_t code)
221 for (; i < REPORT_KEYS; i++) {
222 if (keyboard_report->keys[i] == code) {
223 keyboard_report->keys[i] = 0;
228 static inline void add_key_bit(uint8_t code)
230 if ((code>>3) < REPORT_KEYS) {
231 keyboard_report->keys[code>>3] |= 1<<(code&7);
233 debug("add_key_bit: can't add: "); phex(code); debug("\n");
237 static inline void del_key_bit(uint8_t code)
239 if ((code>>3) < REPORT_KEYS) {
240 keyboard_report->keys[code>>3] &= ~(1<<(code&7));
242 debug("del_key_bit: can't del: "); phex(code); debug("\n");