report.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 Checks if a key is pressed in the report
  68. *
  69. * Returns true if the keyboard_report reports that the key is pressed, otherwise false
  70. * Note: The function doesn't support modifers currently, and it returns false for KC_NO
  71. */
  72. bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key) {
  73. if (key == KC_NO) {
  74. return false;
  75. }
  76. #ifdef NKRO_ENABLE
  77. if (keyboard_protocol && keymap_config.nkro) {
  78. if ((key >> 3) < KEYBOARD_REPORT_BITS) {
  79. return keyboard_report->nkro.bits[key >> 3] & 1 << (key & 7);
  80. } else {
  81. return false;
  82. }
  83. }
  84. #endif
  85. for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  86. if (keyboard_report->keys[i] == key) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. /** \brief add key byte
  93. *
  94. * FIXME: Needs doc
  95. */
  96. void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) {
  97. #ifdef USB_6KRO_ENABLE
  98. int8_t i = cb_head;
  99. int8_t empty = -1;
  100. if (cb_count) {
  101. do {
  102. if (keyboard_report->keys[i] == code) {
  103. return;
  104. }
  105. if (empty == -1 && keyboard_report->keys[i] == 0) {
  106. empty = i;
  107. }
  108. i = RO_INC(i);
  109. } while (i != cb_tail);
  110. if (i == cb_tail) {
  111. if (cb_tail == cb_head) {
  112. // buffer is full
  113. if (empty == -1) {
  114. // pop head when has no empty space
  115. cb_head = RO_INC(cb_head);
  116. cb_count--;
  117. } else {
  118. // left shift when has empty space
  119. uint8_t offset = 1;
  120. i = RO_INC(empty);
  121. do {
  122. if (keyboard_report->keys[i] != 0) {
  123. keyboard_report->keys[empty] = keyboard_report->keys[i];
  124. keyboard_report->keys[i] = 0;
  125. empty = RO_INC(empty);
  126. } else {
  127. offset++;
  128. }
  129. i = RO_INC(i);
  130. } while (i != cb_tail);
  131. cb_tail = RO_SUB(cb_tail, offset);
  132. }
  133. }
  134. }
  135. }
  136. // add to tail
  137. keyboard_report->keys[cb_tail] = code;
  138. cb_tail = RO_INC(cb_tail);
  139. cb_count++;
  140. #else
  141. int8_t i = 0;
  142. int8_t empty = -1;
  143. for (; i < KEYBOARD_REPORT_KEYS; i++) {
  144. if (keyboard_report->keys[i] == code) {
  145. break;
  146. }
  147. if (empty == -1 && keyboard_report->keys[i] == 0) {
  148. empty = i;
  149. }
  150. }
  151. if (i == KEYBOARD_REPORT_KEYS) {
  152. if (empty != -1) {
  153. keyboard_report->keys[empty] = code;
  154. }
  155. }
  156. #endif
  157. }
  158. /** \brief del key byte
  159. *
  160. * FIXME: Needs doc
  161. */
  162. void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) {
  163. #ifdef USB_6KRO_ENABLE
  164. uint8_t i = cb_head;
  165. if (cb_count) {
  166. do {
  167. if (keyboard_report->keys[i] == code) {
  168. keyboard_report->keys[i] = 0;
  169. cb_count--;
  170. if (cb_count == 0) {
  171. // reset head and tail
  172. cb_tail = cb_head = 0;
  173. }
  174. if (i == RO_DEC(cb_tail)) {
  175. // left shift when next to tail
  176. do {
  177. cb_tail = RO_DEC(cb_tail);
  178. if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
  179. break;
  180. }
  181. } while (cb_tail != cb_head);
  182. }
  183. break;
  184. }
  185. i = RO_INC(i);
  186. } while (i != cb_tail);
  187. }
  188. #else
  189. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  190. if (keyboard_report->keys[i] == code) {
  191. keyboard_report->keys[i] = 0;
  192. }
  193. }
  194. #endif
  195. }
  196. #ifdef NKRO_ENABLE
  197. /** \brief add key bit
  198. *
  199. * FIXME: Needs doc
  200. */
  201. void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code) {
  202. if ((code >> 3) < KEYBOARD_REPORT_BITS) {
  203. keyboard_report->nkro.bits[code >> 3] |= 1 << (code & 7);
  204. } else {
  205. dprintf("add_key_bit: can't add: %02X\n", code);
  206. }
  207. }
  208. /** \brief del key bit
  209. *
  210. * FIXME: Needs doc
  211. */
  212. void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) {
  213. if ((code >> 3) < KEYBOARD_REPORT_BITS) {
  214. keyboard_report->nkro.bits[code >> 3] &= ~(1 << (code & 7));
  215. } else {
  216. dprintf("del_key_bit: can't del: %02X\n", code);
  217. }
  218. }
  219. #endif
  220. /** \brief add key to report
  221. *
  222. * FIXME: Needs doc
  223. */
  224. void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) {
  225. #ifdef NKRO_ENABLE
  226. if (keyboard_protocol && keymap_config.nkro) {
  227. add_key_bit(keyboard_report, key);
  228. return;
  229. }
  230. #endif
  231. add_key_byte(keyboard_report, key);
  232. }
  233. /** \brief del key from report
  234. *
  235. * FIXME: Needs doc
  236. */
  237. void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) {
  238. #ifdef NKRO_ENABLE
  239. if (keyboard_protocol && keymap_config.nkro) {
  240. del_key_bit(keyboard_report, key);
  241. return;
  242. }
  243. #endif
  244. del_key_byte(keyboard_report, key);
  245. }
  246. /** \brief clear key from report
  247. *
  248. * FIXME: Needs doc
  249. */
  250. void clear_keys_from_report(report_keyboard_t* keyboard_report) {
  251. // not clear mods
  252. #ifdef NKRO_ENABLE
  253. if (keyboard_protocol && keymap_config.nkro) {
  254. memset(keyboard_report->nkro.bits, 0, sizeof(keyboard_report->nkro.bits));
  255. return;
  256. }
  257. #endif
  258. memset(keyboard_report->keys, 0, sizeof(keyboard_report->keys));
  259. }