You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.4 KiB

  1. #include "333fred.h"
  2. #include "quantum.h"
  3. #include "action.h"
  4. typedef enum {
  5. SINGLE_TAP, SINGLE_HOLD, DOUBLE
  6. } tap_dance_state_enum;
  7. static tap_dance_state_enum tap_dance_state;
  8. static bool tap_dance_active = false;
  9. void tap_dance_sym_vim_finished(qk_tap_dance_state_t *state, void *user_data) {
  10. // Determine the current state
  11. if (state->count == 1) {
  12. if (state->interrupted || state->pressed == 0) tap_dance_state = SINGLE_TAP;
  13. else tap_dance_state = SINGLE_HOLD;
  14. } else {
  15. // Handle any number of other taps as a VIM movement hold
  16. tap_dance_state = DOUBLE;
  17. }
  18. switch (tap_dance_state) {
  19. case SINGLE_TAP:
  20. if (tap_dance_active) {
  21. reset_oneshot_layer();
  22. tap_dance_active = false;
  23. } else {
  24. set_oneshot_layer(SYMB, ONESHOT_START);
  25. tap_dance_active = true;
  26. }
  27. break;
  28. case SINGLE_HOLD:
  29. layer_on(SYMB);
  30. break;
  31. case DOUBLE:
  32. layer_on(VIM);
  33. break;
  34. }
  35. }
  36. void tap_dance_sym_vim_reset(qk_tap_dance_state_t *state, void *user_data) {
  37. switch(tap_dance_state) {
  38. case SINGLE_TAP:
  39. clear_oneshot_layer_state(ONESHOT_PRESSED);
  40. break;
  41. case SINGLE_HOLD:
  42. layer_off(SYMB);
  43. break;
  44. case DOUBLE:
  45. layer_off(VIM);
  46. break;
  47. }
  48. }
  49. void tap_dance_copy_paste_finished(qk_tap_dance_state_t *state, void *user_data) {
  50. bool is_paste = state->count == 2;
  51. // If either the one-shot shift is set, or if shift is being held, count as shift being held.
  52. // We'll clear the one-shot shift if it was held
  53. uint8_t one_shot_mods = get_oneshot_mods();
  54. bool is_shift = false;
  55. if (get_mods() & MOD_MASK_SHIFT) {
  56. is_shift = true;
  57. } else if (one_shot_mods & MOD_MASK_SHIFT) {
  58. set_oneshot_mods(one_shot_mods & ~MOD_MASK_SHIFT);
  59. is_shift = true;
  60. }
  61. if (is_paste) {
  62. if (is_shift) {
  63. SEND_STRING(SS_LSFT(SS_TAP(X_INSERT)));
  64. } else {
  65. SEND_STRING(SS_LCTRL("v"));
  66. }
  67. } else {
  68. if (is_shift) {
  69. SEND_STRING(SS_LCTRL(SS_TAP(X_INSERT)));
  70. } else {
  71. SEND_STRING(SS_LCTRL("c"));
  72. }
  73. }
  74. }
  75. qk_tap_dance_action_t tap_dance_actions[] = {
  76. [TD_SYM_VIM] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, tap_dance_sym_vim_finished, tap_dance_sym_vim_reset),
  77. [TD_COPY_PASTE] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, tap_dance_copy_paste_finished, NULL)
  78. };
  79. void tap_dance_process_keycode(uint16_t keycode) {
  80. if (tap_dance_state == SINGLE_TAP && keycode != TD(TD_SYM_VIM)) {
  81. tap_dance_active = false;
  82. }
  83. }
  84. __attribute__ ((weak))
  85. void layer_state_set_rgb(uint32_t state) {}
  86. uint32_t layer_state_set_user(uint32_t state) {
  87. layer_state_set_rgb(state);
  88. return state;
  89. }
  90. bool try_handle_macro(uint16_t keycode, keyrecord_t *record) {
  91. switch (keycode)
  92. {
  93. case DLEFT:
  94. if (record->event.pressed)
  95. SEND_STRING(SS_LGUI(SS_LALT(SS_TAP(X_LEFT))));
  96. return true;
  97. case DRIGHT:
  98. if (record->event.pressed)
  99. SEND_STRING(SS_LGUI(SS_LALT(SS_TAP(X_RIGHT))));
  100. return true;
  101. case PSCREEN_APP:
  102. if (record->event.pressed)
  103. SEND_STRING(SS_LALT(SS_TAP(X_PSCREEN)));
  104. return true;
  105. default:
  106. return false;
  107. }
  108. }