Skip to content

Commit

Permalink
Overhaul bootmagic logic to have single entrypoint (qmk#8532)
Browse files Browse the repository at this point in the history
* Relocate bootmagic logic to have single entrypoint

* Align init of layer state
  • Loading branch information
zvecr authored and hsgw committed May 18, 2021
1 parent a1e12e7 commit a4d678d
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 153 deletions.
21 changes: 17 additions & 4 deletions common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions quantum/bootmagic/bootmagic.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once

#if defined(BOOTMAGIC_ENABLE)
# include "bootmagic_full.h"
#elif defined(BOOTMAGIC_LITE)
# include "bootmagic_lite.h"
#endif

void bootmagic(void);
104 changes: 48 additions & 56 deletions tmk_core/common/bootmagic.c → quantum/bootmagic/bootmagic_full.c
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdbool.h>
#include "wait.h"
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
21 changes: 17 additions & 4 deletions tmk_core/common/bootmagic.h → quantum/bootmagic/bootmagic_full.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#pragma once

/* FIXME: Add special doxygen comments for defines here. */
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "quantum.h"

/** \brief Reset eeprom
Expand Down Expand Up @@ -47,3 +62,5 @@ __attribute__((weak)) void bootmagic_lite(void) {
bootloader_jump();
}
}

void bootmagic(void) { bootmagic_lite(); }
25 changes: 25 additions & 0 deletions quantum/bootmagic/bootmagic_lite.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
54 changes: 54 additions & 0 deletions quantum/bootmagic/magic.c
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdbool.h>
#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);
}
18 changes: 18 additions & 0 deletions quantum/bootmagic/magic.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once

void magic(void);
8 changes: 2 additions & 6 deletions quantum/quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "quantum.h"
#include "magic.h"

#ifdef BLUETOOTH_ENABLE
# include "outputselect.h"
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit a4d678d

Please sign in to comment.