Hash dynamic keymap at runtime

This commit is contained in:
zvecr 2022-07-15 03:33:34 +01:00
parent 8874c664a8
commit 28855f658a
7 changed files with 67 additions and 62 deletions

View File

@ -1,4 +1,4 @@
# Determine what keyboard we are building and setup the build environment.
# Determine what keyboard we are building and setup the build environment.
#
# We support folders up to 5 levels deep below `keyboards/`. This file is
# responsible for determining which folder is being used and doing the
@ -352,12 +352,7 @@ $(KEYBOARD_OUTPUT)/src/layouts.h: $(INFO_JSON_FILES)
$(eval CMD=$(QMK_BIN) generate-layouts --quiet --keyboard $(KEYBOARD) --output $(KEYBOARD_OUTPUT)/src/layouts.h)
@$(BUILD_CMD)
$(KEYBOARD_OUTPUT)/src/keymap_hash.h:
@$(SILENT) || printf "$(MSG_GENERATING) $@" | $(AWK_CMD)
$(eval CMD=$(QMK_BIN) generate-keymap-hash -q -o "$(KEYMAP_OUTPUT)/src/keymap_hash.h" -kb $(KEYBOARD) -km $(KEYMAP))
@$(BUILD_CMD)
generated-files: $(KEYBOARD_OUTPUT)/src/keymap_hash.h $(KEYBOARD_OUTPUT)/src/info_config.h $(KEYBOARD_OUTPUT)/src/default_keyboard.c $(KEYBOARD_OUTPUT)/src/default_keyboard.h $(KEYBOARD_OUTPUT)/src/layouts.h
generated-files: $(KEYBOARD_OUTPUT)/src/info_config.h $(KEYBOARD_OUTPUT)/src/default_keyboard.c $(KEYBOARD_OUTPUT)/src/default_keyboard.h $(KEYBOARD_OUTPUT)/src/layouts.h
.INTERMEDIATE : generated-files

View File

@ -610,6 +610,22 @@ ifeq ($(strip $(VIA_ENABLE)), yes)
OPT_DEFS += -DVIA_ENABLE
endif
ifeq ($(strip $(XAP_ENABLE)), yes)
ifeq ($(strip $(VIA_ENABLE)), yes)
$(error 'XAP_ENABLE = $(XAP_ENABLE)' deprecates 'VIA_ENABLE = $(VIA_ENABLE)'. Please set 'VIA_ENABLE = no')
endif
DYNAMIC_KEYMAP_ENABLE := yes
FNV_ENABLE := yes
SECURE_ENABLE := yes
BOOTMAGIC_ENABLE := yes
OPT_DEFS += -DXAP_ENABLE
OPT_DEFS += -DBOOTLOADER_JUMP_SUPPORTED
VPATH += $(QUANTUM_DIR)/xap
SRC += $(QUANTUM_DIR)/xap/xap.c $(QUANTUM_DIR)/xap/xap_handlers.c
endif
VALID_MAGIC_TYPES := yes
BOOTMAGIC_ENABLE ?= no
ifneq ($(strip $(BOOTMAGIC_ENABLE)), no)
@ -872,20 +888,6 @@ ifeq ($(strip $(USBPD_ENABLE)), yes)
endif
endif
ifeq ($(strip $(XAP_ENABLE)), yes)
ifeq ($(strip $(VIA_ENABLE)), yes)
$(error 'XAP_ENABLE = $(XAP_ENABLE)' deprecates 'VIA_ENABLE = $(VIA_ENABLE)'. Please set 'VIA_ENABLE = no')
endif
OPT_DEFS += -DXAP_ENABLE
OPT_DEFS += -DBOOTLOADER_JUMP_SUPPORTED
DYNAMIC_KEYMAP_ENABLE := yes
SECURE_ENABLE := yes
EMBED_INFO_JSON := yes
VPATH += $(QUANTUM_DIR)/xap
SRC += $(QUANTUM_DIR)/xap/xap.c $(QUANTUM_DIR)/xap/xap_handlers.c
endif
BLUETOOTH_ENABLE ?= no
VALID_BLUETOOTH_DRIVER_TYPES := BluefruitLE RN42 custom
ifeq ($(strip $(BLUETOOTH_ENABLE)), yes)

View File

@ -55,7 +55,6 @@ subcommands = [
'qmk.cli.generate.info_json',
'qmk.cli.generate.keyboard_c',
'qmk.cli.generate.keyboard_h',
'qmk.cli.generate.keymap_hash',
'qmk.cli.generate.layouts',
'qmk.cli.generate.rgb_breathe_table',
'qmk.cli.generate.rules_mk',

View File

@ -1,36 +0,0 @@
"""Used by the make system to generate header.
"""
from fnvhash import fnv1a_32
from milc import cli
from qmk.commands import dump_lines
from qmk.keymap import locate_keymap
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.path import normpath
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
@cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate header for.')
@cli.argument('-km', '--keymap', arg_only=True, required=True, help='Keymap to generate header for.')
@cli.subcommand('Used by the make system to generate header', hidden=True)
def generate_keymap_hash(cli):
# Build the header file.
header_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once']
keymap_folder = locate_keymap(cli.args.keyboard, cli.args.keymap).parent
keymap_files = list(keymap_folder.glob('**/*'))
keymap_files.sort()
content = ""
for file in keymap_files:
content += file.read_text(encoding='utf-8')
val = fnv1a_32(bytes(content, 'utf-8'))
header_lines.append(f'#define KEYMAP_HASH 0x{val:08X}ul')
# Show the results
dump_lines(cli.args.output, header_lines, cli.args.quiet)

View File

@ -19,6 +19,7 @@
#include "progmem.h" // to read default from flash
#include "quantum.h" // for send_string()
#include "dynamic_keymap.h"
#include "fnv.h"
#ifdef VIA_ENABLE
# include "via.h" // for VIA_EEPROM_CONFIG_END
@ -147,6 +148,47 @@ void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwis
}
#endif // ENCODER_MAP_ENABLE
static uint32_t dynamic_keymap_compute_hash(void) {
Fnv32_t hash = FNV1_32A_INIT;
uint16_t keycode;
for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
for (int row = 0; row < MATRIX_ROWS; row++) {
for (int column = 0; column < MATRIX_COLS; column++) {
keycode = pgm_read_word(&keymaps[layer][row][column]);
hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
}
}
#ifdef ENCODER_MAP_ENABLE
for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
keycode = pgm_read_word(&encoder_map[layer][encoder][0]);
hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
keycode = pgm_read_word(&encoder_map[layer][encoder][1]);
hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
}
#endif // ENCODER_MAP_ENABLE
}
return hash;
}
static uint32_t dynamic_keymap_hash(void) {
static uint32_t hash = 0;
static uint8_t s_init = 0;
if (!s_init) {
s_init = 1;
hash = dynamic_keymap_compute_hash();
}
return hash;
}
bool dynamic_keymap_is_valid(void) {
return eeprom_read_dword(EECONFIG_KEYMAP_HASH) == dynamic_keymap_hash();
}
void dynamic_keymap_reset(void) {
// Reset the keymaps in EEPROM to what is in flash.
// All keyboards using dynamic keymaps should define a layout
@ -164,6 +206,7 @@ void dynamic_keymap_reset(void) {
}
#endif // ENCODER_MAP_ENABLE
}
eeprom_update_dword(EECONFIG_KEYMAP_HASH, dynamic_keymap_hash());
}
void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {

View File

@ -26,7 +26,10 @@ void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column,
uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise);
void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode);
#endif // ENCODER_MAP_ENABLE
bool dynamic_keymap_is_valid(void);
void dynamic_keymap_reset(void);
// These get/set the keycodes as stored in the EEPROM buffer
// Data is big-endian 16-bit values (the keycodes)
// Order is by layer/row/column

View File

@ -17,7 +17,7 @@ bool via_eeprom_is_valid(void);
void via_eeprom_set_valid(bool valid);
void eeconfig_init_via(void);
#elif defined(DYNAMIC_KEYMAP_ENABLE)
# include "keymap_hash.h"
bool dynamic_keymap_is_valid(void);
void dynamic_keymap_reset(void);
#endif
@ -86,7 +86,6 @@ void eeconfig_init_quantum(void) {
eeconfig_init_via();
#elif defined(DYNAMIC_KEYMAP_ENABLE)
dynamic_keymap_reset();
eeprom_update_dword(EECONFIG_KEYMAP_HASH, KEYMAP_HASH);
#endif
eeconfig_init_kb();
@ -131,7 +130,7 @@ bool eeconfig_is_enabled(void) {
}
#elif defined(DYNAMIC_KEYMAP_ENABLE)
if (is_eeprom_enabled) {
is_eeprom_enabled = (eeprom_read_dword(EECONFIG_KEYMAP_HASH) == KEYMAP_HASH);
is_eeprom_enabled = dynamic_keymap_is_valid();
}
#endif
return is_eeprom_enabled;
@ -149,7 +148,7 @@ bool eeconfig_is_disabled(void) {
}
#elif defined(DYNAMIC_KEYMAP_ENABLE)
if (!is_eeprom_disabled) {
is_eeprom_disabled = (eeprom_read_dword(EECONFIG_KEYMAP_HASH) != KEYMAP_HASH);
is_eeprom_disabled = !dynamic_keymap_is_valid();
}
#endif
return is_eeprom_disabled;