process_tap_dance.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "quantum.h"
  2. static qk_tap_dance_state_t qk_tap_dance_state;
  3. static void _process_tap_dance_action_pair (qk_tap_dance_state_t *state,
  4. uint16_t kc1, uint16_t kc2) {
  5. uint16_t kc;
  6. if (state->count == 0)
  7. return;
  8. kc = (state->count == 1) ? kc1 : kc2;
  9. register_code (kc);
  10. unregister_code (kc);
  11. if (state->count >= 2) {
  12. reset_tap_dance (state);
  13. }
  14. }
  15. static void _process_tap_dance_action_fn (qk_tap_dance_state_t *state,
  16. qk_tap_dance_user_fn_t fn)
  17. {
  18. if (fn) {
  19. fn(state);
  20. }
  21. }
  22. void process_tap_dance_action (uint16_t keycode)
  23. {
  24. uint16_t idx = keycode - QK_TAP_DANCE;
  25. qk_tap_dance_action_t action;
  26. action = tap_dance_actions[idx];
  27. switch (action.type) {
  28. case QK_TAP_DANCE_TYPE_PAIR:
  29. _process_tap_dance_action_pair (&qk_tap_dance_state,
  30. action.pair.kc1, action.pair.kc2);
  31. break;
  32. case QK_TAP_DANCE_TYPE_FN:
  33. _process_tap_dance_action_fn (&qk_tap_dance_state, action.fn);
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. bool process_tap_dance(uint16_t keycode, keyrecord_t *record) {
  40. bool r = true;
  41. switch(keycode) {
  42. case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
  43. if (qk_tap_dance_state.keycode && qk_tap_dance_state.keycode != keycode) {
  44. process_tap_dance_action (qk_tap_dance_state.keycode);
  45. } else {
  46. r = false;
  47. }
  48. if (record->event.pressed) {
  49. qk_tap_dance_state.keycode = keycode;
  50. qk_tap_dance_state.timer = timer_read ();
  51. qk_tap_dance_state.count++;
  52. }
  53. break;
  54. default:
  55. if (qk_tap_dance_state.keycode) {
  56. process_tap_dance_action (qk_tap_dance_state.keycode);
  57. reset_tap_dance (&qk_tap_dance_state);
  58. }
  59. break;
  60. }
  61. return r;
  62. }
  63. void matrix_scan_tap_dance () {
  64. if (qk_tap_dance_state.keycode && timer_elapsed (qk_tap_dance_state.timer) > TAPPING_TERM) {
  65. process_tap_dance_action (qk_tap_dance_state.keycode);
  66. reset_tap_dance (&qk_tap_dance_state);
  67. }
  68. }
  69. void reset_tap_dance (qk_tap_dance_state_t *state) {
  70. state->keycode = 0;
  71. state->count = 0;
  72. }