logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

process_tap_dance.c (6724B)


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