diff options
Diffstat (limited to 'common/host.c')
| -rw-r--r-- | common/host.c | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/common/host.c b/common/host.c new file mode 100644 index 000000000..cc26d55c2 --- /dev/null +++ b/common/host.c | |||
| @@ -0,0 +1,237 @@ | |||
| 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 "usb_keycodes.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 | /* keyboard report operations */ | ||
| 60 | void host_add_key(uint8_t key) | ||
| 61 | { | ||
| 62 | #ifdef NKRO_ENABLE | ||
| 63 | if (keyboard_nkro) { | ||
| 64 | add_key_bit(key); | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | #endif | ||
| 68 | add_key_byte(key); | ||
| 69 | } | ||
| 70 | |||
| 71 | void host_del_key(uint8_t key) | ||
| 72 | { | ||
| 73 | #ifdef NKRO_ENABLE | ||
| 74 | if (keyboard_nkro) { | ||
| 75 | del_key_bit(key); | ||
| 76 | return; | ||
| 77 | } | ||
| 78 | #endif | ||
| 79 | del_key_byte(key); | ||
| 80 | } | ||
| 81 | |||
| 82 | void host_add_mod_bit(uint8_t mod) | ||
| 83 | { | ||
| 84 | keyboard_report->mods |= mod; | ||
| 85 | } | ||
| 86 | |||
| 87 | void host_del_mod_bit(uint8_t mod) | ||
| 88 | { | ||
| 89 | keyboard_report->mods &= ~mod; | ||
| 90 | } | ||
| 91 | |||
| 92 | void host_set_mods(uint8_t mods) | ||
| 93 | { | ||
| 94 | keyboard_report->mods = mods; | ||
| 95 | } | ||
| 96 | |||
| 97 | void host_add_code(uint8_t code) | ||
| 98 | { | ||
| 99 | if (IS_MOD(code)) { | ||
| 100 | host_add_mod_bit(MOD_BIT(code)); | ||
| 101 | } else { | ||
| 102 | host_add_key(code); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | void host_del_code(uint8_t code) | ||
| 107 | { | ||
| 108 | if (IS_MOD(code)) { | ||
| 109 | host_del_mod_bit(MOD_BIT(code)); | ||
| 110 | } else { | ||
| 111 | host_del_key(code); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | void host_swap_keyboard_report(void) | ||
| 116 | { | ||
| 117 | uint8_t sreg = SREG; | ||
| 118 | cli(); | ||
| 119 | report_keyboard_t *tmp = keyboard_report_prev; | ||
| 120 | keyboard_report_prev = keyboard_report; | ||
| 121 | keyboard_report = tmp; | ||
| 122 | SREG = sreg; | ||
| 123 | } | ||
| 124 | |||
| 125 | void host_clear_keyboard_report(void) | ||
| 126 | { | ||
| 127 | keyboard_report->mods = 0; | ||
| 128 | for (int8_t i = 0; i < REPORT_KEYS; i++) { | ||
| 129 | keyboard_report->keys[i] = 0; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | uint8_t host_has_anykey(void) | ||
| 134 | { | ||
| 135 | uint8_t cnt = 0; | ||
| 136 | for (int i = 0; i < REPORT_KEYS; i++) { | ||
| 137 | if (keyboard_report->keys[i]) | ||
| 138 | cnt++; | ||
| 139 | } | ||
| 140 | return cnt; | ||
| 141 | } | ||
| 142 | |||
| 143 | uint8_t host_get_first_key(void) | ||
| 144 | { | ||
| 145 | #ifdef NKRO_ENABLE | ||
| 146 | if (keyboard_nkro) { | ||
| 147 | uint8_t i = 0; | ||
| 148 | for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++) | ||
| 149 | ; | ||
| 150 | return i<<3 | biton(keyboard_report->keys[i]); | ||
| 151 | } | ||
| 152 | #endif | ||
| 153 | return keyboard_report->keys[0]; | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | void host_send_keyboard_report(void) | ||
| 158 | { | ||
| 159 | if (!driver) return; | ||
| 160 | (*driver->send_keyboard)(keyboard_report); | ||
| 161 | } | ||
| 162 | |||
| 163 | void host_mouse_send(report_mouse_t *report) | ||
| 164 | { | ||
| 165 | if (!driver) return; | ||
| 166 | (*driver->send_mouse)(report); | ||
| 167 | } | ||
| 168 | |||
| 169 | void host_system_send(uint16_t data) | ||
| 170 | { | ||
| 171 | if (!driver) return; | ||
| 172 | (*driver->send_system)(data); | ||
| 173 | } | ||
| 174 | |||
| 175 | void host_consumer_send(uint16_t data) | ||
| 176 | { | ||
| 177 | // TODO: this is needed? | ||
| 178 | static uint16_t last_data = 0; | ||
| 179 | if (data == last_data) return; | ||
| 180 | last_data = data; | ||
| 181 | |||
| 182 | if (!driver) return; | ||
| 183 | (*driver->send_consumer)(data); | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | static inline void add_key_byte(uint8_t code) | ||
| 188 | { | ||
| 189 | // TODO: fix ugly code | ||
| 190 | int8_t i = 0; | ||
| 191 | int8_t empty = -1; | ||
| 192 | for (; i < REPORT_KEYS; i++) { | ||
| 193 | if (keyboard_report_prev->keys[i] == code) { | ||
| 194 | keyboard_report->keys[i] = code; | ||
| 195 | break; | ||
| 196 | } | ||
| 197 | if (empty == -1 && | ||
| 198 | keyboard_report_prev->keys[i] == 0 && | ||
| 199 | keyboard_report->keys[i] == 0) { | ||
| 200 | empty = i; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | if (i == REPORT_KEYS) { | ||
| 204 | if (empty != -1) { | ||
| 205 | keyboard_report->keys[empty] = code; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | static inline void del_key_byte(uint8_t code) | ||
| 211 | { | ||
| 212 | int i = 0; | ||
| 213 | for (; i < REPORT_KEYS; i++) { | ||
| 214 | if (keyboard_report->keys[i] == code) { | ||
| 215 | keyboard_report->keys[i] = 0; | ||
| 216 | break; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | static inline void add_key_bit(uint8_t code) | ||
| 222 | { | ||
| 223 | if ((code>>3) < REPORT_KEYS) { | ||
| 224 | keyboard_report->keys[code>>3] |= 1<<(code&7); | ||
| 225 | } else { | ||
| 226 | debug("add_key_bit: can't add: "); phex(code); debug("\n"); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | static inline void del_key_bit(uint8_t code) | ||
| 231 | { | ||
| 232 | if ((code>>3) < REPORT_KEYS) { | ||
| 233 | keyboard_report->keys[code>>3] &= ~(1<<(code&7)); | ||
| 234 | } else { | ||
| 235 | debug("del_key_bit: can't del: "); phex(code); debug("\n"); | ||
| 236 | } | ||
| 237 | } | ||
