process_combo.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "process_combo.h"
  2. #include "print.h"
  3. #define SEND_KEY(key) \
  4. do { \
  5. register_code16(key); \
  6. send_keyboard_report(); \
  7. unregister_code16(key); \
  8. } while(0)
  9. #define COMBO_TIMER_ELAPSED -1
  10. #if COMBO_TERM
  11. #define IS_COMBO_KEY_HELD(combo) (COMBO_TIMER_ELAPSED == combo->timer ? false : true)
  12. #define RESET_COMBO_TIMER_AND_KEY(combo) combo->timer = 0; combo->key = 0
  13. #else
  14. #define IS_COMBO_KEY_HELD(combo) (true)
  15. #define RESET_COMBO_TIMER_AND_KEY(combo) do {} while (0)
  16. #endif
  17. __attribute__ ((weak))
  18. combo_t key_combos[COMBO_COUNT] = {
  19. };
  20. static inline void reset_combo(combo_t *combo)
  21. {
  22. combo->state = 0;
  23. RESET_COMBO_TIMER_AND_KEY(combo);
  24. }
  25. #define ALL_COMBO_KEYS_ARE_DOWN (((1<<count)-1) == combo->state)
  26. #define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state)
  27. #define KEY_STATE_DOWN(key) do{ combo->state |= (1<<key); } while(0)
  28. #define KEY_STATE_UP(key) do{ combo->state &= ~(1<<key); } while(0)
  29. static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record)
  30. {
  31. uint8_t count = 0;
  32. uint8_t index = -1;
  33. /* Find index of keycode and number of combo keys */
  34. for (const uint16_t *keys = combo->keys; ;++count) {
  35. uint16_t key = pgm_read_word(&keys[count]);
  36. if (keycode == key) index = count;
  37. if (COMBO_END == key) break;
  38. }
  39. /* Return if not a combo key */
  40. if (-1 == index) return false;
  41. bool is_combo_active = IS_COMBO_KEY_HELD(combo);
  42. if (record->event.pressed) {
  43. KEY_STATE_DOWN(index);
  44. #if COMBO_TERM
  45. if (is_combo_active) {
  46. combo->timer = timer_read();
  47. combo->key = keycode;
  48. }
  49. #endif
  50. } else {
  51. if (is_combo_active && combo->state) { /* Combo key was tapped */
  52. RESET_COMBO_TIMER_AND_KEY(combo);
  53. SEND_KEY(keycode);
  54. }
  55. #if COMBO_TERM
  56. if (!is_combo_active && keycode == combo->key) { /* Held combo key was released */
  57. unregister_code16(combo->key);
  58. }
  59. #endif
  60. KEY_STATE_UP(index);
  61. }
  62. if (ALL_COMBO_KEYS_ARE_DOWN && is_combo_active) {
  63. SEND_KEY(combo->action);
  64. reset_combo(combo);
  65. }
  66. if(NO_COMBO_KEYS_ARE_DOWN && !is_combo_active) {
  67. reset_combo(combo);
  68. }
  69. return is_combo_active;
  70. }
  71. bool process_combo(uint16_t keycode, keyrecord_t *record)
  72. {
  73. bool is_combo_key = false;
  74. for (int i = 0; i < COMBO_COUNT; ++i) {
  75. combo_t *combo = &key_combos[i];
  76. is_combo_key |= process_single_combo(combo, keycode, record);
  77. }
  78. return !is_combo_key;
  79. }
  80. void matrix_scan_combo(void)
  81. {
  82. #if COMBO_TERM
  83. for (int i = 0; i < COMBO_COUNT; ++i) {
  84. combo_t *combo = &key_combos[i];
  85. if (combo->timer &&
  86. combo->timer != COMBO_TIMER_ELAPSED &&
  87. timer_elapsed(combo->timer) > COMBO_TERM) {
  88. combo->timer = COMBO_TIMER_ELAPSED;
  89. unregister_code16(combo->key);
  90. register_code16(combo->key);
  91. }
  92. }
  93. #endif
  94. }