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