process_combo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. uint16_t index = 0;
  179. longest_term = 0;
  180. for (index = 0; index < combo_count(); ++index) {
  181. combo_t *combo = combo_get(index);
  182. if (!COMBO_ACTIVE(combo)) {
  183. RESET_COMBO_STATE(combo);
  184. }
  185. }
  186. }
  187. static inline void dump_key_buffer(void) {
  188. /* First call start from 0 index; recursive calls need to start from i+1 index */
  189. static uint8_t key_buffer_next = 0;
  190. #if TAP_CODE_DELAY > 0
  191. bool delay_done = false;
  192. #endif
  193. if (key_buffer_size == 0) {
  194. return;
  195. }
  196. for (uint8_t key_buffer_i = key_buffer_next; key_buffer_i < key_buffer_size; key_buffer_i++) {
  197. key_buffer_next = key_buffer_i + 1;
  198. queued_record_t *qrecord = &key_buffer[key_buffer_i];
  199. keyrecord_t *record = &qrecord->record;
  200. if (IS_NOEVENT(record->event)) {
  201. continue;
  202. }
  203. if (!record->keycode && qrecord->combo_index != (uint16_t)-1) {
  204. process_combo_event(qrecord->combo_index, true);
  205. } else {
  206. #ifndef NO_ACTION_TAPPING
  207. action_tapping_process(*record);
  208. #else
  209. process_record(record);
  210. #endif
  211. }
  212. record->event.type = TICK_EVENT;
  213. #if defined(CAPS_WORD_ENABLE) && defined(AUTO_SHIFT_ENABLE)
  214. // Edge case: preserve the weak Left Shift mod if both Caps Word and
  215. // Auto Shift are on. Caps Word capitalizes by setting the weak Left
  216. // Shift mod during the press event, but Auto Shift doesn't send the
  217. // key until it receives the release event.
  218. del_weak_mods((is_caps_word_on() && get_autoshift_state()) ? ~MOD_BIT(KC_LSFT) : 0xff);
  219. #else
  220. clear_weak_mods();
  221. #endif // defined(CAPS_WORD_ENABLE) && defined(AUTO_SHIFT_ENABLE)
  222. #if TAP_CODE_DELAY > 0
  223. // only delay once and for a non-tapping key
  224. if (!delay_done && !is_tap_record(record)) {
  225. delay_done = true;
  226. wait_ms(TAP_CODE_DELAY);
  227. }
  228. #endif
  229. }
  230. key_buffer_next = key_buffer_size = 0;
  231. }
  232. #define NO_COMBO_KEYS_ARE_DOWN (0 == COMBO_STATE(combo))
  233. #define ALL_COMBO_KEYS_ARE_DOWN(state, key_count) (((1 << key_count) - 1) == state)
  234. #define ONLY_ONE_KEY_IS_DOWN(state) !(state & (state - 1))
  235. #define KEY_NOT_YET_RELEASED(state, key_index) ((1 << key_index) & state)
  236. #define KEY_STATE_DOWN(state, key_index) \
  237. do { \
  238. state |= (1 << key_index); \
  239. } while (0)
  240. #define KEY_STATE_UP(state, key_index) \
  241. do { \
  242. state &= ~(1 << key_index); \
  243. } while (0)
  244. static inline void _find_key_index_and_count(const uint16_t *keys, uint16_t keycode, uint16_t *key_index, uint8_t *key_count) {
  245. while (true) {
  246. uint16_t key = pgm_read_word(&keys[*key_count]);
  247. if (keycode == key) *key_index = *key_count;
  248. if (COMBO_END == key) break;
  249. (*key_count)++;
  250. }
  251. }
  252. void drop_combo_from_buffer(uint16_t combo_index) {
  253. /* Mark a combo as processed from the buffer. If the buffer is in the
  254. * beginning of the buffer, drop it. */
  255. uint8_t i = combo_buffer_read;
  256. while (i != combo_buffer_write) {
  257. queued_combo_t *qcombo = &combo_buffer[i];
  258. if (qcombo->combo_index == combo_index) {
  259. combo_t *combo = combo_get(combo_index);
  260. DISABLE_COMBO(combo);
  261. if (i == combo_buffer_read) {
  262. INCREMENT_MOD(combo_buffer_read);
  263. }
  264. break;
  265. }
  266. INCREMENT_MOD(i);
  267. }
  268. }
  269. void apply_combo(uint16_t combo_index, combo_t *combo) {
  270. /* Apply combo's result keycode to the last chord key of the combo and
  271. * disable the other keys. */
  272. if (COMBO_DISABLED(combo)) {
  273. return;
  274. }
  275. // state to check against so we find the last key of the combo from the buffer
  276. #if defined(EXTRA_EXTRA_LONG_COMBOS)
  277. uint32_t state = 0;
  278. #elif defined(EXTRA_LONG_COMBOS)
  279. uint16_t state = 0;
  280. #else
  281. uint8_t state = 0;
  282. #endif
  283. for (uint8_t key_buffer_i = 0; key_buffer_i < key_buffer_size; key_buffer_i++) {
  284. queued_record_t *qrecord = &key_buffer[key_buffer_i];
  285. keyrecord_t *record = &qrecord->record;
  286. uint16_t keycode = qrecord->keycode;
  287. uint8_t key_count = 0;
  288. uint16_t key_index = -1;
  289. _find_key_index_and_count(combo->keys, keycode, &key_index, &key_count);
  290. if (-1 == (int16_t)key_index) {
  291. // key not part of this combo
  292. continue;
  293. }
  294. KEY_STATE_DOWN(state, key_index);
  295. if (ALL_COMBO_KEYS_ARE_DOWN(state, key_count)) {
  296. // this in the end executes the combo when the key_buffer is dumped.
  297. record->keycode = combo->keycode;
  298. record->event.type = COMBO_EVENT;
  299. record->event.key = MAKE_KEYPOS(0, 0);
  300. qrecord->combo_index = combo_index;
  301. ACTIVATE_COMBO(combo);
  302. if (key_count == 1) {
  303. release_combo(combo_index, combo);
  304. }
  305. break;
  306. } else {
  307. // key was part of the combo but not the last one, "disable" it
  308. // by making it a TICK event.
  309. record->event.type = TICK_EVENT;
  310. }
  311. }
  312. drop_combo_from_buffer(combo_index);
  313. }
  314. static inline void apply_combos(void) {
  315. // Apply all buffered normal combos.
  316. for (uint8_t i = combo_buffer_read; i != combo_buffer_write; INCREMENT_MOD(i)) {
  317. queued_combo_t *buffered_combo = &combo_buffer[i];
  318. combo_t *combo = combo_get(buffered_combo->combo_index);
  319. #ifdef COMBO_MUST_TAP_PER_COMBO
  320. if (get_combo_must_tap(buffered_combo->combo_index, combo)) {
  321. // Tap-only combos are applied on key release only, so let's drop 'em here.
  322. drop_combo_from_buffer(buffered_combo->combo_index);
  323. continue;
  324. }
  325. #endif
  326. apply_combo(buffered_combo->combo_index, combo);
  327. }
  328. dump_key_buffer();
  329. clear_combos();
  330. }
  331. combo_t *overlaps(combo_t *combo1, combo_t *combo2) {
  332. /* Checks if the combos overlap and returns the combo that should be
  333. * dropped from the combo buffer.
  334. * The combo that has less keys will be dropped. If they have the same
  335. * amount of keys, drop combo1. */
  336. uint8_t idx1 = 0, idx2 = 0;
  337. uint16_t key1, key2;
  338. bool overlaps = false;
  339. while ((key1 = pgm_read_word(&combo1->keys[idx1])) != COMBO_END) {
  340. idx2 = 0;
  341. while ((key2 = pgm_read_word(&combo2->keys[idx2])) != COMBO_END) {
  342. if (key1 == key2) overlaps = true;
  343. idx2 += 1;
  344. }
  345. idx1 += 1;
  346. }
  347. if (!overlaps) return NULL;
  348. if (idx2 < idx1) return combo2;
  349. return combo1;
  350. }
  351. #if defined(COMBO_MUST_PRESS_IN_ORDER) || defined(COMBO_MUST_PRESS_IN_ORDER_PER_COMBO)
  352. static bool keys_pressed_in_order(uint16_t combo_index, combo_t *combo, uint16_t key_index, uint16_t keycode, keyrecord_t *record) {
  353. # ifdef COMBO_MUST_PRESS_IN_ORDER_PER_COMBO
  354. if (!get_combo_must_press_in_order(combo_index, combo)) {
  355. return true;
  356. }
  357. # endif
  358. if (
  359. // The `state` bit for the key being pressed.
  360. (1 << key_index) ==
  361. // The *next* combo key's bit.
  362. (COMBO_STATE(combo) + 1)
  363. // E.g. two keys already pressed: `state == 11`.
  364. // Next possible `state` is `111`.
  365. // So the needed bit is `100` which we get with `11 + 1`.
  366. ) {
  367. return true;
  368. }
  369. return false;
  370. }
  371. #endif
  372. static combo_key_action_t process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record, uint16_t combo_index) {
  373. uint8_t key_count = 0;
  374. uint16_t key_index = -1;
  375. _find_key_index_and_count(combo->keys, keycode, &key_index, &key_count);
  376. /* Continue processing if key isn't part of current combo. */
  377. if (-1 == (int16_t)key_index) {
  378. return COMBO_KEY_NOT_PRESSED;
  379. }
  380. bool key_is_part_of_combo = (!COMBO_DISABLED(combo) && is_combo_enabled()
  381. #if defined(COMBO_MUST_PRESS_IN_ORDER) || defined(COMBO_MUST_PRESS_IN_ORDER_PER_COMBO)
  382. && keys_pressed_in_order(combo_index, combo, key_index, keycode, record)
  383. #endif
  384. #ifdef COMBO_SHOULD_TRIGGER
  385. && combo_should_trigger(combo_index, combo, keycode, record)
  386. #endif
  387. );
  388. if (record->event.pressed && key_is_part_of_combo) {
  389. uint16_t time = _get_combo_term(combo_index, combo);
  390. if (!COMBO_ACTIVE(combo)) {
  391. KEY_STATE_DOWN(combo->state, key_index);
  392. if (longest_term < time) {
  393. longest_term = time;
  394. }
  395. }
  396. if (ALL_COMBO_KEYS_ARE_DOWN(COMBO_STATE(combo), key_count)) {
  397. /* Combo was fully pressed */
  398. /* Buffer the combo so we can fire it after COMBO_TERM */
  399. #ifndef COMBO_NO_TIMER
  400. /* Don't buffer this combo if its combo term has passed. */
  401. if (timer && timer_elapsed(timer) > time) {
  402. DISABLE_COMBO(combo);
  403. return COMBO_KEY_PRESSED;
  404. } else
  405. #endif
  406. {
  407. // disable readied combos that overlap with this combo
  408. combo_t *drop = NULL;
  409. for (uint8_t combo_buffer_i = combo_buffer_read; combo_buffer_i != combo_buffer_write; INCREMENT_MOD(combo_buffer_i)) {
  410. queued_combo_t *qcombo = &combo_buffer[combo_buffer_i];
  411. combo_t *buffered_combo = combo_get(qcombo->combo_index);
  412. if ((drop = overlaps(buffered_combo, combo))) {
  413. DISABLE_COMBO(drop);
  414. if (drop == combo) {
  415. // stop checking for overlaps if dropped combo was current combo.
  416. break;
  417. } else if (combo_buffer_i == combo_buffer_read && drop == buffered_combo) {
  418. /* Drop the disabled buffered combo from the buffer if
  419. * it is in the beginning of the buffer. */
  420. INCREMENT_MOD(combo_buffer_read);
  421. }
  422. }
  423. }
  424. if (drop != combo) {
  425. // save this combo to buffer
  426. combo_buffer[combo_buffer_write] = (queued_combo_t){
  427. .combo_index = combo_index,
  428. };
  429. INCREMENT_MOD(combo_buffer_write);
  430. // get possible longer waiting time for tap-/hold-only combos.
  431. longest_term = _get_wait_time(combo_index, combo);
  432. }
  433. } // if timer elapsed end
  434. }
  435. #ifdef COMBO_PROCESS_KEY_REPRESS
  436. } else if (record->event.pressed) {
  437. if (COMBO_ACTIVE(combo)) {
  438. if (process_combo_key_repress(combo_index, combo, key_index, keycode)) {
  439. KEY_STATE_DOWN(combo->state, key_index);
  440. return COMBO_KEY_REPRESSED;
  441. }
  442. }
  443. #endif
  444. } else {
  445. // chord releases
  446. if (!COMBO_ACTIVE(combo) && ALL_COMBO_KEYS_ARE_DOWN(COMBO_STATE(combo), key_count)) {
  447. /* First key quickly released */
  448. if (COMBO_DISABLED(combo) || _get_combo_must_hold(combo_index, combo)) {
  449. // combo wasn't tappable, disable it and drop it from buffer.
  450. drop_combo_from_buffer(combo_index);
  451. key_is_part_of_combo = false;
  452. }
  453. #ifdef COMBO_MUST_TAP_PER_COMBO
  454. else if (get_combo_must_tap(combo_index, combo)) {
  455. // immediately apply tap-only combo
  456. apply_combo(combo_index, combo);
  457. apply_combos(); // also apply other prepared combos and dump key buffer
  458. # ifdef COMBO_PROCESS_KEY_RELEASE
  459. if (process_combo_key_release(combo_index, combo, key_index, keycode)) {
  460. release_combo(combo_index, combo);
  461. }
  462. # endif
  463. }
  464. #endif
  465. } else if (COMBO_ACTIVE(combo) && ONLY_ONE_KEY_IS_DOWN(COMBO_STATE(combo)) && KEY_NOT_YET_RELEASED(COMBO_STATE(combo), key_index)) {
  466. /* last key released */
  467. release_combo(combo_index, combo);
  468. key_is_part_of_combo = true;
  469. #ifdef COMBO_PROCESS_KEY_RELEASE
  470. process_combo_key_release(combo_index, combo, key_index, keycode);
  471. #endif
  472. } else if (COMBO_ACTIVE(combo) && KEY_NOT_YET_RELEASED(COMBO_STATE(combo), key_index)) {
  473. /* first or middle key released */
  474. key_is_part_of_combo = true;
  475. #ifdef COMBO_PROCESS_KEY_RELEASE
  476. if (process_combo_key_release(combo_index, combo, key_index, keycode)) {
  477. release_combo(combo_index, combo);
  478. }
  479. #endif
  480. } else {
  481. /* The released key was part of an incomplete combo */
  482. key_is_part_of_combo = false;
  483. }
  484. KEY_STATE_UP(combo->state, key_index);
  485. }
  486. return key_is_part_of_combo ? COMBO_KEY_PRESSED : COMBO_KEY_NOT_PRESSED;
  487. }
  488. bool process_combo(uint16_t keycode, keyrecord_t *record) {
  489. uint8_t is_combo_key = COMBO_KEY_NOT_PRESSED;
  490. bool no_combo_keys_pressed = true;
  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. no_combo_keys_pressed = no_combo_keys_pressed && (NO_COMBO_KEYS_ARE_DOWN || COMBO_ACTIVE(combo) || COMBO_DISABLED(combo));
  517. }
  518. if (record->event.pressed && is_combo_key) {
  519. #ifndef COMBO_NO_TIMER
  520. # ifdef COMBO_STRICT_TIMER
  521. if (!timer) {
  522. // timer is set only on the first key
  523. timer = timer_read();
  524. }
  525. # else
  526. timer = timer_read();
  527. # endif
  528. #endif
  529. #ifdef COMBO_PROCESS_KEY_REPRESS
  530. if (is_combo_key == COMBO_KEY_PRESSED)
  531. #endif
  532. {
  533. if (key_buffer_size < COMBO_KEY_BUFFER_LENGTH) {
  534. key_buffer[key_buffer_size++] = (queued_record_t){
  535. .record = *record,
  536. .keycode = keycode,
  537. .combo_index = -1, // this will be set when applying combos
  538. };
  539. }
  540. }
  541. } else {
  542. if (combo_buffer_read != combo_buffer_write) {
  543. // some combo is prepared
  544. apply_combos();
  545. } else {
  546. // reset state if there are no combo keys pressed at all
  547. dump_key_buffer();
  548. #ifndef COMBO_NO_TIMER
  549. timer = 0;
  550. #endif
  551. clear_combos();
  552. }
  553. }
  554. return !is_combo_key;
  555. }
  556. void combo_task(void) {
  557. if (!b_combo_enable) {
  558. return;
  559. }
  560. #ifndef COMBO_NO_TIMER
  561. if (timer && timer_elapsed(timer) > longest_term) {
  562. if (combo_buffer_read != combo_buffer_write) {
  563. apply_combos();
  564. longest_term = 0;
  565. timer = 0;
  566. } else {
  567. dump_key_buffer();
  568. timer = 0;
  569. clear_combos();
  570. }
  571. }
  572. #endif
  573. }
  574. void combo_enable(void) {
  575. b_combo_enable = true;
  576. }
  577. void combo_disable(void) {
  578. #ifndef COMBO_NO_TIMER
  579. timer = 0;
  580. #endif
  581. b_combo_enable = false;
  582. combo_buffer_read = combo_buffer_write;
  583. clear_combos();
  584. dump_key_buffer();
  585. }
  586. void combo_toggle(void) {
  587. if (b_combo_enable) {
  588. combo_disable();
  589. } else {
  590. combo_enable();
  591. }
  592. }
  593. bool is_combo_enabled(void) {
  594. return b_combo_enable;
  595. }