-
-
Notifications
You must be signed in to change notification settings - Fork 40.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Core] get_keycode_string(): function to format keycodes as strings, for more readable debug logging. #24787
base: develop
Are you sure you want to change the base?
Conversation
This adds the `keycode_string()` function described in https://getreuer.info/posts/keyboards/keycode-string/index.html as a core feature.
* 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.
Thanks for the quick review, @drashna! I've updated the PR according to your comments. |
Co-authored-by: Ryan <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like LTO really doesn't like the user/keeb strings (and even adding it via QUANTUM_LIB_SRC causes warnings).
Uh oh. The problem is with strings defined in |
@drashna I made a tweak in 45f573f that builds and runs successfully with custom names on Voyager. The user syntax is slightly different (and uglier, unfortunately) from before: change const keycode_string_name_t keycode_string_names_user[] = {
KEYCODE_STRING_NAME(MYMACRO1),
KEYCODE_STRING_NAME(MYMACRO2),
KEYCODE_STRING_NAMES_END // End of table sentinel.
}; to const keycode_string_name_t *keycode_string_names_user = (keycode_string_name_t []){
KEYCODE_STRING_NAME(MYMACRO1),
KEYCODE_STRING_NAME(MYMACRO2),
KEYCODE_STRING_NAMES_END // End of table sentinel.
}; |
Yeah, it was specifically with the const's yeah. It didn't like that they were different/declared differently.
Can confirm that this works, but yeah, it's definitely uglier. (Also testing it out myself.... it will probably be a matter of time before somebody asks about layer names for those keycodes :D ) |
@drashna here is another take on the API, hopefully nicer this way. In commit 8eaf67d, user keycode names are added like this: define #define KEYCODE_STRING_NAMES_USER and define keycode names in keymap.c as const keycode_string_name_t keycode_string_names_user[] = {
KEYCODE_STRING_NAME(MYMACRO1),
KEYCODE_STRING_NAME(MYMACRO2),
}; This removes the ugly
That would be cool! =D I'd be happy to add it. Should I hold off on that as a separate PR or go ahead and include in this one? |
This is preferred and matches the introspection changes for combos and the like.
Keep it separate, far easier for reviews to keep things limited to one item, and if precedents are set they can be reused. |
quantum/keymap_introspection.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, this causes issues. It expects that both the kb and user string arrays are gonna to be in the keymap.c. Which for the kb one array is likely not to be true.
At best, it would require the kb array to be put into the keyboard's h file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops! Right, thanks for catching that. I've reverted back to syntax
const keycode_string_name_t *keycode_string_names_user = (keycode_string_name_t []){
KEYCODE_STRING_NAME(MYMACRO1),
KEYCODE_STRING_NAME(MYMACRO2),
KEYCODE_STRING_NAMES_END // End of table sentinel.
};
While I was at it, I realized that using PSTR()
within the KEYCODE_STRING_NAME
does not build on AVR (a compile error that, essentially, PSTR cannot be used within a braced expression). It's desirable to use PSTR at least for the common keycode names, since this is ~half a kilobyte of data. I've revised so that the _user and _kb names are in RAM, not using PSTR, while the common names use a separate representation still using PSTR, and in a way that builds on AVR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option could be to wrap the definition in a macro, somewhat similar to RGBLIGHT_LAYER_SEGMENTS
(but more than that, because the definition needs to be performed by the macro too):
#define DEFINE_KEYCODE_STRING_NAMES_KB(...) \
static const keycode_string_name_t keycode_string_names_kb_data[] = { __VA_ARGS__, KEYCODE_STRING_NAMES_END }; \
const keycode_string_name_t *keycode_string_names_kb = keycode_string_names_kb_data
and similarly for the _user
version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d say if we can get it in without introspection for now we’re fine. Can always give it another pass later if necessary — the introspection is somewhat of a red herring in this scenario because they’re not really intended to be runtime-reconfigurable.
I made mention of it mainly as an example of not using null terminators and the like as a pattern, not as a “please hook it into introspection too” request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sigprof thank you for the suggestion! Using that, custom keycode names can now work with the interface
KEYCODE_STRING_NAMES_USER(
KEYCODE_STRING_NAME(MYMACRO1),
KEYCODE_STRING_NAME(MYMACRO2),
);
* Don't use PSTR in KEYCODE_STRING_NAME, since this fails to build on AVR. Store custom names in RAM. * Revise the internal table of common keycode names to use its own storage representation, still in PROGMEM, and now more efficiently stored flat in 8 bytes per entry. * Support Swap Hands keycodes and a few other keycodes.
This reverts commit 2a27710.
This reverts commit 8eaf67d.
* Don't use PSTR in KEYCODE_STRING_NAME, since this fails to build on AVR. Store custom names in RAM. * Revise the internal table of common keycode names to use its own storage representation, still in PROGMEM, and now more efficiently stored flat in 8 bytes per entry. * Support Swap Hands keycodes and a few other keycodes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@getreuer Great stuff, this also opens up the opportunity to get rid of the unit-tests only implementation for deriving string representations... which I already did in my branch https://github.com/KarlK90/qmk_firmware/commits/core/keycode_string_unit_tests/ - let me know if you want to cherry-pick the commits in this PR otherwise I'll just open a PR myself afterwards.
Thank you @KarlK90! Using A design question to weigh is that while Arguably, Or arguably, it is Ok that I suggest that reusing |
This is a good plan but IMHO this extension can happen in the future as the most frequently used keycodes are covered.
That is totally fine, the use-case are unit-tests and the implementer can extend the printing functions to get coverage (or just live with the fallback). From my POV their is greater gain in having only one maintained solution.
Agreed, let's do this in separate PR :). |
Random thought -- perhaps for the undeclared keycodes if they're within a known range, you could do something like |
* Add QK_LOCK keycode. * Add Secure keycodes. * Add Joystick keycodes. * Add Programmable Button keycodes. * Add macro MC_ keycodes. * For remaining keys in known code ranges, stringify them as "QK_<feature>+<number>". For instance, "QK_MIDI+7".
That's a nice solution! I've updated to do that. |
This PR adds
get_keycode_string()
as described in https://getreuer.info/posts/keyboards/keycode-string/index.html as a utility function in core.Description
It's much nicer to read keycodes as names like "
LT(2,KC_D)
" than numerical codes like "0x4207
." This PR adds a functionget_keycode_string()
for that purpose, based on my userspace implementation linked above.How to use it
Add in
rules.mk
:KEYCODE_STRING_ENABLE = yes
Then use the
get_keycode_string(kc)
function to convert a given 16-bit keycode to a human-readable string. Example:Limitations
Many common QMK keycodes are recognized by
get_keycode_string()
, but not all. These include some common basic keycodes, layer switch keycodes, mod-taps, one-shot keycodes, tap dance keycodes, and Unicode keycodes. As a fallback, an unrecognized keycode is written as a hex number. Names for additional keycodes can also be added, see below.Keycodes are stringified according to the default KC_-prefixed keycodes, which is suboptimal when the computer is configured to a non-US-QWERTY layout.
Defining names for additional keycodes
Optionally,
KEYCODE_STRING_NAMES_USER
orKEYCODE_STRING_NAMES_KB
may be used to add names for additional keycodes. For example, supposing keymap.c definesMYMACRO1
andMYMACRO2
as custom keycodes, the following adds their names:Types of Changes
Checklist