From 578db02d18336ceb87ab4232629c1b5b6449614b Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sun, 28 Feb 2021 15:50:15 +0000 Subject: [PATCH] Overhaul bootmagic logic to have single entrypoint (#8532) * Relocate bootmagic logic to have single entrypoint * Align init of layer state --- common_features.mk | 21 +++- quantum/bootmagic/bootmagic.h | 24 ++++ .../bootmagic/bootmagic_full.c | 104 ++++++++---------- .../bootmagic/bootmagic_full.h | 21 +++- .../bootmagic}/bootmagic_lite.c | 17 +++ quantum/bootmagic/bootmagic_lite.h | 25 +++++ quantum/bootmagic/magic.c | 54 +++++++++ quantum/bootmagic/magic.h | 18 +++ quantum/quantum.c | 8 +- quantum/quantum.h | 10 +- tmk_core/common.mk | 22 ---- tmk_core/common/keyboard.c | 10 -- tmk_core/common/magic.c | 39 ------- tmk_core/common/magic.h | 3 - tmk_core/protocol.mk | 4 + 15 files changed, 227 insertions(+), 153 deletions(-) create mode 100644 quantum/bootmagic/bootmagic.h rename tmk_core/common/bootmagic.c => quantum/bootmagic/bootmagic_full.c (74%) rename tmk_core/common/bootmagic.h => quantum/bootmagic/bootmagic_full.h (79%) rename {tmk_core/common => quantum/bootmagic}/bootmagic_lite.c (65%) create mode 100644 quantum/bootmagic/bootmagic_lite.h create mode 100644 quantum/bootmagic/magic.c create mode 100644 quantum/bootmagic/magic.h delete mode 100644 tmk_core/common/magic.c delete mode 100644 tmk_core/common/magic.h diff --git a/common_features.mk b/common_features.mk index 8c9d0a90c70e..bdde278b9844 100644 --- a/common_features.mk +++ b/common_features.mk @@ -422,10 +422,6 @@ ifeq ($(strip $(TERMINAL_ENABLE)), yes) OPT_DEFS += -DUSER_PRINT endif -ifeq ($(strip $(USB_HID_ENABLE)), yes) - include $(TMK_DIR)/protocol/usb_hid.mk -endif - ifeq ($(strip $(WPM_ENABLE)), yes) SRC += $(QUANTUM_DIR)/wpm.c OPT_DEFS += -DWPM_ENABLE @@ -459,6 +455,23 @@ ifeq ($(strip $(DIP_SWITCH_ENABLE)), yes) SRC += $(QUANTUM_DIR)/dip_switch.c endif +VALID_MAGIC_TYPES := yes full lite +BOOTMAGIC_ENABLE ?= no +ifneq ($(strip $(BOOTMAGIC_ENABLE)), no) + ifeq ($(filter $(BOOTMAGIC_ENABLE),$(VALID_MAGIC_TYPES)),) + $(error BOOTMAGIC_ENABLE="$(BOOTMAGIC_ENABLE)" is not a valid type of magic) + endif + ifeq ($(strip $(BOOTMAGIC_ENABLE)), lite) + OPT_DEFS += -DBOOTMAGIC_LITE + QUANTUM_SRC += $(QUANTUM_DIR)/bootmagic/bootmagic_lite.c + else + OPT_DEFS += -DBOOTMAGIC_ENABLE + QUANTUM_SRC += $(QUANTUM_DIR)/bootmagic/bootmagic_full.c + endif +endif +COMMON_VPATH += $(QUANTUM_DIR)/bootmagic +QUANTUM_SRC += $(QUANTUM_DIR)/bootmagic/magic.c + VALID_CUSTOM_MATRIX_TYPES:= yes lite no CUSTOM_MATRIX ?= no diff --git a/quantum/bootmagic/bootmagic.h b/quantum/bootmagic/bootmagic.h new file mode 100644 index 000000000000..959750178d38 --- /dev/null +++ b/quantum/bootmagic/bootmagic.h @@ -0,0 +1,24 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#if defined(BOOTMAGIC_ENABLE) +# include "bootmagic_full.h" +#elif defined(BOOTMAGIC_LITE) +# include "bootmagic_lite.h" +#endif + +void bootmagic(void); diff --git a/tmk_core/common/bootmagic.c b/quantum/bootmagic/bootmagic_full.c similarity index 74% rename from tmk_core/common/bootmagic.c rename to quantum/bootmagic/bootmagic_full.c index c1b3adf94df1..18c28cde8ab5 100644 --- a/tmk_core/common/bootmagic.c +++ b/quantum/bootmagic/bootmagic_full.c @@ -1,3 +1,18 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #include #include #include "wait.h" @@ -10,18 +25,35 @@ #include "eeconfig.h" #include "bootmagic.h" -keymap_config_t keymap_config; - -/** \brief Bootmagic +/** \brief Scan Keycode * * FIXME: needs doc */ -void bootmagic(void) { - /* check signature */ - if (!eeconfig_is_enabled()) { - eeconfig_init(); +static bool scan_keycode(uint8_t keycode) { + for (uint8_t r = 0; r < MATRIX_ROWS; r++) { + matrix_row_t matrix_row = matrix_get_row(r); + for (uint8_t c = 0; c < MATRIX_COLS; c++) { + if (matrix_row & ((matrix_row_t)1 << c)) { + if (keycode == keymap_key_to_keycode(0, (keypos_t){.row = r, .col = c})) { + return true; + } + } + } } + return false; +} + +/** \brief Bootmagic Scan Keycode + * + * FIXME: needs doc + */ +static bool bootmagic_scan_keycode(uint8_t keycode) { + if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false; + + return scan_keycode(keycode); +} +void bootmagic(void) { /* do scans in case of bounce */ print("bootmagic scan: ... "); uint8_t scan = 100; @@ -46,8 +78,6 @@ void bootmagic(void) { bootloader_jump(); } - /* debug enable */ - debug_config.raw = eeconfig_read_debug(); if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) { if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) { debug_config.matrix = !debug_config.matrix; @@ -61,8 +91,6 @@ void bootmagic(void) { } eeconfig_update_debug(debug_config.raw); - /* keymap config */ - keymap_config.raw = eeconfig_read_keymap(); if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) { keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock; } @@ -94,70 +122,34 @@ void bootmagic(void) { if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) { default_layer |= (1 << 0); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { + else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) { default_layer |= (1 << 1); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { + else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) { default_layer |= (1 << 2); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { + else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) { default_layer |= (1 << 3); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { + else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) { default_layer |= (1 << 4); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { + else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) { default_layer |= (1 << 5); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { + else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) { default_layer |= (1 << 6); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { + else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1 << 7); } - if (default_layer) { - eeconfig_update_default_layer(default_layer); - default_layer_set((layer_state_t)default_layer); - } else { - default_layer = eeconfig_read_default_layer(); - default_layer_set((layer_state_t)default_layer); - } - /* Also initialize layer state to trigger callback functions for layer_state */ - layer_state_set_kb((layer_state_t)layer_state); + eeconfig_update_default_layer(default_layer); /* EE_HANDS handedness */ if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EE_HANDS_LEFT)) { eeconfig_update_handedness(true); } - if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EE_HANDS_RIGHT)) { + else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EE_HANDS_RIGHT)) { eeconfig_update_handedness(false); } } - -/** \brief Scan Keycode - * - * FIXME: needs doc - */ -static bool scan_keycode(uint8_t keycode) { - for (uint8_t r = 0; r < MATRIX_ROWS; r++) { - matrix_row_t matrix_row = matrix_get_row(r); - for (uint8_t c = 0; c < MATRIX_COLS; c++) { - if (matrix_row & ((matrix_row_t)1 << c)) { - if (keycode == keymap_key_to_keycode(0, (keypos_t){.row = r, .col = c})) { - return true; - } - } - } - } - return false; -} - -/** \brief Bootmagic Scan Keycode - * - * FIXME: needs doc - */ -bool bootmagic_scan_keycode(uint8_t keycode) { - if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false; - - return scan_keycode(keycode); -} diff --git a/tmk_core/common/bootmagic.h b/quantum/bootmagic/bootmagic_full.h similarity index 79% rename from tmk_core/common/bootmagic.h rename to quantum/bootmagic/bootmagic_full.h index 8209d0194f6c..28f914c1b638 100644 --- a/tmk_core/common/bootmagic.h +++ b/quantum/bootmagic/bootmagic_full.h @@ -1,3 +1,19 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #pragma once /* FIXME: Add special doxygen comments for defines here. */ @@ -96,7 +112,4 @@ #endif #ifndef BOOTMAGIC_KEY_DEFAULT_LAYER_7 # define BOOTMAGIC_KEY_DEFAULT_LAYER_7 KC_7 -#endif - -void bootmagic(void); -bool bootmagic_scan_keycode(uint8_t keycode); +#endif \ No newline at end of file diff --git a/tmk_core/common/bootmagic_lite.c b/quantum/bootmagic/bootmagic_lite.c similarity index 65% rename from tmk_core/common/bootmagic_lite.c rename to quantum/bootmagic/bootmagic_lite.c index cbf756a1756b..9cbdcb0bbdfc 100644 --- a/tmk_core/common/bootmagic_lite.c +++ b/quantum/bootmagic/bootmagic_lite.c @@ -1,3 +1,18 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #include "quantum.h" /** \brief Reset eeprom @@ -47,3 +62,5 @@ __attribute__((weak)) void bootmagic_lite(void) { bootloader_jump(); } } + +void bootmagic(void) { bootmagic_lite(); } diff --git a/quantum/bootmagic/bootmagic_lite.h b/quantum/bootmagic/bootmagic_lite.h new file mode 100644 index 000000000000..17777e6b4a53 --- /dev/null +++ b/quantum/bootmagic/bootmagic_lite.h @@ -0,0 +1,25 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#ifndef BOOTMAGIC_LITE_COLUMN +# define BOOTMAGIC_LITE_COLUMN 0 +#endif +#ifndef BOOTMAGIC_LITE_ROW +# define BOOTMAGIC_LITE_ROW 0 +#endif + +void bootmagic_lite(void); diff --git a/quantum/bootmagic/magic.c b/quantum/bootmagic/magic.c new file mode 100644 index 000000000000..f1cb11c39576 --- /dev/null +++ b/quantum/bootmagic/magic.c @@ -0,0 +1,54 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include "wait.h" +#include "matrix.h" +#include "bootloader.h" +#include "debug.h" +#include "keymap.h" +#include "host.h" +#include "action_layer.h" +#include "eeconfig.h" +#include "bootmagic.h" + +keymap_config_t keymap_config; + +__attribute__((weak)) void bootmagic(void) {} + +/** \brief Magic + * + * FIXME: Needs doc + */ +void magic(void) { + /* check signature */ + if (!eeconfig_is_enabled()) { + eeconfig_init(); + } + + /* init globals */ + debug_config.raw = eeconfig_read_debug(); + keymap_config.raw = eeconfig_read_keymap(); + + bootmagic(); + + /* read here just incase bootmagic process changed its value */ + layer_state_t default_layer = (layer_state_t)eeconfig_read_default_layer(); + default_layer_set(default_layer); + + /* Also initialize layer state to trigger callback functions for layer_state */ + layer_state_set_kb((layer_state_t)layer_state); +} diff --git a/quantum/bootmagic/magic.h b/quantum/bootmagic/magic.h new file mode 100644 index 000000000000..2c3969b85ca5 --- /dev/null +++ b/quantum/bootmagic/magic.h @@ -0,0 +1,18 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +void magic(void); diff --git a/quantum/quantum.c b/quantum/quantum.c index b40b40544a0b..1fdb5d0f34c1 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -15,6 +15,7 @@ */ #include "quantum.h" +#include "magic.h" #ifdef BLUETOOTH_ENABLE # include "outputselect.h" @@ -341,12 +342,7 @@ layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_ void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) { layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3)); } void matrix_init_quantum() { -#ifdef BOOTMAGIC_LITE - bootmagic_lite(); -#endif - if (!eeconfig_is_enabled()) { - eeconfig_init(); - } + magic(); #if defined(LED_NUM_LOCK_PIN) || defined(LED_CAPS_LOCK_PIN) || defined(LED_SCROLL_LOCK_PIN) || defined(LED_COMPOSE_PIN) || defined(LED_KANA_PIN) // TODO: remove calls to led_init_ports from keyboards and remove ifdef led_init_ports(); diff --git a/quantum/quantum.h b/quantum/quantum.h index e24a4c43a3dc..8c267e4e4c47 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -52,6 +52,7 @@ #include "action_layer.h" #include "eeconfig.h" #include "bootloader.h" +#include "bootmagic.h" #include "timer.h" #include "sync_timer.h" #include "config_common.h" @@ -256,15 +257,6 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record); void post_process_record_kb(uint16_t keycode, keyrecord_t *record); void post_process_record_user(uint16_t keycode, keyrecord_t *record); -#ifndef BOOTMAGIC_LITE_COLUMN -# define BOOTMAGIC_LITE_COLUMN 0 -#endif -#ifndef BOOTMAGIC_LITE_ROW -# define BOOTMAGIC_LITE_ROW 0 -#endif - -void bootmagic_lite(void); - void reset_keyboard(void); void startup_user(void); diff --git a/tmk_core/common.mk b/tmk_core/common.mk index 238b3c69fd56..c2fc522cea8b 100644 --- a/tmk_core/common.mk +++ b/tmk_core/common.mk @@ -24,28 +24,6 @@ else include $(TMK_PATH)/$(COMMON_DIR)/lib_printf.mk endif -# Option modules -BOOTMAGIC_ENABLE ?= no -VALID_MAGIC_TYPES := yes full lite -ifneq ($(strip $(BOOTMAGIC_ENABLE)), no) - ifeq ($(filter $(BOOTMAGIC_ENABLE),$(VALID_MAGIC_TYPES)),) - $(error BOOTMAGIC_ENABLE="$(BOOTMAGIC_ENABLE)" is not a valid type of magic) - endif - ifeq ($(strip $(BOOTMAGIC_ENABLE)), lite) - TMK_COMMON_DEFS += -DBOOTMAGIC_LITE - TMK_COMMON_SRC += $(COMMON_DIR)/bootmagic_lite.c - - TMK_COMMON_DEFS += -DMAGIC_ENABLE - TMK_COMMON_SRC += $(COMMON_DIR)/magic.c - else - TMK_COMMON_DEFS += -DBOOTMAGIC_ENABLE - TMK_COMMON_SRC += $(COMMON_DIR)/bootmagic.c - endif -else - TMK_COMMON_DEFS += -DMAGIC_ENABLE - TMK_COMMON_SRC += $(COMMON_DIR)/magic.c -endif - SHARED_EP_ENABLE = no MOUSE_SHARED_EP ?= yes ifeq ($(strip $(KEYBOARD_SHARED_EP)), yes) diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c index ce3255c06965..65d9e00c7a66 100644 --- a/tmk_core/common/keyboard.c +++ b/tmk_core/common/keyboard.c @@ -34,11 +34,6 @@ along with this program. If not, see . #ifdef BACKLIGHT_ENABLE # include "backlight.h" #endif -#ifdef BOOTMAGIC_ENABLE -# include "bootmagic.h" -#else -# include "magic.h" -#endif #ifdef MOUSEKEY_ENABLE # include "mousekey.h" #endif @@ -296,11 +291,6 @@ void keyboard_init(void) { #ifdef ADB_MOUSE_ENABLE adb_mouse_init(); #endif -#ifdef BOOTMAGIC_ENABLE - bootmagic(); -#else - magic(); -#endif #ifdef BACKLIGHT_ENABLE backlight_init(); #endif diff --git a/tmk_core/common/magic.c b/tmk_core/common/magic.c deleted file mode 100644 index e14994164e22..000000000000 --- a/tmk_core/common/magic.c +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include -#if defined(__AVR__) -# include -#endif -#include "matrix.h" -#include "bootloader.h" -#include "debug.h" -#include "keymap.h" -#include "host.h" -#include "action_layer.h" -#include "eeconfig.h" -#include "magic.h" - -keymap_config_t keymap_config; - -/** \brief Magic - * - * FIXME: Needs doc - */ -void magic(void) { - /* check signature */ - if (!eeconfig_is_enabled()) { - eeconfig_init(); - } - - /* debug enable */ - debug_config.raw = eeconfig_read_debug(); - - /* keymap config */ - keymap_config.raw = eeconfig_read_keymap(); - - uint8_t default_layer = 0; - default_layer = eeconfig_read_default_layer(); - default_layer_set((layer_state_t)default_layer); - - /* Also initialize layer state to trigger callback functions for layer_state */ - layer_state_set_kb((layer_state_t)layer_state); -} diff --git a/tmk_core/common/magic.h b/tmk_core/common/magic.h deleted file mode 100644 index a6552c04dc2f..000000000000 --- a/tmk_core/common/magic.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void magic(void); diff --git a/tmk_core/protocol.mk b/tmk_core/protocol.mk index 0c41642b9be8..cc87e8347839 100644 --- a/tmk_core/protocol.mk +++ b/tmk_core/protocol.mk @@ -54,5 +54,9 @@ ifeq ($(strip $(XT_ENABLE)), yes) OPT_DEFS += -DXT_ENABLE endif +ifeq ($(strip $(USB_HID_ENABLE)), yes) + include $(TMK_DIR)/protocol/usb_hid.mk +endif + # Search Path VPATH += $(TMK_DIR)/protocol