process_combo.c 21 KB

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