aboutsummaryrefslogtreecommitdiff
path: root/users/haervig/haervig.c
diff options
context:
space:
mode:
Diffstat (limited to 'users/haervig/haervig.c')
-rw-r--r--users/haervig/haervig.c234
1 files changed, 234 insertions, 0 deletions
diff --git a/users/haervig/haervig.c b/users/haervig/haervig.c
new file mode 100644
index 000000000..d03b43edc
--- /dev/null
+++ b/users/haervig/haervig.c
@@ -0,0 +1,234 @@
1/*
2Copyright 2021 Jakob Hærvig <jakob.haervig@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "haervig.h"
19
20#ifdef DANISH_ENABLE
21// These indicate if left and right shift are physically pressed
22bool lshift = false;
23bool rshift = false;
24
25// Interrupt and times for space cadet shift
26bool lshiftp = false;
27bool rshiftp = false;
28uint16_t lshift_timer = 0;
29uint16_t rshift_timer = 0;
30
31// Number of items that are saved in prev_kcs
32uint8_t prev_indx = 0;
33// Used to save the last 6 actual keycodes activated by frankenkeycodes
34uint16_t prev_kcs[6] = {0, 0, 0, 0, 0, 0};
35
36// If true the deadkey characters grave and circonflexe are not automatically escaped
37bool esct = false;
38
39/*
40Used to add a keycode to a prev_kcs to remember it.
41When full the last code gets discarded and replaced by
42the new one.
43*/
44void add_to_prev(uint16_t kc){
45 for (int i=0; i<prev_indx; i++){
46 if (kc == prev_kcs[i])
47 return;
48 }
49 if (prev_indx == 6){
50 for (int i=5; i>0; i--){
51 prev_kcs[i] = prev_kcs[i-1];
52 }
53 prev_kcs[0] = kc;
54 } else {
55 prev_kcs[prev_indx] = kc;
56 prev_indx++;
57 }
58}
59
60/*
61Unregisters all codes saved in prev_kcs and resets prev_indx.
62gets called on multiple occasions mainly when shift is released
63and when frankenkeycodes are pressed. Prevents output of
64wrong characters when really specific key combinations
65that would never occur during normal usage are pressed.
66*/
67void unreg_prev(void){
68 if (prev_indx == 0)
69 return;
70 for (int i=0; i<prev_indx; i++){
71 unregister_code(prev_kcs[i]);
72 }
73 prev_indx = 0;
74}
75#endif
76
77// Interrupt and times for Nav/Esc
78bool navesc = false;
79uint16_t navesc_timer = 0;
80
81// Interrupts all timers
82void timer_timeout(void){
83 #ifdef DANISH_ENABLE
84 lshiftp = false;
85 rshiftp = false;
86 #endif
87 navesc = false;
88 timer_timeout_keymap();
89}
90
91__attribute__((weak))
92void timer_timeout_keymap(void){
93}
94
95bool process_record_user(uint16_t keycode, keyrecord_t *record) {
96 switch (keycode) {
97 case KC_LGUI:
98 case KC_RGUI:
99 if (record->event.pressed)
100 timer_timeout();
101 return true;
102 case CU_NAV:
103 if(record->event.pressed) {
104 navesc = true;
105 navesc_timer = timer_read();
106 layer_on(_NAV);
107 } else {
108 if (timer_elapsed(navesc_timer) < TAPPING_TERM && navesc) {
109 tap_code(KC_ESC);
110 }
111 layer_off(_NAV);
112 }
113 return false;
114
115 #ifdef DANISH_ENABLE
116 case CU_LSFT:
117 if(record->event.pressed) {
118 lshiftp = true;
119 lshift_timer = timer_read();
120 unregister_code(KC_LSFT);
121 register_code(KC_LSFT);
122 lshift = true;
123 } else {
124 if (timer_elapsed(lshift_timer) < TAPPING_TERM && lshiftp) {
125 register_code(KC_LSFT);
126 tap_code(KC_8);
127 unregister_code(KC_LSFT);
128 }
129 unreg_prev();
130 if (!rshift)
131 unregister_code(KC_LSFT);
132 lshift = false;
133 }
134 return false;
135 case CU_RSFT:
136 if(record->event.pressed) {
137 rshiftp = true;
138 rshift_timer = timer_read();
139 unregister_code(KC_LSFT);
140 register_code(KC_LSFT);
141 rshift = true;
142 } else {
143 if (timer_elapsed(rshift_timer) < TAPPING_TERM && rshiftp) {
144 register_code(KC_LSFT);
145 tap_code(KC_9);
146 unregister_code(KC_LSFT);
147 }
148 unreg_prev();
149 if (!lshift)
150 unregister_code(KC_LSFT);
151 rshift = false;
152 }
153 return false;
154 case CU_COMM:
155 SHIFT_NO(DK_COMM, KC_GRV)
156 case CU_DOT:
157 SHIFT_NORM(DK_DOT, KC_GRV)
158 case CU_SLSH:
159 SHIFT_ALL(DK_7, KC_MINS)
160 case CU_SCLN:
161 SHIFT_ALL(DK_COMM, DK_DOT)
162 case CU_QUOT:
163 SHIFT_NORM(DK_QUOT, DK_2)
164 case CU_2:
165 NORM_ALGR(DK_2, KC_NUHS)
166 case CU_4:
167 if (record->event.pressed) { \
168 timer_timeout(); \
169 if (lshift || rshift) { \
170 register_code(KC_LSFT); \
171 register_code(KC_ALGR); \
172 unregister_code(KC_3); \
173 tap_code(KC_3); \
174 unregister_code(KC_3); \
175 } else { \
176 unregister_code(KC_4); \
177 tap_code(KC_4); \
178 } \
179 unregister_code(KC_ALGR); \
180 unregister_code(KC_LSFT); \
181 } \
182 return false;
183 case CU_6:
184 SHIFT_NORM(DK_6, KC_RBRC)
185 case CU_7:
186 SHIFT_NORM(DK_7, DK_6)
187 case CU_8:
188 SHIFT_NORM(DK_8, KC_NUHS)
189 case CU_9:
190 SHIFT_NORM(DK_9, DK_8)
191 case CU_0:
192 SHIFT_NORM(DK_0, DK_9)
193 case CU_MINS:
194 SHIFT_NORM(KC_SLSH, KC_SLSH)
195 case CU_EQL:
196 SHIFT_SWITCH(DK_0, DK_PLUS)
197 case CU_BSPC:
198 SHIFT_NO(KC_BSPC, KC_DEL)
199 case CU_LBRC:
200 NORM_ALGRSHIFT(DK_8, DK_8)
201 case CU_RBRC:
202 NORM_ALGRSHIFT(DK_9, DK_9)
203 case CU_BSLS:
204 ALGR_SWITCH(DK_7, DK_I)
205 case KC_LCTL:
206 case KC_RCTL:
207 if(!record->event.pressed) {
208 timer_timeout();
209 unregister_code(KC_Z);
210 unregister_code(KC_Y);
211 }
212 return true;
213 #endif
214
215 default:
216 if(record->event.pressed) {
217 timer_timeout();
218
219 #ifdef DANISH_ENABLE
220 if (lshift || rshift)
221 register_code(KC_LSFT);
222 else
223 unregister_code(KC_LSFT);
224 #endif
225
226 }
227 return process_record_keymap(keycode, record);
228 }
229}
230
231__attribute__((weak))
232bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
233 return true;
234}