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 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. static void process(keyevent_t event);
  12. static void register_code(uint8_t code);
  13. static void unregister_code(uint8_t code);
  14. static void add_mods(uint8_t mods);
  15. static void del_mods(uint8_t mods);
  16. static void set_mods(uint8_t mods);
  17. static void clear_keyboard(void);
  18. static void clear_keyboard_but_mods(void);
  19. static bool sending_anykey(void);
  20. static void layer_switch(uint8_t new_layer);
  21. /* tap */
  22. #define TAP_TIME 300
  23. /* This counts up when tap occurs */
  24. static uint8_t tap_count = 0;
  25. static bool is_tap_key(keyevent_t event)
  26. {
  27. action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
  28. switch (action.kind.id) {
  29. case ACT_LMODS_TAP:
  30. case ACT_RMODS_TAP:
  31. return true;
  32. case ACT_LAYER_PRESSED:
  33. case ACT_LAYER_BIT:
  34. switch (action.layer.code) {
  35. case 0x00:
  36. case 0xF1 ... 0xFF:
  37. return false;
  38. case 0xF0:
  39. default:
  40. return true;
  41. }
  42. return false;
  43. }
  44. return false;
  45. }
  46. /* layer */
  47. uint8_t default_layer = 0;
  48. uint8_t current_layer = 0;
  49. static keyevent_t tapping_event = {};
  50. /* TAPPING: This indicates that whether tap or not is not decided yet. */
  51. // NOTE: keyevent_t.time 0 means no event.
  52. #define IS_TAPPING(k) (tapping_event.time != 0 && KEYEQ(tapping_event.key, (k)))
  53. /* waiting keys buffer */
  54. #define WAITING_KEYS_BUFFER 8
  55. static keyevent_t waiting_events[WAITING_KEYS_BUFFER] = {};
  56. static uint8_t waiting_events_head = 0;
  57. static bool waiting_events_enqueue(keyevent_t event)
  58. {
  59. if (IS_NOEVENT(event)) { return true; }
  60. if (waiting_events_head < WAITING_KEYS_BUFFER) {
  61. debug("waiting_events["); debug_dec(waiting_events_head); debug("] = ");
  62. debug_hex16(event.key.raw); debug("\n");
  63. waiting_events[waiting_events_head++] = event;
  64. return true;
  65. }
  66. debug("waiting_events_enqueue: Over flow.\n");
  67. return false;
  68. }
  69. static void waiting_events_clear(void)
  70. {
  71. waiting_events_head = 0;
  72. }
  73. static bool waiting_events_has(key_t key)
  74. {
  75. for (uint8_t i = 0; i < waiting_events_head; i++) {
  76. if KEYEQ(key, waiting_events[i].key) return true;
  77. }
  78. return false;
  79. }
  80. static void waiting_events_process_in_current_layer(void)
  81. {
  82. // TODO: in case of including tap key in waiting keys
  83. for (uint8_t i = 0; i < waiting_events_head; i++) {
  84. debug("waiting_events_process_in_current_layer["); debug_dec(i); debug("]\n");
  85. process(waiting_events[i]);
  86. }
  87. waiting_events_clear();
  88. }
  89. static bool waiting_events_has_anykey_pressed(void)
  90. {
  91. for (uint8_t i = 0; i < waiting_events_head; i++) {
  92. if (waiting_events[i].pressed) return true;
  93. }
  94. return false;
  95. }
  96. void action_exec(keyevent_t event)
  97. {
  98. if (!IS_NOEVENT(event)) {
  99. debug("event: "); debug_hex16(event.key.raw);
  100. debug("[");
  101. if (event.pressed) debug("down"); else debug("up");
  102. debug("]\n");
  103. }
  104. // In tapping term
  105. if (tapping_event.time && timer_elapsed(tapping_event.time) < TAP_TIME) {
  106. if (tapping_event.pressed) {
  107. if (!event.pressed && KEYEQ(tapping_event.key, event.key)) {
  108. debug("Tapping: Release tap key.\n");
  109. if (tap_count == 0) {
  110. debug("Tapping: First tap.\n");
  111. // count up on release
  112. tap_count++;
  113. process(tapping_event);
  114. waiting_events_process_in_current_layer();
  115. }
  116. tapping_event = event;
  117. process(event);
  118. } else if (!event.pressed && waiting_events_has(event.key)) {
  119. debug("Tapping: End(No tap by typing waiting key).\n");
  120. process(tapping_event);
  121. waiting_events_process_in_current_layer();
  122. process(event);
  123. tap_count = 0;
  124. tapping_event = (keyevent_t){};
  125. } else {
  126. //debug("Tapping: pressing tap key.\n");
  127. if (tap_count == 0) {
  128. // store event
  129. waiting_events_enqueue(event);
  130. return;
  131. }
  132. process(event);
  133. }
  134. } else {
  135. //debug("Tapping after releasing tap.\n");
  136. // Sequential tap
  137. if (tap_count && event.pressed && KEYEQ(tapping_event.key, event.key)) {
  138. tap_count++;
  139. tapping_event = event;
  140. debug("Tapping: Sequential tap("); debug_hex(tap_count); debug(")\n");
  141. }
  142. process(event);
  143. }
  144. }
  145. // Not in tapping term
  146. else {
  147. if (tapping_event.time) {
  148. if (tapping_event.pressed) {
  149. if (tap_count == 0) {
  150. // Not tap, holding down normal key.
  151. debug("Not tap.\n");
  152. process(tapping_event);
  153. waiting_events_process_in_current_layer();
  154. tap_count = 0;
  155. tapping_event = (keyevent_t){};
  156. process(event);
  157. } else {
  158. // Holding down last tap key.
  159. //debug("Time out with holding last tap.\n");
  160. process(event);
  161. if (!event.pressed && KEYEQ(tapping_event.key, event.key)) {
  162. debug("Tapping: End(Release holding last tap).\n");
  163. // clear after release last tap key
  164. tap_count = 0;
  165. tapping_event = (keyevent_t){};
  166. waiting_events_clear();
  167. }
  168. }
  169. } else {
  170. // time out for sequential tap after complete last tap
  171. debug("Tapping: End(Time out after releasing last tap).\n");
  172. tap_count = 0;
  173. tapping_event = (keyevent_t){};
  174. waiting_events_clear();
  175. process(event);
  176. }
  177. } else {
  178. // Normal state without tapping
  179. if (event.pressed && is_tap_key(event)) {
  180. debug("Tapping: Start(Press tap key).\n");
  181. tapping_event = event;
  182. tap_count = 0;
  183. waiting_events_clear();
  184. } else {
  185. //debug("Normal event(No tapping)\n");
  186. process(event);
  187. }
  188. }
  189. }
  190. }
  191. static void process(keyevent_t event)
  192. {
  193. if (IS_NOEVENT(event)) { return; }
  194. action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col);
  195. debug("action: "); debug_hex16(action.code);
  196. if (event.pressed) debug("[down]\n"); else debug("[up]\n");
  197. switch (action.kind.id) {
  198. /* Key and Mods */
  199. case ACT_LMODS:
  200. // |pressed |released
  201. // --------------+---------------------------------+------------
  202. // key |down(key) |up(key)
  203. // mods |add(mods) |del(mods)
  204. // key with mods |add(mods), down(key), unset(mods)|up(key)
  205. if (event.pressed) {
  206. uint8_t tmp_mods = host_get_mods();
  207. if (action.key.mods) {
  208. host_add_mods(action.key.mods);
  209. host_send_keyboard_report();
  210. }
  211. register_code(action.key.code);
  212. if (action.key.mods && action.key.code) {
  213. host_set_mods(tmp_mods);
  214. host_send_keyboard_report();
  215. }
  216. } else {
  217. if (action.key.mods && !action.key.code) {
  218. host_del_mods(action.key.mods);
  219. host_send_keyboard_report();
  220. }
  221. unregister_code(action.key.code);
  222. }
  223. break;
  224. case ACT_RMODS:
  225. // |pressed |released
  226. // --------------+---------------------------------+------------
  227. // key |down(key) |up(key)
  228. // mods |add(mods) |del(mods)
  229. // key with mods |add(mods), down(key), unset(mods)|up(key)
  230. if (event.pressed) {
  231. uint8_t tmp_mods = host_get_mods();
  232. if (action.key.mods) {
  233. host_add_mods(action.key.mods<<4);
  234. host_send_keyboard_report();
  235. }
  236. register_code(action.key.code);
  237. if (action.key.mods && action.key.code) {
  238. host_set_mods(tmp_mods);
  239. host_send_keyboard_report();
  240. }
  241. } else {
  242. if (action.key.mods && !action.key.code) {
  243. host_del_mods(action.key.mods<<4);
  244. host_send_keyboard_report();
  245. }
  246. unregister_code(action.key.code);
  247. }
  248. break;
  249. case ACT_LMODS_TAP:
  250. case ACT_RMODS_TAP:
  251. {
  252. uint8_t tmp_mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  253. action.key.mods<<4;
  254. if (event.pressed) {
  255. if (IS_TAPPING(event.key) && tap_count > 0) {
  256. if (waiting_events_has_anykey_pressed()) {
  257. debug("MODS_TAP: Tap: Cancel: add_mods\n");
  258. tap_count = 0;
  259. add_mods(tmp_mods);
  260. } else {
  261. debug("MODS_TAP: Tap: register_code\n");
  262. register_code(action.key.code);
  263. }
  264. } else {
  265. debug("MODS_TAP: No tap: add_mods\n");
  266. add_mods(tmp_mods);
  267. }
  268. } else {
  269. if (IS_TAPPING(event.key) && tap_count > 0) {
  270. debug("MODS_TAP: Tap: unregister_code\n");
  271. unregister_code(action.key.code);
  272. } else {
  273. debug("MODS_TAP: No tap: add_mods\n");
  274. del_mods(tmp_mods);
  275. }
  276. }
  277. }
  278. break;
  279. /* other HID usage */
  280. case ACT_USAGE:
  281. #ifdef EXTRAKEY_ENABLE
  282. switch (action.usage.page) {
  283. case ACTION_USAGE_PAGE_SYSTEM:
  284. if (event.pressed) {
  285. host_system_send(action.usage.code);
  286. } else {
  287. host_system_send(0);
  288. }
  289. break;
  290. case ACTION_USAGE_PAGE_CONSUMER:
  291. if (event.pressed) {
  292. host_consumer_send(action.usage.code);
  293. } else {
  294. host_consumer_send(0);
  295. }
  296. break;
  297. }
  298. #endif
  299. break;
  300. /* Mouse key */
  301. case ACT_MOUSEKEY:
  302. #ifdef MOUSEKEY_ENABLE
  303. if (event.pressed) {
  304. mousekey_on(action.key.code);
  305. mousekey_send();
  306. } else {
  307. mousekey_off(action.key.code);
  308. mousekey_send();
  309. }
  310. #endif
  311. break;
  312. /* Layer key */
  313. case ACT_LAYER_PRESSED:
  314. // layer action when pressed
  315. switch (action.layer.code) {
  316. case 0x00:
  317. if (event.pressed) {
  318. layer_switch(action.layer.opt);
  319. }
  320. break;
  321. case 0xF0:
  322. // TODO: tap toggle
  323. break;
  324. case 0xFF:
  325. if (event.pressed) {
  326. default_layer = action.layer.opt;
  327. layer_switch(default_layer);
  328. }
  329. break;
  330. default:
  331. // with tap key
  332. if (event.pressed) {
  333. if (IS_TAPPING(event.key) && tap_count > 0) {
  334. debug("LAYER_PRESSED: Tap: register_code\n");
  335. register_code(action.layer.code);
  336. } else {
  337. debug("LAYER_PRESSED: No tap: layer_switch\n");
  338. layer_switch(action.layer.opt);
  339. }
  340. } else {
  341. if (IS_TAPPING(event.key) && tap_count > 0) {
  342. debug("LAYER_PRESSED: Tap: unregister_code\n");
  343. unregister_code(action.layer.code);
  344. } else {
  345. debug("LAYER_PRESSED: No tap: NO ACTION\n");
  346. }
  347. }
  348. break;
  349. }
  350. break;
  351. case ACT_LAYER_RELEASED:
  352. switch (action.layer.code) {
  353. case 0x00:
  354. if (!event.pressed) {
  355. layer_switch(action.layer.opt);
  356. }
  357. break;
  358. case 0xF0:
  359. // Ignored. LAYER_RELEASED with tap toggle is invalid action.
  360. break;
  361. case 0xFF:
  362. if (!event.pressed) {
  363. default_layer = action.layer.opt;
  364. layer_switch(default_layer);
  365. }
  366. break;
  367. default:
  368. // Ignored. LAYER_RELEASED with tap key is invalid action.
  369. break;
  370. }
  371. break;
  372. case ACT_LAYER_BIT:
  373. switch (action.layer.code) {
  374. case 0x00:
  375. if (event.pressed) {
  376. layer_switch(current_layer | action.layer.opt);
  377. } else {
  378. layer_switch(current_layer & ~action.layer.opt);
  379. }
  380. break;
  381. case 0xF0:
  382. // TODO: tap toggle
  383. break;
  384. case 0xFF:
  385. // change default layer
  386. if (event.pressed) {
  387. default_layer = current_layer | action.layer.opt;
  388. layer_switch(default_layer);
  389. } else {
  390. default_layer = current_layer & ~action.layer.opt;
  391. layer_switch(default_layer);
  392. }
  393. break;
  394. default:
  395. // with tap key
  396. if (event.pressed) {
  397. if (IS_TAPPING(event.key) && tap_count > 0) {
  398. debug("LAYER_BIT: Tap: register_code\n");
  399. register_code(action.layer.code);
  400. } else {
  401. debug("LAYER_BIT: No tap: layer_switch(bit on)\n");
  402. layer_switch(current_layer | action.layer.opt);
  403. }
  404. } else {
  405. if (IS_TAPPING(event.key) && tap_count > 0) {
  406. debug("LAYER_BIT: Tap: unregister_code\n");
  407. unregister_code(action.layer.code);
  408. } else {
  409. debug("LAYER_BIT: No tap: layer_switch(bit off)\n");
  410. layer_switch(current_layer & ~action.layer.opt);
  411. }
  412. }
  413. break;
  414. }
  415. case ACT_LAYER_EXT:
  416. switch (action.layer.opt) {
  417. case 0x00:
  418. // set default layer when pressed
  419. switch (action.layer.code) {
  420. case 0x00:
  421. if (event.pressed) {
  422. layer_switch(default_layer);
  423. }
  424. break;
  425. case 0xF0:
  426. // TODO: tap toggle
  427. break;
  428. case 0xFF:
  429. if (event.pressed) {
  430. default_layer = current_layer;
  431. layer_switch(default_layer);
  432. }
  433. break;
  434. default:
  435. // TODO: tap key
  436. break;
  437. }
  438. break;
  439. case 0x01:
  440. // set default layer when released
  441. switch (action.layer.code) {
  442. case 0x00:
  443. if (!event.pressed) {
  444. layer_switch(default_layer);
  445. }
  446. break;
  447. case 0xFF:
  448. if (!event.pressed) {
  449. default_layer = current_layer;
  450. layer_switch(default_layer);
  451. }
  452. break;
  453. case 0xF0:
  454. default:
  455. // Ignore tap.
  456. if (!event.pressed) {
  457. layer_switch(default_layer);
  458. }
  459. break;
  460. }
  461. break;
  462. }
  463. break;
  464. /* Extentions */
  465. case ACT_MACRO:
  466. case ACT_COMMAND:
  467. case ACT_FUNCTION:
  468. default:
  469. break;
  470. }
  471. }
  472. static void register_code(uint8_t code)
  473. {
  474. if (code == KC_NO) {
  475. return;
  476. }
  477. else if IS_KEY(code) {
  478. // TODO: should push command_proc out of this block?
  479. if (!command_proc(code)) {
  480. host_add_key(code);
  481. host_send_keyboard_report();
  482. }
  483. }
  484. else if IS_MOD(code) {
  485. host_add_mods(MOD_BIT(code));
  486. host_send_keyboard_report();
  487. }
  488. }
  489. static void unregister_code(uint8_t code)
  490. {
  491. if IS_KEY(code) {
  492. host_del_key(code);
  493. host_send_keyboard_report();
  494. }
  495. else if IS_MOD(code) {
  496. host_del_mods(MOD_BIT(code));
  497. host_send_keyboard_report();
  498. }
  499. }
  500. static void add_mods(uint8_t mods)
  501. {
  502. if (mods) {
  503. host_add_mods(mods);
  504. host_send_keyboard_report();
  505. }
  506. }
  507. static void del_mods(uint8_t mods)
  508. {
  509. if (mods) {
  510. host_del_mods(mods);
  511. host_send_keyboard_report();
  512. }
  513. }
  514. static void set_mods(uint8_t mods)
  515. {
  516. host_set_mods(mods);
  517. host_send_keyboard_report();
  518. }
  519. static void clear_keyboard(void)
  520. {
  521. host_clear_mods();
  522. clear_keyboard_but_mods();
  523. }
  524. static void clear_keyboard_but_mods(void)
  525. {
  526. host_clear_keys();
  527. host_send_keyboard_report();
  528. #ifdef MOUSEKEY_ENABLE
  529. mousekey_clear();
  530. mousekey_send();
  531. #endif
  532. #ifdef EXTRAKEY_ENABLE
  533. host_system_send(0);
  534. host_consumer_send(0);
  535. #endif
  536. }
  537. static bool sending_anykey(void)
  538. {
  539. return (host_has_anykey() || host_mouse_in_use() ||
  540. host_last_sysytem_report() || host_last_consumer_report());
  541. }
  542. static void layer_switch(uint8_t new_layer)
  543. {
  544. if (current_layer != new_layer) {
  545. debug("Layer Switch: "); debug_hex(current_layer);
  546. debug(" -> "); debug_hex(new_layer); debug("\n");
  547. current_layer = new_layer;
  548. clear_keyboard_but_mods(); // To avoid stuck keys
  549. // TODO: update mods with full scan of matrix? if modifier changes between layers
  550. }
  551. }