process_tap_dance.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "quantum.h"
  2. static qk_tap_dance_state_t qk_tap_dance_state;
  3. bool td_debug_enable = false;
  4. #if CONSOLE_ENABLE
  5. #define td_debug(s) if (td_debug_enable) \
  6. { \
  7. xprintf ("D:tap_dance:%s:%s = { keycode = %d, count = %d, active = %d, pressed = %d }\n", __FUNCTION__, s, \
  8. qk_tap_dance_state.keycode, qk_tap_dance_state.count, \
  9. qk_tap_dance_state.active, qk_tap_dance_state.pressed); \
  10. }
  11. #else
  12. #define td_debug(s)
  13. #endif
  14. void qk_tap_dance_pair_finished (qk_tap_dance_state_t *state, void *user_data) {
  15. qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
  16. if (state->count == 1) {
  17. register_code (pair->kc1);
  18. } else if (state->count == 2) {
  19. register_code (pair->kc2);
  20. }
  21. }
  22. void qk_tap_dance_pair_reset (qk_tap_dance_state_t *state, void *user_data) {
  23. qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
  24. if (state->count == 1) {
  25. unregister_code (pair->kc1);
  26. } else if (state->count == 2) {
  27. unregister_code (pair->kc2);
  28. }
  29. }
  30. static inline void _process_tap_dance_action_fn (qk_tap_dance_state_t *state,
  31. void *user_data,
  32. qk_tap_dance_user_fn_t fn)
  33. {
  34. if (fn) {
  35. fn(state, user_data);
  36. }
  37. }
  38. static inline void process_tap_dance_action_on_each_tap (qk_tap_dance_action_t action)
  39. {
  40. td_debug("trigger");
  41. _process_tap_dance_action_fn (&qk_tap_dance_state, action.user_data, action.fn.on_each_tap);
  42. }
  43. static inline void process_tap_dance_action_on_dance_finished (qk_tap_dance_action_t action)
  44. {
  45. td_debug("trigger");
  46. _process_tap_dance_action_fn (&qk_tap_dance_state, action.user_data, action.fn.on_dance_finished);
  47. }
  48. static inline void process_tap_dance_action_on_reset (qk_tap_dance_action_t action)
  49. {
  50. td_debug("trigger")
  51. _process_tap_dance_action_fn (&qk_tap_dance_state, action.user_data, action.fn.on_reset);
  52. }
  53. bool process_tap_dance(uint16_t keycode, keyrecord_t *record) {
  54. bool r = true;
  55. uint16_t idx = keycode - QK_TAP_DANCE;
  56. qk_tap_dance_action_t action;
  57. switch(keycode) {
  58. case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
  59. action = tap_dance_actions[idx];
  60. process_tap_dance_action_on_each_tap (action);
  61. if (qk_tap_dance_state.keycode && qk_tap_dance_state.keycode != keycode) {
  62. process_tap_dance_action_on_dance_finished (action);
  63. } else if (qk_tap_dance_state.active && qk_tap_dance_state.pressed) {
  64. reset_tap_dance (&qk_tap_dance_state);
  65. } else {
  66. r = false;
  67. }
  68. qk_tap_dance_state.active = true;
  69. qk_tap_dance_state.pressed = record->event.pressed;
  70. if (record->event.pressed) {
  71. qk_tap_dance_state.keycode = keycode;
  72. qk_tap_dance_state.timer = timer_read ();
  73. qk_tap_dance_state.count++;
  74. }
  75. break;
  76. default:
  77. if (qk_tap_dance_state.keycode) {
  78. // if we are here, the tap dance was interrupted by a different key
  79. idx = qk_tap_dance_state.keycode - QK_TAP_DANCE;
  80. action = tap_dance_actions[idx];
  81. process_tap_dance_action_on_each_tap (action);
  82. process_tap_dance_action_on_dance_finished (action);
  83. reset_tap_dance (&qk_tap_dance_state);
  84. qk_tap_dance_state.active = false;
  85. }
  86. break;
  87. }
  88. return r;
  89. }
  90. void matrix_scan_tap_dance () {
  91. if (qk_tap_dance_state.active && timer_elapsed (qk_tap_dance_state.timer) > TAPPING_TERM) {
  92. // if we are here, the tap dance was timed out
  93. uint16_t idx = qk_tap_dance_state.keycode - QK_TAP_DANCE;
  94. qk_tap_dance_action_t action = tap_dance_actions[idx];
  95. process_tap_dance_action_on_dance_finished (action);
  96. reset_tap_dance (&qk_tap_dance_state);
  97. }
  98. }
  99. void reset_tap_dance (qk_tap_dance_state_t *state) {
  100. uint16_t idx = state->keycode - QK_TAP_DANCE;
  101. qk_tap_dance_action_t action;
  102. if (state->pressed)
  103. return;
  104. action = tap_dance_actions[idx];
  105. process_tap_dance_action_on_reset (action);
  106. state->keycode = 0;
  107. state->count = 0;
  108. state->active = false;
  109. }