process_tap_dance.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #pragma once
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include "action.h"
  20. typedef struct {
  21. uint16_t interrupting_keycode;
  22. uint8_t count;
  23. uint8_t weak_mods;
  24. #ifndef NO_ACTION_ONESHOT
  25. uint8_t oneshot_mods;
  26. #endif
  27. bool pressed : 1;
  28. bool finished : 1;
  29. bool interrupted : 1;
  30. } tap_dance_state_t;
  31. typedef void (*tap_dance_user_fn_t)(tap_dance_state_t *state, void *user_data);
  32. typedef struct {
  33. tap_dance_state_t state;
  34. struct {
  35. tap_dance_user_fn_t on_each_tap;
  36. tap_dance_user_fn_t on_dance_finished;
  37. tap_dance_user_fn_t on_reset;
  38. tap_dance_user_fn_t on_each_release;
  39. } fn;
  40. void *user_data;
  41. } tap_dance_action_t;
  42. typedef struct {
  43. uint16_t kc1;
  44. uint16_t kc2;
  45. } tap_dance_pair_t;
  46. typedef struct {
  47. uint16_t kc;
  48. uint8_t layer;
  49. void (*layer_function)(uint8_t);
  50. } tap_dance_dual_role_t;
  51. #define ACTION_TAP_DANCE_DOUBLE(kc1, kc2) \
  52. { .fn = {tap_dance_pair_on_each_tap, tap_dance_pair_finished, tap_dance_pair_reset, NULL}, .user_data = (void *)&((tap_dance_pair_t){kc1, kc2}), }
  53. #define ACTION_TAP_DANCE_LAYER_MOVE(kc, layer) \
  54. { .fn = {tap_dance_dual_role_on_each_tap, tap_dance_dual_role_finished, tap_dance_dual_role_reset, NULL}, .user_data = (void *)&((tap_dance_dual_role_t){kc, layer, layer_move}), }
  55. #define ACTION_TAP_DANCE_LAYER_TOGGLE(kc, layer) \
  56. { .fn = {NULL, tap_dance_dual_role_finished, tap_dance_dual_role_reset, NULL}, .user_data = (void *)&((tap_dance_dual_role_t){kc, layer, layer_invert}), }
  57. #define ACTION_TAP_DANCE_FN(user_fn) \
  58. { .fn = {NULL, user_fn, NULL, NULL}, .user_data = NULL, }
  59. #define ACTION_TAP_DANCE_FN_ADVANCED(user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset) \
  60. { .fn = {user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset, NULL}, .user_data = NULL, }
  61. #define ACTION_TAP_DANCE_FN_ADVANCED_WITH_RELEASE(user_fn_on_each_tap, user_fn_on_each_release, user_fn_on_dance_finished, user_fn_on_dance_reset) \
  62. { .fn = {user_fn_on_each_tap, user_fn_on_dance_finished, user_fn_on_dance_reset, user_fn_on_each_release}, .user_data = NULL, }
  63. #define TD(n) (QK_TAP_DANCE | TD_INDEX(n))
  64. #define TD_INDEX(code) ((code)&0xFF)
  65. #define TAP_DANCE_KEYCODE(state) TD(((tap_dance_action_t *)state) - tap_dance_actions)
  66. extern tap_dance_action_t tap_dance_actions[];
  67. void reset_tap_dance(tap_dance_state_t *state);
  68. /* To be used internally */
  69. bool preprocess_tap_dance(uint16_t keycode, keyrecord_t *record);
  70. bool process_tap_dance(uint16_t keycode, keyrecord_t *record);
  71. void tap_dance_task(void);
  72. void tap_dance_pair_on_each_tap(tap_dance_state_t *state, void *user_data);
  73. void tap_dance_pair_finished(tap_dance_state_t *state, void *user_data);
  74. void tap_dance_pair_reset(tap_dance_state_t *state, void *user_data);
  75. void tap_dance_dual_role_on_each_tap(tap_dance_state_t *state, void *user_data);
  76. void tap_dance_dual_role_finished(tap_dance_state_t *state, void *user_data);
  77. void tap_dance_dual_role_reset(tap_dance_state_t *state, void *user_data);