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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 add_key_bit(uint8_t code);
  30. void host_set_driver(host_driver_t *d)
  31. {
  32. driver = d;
  33. }
  34. host_driver_t *host_get_driver(void)
  35. {
  36. return driver;
  37. }
  38. uint8_t host_keyboard_leds(void)
  39. {
  40. if (!driver) return 0;
  41. return (*driver->keyboard_leds)();
  42. }
  43. /* keyboard report operations */
  44. void host_add_key(uint8_t key)
  45. {
  46. #ifdef NKRO_ENABLE
  47. if (keyboard_nkro) {
  48. add_key_bit(key);
  49. return;
  50. }
  51. #endif
  52. add_key_byte(key);
  53. }
  54. void host_add_mod_bit(uint8_t mod)
  55. {
  56. keyboard_report->mods |= mod;
  57. }
  58. void host_set_mods(uint8_t mods)
  59. {
  60. keyboard_report->mods = mods;
  61. }
  62. void host_add_code(uint8_t code)
  63. {
  64. if (IS_MOD(code)) {
  65. host_add_mod_bit(MOD_BIT(code));
  66. } else {
  67. host_add_key(code);
  68. }
  69. }
  70. void host_swap_keyboard_report(void)
  71. {
  72. uint8_t sreg = SREG;
  73. cli();
  74. report_keyboard_t *tmp = keyboard_report_prev;
  75. keyboard_report_prev = keyboard_report;
  76. keyboard_report = tmp;
  77. SREG = sreg;
  78. }
  79. void host_clear_keyboard_report(void)
  80. {
  81. keyboard_report->mods = 0;
  82. for (int8_t i = 0; i < REPORT_KEYS; i++) {
  83. keyboard_report->keys[i] = 0;
  84. }
  85. }
  86. uint8_t host_has_anykey(void)
  87. {
  88. uint8_t cnt = 0;
  89. for (int i = 0; i < REPORT_KEYS; i++) {
  90. if (keyboard_report->keys[i])
  91. cnt++;
  92. }
  93. return cnt;
  94. }
  95. uint8_t host_get_first_key(void)
  96. {
  97. #ifdef NKRO_ENABLE
  98. if (keyboard_nkro) {
  99. uint8_t i = 0;
  100. for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
  101. ;
  102. return i<<3 | biton(keyboard_report->keys[i]);
  103. }
  104. #endif
  105. return keyboard_report->keys[0];
  106. }
  107. void host_send_keyboard_report(void)
  108. {
  109. if (!driver) return;
  110. (*driver->send_keyboard)(keyboard_report);
  111. }
  112. void host_mouse_send(report_mouse_t *report)
  113. {
  114. if (!driver) return;
  115. (*driver->send_mouse)(report);
  116. }
  117. void host_system_send(uint16_t data)
  118. {
  119. if (!driver) return;
  120. (*driver->send_consumer)(data);
  121. }
  122. void host_consumer_send(uint16_t data)
  123. {
  124. // TODO: this is needed?
  125. static uint16_t last_data = 0;
  126. if (data == last_data) return;
  127. last_data = data;
  128. if (!driver) return;
  129. (*driver->send_consumer)(data);
  130. }
  131. static inline void add_key_byte(uint8_t code)
  132. {
  133. // TODO: fix ugly code
  134. int8_t i = 0;
  135. int8_t empty = -1;
  136. for (; i < REPORT_KEYS; i++) {
  137. if (keyboard_report_prev->keys[i] == code) {
  138. keyboard_report->keys[i] = code;
  139. break;
  140. }
  141. if (empty == -1 &&
  142. keyboard_report_prev->keys[i] == 0 &&
  143. keyboard_report->keys[i] == 0) {
  144. empty = i;
  145. }
  146. }
  147. if (i == REPORT_KEYS) {
  148. if (empty != -1) {
  149. keyboard_report->keys[empty] = code;
  150. }
  151. }
  152. }
  153. static inline void add_key_bit(uint8_t code)
  154. {
  155. if ((code>>3) < REPORT_KEYS) {
  156. keyboard_report->keys[code>>3] |= 1<<(code&7);
  157. } else {
  158. debug("add_key_bit: can't add: "); phex(code); debug("\n");
  159. }
  160. }