mirror of
https://github.com/qmk/qmk_firmware.git
synced 2026-09-29 13:30:33 +02:00
keycode_string review revisions.
* Rename keycode_string() -> get_keycode_string() for consistency with existing string utils like get_u8_str(). * Revise custom keycode names with separate _user and _kb tables.
This commit is contained in:
@@ -90,7 +90,9 @@ static const keycode_string_name_t keycode_names[] = {
|
||||
};
|
||||
// clang-format on
|
||||
/** Users can override this to define names of additional keycodes. */
|
||||
__attribute__((weak)) const keycode_string_name_t custom_keycode_names[] = {KEYCODE_STRING_NAMES_END};
|
||||
__attribute__((weak)) const keycode_string_name_t keycode_string_names_user[] = {KEYCODE_STRING_NAMES_END};
|
||||
/** Keyboard vendors can override this to define names of additional keycodes. */
|
||||
__attribute__((weak)) const keycode_string_name_t keycode_string_names_kb[] = {KEYCODE_STRING_NAMES_END};
|
||||
/** Names of the 4 mods on each hand. */
|
||||
static const char* mod_names[4] = {PSTR("CTL"), PSTR("SFT"), PSTR("ALT"), PSTR("GUI")};
|
||||
/** Internal buffer for holding a stringified keycode. */
|
||||
@@ -198,14 +200,18 @@ static void append_unary_keycode(const char* name, const char* param) {
|
||||
|
||||
/** Stringifies `keycode` and appends it to `buffer`. */
|
||||
static void append_keycode(uint16_t keycode) {
|
||||
// Search the `custom_keycode_names` table first so that it is possible to
|
||||
// override how any keycode would be formatted otherwise.
|
||||
const char* keycode_name = find_keycode_name(custom_keycode_names, keycode);
|
||||
// In case there is overlap among tables, search `keycode_string_names_user`
|
||||
// first so that it takes precedence.
|
||||
const char* keycode_name = find_keycode_name(keycode_string_names_user, keycode);
|
||||
if (keycode_name) {
|
||||
append_P(keycode_name);
|
||||
return;
|
||||
}
|
||||
keycode_name = find_keycode_name(keycode_string_names_kb, keycode);
|
||||
if (keycode_name) {
|
||||
append_P(keycode_name);
|
||||
return;
|
||||
}
|
||||
// Search the `keycode_names` table.
|
||||
keycode_name = find_keycode_name(keycode_names, keycode);
|
||||
if (keycode_name) {
|
||||
append_P(keycode_name);
|
||||
@@ -396,7 +402,7 @@ static void append_keycode(uint16_t keycode) {
|
||||
append_number(keycode, 16); // Fallback: write keycode as hex value.
|
||||
}
|
||||
|
||||
const char* keycode_string(uint16_t keycode) {
|
||||
const char* get_keycode_string(uint16_t keycode) {
|
||||
buffer_len = 0;
|
||||
buffer[0] = '\0';
|
||||
append_keycode(keycode);
|
||||
|
||||
@@ -55,13 +55,14 @@
|
||||
*
|
||||
* Unrecognized keycodes are printed numerically as hex values like `0x1ABC`.
|
||||
*
|
||||
* Optionally, use `custom_keycode_names` to define names for additional
|
||||
* keycodes or override how any of the above are formatted.
|
||||
* Optionally, use `keycode_string_names_user` or `keycode_string_names_kb` to
|
||||
* define names for additional keycodes or override how any of the above are
|
||||
* formatted.
|
||||
*
|
||||
* @param keycode QMK keycode.
|
||||
* @return Stringified keycode.
|
||||
*/
|
||||
const char* keycode_string(uint16_t keycode);
|
||||
const char* get_keycode_string(uint16_t keycode);
|
||||
|
||||
/** Defines a human-readable name for a keycode. */
|
||||
typedef struct {
|
||||
@@ -70,7 +71,7 @@ typedef struct {
|
||||
} keycode_string_name_t;
|
||||
|
||||
/**
|
||||
* @brief Names for additional keycodes for `keycode_string()`.
|
||||
* @brief Names for additional keycodes for `get_keycode_string()`.
|
||||
*
|
||||
* @note The table *must* end with `KEYCODE_STRING_NAMES_END`.
|
||||
*
|
||||
@@ -79,7 +80,7 @@ typedef struct {
|
||||
* override how `keycode_string()` formats a keycode. For example, supposing
|
||||
* keymap.c defines `MYMACRO1` and `MYMACRO2` as custom keycodes:
|
||||
*
|
||||
* const keycode_string_name_t custom_keycode_names[] = {
|
||||
* const keycode_string_name_t keycode_string_names_user[] = {
|
||||
* KEYCODE_STRING_NAME(MYMACRO1),
|
||||
* KEYCODE_STRING_NAME(MYMACRO2),
|
||||
* KEYCODE_STRING_NAME(KC_EXLM),
|
||||
@@ -89,7 +90,9 @@ typedef struct {
|
||||
* The above defines names for `MYMACRO1` and `MYMACRO2`, and overrides
|
||||
* `KC_EXLM` to format as "KC_EXLM" instead of the default "S(KC_1)".
|
||||
*/
|
||||
extern const keycode_string_name_t custom_keycode_names[];
|
||||
extern const keycode_string_name_t keycode_string_names_user[];
|
||||
/** Same as `keycode_string_names_user`, but for use at the keyboard level. */
|
||||
extern const keycode_string_name_t keycode_string_names_kb[];
|
||||
|
||||
/** Helper to define a keycode_string_name_t. */
|
||||
# define KEYCODE_STRING_NAME(kc) \
|
||||
@@ -102,7 +105,7 @@ extern const keycode_string_name_t custom_keycode_names[];
|
||||
|
||||
// When keycode_string is disabled, fall back to printing keycodes numerically
|
||||
// as decimal values, using get_u16_str() from quantum.c.
|
||||
# define keycode_string(kc) get_u16_str(kc, ' ')
|
||||
# define get_keycode_string(kc) get_u16_str(kc, ' ')
|
||||
|
||||
const char *get_u16_str(uint16_t curr_num, char curr_pad);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user