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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. Copyright 2011,2012 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 "keycode.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. report_keyboard_t *keyboard_report = &(report_keyboard_t){};
  24. report_mouse_t mouse_report = {};
  25. static host_driver_t *driver;
  26. static uint16_t last_system_report = 0;
  27. static uint16_t last_consumer_report = 0;
  28. static inline void add_key_byte(uint8_t code);
  29. static inline void del_key_byte(uint8_t code);
  30. #ifdef NKRO_ENABLE
  31. static inline void add_key_bit(uint8_t code);
  32. static inline void del_key_bit(uint8_t code);
  33. #endif
  34. void host_set_driver(host_driver_t *d)
  35. {
  36. driver = d;
  37. }
  38. host_driver_t *host_get_driver(void)
  39. {
  40. return driver;
  41. }
  42. uint8_t host_keyboard_leds(void)
  43. {
  44. if (!driver) return 0;
  45. return (*driver->keyboard_leds)();
  46. }
  47. /* send report */
  48. void host_keyboard_send(report_keyboard_t *report)
  49. {
  50. if (!driver) return;
  51. (*driver->send_keyboard)(report);
  52. if (debug_keyboard) {
  53. dprint("keyboard_report: ");
  54. for (uint8_t i = 0; i < REPORT_SIZE; i++) {
  55. dprintf("%02X ", keyboard_report->raw[i]);
  56. }
  57. dprint("\n");
  58. }
  59. }
  60. void host_mouse_send(report_mouse_t *report)
  61. {
  62. if (!driver) return;
  63. (*driver->send_mouse)(report);
  64. }
  65. void host_system_send(uint16_t report)
  66. {
  67. if (report == last_system_report) return;
  68. last_system_report = report;
  69. if (!driver) return;
  70. (*driver->send_system)(report);
  71. }
  72. void host_consumer_send(uint16_t report)
  73. {
  74. if (report == last_consumer_report) return;
  75. last_consumer_report = report;
  76. if (!driver) return;
  77. (*driver->send_consumer)(report);
  78. }
  79. /* keyboard report utils */
  80. void host_add_key(uint8_t key)
  81. {
  82. #ifdef NKRO_ENABLE
  83. if (keyboard_nkro) {
  84. add_key_bit(key);
  85. return;
  86. }
  87. #endif
  88. add_key_byte(key);
  89. }
  90. void host_del_key(uint8_t key)
  91. {
  92. #ifdef NKRO_ENABLE
  93. if (keyboard_nkro) {
  94. del_key_bit(key);
  95. return;
  96. }
  97. #endif
  98. del_key_byte(key);
  99. }
  100. void host_clear_keys(void)
  101. {
  102. // not clea mods
  103. for (int8_t i = 1; i < REPORT_SIZE; i++) {
  104. keyboard_report->raw[i] = 0;
  105. }
  106. }
  107. uint8_t host_get_mods(void)
  108. {
  109. return keyboard_report->mods;
  110. }
  111. void host_add_mods(uint8_t mods)
  112. {
  113. keyboard_report->mods |= mods;
  114. }
  115. void host_del_mods(uint8_t mods)
  116. {
  117. keyboard_report->mods &= ~mods;
  118. }
  119. void host_set_mods(uint8_t mods)
  120. {
  121. keyboard_report->mods = mods;
  122. }
  123. void host_clear_mods(void)
  124. {
  125. keyboard_report->mods = 0;
  126. }
  127. uint8_t host_has_anykey(void)
  128. {
  129. uint8_t cnt = 0;
  130. for (uint8_t i = 1; i < REPORT_SIZE; i++) {
  131. if (keyboard_report->raw[i])
  132. cnt++;
  133. }
  134. return cnt;
  135. }
  136. uint8_t host_has_anymod(void)
  137. {
  138. return bitpop(keyboard_report->mods);
  139. }
  140. uint8_t host_get_first_key(void)
  141. {
  142. #ifdef NKRO_ENABLE
  143. if (keyboard_nkro) {
  144. uint8_t i = 0;
  145. for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
  146. ;
  147. return i<<3 | biton(keyboard_report->nkro.bits[i]);
  148. }
  149. #endif
  150. return keyboard_report->keys[0];
  151. }
  152. void host_send_keyboard_report(void)
  153. {
  154. if (!driver) return;
  155. host_keyboard_send(keyboard_report);
  156. }
  157. uint8_t host_mouse_in_use(void)
  158. {
  159. return (mouse_report.buttons | mouse_report.x | mouse_report.y | mouse_report.v | mouse_report.h);
  160. }
  161. uint16_t host_last_sysytem_report(void)
  162. {
  163. return last_system_report;
  164. }
  165. uint16_t host_last_consumer_report(void)
  166. {
  167. return last_consumer_report;
  168. }
  169. static inline void add_key_byte(uint8_t code)
  170. {
  171. int8_t i = 0;
  172. int8_t empty = -1;
  173. for (; i < REPORT_KEYS; i++) {
  174. if (keyboard_report->keys[i] == code) {
  175. break;
  176. }
  177. if (empty == -1 && keyboard_report->keys[i] == 0) {
  178. empty = i;
  179. }
  180. }
  181. if (i == REPORT_KEYS) {
  182. if (empty != -1) {
  183. keyboard_report->keys[empty] = code;
  184. }
  185. }
  186. }
  187. static inline void del_key_byte(uint8_t code)
  188. {
  189. for (uint8_t i = 0; i < REPORT_KEYS; i++) {
  190. if (keyboard_report->keys[i] == code) {
  191. keyboard_report->keys[i] = 0;
  192. }
  193. }
  194. }
  195. #ifdef NKRO_ENABLE
  196. static inline void add_key_bit(uint8_t code)
  197. {
  198. if ((code>>3) < REPORT_BITS) {
  199. keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
  200. } else {
  201. dprintf("add_key_bit: can't add: %02X\n", code);
  202. }
  203. }
  204. static inline void del_key_bit(uint8_t code)
  205. {
  206. if ((code>>3) < REPORT_BITS) {
  207. keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
  208. } else {
  209. dprintf("del_key_bit: can't del: %02X\n", code);
  210. }
  211. }
  212. #endif