1
0

process_combo.c 21 KB

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