process_combo.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "process_combo.h"
  17. #include "print.h"
  18. __attribute__ ((weak))
  19. combo_t key_combos[COMBO_COUNT] = {
  20. };
  21. __attribute__ ((weak))
  22. void process_combo_event(uint8_t combo_index, bool pressed) {
  23. }
  24. static uint8_t current_combo_index = 0;
  25. static inline void send_combo(uint16_t action, bool pressed)
  26. {
  27. if (action) {
  28. if (pressed) {
  29. register_code16(action);
  30. } else {
  31. unregister_code16(action);
  32. }
  33. } else {
  34. process_combo_event(current_combo_index, pressed);
  35. }
  36. }
  37. #define ALL_COMBO_KEYS_ARE_DOWN (((1<<count)-1) == combo->state)
  38. #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
  39. #define KEY_STATE_DOWN(key) do{ combo->state |= (1<<key); } while(0)
  40. #define KEY_STATE_UP(key) do{ combo->state &= ~(1<<key); } while(0)
  41. static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record)
  42. {
  43. uint8_t count = 0;
  44. uint8_t index = -1;
  45. /* Find index of keycode and number of combo keys */
  46. for (const uint16_t *keys = combo->keys; ;++count) {
  47. uint16_t key = pgm_read_word(&keys[count]);
  48. if (keycode == key) index = count;
  49. if (COMBO_END == key) break;
  50. }
  51. /* Return if not a combo key */
  52. if (-1 == (int8_t)index) return false;
  53. /* The combos timer is used to signal whether the combo is active */
  54. bool is_combo_active = combo->is_active;
  55. if (record->event.pressed) {
  56. KEY_STATE_DOWN(index);
  57. if (is_combo_active) {
  58. if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
  59. send_combo(combo->keycode, true);
  60. combo->is_active = false;
  61. } else { /* Combo key was pressed */
  62. combo->timer = timer_read();
  63. combo->is_active = true;
  64. #ifdef COMBO_ALLOW_ACTION_KEYS
  65. combo->prev_record = *record;
  66. #else
  67. combo->prev_key = keycode;
  68. #endif
  69. }
  70. }
  71. } else {
  72. if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
  73. send_combo(combo->keycode, false);
  74. }
  75. if (is_combo_active) { /* Combo key was tapped */
  76. #ifdef COMBO_ALLOW_ACTION_KEYS
  77. record->event.pressed = true;
  78. process_action(record, store_or_get_action(record->event.pressed, record->event.key));
  79. record->event.pressed = false;
  80. process_action(record, store_or_get_action(record->event.pressed, record->event.key));
  81. #else
  82. register_code16(keycode);
  83. send_keyboard_report();
  84. unregister_code16(keycode);
  85. #endif
  86. combo->is_active = false;
  87. combo->timer = 0;
  88. }
  89. KEY_STATE_UP(index);
  90. }
  91. if (NO_COMBO_KEYS_ARE_DOWN) {
  92. combo->is_active = true;
  93. combo->timer = 0;
  94. }
  95. return is_combo_active;
  96. }
  97. bool process_combo(uint16_t keycode, keyrecord_t *record)
  98. {
  99. bool is_combo_key = false;
  100. for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) {
  101. combo_t *combo = &key_combos[current_combo_index];
  102. is_combo_key |= process_single_combo(combo, keycode, record);
  103. }
  104. return !is_combo_key;
  105. }
  106. void matrix_scan_combo(void)
  107. {
  108. for (int i = 0; i < COMBO_COUNT; ++i) {
  109. // Do not treat the (weak) key_combos too strict.
  110. #pragma GCC diagnostic push
  111. #pragma GCC diagnostic ignored "-Warray-bounds"
  112. combo_t *combo = &key_combos[i];
  113. #pragma GCC diagnostic pop
  114. if (combo->is_active &&
  115. combo->timer &&
  116. timer_elapsed(combo->timer) > COMBO_TERM) {
  117. /* This disables the combo, meaning key events for this
  118. * combo will be handled by the next processors in the chain
  119. */
  120. combo->is_active = false;
  121. #ifdef COMBO_ALLOW_ACTION_KEYS
  122. process_action(&combo->prev_record,
  123. store_or_get_action(combo->prev_record.event.pressed,
  124. combo->prev_record.event.key));
  125. #else
  126. unregister_code16(combo->prev_key);
  127. register_code16(combo->prev_key);
  128. #endif
  129. }
  130. }
  131. }