diff options
| author | tmk <nobody@nowhere> | 2012-10-09 16:50:14 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2012-10-17 15:55:37 +0900 |
| commit | 71ac82337f803e8ec0c081b3221ac0ccf61035b0 (patch) | |
| tree | 5692c88c0b16d7f4992321c19a82f5193ebe4dc2 /common/host.c | |
| parent | 373ab0e7192811944786c095facb80938c33f1d5 (diff) | |
| download | qmk_firmware-71ac82337f803e8ec0c081b3221ac0ccf61035b0.tar.gz qmk_firmware-71ac82337f803e8ec0c081b3221ac0ccf61035b0.zip | |
Clean host.h interface.
Diffstat (limited to 'common/host.c')
| -rw-r--r-- | common/host.c | 85 |
1 files changed, 20 insertions, 65 deletions
diff --git a/common/host.c b/common/host.c index 37f707d0b..a671c97d3 100644 --- a/common/host.c +++ b/common/host.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | Copyright 2011 Jun Wako <wakojun@gmail.com> | 2 | Copyright 2011,2012 Jun Wako <wakojun@gmail.com> |
| 3 | 3 | ||
| 4 | This program is free software: you can redistribute it and/or modify | 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 | 5 | it under the terms of the GNU General Public License as published by |
| @@ -28,10 +28,7 @@ bool keyboard_nkro = false; | |||
| 28 | #endif | 28 | #endif |
| 29 | 29 | ||
| 30 | static host_driver_t *driver; | 30 | static host_driver_t *driver; |
| 31 | static report_keyboard_t report0; | 31 | report_keyboard_t *keyboard_report = &(report_keyboard_t){}; |
| 32 | static report_keyboard_t report1; | ||
| 33 | report_keyboard_t *keyboard_report = &report0; | ||
| 34 | report_keyboard_t *keyboard_report_prev = &report1; | ||
| 35 | 32 | ||
| 36 | 33 | ||
| 37 | static inline void add_key_byte(uint8_t code); | 34 | static inline void add_key_byte(uint8_t code); |
| @@ -56,27 +53,6 @@ uint8_t host_keyboard_leds(void) | |||
| 56 | return (*driver->keyboard_leds)(); | 53 | return (*driver->keyboard_leds)(); |
| 57 | } | 54 | } |
| 58 | 55 | ||
| 59 | /* new interface */ | ||
| 60 | void host_register_key(uint8_t key) | ||
| 61 | { | ||
| 62 | host_add_key(key); | ||
| 63 | host_send_keyboard_report(); | ||
| 64 | } | ||
| 65 | |||
| 66 | void host_unregister_key(uint8_t key) | ||
| 67 | { | ||
| 68 | host_del_key(key); | ||
| 69 | host_send_keyboard_report(); | ||
| 70 | } | ||
| 71 | |||
| 72 | void host_clear_all_keys_but_mods(void) | ||
| 73 | { | ||
| 74 | for (int8_t i = 0; i < REPORT_KEYS; i++) { | ||
| 75 | keyboard_report->keys[i] = 0; | ||
| 76 | } | ||
| 77 | host_send_keyboard_report(); | ||
| 78 | } | ||
| 79 | |||
| 80 | /* keyboard report operations */ | 56 | /* keyboard report operations */ |
| 81 | void host_add_key(uint8_t key) | 57 | void host_add_key(uint8_t key) |
| 82 | { | 58 | { |
| @@ -100,6 +76,13 @@ void host_del_key(uint8_t key) | |||
| 100 | del_key_byte(key); | 76 | del_key_byte(key); |
| 101 | } | 77 | } |
| 102 | 78 | ||
| 79 | void host_clear_keys(void) | ||
| 80 | { | ||
| 81 | for (int8_t i = 0; i < REPORT_KEYS; i++) { | ||
| 82 | keyboard_report->keys[i] = 0; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 103 | void host_add_mod_bit(uint8_t mod) | 86 | void host_add_mod_bit(uint8_t mod) |
| 104 | { | 87 | { |
| 105 | keyboard_report->mods |= mod; | 88 | keyboard_report->mods |= mod; |
| @@ -115,40 +98,9 @@ void host_set_mods(uint8_t mods) | |||
| 115 | keyboard_report->mods = mods; | 98 | keyboard_report->mods = mods; |
| 116 | } | 99 | } |
| 117 | 100 | ||
| 118 | void host_add_code(uint8_t code) | 101 | void host_clear_mods(void) |
| 119 | { | ||
| 120 | if (IS_MOD(code)) { | ||
| 121 | host_add_mod_bit(MOD_BIT(code)); | ||
| 122 | } else { | ||
| 123 | host_add_key(code); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | void host_del_code(uint8_t code) | ||
| 128 | { | ||
| 129 | if (IS_MOD(code)) { | ||
| 130 | host_del_mod_bit(MOD_BIT(code)); | ||
| 131 | } else { | ||
| 132 | host_del_key(code); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | void host_swap_keyboard_report(void) | ||
| 137 | { | ||
| 138 | uint8_t sreg = SREG; | ||
| 139 | cli(); | ||
| 140 | report_keyboard_t *tmp = keyboard_report_prev; | ||
| 141 | keyboard_report_prev = keyboard_report; | ||
| 142 | keyboard_report = tmp; | ||
| 143 | SREG = sreg; | ||
| 144 | } | ||
| 145 | |||
| 146 | void host_clear_keyboard_report(void) | ||
| 147 | { | 102 | { |
| 148 | keyboard_report->mods = 0; | 103 | keyboard_report->mods = 0; |
| 149 | for (int8_t i = 0; i < REPORT_KEYS; i++) { | ||
| 150 | keyboard_report->keys[i] = 0; | ||
| 151 | } | ||
| 152 | } | 104 | } |
| 153 | 105 | ||
| 154 | uint8_t host_has_anykey(void) | 106 | uint8_t host_has_anykey(void) |
| @@ -174,7 +126,6 @@ uint8_t host_get_first_key(void) | |||
| 174 | return keyboard_report->keys[0]; | 126 | return keyboard_report->keys[0]; |
| 175 | } | 127 | } |
| 176 | 128 | ||
| 177 | |||
| 178 | void host_send_keyboard_report(void) | 129 | void host_send_keyboard_report(void) |
| 179 | { | 130 | { |
| 180 | if (!driver) return; | 131 | if (!driver) return; |
| @@ -189,6 +140,14 @@ void host_send_keyboard_report(void) | |||
| 189 | } | 140 | } |
| 190 | } | 141 | } |
| 191 | 142 | ||
| 143 | |||
| 144 | /* send report */ | ||
| 145 | void host_keyboard_send(report_keyboard_t *report) | ||
| 146 | { | ||
| 147 | if (!driver) return; | ||
| 148 | (*driver->send_keyboard)(report); | ||
| 149 | } | ||
| 150 | |||
| 192 | void host_mouse_send(report_mouse_t *report) | 151 | void host_mouse_send(report_mouse_t *report) |
| 193 | { | 152 | { |
| 194 | if (!driver) return; | 153 | if (!driver) return; |
| @@ -218,17 +177,13 @@ void host_consumer_send(uint16_t data) | |||
| 218 | 177 | ||
| 219 | static inline void add_key_byte(uint8_t code) | 178 | static inline void add_key_byte(uint8_t code) |
| 220 | { | 179 | { |
| 221 | // TODO: fix ugly code | ||
| 222 | int8_t i = 0; | 180 | int8_t i = 0; |
| 223 | int8_t empty = -1; | 181 | int8_t empty = -1; |
| 224 | for (; i < REPORT_KEYS; i++) { | 182 | for (; i < REPORT_KEYS; i++) { |
| 225 | if (keyboard_report_prev->keys[i] == code) { | 183 | if (keyboard_report->keys[i] == code) { |
| 226 | keyboard_report->keys[i] = code; | ||
| 227 | break; | 184 | break; |
| 228 | } | 185 | } |
| 229 | if (empty == -1 && | 186 | if (empty == -1 && keyboard_report->keys[i] == 0) { |
| 230 | keyboard_report_prev->keys[i] == 0 && | ||
| 231 | keyboard_report->keys[i] == 0) { | ||
| 232 | empty = i; | 187 | empty = i; |
| 233 | } | 188 | } |
| 234 | } | 189 | } |
