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"
27 bool keyboard_nkro
= false;
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
;
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
);
43 void host_set_driver(host_driver_t
*d
)
48 host_driver_t
*host_get_driver(void)
53 uint8_t host_keyboard_leds(void)
55 if (!driver
) return 0;
56 return (*driver
->keyboard_leds
)();
60 void host_register_key(uint8_t key
)
63 host_send_keyboard_report();
66 void host_unregister_key(uint8_t key
)
69 host_send_keyboard_report();
72 void host_clear_all_keys_but_mods(void)
74 for (int8_t i
= 0; i
< REPORT_KEYS
; i
++) {
75 keyboard_report
->keys
[i
] = 0;
77 host_send_keyboard_report();
80 /* keyboard report operations */
81 void host_add_key(uint8_t key
)
92 void host_del_key(uint8_t key
)
103 void host_add_mod_bit(uint8_t mod
)
105 keyboard_report
->mods
|= mod
;
108 void host_del_mod_bit(uint8_t mod
)
110 keyboard_report
->mods
&= ~mod
;
113 void host_set_mods(uint8_t mods
)
115 keyboard_report
->mods
= mods
;
118 void host_add_code(uint8_t code
)
121 host_add_mod_bit(MOD_BIT(code
));
127 void host_del_code(uint8_t code
)
130 host_del_mod_bit(MOD_BIT(code
));
136 void host_swap_keyboard_report(void)
140 report_keyboard_t
*tmp
= keyboard_report_prev
;
141 keyboard_report_prev
= keyboard_report
;
142 keyboard_report
= tmp
;
146 void host_clear_keyboard_report(void)
148 keyboard_report
->mods
= 0;
149 for (int8_t i
= 0; i
< REPORT_KEYS
; i
++) {
150 keyboard_report
->keys
[i
] = 0;
154 uint8_t host_has_anykey(void)
157 for (int i
= 0; i
< REPORT_KEYS
; i
++) {
158 if (keyboard_report
->keys
[i
])
164 uint8_t host_get_first_key(void)
169 for (; i
< REPORT_KEYS
&& !keyboard_report
->keys
[i
]; i
++)
171 return i
<<3 | biton(keyboard_report
->keys
[i
]);
174 return keyboard_report
->keys
[0];
178 void host_send_keyboard_report(void)
181 (*driver
->send_keyboard
)(keyboard_report
);
183 if (debug_keyboard
) {
185 for (int i
= 0; i
< REPORT_KEYS
; i
++) {
186 phex(keyboard_report
->keys
[i
]); print(" ");
188 print(" mods: "); phex(keyboard_report
->mods
); print("\n");
192 void host_mouse_send(report_mouse_t
*report
)
195 (*driver
->send_mouse
)(report
);
198 void host_system_send(uint16_t data
)
200 static uint16_t last_data
= 0;
201 if (data
== last_data
) return;
205 (*driver
->send_system
)(data
);
208 void host_consumer_send(uint16_t data
)
210 static uint16_t last_data
= 0;
211 if (data
== last_data
) return;
215 (*driver
->send_consumer
)(data
);
219 static inline void add_key_byte(uint8_t code
)
221 // TODO: fix ugly code
224 for (; i
< REPORT_KEYS
; i
++) {
225 if (keyboard_report_prev
->keys
[i
] == code
) {
226 keyboard_report
->keys
[i
] = code
;
230 keyboard_report_prev
->keys
[i
] == 0 &&
231 keyboard_report
->keys
[i
] == 0) {
235 if (i
== REPORT_KEYS
) {
237 keyboard_report
->keys
[empty
] = code
;
242 static inline void del_key_byte(uint8_t code
)
245 for (; i
< REPORT_KEYS
; i
++) {
246 if (keyboard_report
->keys
[i
] == code
) {
247 keyboard_report
->keys
[i
] = 0;
252 static inline void add_key_bit(uint8_t code
)
254 if ((code
>>3) < REPORT_KEYS
) {
255 keyboard_report
->keys
[code
>>3] |= 1<<(code
&7);
257 debug("add_key_bit: can't add: "); phex(code
); debug("\n");
261 static inline void del_key_bit(uint8_t code
)
263 if ((code
>>3) < REPORT_KEYS
) {
264 keyboard_report
->keys
[code
>>3] &= ~(1<<(code
&7));
266 debug("del_key_bit: can't del: "); phex(code
); debug("\n");