2 Copyright 2011 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>
20 #include "usb_keycodes.h"
21 #include "usb_keyboard.h"
22 #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
23 #include "usb_mouse.h"
25 #ifdef EXTRAKEY_ENABLE
26 #include "usb_extra.h"
34 bool keyboard_nkro = false;
37 static report_keyboard_t report0;
38 static report_keyboard_t report1;
39 report_keyboard_t *keyboard_report = &report0;
40 report_keyboard_t *keyboard_report_prev = &report1;
42 static inline void add_key_byte(uint8_t code);
43 static inline void add_key_bit(uint8_t code);
46 uint8_t host_keyboard_leds(void)
48 return usb_keyboard_leds;
51 /* keyboard report operations */
52 void host_add_key(uint8_t key)
63 void host_add_mod_bit(uint8_t mod)
65 keyboard_report->mods |= mod;
68 void host_set_mods(uint8_t mods)
70 keyboard_report->mods = mods;
73 void host_add_code(uint8_t code)
76 host_add_mod_bit(MOD_BIT(code));
82 void host_swap_keyboard_report(void)
86 report_keyboard_t *tmp = keyboard_report_prev;
87 keyboard_report_prev = keyboard_report;
88 keyboard_report = tmp;
92 void host_clear_keyboard_report(void)
94 keyboard_report->mods = 0;
95 for (int8_t i = 0; i < REPORT_KEYS; i++) {
96 keyboard_report->keys[i] = 0;
100 uint8_t host_has_anykey(void)
103 for (int i = 0; i < REPORT_KEYS; i++) {
104 if (keyboard_report->keys[i])
110 uint8_t host_get_first_key(void)
115 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
117 return i<<3 | biton(keyboard_report->keys[i]);
120 return keyboard_report->keys[0];
124 void host_send_keyboard_report(void)
126 usb_keyboard_send_report(keyboard_report);
129 #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
130 void host_mouse_send(report_mouse_t *report)
132 usb_mouse_send(report->x, report->y, report->v, report->h, report->buttons);
136 #ifdef EXTRAKEY_ENABLE
137 void host_system_send(uint16_t data)
139 usb_extra_system_send(data);
142 void host_consumer_send(uint16_t data)
144 static uint16_t last_data = 0;
145 if (data == last_data) return;
148 usb_extra_consumer_send(data);
153 static inline void add_key_byte(uint8_t code)
155 // TODO: fix ugly code
158 for (; i < REPORT_KEYS; i++) {
159 if (keyboard_report_prev->keys[i] == code) {
160 keyboard_report->keys[i] = code;
164 keyboard_report_prev->keys[i] == 0 &&
165 keyboard_report->keys[i] == 0) {
169 if (i == REPORT_KEYS) {
171 keyboard_report->keys[empty] = code;
176 static inline void add_key_bit(uint8_t code)
178 if ((code>>3) < REPORT_KEYS) {
179 keyboard_report->keys[code>>3] |= 1<<(code&7);
181 debug("add_key_bit: can't add: "); phex(code); debug("\n");