You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
6.1 KiB

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