process_combo.c 5.1 KB

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