From b31426252ed379937132ed7d2baf84a677123b5b Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Wed, 1 Nov 2023 02:11:42 +0000 Subject: [PATCH] Generate switch statement helpers for keycode ranges (#20059) --- lib/python/qmk/cli/generate/keycodes.py | 8 ++++++++ quantum/keycodes.h | 22 ++++++++++++++++++++++ quantum/keymap_common.c | 10 +++++----- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/python/qmk/cli/generate/keycodes.py b/lib/python/qmk/cli/generate/keycodes.py index ed8b6827bdf..719fced5d54 100644 --- a/lib/python/qmk/cli/generate/keycodes.py +++ b/lib/python/qmk/cli/generate/keycodes.py @@ -94,6 +94,14 @@ def _generate_helpers(lines, keycodes): hi = keycodes["keycodes"][f'0x{codes[1]:04X}']['key'] lines.append(f'#define IS_{ _translate_group(group).upper() }_KEYCODE(code) ((code) >= {lo} && (code) <= {hi})') + lines.append('') + lines.append('// Switch statement Helpers') + for group, codes in temp.items(): + lo = keycodes["keycodes"][f'0x{codes[0]:04X}']['key'] + hi = keycodes["keycodes"][f'0x{codes[1]:04X}']['key'] + name = f'{ _translate_group(group).upper() }_KEYCODE_RANGE' + lines.append(f'#define { name.ljust(35) } {lo} ... {hi}') + def _generate_aliases(lines, keycodes): # Work around ChibiOS ch.h include guard diff --git a/quantum/keycodes.h b/quantum/keycodes.h index bbf10da36d9..39fd2e27266 100644 --- a/quantum/keycodes.h +++ b/quantum/keycodes.h @@ -1420,3 +1420,25 @@ enum qk_keycode_defines { #define IS_QUANTUM_KEYCODE(code) ((code) >= QK_BOOTLOADER && (code) <= QK_ALT_REPEAT_KEY) #define IS_KB_KEYCODE(code) ((code) >= QK_KB_0 && (code) <= QK_KB_31) #define IS_USER_KEYCODE(code) ((code) >= QK_USER_0 && (code) <= QK_USER_31) + +// Switch statement Helpers +#define INTERNAL_KEYCODE_RANGE KC_NO ... KC_TRANSPARENT +#define BASIC_KEYCODE_RANGE KC_A ... KC_EXSEL +#define SYSTEM_KEYCODE_RANGE KC_SYSTEM_POWER ... KC_SYSTEM_WAKE +#define CONSUMER_KEYCODE_RANGE KC_AUDIO_MUTE ... KC_LAUNCHPAD +#define MOUSE_KEYCODE_RANGE KC_MS_UP ... KC_MS_ACCEL2 +#define MODIFIER_KEYCODE_RANGE KC_LEFT_CTRL ... KC_RIGHT_GUI +#define SWAP_HANDS_KEYCODE_RANGE QK_SWAP_HANDS_TOGGLE ... QK_SWAP_HANDS_ONE_SHOT +#define MAGIC_KEYCODE_RANGE QK_MAGIC_SWAP_CONTROL_CAPS_LOCK ... QK_MAGIC_TOGGLE_ESCAPE_CAPS_LOCK +#define MIDI_KEYCODE_RANGE QK_MIDI_ON ... QK_MIDI_PITCH_BEND_UP +#define SEQUENCER_KEYCODE_RANGE QK_SEQUENCER_ON ... QK_SEQUENCER_STEPS_CLEAR +#define JOYSTICK_KEYCODE_RANGE QK_JOYSTICK_BUTTON_0 ... QK_JOYSTICK_BUTTON_31 +#define PROGRAMMABLE_BUTTON_KEYCODE_RANGE QK_PROGRAMMABLE_BUTTON_1 ... QK_PROGRAMMABLE_BUTTON_32 +#define AUDIO_KEYCODE_RANGE QK_AUDIO_ON ... QK_AUDIO_VOICE_PREVIOUS +#define STENO_KEYCODE_RANGE QK_STENO_BOLT ... QK_STENO_COMB_MAX +#define MACRO_KEYCODE_RANGE QK_MACRO_0 ... QK_MACRO_31 +#define BACKLIGHT_KEYCODE_RANGE QK_BACKLIGHT_ON ... QK_BACKLIGHT_TOGGLE_BREATHING +#define RGB_KEYCODE_RANGE RGB_TOG ... RGB_MODE_TWINKLE +#define QUANTUM_KEYCODE_RANGE QK_BOOTLOADER ... QK_ALT_REPEAT_KEY +#define KB_KEYCODE_RANGE QK_KB_0 ... QK_KB_31 +#define USER_KEYCODE_RANGE QK_USER_0 ... QK_USER_31 diff --git a/quantum/keymap_common.c b/quantum/keymap_common.c index 9a67fad278c..91e47a72ee5 100644 --- a/quantum/keymap_common.c +++ b/quantum/keymap_common.c @@ -59,19 +59,19 @@ action_t action_for_keycode(uint16_t keycode) { (void)mod; switch (keycode) { - case KC_A ... KC_EXSEL: - case KC_LEFT_CTRL ... KC_RIGHT_GUI: + case BASIC_KEYCODE_RANGE: + case MODIFIER_KEYCODE_RANGE: action.code = ACTION_KEY(keycode); break; #ifdef EXTRAKEY_ENABLE - case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE: + case SYSTEM_KEYCODE_RANGE: action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode)); break; - case KC_AUDIO_MUTE ... KC_LAUNCHPAD: + case CONSUMER_KEYCODE_RANGE: action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode)); break; #endif - case KC_MS_UP ... KC_MS_ACCEL2: + case MOUSE_KEYCODE_RANGE: action.code = ACTION_MOUSEKEY(keycode); break; case KC_TRANSPARENT: