Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

action_util.c 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #ifdef USB_6KRO_ENABLE
  28. #define RO_ADD(a, b) ((a + b) % REPORT_KEYS)
  29. #define RO_SUB(a, b) ((a - b + REPORT_KEYS) % REPORT_KEYS)
  30. #define RO_INC(a) RO_ADD(a, 1)
  31. #define RO_DEC(a) RO_SUB(a, 1)
  32. static int8_t cb_head = 0;
  33. static int8_t cb_tail = 0;
  34. static int8_t cb_count = 0;
  35. #endif
  36. // TODO: pointer variable is not needed
  37. //report_keyboard_t keyboard_report = {};
  38. report_keyboard_t *keyboard_report = &(report_keyboard_t){};
  39. #ifndef NO_ACTION_ONESHOT
  40. static int8_t oneshot_mods = 0;
  41. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  42. static int16_t oneshot_time = 0;
  43. #endif
  44. #endif
  45. void send_keyboard_report(void) {
  46. keyboard_report->mods = real_mods;
  47. keyboard_report->mods |= weak_mods;
  48. #ifndef NO_ACTION_ONESHOT
  49. if (oneshot_mods) {
  50. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  51. if (TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT) {
  52. dprintf("Oneshot: timeout\n");
  53. clear_oneshot_mods();
  54. }
  55. #endif
  56. keyboard_report->mods |= oneshot_mods;
  57. if (has_anykey()) {
  58. clear_oneshot_mods();
  59. }
  60. }
  61. #endif
  62. host_keyboard_send(keyboard_report);
  63. }
  64. /* key */
  65. void add_key(uint8_t key)
  66. {
  67. #ifdef NKRO_ENABLE
  68. if (keyboard_nkro) {
  69. add_key_bit(key);
  70. return;
  71. }
  72. #endif
  73. add_key_byte(key);
  74. }
  75. void del_key(uint8_t key)
  76. {
  77. #ifdef NKRO_ENABLE
  78. if (keyboard_nkro) {
  79. del_key_bit(key);
  80. return;
  81. }
  82. #endif
  83. del_key_byte(key);
  84. }
  85. void clear_keys(void)
  86. {
  87. // not clear mods
  88. for (int8_t i = 1; i < REPORT_SIZE; i++) {
  89. keyboard_report->raw[i] = 0;
  90. }
  91. }
  92. /* modifier */
  93. uint8_t get_mods(void) { return real_mods; }
  94. void add_mods(uint8_t mods) { real_mods |= mods; }
  95. void del_mods(uint8_t mods) { real_mods &= ~mods; }
  96. void set_mods(uint8_t mods) { real_mods = mods; }
  97. void clear_mods(void) { real_mods = 0; }
  98. /* weak modifier */
  99. uint8_t get_weak_mods(void) { return weak_mods; }
  100. void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
  101. void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
  102. void set_weak_mods(uint8_t mods) { weak_mods = mods; }
  103. void clear_weak_mods(void) { weak_mods = 0; }
  104. /* Oneshot modifier */
  105. #ifndef NO_ACTION_ONESHOT
  106. void set_oneshot_mods(uint8_t mods)
  107. {
  108. oneshot_mods = mods;
  109. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  110. oneshot_time = timer_read();
  111. #endif
  112. }
  113. void clear_oneshot_mods(void)
  114. {
  115. oneshot_mods = 0;
  116. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  117. oneshot_time = 0;
  118. #endif
  119. }
  120. #endif
  121. /*
  122. * inspect keyboard state
  123. */
  124. uint8_t has_anykey(void)
  125. {
  126. #ifdef USB_6KRO_ENABLE
  127. #ifdef NKRO_ENABLE
  128. if (!keyboard_nkro) {
  129. #endif
  130. return cb_count;
  131. #ifdef NKRO_ENABLE
  132. }
  133. #endif
  134. #else
  135. uint8_t cnt = 0;
  136. for (uint8_t i = 1; i < REPORT_SIZE; i++) {
  137. if (keyboard_report->raw[i])
  138. cnt++;
  139. }
  140. return cnt;
  141. #endif
  142. }
  143. uint8_t has_anymod(void)
  144. {
  145. return bitpop(real_mods);
  146. }
  147. uint8_t get_first_key(void)
  148. {
  149. #ifdef NKRO_ENABLE
  150. if (keyboard_nkro) {
  151. uint8_t i = 0;
  152. for (; i < REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
  153. ;
  154. return i<<3 | biton(keyboard_report->nkro.bits[i]);
  155. }
  156. #endif
  157. #ifdef USB_6KRO_ENABLE
  158. uint8_t i = cb_head;
  159. do {
  160. if (keyboard_report->keys[i] != 0) {
  161. break;
  162. }
  163. i = RO_INC(i);
  164. } while (i != cb_tail);
  165. return keyboard_report->keys[i];
  166. #else
  167. return keyboard_report->keys[0];
  168. #endif
  169. }
  170. #ifdef USB_6KRO_ENABLE
  171. #ifdef DEBUG
  172. static void dump_report_keys(void) {
  173. dprintf("\n");
  174. for (uint8_t i = 0; i < REPORT_KEYS; i++) {
  175. dprintf("%02X ", keyboard_report->keys[i]);
  176. }
  177. dprintf("\n");
  178. for (uint8_t i = 0; i < REPORT_KEYS; i++) {
  179. dprintf("%c%c ", i==cb_head?'H':' ', i==cb_tail?'T':' ');
  180. }
  181. dprintf("\n");
  182. }
  183. #endif
  184. #endif
  185. /* local functions */
  186. static inline void add_key_byte(uint8_t code)
  187. {
  188. #ifdef USB_6KRO_ENABLE
  189. int8_t i = cb_head;
  190. int8_t empty = -1;
  191. if (cb_count) {
  192. do {
  193. if (keyboard_report->keys[i] == code) {
  194. return;
  195. }
  196. if (empty == -1 && keyboard_report->keys[i] == 0) {
  197. empty = i;
  198. }
  199. i = RO_INC(i);
  200. } while (i != cb_tail);
  201. if (i == cb_tail) {
  202. if (cb_tail == cb_head) {
  203. // buffer is full
  204. if (empty == -1) {
  205. // pop head when has no empty space
  206. cb_head = RO_INC(cb_head);
  207. cb_count--;
  208. }
  209. else {
  210. // left shift when has empty space
  211. uint8_t offset = 1;
  212. i = RO_INC(empty);
  213. do {
  214. if (keyboard_report->keys[i] != 0) {
  215. keyboard_report->keys[empty] = keyboard_report->keys[i];
  216. keyboard_report->keys[i] = 0;
  217. empty = RO_INC(empty);
  218. }
  219. else {
  220. offset++;
  221. }
  222. i = RO_INC(i);
  223. } while (i != cb_tail);
  224. cb_tail = RO_SUB(cb_tail, offset);
  225. }
  226. }
  227. }
  228. }
  229. // add to tail
  230. keyboard_report->keys[cb_tail] = code;
  231. cb_tail = RO_INC(cb_tail);
  232. cb_count++;
  233. #ifdef DEBUG
  234. dump_report_keys();
  235. #endif
  236. #else
  237. int8_t i = 0;
  238. int8_t empty = -1;
  239. for (; i < REPORT_KEYS; i++) {
  240. if (keyboard_report->keys[i] == code) {
  241. break;
  242. }
  243. if (empty == -1 && keyboard_report->keys[i] == 0) {
  244. empty = i;
  245. }
  246. }
  247. if (i == REPORT_KEYS) {
  248. if (empty != -1) {
  249. keyboard_report->keys[empty] = code;
  250. }
  251. }
  252. #endif
  253. }
  254. static inline void del_key_byte(uint8_t code)
  255. {
  256. #ifdef USB_6KRO_ENABLE
  257. uint8_t i = cb_head;
  258. if (cb_count) {
  259. do {
  260. if (keyboard_report->keys[i] == code) {
  261. keyboard_report->keys[i] = 0;
  262. cb_count--;
  263. if (cb_count == 0) {
  264. // reset head and tail
  265. cb_tail = cb_head = 0;
  266. }
  267. if (i == RO_DEC(cb_tail)) {
  268. // left shift when next to tail
  269. do {
  270. cb_tail = RO_DEC(cb_tail);
  271. if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
  272. break;
  273. }
  274. } while (cb_tail != cb_head);
  275. }
  276. break;
  277. }
  278. i = RO_INC(i);
  279. } while (i != cb_tail);
  280. }
  281. #ifdef DEBUG
  282. dump_report_keys();
  283. #endif
  284. #else
  285. for (uint8_t i = 0; i < REPORT_KEYS; i++) {
  286. if (keyboard_report->keys[i] == code) {
  287. keyboard_report->keys[i] = 0;
  288. }
  289. }
  290. #endif
  291. }
  292. #ifdef NKRO_ENABLE
  293. static inline void add_key_bit(uint8_t code)
  294. {
  295. if ((code>>3) < REPORT_BITS) {
  296. keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
  297. } else {
  298. dprintf("add_key_bit: can't add: %02X\n", code);
  299. }
  300. }
  301. static inline void del_key_bit(uint8_t code)
  302. {
  303. if ((code>>3) < REPORT_BITS) {
  304. keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
  305. } else {
  306. dprintf("del_key_bit: can't del: %02X\n", code);
  307. }
  308. }
  309. #endif