process_tap_dance.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "quantum.h"
  17. static uint16_t active_td;
  18. static uint16_t last_tap_time;
  19. void qk_tap_dance_pair_on_each_tap(qk_tap_dance_state_t *state, void *user_data) {
  20. qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
  21. if (state->count == 2) {
  22. register_code16(pair->kc2);
  23. state->finished = true;
  24. }
  25. }
  26. void qk_tap_dance_pair_finished(qk_tap_dance_state_t *state, void *user_data) {
  27. qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
  28. register_code16(pair->kc1);
  29. }
  30. void qk_tap_dance_pair_reset(qk_tap_dance_state_t *state, void *user_data) {
  31. qk_tap_dance_pair_t *pair = (qk_tap_dance_pair_t *)user_data;
  32. if (state->count == 1) {
  33. wait_ms(TAP_CODE_DELAY);
  34. unregister_code16(pair->kc1);
  35. } else if (state->count == 2) {
  36. unregister_code16(pair->kc2);
  37. }
  38. }
  39. void qk_tap_dance_dual_role_on_each_tap(qk_tap_dance_state_t *state, void *user_data) {
  40. qk_tap_dance_dual_role_t *pair = (qk_tap_dance_dual_role_t *)user_data;
  41. if (state->count == 2) {
  42. layer_move(pair->layer);
  43. state->finished = true;
  44. }
  45. }
  46. void qk_tap_dance_dual_role_finished(qk_tap_dance_state_t *state, void *user_data) {
  47. qk_tap_dance_dual_role_t *pair = (qk_tap_dance_dual_role_t *)user_data;
  48. if (state->count == 1) {
  49. register_code16(pair->kc);
  50. } else if (state->count == 2) {
  51. pair->layer_function(pair->layer);
  52. }
  53. }
  54. void qk_tap_dance_dual_role_reset(qk_tap_dance_state_t *state, void *user_data) {
  55. qk_tap_dance_dual_role_t *pair = (qk_tap_dance_dual_role_t *)user_data;
  56. if (state->count == 1) {
  57. wait_ms(TAP_CODE_DELAY);
  58. unregister_code16(pair->kc);
  59. }
  60. }
  61. static inline void _process_tap_dance_action_fn(qk_tap_dance_state_t *state, void *user_data, qk_tap_dance_user_fn_t fn) {
  62. if (fn) {
  63. fn(state, user_data);
  64. }
  65. }
  66. static inline void process_tap_dance_action_on_each_tap(qk_tap_dance_action_t *action) {
  67. action->state.count++;
  68. action->state.weak_mods = get_mods();
  69. action->state.weak_mods |= get_weak_mods();
  70. #ifndef NO_ACTION_ONESHOT
  71. action->state.oneshot_mods = get_oneshot_mods();
  72. #endif
  73. _process_tap_dance_action_fn(&action->state, action->user_data, action->fn.on_each_tap);
  74. }
  75. static inline void process_tap_dance_action_on_reset(qk_tap_dance_action_t *action) {
  76. _process_tap_dance_action_fn(&action->state, action->user_data, action->fn.on_reset);
  77. del_weak_mods(action->state.weak_mods);
  78. #ifndef NO_ACTION_ONESHOT
  79. del_mods(action->state.oneshot_mods);
  80. #endif
  81. send_keyboard_report();
  82. action->state = (const qk_tap_dance_state_t){0};
  83. }
  84. static inline void process_tap_dance_action_on_dance_finished(qk_tap_dance_action_t *action) {
  85. if (!action->state.finished) {
  86. action->state.finished = true;
  87. add_weak_mods(action->state.weak_mods);
  88. #ifndef NO_ACTION_ONESHOT
  89. add_mods(action->state.oneshot_mods);
  90. #endif
  91. send_keyboard_report();
  92. _process_tap_dance_action_fn(&action->state, action->user_data, action->fn.on_dance_finished);
  93. }
  94. active_td = 0;
  95. if (!action->state.pressed) {
  96. // There will not be a key release event, so reset now.
  97. process_tap_dance_action_on_reset(action);
  98. }
  99. }
  100. bool preprocess_tap_dance(uint16_t keycode, keyrecord_t *record) {
  101. qk_tap_dance_action_t *action;
  102. if (!record->event.pressed) return false;
  103. if (!active_td || keycode == active_td) return false;
  104. action = &tap_dance_actions[TD_INDEX(active_td)];
  105. action->state.interrupted = true;
  106. action->state.interrupting_keycode = keycode;
  107. process_tap_dance_action_on_dance_finished(action);
  108. // Tap dance actions can leave some weak mods active (e.g., if the tap dance is mapped to a keycode with
  109. // modifiers), but these weak mods should not affect the keypress which interrupted the tap dance.
  110. clear_weak_mods();
  111. // Signal that a tap dance has been finished due to being interrupted,
  112. // therefore the keymap lookup for the currently processed event needs to
  113. // be repeated with the current layer state that might have been updated by
  114. // the finished tap dance.
  115. return true;
  116. }
  117. bool process_tap_dance(uint16_t keycode, keyrecord_t *record) {
  118. qk_tap_dance_action_t *action;
  119. switch (keycode) {
  120. case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
  121. action = &tap_dance_actions[TD_INDEX(keycode)];
  122. action->state.pressed = record->event.pressed;
  123. if (record->event.pressed) {
  124. last_tap_time = timer_read();
  125. process_tap_dance_action_on_each_tap(action);
  126. active_td = action->state.finished ? 0 : keycode;
  127. } else {
  128. if (action->state.finished) {
  129. process_tap_dance_action_on_reset(action);
  130. }
  131. }
  132. break;
  133. }
  134. return true;
  135. }
  136. void tap_dance_task() {
  137. qk_tap_dance_action_t *action;
  138. if (!active_td || timer_elapsed(last_tap_time) <= GET_TAPPING_TERM(active_td, &(keyrecord_t){})) return;
  139. action = &tap_dance_actions[TD_INDEX(active_td)];
  140. if (!action->state.interrupted) {
  141. process_tap_dance_action_on_dance_finished(action);
  142. }
  143. }
  144. void reset_tap_dance(qk_tap_dance_state_t *state) {
  145. active_td = 0;
  146. process_tap_dance_action_on_reset((qk_tap_dance_action_t *)state);
  147. }