process_combo.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #define COMBO_TIMER_ELAPSED -1
  19. __attribute__ ((weak))
  20. combo_t key_combos[] = {
  21. };
  22. __attribute__ ((weak))
  23. void process_combo_event(uint8_t combo_index, bool pressed) {
  24. }
  25. static uint8_t current_combo_index = 0;
  26. static inline void send_combo(uint16_t action, bool pressed)
  27. {
  28. if (action) {
  29. if (pressed) {
  30. register_code16(action);
  31. } else {
  32. unregister_code16(action);
  33. }
  34. } else {
  35. process_combo_event(current_combo_index, pressed);
  36. }
  37. }
  38. #define ALL_COMBO_KEYS_ARE_DOWN (((1<<count)-1) == combo->state)
  39. #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
  40. #define KEY_STATE_DOWN(key) do{ combo->state |= (1<<key); } while(0)
  41. #define KEY_STATE_UP(key) do{ combo->state &= ~(1<<key); } while(0)
  42. static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record)
  43. {
  44. uint8_t count = 0;
  45. uint8_t index = -1;
  46. /* Find index of keycode and number of combo keys */
  47. for (const uint16_t *keys = combo->keys; ;++count) {
  48. uint16_t key = pgm_read_word(&keys[count]);
  49. if (keycode == key) index = count;
  50. if (COMBO_END == key) break;
  51. }
  52. /* Return if not a combo key */
  53. if (-1 == (int8_t)index) return false;
  54. /* The combos timer is used to signal whether the combo is active */
  55. bool is_combo_active = COMBO_TIMER_ELAPSED == combo->timer ? false : true;
  56. if (record->event.pressed) {
  57. KEY_STATE_DOWN(index);
  58. if (is_combo_active) {
  59. if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
  60. send_combo(combo->keycode, true);
  61. combo->timer = COMBO_TIMER_ELAPSED;
  62. } else { /* Combo key was pressed */
  63. combo->timer = timer_read();
  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->timer = 0;
  87. }
  88. KEY_STATE_UP(index);
  89. }
  90. if (NO_COMBO_KEYS_ARE_DOWN) {
  91. combo->timer = 0;
  92. }
  93. return is_combo_active;
  94. }
  95. bool process_combo(uint16_t keycode, keyrecord_t *record)
  96. {
  97. bool is_combo_key = false;
  98. for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) {
  99. combo_t *combo = &key_combos[current_combo_index];
  100. is_combo_key |= process_single_combo(combo, keycode, record);
  101. }
  102. return !is_combo_key;
  103. }
  104. void matrix_scan_combo(void)
  105. {
  106. for (int i = 0; i < COMBO_COUNT; ++i) {
  107. combo_t *combo = &key_combos[i];
  108. if (combo->timer &&
  109. combo->timer != COMBO_TIMER_ELAPSED &&
  110. timer_elapsed(combo->timer) > COMBO_TERM) {
  111. /* This disables the combo, meaning key events for this
  112. * combo will be handled by the next processors in the chain
  113. */
  114. combo->timer = COMBO_TIMER_ELAPSED;
  115. #ifdef COMBO_ALLOW_ACTION_KEYS
  116. process_action(&combo->prev_record,
  117. store_or_get_action(combo->prev_record.event.pressed,
  118. combo->prev_record.event.key));
  119. #else
  120. unregister_code16(combo->prev_key);
  121. register_code16(combo->prev_key);
  122. #endif
  123. }
  124. }
  125. }