summaryrefslogtreecommitdiffstats
path: root/users/ericgebhart/extensions/tap_dances.c
blob: b292dab67816d492ebf4a183138ad51a936d6050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
  Copyright 2018 Eric Gebhart <e.a.gebhart@gmail.com>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef TAP_DANCES_ENABLE

#include "tap_dances.h"

#include "action.h"
#include "action_layer.h"
#include "process_keycode/process_tap_dance.h"

void tap_dance_mouse_btns (qk_tap_dance_state_t *state, void *user_data) {
    switch(state->count){
    case 1:
        register_code(KC_BTN1);
        break;
    case 2:
        register_code(KC_BTN2);
        break;
    case 3:
        register_code(KC_BTN3);
        break;
    case 4:
        register_code(KC_BTN4);
        break;
    case 5:
        register_code(KC_BTN5);
        break;
    default:
        break;
    }
    reset_tap_dance(state);
}

// counting on all the qwerty layers to be less than dvorak_on_bepo
int on_qwerty(){
    uint8_t deflayer = (biton32(default_layer_state));
    switch(deflayer){
    case _DVORAK_BP:
    case _BEAKL_BP:
    case _BEPO:
      return (false);
    default:
      break;
    }
    return (true);
}

static void switch_default_layer(uint8_t layer) {
  default_layer_set(1UL<<layer);
  clear_keyboard();
}

// so the keyboard remembers which layer it's in after power disconnect.
/*
  uint32_t default_layer_state_set_kb(uint32_t state) {
  eeconfig_update_default_layer(state);
  return state;
  }
*/

void tap_dance_df_bepo_layers_switch (qk_tap_dance_state_t *state, void *user_data) {
  switch(state->count){
  case 1:
    switch_default_layer(_DVORAK_BP);
    break;
  case 2:
    switch_default_layer(_BEPO);
    break;
  case 3:
    layer_invert(_LAYERS);
    break;
  default:
    break;
  }
  reset_tap_dance(state);
}

void tap_dance_layer_switch (qk_tap_dance_state_t *state, void *user_data) {
  switch(state->count){
  case 1:
    if(on_qwerty())
      layer_invert(_SYMB);
    else
      layer_invert(_SYMB_BP);
    break;
  case 2:
    layer_invert(_NAV);
    break;
  case 3:
    layer_invert(_LAYERS);
    break;
  case 4:
    if(on_qwerty())
      layer_invert(_KEYPAD);
    else
      layer_invert(_KEYPAD_BP);
    break;
  default:
    break;
  }
  reset_tap_dance(state);
}

void tap_dance_default_layer_switch (qk_tap_dance_state_t *state, void *user_data) {
    switch(state->count){
    case 1:
      switch_default_layer(_DVORAK);
      break;
    case 2:
      switch_default_layer(_DVORAK_BP);
      break;
    case 3:
      switch_default_layer(_BEPO);
      break;
    default:
        break;
    }
    reset_tap_dance(state);
}

// switch the default layer to another qwerty based layer.
void switch_default_layer_on_qwerty(int count) {
    switch(count){
    case 1:
      switch_default_layer(_DVORAK);
      break;
    case 2:
      switch_default_layer(_QWERTY);
      break;
    case 3:
      switch_default_layer(_COLEMAK);
      break;

      /* case 4: */
      /*   switch_default_layer(_WORKMAN); */
      /*   break; */
      /* case 5: */
      /*   switch_default_layer(_NORMAN); */
      /*   break; */

    default:
      switch_default_layer(_DVORAK);
      break;
    }
}

// switch the default layer to another bepo based layer.
void switch_default_layer_on_bepo(int count) {
    switch(count){
    case 1:
      switch_default_layer(_DVORAK_BP);
      break;
    case 2:
      switch_default_layer(_BEPO);
      break;
    default:
      switch_default_layer(_DVORAK_BP);
      break;
    }
}


// tap to change the default layer. Distinguishes between layers that are based on
// a qwerty software keyboard and a bepo software keyboard.
// if shifted, choose layers based on the other software keyboard, otherwise choose only
// layers that work on the current software keyboard.
void tap_dance_default_os_layer_switch (qk_tap_dance_state_t *state, void *user_data) {
    //uint8_t shifted = (get_mods() & MOD_BIT(KC_LSFT|KC_RSFT));
    bool shifted = ( keyboard_report->mods & (MOD_BIT(KC_LSFT)|MOD_BIT(KC_RSFT)) );
    int qwerty = on_qwerty();


    // shifted, choose between layers on the other software keyboard
    if(shifted){
        if (qwerty)
            switch_default_layer_on_bepo(state->count);
        else
            switch_default_layer_on_qwerty(state->count);

        // not shifted, choose between layers on the same software keyboard
    } else {
        if (qwerty)
            switch_default_layer_on_qwerty(state->count);
        else
            switch_default_layer_on_bepo(state->count);
    }

    reset_tap_dance(state);
}


/* Return an integer that corresponds to what kind of tap dance should be executed.
 *
 * How to figure out tap dance state: interrupted and pressed.
 *
 * Interrupted: If the state of a dance dance is "interrupted", that means that another key has been hit
 *  under the tapping term. This is typically indicitive that you are trying to "tap" the key.
 *
 * Pressed: Whether or not the key is still being pressed. If this value is true, that means the tapping term
 *  has ended, but the key is still being pressed down. This generally means the key is being "held".
 *
 * One thing that is currenlty not possible with qmk software in regards to tap dance is to mimic the "permissive hold"
 *  feature. In general, advanced tap dances do not work well if they are used with commonly typed letters.
 *  For example "A". Tap dances are best used on non-letter keys that are not hit while typing letters.
 *
 * Good places to put an advanced tap dance:
 *  z,q,x,j,k,v,b, any function key, home/end, comma, semi-colon
 *
 * Criteria for "good placement" of a tap dance key:
 *  Not a key that is hit frequently in a sentence
 *  Not a key that is used frequently to double tap, for example 'tab' is often double tapped in a terminal, or
 *    in a web form. So 'tab' would be a poor choice for a tap dance.
 *  Letters used in common words as a double. For example 'p' in 'pepper'. If a tap dance function existed on the
 *    letter 'p', the word 'pepper' would be quite frustating to type.
 *
 * For the third point, there does exist the 'DOUBLE_SINGLE_TAP', however this is not fully tested
 *
 */
int cur_dance (qk_tap_dance_state_t *state) {
    if (state->count == 1) {
        if (state->interrupted || !state->pressed)  return SINGLE_TAP;
        //key has not been interrupted, but they key is still held. Means you want to send a 'HOLD'.
        else return SINGLE_HOLD;
    }
    else if (state->count == 2) {
        /*
         * DOUBLE_SINGLE_TAP is to distinguish between typing "pepper", and actually wanting a double tap
         * action when hitting 'pp'. Suggested use case for this return value is when you want to send two
         * keystrokes of the key, and not the 'double tap' action/macro.
         */
        if (state->interrupted) return DOUBLE_SINGLE_TAP;
        else if (state->pressed) return DOUBLE_HOLD;
        else return DOUBLE_TAP;
    }
    //Assumes no one is trying to type the same letter three times (at least not quickly).
    //If your tap dance key is 'KC_W', and you want to type "www." quickly - then you will need to add
    //an exception here to return a 'TRIPLE_SINGLE_TAP', and define that enum just like 'DOUBLE_SINGLE_TAP'
    if (state->count == 3) {
        if (state->interrupted || !state->pressed)  return TRIPLE_TAP;
        else return TRIPLE_HOLD;
    }
    else return 8; //magic number. At some point this method will expand to work for more presses
}

//Tap Dance Definitions
qk_tap_dance_action_t tap_dance_actions[] = {
    //Tap once for Esc, twice for Caps Lock
    [TD_ESC_CAPS]  = ACTION_TAP_DANCE_DOUBLE(KC_ESC, KC_CAPS),
    [TD_TAB_BKTAB] = ACTION_TAP_DANCE_DOUBLE(KC_TAB, LSFT(KC_TAB)),
    [TD_RIGHT_TAB] = ACTION_TAP_DANCE_DOUBLE(KC_RIGHT, KC_TAB),
    [TD_LEFT_BACKTAB] = ACTION_TAP_DANCE_DOUBLE(KC_LEFT, LSFT(KC_TAB)),
    [TD_UP_HOME] = ACTION_TAP_DANCE_DOUBLE(KC_UP, KC_HOME),
    [TD_DOWN_END] = ACTION_TAP_DANCE_DOUBLE(KC_DOWN, KC_END),
    [TD_MDIA_SYMB] = ACTION_TAP_DANCE_FN(tap_dance_layer_switch),
    [TD_DVORAK_BEPO] = ACTION_TAP_DANCE_FN(tap_dance_df_bepo_layers_switch),
    [TD_DEF_LAYER_SW] = ACTION_TAP_DANCE_FN(tap_dance_default_layer_switch),
    [TD_DEF_OS_LAYER_SW] = ACTION_TAP_DANCE_FN(tap_dance_default_os_layer_switch),
    [TD_HOME_END]  = ACTION_TAP_DANCE_DOUBLE(KC_HOME, KC_END),
    [TD_MOUSE_BTNS] = ACTION_TAP_DANCE_FN(tap_dance_mouse_btns)

};

#endif