summaryrefslogtreecommitdiff
path: root/users/ericgebhart/extensions/nshot_mod.c
blob: 1346f7eba6ff8a12225f231fa5544a5b631a67f3 (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
/*
  Copyright 2022 Eric Gebhart <e.a.gebhart@gmail.com>, @possumvibes

  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/>.
*/
// Derived from nshot_mod by @possumvibes.
// Derived from one shot_mod by @Callum.

#include "nshot_mod.h"
#include USERSPACE_H

#undef NSHOT
#define NSHOT(KEYCODE, MOD, COUNT)              \
  {KEYCODE, MOD, COUNT, os_up_unqueued, 0},

#undef ONESHOT
#define ONESHOT(KEYCODE, MOD) NSHOT(KEYCODE, MOD, 1)
#define A_KEY(KEYCODE) case KEYCODE:
#define BLANK(...)

#define CANCEL_KEY BLANK
#define IGNORE_KEY BLANK
nshot_state_t  nshot_states[] = {
#include "nshot.def"
};
uint8_t        NUM_NSHOT_STATES = sizeof(nshot_states) / sizeof(nshot_state_t);

bool process_nshot_state(uint16_t keycode, keyrecord_t *record) {
  nshot_state_t *curr_state = NULL;

  switch(keycode){
  case CLEAR: {
    clear_oneshot_mods();
    clear_mods();
    return false;
  }
  case PANIC: {
    clear_oneshot_mods();
    clear_mods();
    if (get_oneshot_layer() != 0) {
      clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
    }
    layer_move(0);
    return false;
  }
  }

  for (int i = 0; i < NUM_NSHOT_STATES; ++i) {
    curr_state = &nshot_states[i];

    if (keycode == curr_state->trigger) {
      if (record->event.pressed) {
        // Trigger keydown
        if (curr_state->state == os_up_unqueued) {
          register_code(curr_state->mod);
        }
        curr_state->state = os_down_unused;
        curr_state->count = 0;
      } else {
        // Trigger keyup
        switch (curr_state->state) {
        case os_down_unused:
          // If we didn't use the mod while trigger was held, queue it.
          curr_state->state = os_up_queued;
          break;
        case os_down_used:
          // If we did use the mod while trigger was held, unregister it.
          curr_state->state = os_up_unqueued;
          unregister_code(curr_state->mod);
          break;
        default:
          break;
        }
      }
} else {
       if (record->event.pressed) {
         if (is_nshot_cancel_key(keycode) && curr_state->state != os_up_unqueued) {
           // Cancel oneshot on designated cancel keydown.
           curr_state->state = os_up_unqueued;
           curr_state->count = 0;
           unregister_code(curr_state->mod);
         }
       } else {
         if (!is_nshot_ignored_key(keycode)) {
           // On non-ignored keyup, consider the oneshot used.
           switch (curr_state->state) {
           case os_down_unused:
             // The mod key is being held as a normal mod.
             curr_state->state = os_down_used;
             break;
           case os_up_queued:
             // The mod key is being used as an n-shot.
             // Increment the keys-used count.
             curr_state->count = curr_state->count + 1;

             // If the n-shot max has been reached, complete the n-shot.
             if (curr_state->count == curr_state->max_count) {
               curr_state->state = os_up_unqueued;
               curr_state->count = 0;
               unregister_code(curr_state->mod);
             }
             break;
           default:
             break;
           }
         }
       }
     }
   }
  return true;
}

// turn off the nshot/oneshot macros
#undef ONESHOT
#undef NSHOT
#define ONESHOT BLANK
#define NSHOT BLANK

#undef CANCEL_KEY
#undef IGNORE_KEY
#define IGNORE_KEY BLANK
#define CANCEL_KEY A_KEY
bool is_nshot_cancel_key(uint16_t keycode) {
  switch (keycode) {
#include "nshot.def"
    return true;
  default:
    return false;
  }
}

#undef CANCEL_KEY
#undef IGNORE_KEY
#define CANCEL_KEY BLANK
#define IGNORE_KEY A_KEY
bool is_nshot_ignored_key(uint16_t keycode) {
    switch (keycode) {
#include "nshot.def"
      return true;
    default:
            return false;
    }
}