process_combo.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "process_combo.h"
  2. #include "print.h"
  3. #define COMBO_TIMER_ELAPSED -1
  4. __attribute__ ((weak))
  5. combo_t key_combos[] = {
  6. };
  7. __attribute__ ((weak))
  8. void process_combo_event(uint8_t combo_index, bool pressed) {
  9. }
  10. static uint8_t current_combo_index = 0;
  11. static inline void send_combo(uint16_t action, bool pressed)
  12. {
  13. if (action) {
  14. if (pressed) {
  15. register_code16(action);
  16. } else {
  17. unregister_code16(action);
  18. }
  19. } else {
  20. process_combo_event(current_combo_index, pressed);
  21. }
  22. }
  23. #define ALL_COMBO_KEYS_ARE_DOWN (((1<<count)-1) == combo->state)
  24. #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
  25. #define KEY_STATE_DOWN(key) do{ combo->state |= (1<<key); } while(0)
  26. #define KEY_STATE_UP(key) do{ combo->state &= ~(1<<key); } while(0)
  27. static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record)
  28. {
  29. uint8_t count = 0;
  30. uint8_t index = -1;
  31. /* Find index of keycode and number of combo keys */
  32. for (const uint16_t *keys = combo->keys; ;++count) {
  33. uint16_t key = pgm_read_word(&keys[count]);
  34. if (keycode == key) index = count;
  35. if (COMBO_END == key) break;
  36. }
  37. /* Return if not a combo key */
  38. if (-1 == (int8_t)index) return false;
  39. /* The combos timer is used to signal whether the combo is active */
  40. bool is_combo_active = COMBO_TIMER_ELAPSED == combo->timer ? false : true;
  41. if (record->event.pressed) {
  42. KEY_STATE_DOWN(index);
  43. if (is_combo_active) {
  44. if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
  45. send_combo(combo->keycode, true);
  46. combo->timer = COMBO_TIMER_ELAPSED;
  47. } else { /* Combo key was pressed */
  48. combo->timer = timer_read();
  49. #ifdef COMBO_ALLOW_ACTION_KEYS
  50. combo->prev_record = *record;
  51. #else
  52. combo->prev_key = keycode;
  53. #endif
  54. }
  55. }
  56. } else {
  57. if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
  58. send_combo(combo->keycode, false);
  59. }
  60. if (is_combo_active) { /* Combo key was tapped */
  61. #ifdef COMBO_ALLOW_ACTION_KEYS
  62. record->event.pressed = true;
  63. process_action(record, store_or_get_action(record->event.pressed, record->event.key));
  64. record->event.pressed = false;
  65. process_action(record, store_or_get_action(record->event.pressed, record->event.key));
  66. #else
  67. register_code16(keycode);
  68. send_keyboard_report();
  69. unregister_code16(keycode);
  70. #endif
  71. combo->timer = 0;
  72. }
  73. KEY_STATE_UP(index);
  74. }
  75. if (NO_COMBO_KEYS_ARE_DOWN) {
  76. combo->timer = 0;
  77. }
  78. return is_combo_active;
  79. }
  80. bool process_combo(uint16_t keycode, keyrecord_t *record)
  81. {
  82. bool is_combo_key = false;
  83. for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) {
  84. combo_t *combo = &key_combos[current_combo_index];
  85. is_combo_key |= process_single_combo(combo, keycode, record);
  86. }
  87. return !is_combo_key;
  88. }
  89. void matrix_scan_combo(void)
  90. {
  91. for (int i = 0; i < COMBO_COUNT; ++i) {
  92. combo_t *combo = &key_combos[i];
  93. if (combo->timer &&
  94. combo->timer != COMBO_TIMER_ELAPSED &&
  95. timer_elapsed(combo->timer) > COMBO_TERM) {
  96. /* This disables the combo, meaning key events for this
  97. * combo will be handled by the next processors in the chain
  98. */
  99. combo->timer = COMBO_TIMER_ELAPSED;
  100. #ifdef COMBO_ALLOW_ACTION_KEYS
  101. process_action(&combo->prev_record,
  102. store_or_get_action(combo->prev_record.event.pressed,
  103. combo->prev_record.event.key));
  104. #else
  105. unregister_code16(combo->prev_key);
  106. register_code16(combo->prev_key);
  107. #endif
  108. }
  109. }
  110. }