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