Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

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