process_combo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /* Copyright 2016 Jack Humbert
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "process_combo.h"
  17. #include <stddef.h>
  18. #include "process_auto_shift.h"
  19. #include "caps_word.h"
  20. #include "timer.h"
  21. #include "wait.h"
  22. #include "keyboard.h"
  23. #include "keymap_common.h"
  24. #include "action_layer.h"
  25. #include "action_tapping.h"
  26. #include "action_util.h"
  27. #include "keymap_introspection.h"
  28. __attribute__((weak)) void process_combo_event(uint16_t combo_index, bool pressed) {}
  29. #ifndef COMBO_ONLY_FROM_LAYER
  30. __attribute__((weak)) uint8_t combo_ref_from_layer(uint8_t layer) {
  31. return layer;
  32. }
  33. #endif
  34. #ifdef COMBO_MUST_HOLD_PER_COMBO
  35. __attribute__((weak)) bool get_combo_must_hold(uint16_t combo_index, combo_t *combo) {
  36. return false;
  37. }
  38. #endif
  39. #ifdef COMBO_MUST_TAP_PER_COMBO
  40. __attribute__((weak)) bool get_combo_must_tap(uint16_t combo_index, combo_t *combo) {
  41. return false;
  42. }
  43. #endif
  44. #ifdef COMBO_TERM_PER_COMBO
  45. __attribute__((weak)) uint16_t get_combo_term(uint16_t combo_index, combo_t *combo) {
  46. return COMBO_TERM;
  47. }
  48. #endif
  49. #ifdef COMBO_MUST_PRESS_IN_ORDER_PER_COMBO
  50. __attribute__((weak)) bool get_combo_must_press_in_order(uint16_t combo_index, combo_t *combo) {
  51. return true;
  52. }
  53. #endif
  54. #ifdef COMBO_PROCESS_KEY_RELEASE
  55. __attribute__((weak)) bool process_combo_key_release(uint16_t combo_index, combo_t *combo, uint8_t key_index, uint16_t keycode) {
  56. return false;
  57. }
  58. #endif
  59. #ifdef COMBO_PROCESS_KEY_REPRESS
  60. __attribute__((weak)) bool process_combo_key_repress(uint16_t combo_index, combo_t *combo, uint8_t key_index, uint16_t keycode) {
  61. return false;
  62. }
  63. #endif
  64. #ifdef COMBO_SHOULD_TRIGGER
  65. __attribute__((weak)) bool combo_should_trigger(uint16_t combo_index, combo_t *combo, uint16_t keycode, keyrecord_t *record) {
  66. return true;
  67. }
  68. #endif
  69. typedef enum { COMBO_KEY_NOT_PRESSED, COMBO_KEY_PRESSED, COMBO_KEY_REPRESSED } combo_key_action_t;
  70. #ifndef COMBO_NO_TIMER
  71. static uint16_t timer = 0;
  72. #endif
  73. static bool b_combo_enable = true; // defaults to enabled
  74. static uint16_t longest_term = 0;
  75. typedef struct {
  76. keyrecord_t record;
  77. uint16_t combo_index;
  78. uint16_t keycode;
  79. } queued_record_t;
  80. static uint8_t key_buffer_size = 0;
  81. static queued_record_t key_buffer[COMBO_KEY_BUFFER_LENGTH];
  82. typedef struct {
  83. uint16_t combo_index;
  84. } queued_combo_t;
  85. static uint8_t combo_buffer_write = 0;
  86. static uint8_t combo_buffer_read = 0;
  87. static queued_combo_t combo_buffer[COMBO_BUFFER_LENGTH];
  88. #define INCREMENT_MOD(i) i = (i + 1) % COMBO_BUFFER_LENGTH
  89. #ifndef EXTRA_SHORT_COMBOS
  90. /* flags are their own elements in combo_t struct. */
  91. # define COMBO_ACTIVE(combo) (combo->active)
  92. # define COMBO_DISABLED(combo) (combo->disabled)
  93. # define COMBO_STATE(combo) (combo->state)
  94. # define ACTIVATE_COMBO(combo) \
  95. do { \
  96. combo->active = true; \
  97. } while (0)
  98. # define DEACTIVATE_COMBO(combo) \
  99. do { \
  100. combo->active = false; \
  101. } while (0)
  102. # define DISABLE_COMBO(combo) \
  103. do { \
  104. combo->disabled = true; \
  105. } while (0)
  106. # define RESET_COMBO_STATE(combo) \
  107. do { \
  108. combo->disabled = false; \
  109. combo->state = 0; \
  110. } while (0)
  111. #else
  112. /* flags are at the two high bits of state. */
  113. # define COMBO_ACTIVE(combo) (combo->state & 0x80)
  114. # define COMBO_DISABLED(combo) (combo->state & 0x40)
  115. # define COMBO_STATE(combo) (combo->state & 0x3F)
  116. # define ACTIVATE_COMBO(combo) \
  117. do { \
  118. combo->state |= 0x80; \
  119. } while (0)
  120. # define DEACTIVATE_COMBO(combo) \
  121. do { \
  122. combo->state &= ~0x80; \
  123. } while (0)
  124. # define DISABLE_COMBO(combo) \
  125. do { \
  126. combo->state |= 0x40; \
  127. } while (0)
  128. # define RESET_COMBO_STATE(combo) \
  129. do { \
  130. combo->state &= ~0x7F; \
  131. } while (0)
  132. #endif
  133. static inline void release_combo(uint16_t combo_index, combo_t *combo) {
  134. if (combo->keycode) {
  135. keyrecord_t record = {
  136. .event = MAKE_COMBOEVENT(false),
  137. .keycode = combo->keycode,
  138. };
  139. #ifndef NO_ACTION_TAPPING
  140. action_tapping_process(record);
  141. #else
  142. process_record(&record);
  143. #endif
  144. } else {
  145. process_combo_event(combo_index, false);
  146. }
  147. DEACTIVATE_COMBO(combo);
  148. }
  149. static inline bool _get_combo_must_hold(uint16_t combo_index, combo_t *combo) {
  150. #ifdef COMBO_NO_TIMER
  151. return false;
  152. #elif defined(COMBO_MUST_HOLD_PER_COMBO)
  153. return get_combo_must_hold(combo_index, combo);
  154. #elif defined(COMBO_MUST_HOLD_MODS)
  155. return (KEYCODE_IS_MOD(combo->keycode) || (combo->keycode >= QK_MOMENTARY && combo->keycode <= QK_MOMENTARY_MAX));
  156. #endif
  157. return false;
  158. }
  159. static inline uint16_t _get_wait_time(uint16_t combo_index, combo_t *combo) {
  160. if (_get_combo_must_hold(combo_index, combo)
  161. #ifdef COMBO_MUST_TAP_PER_COMBO
  162. || get_combo_must_tap(combo_index, combo)
  163. #endif
  164. ) {
  165. if (longest_term < COMBO_HOLD_TERM) {
  166. return COMBO_HOLD_TERM;
  167. }
  168. }
  169. return longest_term;
  170. }
  171. static inline uint16_t _get_combo_term(uint16_t combo_index, combo_t *combo) {
  172. #if defined(COMBO_TERM_PER_COMBO)
  173. return get_combo_term(combo_index, combo);
  174. #endif
  175. return COMBO_TERM;
  176. }
  177. void clear_combos(void) {
  178. longest_term = 0;
  179. for (uint16_t index = 0; index < combo_count(); ++index) {
  180. combo_t *combo = combo_get(index);
  181. if (!COMBO_ACTIVE(combo)) {
  182. RESET_COMBO_STATE(combo);
  183. }
  184. }
  185. }
  186. static inline void dump_key_buffer(void) {
  187. /* First call start from 0 index; recursive calls need to start from i+1 index */
  188. static uint8_t key_buffer_next = 0;
  189. #if TAP_CODE_DELAY > 0
  190. bool delay_done = false;
  191. #endif
  192. if (key_buffer_size == 0) {
  193. return;
  194. }
  195. for (uint8_t key_buffer_i = key_buffer_next; key_buffer_i < key_buffer_size; key_buffer_i++) {
  196. key_buffer_next = key_buffer_i + 1;
  197. queued_record_t *qrecord = &key_buffer[key_buffer_i];
  198. keyrecord_t *record = &qrecord->record;
  199. if (IS_NOEVENT(record->event)) {
  200. continue;
  201. }
  202. if (!record->keycode && qrecord->combo_index != (uint16_t)-1) {
  203. process_combo_event(qrecord->combo_index, true);
  204. } else {
  205. #ifndef NO_ACTION_TAPPING
  206. action_tapping_process(*record);
  207. #else
  208. process_record(record);
  209. #endif
  210. }
  211. record->event.type = TICK_EVENT;
  212. #if defined(CAPS_WORD_ENABLE) && defined(AUTO_SHIFT_ENABLE)
  213. // Edge case: preserve the weak Left Shift mod if both Caps Word and
  214. // Auto Shift are on. Caps Word capitalizes by setting the weak Left
  215. // Shift mod during the press event, but Auto Shift doesn't send the
  216. // key until it receives the release event.
  217. del_weak_mods((is_caps_word_on() && get_autoshift_state()) ? ~MOD_BIT(KC_LSFT) : 0xff);
  218. #else
  219. clear_weak_mods();
  220. #endif // defined(CAPS_WORD_ENABLE) && defined(AUTO_SHIFT_ENABLE)
  221. #if TAP_CODE_DELAY > 0
  222. // only delay once and for a non-tapping key
  223. if (!delay_done && !is_tap_record(record)) {
  224. delay_done = true;
  225. wait_ms(TAP_CODE_DELAY);
  226. }
  227. #endif
  228. }
  229. key_buffer_next = key_buffer_size = 0;
  230. }
  231. #define NO_COMBO_KEYS_ARE_DOWN (0 == COMBO_STATE(combo))
  232. #define ALL_COMBO_KEYS_ARE_DOWN(state, key_count) (((1 << key_count) - 1) == state)
  233. #define ONLY_ONE_KEY_IS_DOWN(state) !(state & (state - 1))
  234. #define KEY_NOT_YET_RELEASED(state, key_index) ((1 << key_index) & state)
  235. #define KEY_STATE_DOWN(state, key_index) \
  236. do { \
  237. state |= (1 << key_index); \
  238. } while (0)
  239. #define KEY_STATE_UP(state, key_index) \
  240. do { \
  241. state &= ~(1 << key_index); \
  242. } while (0)
  243. static inline void _find_key_index_and_count(const uint16_t *keys, uint16_t keycode, uint16_t *key_index, uint8_t *key_count) {
  244. while (true) {
  245. uint16_t key = pgm_read_word(&keys[*key_count]);
  246. if (keycode == key) *key_index = *key_count;
  247. if (COMBO_END == key) break;
  248. (*key_count)++;
  249. }
  250. }
  251. void drop_combo_from_buffer(uint16_t combo_index) {
  252. /* Mark a combo as processed from the buffer. If the buffer is in the
  253. * beginning of the buffer, drop it. */
  254. uint8_t i = combo_buffer_read;
  255. while (i != combo_buffer_write) {
  256. queued_combo_t *qcombo = &combo_buffer[i];
  257. if (qcombo->combo_index == combo_index) {
  258. combo_t *combo = combo_get(combo_index);
  259. DISABLE_COMBO(combo);
  260. if (i == combo_buffer_read) {
  261. INCREMENT_MOD(combo_buffer_read);
  262. }
  263. break;
  264. }
  265. INCREMENT_MOD(i);
  266. }
  267. }
  268. void apply_combo(uint16_t combo_index, combo_t *combo) {
  269. /* Apply combo's result keycode to the last chord key of the combo and
  270. * disable the other keys. */
  271. if (COMBO_DISABLED(combo)) {
  272. return;
  273. }
  274. // state to check against so we find the last key of the combo from the buffer
  275. #if defined(EXTRA_EXTRA_LONG_COMBOS)
  276. uint32_t state = 0;
  277. #elif defined(EXTRA_LONG_COMBOS)
  278. uint16_t state = 0;
  279. #else
  280. uint8_t state = 0;
  281. #endif
  282. for (uint8_t key_buffer_i = 0; key_buffer_i < key_buffer_size; key_buffer_i++) {
  283. queued_record_t *qrecord = &key_buffer[key_buffer_i];
  284. keyrecord_t *record = &qrecord->record;
  285. uint16_t keycode = qrecord->keycode;
  286. uint8_t key_count = 0;
  287. uint16_t key_index = -1;
  288. _find_key_index_and_count(combo->keys, keycode, &key_index, &key_count);
  289. if (-1 == (int16_t)key_index) {
  290. // key not part of this combo
  291. continue;
  292. }
  293. KEY_STATE_DOWN(state, key_index);
  294. if (ALL_COMBO_KEYS_ARE_DOWN(state, key_count)) {
  295. // this in the end executes the combo when the key_buffer is dumped.
  296. record->keycode = combo->keycode;
  297. record->event.type = COMBO_EVENT;
  298. record->event.key = MAKE_KEYPOS(0, 0);
  299. qrecord->combo_index = combo_index;
  300. ACTIVATE_COMBO(combo);
  301. if (key_count == 1) {
  302. release_combo(combo_index, combo);
  303. }
  304. break;
  305. } else {
  306. // key was part of the combo but not the last one, "disable" it
  307. // by making it a TICK event.
  308. record->event.type = TICK_EVENT;
  309. }
  310. }
  311. drop_combo_from_buffer(combo_index);
  312. }
  313. static inline void apply_combos(void) {
  314. // Apply all buffered normal combos.
  315. for (uint8_t i = combo_buffer_read; i != combo_buffer_write; INCREMENT_MOD(i)) {
  316. queued_combo_t *buffered_combo = &combo_buffer[i];
  317. combo_t *combo = combo_get(buffered_combo->combo_index);
  318. #ifdef COMBO_MUST_TAP_PER_COMBO
  319. if (get_combo_must_tap(buffered_combo->combo_index, combo)) {
  320. // Tap-only combos are applied on key release only, so let's drop 'em here.
  321. drop_combo_from_buffer(buffered_combo->combo_index);
  322. continue;
  323. }
  324. #endif
  325. apply_combo(buffered_combo->combo_index, combo);
  326. }
  327. dump_key_buffer();
  328. clear_combos();
  329. }
  330. combo_t *overlaps(combo_t *combo1, combo_t *combo2) {
  331. /* Checks if the combos overlap and returns the combo that should be
  332. * dropped from the combo buffer.
  333. * The combo that has less keys will be dropped. If they have the same
  334. * amount of keys, drop combo1. */
  335. uint8_t idx1 = 0, idx2 = 0;
  336. uint16_t key1, key2;
  337. bool overlaps = false;
  338. while ((key1 = pgm_read_word(&combo1->keys[idx1])) != COMBO_END) {
  339. idx2 = 0;
  340. while ((key2 = pgm_read_word(&combo2->keys[idx2])) != COMBO_END) {
  341. if (key1 == key2) {
  342. overlaps = true;
  343. }
  344. idx2 += 1;
  345. }
  346. idx1 += 1;
  347. }
  348. if (!overlaps) return NULL;
  349. if (idx2 < idx1) return combo2;
  350. return combo1;
  351. }
  352. #if defined(COMBO_MUST_PRESS_IN_ORDER) || defined(COMBO_MUST_PRESS_IN_ORDER_PER_COMBO)
  353. static bool keys_pressed_in_order(uint16_t combo_index, combo_t *combo, uint16_t key_index, uint16_t keycode, keyrecord_t *record) {
  354. # ifdef COMBO_MUST_PRESS_IN_ORDER_PER_COMBO
  355. if (!get_combo_must_press_in_order(combo_index, combo)) {
  356. return true;
  357. }
  358. # endif
  359. if (
  360. // The `state` bit for the key being pressed.
  361. (1 << key_index) ==
  362. // The *next* combo key's bit.
  363. (COMBO_STATE(combo) + 1)
  364. // E.g. two keys already pressed: `state == 11`.
  365. // Next possible `state` is `111`.
  366. // So the needed bit is `100` which we get with `11 + 1`.
  367. ) {
  368. return true;
  369. }
  370. return false;
  371. }
  372. #endif
  373. static combo_key_action_t process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record, uint16_t combo_index) {
  374. uint8_t key_count = 0;
  375. uint16_t key_index = -1;
  376. _find_key_index_and_count(combo->keys, keycode, &key_index, &key_count);
  377. /* Continue processing if key isn't part of current combo. */
  378. if (-1 == (int16_t)key_index) {
  379. return COMBO_KEY_NOT_PRESSED;
  380. }
  381. bool key_is_part_of_combo = (!COMBO_DISABLED(combo) && is_combo_enabled()
  382. #if defined(COMBO_MUST_PRESS_IN_ORDER) || defined(COMBO_MUST_PRESS_IN_ORDER_PER_COMBO)
  383. && keys_pressed_in_order(combo_index, combo, key_index, keycode, record)
  384. #endif
  385. #ifdef COMBO_SHOULD_TRIGGER
  386. && combo_should_trigger(combo_index, combo, keycode, record)
  387. #endif
  388. );
  389. if (record->event.pressed && key_is_part_of_combo) {
  390. uint16_t time = _get_combo_term(combo_index, combo);
  391. if (!COMBO_ACTIVE(combo)) {
  392. KEY_STATE_DOWN(combo->state, key_index);
  393. if (longest_term < time) {
  394. longest_term = time;
  395. }
  396. }
  397. if (ALL_COMBO_KEYS_ARE_DOWN(COMBO_STATE(combo), key_count)) {
  398. /* Combo was fully pressed */
  399. /* Buffer the combo so we can fire it after COMBO_TERM */
  400. #ifndef COMBO_NO_TIMER
  401. /* Don't buffer this combo if its combo term has passed. */
  402. if (timer && timer_elapsed(timer) > time) {
  403. DISABLE_COMBO(combo);
  404. return COMBO_KEY_PRESSED;
  405. } else
  406. #endif
  407. {
  408. // disable readied combos that overlap with this combo
  409. combo_t *drop = NULL;
  410. for (uint8_t combo_buffer_i = combo_buffer_read; combo_buffer_i != combo_buffer_write; INCREMENT_MOD(combo_buffer_i)) {
  411. queued_combo_t *qcombo = &combo_buffer[combo_buffer_i];
  412. combo_t *buffered_combo = combo_get(qcombo->combo_index);
  413. if ((drop = overlaps(buffered_combo, combo))) {
  414. DISABLE_COMBO(drop);
  415. if (drop == combo) {
  416. // stop checking for overlaps if dropped combo was current combo.
  417. break;
  418. } else if (combo_buffer_i == combo_buffer_read && drop == buffered_combo) {
  419. /* Drop the disabled buffered combo from the buffer if
  420. * it is in the beginning of the buffer. */
  421. INCREMENT_MOD(combo_buffer_read);
  422. }
  423. }
  424. }
  425. if (drop != combo) {
  426. // save this combo to buffer
  427. combo_buffer[combo_buffer_write] = (queued_combo_t){
  428. .combo_index = combo_index,
  429. };
  430. INCREMENT_MOD(combo_buffer_write);
  431. // get possible longer waiting time for tap-/hold-only combos.
  432. longest_term = _get_wait_time(combo_index, combo);
  433. }
  434. } // if timer elapsed end
  435. }
  436. #ifdef COMBO_PROCESS_KEY_REPRESS
  437. } else if (record->event.pressed) {
  438. if (COMBO_ACTIVE(combo)) {
  439. if (process_combo_key_repress(combo_index, combo, key_index, keycode)) {
  440. KEY_STATE_DOWN(combo->state, key_index);
  441. return COMBO_KEY_REPRESSED;
  442. }
  443. }
  444. #endif
  445. } else {
  446. // chord releases
  447. if (!COMBO_ACTIVE(combo) && ALL_COMBO_KEYS_ARE_DOWN(COMBO_STATE(combo), key_count)) {
  448. /* First key quickly released */
  449. if (COMBO_DISABLED(combo) || _get_combo_must_hold(combo_index, combo)) {
  450. // combo wasn't tappable, disable it and drop it from buffer.
  451. drop_combo_from_buffer(combo_index);
  452. key_is_part_of_combo = false;
  453. }
  454. #ifdef COMBO_MUST_TAP_PER_COMBO
  455. else if (get_combo_must_tap(combo_index, combo)) {
  456. // immediately apply tap-only combo
  457. apply_combo(combo_index, combo);
  458. apply_combos(); // also apply other prepared combos and dump key buffer
  459. # ifdef COMBO_PROCESS_KEY_RELEASE
  460. if (process_combo_key_release(combo_index, combo, key_index, keycode)) {
  461. release_combo(combo_index, combo);
  462. }
  463. # endif
  464. }
  465. #endif
  466. } else if (COMBO_ACTIVE(combo) && ONLY_ONE_KEY_IS_DOWN(COMBO_STATE(combo)) && KEY_NOT_YET_RELEASED(COMBO_STATE(combo), key_index)) {
  467. /* last key released */
  468. release_combo(combo_index, combo);
  469. key_is_part_of_combo = true;
  470. #ifdef COMBO_PROCESS_KEY_RELEASE
  471. process_combo_key_release(combo_index, combo, key_index, keycode);
  472. #endif
  473. } else if (COMBO_ACTIVE(combo) && KEY_NOT_YET_RELEASED(COMBO_STATE(combo), key_index)) {
  474. /* first or middle key released */
  475. key_is_part_of_combo = true;
  476. #ifdef COMBO_PROCESS_KEY_RELEASE
  477. if (process_combo_key_release(combo_index, combo, key_index, keycode)) {
  478. release_combo(combo_index, combo);
  479. }
  480. #endif
  481. } else {
  482. /* The released key was part of an incomplete combo */
  483. key_is_part_of_combo = false;
  484. }
  485. KEY_STATE_UP(combo->state, key_index);
  486. }
  487. return key_is_part_of_combo ? COMBO_KEY_PRESSED : COMBO_KEY_NOT_PRESSED;
  488. }
  489. bool process_combo(uint16_t keycode, keyrecord_t *record) {
  490. uint8_t is_combo_key = COMBO_KEY_NOT_PRESSED;
  491. if (keycode == QK_COMBO_ON && record->event.pressed) {
  492. combo_enable();
  493. return true;
  494. }
  495. if (keycode == QK_COMBO_OFF && record->event.pressed) {
  496. combo_disable();
  497. return true;
  498. }
  499. if (keycode == QK_COMBO_TOGGLE && record->event.pressed) {
  500. combo_toggle();
  501. return true;
  502. }
  503. #ifdef COMBO_ONLY_FROM_LAYER
  504. /* Only check keycodes from one layer. */
  505. keycode = keymap_key_to_keycode(COMBO_ONLY_FROM_LAYER, record->event.key);
  506. #else
  507. uint8_t highest_layer = get_highest_layer(layer_state | default_layer_state);
  508. uint8_t ref_layer = combo_ref_from_layer(highest_layer);
  509. if (ref_layer != highest_layer) {
  510. keycode = keymap_key_to_keycode(ref_layer, record->event.key);
  511. }
  512. #endif
  513. for (uint16_t idx = 0; idx < combo_count(); ++idx) {
  514. combo_t *combo = combo_get(idx);
  515. is_combo_key |= process_single_combo(combo, keycode, record, idx);
  516. }
  517. if (record->event.pressed && is_combo_key) {
  518. #ifndef COMBO_NO_TIMER
  519. # ifdef COMBO_STRICT_TIMER
  520. if (!timer) {
  521. // timer is set only on the first key
  522. timer = timer_read();
  523. }
  524. # else
  525. timer = timer_read();
  526. # endif
  527. #endif
  528. #ifdef COMBO_PROCESS_KEY_REPRESS
  529. if (is_combo_key == COMBO_KEY_PRESSED)
  530. #endif
  531. {
  532. if (key_buffer_size < COMBO_KEY_BUFFER_LENGTH) {
  533. key_buffer[key_buffer_size++] = (queued_record_t){
  534. .record = *record,
  535. .keycode = keycode,
  536. .combo_index = -1, // this will be set when applying combos
  537. };
  538. }
  539. }
  540. } else {
  541. if (combo_buffer_read != combo_buffer_write) {
  542. // some combo is prepared
  543. apply_combos();
  544. } else {
  545. // reset state if there are no combo keys pressed at all
  546. dump_key_buffer();
  547. #ifndef COMBO_NO_TIMER
  548. timer = 0;
  549. #endif
  550. clear_combos();
  551. }
  552. }
  553. return !is_combo_key;
  554. }
  555. void combo_task(void) {
  556. if (!b_combo_enable) {
  557. return;
  558. }
  559. #ifndef COMBO_NO_TIMER
  560. if (timer && timer_elapsed(timer) > longest_term) {
  561. if (combo_buffer_read != combo_buffer_write) {
  562. apply_combos();
  563. longest_term = 0;
  564. } else {
  565. dump_key_buffer();
  566. clear_combos();
  567. }
  568. timer = 0;
  569. }
  570. #endif
  571. }
  572. void combo_enable(void) {
  573. b_combo_enable = true;
  574. }
  575. void combo_disable(void) {
  576. #ifndef COMBO_NO_TIMER
  577. timer = 0;
  578. #endif
  579. b_combo_enable = false;
  580. combo_buffer_read = combo_buffer_write;
  581. clear_combos();
  582. dump_key_buffer();
  583. }
  584. void combo_toggle(void) {
  585. if (b_combo_enable) {
  586. combo_disable();
  587. } else {
  588. combo_enable();
  589. }
  590. }
  591. bool is_combo_enabled(void) {
  592. return b_combo_enable;
  593. }