process_combo.c 5.9 KB

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