summaryrefslogtreecommitdiff
path: root/users/rverst/rverst.c
blob: 23391caa6a6c821808513be79a11b564f242b559 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* Copyright 2021 Robert Verst <robert@verst.eu> @rverst
 *
 * 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/>.
 */

#include "rverst.h"
#include "print.h"

#ifdef UNICODEMAP_ENABLE
#    include "unicode.h"
#endif

userspace_config_t userspace_config;

uint8_t get_mode(void) {
    int m = 0;
    if (userspace_config.mode_1) {
        m += 1;
    }
    if (userspace_config.mode_2) {
        m += 2;
    }
    if (userspace_config.mode_3) {
        m += 4;
    }

    return m;
}

void set_mode(uint8_t mode, bool save) {
    if (mode == get_mode()) {
        return;
    }
    switch_mode(mode);

    if (mode > 7) {
        mode = 7;
    }

    if (mode >= 4) {
        userspace_config.mode_3 = true;
        mode -= 4;
    } else {
        userspace_config.mode_3 = false;
    }

    if (mode >= 2) {
        userspace_config.mode_2 = true;
        mode -= 2;
    } else {
        userspace_config.mode_2 = false;
    }

    if (mode > 0) {
        userspace_config.mode_1 = true;
    } else {
        userspace_config.mode_1 = false;
    }

    if (save) {
        eeconfig_update_user(userspace_config.raw);
    }
}

void switch_mode(uint8_t mode) {
#ifdef UNICODEMAP_ENABLE
    switch (mode) {
        case MAC_UNI:
            set_unicode_input_mode(UC_MAC);
            break;
        case WINDOWS_UNI:
            set_unicode_input_mode(UC_WINC);
            break;
        case LINUX_UNI:
            set_unicode_input_mode(UC_LNX);
            break;
    }
#endif
}

bool is_unicode(uint8_t mode) { return (mode == MAC_UNI) || (mode == WINDOWS_UNI) || (mode == LINUX_UNI); }

//**********************
// keyboard_pre_init
//**********************
__attribute__((weak)) void keyboard_pre_init_keymap(void) {}

void keyboard_pre_init_user(void) {
    userspace_config.raw = eeconfig_read_user();
    switch_mode(get_mode());
    keyboard_pre_init_keymap();
}

//************************
// keyboard_post_init
//************************
__attribute__((weak)) void keyboard_post_init_keymap(void) {}

void keyboard_post_init_user(void) {
    // debug_enable = true;
    // debug_matrix=true;
    // debug_keyboard = true;

#ifdef RGBLIGHT_ENABLE

#endif

    keyboard_post_init_keymap();
}

//**********************
// eeconfig_init
//**********************

__attribute__((weak)) void eeconfig_init_keymap(void) {}

void eeconfig_init_user(void) {
    userspace_config.raw = 0;
    eeconfig_update_user(userspace_config.raw);
    eeconfig_init_keymap();
    keyboard_init();
}

//**********************
// process_record
//**********************
__attribute__((weak)) bool process_record_keymap(uint16_t keycode, keyrecord_t *record) { return true; }

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
    if (!process_record_keymap(keycode, record)) {
        return false;
    }

    if (!record->event.pressed) {
        return true;
    }

    bool ls = (get_mods() | get_weak_mods()) & MOD_BIT(KC_LSFT);
    bool rs = (get_mods() | get_weak_mods()) & MOD_BIT(KC_RSFT);
    bool as = ls || rs;

    int mode = get_mode();

    switch (keycode) {
        case RV_SM0:
        case RV_SM0S:
            set_mode(MAC_UNI, keycode == RV_SM0S);
            return false;
        case RV_SM1:
        case RV_SM1S:
            set_mode(WINDOWS_UNI, keycode == RV_SM1S);
            return false;
        case RV_SM2:
        case RV_SM2S:
            set_mode(LINUX_UNI, keycode == RV_SM2S);
            return false;
        case RV_SM3:
        case RV_SM3S:
            set_mode(MAC, keycode == RV_SM3S);
            return false;
        case RV_SM4:
        case RV_SM4S:
            set_mode(WINDOWS, keycode == RV_SM4S);
            return false;

        case RV_SAYM:
            switch (mode) {
                case MAC:
                    send_string("MacOS (normal)");
                    break;
                case WINDOWS:
                    send_string("Windows (normal)");
                    break;
                case MAC_UNI:
                    send_string("MacOS (unicode)");
                    break;
                case WINDOWS_UNI:
                    send_string("Windows (unicode)");
                    break;
                case LINUX_UNI:
                    send_string("Linux (unicode)");
                    break;
            }
            return false;

        // Lock computer
        case RV_LOCK:
            if (mode == MAC || mode == MAC_UNI) {
                tap_code16(G(C(KC_Q)));
            } else if (mode == WINDOWS || mode == WINDOWS_UNI) {
                tap_code16(G(KC_L));
            }
            return false;

        // Screenshot
        case RV_SNAP:
            if (mode == MAC || mode == MAC_UNI) {
                if (ls) unregister_code(KC_LSFT);
                if (rs) unregister_code(KC_RSFT);

                tap_code16(G(S(as ? KC_4 : KC_5)));

                if (ls) register_code(KC_LSFT);
                if (rs) register_code(KC_RSFT);
            } else if (mode == WINDOWS || mode == WINDOWS_UNI) {
                tap_code16(G(S(KC_S)));
            }
            return false;

        // Umlauts - äÄöÖüÜ
        case RV_AUML:
        case RV_OUML:
        case RV_UUML:
            if (is_unicode(mode)) {
                if (keycode == RV_AUML) {
                    if (as)
                        send_unicode_string("Ä");
                    else
                        send_unicode_string("ä");
                } else if (keycode == RV_OUML) {
                    if (as)
                        send_unicode_string("Ö");
                    else
                        send_unicode_string("ö");
                } else if (keycode == RV_UUML) {
                    if (as)
                        send_unicode_string("Ü");
                    else
                        send_unicode_string("ü");
                }
            } else if (mode == MAC) {
                if (ls) unregister_code(KC_LSFT);
                if (rs) unregister_code(KC_RSFT);

                tap_code16(A(KC_U));

                if (as) register_code(KC_LSFT);
                if (keycode == RV_AUML) {
                    tap_code(KC_A);
                } else if (keycode == RV_OUML) {
                    tap_code(KC_O);
                } else if (keycode == RV_UUML) {
                    tap_code(KC_U);
                }
                if (rs) {
                    unregister_code(KC_LSFT);
                    register_code(KC_RSFT);
                }
            } else if (mode == WINDOWS) {
                if (ls) unregister_code(KC_LSFT);
                if (rs) unregister_code(KC_RSFT);

                register_code(KC_RALT);
                tap_code(KC_1);
                if (keycode == RV_AUML) {
                    if (as)
                        tap_code(KC_4);
                    else
                        tap_code(KC_3);
                    tap_code(KC_2);
                } else if (keycode == RV_OUML) {
                    if (as) {
                        tap_code(KC_5);
                        tap_code(KC_3);
                    } else {
                        tap_code(KC_4);
                        tap_code(KC_8);
                    }
                } else if (keycode == RV_UUML) {
                    if (as) {
                        tap_code(KC_5);
                        tap_code(KC_4);
                    } else {
                        tap_code(KC_2);
                        tap_code(KC_9);
                    }
                }
                unregister_code(KC_RALT);

                if (ls) register_code(KC_LSFT);
                if (rs) register_code(KC_RSFT);
            }
            return false;

        // Euro sign - €
        // with legacy-mode for MAC and WINDOWS without unicode support
        case RV_EUR:
            if (is_unicode(mode)) {
                send_unicode_string("€");
            } else if (mode == MAC) {
                tap_code16(S(A(KC_2)));
            } else if (mode == WINDOWS) {
                register_code(KC_RALT);
                tap_code(KC_0);
                tap_code(KC_1);
                tap_code(KC_2);
                tap_code(KC_8);
                unregister_code(KC_RALT);
            }
            return false;

        // Sharp-S - ß
        // with legacy-mode for MAC and WINDOWS without unicode support
        case RV_SZ:
            if (is_unicode(mode)) {
                if (as) {
                    send_unicode_string("§");
                } else {
                    send_unicode_string("ß");
                }
            } else if (mode == MAC) {
                tap_code16(A(KC_S));
            } else if (mode == WINDOWS) {
                register_code(KC_RALT);
                tap_code(KC_2);
                tap_code(KC_2);
                tap_code(KC_5);
                unregister_code(KC_RALT);
            }
            return false;

        // Trademark - ™
        case RV_TM:
            if (is_unicode(mode)) {
                send_unicode_string("™");
            }
            return false;

        // Registered trademark - ®
        case RV_RT:
            if (is_unicode(mode)) {
                send_unicode_string("®");
            }
            return false;

        // Copyright - ©
        case RV_CC:
            if (is_unicode(mode)) {
                send_unicode_string("©");
            }
            return false;

        // Degree - °
        case RV_DEG:
            if (is_unicode(mode)) {
                send_unicode_string("°");
            }
            return false;

        // Plus-minus - ±
        case RV_PM:
            if (is_unicode(mode)) {
                send_unicode_string("±");
            }
            return false;

        // Not equal - ≠
        case RV_UNEQ:
            if (is_unicode(mode)) {
                send_unicode_string("≠");
            }
            return false;

        // Superscript one - ¹
        case RV_SUP1:
            if (is_unicode(mode)) {
                send_unicode_string("¹");
            }
            return false;

        // Superscript two - ²
        case RV_SUP2:
            if (is_unicode(mode)) {
                send_unicode_string("²");
            }
            return false;

        // Superscript three - ³
        case RV_SUP3:
            if (is_unicode(mode)) {
                send_unicode_string("³");
            }
            return false;
    }

    return true;
}