summaryrefslogtreecommitdiffstats
path: root/keyboards/planck/keymaps/orthodeluxe/keymap.c
blob: f7a48342274551010d8b0e8517bf2c232bd2fee2 (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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/* Copyright 2020 Erik Bakker
 *
 * 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 QMK_KEYBOARD_H
#include "muse.h"

enum planck_layers {
    _BASELAYER_MACOS,
    _BASELAYER_WINDOWS,
    _LEFTYLAYER,
    _NUMLAYER_MACOS,
    _NUMLAYER_WINDOWS,
    _NAVLAYER_MACOS,
    _NAVLAYER_WINDOWS,
    _SYMLAYER_MACOS,
    _SYMLAYER_WINDOWS,
    _UTILSLAYER
};

enum planck_keycodes {
    MACOS = SAFE_RANGE, 
    WINDOWS,
    RCMDCTRL, 
    LEFTYCUT, LEFTYCOPY, LEFTYPASTE,
    PLUSCOLON,
    APPSWITCH, TASKVIEW, PREVDESK, NEXTDESK,
    QUOTES, DQUOTES,
    POINTYBR, SQUAREBR, CURLYBR, ROUNDBR
};

enum appswitch_mode {
    APPSWITCH_OFF, 
    APPSWITCH_FIRST_TIME,
    APPSWITCH_ON,
    APPSWITCH_TASK_VIEW
};

enum quote_shapes {
    SINGLE_QUOTE, DOUBLE_QUOTE
};

enum bracket_shapes {
    POINTY, SQUARE, CURLY, ROUND
};

uint8_t windowsmode = 0;

#define L_NUM_M MO(_NUMLAYER_MACOS)
#define L_NUM_W MO(_NUMLAYER_WINDOWS)
#define L_NAV_M MO(_NAVLAYER_MACOS)
#define L_NAV_W MO(_NAVLAYER_WINDOWS)
#define L_SYM_M MO(_SYMLAYER_MACOS)
#define L_SYM_W MO(_SYMLAYER_WINDOWS)
#define UTILSLAYER MO(_UTILSLAYER)

static void set_keycodes_for_os(int os);
static bool handle_app_switch(bool pressed);
static bool app_switch_valid_keycode(uint16_t keycode);
static bool handle_right_command_or_control(bool pressed);
static bool handle_pluscolon(bool pressed);
static bool type_quotes(uint8_t quote_shape, bool pressed);
static bool type_brackets(uint8_t bracket_shape, bool pressed);

const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {

/* Base layer:
 * ,-----------------------------------------------------------------------------------.
 * |BckSpc|   Q  |   W  |   E  |   R  |   T  |   Y  |   U  |   I  |   O  |   P  |  Del |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |  Esc |   A  |   S  |   D  |   F  |   G  |   H  |   J  |   K  |   L  | ;  : | Enter|
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * | Tab- |   Z  |   X  |   C  |   V  |   B  |   N  |   M  | ,  < | .  > | /  ? |  Tab |
 * |------+------+------+------+------+-------------+------+------+------+------+------|
 * | Shift|      |      |      |NumLyr|    Space    |NavLyr|      |      |      | Shift|
 * `-----------------------------------------------------------------------------------'    
 *
 * Bottom row for MacOS:
 * |------+------+------+------+------+-------------+------+------+------+------+------|
 * | Shift| Ctrl |Option|  Cmd |NumLyr|    Space    |NavLyr|  Cmd |Option| Ctrl | Shift|
 * `-----------------------------------------------------------------------------------'
 *
 * Bottom row for Windows: 
 * |------+------+------+------+------+-------------+------+------+------+------+------|
 * | Shift|  Win |  Alt | Ctrl |NumLyr|    Space    |NavLyr| Ctrl |  Alt | Menu | Shift|
 * `-----------------------------------------------------------------------------------'
 */
[_BASELAYER_MACOS] = LAYOUT_planck_grid(
    KC_BSPC, KC_Q,     KC_W,    KC_E,     KC_R,    KC_T,    KC_Y,    KC_U,    KC_I,     KC_O,    KC_P,     KC_DEL,
    KC_ESC,  KC_A,     KC_S,    KC_D,     KC_F,    KC_G,    KC_H,    KC_J,    KC_K,     KC_L,    KC_SCLN,  KC_ENT,
    _______, KC_Z,     KC_X,    KC_C,     KC_V,    KC_B,    KC_N,    KC_M,    KC_COMM,  KC_DOT,  KC_SLSH,  KC_TAB,
    KC_LSFT, KC_LCTRL, KC_LALT, KC_LGUI,  L_NUM_M, KC_SPC,  KC_SPC,  L_NAV_M, RCMDCTRL, KC_RALT, KC_RCTRL, KC_RSFT
),
[_BASELAYER_WINDOWS] = LAYOUT_planck_grid(
    KC_BSPC, KC_Q,     KC_W,    KC_E,     KC_R,    KC_T,    KC_Y,    KC_U,    KC_I,     KC_O,    KC_P,     KC_DEL,
    KC_ESC,  KC_A,     KC_S,    KC_D,     KC_F,    KC_G,    KC_H,    KC_J,    KC_K,     KC_L,    KC_SCLN,  KC_ENT,
    _______, KC_Z,     KC_X,    KC_C,     KC_V,    KC_B,    KC_N,    KC_M,    KC_COMM,  KC_DOT,  KC_SLSH,  KC_TAB,
    KC_LSFT, KC_LGUI,  KC_LALT, KC_LCTRL, L_NUM_W, KC_SPC,  KC_SPC,  L_NAV_W, RCMDCTRL, KC_RALT, KC_APP,   KC_RSFT
),

/* Lefty layer:
 * This layer enables lefties (who mouse with their left hand) to easily cut, copy and paste using their right hand.
 * ,-----------------------------------------------------------------------------------.
 * |      |      |      |      |      |      |      |      |      |      |      |      |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |      |      |      |      |      |      |      |      |      |      |      |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |      |      |      |      |      |      |  Cut | Copy | Paste|      |      |
 * |------+------+------+------+------+-------------+------+------+------+------+------|
 * |      |      |      |      |      |             |      |(hold)|      |      |      |
 * '-----------------------------------------------------------------------------------'
 */
 
 [_LEFTYLAYER] = LAYOUT_planck_grid(
    _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
    _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
    _______, _______, _______, _______, _______, _______, _______, KC_X,    KC_C,    KC_V,    _______, _______,
    _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
),

/* Numpad layer:
 * ,-----------------------------------------------------------------------------------.
 * |      |   &  |   |  |   !  |   +  |   =  |   %  |   7  |   8  |   9  |   *  |   =  |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |   @  | '  ' | "  " |   -  |   _  |   €  |   4  |   5  |   6  | +  : | Enter|
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      | <  > | [  ] | {  } | (  ) |   \  |   $  |   1  |   2  |   3  |   /  |      |
 * |------+------+------+------+------+-------------+------+------+------+------+------|
 * |      |      |      |      |(hold)|             |SymLyr|   0  |   .  |   -  |      |
 * `-----------------------------------------------------------------------------------'
 */
[_NUMLAYER_MACOS] = LAYOUT_planck_grid(
    _______, KC_AMPR,  KC_PIPE,  KC_EXLM, KC_PPLS, KC_EQL,  KC_PERC,    KC_7,    KC_8,    KC_9,    KC_PAST,   KC_EQL,
    _______, KC_AT,    QUOTES,   DQUOTES, KC_MINS, KC_UNDS, RALT(KC_2), KC_4,    KC_5,    KC_6,    PLUSCOLON, KC_PENT,
    _______, POINTYBR, SQUAREBR, CURLYBR, ROUNDBR, KC_BSLS, KC_DLR,     KC_1,    KC_2,    KC_3,    KC_PSLS,   _______,
    _______, _______,  _______,  _______, _______, _______, _______,    L_SYM_M, KC_0,    KC_PDOT, KC_PMNS,   _______
),
[_NUMLAYER_WINDOWS] = LAYOUT_planck_grid(
    _______, KC_AMPR,  KC_PIPE,  KC_EXLM, KC_PPLS, KC_EQL,  KC_PERC,    KC_7,    KC_8,    KC_9,    KC_PAST,   KC_EQL,
    _______, KC_AT,    QUOTES,   DQUOTES, KC_MINS, KC_UNDS, RALT(KC_5), KC_4,    KC_5,    KC_6,    PLUSCOLON, KC_PENT,
    _______, POINTYBR, SQUAREBR, CURLYBR, ROUNDBR, KC_BSLS, KC_DLR,     KC_1,    KC_2,    KC_3,    KC_PSLS,   _______,
    _______, _______,  _______,  _______, _______, _______, _______,    L_SYM_W, KC_0,    KC_PDOT, KC_PMNS,   _______
),

/* Symbol layer:
 * ,-----------------------------------------------------------------------------------.
 * |      |      |      |   €  |      |   ~  |      |   _  |   ^  |      |   %  |      |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |   @  |   $  |   $  |      |   `  |   #  |      |      |      |      |      |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |      |   !  |      |      |   \  |   ~  |   &  |      |      |   !  |      |
 * |------+------+------+------+------+-------------+------+------+------+------+------|
 * |      |      |      |      |(hold)|  UtilsLayer |(hold)|      |      |      |      |
 * `-----------------------------------------------------------------------------------'
 */
[_SYMLAYER_MACOS] = LAYOUT_planck_grid(
    _______, XXXXXXX, XXXXXXX, RALT(KC_2), XXXXXXX, KC_TILD,    XXXXXXX,    KC_UNDS, KC_CIRC, XXXXXXX, KC_PERC, _______,
    _______, KC_AT,   KC_DLR,  KC_DLR,     XXXXXXX, KC_GRV,     KC_HASH,    XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, _______,
    _______, XXXXXXX, KC_EXLM, XXXXXXX,    XXXXXXX, KC_BSLS,    KC_TILD,    KC_AMPR, XXXXXXX, XXXXXXX, KC_EXLM, _______,
    _______, _______, _______, _______,    _______, UTILSLAYER, UTILSLAYER, _______, _______, _______, _______, _______
),
[_SYMLAYER_WINDOWS] = LAYOUT_planck_grid(
    _______, XXXXXXX, XXXXXXX, RALT(KC_5), XXXXXXX, KC_TILD,    XXXXXXX,    KC_UNDS, KC_CIRC, XXXXXXX, KC_PERC, _______,
    _______, KC_AT,   KC_DLR,  KC_DLR,     XXXXXXX, KC_GRV,     KC_HASH,    XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, _______,
    _______, XXXXXXX, KC_EXLM, XXXXXXX,    XXXXXXX, KC_BSLS,    KC_TILD,    KC_AMPR, XXXXXXX, XXXXXXX, KC_EXLM, _______,
    _______, _______, _______, _______,    _______, UTILSLAYER, UTILSLAYER, _______, _______, _______, _______, _______
),

/* Navigation layer:
 * ,-----------------------------------------------------------------------------------.
 * |      |  F1  |  F2  |  F3  |  F4  |      | PgUp | Home |  Up  |  End |PrtScr| Lock |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |  F5  |  F6  |  F7  |  F8  |      | PgDn | Left | Down | Right| AppSw|      |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |  F9  |  F10 |  F11 |  F12 |      |      |PrDesk| Down |NxDesk|TaskVw|      |
 * |------+------+------+------+------+-------------+------+------+------+------+------|
 * |      |      |      |      |SymLyr|             |(hold)|      |      |      |      |
 * `-----------------------------------------------------------------------------------'
 */
[_NAVLAYER_MACOS] = LAYOUT_planck_grid(
    _______, KC_F1,   KC_F2,   KC_F3,   KC_F4,   XXXXXXX, KC_PGUP, G(KC_LEFT), KC_UP,   G(KC_RGHT), SGUI(KC_4), C(G(KC_Q)),
    _______, KC_F5,   KC_F6,   KC_F7,   KC_F8,   XXXXXXX, KC_PGDN, KC_LEFT,    KC_DOWN, KC_RGHT,    APPSWITCH,  _______,
    _______, KC_F9,   KC_F10,  KC_F11,  KC_F12,  XXXXXXX, XXXXXXX, C(KC_LEFT), KC_DOWN, C(KC_RGHT), C(KC_UP),   _______,
    _______, _______, _______, _______, L_SYM_M, _______, _______, _______,    _______, _______,    _______,    _______
),
[_NAVLAYER_WINDOWS] = LAYOUT_planck_grid(
    _______, KC_F1,   KC_F2,   KC_F3,   KC_F4,   XXXXXXX, KC_PGUP, KC_HOME,       KC_UP,   KC_END,        KC_PSCR,   G(KC_L),
    _______, KC_F5,   KC_F6,   KC_F7,   KC_F8,   XXXXXXX, KC_PGDN, KC_LEFT,       KC_DOWN, KC_RGHT,       APPSWITCH, _______,
    _______, KC_F9,   KC_F10,  KC_F11,  KC_F12,  XXXXXXX, XXXXXXX, C(G(KC_LEFT)), KC_DOWN, C(G(KC_RGHT)), G(KC_TAB), _______,
    _______, _______, _______, _______, L_SYM_W, _______, _______, _______,       _______, _______,       _______,   _______
),

/* Utilities layer:
 * ,-----------------------------------------------------------------------------------.
 * |      |      |  Win |      | Reset|      |      |      |  Ins |      | Pause|      |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |      |ScrlLk| Debug|      |      |      |      |      |      |      |      |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |      |      |CapsLk|      |      | NumLk|  Mac |      |      |      |      |
 * |------+------+------+------+------+-------------+------+------+------+------+------|
 * |      |      |      |      |(hold)|    (hold)   |(hold)|      |      |      |      |
 * `-----------------------------------------------------------------------------------'
 */
[_UTILSLAYER] = LAYOUT_planck_grid(
    _______, XXXXXXX, WINDOWS, XXXXXXX, RESET,   XXXXXXX, XXXXXXX, XXXXXXX, KC_INS,  XXXXXXX, KC_PAUS, _______,
    _______, XXXXXXX, KC_SLCK, DEBUG,   XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, _______,
    _______, XXXXXXX, XXXXXXX, KC_CAPS, XXXXXXX, XXXXXXX, KC_NLCK, MACOS,   XXXXXXX, XXXXXXX, XXXXXXX, _______,
    _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
)
};

/* Bootmagic keys (hold when plugging in the USB cable):
 * ,-----------------------------------------------------------------------------------.
 * |BootLd|      |  Win |      |      |      |      |      |      |      |      |      |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |      |      |      |      |      |      |      |      |      |      |      |
 * |------+------+------+------+------+------+------+------+------+------+------+------|
 * |      |      |      |      |      |      |      |  Mac |      |      |      |      |
 * |------+------+------+------+------+-------------+------+------+------+------+------|
 * |      |      |      |      |      |             |      |      |      |      |      |
 * `-----------------------------------------------------------------------------------'
 */

#ifdef AUDIO_ENABLE
  float plover_song[][2]     = SONG(PLOVER_SOUND);
  float plover_gb_song[][2]  = SONG(PLOVER_GOODBYE_SOUND);
#endif

bool app_switch_active = false;
static uint16_t timer;
static uint16_t duration = 0;
static bool double_tap = false;
uint8_t active_modifiers = 0;
bool shift_modifier_active = false;
uint8_t keycode_rightcommand;
uint8_t keycode_appswitch;

/* main loop */
bool process_record_user(uint16_t keycode, keyrecord_t *record) {

    /* Store modifiers in booleans for easy access in underlying methods. */
    active_modifiers = get_mods();
    shift_modifier_active = active_modifiers & ((1<<((KC_LSFT) & 0x07)) | (1<<((KC_RSFT) & 0x07)));

    /* Switch off timer when double click time is exceeded. */
    if (timer > 0) {
        duration = timer_elapsed(timer);
        if (duration > TAPPING_TERM) { timer = 0; }
    }
        
    /* Keep app_switch_active true, while a valid key is pressed. */
    if (app_switch_active) {
        if ((keycode == L_NAV_M || keycode == L_NAV_W) && record->event.pressed == false) {
            unregister_code(keycode_appswitch);
            app_switch_active = false;
        }
        if (app_switch_valid_keycode(keycode) == false) {
            unregister_code(keycode_appswitch);
            app_switch_active = false;
        }
    }

    switch (keycode) {
        case MACOS:
            #ifdef AUDIO_ENABLE
                stop_all_notes();
                PLAY_SONG(plover_song);
            #endif
            set_keycodes_for_os(MACOS);
            default_layer_set(_BASELAYER_MACOS);
            layer_on(_BASELAYER_MACOS);
            layer_off(_BASELAYER_WINDOWS);
            return false;
            break;
        case WINDOWS:
            #ifdef AUDIO_ENABLE
                stop_all_notes();
                PLAY_SONG(plover_gb_song);
            #endif
            set_keycodes_for_os(WINDOWS);
            default_layer_set(_BASELAYER_WINDOWS);
            layer_on(_BASELAYER_WINDOWS);
            layer_off(_BASELAYER_MACOS);
            return false;
            break;
        case RCMDCTRL:
            return handle_right_command_or_control(record->event.pressed);
            break;
        case APPSWITCH:
            return handle_app_switch(record->event.pressed);
            break;
        case QUOTES:
            return type_quotes(SINGLE_QUOTE, record->event.pressed);
            break;
        case DQUOTES:
            return type_quotes(DOUBLE_QUOTE, record->event.pressed);
            break;
        case POINTYBR:
            return type_brackets(POINTY, record->event.pressed);
            break;
        case SQUAREBR:
            return type_brackets(SQUARE, record->event.pressed);
            break;
        case CURLYBR:
            return type_brackets(CURLY, record->event.pressed);
            break;
        case ROUNDBR:
            return type_brackets(ROUND, record->event.pressed);
            break;
        case PLUSCOLON:
            return handle_pluscolon(record->event.pressed);
            break;
    }
    return true;
}

void set_keycodes_for_os(int os) {
    if (os == MACOS) {
        keycode_rightcommand = KC_RGUI;
        keycode_appswitch = KC_LGUI;
    } else {
        keycode_rightcommand = KC_RCTRL;
        keycode_appswitch = KC_LALT;
    }
}

/* While lefties use their mouse with their left hand, it is handy to use their right hand for Cut, Copy and Paste. */
bool handle_right_command_or_control(bool pressed) {
    if (pressed) {
        register_code(keycode_rightcommand);
        layer_on(_LEFTYLAYER);
    } else {
        layer_off(_LEFTYLAYER);
        unregister_code(keycode_rightcommand);
    }
    return false;
}

/* Key combo to easily switch applications. Press and hold Command or Alt, press and release Tab. Command or Alt is released when an invalid key is pressed in main loop. */
bool handle_app_switch(bool pressed) {
    if (pressed) {
        if (app_switch_active == false) {
          app_switch_active = true;
          register_code(keycode_appswitch);
        }
        register_code(KC_TAB);
    } else {
        unregister_code(KC_TAB);
    }
    return false;
}

/* Defines valid keys for app_switch function. */
bool app_switch_valid_keycode(uint16_t keycode) {
    if (keycode == APPSWITCH || KC_UP || KC_LEFT || KC_DOWN || KC_RIGHT) {
        return true;
    }
    return false;
}

/* Type 1 or 2 quotation marks, either single or double quotation marks.
 *  - Press once: type 1 quote and start timer. 
 *  - Quickly press twice: type pair of quotes and put cursor in between. 
 */
bool type_quotes(uint8_t quote_shape, bool pressed) {
    clear_mods();
    bool use_shift = quote_shape == DOUBLE_QUOTE ? true : false;

    duration = timer_elapsed(timer);

    if (pressed) {
        if ((timer == 0) || (duration == 0) || (duration > TAPPING_TERM)) {
            if (use_shift) { register_code(KC_LSFT); }
            register_code(KC_QUOT);
            double_tap = false;
        } else {
            if (duration < TAPPING_TERM) {
                if (use_shift) { 
                    tap_code16(S(KC_QUOT));
                } else {
                    tap_code(KC_QUOT);
                }
                double_tap = true;
            }
        }
    } else {
        if (double_tap) {
            tap_code(KC_LEFT);
            timer = 0;
            double_tap = false;
        } else {
            unregister_code(KC_QUOT);
            if (use_shift) { unregister_code(KC_LSFT); }
            timer = timer_read();
            double_tap = false;
        }
    }

    set_mods(active_modifiers);
    return false;
}

/* Type a bracket or a pair of brackets
 *  - Press once: type opening bracket.
 *  - Press while holding shift: type closing bracket.
 *  - Quickly press twice: type pair of brackets and put cursor in between.
 */
bool type_brackets(uint8_t bracket_shape, bool pressed) {
    clear_mods();

    uint8_t opening_bracket;
    uint8_t closing_bracket;
    bool use_shift;
    switch (bracket_shape) {
        case POINTY:
            opening_bracket = KC_COMM;
            closing_bracket = KC_DOT;
            use_shift = true;
            break;
        case SQUARE:
            opening_bracket = KC_LBRC;
            closing_bracket = KC_RBRC;
            use_shift = false;
            break;
        case CURLY:
            opening_bracket = KC_LBRC;
            closing_bracket = KC_RBRC;
            use_shift = true;
            break;
        case ROUND:
            opening_bracket = KC_9;
            closing_bracket = KC_0;
            use_shift = true;
            break;
    }
    duration = timer_elapsed(timer);

    if (pressed) {
        if ((timer == 0) || (duration == 0) || (duration > TAPPING_TERM) || (shift_modifier_active)) {
            if (use_shift) { register_code(KC_LSFT); }
            register_code(shift_modifier_active ? closing_bracket : opening_bracket);
            double_tap = false;
        } else {
            if (duration < TAPPING_TERM) {
                if (use_shift) { 
                    tap_code16(S(closing_bracket));
                } else {
                    tap_code(closing_bracket);
                }
                double_tap = true;
            }
        }
    } else {
        if (double_tap) {
            tap_code(KC_LEFT);
            timer = 0;
            double_tap = false;
        } else {
            unregister_code(shift_modifier_active ? closing_bracket : opening_bracket);
            if (use_shift) { unregister_code(KC_LSFT); }
            timer = timer_read();
            double_tap = false;
        }
    }

    set_mods(active_modifiers);
    return false;
}

/* On the Numpad-layer, the semicolon key produces a Plus ('+'). But to help entering time values, we like to have a colon (':') when shift is held. */
bool handle_pluscolon(bool pressed) {
    if (pressed) {
        register_code(shift_modifier_active ? KC_SCLN : KC_PPLS);
    } else {
        unregister_code(shift_modifier_active ? KC_SCLN : KC_PPLS);
    }
    return false;
}

// Bootmagic configures the keyboard when initializing (starting up after connecting the USB cable)
void bootmagic_lite(void) {
    matrix_scan();
    wait_ms(DEBOUNCE * 2);
    matrix_scan();
    int os;

    // hold key in upper left corner while plugging in the USB cable to enter the Bootloader for flashing.
    if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
        bootloader_jump();
    }

    // hold M key while plugging in the USB cable to use the keyboard in MacOs mode.
    if (matrix_get_row(BOOTMAGIC_LITE_MACOS_ROW) & (1 << BOOTMAGIC_LITE_MACOS_COLUMN)) {
        os = MACOS;
    }

    // hold W key while plugging in the USB cable to use the keyboard in Windows mode.
    if (matrix_get_row(BOOTMAGIC_LITE_WIN_ROW) & (1 << BOOTMAGIC_LITE_WIN_COLUMN)) {
        os = WINDOWS;
    }

    switch (os){
        case MACOS:
            default_layer_set(_BASELAYER_MACOS);
            layer_on(_BASELAYER_MACOS);
            layer_off(_BASELAYER_WINDOWS);
            break;
        case WINDOWS:
            default_layer_set(_BASELAYER_WINDOWS);
            layer_on(_BASELAYER_WINDOWS);
            layer_off(_BASELAYER_MACOS);
            break;
        default:
            os = MACOS;
            default_layer_set(_BASELAYER_MACOS);
            layer_on(_BASELAYER_MACOS);
            layer_off(_BASELAYER_WINDOWS);
            break;
    }
    set_keycodes_for_os(os);
}