action_util.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. Copyright 2013 Jun Wako <wakojun@gmail.com>
  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 "action_layer.h"
  19. #include "timer.h"
  20. #include "keycode_config.h"
  21. extern keymap_config_t keymap_config;
  22. static inline void add_key_byte(uint8_t code);
  23. static inline void del_key_byte(uint8_t code);
  24. #ifdef NKRO_ENABLE
  25. static inline void add_key_bit(uint8_t code);
  26. static inline void del_key_bit(uint8_t code);
  27. #endif
  28. static uint8_t real_mods = 0;
  29. static uint8_t weak_mods = 0;
  30. static uint8_t macro_mods = 0;
  31. #ifdef USB_6KRO_ENABLE
  32. #define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
  33. #define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
  34. #define RO_INC(a) RO_ADD(a, 1)
  35. #define RO_DEC(a) RO_SUB(a, 1)
  36. static int8_t cb_head = 0;
  37. static int8_t cb_tail = 0;
  38. static int8_t cb_count = 0;
  39. #endif
  40. // TODO: pointer variable is not needed
  41. //report_keyboard_t keyboard_report = {};
  42. report_keyboard_t *keyboard_report = &(report_keyboard_t){};
  43. #ifndef NO_ACTION_ONESHOT
  44. static int8_t oneshot_mods = 0;
  45. static int8_t oneshot_locked_mods = 0;
  46. int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
  47. void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
  48. void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
  49. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  50. static int16_t oneshot_time = 0;
  51. inline bool has_oneshot_mods_timed_out() {
  52. return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
  53. }
  54. #endif
  55. #endif
  56. /* oneshot layer */
  57. #ifndef NO_ACTION_ONESHOT
  58. /* oneshot_layer_data bits
  59. * LLLL LSSS
  60. * where:
  61. * L => are layer bits
  62. * S => oneshot state bits
  63. */
  64. static int8_t oneshot_layer_data = 0;
  65. inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
  66. inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
  67. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  68. static int16_t oneshot_layer_time = 0;
  69. inline bool has_oneshot_layer_timed_out() {
  70. return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
  71. !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
  72. }
  73. #endif
  74. /* Oneshot layer */
  75. void set_oneshot_layer(uint8_t layer, uint8_t state)
  76. {
  77. oneshot_layer_data = layer << 3 | state;
  78. layer_on(layer);
  79. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  80. oneshot_layer_time = timer_read();
  81. #endif
  82. }
  83. void reset_oneshot_layer(void) {
  84. oneshot_layer_data = 0;
  85. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  86. oneshot_layer_time = 0;
  87. #endif
  88. }
  89. void clear_oneshot_layer_state(oneshot_fullfillment_t state)
  90. {
  91. uint8_t start_state = oneshot_layer_data;
  92. oneshot_layer_data &= ~state;
  93. if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
  94. layer_off(get_oneshot_layer());
  95. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  96. oneshot_layer_time = 0;
  97. #endif
  98. }
  99. }
  100. bool is_oneshot_layer_active(void)
  101. {
  102. return get_oneshot_layer_state();
  103. }
  104. #endif
  105. void send_keyboard_report(void) {
  106. keyboard_report->mods = real_mods;
  107. keyboard_report->mods |= weak_mods;
  108. keyboard_report->mods |= macro_mods;
  109. #ifndef NO_ACTION_ONESHOT
  110. if (oneshot_mods) {
  111. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  112. if (has_oneshot_mods_timed_out()) {
  113. dprintf("Oneshot: timeout\n");
  114. clear_oneshot_mods();
  115. }
  116. #endif
  117. keyboard_report->mods |= oneshot_mods;
  118. if (has_anykey()) {
  119. clear_oneshot_mods();
  120. }
  121. }
  122. #endif
  123. host_keyboard_send(keyboard_report);
  124. }
  125. /* key */
  126. void add_key(uint8_t key)
  127. {
  128. #ifdef NKRO_ENABLE
  129. if (keyboard_protocol && keymap_config.nkro) {
  130. add_key_bit(key);
  131. return;
  132. }
  133. #endif
  134. add_key_byte(key);
  135. }
  136. void del_key(uint8_t key)
  137. {
  138. #ifdef NKRO_ENABLE
  139. if (keyboard_protocol && keymap_config.nkro) {
  140. del_key_bit(key);
  141. return;
  142. }
  143. #endif
  144. del_key_byte(key);
  145. }
  146. void clear_keys(void)
  147. {
  148. // not clear mods
  149. for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
  150. keyboard_report->raw[i] = 0;
  151. }
  152. }
  153. /* modifier */
  154. uint8_t get_mods(void) { return real_mods; }
  155. void add_mods(uint8_t mods) { real_mods |= mods; }
  156. void del_mods(uint8_t mods) { real_mods &= ~mods; }
  157. void set_mods(uint8_t mods) { real_mods = mods; }
  158. void clear_mods(void) { real_mods = 0; }
  159. /* weak modifier */
  160. uint8_t get_weak_mods(void) { return weak_mods; }
  161. void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
  162. void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
  163. void set_weak_mods(uint8_t mods) { weak_mods = mods; }
  164. void clear_weak_mods(void) { weak_mods = 0; }
  165. /* macro modifier */
  166. uint8_t get_macro_mods(void) { return macro_mods; }
  167. void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
  168. void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
  169. void set_macro_mods(uint8_t mods) { macro_mods = mods; }
  170. void clear_macro_mods(void) { macro_mods = 0; }
  171. /* Oneshot modifier */
  172. #ifndef NO_ACTION_ONESHOT
  173. void set_oneshot_mods(uint8_t mods)
  174. {
  175. oneshot_mods = mods;
  176. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  177. oneshot_time = timer_read();
  178. #endif
  179. }
  180. void clear_oneshot_mods(void)
  181. {
  182. oneshot_mods = 0;
  183. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  184. oneshot_time = 0;
  185. #endif
  186. }
  187. uint8_t get_oneshot_mods(void)
  188. {
  189. return oneshot_mods;
  190. }
  191. #endif
  192. /*
  193. * inspect keyboard state
  194. */
  195. uint8_t has_anykey(void)
  196. {
  197. uint8_t cnt = 0;
  198. for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
  199. if (keyboard_report->raw[i])
  200. cnt++;
  201. }
  202. return cnt;
  203. }
  204. uint8_t has_anymod(void)
  205. {
  206. return bitpop(real_mods);
  207. }
  208. uint8_t get_first_key(void)
  209. {
  210. #ifdef NKRO_ENABLE
  211. if (keyboard_protocol && keymap_config.nkro) {
  212. uint8_t i = 0;
  213. for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
  214. ;
  215. return i<<3 | biton(keyboard_report->nkro.bits[i]);
  216. }
  217. #endif
  218. #ifdef USB_6KRO_ENABLE
  219. uint8_t i = cb_head;
  220. do {
  221. if (keyboard_report->keys[i] != 0) {
  222. break;
  223. }
  224. i = RO_INC(i);
  225. } while (i != cb_tail);
  226. return keyboard_report->keys[i];
  227. #else
  228. return keyboard_report->keys[0];
  229. #endif
  230. }
  231. /* local functions */
  232. static inline void add_key_byte(uint8_t code)
  233. {
  234. #ifdef USB_6KRO_ENABLE
  235. int8_t i = cb_head;
  236. int8_t empty = -1;
  237. if (cb_count) {
  238. do {
  239. if (keyboard_report->keys[i] == code) {
  240. return;
  241. }
  242. if (empty == -1 && keyboard_report->keys[i] == 0) {
  243. empty = i;
  244. }
  245. i = RO_INC(i);
  246. } while (i != cb_tail);
  247. if (i == cb_tail) {
  248. if (cb_tail == cb_head) {
  249. // buffer is full
  250. if (empty == -1) {
  251. // pop head when has no empty space
  252. cb_head = RO_INC(cb_head);
  253. cb_count--;
  254. }
  255. else {
  256. // left shift when has empty space
  257. uint8_t offset = 1;
  258. i = RO_INC(empty);
  259. do {
  260. if (keyboard_report->keys[i] != 0) {
  261. keyboard_report->keys[empty] = keyboard_report->keys[i];
  262. keyboard_report->keys[i] = 0;
  263. empty = RO_INC(empty);
  264. }
  265. else {
  266. offset++;
  267. }
  268. i = RO_INC(i);
  269. } while (i != cb_tail);
  270. cb_tail = RO_SUB(cb_tail, offset);
  271. }
  272. }
  273. }
  274. }
  275. // add to tail
  276. keyboard_report->keys[cb_tail] = code;
  277. cb_tail = RO_INC(cb_tail);
  278. cb_count++;
  279. #else
  280. int8_t i = 0;
  281. int8_t empty = -1;
  282. for (; i < KEYBOARD_REPORT_KEYS; i++) {
  283. if (keyboard_report->keys[i] == code) {
  284. break;
  285. }
  286. if (empty == -1 && keyboard_report->keys[i] == 0) {
  287. empty = i;
  288. }
  289. }
  290. if (i == KEYBOARD_REPORT_KEYS) {
  291. if (empty != -1) {
  292. keyboard_report->keys[empty] = code;
  293. }
  294. }
  295. #endif
  296. }
  297. static inline void del_key_byte(uint8_t code)
  298. {
  299. #ifdef USB_6KRO_ENABLE
  300. uint8_t i = cb_head;
  301. if (cb_count) {
  302. do {
  303. if (keyboard_report->keys[i] == code) {
  304. keyboard_report->keys[i] = 0;
  305. cb_count--;
  306. if (cb_count == 0) {
  307. // reset head and tail
  308. cb_tail = cb_head = 0;
  309. }
  310. if (i == RO_DEC(cb_tail)) {
  311. // left shift when next to tail
  312. do {
  313. cb_tail = RO_DEC(cb_tail);
  314. if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
  315. break;
  316. }
  317. } while (cb_tail != cb_head);
  318. }
  319. break;
  320. }
  321. i = RO_INC(i);
  322. } while (i != cb_tail);
  323. }
  324. #else
  325. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  326. if (keyboard_report->keys[i] == code) {
  327. keyboard_report->keys[i] = 0;
  328. }
  329. }
  330. #endif
  331. }
  332. #ifdef NKRO_ENABLE
  333. static inline void add_key_bit(uint8_t code)
  334. {
  335. if ((code>>3) < KEYBOARD_REPORT_BITS) {
  336. keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
  337. } else {
  338. dprintf("add_key_bit: can't add: %02X\n", code);
  339. }
  340. }
  341. static inline void del_key_bit(uint8_t code)
  342. {
  343. if ((code>>3) < KEYBOARD_REPORT_BITS) {
  344. keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
  345. } else {
  346. dprintf("del_key_bit: can't del: %02X\n", code);
  347. }
  348. }
  349. #endif