process_combo.c 21 KB

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