]> git.gir.st - tmk_keyboard.git/blob - common/bootmagic.c
Add bootmagic salt key to avoid accidental configuration
[tmk_keyboard.git] / common / bootmagic.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include <util/delay.h>
4 #include "matrix.h"
5 #include "bootloader.h"
6 #include "debug.h"
7 #include "keymap.h"
8 #include "action_layer.h"
9 #include "eeconfig.h"
10 #include "bootmagic.h"
11
12
13 void bootmagic(void)
14 {
15 /* check signature */
16 if (!eeconfig_is_enabled()) {
17 eeconfig_init();
18 }
19
20 /* do scans in case of bounce */
21 uint8_t scan = 100;
22 while (scan--) { matrix_scan(); _delay_ms(10); }
23
24 /* bootmagic skip */
25 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SKIP)) {
26 return;
27 }
28
29 /* eeconfig clear */
30 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
31 eeconfig_init();
32 }
33
34 /* bootloader */
35 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
36 bootloader_jump();
37 }
38
39 /* debug enable */
40 debug_config.raw = eeconfig_read_debug();
41 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
42 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
43 debug_config.matrix = !debug_config.matrix;
44 } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
45 debug_config.keyboard = !debug_config.keyboard;
46 } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
47 debug_config.mouse = !debug_config.mouse;
48 } else {
49 debug_config.enable = !debug_config.enable;
50 }
51 }
52 eeconfig_write_debug(debug_config.raw);
53
54 /* keymap config */
55 keymap_config.raw = eeconfig_read_keymap();
56 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CPASLOCK)) {
57 keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
58 }
59 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
60 keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
61 }
62 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
63 keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
64 }
65 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
66 keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
67 }
68 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
69 keymap_config.no_gui = !keymap_config.no_gui;
70 }
71 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
72 keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
73 }
74 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
75 keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
76 }
77 eeconfig_write_keymap(keymap_config.raw);
78
79 /* default layer */
80 uint8_t default_layer = 0;
81 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); }
82 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1<<1); }
83 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1<<2); }
84 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1<<3); }
85 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1<<4); }
86 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1<<5); }
87 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1<<6); }
88 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); }
89 if (default_layer) {
90 eeconfig_write_default_layer(default_layer);
91 default_layer_set((uint32_t)default_layer);
92 } else {
93 default_layer = eeconfig_read_default_layer();
94 default_layer_set((uint32_t)default_layer);
95 }
96 }
97
98 static bool scan_keycode(uint8_t keycode)
99 {
100 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
101 matrix_row_t matrix_row = matrix_get_row(r);
102 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
103 if (matrix_row & ((matrix_row_t)1<<c)) {
104 if (keycode == keymap_key_to_keycode(0, (key_t){ .row = r, .col = c })) {
105 return true;
106 }
107 }
108 }
109 }
110 return false;
111 }
112
113 bool bootmagic_scan_keycode(uint8_t keycode)
114 {
115 if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
116
117 return scan_keycode(keycode);
118 }
Imprint / Impressum