process_combo.c 18 KB

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