]> git.gir.st - tmk_keyboard.git/blob - common/bootmagic.c
056806f23065dfec5be575e934c754e2be4312fb
[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 "host.h"
9 #include "action_layer.h"
10 #include "eeconfig.h"
11 #include "bootmagic.h"
12 #include "hook.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_keycode(BOOTMAGIC_KEY_SKIP)) {
31 return;
32 }
33
34 /* eeconfig clear */
35 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
36 eeconfig_init();
37 }
38
39 /* bootloader */
40 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
41 bootloader_jump();
42 }
43
44 /* user-defined checks */
45 hook_bootmagic();
46
47 /* debug enable */
48 debug_config.raw = eeconfig_read_debug();
49 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
50 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
51 debug_config.matrix = !debug_config.matrix;
52 } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
53 debug_config.keyboard = !debug_config.keyboard;
54 } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
55 debug_config.mouse = !debug_config.mouse;
56 } else {
57 debug_config.enable = !debug_config.enable;
58 }
59 }
60 eeconfig_write_debug(debug_config.raw);
61
62 /* keymap config */
63 keymap_config.raw = eeconfig_read_keymap();
64 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
65 keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
66 }
67 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
68 keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
69 }
70 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
71 keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
72 }
73 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
74 keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
75 }
76 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
77 keymap_config.no_gui = !keymap_config.no_gui;
78 }
79 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
80 keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
81 }
82 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
83 keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
84 }
85 if (bootmagic_scan_keycode(BOOTMAGIC_HOST_NKRO)) {
86 keymap_config.nkro = !keymap_config.nkro;
87 }
88 eeconfig_write_keymap(keymap_config.raw);
89
90 #ifdef NKRO_ENABLE
91 keyboard_nkro = keymap_config.nkro;
92 #endif
93
94 /* default layer */
95 uint8_t default_layer = 0;
96 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); }
97 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1<<1); }
98 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1<<2); }
99 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1<<3); }
100 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1<<4); }
101 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1<<5); }
102 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1<<6); }
103 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); }
104 if (default_layer) {
105 eeconfig_write_default_layer(default_layer);
106 default_layer_set((uint32_t)default_layer);
107 } else {
108 default_layer = eeconfig_read_default_layer();
109 default_layer_set((uint32_t)default_layer);
110 }
111 }
112
113 static bool scan_keycode(uint8_t keycode)
114 {
115 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
116 matrix_row_t matrix_row = matrix_get_row(r);
117 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
118 if (matrix_row & ((matrix_row_t)1<<c)) {
119 if (keycode == keymap_key_to_keycode(0, (keypos_t){ .row = r, .col = c })) {
120 return true;
121 }
122 }
123 }
124 }
125 return false;
126 }
127
128 bool bootmagic_scan_keycode(uint8_t keycode)
129 {
130 if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
131
132 return scan_keycode(keycode);
133 }
Imprint / Impressum