]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/common/bootmagic.c
Merge branch 'actionmap'
[max/tmk_keyboard.git] / tmk_core / common / bootmagic.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include "wait.h"
4 #include "matrix.h"
5 #include "bootloader.h"
6 #include "debug.h"
7 #include "keymap.h"
8 #include "actionmap.h"
9 #include "host.h"
10 #include "action_layer.h"
11 #include "eeconfig.h"
12 #include "bootmagic.h"
13
14 keymap_config_t keymap_config;
15
16 void bootmagic(void)
17 {
18     /* check signature */
19     if (!eeconfig_is_enabled()) {
20         eeconfig_init();
21     }
22
23     /* do scans in case of bounce */
24     print("bootmagic scan: ... ");
25     uint8_t scan = 100;
26     while (scan--) { matrix_scan(); wait_ms(10); }
27     print("done.\n");
28
29     /* bootmagic skip */
30     if (bootmagic_scan_key(BOOTMAGIC_KEY_SKIP)) {
31         return;
32     }
33
34     /* eeconfig clear */
35     if (bootmagic_scan_key(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
36         eeconfig_init();
37     }
38
39     /* bootloader */
40     if (bootmagic_scan_key(BOOTMAGIC_KEY_BOOTLOADER)) {
41         bootloader_jump();
42     }
43
44     /* debug enable */
45     debug_config.raw = eeconfig_read_debug();
46     if (bootmagic_scan_key(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
47         if (bootmagic_scan_key(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
48             debug_config.matrix = !debug_config.matrix;
49         } else if (bootmagic_scan_key(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
50             debug_config.keyboard = !debug_config.keyboard;
51         } else if (bootmagic_scan_key(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
52             debug_config.mouse = !debug_config.mouse;
53         } else {
54             debug_config.enable = !debug_config.enable;
55         }
56     }
57     eeconfig_write_debug(debug_config.raw);
58
59     /* keymap config */
60     keymap_config.raw = eeconfig_read_keymap();
61     if (bootmagic_scan_key(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
62         keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
63     }
64     if (bootmagic_scan_key(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
65         keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
66     }
67     if (bootmagic_scan_key(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
68         keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
69     }
70     if (bootmagic_scan_key(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
71         keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
72     }
73     if (bootmagic_scan_key(BOOTMAGIC_KEY_NO_GUI)) {
74         keymap_config.no_gui = !keymap_config.no_gui;
75     }
76     if (bootmagic_scan_key(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
77         keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
78     }
79     if (bootmagic_scan_key(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
80         keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
81     }
82     if (bootmagic_scan_key(BOOTMAGIC_HOST_NKRO)) {
83         keymap_config.nkro = !keymap_config.nkro;
84     }
85     eeconfig_write_keymap(keymap_config.raw);
86
87 #ifdef NKRO_ENABLE
88     keyboard_nkro = keymap_config.nkro;
89 #endif
90
91     /* default layer */
92     uint8_t default_layer = 0;
93     if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); }
94     if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1<<1); }
95     if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1<<2); }
96     if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1<<3); }
97     if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1<<4); }
98     if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1<<5); }
99     if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1<<6); }
100     if (bootmagic_scan_key(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); }
101     if (default_layer) {
102         eeconfig_write_default_layer(default_layer);
103         default_layer_set((uint32_t)default_layer);
104     } else {
105         default_layer = eeconfig_read_default_layer();
106         default_layer_set((uint32_t)default_layer);
107     }
108 }
109
110 static bool scan_key(uint16_t code)
111 {
112     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
113         matrix_row_t matrix_row = matrix_get_row(r);
114         for (uint8_t c = 0; c < MATRIX_COLS; c++) {
115             if (matrix_row & ((matrix_row_t)1<<c)) {
116                 // read key from Layer 0
117                 action_t action = action_for_key(0, (keypos_t){ .row = r, .col = c });
118                 if (action.code == code ||
119                     ((action.kind.id == ACT_LMODS ||
120                       action.kind.id == ACT_RMODS ||
121                       action.kind.id == ACT_LMODS_TAP ||
122                       action.kind.id == ACT_RMODS_TAP ||
123                       action.kind.id == ACT_LAYER_TAP ||
124                       action.kind.id == ACT_LAYER_TAP_EXT) &&
125                      action.key.code == code)) {
126                     return true;
127                 }
128             }
129         }
130     }
131     return false;
132 }
133
134 bool bootmagic_scan_key(uint16_t code)
135 {
136     if (!scan_key(BOOTMAGIC_KEY_SALT)) return false;
137
138     return scan_key(code);
139 }