process_combo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. __attribute__((weak)) combo_t key_combos[COMBO_COUNT] = {
  19. };
  20. __attribute__((weak)) void process_combo_event(uint8_t combo_index, bool pressed) {}
  21. static uint16_t timer = 0;
  22. static uint8_t current_combo_index = 0;
  23. static bool drop_buffer = false;
  24. static bool is_active = false;
  25. static uint8_t buffer_size = 0;
  26. #ifdef COMBO_ALLOW_ACTION_KEYS
  27. static keyrecord_t key_buffer[MAX_COMBO_LENGTH];
  28. #else
  29. static uint16_t key_buffer[MAX_COMBO_LENGTH];
  30. #endif
  31. static inline void send_combo(uint16_t action, bool pressed) {
  32. if (action) {
  33. if (pressed) {
  34. register_code16(action);
  35. } else {
  36. unregister_code16(action);
  37. }
  38. } else {
  39. process_combo_event(current_combo_index, pressed);
  40. }
  41. }
  42. static inline void dump_key_buffer(bool emit) {
  43. if (buffer_size == 0) {
  44. return;
  45. }
  46. if (emit) {
  47. for (uint8_t i = 0; i < buffer_size; i++) {
  48. #ifdef COMBO_ALLOW_ACTION_KEYS
  49. const action_t action = store_or_get_action(key_buffer[i].event.pressed, key_buffer[i].event.key);
  50. process_action(&(key_buffer[i]), action);
  51. #else
  52. register_code16(key_buffer[i]);
  53. send_keyboard_report();
  54. #endif
  55. }
  56. }
  57. buffer_size = 0;
  58. }
  59. #define ALL_COMBO_KEYS_ARE_DOWN (((1 << count) - 1) == combo->state)
  60. #define KEY_STATE_DOWN(key) \
  61. do { \
  62. combo->state |= (1 << key); \
  63. } while (0)
  64. #define KEY_STATE_UP(key) \
  65. do { \
  66. combo->state &= ~(1 << key); \
  67. } while (0)
  68. static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record) {
  69. uint8_t count = 0;
  70. uint8_t index = -1;
  71. /* Find index of keycode and number of combo keys */
  72. for (const uint16_t *keys = combo->keys;; ++count) {
  73. uint16_t key = pgm_read_word(&keys[count]);
  74. if (keycode == key) index = count;
  75. if (COMBO_END == key) break;
  76. }
  77. /* Continue processing if not a combo key */
  78. if (-1 == (int8_t)index) return false;
  79. bool is_combo_active = is_active;
  80. if (record->event.pressed) {
  81. KEY_STATE_DOWN(index);
  82. if (is_combo_active) {
  83. if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
  84. send_combo(combo->keycode, true);
  85. drop_buffer = true;
  86. }
  87. }
  88. } else {
  89. if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
  90. send_combo(combo->keycode, false);
  91. } else {
  92. /* continue processing without immediately returning */
  93. is_combo_active = false;
  94. }
  95. KEY_STATE_UP(index);
  96. }
  97. return is_combo_active;
  98. }
  99. #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
  100. bool process_combo(uint16_t keycode, keyrecord_t *record) {
  101. bool is_combo_key = false;
  102. drop_buffer = false;
  103. bool no_combo_keys_pressed = true;
  104. for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) {
  105. combo_t *combo = &key_combos[current_combo_index];
  106. is_combo_key |= process_single_combo(combo, keycode, record);
  107. no_combo_keys_pressed = no_combo_keys_pressed && NO_COMBO_KEYS_ARE_DOWN;
  108. }
  109. if (drop_buffer) {
  110. /* buffer is only dropped when we complete a combo, so we refresh the timer
  111. * here */
  112. timer = timer_read();
  113. dump_key_buffer(false);
  114. } else if (!is_combo_key) {
  115. /* if no combos claim the key we need to emit the keybuffer */
  116. dump_key_buffer(true);
  117. // reset state if there are no combo keys pressed at all
  118. if (no_combo_keys_pressed) {
  119. timer = 0;
  120. is_active = true;
  121. }
  122. } else if (record->event.pressed && is_active) {
  123. /* otherwise the key is consumed and placed in the buffer */
  124. timer = timer_read();
  125. if (buffer_size < MAX_COMBO_LENGTH) {
  126. #ifdef COMBO_ALLOW_ACTION_KEYS
  127. key_buffer[buffer_size++] = *record;
  128. #else
  129. key_buffer[buffer_size++] = keycode;
  130. #endif
  131. }
  132. }
  133. return !is_combo_key;
  134. }
  135. void matrix_scan_combo(void) {
  136. if (is_active && timer && timer_elapsed(timer) > COMBO_TERM) {
  137. /* This disables the combo, meaning key events for this
  138. * combo will be handled by the next processors in the chain
  139. */
  140. is_active = false;
  141. dump_key_buffer(true);
  142. }
  143. }