From b7dfcedf59483e21d78ef82ce95049692f09f509 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Wed, 23 Oct 2024 11:29:14 -0700 Subject: [PATCH 01/14] enable via, unregister key on dynamic set --- keyboards/kbdfans/odin/v2/keymaps/default/rules.mk | 1 + quantum/dynamic_keymap.c | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 keyboards/kbdfans/odin/v2/keymaps/default/rules.mk diff --git a/keyboards/kbdfans/odin/v2/keymaps/default/rules.mk b/keyboards/kbdfans/odin/v2/keymaps/default/rules.mk new file mode 100644 index 00000000000..036bd6d1c3e --- /dev/null +++ b/keyboards/kbdfans/odin/v2/keymaps/default/rules.mk @@ -0,0 +1 @@ +VIA_ENABLE = yes \ No newline at end of file diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index 3c22bbd4457..0530cb7e81e 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -121,6 +121,8 @@ uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) { if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return; + uint16_t prev_keycode = dynamic_keymap_get_keycode(layer, row, column); + unregister_code(prev_keycode); void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column); // Big endian, so we can read/write EEPROM directly from host if we want eeprom_update_byte(address, (uint8_t)(keycode >> 8)); From 37d88241a9140b82c4c8c0c6a570ad9d36288a53 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Wed, 23 Oct 2024 11:50:08 -0700 Subject: [PATCH 02/14] unregister at end of function --- quantum/dynamic_keymap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index 0530cb7e81e..632d10bdc40 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -122,11 +122,11 @@ uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) { if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return; uint16_t prev_keycode = dynamic_keymap_get_keycode(layer, row, column); - unregister_code(prev_keycode); void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column); // Big endian, so we can read/write EEPROM directly from host if we want eeprom_update_byte(address, (uint8_t)(keycode >> 8)); eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF)); + unregister_code(prev_keycode); } #ifdef ENCODER_MAP_ENABLE From e890fa0e76e40990cdc9834ddec316b872b2df27 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Wed, 23 Oct 2024 11:54:45 -0700 Subject: [PATCH 03/14] removing custom rules from repo --- keyboards/kbdfans/odin/v2/keymaps/default/rules.mk | 1 - 1 file changed, 1 deletion(-) delete mode 100644 keyboards/kbdfans/odin/v2/keymaps/default/rules.mk diff --git a/keyboards/kbdfans/odin/v2/keymaps/default/rules.mk b/keyboards/kbdfans/odin/v2/keymaps/default/rules.mk deleted file mode 100644 index 036bd6d1c3e..00000000000 --- a/keyboards/kbdfans/odin/v2/keymaps/default/rules.mk +++ /dev/null @@ -1 +0,0 @@ -VIA_ENABLE = yes \ No newline at end of file From 9f3e5fae782892ba854ce04e20f57761c9914b83 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Thu, 24 Oct 2024 09:14:11 -0700 Subject: [PATCH 04/14] Update quantum/dynamic_keymap.c lint formatting Co-authored-by: Drashna Jaelre --- quantum/dynamic_keymap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index 632d10bdc40..19fb8c4cbef 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -122,7 +122,7 @@ uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) { if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return; uint16_t prev_keycode = dynamic_keymap_get_keycode(layer, row, column); - void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column); + void * address = dynamic_keymap_key_to_eeprom_address(layer, row, column); // Big endian, so we can read/write EEPROM directly from host if we want eeprom_update_byte(address, (uint8_t)(keycode >> 8)); eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF)); From d3b8178e7c3220d0e4bc268ed503386dba1d8073 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Thu, 24 Oct 2024 19:28:48 -0700 Subject: [PATCH 05/14] moving logic to via.c, reverting dynamic_keymap.c changes --- quantum/dynamic_keymap.c | 4 +--- quantum/via.c | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index 19fb8c4cbef..3c22bbd4457 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -121,12 +121,10 @@ uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) { if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return; - uint16_t prev_keycode = dynamic_keymap_get_keycode(layer, row, column); - void * address = dynamic_keymap_key_to_eeprom_address(layer, row, column); + void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column); // Big endian, so we can read/write EEPROM directly from host if we want eeprom_update_byte(address, (uint8_t)(keycode >> 8)); eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF)); - unregister_code(prev_keycode); } #ifdef ENCODER_MAP_ENABLE diff --git a/quantum/via.c b/quantum/via.c index 643d7aa3c39..de9c5180e80 100644 --- a/quantum/via.c +++ b/quantum/via.c @@ -374,7 +374,9 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { break; } case id_dynamic_keymap_set_keycode: { + uint16_t keycode = dynamic_keymap_get_keycode(command_data[0], command_data[1], command_data[2]); dynamic_keymap_set_keycode(command_data[0], command_data[1], command_data[2], (command_data[3] << 8) | command_data[4]); + unregister_code(keycode); break; } case id_dynamic_keymap_reset: { From 751ad39ad4286bf9145c824eb3cdd64cf5e62a9a Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Fri, 1 Nov 2024 21:56:45 -0700 Subject: [PATCH 06/14] working example --- quantum/action_layer.c | 56 ++++++++++++++++++++++++++++++++++++++++++ quantum/action_layer.h | 2 ++ quantum/quantum.c | 7 +++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/quantum/action_layer.c b/quantum/action_layer.c index 7c09a5bd1e5..510ed74b75d 100644 --- a/quantum/action_layer.c +++ b/quantum/action_layer.c @@ -225,6 +225,11 @@ uint8_t source_layers_cache[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR uint8_t encoder_source_layers_cache[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][MAX_LAYER_BITS] = {{0}}; # endif // ENCODER_MAP_ENABLE +uint16_t keycode_map[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; +# ifdef ENCODER_MAP_ENABLE +uint16_t encoder_keycode_map[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; +# endif // ENCODER_MAP_ENABLE + /** \brief update source layers cache impl * * Updates the supplied cache when changing layers @@ -237,6 +242,14 @@ void update_source_layers_cache_impl(uint8_t layer, uint16_t entry_number, uint8 } } +/** \brief update encoder keycode map + * + * Updates map of keycodes when pressing a key down + */ +void update_keycode_map_impl(uint16_t entry_number, uint16_t keycode, uint16_t cache[][16]) { + cache[entry_number / 16][entry_number % 16] = keycode; +} + /** \brief read source layers cache * * reads the cached keys stored when the layer was changed @@ -253,6 +266,14 @@ uint8_t read_source_layers_cache_impl(uint16_t entry_number, uint8_t cache[][MAX return layer; } +/** \brief read keycode map + * + * reads map of keycodes when unpressing a key + */ +uint16_t read_keycode_map_impl(uint16_t entry_number, uint16_t cache[][16]) { + return cache[entry_number / 16][entry_number % 16]; +} + /** \brief update encoder source layers cache * * Updates the cached encoders when changing layers @@ -270,6 +291,41 @@ void update_source_layers_cache(keypos_t key, uint8_t layer) { # endif // ENCODER_MAP_ENABLE } +/** \brief update encoder keycode map + * + * Updates map of keycodes when pressing a key down + */ +void update_keycode_map(keypos_t key, uint16_t keycode) { + if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { + const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; + update_keycode_map_impl(entry_number, keycode, keycode_map); + } +# ifdef ENCODER_MAP_ENABLE + else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { + const uint16_t entry_number = key.col; + update_keycode_map_impl(entry_number, keycode, encoder_keycode_map); + } +# endif // ENCODER_MAP_ENABLE +} + +/** \brief read keycode map + * + * reads map of keycodes when unpressing a key + */ +uint16_t read_keycode_map(keypos_t key) { + if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { + const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; + return read_keycode_map_impl(entry_number, keycode_map); + } +# ifdef ENCODER_MAP_ENABLE + else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { + const uint16_t entry_number = key.col; + return read_keycode_map_impl(entry_number, encoder_keycode_map); + } +# endif // ENCODER_MAP_ENABLE + return KC_NO; +} + /** \brief read source layers cache * * reads the cached keys stored when the layer was changed diff --git a/quantum/action_layer.h b/quantum/action_layer.h index a2410d49a5e..02d1348eac1 100644 --- a/quantum/action_layer.h +++ b/quantum/action_layer.h @@ -159,7 +159,9 @@ layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_ #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE) void update_source_layers_cache(keypos_t key, uint8_t layer); +void update_keycode_map(keypos_t key, uint16_t keycode); uint8_t read_source_layers_cache(keypos_t key); +uint16_t read_keycode_map(keypos_t key); #endif action_t store_or_get_action(bool pressed, keypos_t key); diff --git a/quantum/quantum.c b/quantum/quantum.c index 011f9d73e4a..a3dc34b6e9c 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -231,14 +231,19 @@ uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) { /* TODO: Use store_or_get_action() or a similar function. */ if (!disable_action_cache) { uint8_t layer; + uint16_t keycode; if (event.pressed && update_layer_cache) { layer = layer_switch_get_layer(event.key); update_source_layers_cache(event.key, layer); + keycode = keymap_key_to_keycode(layer, event.key); + update_keycode_map(event.key, keycode); } else { layer = read_source_layers_cache(event.key); + keycode = read_keycode_map(event.key); } - return keymap_key_to_keycode(layer, event.key); + return keycode; + // return keymap_key_to_keycode(layer, event.key); } else #endif return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key); From 168cabeaf1bf72852becee4edf95f192f1e39c35 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Fri, 1 Nov 2024 21:57:14 -0700 Subject: [PATCH 07/14] revert via.c --- quantum/via.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/quantum/via.c b/quantum/via.c index de9c5180e80..643d7aa3c39 100644 --- a/quantum/via.c +++ b/quantum/via.c @@ -374,9 +374,7 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { break; } case id_dynamic_keymap_set_keycode: { - uint16_t keycode = dynamic_keymap_get_keycode(command_data[0], command_data[1], command_data[2]); dynamic_keymap_set_keycode(command_data[0], command_data[1], command_data[2], (command_data[3] << 8) | command_data[4]); - unregister_code(keycode); break; } case id_dynamic_keymap_reset: { From 3727bc70b7f0fd7f5caf95c41f19983f554e45ef Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Sat, 9 Nov 2024 12:22:13 -0800 Subject: [PATCH 08/14] move functions --- quantum/action_layer.c | 78 +++++++++++++++++++++--------------------- quantum/quantum.c | 2 -- 2 files changed, 39 insertions(+), 41 deletions(-) diff --git a/quantum/action_layer.c b/quantum/action_layer.c index 510ed74b75d..4f5c9e1f7cb 100644 --- a/quantum/action_layer.c +++ b/quantum/action_layer.c @@ -225,11 +225,6 @@ uint8_t source_layers_cache[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR uint8_t encoder_source_layers_cache[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][MAX_LAYER_BITS] = {{0}}; # endif // ENCODER_MAP_ENABLE -uint16_t keycode_map[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; -# ifdef ENCODER_MAP_ENABLE -uint16_t encoder_keycode_map[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; -# endif // ENCODER_MAP_ENABLE - /** \brief update source layers cache impl * * Updates the supplied cache when changing layers @@ -242,14 +237,6 @@ void update_source_layers_cache_impl(uint8_t layer, uint16_t entry_number, uint8 } } -/** \brief update encoder keycode map - * - * Updates map of keycodes when pressing a key down - */ -void update_keycode_map_impl(uint16_t entry_number, uint16_t keycode, uint16_t cache[][16]) { - cache[entry_number / 16][entry_number % 16] = keycode; -} - /** \brief read source layers cache * * reads the cached keys stored when the layer was changed @@ -266,14 +253,6 @@ uint8_t read_source_layers_cache_impl(uint16_t entry_number, uint8_t cache[][MAX return layer; } -/** \brief read keycode map - * - * reads map of keycodes when unpressing a key - */ -uint16_t read_keycode_map_impl(uint16_t entry_number, uint16_t cache[][16]) { - return cache[entry_number / 16][entry_number % 16]; -} - /** \brief update encoder source layers cache * * Updates the cached encoders when changing layers @@ -291,6 +270,45 @@ void update_source_layers_cache(keypos_t key, uint8_t layer) { # endif // ENCODER_MAP_ENABLE } +/** \brief read source layers cache + * + * reads the cached keys stored when the layer was changed + */ +uint8_t read_source_layers_cache(keypos_t key) { + if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { + const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; + return read_source_layers_cache_impl(entry_number, source_layers_cache); + } +# ifdef ENCODER_MAP_ENABLE + else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { + const uint16_t entry_number = key.col; + return read_source_layers_cache_impl(entry_number, encoder_source_layers_cache); + } +# endif // ENCODER_MAP_ENABLE + return 0; +} + +uint16_t keycode_map[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; +# ifdef ENCODER_MAP_ENABLE +uint16_t encoder_keycode_map[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; +# endif // ENCODER_MAP_ENABLE + +/** \brief update encoder keycode map + * + * Updates map of keycodes when pressing a key down + */ +void update_keycode_map_impl(uint16_t entry_number, uint16_t keycode, uint16_t cache[][16]) { + cache[entry_number / 16][entry_number % 16] = keycode; +} + +/** \brief read keycode map + * + * reads map of keycodes when releasing a key + */ +uint16_t read_keycode_map_impl(uint16_t entry_number, uint16_t cache[][16]) { + return cache[entry_number / 16][entry_number % 16]; +} + /** \brief update encoder keycode map * * Updates map of keycodes when pressing a key down @@ -325,24 +343,6 @@ uint16_t read_keycode_map(keypos_t key) { # endif // ENCODER_MAP_ENABLE return KC_NO; } - -/** \brief read source layers cache - * - * reads the cached keys stored when the layer was changed - */ -uint8_t read_source_layers_cache(keypos_t key) { - if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { - const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; - return read_source_layers_cache_impl(entry_number, source_layers_cache); - } -# ifdef ENCODER_MAP_ENABLE - else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { - const uint16_t entry_number = key.col; - return read_source_layers_cache_impl(entry_number, encoder_source_layers_cache); - } -# endif // ENCODER_MAP_ENABLE - return 0; -} #endif /** \brief Store or get action (FIXME: Needs better summary) diff --git a/quantum/quantum.c b/quantum/quantum.c index a3dc34b6e9c..bb3a4fca5f5 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -239,11 +239,9 @@ uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) { keycode = keymap_key_to_keycode(layer, event.key); update_keycode_map(event.key, keycode); } else { - layer = read_source_layers_cache(event.key); keycode = read_keycode_map(event.key); } return keycode; - // return keymap_key_to_keycode(layer, event.key); } else #endif return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key); From 46d072477c2c0122d4102cb68e0dda402c591733 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Sat, 9 Nov 2024 12:24:34 -0800 Subject: [PATCH 09/14] move additions together --- quantum/action_layer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/action_layer.h b/quantum/action_layer.h index 02d1348eac1..539f4137e4e 100644 --- a/quantum/action_layer.h +++ b/quantum/action_layer.h @@ -159,8 +159,8 @@ layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_ #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE) void update_source_layers_cache(keypos_t key, uint8_t layer); -void update_keycode_map(keypos_t key, uint16_t keycode); uint8_t read_source_layers_cache(keypos_t key); +void update_keycode_map(keypos_t key, uint16_t keycode); uint16_t read_keycode_map(keypos_t key); #endif action_t store_or_get_action(bool pressed, keypos_t key); From 1dc3373fda4fba9a4929d9b1c73ab3e0db6f7ff6 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Sat, 9 Nov 2024 12:26:28 -0800 Subject: [PATCH 10/14] comments --- quantum/action_layer.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/quantum/action_layer.c b/quantum/action_layer.c index 4f5c9e1f7cb..24894789415 100644 --- a/quantum/action_layer.c +++ b/quantum/action_layer.c @@ -293,9 +293,9 @@ uint16_t keycode_map[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)][ uint16_t encoder_keycode_map[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; # endif // ENCODER_MAP_ENABLE -/** \brief update encoder keycode map +/** \brief update keycode map * - * Updates map of keycodes when pressing a key down + * Updates map of keycodes when a key is pressed down */ void update_keycode_map_impl(uint16_t entry_number, uint16_t keycode, uint16_t cache[][16]) { cache[entry_number / 16][entry_number % 16] = keycode; @@ -303,15 +303,15 @@ void update_keycode_map_impl(uint16_t entry_number, uint16_t keycode, uint16_t c /** \brief read keycode map * - * reads map of keycodes when releasing a key + * reads from map of keycodes when a key is released */ uint16_t read_keycode_map_impl(uint16_t entry_number, uint16_t cache[][16]) { return cache[entry_number / 16][entry_number % 16]; } -/** \brief update encoder keycode map +/** \brief update keycode map * - * Updates map of keycodes when pressing a key down + * Updates map of keycodes when a key is pressed down */ void update_keycode_map(keypos_t key, uint16_t keycode) { if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { @@ -328,7 +328,7 @@ void update_keycode_map(keypos_t key, uint16_t keycode) { /** \brief read keycode map * - * reads map of keycodes when unpressing a key + * reads from map of keycodes when a key is released */ uint16_t read_keycode_map(keypos_t key) { if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { From f512383a6e8d821511fe3eb5e81e4c7a25bd6288 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Thu, 14 Nov 2024 21:52:05 -0800 Subject: [PATCH 11/14] opt in option, move to store_or_get_action --- docs/config_options.md | 2 ++ quantum/action_layer.c | 29 ++++++++++++++++++++++------- quantum/action_layer.h | 5 ++++- quantum/quantum.c | 7 ++----- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/docs/config_options.md b/docs/config_options.md index fec6b22b133..4a7ed609d06 100644 --- a/docs/config_options.md +++ b/docs/config_options.md @@ -144,6 +144,8 @@ If you define these options you will enable the associated feature, which may in * NKRO by default requires to be turned on, this forces it on during keyboard startup regardless of EEPROM setting. NKRO can still be turned off but will be turned on again if the keyboard reboots. * `#define STRICT_LAYER_RELEASE` * force a key release to be evaluated using the current layer stack instead of remembering which layer it came from (used for advanced cases) +* `#define KEYCODE_CACHE_ENABLE` + * Cache keycode for pressed keys, to be used on key release, across entire physical keyboard layout. ## Behaviors That Can Be Configured diff --git a/quantum/action_layer.c b/quantum/action_layer.c index 24894789415..0e8dcbe43bb 100644 --- a/quantum/action_layer.c +++ b/quantum/action_layer.c @@ -288,10 +288,11 @@ uint8_t read_source_layers_cache(keypos_t key) { return 0; } +# ifdef KEYCODE_CACHE_ENABLE uint16_t keycode_map[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; -# ifdef ENCODER_MAP_ENABLE +# ifdef ENCODER_MAP_ENABLE uint16_t encoder_keycode_map[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; -# endif // ENCODER_MAP_ENABLE +# endif // ENCODER_MAP_ENABLE /** \brief update keycode map * @@ -318,12 +319,12 @@ void update_keycode_map(keypos_t key, uint16_t keycode) { const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; update_keycode_map_impl(entry_number, keycode, keycode_map); } -# ifdef ENCODER_MAP_ENABLE +# ifdef ENCODER_MAP_ENABLE else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { const uint16_t entry_number = key.col; update_keycode_map_impl(entry_number, keycode, encoder_keycode_map); } -# endif // ENCODER_MAP_ENABLE +# endif // ENCODER_MAP_ENABLE } /** \brief read keycode map @@ -335,14 +336,15 @@ uint16_t read_keycode_map(keypos_t key) { const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; return read_keycode_map_impl(entry_number, keycode_map); } -# ifdef ENCODER_MAP_ENABLE +# ifdef ENCODER_MAP_ENABLE else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { const uint16_t entry_number = key.col; return read_keycode_map_impl(entry_number, encoder_keycode_map); } -# endif // ENCODER_MAP_ENABLE +# endif // ENCODER_MAP_ENABLE return KC_NO; } +# endif #endif /** \brief Store or get action (FIXME: Needs better summary) @@ -359,14 +361,27 @@ action_t store_or_get_action(bool pressed, keypos_t key) { } uint8_t layer; - +# ifdef KEYCODE_CACHE_ENABLE + uint16_t keycode; +# endif if (pressed) { layer = layer_switch_get_layer(key); update_source_layers_cache(key, layer); +# ifdef KEYCODE_CACHE_ENABLE + keycode = keymap_key_to_keycode(layer, key); + update_keycode_map(key, keycode); +# endif } else { layer = read_source_layers_cache(key); +# ifdef KEYCODE_CACHE_ENABLE + keycode = read_keycode_map(key); +# endif } +# ifndef KEYCODE_CACHE_ENABLE return action_for_key(layer, key); +# else + return action_for_keycode(keycode); +# endif #else return layer_switch_get_action(key); #endif diff --git a/quantum/action_layer.h b/quantum/action_layer.h index 539f4137e4e..4a8e8cdc6f5 100644 --- a/quantum/action_layer.h +++ b/quantum/action_layer.h @@ -160,8 +160,11 @@ layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_ void update_source_layers_cache(keypos_t key, uint8_t layer); uint8_t read_source_layers_cache(keypos_t key); -void update_keycode_map(keypos_t key, uint16_t keycode); +# ifdef KEYCODE_CACHE_ENABLE +void update_keycode_map(keypos_t key, uint16_t keycode); uint16_t read_keycode_map(keypos_t key); +uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key); +# endif #endif action_t store_or_get_action(bool pressed, keypos_t key); diff --git a/quantum/quantum.c b/quantum/quantum.c index bb3a4fca5f5..011f9d73e4a 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -231,17 +231,14 @@ uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) { /* TODO: Use store_or_get_action() or a similar function. */ if (!disable_action_cache) { uint8_t layer; - uint16_t keycode; if (event.pressed && update_layer_cache) { layer = layer_switch_get_layer(event.key); update_source_layers_cache(event.key, layer); - keycode = keymap_key_to_keycode(layer, event.key); - update_keycode_map(event.key, keycode); } else { - keycode = read_keycode_map(event.key); + layer = read_source_layers_cache(event.key); } - return keycode; + return keymap_key_to_keycode(layer, event.key); } else #endif return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key); From 1378e5660630cfbe9775c04bb593e5b76d40af17 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Thu, 14 Nov 2024 22:09:26 -0800 Subject: [PATCH 12/14] keycode_map simplification, encoder code removed --- quantum/action_layer.c | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/quantum/action_layer.c b/quantum/action_layer.c index 0e8dcbe43bb..779841a39c7 100644 --- a/quantum/action_layer.c +++ b/quantum/action_layer.c @@ -289,25 +289,22 @@ uint8_t read_source_layers_cache(keypos_t key) { } # ifdef KEYCODE_CACHE_ENABLE -uint16_t keycode_map[((MATRIX_ROWS * MATRIX_COLS) + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; -# ifdef ENCODER_MAP_ENABLE -uint16_t encoder_keycode_map[(NUM_ENCODERS + (CHAR_BIT)-1) / (CHAR_BIT)][16] = {{KC_NO}}; -# endif // ENCODER_MAP_ENABLE +static uint16_t keycode_map[MATRIX_ROWS][MATRIX_COLS] = {{KC_NO}}; /** \brief update keycode map * * Updates map of keycodes when a key is pressed down */ -void update_keycode_map_impl(uint16_t entry_number, uint16_t keycode, uint16_t cache[][16]) { - cache[entry_number / 16][entry_number % 16] = keycode; +void update_keycode_map_impl(uint16_t entry_number, uint16_t keycode) { + keycode_map[entry_number / 16][entry_number % 16] = keycode; } /** \brief read keycode map * * reads from map of keycodes when a key is released */ -uint16_t read_keycode_map_impl(uint16_t entry_number, uint16_t cache[][16]) { - return cache[entry_number / 16][entry_number % 16]; +uint16_t read_keycode_map_impl(uint16_t entry_number) { + return keycode_map[entry_number / 16][entry_number % 16]; } /** \brief update keycode map @@ -317,14 +314,8 @@ uint16_t read_keycode_map_impl(uint16_t entry_number, uint16_t cache[][16]) { void update_keycode_map(keypos_t key, uint16_t keycode) { if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; - update_keycode_map_impl(entry_number, keycode, keycode_map); + update_keycode_map_impl(entry_number, keycode); } -# ifdef ENCODER_MAP_ENABLE - else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { - const uint16_t entry_number = key.col; - update_keycode_map_impl(entry_number, keycode, encoder_keycode_map); - } -# endif // ENCODER_MAP_ENABLE } /** \brief read keycode map @@ -334,14 +325,8 @@ void update_keycode_map(keypos_t key, uint16_t keycode) { uint16_t read_keycode_map(keypos_t key) { if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; - return read_keycode_map_impl(entry_number, keycode_map); + return read_keycode_map_impl(entry_number); } -# ifdef ENCODER_MAP_ENABLE - else if (key.row == KEYLOC_ENCODER_CW || key.row == KEYLOC_ENCODER_CCW) { - const uint16_t entry_number = key.col; - return read_keycode_map_impl(entry_number, encoder_keycode_map); - } -# endif // ENCODER_MAP_ENABLE return KC_NO; } # endif From 9f71517f8dce13d67114a550eaee13b6676c0993 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Thu, 14 Nov 2024 22:27:41 -0800 Subject: [PATCH 13/14] keycode_map simplification cont --- quantum/action_layer.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/quantum/action_layer.c b/quantum/action_layer.c index 779841a39c7..d35c0a405ee 100644 --- a/quantum/action_layer.c +++ b/quantum/action_layer.c @@ -295,16 +295,16 @@ static uint16_t keycode_map[MATRIX_ROWS][MATRIX_COLS] = {{KC_NO}}; * * Updates map of keycodes when a key is pressed down */ -void update_keycode_map_impl(uint16_t entry_number, uint16_t keycode) { - keycode_map[entry_number / 16][entry_number % 16] = keycode; +void update_keycode_map_impl(uint8_t row, uint8_t col, uint16_t keycode) { + keycode_map[row][col] = keycode; } /** \brief read keycode map * * reads from map of keycodes when a key is released */ -uint16_t read_keycode_map_impl(uint16_t entry_number) { - return keycode_map[entry_number / 16][entry_number % 16]; +uint16_t read_keycode_map_impl(uint8_t row, uint8_t col) { + return keycode_map[row][col]; } /** \brief update keycode map @@ -313,8 +313,7 @@ uint16_t read_keycode_map_impl(uint16_t entry_number) { */ void update_keycode_map(keypos_t key, uint16_t keycode) { if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { - const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; - update_keycode_map_impl(entry_number, keycode); + update_keycode_map_impl(key.row, key.col, keycode); } } @@ -324,8 +323,7 @@ void update_keycode_map(keypos_t key, uint16_t keycode) { */ uint16_t read_keycode_map(keypos_t key) { if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { - const uint16_t entry_number = (uint16_t)(key.row * MATRIX_COLS) + key.col; - return read_keycode_map_impl(entry_number); + return read_keycode_map_impl(key.row, key.col); } return KC_NO; } From 610863f6def4269cd77be8441d0a9338698c3e29 Mon Sep 17 00:00:00 2001 From: Garretonzo Date: Thu, 14 Nov 2024 22:30:30 -0800 Subject: [PATCH 14/14] keycode_map simplification cont --- quantum/action_layer.c | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/quantum/action_layer.c b/quantum/action_layer.c index d35c0a405ee..8200c74b826 100644 --- a/quantum/action_layer.c +++ b/quantum/action_layer.c @@ -291,29 +291,13 @@ uint8_t read_source_layers_cache(keypos_t key) { # ifdef KEYCODE_CACHE_ENABLE static uint16_t keycode_map[MATRIX_ROWS][MATRIX_COLS] = {{KC_NO}}; -/** \brief update keycode map - * - * Updates map of keycodes when a key is pressed down - */ -void update_keycode_map_impl(uint8_t row, uint8_t col, uint16_t keycode) { - keycode_map[row][col] = keycode; -} - -/** \brief read keycode map - * - * reads from map of keycodes when a key is released - */ -uint16_t read_keycode_map_impl(uint8_t row, uint8_t col) { - return keycode_map[row][col]; -} - /** \brief update keycode map * * Updates map of keycodes when a key is pressed down */ void update_keycode_map(keypos_t key, uint16_t keycode) { if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { - update_keycode_map_impl(key.row, key.col, keycode); + keycode_map[key.row][key.col] = keycode; } } @@ -323,7 +307,7 @@ void update_keycode_map(keypos_t key, uint16_t keycode) { */ uint16_t read_keycode_map(keypos_t key) { if (key.row < MATRIX_ROWS && key.col < MATRIX_COLS) { - return read_keycode_map_impl(key.row, key.col); + return keycode_map[key.row][key.col]; } return KC_NO; }