report.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. uint8_t cnt = 0;
  28. uint8_t* p = keyboard_report->keys;
  29. uint8_t lp = sizeof(keyboard_report->keys);
  30. #ifdef NKRO_ENABLE
  31. if (keyboard_protocol && keymap_config.nkro) {
  32. p = keyboard_report->nkro.bits;
  33. lp = sizeof(keyboard_report->nkro.bits);
  34. }
  35. #endif
  36. while (lp--) {
  37. if (*p++) cnt++;
  38. }
  39. return cnt;
  40. }
  41. /** \brief get_first_key
  42. *
  43. * FIXME: Needs doc
  44. */
  45. uint8_t get_first_key(report_keyboard_t* keyboard_report) {
  46. #ifdef NKRO_ENABLE
  47. if (keyboard_protocol && keymap_config.nkro) {
  48. uint8_t i = 0;
  49. for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
  50. ;
  51. return i << 3 | biton(keyboard_report->nkro.bits[i]);
  52. }
  53. #endif
  54. #ifdef USB_6KRO_ENABLE
  55. uint8_t i = cb_head;
  56. do {
  57. if (keyboard_report->keys[i] != 0) {
  58. break;
  59. }
  60. i = RO_INC(i);
  61. } while (i != cb_tail);
  62. return keyboard_report->keys[i];
  63. #else
  64. return keyboard_report->keys[0];
  65. #endif
  66. }
  67. /** \brief add key byte
  68. *
  69. * FIXME: Needs doc
  70. */
  71. void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) {
  72. #ifdef USB_6KRO_ENABLE
  73. int8_t i = cb_head;
  74. int8_t empty = -1;
  75. if (cb_count) {
  76. do {
  77. if (keyboard_report->keys[i] == code) {
  78. return;
  79. }
  80. if (empty == -1 && keyboard_report->keys[i] == 0) {
  81. empty = i;
  82. }
  83. i = RO_INC(i);
  84. } while (i != cb_tail);
  85. if (i == cb_tail) {
  86. if (cb_tail == cb_head) {
  87. // buffer is full
  88. if (empty == -1) {
  89. // pop head when has no empty space
  90. cb_head = RO_INC(cb_head);
  91. cb_count--;
  92. } else {
  93. // left shift when has empty space
  94. uint8_t offset = 1;
  95. i = RO_INC(empty);
  96. do {
  97. if (keyboard_report->keys[i] != 0) {
  98. keyboard_report->keys[empty] = keyboard_report->keys[i];
  99. keyboard_report->keys[i] = 0;
  100. empty = RO_INC(empty);
  101. } else {
  102. offset++;
  103. }
  104. i = RO_INC(i);
  105. } while (i != cb_tail);
  106. cb_tail = RO_SUB(cb_tail, offset);
  107. }
  108. }
  109. }
  110. }
  111. // add to tail
  112. keyboard_report->keys[cb_tail] = code;
  113. cb_tail = RO_INC(cb_tail);
  114. cb_count++;
  115. #else
  116. int8_t i = 0;
  117. int8_t empty = -1;
  118. for (; i < KEYBOARD_REPORT_KEYS; i++) {
  119. if (keyboard_report->keys[i] == code) {
  120. break;
  121. }
  122. if (empty == -1 && keyboard_report->keys[i] == 0) {
  123. empty = i;
  124. }
  125. }
  126. if (i == KEYBOARD_REPORT_KEYS) {
  127. if (empty != -1) {
  128. keyboard_report->keys[empty] = code;
  129. }
  130. }
  131. #endif
  132. }
  133. /** \brief del key byte
  134. *
  135. * FIXME: Needs doc
  136. */
  137. void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) {
  138. #ifdef USB_6KRO_ENABLE
  139. uint8_t i = cb_head;
  140. if (cb_count) {
  141. do {
  142. if (keyboard_report->keys[i] == code) {
  143. keyboard_report->keys[i] = 0;
  144. cb_count--;
  145. if (cb_count == 0) {
  146. // reset head and tail
  147. cb_tail = cb_head = 0;
  148. }
  149. if (i == RO_DEC(cb_tail)) {
  150. // left shift when next to tail
  151. do {
  152. cb_tail = RO_DEC(cb_tail);
  153. if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
  154. break;
  155. }
  156. } while (cb_tail != cb_head);
  157. }
  158. break;
  159. }
  160. i = RO_INC(i);
  161. } while (i != cb_tail);
  162. }
  163. #else
  164. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  165. if (keyboard_report->keys[i] == code) {
  166. keyboard_report->keys[i] = 0;
  167. }
  168. }
  169. #endif
  170. }
  171. #ifdef NKRO_ENABLE
  172. /** \brief add key bit
  173. *
  174. * FIXME: Needs doc
  175. */
  176. void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code) {
  177. if ((code >> 3) < KEYBOARD_REPORT_BITS) {
  178. keyboard_report->nkro.bits[code >> 3] |= 1 << (code & 7);
  179. } else {
  180. dprintf("add_key_bit: can't add: %02X\n", code);
  181. }
  182. }
  183. /** \brief del key bit
  184. *
  185. * FIXME: Needs doc
  186. */
  187. void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) {
  188. if ((code >> 3) < KEYBOARD_REPORT_BITS) {
  189. keyboard_report->nkro.bits[code >> 3] &= ~(1 << (code & 7));
  190. } else {
  191. dprintf("del_key_bit: can't del: %02X\n", code);
  192. }
  193. }
  194. #endif
  195. /** \brief add key to report
  196. *
  197. * FIXME: Needs doc
  198. */
  199. void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) {
  200. #ifdef NKRO_ENABLE
  201. if (keyboard_protocol && keymap_config.nkro) {
  202. add_key_bit(keyboard_report, key);
  203. return;
  204. }
  205. #endif
  206. add_key_byte(keyboard_report, key);
  207. }
  208. /** \brief del key from report
  209. *
  210. * FIXME: Needs doc
  211. */
  212. void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) {
  213. #ifdef NKRO_ENABLE
  214. if (keyboard_protocol && keymap_config.nkro) {
  215. del_key_bit(keyboard_report, key);
  216. return;
  217. }
  218. #endif
  219. del_key_byte(keyboard_report, key);
  220. }
  221. /** \brief clear key from report
  222. *
  223. * FIXME: Needs doc
  224. */
  225. void clear_keys_from_report(report_keyboard_t* keyboard_report) {
  226. // not clear mods
  227. #ifdef NKRO_ENABLE
  228. if (keyboard_protocol && keymap_config.nkro) {
  229. memset(keyboard_report->nkro.bits, 0, sizeof(keyboard_report->nkro.bits));
  230. return;
  231. }
  232. #endif
  233. memset(keyboard_report->keys, 0, sizeof(keyboard_report->keys));
  234. }