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

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. Copyright 2012,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 "keycode.h"
  16. #include "keyboard.h"
  17. #include "mousekey.h"
  18. #include "command.h"
  19. #include "debug.h"
  20. #include "led.h"
  21. #include "layer_switch.h"
  22. #include "action_tapping.h"
  23. #include "action_oneshot.h"
  24. #include "action_macro.h"
  25. #include "action.h"
  26. void action_exec(keyevent_t event)
  27. {
  28. if (!IS_NOEVENT(event)) {
  29. debug("\n---- action_exec: start -----\n");
  30. debug("EVENT: "); debug_event(event); debug("\n");
  31. }
  32. keyrecord_t record = { .event = event };
  33. #ifndef NO_ACTION_TAPPING
  34. action_tapping_process(record);
  35. #else
  36. process_action(&record);
  37. if (!IS_NOEVENT(record.event)) {
  38. debug("processed: "); debug_record(record); debug("\n");
  39. }
  40. #endif
  41. }
  42. void process_action(keyrecord_t *record)
  43. {
  44. keyevent_t event = record->event;
  45. uint8_t tap_count = record->tap.count;
  46. if (IS_NOEVENT(event)) { return; }
  47. action_t action = layer_switch_get_action(event.key);
  48. debug("ACTION: "); debug_action(action);
  49. debug(" keymaps: "); keymap_debug();
  50. debug(" default_layer: "); debug_dec(default_layer); debug("\n");
  51. switch (action.kind.id) {
  52. /* Key and Mods */
  53. case ACT_LMODS:
  54. case ACT_RMODS:
  55. {
  56. uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
  57. action.key.mods<<4;
  58. if (event.pressed) {
  59. uint8_t tmp_mods = host_get_mods();
  60. if (mods) {
  61. host_add_mods(mods);
  62. host_send_keyboard_report();
  63. }
  64. register_code(action.key.code);
  65. if (mods && action.key.code) {
  66. host_set_mods(tmp_mods);
  67. host_send_keyboard_report();
  68. }
  69. } else {
  70. if (mods && !action.key.code) {
  71. host_del_mods(mods);
  72. host_send_keyboard_report();
  73. }
  74. unregister_code(action.key.code);
  75. }
  76. }
  77. break;
  78. #ifndef NO_ACTION_TAPPING
  79. case ACT_LMODS_TAP:
  80. case ACT_RMODS_TAP:
  81. {
  82. uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  83. action.key.mods<<4;
  84. switch (action.layer.code) {
  85. #ifndef NO_ACTION_ONESHOT
  86. case 0x00:
  87. // Oneshot modifier
  88. if (event.pressed) {
  89. if (tap_count == 0) {
  90. debug("MODS_TAP: Oneshot: add_mods\n");
  91. add_mods(mods);
  92. }
  93. else if (tap_count == 1) {
  94. debug("MODS_TAP: Oneshot: start\n");
  95. oneshot_start(mods);
  96. }
  97. else if (tap_count == TAPPING_TOGGLE) {
  98. debug("MODS_TAP: Oneshot: toggle\n");
  99. oneshot_toggle();
  100. }
  101. else {
  102. debug("MODS_TAP: Oneshot: cancel&add_mods\n");
  103. // double tap cancels oneshot and works as normal modifier.
  104. oneshot_cancel();
  105. add_mods(mods);
  106. }
  107. } else {
  108. if (tap_count == 0) {
  109. debug("MODS_TAP: Oneshot: cancel/del_mods\n");
  110. // cancel oneshot on hold
  111. oneshot_cancel();
  112. del_mods(mods);
  113. }
  114. else if (tap_count == 1) {
  115. debug("MODS_TAP: Oneshot: del_mods\n");
  116. // retain Oneshot
  117. del_mods(mods);
  118. }
  119. else {
  120. debug("MODS_TAP: Oneshot: del_mods\n");
  121. // cancel Mods
  122. del_mods(mods);
  123. }
  124. }
  125. break;
  126. #endif
  127. default:
  128. if (event.pressed) {
  129. if (tap_count > 0) {
  130. if (record->tap.interrupted) {
  131. debug("MODS_TAP: Tap: Cancel: add_mods\n");
  132. // ad hoc: set 0 to cancel tap
  133. record->tap.count = 0;
  134. add_mods(mods);
  135. } else {
  136. debug("MODS_TAP: Tap: register_code\n");
  137. register_code(action.key.code);
  138. }
  139. } else {
  140. debug("MODS_TAP: No tap: add_mods\n");
  141. add_mods(mods);
  142. }
  143. } else {
  144. if (tap_count > 0) {
  145. debug("MODS_TAP: Tap: unregister_code\n");
  146. unregister_code(action.key.code);
  147. } else {
  148. debug("MODS_TAP: No tap: add_mods\n");
  149. del_mods(mods);
  150. }
  151. }
  152. break;
  153. }
  154. }
  155. break;
  156. #endif
  157. #ifdef EXTRAKEY_ENABLE
  158. /* other HID usage */
  159. case ACT_USAGE:
  160. switch (action.usage.page) {
  161. case PAGE_SYSTEM:
  162. if (event.pressed) {
  163. host_system_send(action.usage.code);
  164. } else {
  165. host_system_send(0);
  166. }
  167. break;
  168. case PAGE_CONSUMER:
  169. if (event.pressed) {
  170. host_consumer_send(action.usage.code);
  171. } else {
  172. host_consumer_send(0);
  173. }
  174. break;
  175. }
  176. break;
  177. #endif
  178. #ifdef MOUSEKEY_ENABLE
  179. /* Mouse key */
  180. case ACT_MOUSEKEY:
  181. if (event.pressed) {
  182. mousekey_on(action.key.code);
  183. mousekey_send();
  184. } else {
  185. mousekey_off(action.key.code);
  186. mousekey_send();
  187. }
  188. break;
  189. #endif
  190. #ifndef NO_ACTION_LAYER
  191. case ACT_LAYER:
  192. case ACT_LAYER1:
  193. switch (action.layer.code) {
  194. /* Keymap clear */
  195. case OP_RESET:
  196. switch (action.layer.val & 0x03) {
  197. case 0:
  198. // NOTE: reserved
  199. keymap_clear();
  200. break;
  201. case ON_PRESS:
  202. if (event.pressed) {
  203. keymap_clear();
  204. }
  205. break;
  206. case ON_RELEASE:
  207. if (!event.pressed) {
  208. keymap_clear();
  209. }
  210. break;
  211. case ON_BOTH:
  212. keymap_clear();
  213. break;
  214. /* NOTE: 4-7 rserved */
  215. }
  216. break;
  217. /* Keymap Reset default layer */
  218. case (OP_RESET | ON_PRESS):
  219. if (event.pressed) {
  220. default_layer_set(action.layer.val);
  221. }
  222. break;
  223. case (OP_RESET | ON_RELEASE):
  224. if (!event.pressed) {
  225. default_layer_set(action.layer.val);
  226. }
  227. break;
  228. case (OP_RESET | ON_BOTH):
  229. default_layer_set(action.layer.val);
  230. break;
  231. /* Keymap Bit invert */
  232. case OP_INV:
  233. /* with tap toggle */
  234. if (event.pressed) {
  235. if (tap_count < TAPPING_TOGGLE) {
  236. debug("KEYMAP_INV: tap toggle(press).\n");
  237. keymap_invert(action.layer.val);
  238. }
  239. } else {
  240. if (tap_count <= TAPPING_TOGGLE) {
  241. debug("KEYMAP_INV: tap toggle(release).\n");
  242. keymap_invert(action.layer.val);
  243. }
  244. }
  245. break;
  246. case (OP_INV | ON_PRESS):
  247. if (event.pressed) {
  248. keymap_invert(action.layer.val);
  249. }
  250. break;
  251. case (OP_INV | ON_RELEASE):
  252. if (!event.pressed) {
  253. keymap_invert(action.layer.val);
  254. }
  255. break;
  256. case (OP_INV | ON_BOTH):
  257. keymap_invert(action.layer.val);
  258. break;
  259. /* Keymap Bit on */
  260. case OP_ON:
  261. if (event.pressed) {
  262. keymap_on(action.layer.val);
  263. } else {
  264. keymap_off(action.layer.val);
  265. }
  266. break;
  267. case (OP_ON | ON_PRESS):
  268. if (event.pressed) {
  269. keymap_on(action.layer.val);
  270. }
  271. break;
  272. case (OP_ON | ON_RELEASE):
  273. if (!event.pressed) {
  274. keymap_on(action.layer.val);
  275. }
  276. break;
  277. case (OP_ON | ON_BOTH):
  278. keymap_on(action.layer.val);
  279. break;
  280. /* Keymap Bit off */
  281. case OP_OFF:
  282. if (event.pressed) {
  283. keymap_off(action.layer.val);
  284. } else {
  285. keymap_on(action.layer.val);
  286. }
  287. break;
  288. case (OP_OFF | ON_PRESS):
  289. if (event.pressed) {
  290. keymap_off(action.layer.val);
  291. }
  292. break;
  293. case (OP_OFF | ON_RELEASE):
  294. if (!event.pressed) {
  295. keymap_off(action.layer.val);
  296. }
  297. break;
  298. case (OP_OFF | ON_BOTH):
  299. keymap_off(action.layer.val);
  300. break;
  301. /* Keymap Bit set */
  302. case OP_SET:
  303. if (event.pressed) {
  304. keymap_set(action.layer.val);
  305. } else {
  306. keymap_clear();
  307. }
  308. break;
  309. case (OP_SET | ON_PRESS):
  310. if (event.pressed) {
  311. keymap_set(action.layer.val);
  312. }
  313. break;
  314. case (OP_SET | ON_RELEASE):
  315. if (!event.pressed) {
  316. keymap_set(action.layer.val);
  317. }
  318. break;
  319. case (OP_SET | ON_BOTH):
  320. keymap_set(action.layer.val);
  321. break;
  322. /* Keymap Bit invert with tap key */
  323. default:
  324. if (event.pressed) {
  325. if (tap_count > 0) {
  326. debug("KEYMAP_TAP_KEY: Tap: register_code\n");
  327. register_code(action.layer.code);
  328. } else {
  329. debug("KEYMAP_TAP_KEY: No tap: On on press\n");
  330. keymap_on(action.layer.val);
  331. }
  332. } else {
  333. if (tap_count > 0) {
  334. debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  335. unregister_code(action.layer.code);
  336. } else {
  337. debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
  338. keymap_off(action.layer.val);
  339. }
  340. }
  341. break;
  342. }
  343. break;
  344. #endif
  345. /* Extentions */
  346. #ifndef NO_ACTION_MACRO
  347. case ACT_MACRO:
  348. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  349. break;
  350. #endif
  351. case ACT_COMMAND:
  352. break;
  353. #ifndef NO_ACTION_FUNCTION
  354. case ACT_FUNCTION:
  355. action_function(record, action.func.id, action.func.opt);
  356. break;
  357. #endif
  358. default:
  359. break;
  360. }
  361. }
  362. /*
  363. * Utilities for actions.
  364. */
  365. void register_code(uint8_t code)
  366. {
  367. if (code == KC_NO) {
  368. return;
  369. }
  370. #ifdef CAPSLOCK_LOCKING_ENABLE
  371. else if (KC_LOCKING_CAPS == code) {
  372. #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
  373. // Resync: ignore if caps lock already is on
  374. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  375. #endif
  376. host_add_key(KC_CAPSLOCK);
  377. host_send_keyboard_report();
  378. host_del_key(KC_CAPSLOCK);
  379. host_send_keyboard_report();
  380. }
  381. #endif
  382. else if IS_KEY(code) {
  383. // TODO: should push command_proc out of this block?
  384. if (command_proc(code)) return;
  385. #ifndef NO_ACTION_ONESHOT
  386. if (oneshot_state.mods && !oneshot_state.disabled) {
  387. uint8_t tmp_mods = host_get_mods();
  388. host_add_mods(oneshot_state.mods);
  389. host_add_key(code);
  390. host_send_keyboard_report();
  391. host_set_mods(tmp_mods);
  392. oneshot_cancel();
  393. } else
  394. #endif
  395. {
  396. host_add_key(code);
  397. host_send_keyboard_report();
  398. }
  399. }
  400. else if IS_MOD(code) {
  401. host_add_mods(MOD_BIT(code));
  402. host_send_keyboard_report();
  403. }
  404. }
  405. void unregister_code(uint8_t code)
  406. {
  407. if (code == KC_NO) {
  408. return;
  409. }
  410. #ifdef CAPSLOCK_LOCKING_ENABLE
  411. else if (KC_LOCKING_CAPS == code) {
  412. #ifdef CAPSLOCK_LOCKING_RESYNC_ENABLE
  413. // Resync: ignore if caps lock already is off
  414. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  415. #endif
  416. host_add_key(KC_CAPSLOCK);
  417. host_send_keyboard_report();
  418. host_del_key(KC_CAPSLOCK);
  419. host_send_keyboard_report();
  420. }
  421. #endif
  422. else if IS_KEY(code) {
  423. host_del_key(code);
  424. host_send_keyboard_report();
  425. }
  426. else if IS_MOD(code) {
  427. host_del_mods(MOD_BIT(code));
  428. host_send_keyboard_report();
  429. }
  430. }
  431. void add_mods(uint8_t mods)
  432. {
  433. if (mods) {
  434. host_add_mods(mods);
  435. host_send_keyboard_report();
  436. }
  437. }
  438. void del_mods(uint8_t mods)
  439. {
  440. if (mods) {
  441. host_del_mods(mods);
  442. host_send_keyboard_report();
  443. }
  444. }
  445. void set_mods(uint8_t mods)
  446. {
  447. host_set_mods(mods);
  448. host_send_keyboard_report();
  449. }
  450. void clear_keyboard(void)
  451. {
  452. host_clear_mods();
  453. clear_keyboard_but_mods();
  454. }
  455. void clear_keyboard_but_mods(void)
  456. {
  457. host_clear_keys();
  458. host_send_keyboard_report();
  459. #ifdef MOUSEKEY_ENABLE
  460. mousekey_clear();
  461. mousekey_send();
  462. #endif
  463. #ifdef EXTRAKEY_ENABLE
  464. host_system_send(0);
  465. host_consumer_send(0);
  466. #endif
  467. }
  468. bool sending_anykey(void)
  469. {
  470. return (host_has_anykey() || host_mouse_in_use() ||
  471. host_last_sysytem_report() || host_last_consumer_report());
  472. }
  473. bool is_tap_key(key_t key)
  474. {
  475. action_t action = layer_switch_get_action(key);
  476. switch (action.kind.id) {
  477. case ACT_LMODS_TAP:
  478. case ACT_RMODS_TAP:
  479. return true;
  480. case ACT_LAYER:
  481. switch (action.layer.code) {
  482. case 0x04 ... 0xEF: /* tap key */
  483. case OP_INV:
  484. return true;
  485. default:
  486. return false;
  487. }
  488. case ACT_MACRO:
  489. case ACT_FUNCTION:
  490. if (action.func.opt & FUNC_TAP) { return true; }
  491. return false;
  492. }
  493. return false;
  494. }
  495. /*
  496. * debug print
  497. */
  498. void debug_event(keyevent_t event)
  499. {
  500. debug_hex16((event.key.row<<8) | event.key.col);
  501. if (event.pressed) debug("d("); else debug("u(");
  502. debug_dec(event.time); debug(")");
  503. }
  504. void debug_record(keyrecord_t record)
  505. {
  506. debug_event(record.event);
  507. #ifndef NO_ACTION_TAPPING
  508. debug(":"); debug_dec(record.tap.count);
  509. if (record.tap.interrupted) debug("-");
  510. #endif
  511. }
  512. void debug_action(action_t action)
  513. {
  514. switch (action.kind.id) {
  515. case ACT_LMODS: debug("ACT_LMODS"); break;
  516. case ACT_RMODS: debug("ACT_RMODS"); break;
  517. case ACT_LMODS_TAP: debug("ACT_LMODS_TAP"); break;
  518. case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
  519. case ACT_USAGE: debug("ACT_USAGE"); break;
  520. case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
  521. case ACT_LAYER: debug("ACT_LAYER"); break;
  522. case ACT_LAYER_BITOP: debug("ACT_LAYER_BITOP"); break;
  523. case ACT_MACRO: debug("ACT_MACRO"); break;
  524. case ACT_COMMAND: debug("ACT_COMMAND"); break;
  525. case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
  526. default: debug("UNKNOWN"); break;
  527. }
  528. debug("[");
  529. debug_hex4(action.kind.param>>8);
  530. debug(":");
  531. debug_hex8(action.kind.param & 0xff);
  532. debug("]");
  533. }