Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

action.c 18KB

před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
před 11 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 "led.h"
  20. #include "backlight.h"
  21. #include "action_layer.h"
  22. #include "action_tapping.h"
  23. #include "action_macro.h"
  24. #include "action_util.h"
  25. #include "action.h"
  26. #ifdef DEBUG_ACTION
  27. #include "debug.h"
  28. #else
  29. #include "nodebug.h"
  30. #endif
  31. void action_exec(keyevent_t event)
  32. {
  33. if (!IS_NOEVENT(event)) {
  34. dprint("\n---- action_exec: start -----\n");
  35. dprint("EVENT: "); debug_event(event); dprintln();
  36. }
  37. keyrecord_t record = { .event = event };
  38. action_keyevent(event);
  39. #ifndef NO_ACTION_TAPPING
  40. action_tapping_process(record);
  41. #else
  42. process_action(&record);
  43. if (!IS_NOEVENT(record.event)) {
  44. dprint("processed: "); debug_record(record); dprintln();
  45. }
  46. #endif
  47. }
  48. void process_action(keyrecord_t *record)
  49. {
  50. keyevent_t event = record->event;
  51. #ifndef NO_ACTION_TAPPING
  52. uint8_t tap_count = record->tap.count;
  53. #endif
  54. if (IS_NOEVENT(event)) { return; }
  55. action_t action = layer_switch_get_action(event.key);
  56. dprint("ACTION: "); debug_action(action);
  57. #ifndef NO_ACTION_LAYER
  58. dprint(" layer_state: "); layer_debug();
  59. dprint(" default_layer_state: "); default_layer_debug();
  60. #endif
  61. dprintln();
  62. switch (action.kind.id) {
  63. /* Key and Mods */
  64. case ACT_LMODS:
  65. case ACT_RMODS:
  66. {
  67. uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
  68. action.key.mods<<4;
  69. if (event.pressed) {
  70. if (mods) {
  71. add_weak_mods(mods);
  72. send_keyboard_report();
  73. }
  74. register_code(action.key.code);
  75. } else {
  76. unregister_code(action.key.code);
  77. if (mods) {
  78. del_weak_mods(mods);
  79. send_keyboard_report();
  80. }
  81. }
  82. }
  83. break;
  84. #ifndef NO_ACTION_TAPPING
  85. case ACT_LMODS_TAP:
  86. case ACT_RMODS_TAP:
  87. {
  88. uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
  89. action.key.mods<<4;
  90. switch (action.layer_tap.code) {
  91. #ifndef NO_ACTION_ONESHOT
  92. case MODS_ONESHOT:
  93. // Oneshot modifier
  94. if (event.pressed) {
  95. if (tap_count == 0) {
  96. register_mods(mods);
  97. }
  98. else if (tap_count == 1) {
  99. dprint("MODS_TAP: Oneshot: start\n");
  100. set_oneshot_mods(mods);
  101. }
  102. else {
  103. register_mods(mods);
  104. }
  105. } else {
  106. if (tap_count == 0) {
  107. clear_oneshot_mods();
  108. unregister_mods(mods);
  109. }
  110. else if (tap_count == 1) {
  111. // Retain Oneshot mods
  112. }
  113. else {
  114. clear_oneshot_mods();
  115. unregister_mods(mods);
  116. }
  117. }
  118. break;
  119. #endif
  120. case MODS_TAP_TOGGLE:
  121. if (event.pressed) {
  122. if (tap_count <= TAPPING_TOGGLE) {
  123. register_mods(mods);
  124. }
  125. } else {
  126. if (tap_count < TAPPING_TOGGLE) {
  127. unregister_mods(mods);
  128. }
  129. }
  130. break;
  131. default:
  132. if (event.pressed) {
  133. if (tap_count > 0) {
  134. if (record->tap.interrupted) {
  135. dprint("MODS_TAP: Tap: Cancel: add_mods\n");
  136. // ad hoc: set 0 to cancel tap
  137. record->tap.count = 0;
  138. register_mods(mods);
  139. } else {
  140. dprint("MODS_TAP: Tap: register_code\n");
  141. register_code(action.key.code);
  142. }
  143. } else {
  144. dprint("MODS_TAP: No tap: add_mods\n");
  145. register_mods(mods);
  146. }
  147. } else {
  148. if (tap_count > 0) {
  149. dprint("MODS_TAP: Tap: unregister_code\n");
  150. unregister_code(action.key.code);
  151. } else {
  152. dprint("MODS_TAP: No tap: add_mods\n");
  153. unregister_mods(mods);
  154. }
  155. }
  156. break;
  157. }
  158. }
  159. break;
  160. #endif
  161. #ifdef EXTRAKEY_ENABLE
  162. /* other HID usage */
  163. case ACT_USAGE:
  164. switch (action.usage.page) {
  165. case PAGE_SYSTEM:
  166. if (event.pressed) {
  167. host_system_send(action.usage.code);
  168. } else {
  169. host_system_send(0);
  170. }
  171. break;
  172. case PAGE_CONSUMER:
  173. if (event.pressed) {
  174. host_consumer_send(action.usage.code);
  175. } else {
  176. host_consumer_send(0);
  177. }
  178. break;
  179. }
  180. break;
  181. #endif
  182. #ifdef MOUSEKEY_ENABLE
  183. /* Mouse key */
  184. case ACT_MOUSEKEY:
  185. if (event.pressed) {
  186. mousekey_on(action.key.code);
  187. mousekey_send();
  188. } else {
  189. mousekey_off(action.key.code);
  190. mousekey_send();
  191. }
  192. break;
  193. #endif
  194. #ifndef NO_ACTION_LAYER
  195. case ACT_LAYER:
  196. if (action.layer_bitop.on == 0) {
  197. /* Default Layer Bitwise Operation */
  198. if (!event.pressed) {
  199. uint8_t shift = action.layer_bitop.part*4;
  200. uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
  201. uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
  202. switch (action.layer_bitop.op) {
  203. case OP_BIT_AND: default_layer_and(bits | mask); break;
  204. case OP_BIT_OR: default_layer_or(bits | mask); break;
  205. case OP_BIT_XOR: default_layer_xor(bits | mask); break;
  206. case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
  207. }
  208. }
  209. } else {
  210. /* Layer Bitwise Operation */
  211. if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
  212. (action.layer_bitop.on & ON_RELEASE)) {
  213. uint8_t shift = action.layer_bitop.part*4;
  214. uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
  215. uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
  216. switch (action.layer_bitop.op) {
  217. case OP_BIT_AND: layer_and(bits | mask); break;
  218. case OP_BIT_OR: layer_or(bits | mask); break;
  219. case OP_BIT_XOR: layer_xor(bits | mask); break;
  220. case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
  221. }
  222. }
  223. }
  224. break;
  225. #ifndef NO_ACTION_TAPPING
  226. case ACT_LAYER_TAP:
  227. case ACT_LAYER_TAP_EXT:
  228. switch (action.layer_tap.code) {
  229. case 0xe0 ... 0xef:
  230. /* layer On/Off with modifiers(left only) */
  231. if (event.pressed) {
  232. layer_on(action.layer_tap.val);
  233. register_mods(action.layer_tap.code & 0x0f);
  234. } else {
  235. layer_off(action.layer_tap.val);
  236. unregister_mods(action.layer_tap.code & 0x0f);
  237. }
  238. break;
  239. case OP_TAP_TOGGLE:
  240. /* tap toggle */
  241. if (event.pressed) {
  242. if (tap_count < TAPPING_TOGGLE) {
  243. layer_invert(action.layer_tap.val);
  244. }
  245. } else {
  246. if (tap_count <= TAPPING_TOGGLE) {
  247. layer_invert(action.layer_tap.val);
  248. }
  249. }
  250. break;
  251. case OP_ON_OFF:
  252. event.pressed ? layer_on(action.layer_tap.val) :
  253. layer_off(action.layer_tap.val);
  254. break;
  255. case OP_OFF_ON:
  256. event.pressed ? layer_off(action.layer_tap.val) :
  257. layer_on(action.layer_tap.val);
  258. break;
  259. case OP_SET_CLEAR:
  260. event.pressed ? layer_move(action.layer_tap.val) :
  261. layer_clear();
  262. break;
  263. default:
  264. /* tap key */
  265. if (event.pressed) {
  266. if (tap_count > 0) {
  267. dprint("KEYMAP_TAP_KEY: Tap: register_code\n");
  268. register_code(action.layer_tap.code);
  269. } else {
  270. dprint("KEYMAP_TAP_KEY: No tap: On on press\n");
  271. layer_on(action.layer_tap.val);
  272. }
  273. } else {
  274. if (tap_count > 0) {
  275. dprint("KEYMAP_TAP_KEY: Tap: unregister_code\n");
  276. unregister_code(action.layer_tap.code);
  277. } else {
  278. dprint("KEYMAP_TAP_KEY: No tap: Off on release\n");
  279. layer_off(action.layer_tap.val);
  280. }
  281. }
  282. break;
  283. }
  284. break;
  285. #endif
  286. #endif
  287. /* Extentions */
  288. #ifndef NO_ACTION_MACRO
  289. case ACT_MACRO:
  290. action_macro_play(action_get_macro(record, action.func.id, action.func.opt));
  291. break;
  292. #endif
  293. #ifdef BACKLIGHT_ENABLE
  294. case ACT_BACKLIGHT:
  295. if (!event.pressed) {
  296. /* Backwards compatibility */
  297. if (action.backlight.level != 0 && action.backlight.opt != BACKLIGHT_LEVEL) {
  298. action.backlight.opt = action.backlight.level;
  299. }
  300. switch (action.backlight.opt) {
  301. case BACKLIGHT_INCREASE:
  302. backlight_increase();
  303. break;
  304. case BACKLIGHT_DECREASE:
  305. backlight_decrease();
  306. break;
  307. case BACKLIGHT_TOGGLE:
  308. backlight_toggle();
  309. break;
  310. case BACKLIGHT_STEP:
  311. backlight_step();
  312. break;
  313. case BACKLIGHT_LEVEL:
  314. backlight_level(action.backlight.level);
  315. break;
  316. }
  317. }
  318. break;
  319. #endif
  320. case ACT_COMMAND:
  321. break;
  322. #ifndef NO_ACTION_FUNCTION
  323. case ACT_FUNCTION:
  324. action_function(record, action.func.id, action.func.opt);
  325. break;
  326. #endif
  327. default:
  328. break;
  329. }
  330. }
  331. /*
  332. * Utilities for actions.
  333. */
  334. void register_code(uint8_t code)
  335. {
  336. if (code == KC_NO) {
  337. return;
  338. }
  339. #ifdef LOCKING_SUPPORT_ENABLE
  340. else if (KC_LOCKING_CAPS == code) {
  341. #ifdef LOCKING_RESYNC_ENABLE
  342. // Resync: ignore if caps lock already is on
  343. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) return;
  344. #endif
  345. add_key(KC_CAPSLOCK);
  346. send_keyboard_report();
  347. del_key(KC_CAPSLOCK);
  348. send_keyboard_report();
  349. }
  350. else if (KC_LOCKING_NUM == code) {
  351. #ifdef LOCKING_RESYNC_ENABLE
  352. if (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) return;
  353. #endif
  354. add_key(KC_NUMLOCK);
  355. send_keyboard_report();
  356. del_key(KC_NUMLOCK);
  357. send_keyboard_report();
  358. }
  359. else if (KC_LOCKING_SCROLL == code) {
  360. #ifdef LOCKING_RESYNC_ENABLE
  361. if (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) return;
  362. #endif
  363. add_key(KC_SCROLLLOCK);
  364. send_keyboard_report();
  365. del_key(KC_SCROLLLOCK);
  366. send_keyboard_report();
  367. }
  368. #endif
  369. else if IS_KEY(code) {
  370. // TODO: should push command_proc out of this block?
  371. if (command_proc(code)) return;
  372. #ifndef NO_ACTION_ONESHOT
  373. /* TODO: remove
  374. if (oneshot_state.mods && !oneshot_state.disabled) {
  375. uint8_t tmp_mods = get_mods();
  376. add_mods(oneshot_state.mods);
  377. add_key(code);
  378. send_keyboard_report();
  379. set_mods(tmp_mods);
  380. send_keyboard_report();
  381. oneshot_cancel();
  382. } else
  383. */
  384. #endif
  385. {
  386. add_key(code);
  387. send_keyboard_report();
  388. }
  389. }
  390. else if IS_MOD(code) {
  391. add_mods(MOD_BIT(code));
  392. send_keyboard_report();
  393. }
  394. else if IS_SYSTEM(code) {
  395. host_system_send(KEYCODE2SYSTEM(code));
  396. }
  397. else if IS_CONSUMER(code) {
  398. host_consumer_send(KEYCODE2CONSUMER(code));
  399. }
  400. }
  401. void unregister_code(uint8_t code)
  402. {
  403. if (code == KC_NO) {
  404. return;
  405. }
  406. #ifdef LOCKING_SUPPORT_ENABLE
  407. else if (KC_LOCKING_CAPS == code) {
  408. #ifdef LOCKING_RESYNC_ENABLE
  409. // Resync: ignore if caps lock already is off
  410. if (!(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) return;
  411. #endif
  412. add_key(KC_CAPSLOCK);
  413. send_keyboard_report();
  414. del_key(KC_CAPSLOCK);
  415. send_keyboard_report();
  416. }
  417. else if (KC_LOCKING_NUM == code) {
  418. #ifdef LOCKING_RESYNC_ENABLE
  419. if (!(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK))) return;
  420. #endif
  421. add_key(KC_NUMLOCK);
  422. send_keyboard_report();
  423. del_key(KC_NUMLOCK);
  424. send_keyboard_report();
  425. }
  426. else if (KC_LOCKING_SCROLL == code) {
  427. #ifdef LOCKING_RESYNC_ENABLE
  428. if (!(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK))) return;
  429. #endif
  430. add_key(KC_SCROLLLOCK);
  431. send_keyboard_report();
  432. del_key(KC_SCROLLLOCK);
  433. send_keyboard_report();
  434. }
  435. #endif
  436. else if IS_KEY(code) {
  437. del_key(code);
  438. send_keyboard_report();
  439. }
  440. else if IS_MOD(code) {
  441. del_mods(MOD_BIT(code));
  442. send_keyboard_report();
  443. }
  444. else if IS_SYSTEM(code) {
  445. host_system_send(0);
  446. }
  447. else if IS_CONSUMER(code) {
  448. host_consumer_send(0);
  449. }
  450. }
  451. void register_mods(uint8_t mods)
  452. {
  453. if (mods) {
  454. add_mods(mods);
  455. send_keyboard_report();
  456. }
  457. }
  458. void unregister_mods(uint8_t mods)
  459. {
  460. if (mods) {
  461. del_mods(mods);
  462. send_keyboard_report();
  463. }
  464. }
  465. void clear_keyboard(void)
  466. {
  467. clear_mods();
  468. clear_keyboard_but_mods();
  469. }
  470. void clear_keyboard_but_mods(void)
  471. {
  472. clear_weak_mods();
  473. clear_keys();
  474. send_keyboard_report();
  475. #ifdef MOUSEKEY_ENABLE
  476. mousekey_clear();
  477. mousekey_send();
  478. #endif
  479. #ifdef EXTRAKEY_ENABLE
  480. host_system_send(0);
  481. host_consumer_send(0);
  482. #endif
  483. }
  484. bool is_tap_key(keypos_t key)
  485. {
  486. action_t action = layer_switch_get_action(key);
  487. switch (action.kind.id) {
  488. case ACT_LMODS_TAP:
  489. case ACT_RMODS_TAP:
  490. case ACT_LAYER_TAP:
  491. case ACT_LAYER_TAP_EXT:
  492. return true;
  493. case ACT_MACRO:
  494. case ACT_FUNCTION:
  495. if (action.func.opt & FUNC_TAP) { return true; }
  496. return false;
  497. }
  498. return false;
  499. }
  500. /*
  501. * debug print
  502. */
  503. void debug_event(keyevent_t event)
  504. {
  505. dprintf("%04X%c(%u)", (event.key.row<<8 | event.key.col), (event.pressed ? 'd' : 'u'), event.time);
  506. }
  507. void debug_record(keyrecord_t record)
  508. {
  509. debug_event(record.event);
  510. #ifndef NO_ACTION_TAPPING
  511. dprintf(":%u%c", record.tap.count, (record.tap.interrupted ? '-' : ' '));
  512. #endif
  513. }
  514. void debug_action(action_t action)
  515. {
  516. switch (action.kind.id) {
  517. case ACT_LMODS: dprint("ACT_LMODS"); break;
  518. case ACT_RMODS: dprint("ACT_RMODS"); break;
  519. case ACT_LMODS_TAP: dprint("ACT_LMODS_TAP"); break;
  520. case ACT_RMODS_TAP: dprint("ACT_RMODS_TAP"); break;
  521. case ACT_USAGE: dprint("ACT_USAGE"); break;
  522. case ACT_MOUSEKEY: dprint("ACT_MOUSEKEY"); break;
  523. case ACT_LAYER: dprint("ACT_LAYER"); break;
  524. case ACT_LAYER_TAP: dprint("ACT_LAYER_TAP"); break;
  525. case ACT_LAYER_TAP_EXT: dprint("ACT_LAYER_TAP_EXT"); break;
  526. case ACT_MACRO: dprint("ACT_MACRO"); break;
  527. case ACT_COMMAND: dprint("ACT_COMMAND"); break;
  528. case ACT_FUNCTION: dprint("ACT_FUNCTION"); break;
  529. default: dprint("UNKNOWN"); break;
  530. }
  531. dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff);
  532. }
  533. __attribute__ ((weak))
  534. void action_keyevent(keyevent_t event)
  535. {
  536. }