Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

host.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <avr/interrupt.h>
  16. #include "usb_keycodes.h"
  17. #include "host.h"
  18. #include "util.h"
  19. #include "debug.h"
  20. #ifdef NKRO_ENABLE
  21. bool keyboard_nkro = false;
  22. #endif
  23. static host_driver_t *driver;
  24. static report_keyboard_t report0;
  25. static report_keyboard_t report1;
  26. report_keyboard_t *keyboard_report = &report0;
  27. report_keyboard_t *keyboard_report_prev = &report1;
  28. static inline void add_key_byte(uint8_t code);
  29. static inline void del_key_byte(uint8_t code);
  30. static inline void add_key_bit(uint8_t code);
  31. static inline void del_key_bit(uint8_t code);
  32. void host_set_driver(host_driver_t *d)
  33. {
  34. driver = d;
  35. }
  36. host_driver_t *host_get_driver(void)
  37. {
  38. return driver;
  39. }
  40. uint8_t host_keyboard_leds(void)
  41. {
  42. if (!driver) return 0;
  43. return (*driver->keyboard_leds)();
  44. }
  45. /* keyboard report operations */
  46. void host_add_key(uint8_t key)
  47. {
  48. #ifdef NKRO_ENABLE
  49. if (keyboard_nkro) {
  50. add_key_bit(key);
  51. return;
  52. }
  53. #endif
  54. add_key_byte(key);
  55. }
  56. void host_del_key(uint8_t key)
  57. {
  58. #ifdef NKRO_ENABLE
  59. if (keyboard_nkro) {
  60. del_key_bit(key);
  61. return;
  62. }
  63. #endif
  64. del_key_byte(key);
  65. }
  66. void host_add_mod_bit(uint8_t mod)
  67. {
  68. keyboard_report->mods |= mod;
  69. }
  70. void host_del_mod_bit(uint8_t mod)
  71. {
  72. keyboard_report->mods &= ~mod;
  73. }
  74. void host_set_mods(uint8_t mods)
  75. {
  76. keyboard_report->mods = mods;
  77. }
  78. void host_add_code(uint8_t code)
  79. {
  80. if (IS_MOD(code)) {
  81. host_add_mod_bit(MOD_BIT(code));
  82. } else {
  83. host_add_key(code);
  84. }
  85. }
  86. void host_del_code(uint8_t code)
  87. {
  88. if (IS_MOD(code)) {
  89. host_del_mod_bit(MOD_BIT(code));
  90. } else {
  91. host_del_key(code);
  92. }
  93. }
  94. void host_swap_keyboard_report(void)
  95. {
  96. uint8_t sreg = SREG;
  97. cli();
  98. report_keyboard_t *tmp = keyboard_report_prev;
  99. keyboard_report_prev = keyboard_report;
  100. keyboard_report = tmp;
  101. SREG = sreg;
  102. }
  103. void host_clear_keyboard_report(void)
  104. {
  105. keyboard_report->mods = 0;
  106. for (int8_t i = 0; i < REPORT_KEYS; i++) {
  107. keyboard_report->keys[i] = 0;
  108. }
  109. }
  110. uint8_t host_has_anykey(void)
  111. {
  112. uint8_t cnt = 0;
  113. for (int i = 0; i < REPORT_KEYS; i++) {
  114. if (keyboard_report->keys[i])
  115. cnt++;
  116. }
  117. return cnt;
  118. }
  119. uint8_t host_get_first_key(void)
  120. {
  121. #ifdef NKRO_ENABLE
  122. if (keyboard_nkro) {
  123. uint8_t i = 0;
  124. for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
  125. ;
  126. return i<<3 | biton(keyboard_report->keys[i]);
  127. }
  128. #endif
  129. return keyboard_report->keys[0];
  130. }
  131. void host_send_keyboard_report(void)
  132. {
  133. if (!driver) return;
  134. (*driver->send_keyboard)(keyboard_report);
  135. }
  136. void host_mouse_send(report_mouse_t *report)
  137. {
  138. if (!driver) return;
  139. (*driver->send_mouse)(report);
  140. }
  141. void host_system_send(uint16_t data)
  142. {
  143. static uint16_t last_data = 0;
  144. if (data == last_data) return;
  145. last_data = data;
  146. if (!driver) return;
  147. (*driver->send_system)(data);
  148. }
  149. void host_consumer_send(uint16_t data)
  150. {
  151. static uint16_t last_data = 0;
  152. if (data == last_data) return;
  153. last_data = data;
  154. if (!driver) return;
  155. (*driver->send_consumer)(data);
  156. }
  157. static inline void add_key_byte(uint8_t code)
  158. {
  159. // TODO: fix ugly code
  160. int8_t i = 0;
  161. int8_t empty = -1;
  162. for (; i < REPORT_KEYS; i++) {
  163. if (keyboard_report_prev->keys[i] == code) {
  164. keyboard_report->keys[i] = code;
  165. break;
  166. }
  167. if (empty == -1 &&
  168. keyboard_report_prev->keys[i] == 0 &&
  169. keyboard_report->keys[i] == 0) {
  170. empty = i;
  171. }
  172. }
  173. if (i == REPORT_KEYS) {
  174. if (empty != -1) {
  175. keyboard_report->keys[empty] = code;
  176. }
  177. }
  178. }
  179. static inline void del_key_byte(uint8_t code)
  180. {
  181. int i = 0;
  182. for (; i < REPORT_KEYS; i++) {
  183. if (keyboard_report->keys[i] == code) {
  184. keyboard_report->keys[i] = 0;
  185. break;
  186. }
  187. }
  188. }
  189. static inline void add_key_bit(uint8_t code)
  190. {
  191. if ((code>>3) < REPORT_KEYS) {
  192. keyboard_report->keys[code>>3] |= 1<<(code&7);
  193. } else {
  194. debug("add_key_bit: can't add: "); phex(code); debug("\n");
  195. }
  196. }
  197. static inline void del_key_bit(uint8_t code)
  198. {
  199. if ((code>>3) < REPORT_KEYS) {
  200. keyboard_report->keys[code>>3] &= ~(1<<(code&7));
  201. } else {
  202. debug("del_key_bit: can't del: "); phex(code); debug("\n");
  203. }
  204. }