Tidy up report.h (#8486)
[jackhill/qmk/firmware.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 "host.h"
9 #include "action_layer.h"
10 #include "eeconfig.h"
11 #include "bootmagic.h"
12
13 keymap_config_t keymap_config;
14
15 /** \brief Bootmagic
16 *
17 * FIXME: needs doc
18 */
19 void bootmagic(void) {
20 /* check signature */
21 if (!eeconfig_is_enabled()) {
22 eeconfig_init();
23 }
24
25 /* do scans in case of bounce */
26 print("bootmagic scan: ... ");
27 uint8_t scan = 100;
28 while (scan--) {
29 matrix_scan();
30 wait_ms(10);
31 }
32 print("done.\n");
33
34 /* bootmagic skip */
35 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SKIP)) {
36 return;
37 }
38
39 /* eeconfig clear */
40 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
41 eeconfig_init();
42 }
43
44 /* bootloader */
45 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
46 bootloader_jump();
47 }
48
49 /* debug enable */
50 debug_config.raw = eeconfig_read_debug();
51 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
52 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
53 debug_config.matrix = !debug_config.matrix;
54 } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
55 debug_config.keyboard = !debug_config.keyboard;
56 } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
57 debug_config.mouse = !debug_config.mouse;
58 } else {
59 debug_config.enable = !debug_config.enable;
60 }
61 }
62 eeconfig_update_debug(debug_config.raw);
63
64 /* keymap config */
65 keymap_config.raw = eeconfig_read_keymap();
66 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
67 keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
68 }
69 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
70 keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
71 }
72 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
73 keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
74 }
75 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
76 keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
77 }
78 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
79 keymap_config.no_gui = !keymap_config.no_gui;
80 }
81 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
82 keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
83 }
84 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
85 keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
86 }
87 if (bootmagic_scan_keycode(BOOTMAGIC_HOST_NKRO)) {
88 keymap_config.nkro = !keymap_config.nkro;
89 }
90 eeconfig_update_keymap(keymap_config.raw);
91
92 /* default layer */
93 uint8_t default_layer = 0;
94 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) {
95 default_layer |= (1 << 0);
96 }
97 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) {
98 default_layer |= (1 << 1);
99 }
100 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) {
101 default_layer |= (1 << 2);
102 }
103 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) {
104 default_layer |= (1 << 3);
105 }
106 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) {
107 default_layer |= (1 << 4);
108 }
109 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) {
110 default_layer |= (1 << 5);
111 }
112 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) {
113 default_layer |= (1 << 6);
114 }
115 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) {
116 default_layer |= (1 << 7);
117 }
118 if (default_layer) {
119 eeconfig_update_default_layer(default_layer);
120 default_layer_set((layer_state_t)default_layer);
121 } else {
122 default_layer = eeconfig_read_default_layer();
123 default_layer_set((layer_state_t)default_layer);
124 }
125
126 /* EE_HANDS handedness */
127 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EE_HANDS_LEFT)) {
128 eeconfig_update_handedness(true);
129 }
130 if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EE_HANDS_RIGHT)) {
131 eeconfig_update_handedness(false);
132 }
133 }
134
135 /** \brief Scan Keycode
136 *
137 * FIXME: needs doc
138 */
139 static bool scan_keycode(uint8_t keycode) {
140 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
141 matrix_row_t matrix_row = matrix_get_row(r);
142 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
143 if (matrix_row & ((matrix_row_t)1 << c)) {
144 if (keycode == keymap_key_to_keycode(0, (keypos_t){.row = r, .col = c})) {
145 return true;
146 }
147 }
148 }
149 }
150 return false;
151 }
152
153 /** \brief Bootmagic Scan Keycode
154 *
155 * FIXME: needs doc
156 */
157 bool bootmagic_scan_keycode(uint8_t keycode) {
158 if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
159
160 return scan_keycode(keycode);
161 }