summaryrefslogtreecommitdiffstats
path: root/keyboards/planck/keymaps/corvec/tapdance.c
blob: 35eef2d838f99baa66869900a18f90961cbe495e (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
/**
 * Tap Dance config and functions
 **/
/* Copyright 2018 Corey Kump
 *
 * 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/>.
 */

enum {
    SINGLE_TAP = 1,
    SINGLE_HOLD = 2,
    DOUBLE_TAP = 3,
    DOUBLE_SINGLE_TAP = 4,
    UNKNOWN_TAPS = 5
};

typedef struct {
  bool is_press_action;
  int state;
} tap;

int cur_dance (tap_dance_state_t *state) {
    if (state->count == 1) {
      if (state->interrupted || !state->pressed) {
        return SINGLE_TAP;
      } else {
        return SINGLE_HOLD;
      }
    }
    if (state->count == 2) {
      if (state->interrupted) {
        return DOUBLE_SINGLE_TAP;
      } else if (!state->pressed) {
        return DOUBLE_TAP;
      }
    }
    return UNKNOWN_TAPS;
}

/**
 * quote
 **/

static tap quote_state = {
  .is_press_action = true,
  .state = 0
};

void quote_finished(tap_dance_state_t *state, void *user_data) {
    quote_state.state = cur_dance(state);
    switch(quote_state.state) {
      case SINGLE_TAP: register_code(KC_QUOT); break;
      case SINGLE_HOLD: layer_on(_LEANDOWN); break;
      case DOUBLE_TAP: register_code(KC_MINS); break;
      case DOUBLE_SINGLE_TAP: register_code(KC_QUOT); unregister_code(KC_QUOT); register_code(KC_QUOT); break;
    }
}
void quote_reset(tap_dance_state_t *state, void *user_data) {
    switch(quote_state.state) {
      case SINGLE_TAP: unregister_code(KC_QUOT); break;
      case SINGLE_HOLD: layer_off(_LEANDOWN); break;
      case DOUBLE_TAP: unregister_code(KC_MINS); break;
      case DOUBLE_SINGLE_TAP: unregister_code(KC_QUOT); break;
    }
    quote_state.state = 0;
}

/**
 * ralt / left bracket / left curly brace
 **/

static tap ralt_state = {
  .is_press_action = true,
  .state = 0
};

void ralt_finished(tap_dance_state_t *state, void *user_data) {
    ralt_state.state = cur_dance(state);
    switch(ralt_state.state) {
      case SINGLE_TAP: register_code(KC_LBRC); break;
      case SINGLE_HOLD: register_code(KC_RALT); break;
      case DOUBLE_TAP: register_code(KC_LSFT); register_code(KC_LBRC); break;
      // fallback to alt because it's the primary purpose of this key
      case DOUBLE_SINGLE_TAP: register_code(KC_RALT); break;
    }
}
void ralt_reset(tap_dance_state_t *state, void *user_data) {
    switch(ralt_state.state) {
      case SINGLE_TAP: unregister_code(KC_LBRC); break;
      case SINGLE_HOLD: unregister_code(KC_RALT); break;
      case DOUBLE_TAP: unregister_code(KC_LBRC); unregister_code(KC_LSFT); break;
      case DOUBLE_SINGLE_TAP: unregister_code(KC_RALT); break;
    }
    ralt_state.state = 0;
}

/**
 * rgui / right bracket / right curly brace
 **/

static tap rgui_state = {
  .is_press_action = true,
  .state = 0
};

void rgui_finished(tap_dance_state_t *state, void *user_data) {
    rgui_state.state = cur_dance(state);
    switch(rgui_state.state) {
      case SINGLE_TAP: register_code(KC_RBRC); break;
      case SINGLE_HOLD: register_code(KC_RGUI); break;
      case DOUBLE_TAP: register_code(KC_LSFT); register_code(KC_RBRC); break;
      // fallback to alt because it's the primary purpose of this key
      case DOUBLE_SINGLE_TAP: register_code(KC_RGUI); break;
    }
}
void rgui_reset(tap_dance_state_t *state, void *user_data) {
    switch(rgui_state.state) {
      case SINGLE_TAP: unregister_code(KC_RBRC); break;
      case SINGLE_HOLD: unregister_code(KC_RGUI); break;
      case DOUBLE_TAP: unregister_code(KC_RBRC); unregister_code(KC_LSFT); break;
      case DOUBLE_SINGLE_TAP: unregister_code(KC_RGUI); break;
    }
    rgui_state.state = 0;
}

/**
 * Define the available tap dance keys
 **/

enum {
  TD_ALT_SHIFT = 0,
  TD_LQUT_MINS,
  TD_QUOT_LEAN_MINS,
  TD_LBRC_RALT_LCBR,
  TD_RBRC_RGUI_RCBR
};

tap_dance_action_t tap_dance_actions[] = {
  // Tap once for Alt, twice for Shift
  [TD_ALT_SHIFT] = ACTION_TAP_DANCE_DOUBLE(KC_RALT, KC_RSFT),
  [TD_QUOT_LEAN_MINS] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, quote_finished, quote_reset),
  [TD_LBRC_RALT_LCBR] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, ralt_finished, ralt_reset),
  [TD_RBRC_RGUI_RCBR] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, rgui_finished, rgui_reset)
};