]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/bootmagic.c
2c6bcbae56fccafae40a627ce0e47ceb4fcce37c
[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 void bootmagic(void)
16 {
17     /* check signature */
18     if (!eeconfig_is_enabled()) {
19         eeconfig_init();
20     }
21
22     /* do scans in case of bounce */
23     print("bootmagic scan: ... ");
24     uint8_t scan = 100;
25     while (scan--) { matrix_scan(); wait_ms(10); }
26     print("done.\n");
27
28     /* bootmagic skip */
29     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SKIP)) {
30         return;
31     }
32
33     /* eeconfig clear */
34     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
35         eeconfig_init();
36     }
37
38     /* bootloader */
39     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
40         bootloader_jump();
41     }
42
43     /* debug enable */
44     debug_config.raw = eeconfig_read_debug();
45     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
46         if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
47             debug_config.matrix = !debug_config.matrix;
48         } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
49             debug_config.keyboard = !debug_config.keyboard;
50         } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
51             debug_config.mouse = !debug_config.mouse;
52         } else {
53             debug_config.enable = !debug_config.enable;
54         }
55     }
56     eeconfig_update_debug(debug_config.raw);
57
58     /* keymap config */
59     keymap_config.raw = eeconfig_read_keymap();
60     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
61         keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
62     }
63     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
64         keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
65     }
66     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
67         keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
68     }
69     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
70         keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
71     }
72     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
73         keymap_config.no_gui = !keymap_config.no_gui;
74     }
75     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
76         keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
77     }
78     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
79         keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
80     }
81     if (bootmagic_scan_keycode(BOOTMAGIC_HOST_NKRO)) {
82         keymap_config.nkro = !keymap_config.nkro;
83     }
84     eeconfig_update_keymap(keymap_config.raw);
85
86     /* default layer */
87     uint8_t default_layer = 0;
88     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1<<0); }
89     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1<<1); }
90     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1<<2); }
91     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1<<3); }
92     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1<<4); }
93     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1<<5); }
94     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1<<6); }
95     if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); }
96     if (default_layer) {
97         eeconfig_update_default_layer(default_layer);
98         default_layer_set((uint32_t)default_layer);
99     } else {
100         default_layer = eeconfig_read_default_layer();
101         default_layer_set((uint32_t)default_layer);
102     }
103 }
104
105 static bool scan_keycode(uint8_t keycode)
106 {
107     for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
108         matrix_row_t matrix_row = matrix_get_row(r);
109         for (uint8_t c = 0; c < MATRIX_COLS; c++) {
110             if (matrix_row & ((matrix_row_t)1<<c)) {
111                 if (keycode == keymap_key_to_keycode(0, (keypos_t){ .row = r, .col = c })) {
112                     return true;
113                 }
114             }
115         }
116     }
117     return false;
118 }
119
120 bool bootmagic_scan_keycode(uint8_t keycode)
121 {
122     if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
123
124     return scan_keycode(keycode);
125 }