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.

action_util.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright 2013 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 "host.h"
  15. #include "report.h"
  16. #include "debug.h"
  17. #include "action_util.h"
  18. #include "timer.h"
  19. static inline void add_key_byte(uint8_t code);
  20. static inline void del_key_byte(uint8_t code);
  21. #ifdef NKRO_ENABLE
  22. static inline void add_key_bit(uint8_t code);
  23. static inline void del_key_bit(uint8_t code);
  24. #endif
  25. static uint8_t real_mods = 0;
  26. static uint8_t weak_mods = 0;
  27. // TODO: pointer variable is not needed
  28. //report_keyboard_t keyboard_report = {};
  29. report_keyboard_t *keyboard_report = &(report_keyboard_t){};
  30. #ifndef NO_ACTION_ONESHOT
  31. static int8_t oneshot_mods = 0;
  32. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  33. static int16_t oneshot_time = 0;
  34. #endif
  35. #endif
  36. void send_keyboard_report(void) {
  37. keyboard_report->mods = real_mods;
  38. keyboard_report->mods |= weak_mods;
  39. #ifndef NO_ACTION_ONESHOT
  40. if (oneshot_mods) {
  41. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  42. if (TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT) {
  43. dprintf("Oneshot: timeout\n");
  44. clear_oneshot_mods();
  45. }
  46. #endif
  47. keyboard_report->mods |= oneshot_mods;
  48. if (has_anykey()) {
  49. clear_oneshot_mods();
  50. }
  51. }
  52. #endif
  53. host_keyboard_send(keyboard_report);
  54. }
  55. /* key */
  56. void add_key(uint8_t key)
  57. {
  58. #ifdef NKRO_ENABLE
  59. if (keyboard_nkro) {
  60. add_key_bit(key);
  61. return;
  62. }
  63. #endif
  64. add_key_byte(key);
  65. }
  66. void del_key(uint8_t key)
  67. {
  68. #ifdef NKRO_ENABLE
  69. if (keyboard_nkro) {
  70. del_key_bit(key);
  71. return;
  72. }
  73. #endif
  74. del_key_byte(key);
  75. }
  76. void clear_keys(void)
  77. {
  78. // not clear mods
  79. for (int8_t i = 1; i < REPORT_SIZE; i++) {
  80. keyboard_report->raw[i] = 0;
  81. }
  82. }
  83. /* modifier */
  84. uint8_t get_mods(void) { return real_mods; }
  85. void add_mods(uint8_t mods) { real_mods |= mods; }
  86. void del_mods(uint8_t mods) { real_mods &= ~mods; }
  87. void set_mods(uint8_t mods) { real_mods = mods; }
  88. void clear_mods(void) { real_mods = 0; }
  89. /* weak modifier */
  90. uint8_t get_weak_mods(void) { return weak_mods; }
  91. void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
  92. void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
  93. void set_weak_mods(uint8_t mods) { weak_mods = mods; }
  94. void clear_weak_mods(void) { weak_mods = 0; }
  95. /* Oneshot modifier */
  96. #ifndef NO_ACTION_ONESHOT
  97. void set_oneshot_mods(uint8_t mods)
  98. {
  99. oneshot_mods = mods;
  100. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  101. oneshot_time = timer_read();
  102. #endif
  103. }
  104. void clear_oneshot_mods(void)
  105. {
  106. oneshot_mods = 0;
  107. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  108. oneshot_time = 0;
  109. #endif
  110. }
  111. #endif
  112. /*
  113. * inspect keyboard state
  114. */
  115. uint8_t has_anykey(void)
  116. {
  117. uint8_t cnt = 0;
  118. for (uint8_t i = 1; i < REPORT_SIZE; i++) {
  119. if (keyboard_report->raw[i])
  120. cnt++;
  121. }
  122. return cnt;
  123. }
  124. uint8_t has_anymod(void)
  125. {
  126. return bitpop(real_mods);
  127. }
  128. uint8_t get_first_key(void)
  129. {
  130. #ifdef NKRO_ENABLE
  131. if (keyboard_nkro) {
  132. uint8_t i = 0;
  133. for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
  134. ;
  135. return i<<3 | biton(keyboard_report->nkro.bits[i]);
  136. }
  137. #endif
  138. return keyboard_report->keys[0];
  139. }
  140. /* local functions */
  141. static inline void add_key_byte(uint8_t code)
  142. {
  143. int8_t i = 0;
  144. int8_t empty = -1;
  145. for (; i < REPORT_KEYS; i++) {
  146. if (keyboard_report->keys[i] == code) {
  147. break;
  148. }
  149. if (empty == -1 && keyboard_report->keys[i] == 0) {
  150. empty = i;
  151. }
  152. }
  153. if (i == REPORT_KEYS) {
  154. if (empty != -1) {
  155. keyboard_report->keys[empty] = code;
  156. }
  157. }
  158. }
  159. static inline void del_key_byte(uint8_t code)
  160. {
  161. for (uint8_t i = 0; i < REPORT_KEYS; i++) {
  162. if (keyboard_report->keys[i] == code) {
  163. keyboard_report->keys[i] = 0;
  164. }
  165. }
  166. }
  167. #ifdef NKRO_ENABLE
  168. static inline void add_key_bit(uint8_t code)
  169. {
  170. if ((code>>3) < REPORT_BITS) {
  171. keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
  172. } else {
  173. dprintf("add_key_bit: can't add: %02X\n", code);
  174. }
  175. }
  176. static inline void del_key_bit(uint8_t code)
  177. {
  178. if ((code>>3) < REPORT_BITS) {
  179. keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
  180. } else {
  181. dprintf("del_key_bit: can't del: %02X\n", code);
  182. }
  183. }
  184. #endif