process_combo.c 5.8 KB

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