process_tap_dance.c 3.9 KB

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