Keyboard firmwares for Atmel AVR and Cortex-M
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.

action.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "host.h"
  2. #include "timer.h"
  3. #include "keymap.h"
  4. #include "keycode.h"
  5. #include "keyboard.h"
  6. #include "mousekey.h"
  7. #include "command.h"
  8. #include "util.h"
  9. #include "debug.h"
  10. #include "action.h"
  11. #define Kdebug(s) do { if (debug_keyboard) debug(s); } while(0)
  12. #define Kdebug_P(s) do { if (debug_keyboard) debug_P(s); } while(0)
  13. #define Kdebug_hex(s) do { if (debug_keyboard) debug_hex(s); } while(0)
  14. /*
  15. *
  16. * Event/State|IDLE PRESSING DELAYING[f] WAITING[f,k]
  17. * -----------+------------------------------------------------------------------
  18. * Fn Down |(L+) -*1 WAITING(Sk) IDLE(Rf,Ps)*7
  19. * Up |(L-) IDLE(L-)*8 IDLE(L-)*8 IDLE(L-)*8
  20. * Fnk Down |DELAYING(Sf)* (Rf) WAITING(Sk) IDLE(Rf,Ps,Rf)
  21. * Up |(L-) IDLE(L-/Uf)*8 IDLE(Rf,Uf/L-)*3 IDLE(Rf,Ps,Uf/L-)*3
  22. * Key Down |PRESSING(Rk) (Rk) WAITING(Sk) IDLE(Rf,Ps,Rk)
  23. * Up |(Uk) IDLE(Uk)*4 (Uk) IDLE(L+,Ps,Pk)/(Uk)*a
  24. * |
  25. * Delay |- - IDLE(L+) IDLE(L+,Ps)
  26. * Magic Key |COMMAND*5
  27. *
  28. * *1: ignore Fn if other key is down.
  29. * *2: register Fnk if any key is pressing
  30. * *3: register/unregister delayed Fnk and move to IDLE if code == delayed Fnk, else *8
  31. * *4: if no keys registered to host
  32. * *5: unregister all keys
  33. * *6: only if no keys down
  34. * *7: ignore Fn because Fnk key and stored key are down.
  35. * *8: move to IDLE if layer switch(off) occurs, else stay at current state
  36. * *9: repeat key if pressing Fnk twice quickly(move to PRESSING)
  37. * *a: layer switch and process waiting key and code if code == wainting key, else unregister key
  38. *
  39. * States:
  40. * IDLE: No key is down except modifiers
  41. * DELAYING: delay layer switch after pressing Fn with alt keycode
  42. * WAITING: key is pressed during DELAYING
  43. *
  44. * Events:
  45. * Fn: Fn key without alternative keycode
  46. * Fnk: Fn key with alternative keycode
  47. * -: ignore
  48. * Delay: layer switch delay term is elapsed
  49. *
  50. * Actions:
  51. * Rk: register key
  52. * Uk: unregister key
  53. * Rf: register Fn(alt keycode)
  54. * Uf: unregister Fn(alt keycode)
  55. * Rs: register stored key
  56. * Us: unregister stored key
  57. * Sk: Store key(waiting Key)
  58. * Sf: Store Fn(delayed Fn)
  59. * Ps: Process stored key
  60. * Ps: Process key
  61. * Is: Interpret stored keys in current layer
  62. * L+: Switch to new layer(*unregister* all keys but modifiers)
  63. * L-: Switch back to last layer(*unregister* all keys but modifiers)
  64. * Ld: Switch back to default layer(*unregister* all keys but modifiers)
  65. */
  66. typedef enum { IDLE, DELAYING, WAITING, PRESSING } kbdstate_t;
  67. #define NEXT(state) do { \
  68. Kdebug("NEXT: "); Kdebug_P(state_str(kbdstate)); \
  69. kbdstate = state; \
  70. Kdebug(" -> "); Kdebug_P(state_str(kbdstate)); Kdebug("\n"); \
  71. } while (0)
  72. static kbdstate_t kbdstate = IDLE;
  73. static uint8_t fn_state_bits = 0;
  74. static const char *state_str(kbdstate_t state)
  75. {
  76. if (state == IDLE) return PSTR("IDLE");
  77. if (state == DELAYING) return PSTR("DELAYING");
  78. if (state == WAITING) return PSTR("WAITING");
  79. if (state == PRESSING) return PSTR("PRESSING");
  80. return PSTR("UNKNOWN");
  81. }
  82. static bool anykey_sent_to_host(void)
  83. {
  84. return (host_has_anykey() || host_mouse_in_use() ||
  85. host_last_sysytem_report() || host_last_consumer_report());
  86. }
  87. static void register_code(uint8_t code);
  88. static void unregister_code(uint8_t code);
  89. static void register_mods(uint8_t mods);
  90. static void unregister_mods(uint8_t mods);
  91. static void clear_keyboard(void);
  92. static void clear_keyboard_but_mods(void);
  93. static void layer_switch(uint8_t new_layer);
  94. /* tap */
  95. #define TAP_TIME 200
  96. #define LAYER_DELAY 200
  97. static keyevent_t last_event = {};
  98. static uint16_t last_event_time = 0;
  99. static uint8_t tap_count = 0;
  100. /* layer */
  101. uint8_t default_layer = 0;
  102. uint8_t current_layer = 0;
  103. keyrecord_t delaying_layer = {};
  104. void action_exec(keyevent_t event)
  105. {
  106. /* count tap when key is up */
  107. if (KEYEQ(event.key, last_event.key) && timer_elapsed(last_event_time) < TAP_TIME) {
  108. if (!event.pressed) tap_count++;
  109. } else {
  110. tap_count = 0;
  111. }
  112. /* layer switch after LAYER_DELAY */
  113. if (delaying_layer.action.code && timer_elapsed(delaying_layer.event.time) > LAYER_DELAY) {
  114. switch (delaying_layer.action.kind.id) {
  115. case ACT_LAYER_PRESSED:
  116. layer_switch(delaying_layer.action.layer.opt);
  117. break;
  118. case ACT_LAYER_BIT:
  119. layer_switch(current_layer | delaying_layer.action.layer.opt);
  120. break;
  121. }
  122. delaying_layer = (keyrecord_t){};
  123. }
  124. action_t action = keymap_get_action(current_layer, event.key.row, event.key.col);
  125. debug("action: "); debug_hex16(action.code); debug("\n");
  126. debug("kind.id: "); debug_hex(action.kind.id); debug("\n");
  127. debug("kind.param: "); debug_hex16(action.kind.param); debug("\n");
  128. debug("key.code: "); debug_hex(action.key.code); debug("\n");
  129. debug("key.mods: "); debug_hex(action.key.mods); debug("\n");
  130. switch (action.kind.id) {
  131. case ACT_LMODS:
  132. // normal key or key plus mods
  133. if (event.pressed) {
  134. register_mods(action.key.mods);
  135. register_code(action.key.code);
  136. } else {
  137. unregister_code(action.key.code);
  138. unregister_mods(action.key.mods);
  139. }
  140. break;
  141. case ACT_RMODS:
  142. if (event.pressed) {
  143. register_mods(action.key.mods<<4);
  144. register_code(action.key.code);
  145. } else {
  146. unregister_code(action.key.code);
  147. unregister_mods(action.key.mods<<4);
  148. }
  149. break;
  150. case ACT_LMOD_TAP:
  151. break;
  152. case ACT_RMOD_TAP:
  153. break;
  154. case ACT_USAGE:
  155. #ifdef EXTRAKEY_ENABLE
  156. switch (action.usage.page) {
  157. case ACTION_USAGE_PAGE_SYSTEM:
  158. if (event.pressed) {
  159. host_system_send(action.usage.code);
  160. } else {
  161. host_system_send(0);
  162. }
  163. break;
  164. case ACTION_USAGE_PAGE_CONSUMER:
  165. if (event.pressed) {
  166. host_consumer_send(action.usage.code);
  167. } else {
  168. host_consumer_send(0);
  169. }
  170. break;
  171. }
  172. #endif
  173. break;
  174. case ACT_MOUSEKEY:
  175. #ifdef MOUSEKEY_ENABLE
  176. if (event.pressed) {
  177. mousekey_on(action.key.code);
  178. mousekey_send();
  179. } else {
  180. mousekey_off(action.key.code);
  181. mousekey_send();
  182. }
  183. #endif
  184. break;
  185. case ACT_LAYER_PRESSED:
  186. // layer action when pressed
  187. switch (action.layer.code) {
  188. case 0x00:
  189. if (event.pressed) {
  190. layer_switch(action.layer.opt);
  191. }
  192. break;
  193. case 0xF0:
  194. // TODO: tap toggle
  195. break;
  196. case 0xFF:
  197. if (event.pressed) {
  198. default_layer = action.layer.opt;
  199. layer_switch(default_layer);
  200. }
  201. break;
  202. default:
  203. // with tap key
  204. debug("tap: "); debug_hex(tap_count); debug("\n");
  205. if (event.pressed) {
  206. if (tap_count == 0) {
  207. if (host_has_anykey()) {
  208. register_code(action.layer.code);
  209. } else {
  210. delaying_layer = (keyrecord_t){
  211. .event = event,
  212. .action = action,
  213. .mods = keyboard_report->mods
  214. };
  215. }
  216. } else if (tap_count > 0) {
  217. register_code(action.layer.code);
  218. }
  219. } else {
  220. // type key after tap
  221. if (tap_count == 1) {
  222. delaying_layer = (keyrecord_t){};
  223. register_code(action.layer.code);
  224. }
  225. unregister_code(action.layer.code);
  226. }
  227. break;
  228. }
  229. break;
  230. case ACT_LAYER_RELEASED:
  231. switch (action.layer.code) {
  232. case 0x00:
  233. if (event.pressed) {
  234. layer_switch(action.layer.opt);
  235. }
  236. break;
  237. case 0xF0:
  238. // Ignored. LAYER_RELEASED with tap toggle is invalid action.
  239. break;
  240. case 0xFF:
  241. if (!event.pressed) {
  242. default_layer = action.layer.opt;
  243. layer_switch(default_layer);
  244. }
  245. break;
  246. default:
  247. // Ignored. LAYER_RELEASED with tap key is invalid action.
  248. break;
  249. }
  250. break;
  251. case ACT_LAYER_BIT:
  252. switch (action.layer.code) {
  253. case 0x00:
  254. if (event.pressed) {
  255. layer_switch(current_layer | action.layer.opt);
  256. } else {
  257. layer_switch(current_layer & ~action.layer.opt);
  258. }
  259. break;
  260. case 0xF0:
  261. // TODO: tap toggle
  262. break;
  263. case 0xFF:
  264. // change default layer
  265. if (event.pressed) {
  266. default_layer = current_layer | action.layer.opt;
  267. layer_switch(default_layer);
  268. } else {
  269. default_layer = current_layer & ~action.layer.opt;
  270. layer_switch(default_layer);
  271. }
  272. break;
  273. default:
  274. // with tap key
  275. debug("tap: "); debug_hex(tap_count); debug("\n");
  276. if (event.pressed) {
  277. if (tap_count == 0) {
  278. if (host_has_anykey()) {
  279. register_code(action.layer.code);
  280. } else {
  281. delaying_layer = (keyrecord_t){
  282. .event = event,
  283. .action = action,
  284. .mods = keyboard_report->mods
  285. };
  286. }
  287. } else if (tap_count > 0) {
  288. register_code(action.layer.code);
  289. }
  290. } else {
  291. if (tap_count == 0) {
  292. // no tap
  293. layer_switch(current_layer & ~action.layer.opt);
  294. } else if (tap_count == 1) {
  295. // tap
  296. register_code(action.layer.code);
  297. }
  298. unregister_code(action.layer.code);
  299. }
  300. break;
  301. }
  302. case ACT_LAYER_EXT:
  303. switch (action.layer.opt) {
  304. case 0x00:
  305. // set default layer when pressed
  306. switch (action.layer.code) {
  307. case 0x00:
  308. if (event.pressed) {
  309. layer_switch(default_layer);
  310. }
  311. break;
  312. case 0xF0:
  313. // TODO: tap toggle
  314. break;
  315. case 0xFF:
  316. if (event.pressed) {
  317. default_layer = current_layer;
  318. layer_switch(default_layer);
  319. }
  320. break;
  321. default:
  322. // TODO: tap key
  323. break;
  324. }
  325. break;
  326. case 0x01:
  327. // set default layer when released
  328. switch (action.layer.code) {
  329. case 0x00:
  330. if (!event.pressed) {
  331. layer_switch(default_layer);
  332. }
  333. break;
  334. case 0xFF:
  335. if (!event.pressed) {
  336. default_layer = current_layer;
  337. layer_switch(default_layer);
  338. }
  339. break;
  340. case 0xF0:
  341. default:
  342. // Ignore tap.
  343. if (!event.pressed) {
  344. layer_switch(default_layer);
  345. }
  346. break;
  347. }
  348. break;
  349. }
  350. break;
  351. case ACT_MACRO:
  352. case ACT_COMMAND:
  353. case ACT_FUNCTION:
  354. default:
  355. break;
  356. }
  357. /* last event */
  358. last_event = event;
  359. last_event_time = timer_read();
  360. }
  361. static void register_code(uint8_t code)
  362. {
  363. if (code == KC_NO) {
  364. return;
  365. }
  366. else if IS_KEY(code) {
  367. // TODO: should push command_proc out of this block?
  368. if (!command_proc(code)) {
  369. host_add_key(code);
  370. host_send_keyboard_report();
  371. }
  372. }
  373. else if IS_MOD(code) {
  374. host_add_mods(MOD_BIT(code));
  375. host_send_keyboard_report();
  376. }
  377. }
  378. static void unregister_code(uint8_t code)
  379. {
  380. if IS_KEY(code) {
  381. host_del_key(code);
  382. host_send_keyboard_report();
  383. }
  384. else if IS_MOD(code) {
  385. host_del_mods(MOD_BIT(code));
  386. host_send_keyboard_report();
  387. }
  388. }
  389. static void register_mods(uint8_t mods)
  390. {
  391. if (!mods) return;
  392. host_add_mods(mods);
  393. host_send_keyboard_report();
  394. }
  395. static void unregister_mods(uint8_t mods)
  396. {
  397. if (!mods) return;
  398. host_del_mods(mods);
  399. host_send_keyboard_report();
  400. }
  401. static void clear_keyboard(void)
  402. {
  403. host_clear_mods();
  404. clear_keyboard_but_mods();
  405. }
  406. static void clear_keyboard_but_mods(void)
  407. {
  408. host_clear_keys();
  409. host_send_keyboard_report();
  410. #ifdef MOUSEKEY_ENABLE
  411. mousekey_clear();
  412. mousekey_send();
  413. #endif
  414. #ifdef EXTRAKEY_ENABLE
  415. host_system_send(0);
  416. host_consumer_send(0);
  417. #endif
  418. }
  419. static void layer_switch(uint8_t new_layer)
  420. {
  421. if (current_layer != new_layer) {
  422. Kdebug("Layer Switch: "); Kdebug_hex(current_layer);
  423. Kdebug(" -> "); Kdebug_hex(new_layer); Kdebug("\n");
  424. current_layer = new_layer;
  425. clear_keyboard_but_mods(); // To avoid stuck keys
  426. }
  427. }