1
0
Fork 0

Add support for key override introspection. (#24120)

This commit is contained in:
Nick Brassel 2024-07-16 09:22:17 +10:00 committed by GitHub
parent fef8e7195b
commit 8abaa3bc2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 90 additions and 25 deletions

View file

@ -10,6 +10,7 @@
#endif // INTROSPECTION_KEYMAP_C
#include "keymap_introspection.h"
#include "util.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Key mapping
@ -83,7 +84,7 @@ uint16_t keycode_at_dip_switch_map_location_raw(uint8_t switch_idx, bool on) {
return KC_TRNS;
}
uint16_t keycode_at_dip_switch_map_location(uint8_t switch_idx, bool on) {
__attribute__((weak)) uint16_t keycode_at_dip_switch_map_location(uint8_t switch_idx, bool on) {
return keycode_at_dip_switch_map_location_raw(switch_idx, on);
}
@ -95,13 +96,16 @@ uint16_t keycode_at_dip_switch_map_location(uint8_t switch_idx, bool on) {
#if defined(COMBO_ENABLE)
uint16_t combo_count_raw(void) {
return sizeof(key_combos) / sizeof(combo_t);
return ARRAY_SIZE(key_combos);
}
__attribute__((weak)) uint16_t combo_count(void) {
return combo_count_raw();
}
combo_t* combo_get_raw(uint16_t combo_idx) {
if (combo_idx >= combo_count_raw()) {
return NULL;
}
return &key_combos[combo_idx];
}
__attribute__((weak)) combo_t* combo_get(uint16_t combo_idx) {
@ -116,19 +120,48 @@ __attribute__((weak)) combo_t* combo_get(uint16_t combo_idx) {
#if defined(TAP_DANCE_ENABLE)
uint16_t tap_dance_count_raw(void) {
return sizeof(tap_dance_actions) / sizeof(tap_dance_action_t);
return ARRAY_SIZE(tap_dance_actions);
}
uint16_t tap_dance_count(void) {
__attribute__((weak)) uint16_t tap_dance_count(void) {
return tap_dance_count_raw();
}
tap_dance_action_t* tap_dance_get_raw(uint16_t tap_dance_idx) {
if (tap_dance_idx >= tap_dance_count_raw()) {
return NULL;
}
return &tap_dance_actions[tap_dance_idx];
}
tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx) {
__attribute__((weak)) tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx) {
return tap_dance_get_raw(tap_dance_idx);
}
#endif // defined(TAP_DANCE_ENABLE)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Key Overrides
#if defined(KEY_OVERRIDE_ENABLE)
uint16_t key_override_count_raw(void) {
return ARRAY_SIZE(key_overrides);
}
__attribute__((weak)) uint16_t key_override_count(void) {
return key_override_count_raw();
}
const key_override_t* key_override_get_raw(uint16_t key_override_idx) {
if (key_override_idx >= key_override_count_raw()) {
return NULL;
}
return key_overrides[key_override_idx];
}
__attribute__((weak)) const key_override_t* key_override_get(uint16_t key_override_idx) {
return key_override_get_raw(key_override_idx);
}
#endif // defined(KEY_OVERRIDE_ENABLE)

View file

@ -88,3 +88,24 @@ tap_dance_action_t* tap_dance_get_raw(uint16_t tap_dance_idx);
tap_dance_action_t* tap_dance_get(uint16_t tap_dance_idx);
#endif // defined(TAP_DANCE_ENABLE)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Key Overrides
#if defined(KEY_OVERRIDE_ENABLE)
// Forward declaration of key_override_t so we don't need to deal with header reordering
struct key_override_t;
typedef struct key_override_t key_override_t;
// Get the number of key overrides defined in the user's keymap, stored in firmware rather than any other persistent storage
uint16_t key_override_count_raw(void);
// Get the number of key overrides defined in the user's keymap, potentially stored dynamically
uint16_t key_override_count(void);
// Get the key override definitions, stored in firmware rather than any other persistent storage
const key_override_t* key_override_get_raw(uint16_t key_override_idx);
// Get the key override definitions, potentially stored dynamically
const key_override_t* key_override_get(uint16_t key_override_idx);
#endif // defined(KEY_OVERRIDE_ENABLE)

View file

@ -23,6 +23,7 @@
#include "action_util.h"
#include "quantum.h"
#include "quantum_keycodes.h"
#include "keymap_introspection.h"
#ifndef KEY_OVERRIDE_REPEAT_DELAY
# define KEY_OVERRIDE_REPEAT_DELAY 500
@ -83,9 +84,6 @@ static uint16_t deferred_register = 0;
// TODO: in future maybe save in EEPROM?
static bool enabled = true;
// Public variables
__attribute__((weak)) const key_override_t **key_overrides = NULL;
// Forward decls
static const key_override_t *clear_active_override(const bool allow_reregister);
@ -247,12 +245,12 @@ static bool check_activation_event(const key_override_t *override, const bool ke
/** Iterates through the list of key overrides and tries activating each, until it finds one that activates or reaches the end of overrides. Returns true if the key action for `keycode` should be sent */
static bool try_activating_override(const uint16_t keycode, const uint8_t layer, const bool key_down, const bool is_mod, const uint8_t active_mods, bool *activated) {
if (key_overrides == NULL) {
if (key_override_count() == 0) {
return true;
}
for (uint8_t i = 0;; i++) {
const key_override_t *const override = key_overrides[i];
for (uint8_t i = 0; i < key_override_count(); i++) {
const key_override_t *const override = key_override_get(i);
// End of array
if (override == NULL) {

View file

@ -55,7 +55,7 @@ typedef enum {
} ko_option_t;
/** Defines a single key override */
typedef struct {
typedef struct key_override_t {
// The non-modifier keycode that triggers the override. This keycode, and the necessary modifiers (trigger_mods) must be pressed to activate this override. Set this to the keycode of the key that should activate the override. Set to KC_NO to require only the necessary modifiers to be pressed and no non-modifier.
uint16_t trigger;
@ -87,9 +87,6 @@ typedef struct {
bool *enabled;
} key_override_t;
/** Define this as a null-terminated array of pointers to key overrides. These key overrides will be used by qmk. */
extern const key_override_t **key_overrides;
/** Turns key overrides on */
void key_override_on(void);