report.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Copyright 2017 Fred Sundvik
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "report.h"
  17. #include "host.h"
  18. #include "keycode_config.h"
  19. #include "debug.h"
  20. #include "util.h"
  21. #include <string.h>
  22. /** \brief has_anykey
  23. *
  24. * FIXME: Needs doc
  25. */
  26. uint8_t has_anykey(report_keyboard_t* keyboard_report)
  27. {
  28. uint8_t cnt = 0;
  29. uint8_t *p = keyboard_report->keys;
  30. uint8_t lp = sizeof(keyboard_report->keys);
  31. #ifdef NKRO_ENABLE
  32. if (keyboard_protocol && keymap_config.nkro) {
  33. p = keyboard_report->nkro.bits;
  34. lp = sizeof(keyboard_report->nkro.bits);
  35. }
  36. #endif
  37. while (lp--) {
  38. if (*p++)
  39. cnt++;
  40. }
  41. return cnt;
  42. }
  43. /** \brief get_first_key
  44. *
  45. * FIXME: Needs doc
  46. */
  47. uint8_t get_first_key(report_keyboard_t* keyboard_report)
  48. {
  49. #ifdef NKRO_ENABLE
  50. if (keyboard_protocol && keymap_config.nkro) {
  51. uint8_t i = 0;
  52. for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
  53. ;
  54. return i<<3 | biton(keyboard_report->nkro.bits[i]);
  55. }
  56. #endif
  57. #ifdef USB_6KRO_ENABLE
  58. uint8_t i = cb_head;
  59. do {
  60. if (keyboard_report->keys[i] != 0) {
  61. break;
  62. }
  63. i = RO_INC(i);
  64. } while (i != cb_tail);
  65. return keyboard_report->keys[i];
  66. #else
  67. return keyboard_report->keys[0];
  68. #endif
  69. }
  70. /** \brief add key byte
  71. *
  72. * FIXME: Needs doc
  73. */
  74. void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code)
  75. {
  76. #ifdef USB_6KRO_ENABLE
  77. int8_t i = cb_head;
  78. int8_t empty = -1;
  79. if (cb_count) {
  80. do {
  81. if (keyboard_report->keys[i] == code) {
  82. return;
  83. }
  84. if (empty == -1 && keyboard_report->keys[i] == 0) {
  85. empty = i;
  86. }
  87. i = RO_INC(i);
  88. } while (i != cb_tail);
  89. if (i == cb_tail) {
  90. if (cb_tail == cb_head) {
  91. // buffer is full
  92. if (empty == -1) {
  93. // pop head when has no empty space
  94. cb_head = RO_INC(cb_head);
  95. cb_count--;
  96. }
  97. else {
  98. // left shift when has empty space
  99. uint8_t offset = 1;
  100. i = RO_INC(empty);
  101. do {
  102. if (keyboard_report->keys[i] != 0) {
  103. keyboard_report->keys[empty] = keyboard_report->keys[i];
  104. keyboard_report->keys[i] = 0;
  105. empty = RO_INC(empty);
  106. }
  107. else {
  108. offset++;
  109. }
  110. i = RO_INC(i);
  111. } while (i != cb_tail);
  112. cb_tail = RO_SUB(cb_tail, offset);
  113. }
  114. }
  115. }
  116. }
  117. // add to tail
  118. keyboard_report->keys[cb_tail] = code;
  119. cb_tail = RO_INC(cb_tail);
  120. cb_count++;
  121. #else
  122. int8_t i = 0;
  123. int8_t empty = -1;
  124. for (; i < KEYBOARD_REPORT_KEYS; i++) {
  125. if (keyboard_report->keys[i] == code) {
  126. break;
  127. }
  128. if (empty == -1 && keyboard_report->keys[i] == 0) {
  129. empty = i;
  130. }
  131. }
  132. if (i == KEYBOARD_REPORT_KEYS) {
  133. if (empty != -1) {
  134. keyboard_report->keys[empty] = code;
  135. }
  136. }
  137. #endif
  138. }
  139. /** \brief del key byte
  140. *
  141. * FIXME: Needs doc
  142. */
  143. void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code)
  144. {
  145. #ifdef USB_6KRO_ENABLE
  146. uint8_t i = cb_head;
  147. if (cb_count) {
  148. do {
  149. if (keyboard_report->keys[i] == code) {
  150. keyboard_report->keys[i] = 0;
  151. cb_count--;
  152. if (cb_count == 0) {
  153. // reset head and tail
  154. cb_tail = cb_head = 0;
  155. }
  156. if (i == RO_DEC(cb_tail)) {
  157. // left shift when next to tail
  158. do {
  159. cb_tail = RO_DEC(cb_tail);
  160. if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
  161. break;
  162. }
  163. } while (cb_tail != cb_head);
  164. }
  165. break;
  166. }
  167. i = RO_INC(i);
  168. } while (i != cb_tail);
  169. }
  170. #else
  171. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  172. if (keyboard_report->keys[i] == code) {
  173. keyboard_report->keys[i] = 0;
  174. }
  175. }
  176. #endif
  177. }
  178. #ifdef NKRO_ENABLE
  179. /** \brief add key bit
  180. *
  181. * FIXME: Needs doc
  182. */
  183. void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code)
  184. {
  185. if ((code>>3) < KEYBOARD_REPORT_BITS) {
  186. keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
  187. } else {
  188. dprintf("add_key_bit: can't add: %02X\n", code);
  189. }
  190. }
  191. /** \brief del key bit
  192. *
  193. * FIXME: Needs doc
  194. */
  195. void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code)
  196. {
  197. if ((code>>3) < KEYBOARD_REPORT_BITS) {
  198. keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
  199. } else {
  200. dprintf("del_key_bit: can't del: %02X\n", code);
  201. }
  202. }
  203. #endif
  204. /** \brief add key to report
  205. *
  206. * FIXME: Needs doc
  207. */
  208. void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key)
  209. {
  210. #ifdef NKRO_ENABLE
  211. if (keyboard_protocol && keymap_config.nkro) {
  212. add_key_bit(keyboard_report, key);
  213. return;
  214. }
  215. #endif
  216. add_key_byte(keyboard_report, key);
  217. }
  218. /** \brief del key from report
  219. *
  220. * FIXME: Needs doc
  221. */
  222. void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key)
  223. {
  224. #ifdef NKRO_ENABLE
  225. if (keyboard_protocol && keymap_config.nkro) {
  226. del_key_bit(keyboard_report, key);
  227. return;
  228. }
  229. #endif
  230. del_key_byte(keyboard_report, key);
  231. }
  232. /** \brief clear key from report
  233. *
  234. * FIXME: Needs doc
  235. */
  236. void clear_keys_from_report(report_keyboard_t* keyboard_report)
  237. {
  238. // not clear mods
  239. #ifdef NKRO_ENABLE
  240. if (keyboard_protocol && keymap_config.nkro) {
  241. memset(keyboard_report->nkro.bits, 0, sizeof(keyboard_report->nkro.bits));
  242. return;
  243. }
  244. #endif
  245. memset(keyboard_report->keys, 0, sizeof(keyboard_report->keys));
  246. }