logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

process_combo.c (22098B)


  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. break;
  303. } else {
  304. // key was part of the combo but not the last one, "disable" it
  305. // by making it a TICK event.
  306. record->event.type = TICK_EVENT;
  307. }
  308. }
  309. drop_combo_from_buffer(combo_index);
  310. }
  311. static inline void apply_combos(void) {
  312. // Apply all buffered normal combos.
  313. for (uint8_t i = combo_buffer_read; i != combo_buffer_write; INCREMENT_MOD(i)) {
  314. queued_combo_t *buffered_combo = &combo_buffer[i];
  315. combo_t * combo = combo_get(buffered_combo->combo_index);
  316. #ifdef COMBO_MUST_TAP_PER_COMBO
  317. if (get_combo_must_tap(buffered_combo->combo_index, combo)) {
  318. // Tap-only combos are applied on key release only, so let's drop 'em here.
  319. drop_combo_from_buffer(buffered_combo->combo_index);
  320. continue;
  321. }
  322. #endif
  323. apply_combo(buffered_combo->combo_index, combo);
  324. }
  325. dump_key_buffer();
  326. clear_combos();
  327. }
  328. combo_t *overlaps(combo_t *combo1, combo_t *combo2) {
  329. /* Checks if the combos overlap and returns the combo that should be
  330. * dropped from the combo buffer.
  331. * The combo that has less keys will be dropped. If they have the same
  332. * amount of keys, drop combo1. */
  333. uint8_t idx1 = 0, idx2 = 0;
  334. uint16_t key1, key2;
  335. bool overlaps = false;
  336. while ((key1 = pgm_read_word(&combo1->keys[idx1])) != COMBO_END) {
  337. idx2 = 0;
  338. while ((key2 = pgm_read_word(&combo2->keys[idx2])) != COMBO_END) {
  339. if (key1 == key2) overlaps = true;
  340. idx2 += 1;
  341. }
  342. idx1 += 1;
  343. }
  344. if (!overlaps) return NULL;
  345. if (idx2 < idx1) return combo2;
  346. return combo1;
  347. }
  348. #if defined(COMBO_MUST_PRESS_IN_ORDER) || defined(COMBO_MUST_PRESS_IN_ORDER_PER_COMBO)
  349. static bool keys_pressed_in_order(uint16_t combo_index, combo_t *combo, uint16_t key_index, uint16_t keycode, keyrecord_t *record) {
  350. # ifdef COMBO_MUST_PRESS_IN_ORDER_PER_COMBO
  351. if (!get_combo_must_press_in_order(combo_index, combo)) {
  352. return true;
  353. }
  354. # endif
  355. if (
  356. // The `state` bit for the key being pressed.
  357. (1 << key_index) ==
  358. // The *next* combo key's bit.
  359. (COMBO_STATE(combo) + 1)
  360. // E.g. two keys already pressed: `state == 11`.
  361. // Next possible `state` is `111`.
  362. // So the needed bit is `100` which we get with `11 + 1`.
  363. ) {
  364. return true;
  365. }
  366. return false;
  367. }
  368. #endif
  369. static combo_key_action_t process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record, uint16_t combo_index) {
  370. uint8_t key_count = 0;
  371. uint16_t key_index = -1;
  372. _find_key_index_and_count(combo->keys, keycode, &key_index, &key_count);
  373. /* Continue processing if key isn't part of current combo. */
  374. if (-1 == (int16_t)key_index) {
  375. return COMBO_KEY_NOT_PRESSED;
  376. }
  377. bool key_is_part_of_combo = (!COMBO_DISABLED(combo) && is_combo_enabled()
  378. #if defined(COMBO_MUST_PRESS_IN_ORDER) || defined(COMBO_MUST_PRESS_IN_ORDER_PER_COMBO)
  379. && keys_pressed_in_order(combo_index, combo, key_index, keycode, record)
  380. #endif
  381. #ifdef COMBO_SHOULD_TRIGGER
  382. && combo_should_trigger(combo_index, combo, keycode, record)
  383. #endif
  384. );
  385. if (record->event.pressed && key_is_part_of_combo) {
  386. uint16_t time = _get_combo_term(combo_index, combo);
  387. if (!COMBO_ACTIVE(combo)) {
  388. KEY_STATE_DOWN(combo->state, key_index);
  389. if (longest_term < time) {
  390. longest_term = time;
  391. }
  392. }
  393. if (ALL_COMBO_KEYS_ARE_DOWN(COMBO_STATE(combo), key_count)) {
  394. /* Combo was fully pressed */
  395. /* Buffer the combo so we can fire it after COMBO_TERM */
  396. #ifndef COMBO_NO_TIMER
  397. /* Don't buffer this combo if its combo term has passed. */
  398. if (timer && timer_elapsed(timer) > time) {
  399. DISABLE_COMBO(combo);
  400. return COMBO_KEY_PRESSED;
  401. } else
  402. #endif
  403. {
  404. // disable readied combos that overlap with this combo
  405. combo_t *drop = NULL;
  406. for (uint8_t combo_buffer_i = combo_buffer_read; combo_buffer_i != combo_buffer_write; INCREMENT_MOD(combo_buffer_i)) {
  407. queued_combo_t *qcombo = &combo_buffer[combo_buffer_i];
  408. combo_t * buffered_combo = combo_get(qcombo->combo_index);
  409. if ((drop = overlaps(buffered_combo, combo))) {
  410. DISABLE_COMBO(drop);
  411. if (drop == combo) {
  412. // stop checking for overlaps if dropped combo was current combo.
  413. break;
  414. } else if (combo_buffer_i == combo_buffer_read && drop == buffered_combo) {
  415. /* Drop the disabled buffered combo from the buffer if
  416. * it is in the beginning of the buffer. */
  417. INCREMENT_MOD(combo_buffer_read);
  418. }
  419. }
  420. }
  421. if (drop != combo) {
  422. // save this combo to buffer
  423. combo_buffer[combo_buffer_write] = (queued_combo_t){
  424. .combo_index = combo_index,
  425. };
  426. INCREMENT_MOD(combo_buffer_write);
  427. // get possible longer waiting time for tap-/hold-only combos.
  428. longest_term = _get_wait_time(combo_index, combo);
  429. }
  430. } // if timer elapsed end
  431. }
  432. #ifdef COMBO_PROCESS_KEY_REPRESS
  433. } else if (record->event.pressed) {
  434. if (COMBO_ACTIVE(combo)) {
  435. if (process_combo_key_repress(combo_index, combo, key_index, keycode)) {
  436. KEY_STATE_DOWN(combo->state, key_index);
  437. return COMBO_KEY_REPRESSED;
  438. }
  439. }
  440. #endif
  441. } else {
  442. // chord releases
  443. if (!COMBO_ACTIVE(combo) && ALL_COMBO_KEYS_ARE_DOWN(COMBO_STATE(combo), key_count)) {
  444. /* First key quickly released */
  445. if (COMBO_DISABLED(combo) || _get_combo_must_hold(combo_index, combo)) {
  446. // combo wasn't tappable, disable it and drop it from buffer.
  447. drop_combo_from_buffer(combo_index);
  448. key_is_part_of_combo = false;
  449. }
  450. #ifdef COMBO_MUST_TAP_PER_COMBO
  451. else if (get_combo_must_tap(combo_index, combo)) {
  452. // immediately apply tap-only combo
  453. apply_combo(combo_index, combo);
  454. apply_combos(); // also apply other prepared combos and dump key buffer
  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. }
  461. #endif
  462. } else if (COMBO_ACTIVE(combo) && ONLY_ONE_KEY_IS_DOWN(COMBO_STATE(combo)) && KEY_NOT_YET_RELEASED(COMBO_STATE(combo), key_index)) {
  463. /* last key released */
  464. release_combo(combo_index, combo);
  465. key_is_part_of_combo = true;
  466. #ifdef COMBO_PROCESS_KEY_RELEASE
  467. process_combo_key_release(combo_index, combo, key_index, keycode);
  468. #endif
  469. } else if (COMBO_ACTIVE(combo) && KEY_NOT_YET_RELEASED(COMBO_STATE(combo), key_index)) {
  470. /* first or middle key released */
  471. key_is_part_of_combo = true;
  472. #ifdef COMBO_PROCESS_KEY_RELEASE
  473. if (process_combo_key_release(combo_index, combo, key_index, keycode)) {
  474. release_combo(combo_index, combo);
  475. }
  476. #endif
  477. } else {
  478. /* The released key was part of an incomplete combo */
  479. key_is_part_of_combo = false;
  480. }
  481. KEY_STATE_UP(combo->state, key_index);
  482. }
  483. return key_is_part_of_combo ? COMBO_KEY_PRESSED : COMBO_KEY_NOT_PRESSED;
  484. }
  485. bool process_combo(uint16_t keycode, keyrecord_t *record) {
  486. uint8_t is_combo_key = COMBO_KEY_NOT_PRESSED;
  487. bool no_combo_keys_pressed = true;
  488. if (keycode == QK_COMBO_ON && record->event.pressed) {
  489. combo_enable();
  490. return true;
  491. }
  492. if (keycode == QK_COMBO_OFF && record->event.pressed) {
  493. combo_disable();
  494. return true;
  495. }
  496. if (keycode == QK_COMBO_TOGGLE && record->event.pressed) {
  497. combo_toggle();
  498. return true;
  499. }
  500. #ifdef COMBO_ONLY_FROM_LAYER
  501. /* Only check keycodes from one layer. */
  502. keycode = keymap_key_to_keycode(COMBO_ONLY_FROM_LAYER, record->event.key);
  503. #else
  504. uint8_t highest_layer = get_highest_layer(layer_state | default_layer_state);
  505. uint8_t ref_layer = combo_ref_from_layer(highest_layer);
  506. if (ref_layer != highest_layer) {
  507. keycode = keymap_key_to_keycode(ref_layer, record->event.key);
  508. }
  509. #endif
  510. for (uint16_t idx = 0; idx < combo_count(); ++idx) {
  511. combo_t *combo = combo_get(idx);
  512. is_combo_key |= process_single_combo(combo, keycode, record, idx);
  513. no_combo_keys_pressed = no_combo_keys_pressed && (NO_COMBO_KEYS_ARE_DOWN || COMBO_ACTIVE(combo) || COMBO_DISABLED(combo));
  514. }
  515. if (record->event.pressed && is_combo_key) {
  516. #ifndef COMBO_NO_TIMER
  517. # ifdef COMBO_STRICT_TIMER
  518. if (!timer) {
  519. // timer is set only on the first key
  520. timer = timer_read();
  521. }
  522. # else
  523. timer = timer_read();
  524. # endif
  525. #endif
  526. #ifdef COMBO_PROCESS_KEY_REPRESS
  527. if (is_combo_key == COMBO_KEY_PRESSED)
  528. #endif
  529. {
  530. if (key_buffer_size < COMBO_KEY_BUFFER_LENGTH) {
  531. key_buffer[key_buffer_size++] = (queued_record_t){
  532. .record = *record,
  533. .keycode = keycode,
  534. .combo_index = -1, // this will be set when applying combos
  535. };
  536. }
  537. }
  538. } else {
  539. if (combo_buffer_read != combo_buffer_write) {
  540. // some combo is prepared
  541. apply_combos();
  542. } else {
  543. // reset state if there are no combo keys pressed at all
  544. dump_key_buffer();
  545. #ifndef COMBO_NO_TIMER
  546. timer = 0;
  547. #endif
  548. clear_combos();
  549. }
  550. }
  551. return !is_combo_key;
  552. }
  553. void combo_task(void) {
  554. if (!b_combo_enable) {
  555. return;
  556. }
  557. #ifndef COMBO_NO_TIMER
  558. if (timer && timer_elapsed(timer) > longest_term) {
  559. if (combo_buffer_read != combo_buffer_write) {
  560. apply_combos();
  561. longest_term = 0;
  562. timer = 0;
  563. } else {
  564. dump_key_buffer();
  565. timer = 0;
  566. clear_combos();
  567. }
  568. }
  569. #endif
  570. }
  571. void combo_enable(void) {
  572. b_combo_enable = true;
  573. }
  574. void combo_disable(void) {
  575. #ifndef COMBO_NO_TIMER
  576. timer = 0;
  577. #endif
  578. b_combo_enable = false;
  579. combo_buffer_read = combo_buffer_write;
  580. clear_combos();
  581. dump_key_buffer();
  582. }
  583. void combo_toggle(void) {
  584. if (b_combo_enable) {
  585. combo_disable();
  586. } else {
  587. combo_enable();
  588. }
  589. }
  590. bool is_combo_enabled(void) {
  591. return b_combo_enable;
  592. }