From eeb83fad76c559b157914347458531588d397e89 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Wed, 20 Mar 2024 17:35:55 -0700 Subject: [PATCH] Refactor key_defs.h to try to pull our actual key definitions out into files ontaining only key definitions to make introspection and codegen around lists of keys more straightforward --- src/kaleidoscope/key_def_constants.h | 32 ++++++++++++++++++++++++++++ src/kaleidoscope/key_defs.h | 29 +++++++++++++++++++++++++ src/kaleidoscope/key_defs/aliases.h | 11 ++-------- src/kaleidoscope/key_defs/base.h | 20 ----------------- src/kaleidoscope/key_defs/keymaps.h | 18 ---------------- src/kaleidoscope/key_defs/legacy.h | 4 ---- src/kaleidoscope/key_defs/sysctl.h | 1 + 7 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/kaleidoscope/key_def_constants.h b/src/kaleidoscope/key_def_constants.h index e8fe6c7fdd..7829297557 100644 --- a/src/kaleidoscope/key_def_constants.h +++ b/src/kaleidoscope/key_def_constants.h @@ -23,6 +23,25 @@ #define HID_KEYBOARD_FIRST_MODIFIER HID_KEYBOARD_LEFT_CONTROL #define HID_KEYBOARD_LAST_MODIFIER HID_KEYBOARD_RIGHT_GUI +static const uint8_t LAYER_OP_OFFSET = 42; +static const uint8_t LAYER_SHIFT_OFFSET = LAYER_OP_OFFSET; +static const uint8_t LAYER_MOVE_OFFSET = LAYER_SHIFT_OFFSET + LAYER_OP_OFFSET; +; + +// Layer number constants +#define KEYMAP_0 0 +#define KEYMAP_1 1 +#define KEYMAP_2 2 +#define KEYMAP_3 3 +#define KEYMAP_4 4 +#define KEYMAP_5 5 +#define KEYMAP_6 6 +#define KEYMAP_7 7 + +// Previous/next layer key constants +#define KEYMAP_PREVIOUS 33 +#define KEYMAP_NEXT 34 + // ----------------------------------------------------------------------------- // Constant flags values #define KEY_FLAGS 0b00000000 @@ -63,5 +82,18 @@ #define HID_TYPE_RTC 0b00000000 #define HID_TYPE_SEL 0b00000000 #define HID_TYPE_SV 0b00000000 +#define HID_TYPE_DV 0b00000000 +#define HID_TYPE_CP 0b00000000 + // Mask defining the allowed usage type flag bits: #define HID_TYPE_MASK 0b00110000 + + +// These legacy keycodes are defined for compatibility with existing ancient +// keymaps. They should not be used in new keymaps. +// We should formally remove them. + +#define KEY_BACKLIGHT_DOWN 0xf1 +#define KEY_BACKLIGHT_UP 0xf2 +#define KEY_RIGHT_FN2 0xfe +#define KEY_LEFT_FN2 0xff diff --git a/src/kaleidoscope/key_defs.h b/src/kaleidoscope/key_defs.h index c60b0c27b0..e2762efe0e 100644 --- a/src/kaleidoscope/key_defs.h +++ b/src/kaleidoscope/key_defs.h @@ -249,6 +249,35 @@ typedef kaleidoscope::Key Key; typedef kaleidoscope::Key Key_; +#define LCTRL(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), CTRL_HELD) +#define LALT(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), LALT_HELD) +#define RALT(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), RALT_HELD) +#define LSHIFT(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), SHIFT_HELD) +#define LGUI(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), GUI_HELD) +// To make it easier to create custom shortcuts, that do not interfere with +// system ones, an old trick is to use many modifiers. To make this easier, +// Ctrl+Shift+Alt is commonly abbreviated as "Meh", while Ctrl+Shift+Alt+GUI is +// often called "Hyper". To support this, we offer the `Key_Meh` and `Key_Hyper` +// aliases, along with `MEH(k)` and `HYPER(k)` to go with them. + +#define MEH(k) LCTRL(LSHIFT(LALT((k)))) +#define HYPER(k) LGUI(MEH((k))) + +#define SYSTEM_KEY(code, hid_type) \ + Key(code, SYNTHETIC | IS_SYSCTL | (hid_type & HID_TYPE_MASK)) + +/* Most Consumer keys are more then 8bit, the highest Consumer hid code + uses 10bit. By using the 11bit as flag to indicate a consumer keys was activate we can + use the 10 lsb as the HID Consumer code. If you need to get the keycode of a Consumer key + use the CONSUMER(key) macro this will return the 10bit keycode. +*/ +constexpr uint16_t CONSUMER_KEYCODE_MASK = 0x03FF; +#define CONSUMER(key) (key.getRaw() & CONSUMER_KEYCODE_MASK) +#define CONSUMER_KEY(code, hid_type) \ + Key((code & CONSUMER_KEYCODE_MASK) | \ + ((SYNTHETIC | IS_CONSUMER | (hid_type & HID_TYPE_MASK)) << 8)) + + namespace kaleidoscope { constexpr Key bad_keymap_key{0, RESERVED}; } diff --git a/src/kaleidoscope/key_defs/aliases.h b/src/kaleidoscope/key_defs/aliases.h index 35614ceaca..4525e9aa34 100644 --- a/src/kaleidoscope/key_defs/aliases.h +++ b/src/kaleidoscope/key_defs/aliases.h @@ -54,17 +54,10 @@ #define Key_Pipe LSHIFT(Key_Backslash) -// To make it easier to create custom shortcuts, that do not interfere with -// system ones, an old trick is to use many modifiers. To make this easier, -// Ctrl+Shift+Alt is commonly abbreviated as "Meh", while Ctrl+Shift+Alt+GUI is -// often called "Hyper". To support this, we offer the `Key_Meh` and `Key_Hyper` -// aliases, along with `MEH(k)` and `HYPER(k)` to go with them. -#define Key_Meh LCTRL(LSHIFT(Key_LeftAlt)) -#define Key_Hyper MEH(Key_LeftGui) +#define Key_Meh LCTRL(LSHIFT(Key_LeftAlt)) +#define Key_Hyper MEH(Key_LeftGui) -#define MEH(k) LCTRL(LSHIFT(LALT((k)))) -#define HYPER(k) LGUI(MEH((k))) // Apple's "globe" key (Consumer Control version). This key will bring up the // emoji tool on MacOS, just like tapping on the `fn` key on an Apple keyboard. diff --git a/src/kaleidoscope/key_defs/base.h b/src/kaleidoscope/key_defs/base.h index bb46c3a59c..61e304b81e 100644 --- a/src/kaleidoscope/key_defs/base.h +++ b/src/kaleidoscope/key_defs/base.h @@ -32,23 +32,3 @@ // The default value for new events. Used to signal that a keymap lookup should // be done. #define Key_Undefined Key_Transparent - -#define LCTRL(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), CTRL_HELD) -#define LALT(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), LALT_HELD) -#define RALT(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), RALT_HELD) -#define LSHIFT(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), SHIFT_HELD) -#define LGUI(k) kaleidoscope::addFlags(CONVERT_TO_KEY(k), GUI_HELD) - -#define SYSTEM_KEY(code, hid_type) \ - Key(code, SYNTHETIC | IS_SYSCTL | (hid_type & HID_TYPE_MASK)) - -/* Most Consumer keys are more then 8bit, the highest Consumer hid code - uses 10bit. By using the 11bit as flag to indicate a consumer keys was activate we can - use the 10 lsb as the HID Consumer code. If you need to get the keycode of a Consumer key - use the CONSUMER(key) macro this will return the 10bit keycode. -*/ -constexpr uint16_t CONSUMER_KEYCODE_MASK = 0x03FF; -#define CONSUMER(key) (key.getRaw() & CONSUMER_KEYCODE_MASK) -#define CONSUMER_KEY(code, hid_type) \ - Key((code & CONSUMER_KEYCODE_MASK) | \ - ((SYNTHETIC | IS_CONSUMER | (hid_type & HID_TYPE_MASK)) << 8)) diff --git a/src/kaleidoscope/key_defs/keymaps.h b/src/kaleidoscope/key_defs/keymaps.h index 5c29a63eef..bea625b9d5 100644 --- a/src/kaleidoscope/key_defs/keymaps.h +++ b/src/kaleidoscope/key_defs/keymaps.h @@ -18,24 +18,6 @@ #include // for uint8_t -static const uint8_t LAYER_OP_OFFSET = 42; -static const uint8_t LAYER_SHIFT_OFFSET = LAYER_OP_OFFSET; -static const uint8_t LAYER_MOVE_OFFSET = LAYER_SHIFT_OFFSET + LAYER_OP_OFFSET; -; - -// Layer number constants -#define KEYMAP_0 0 -#define KEYMAP_1 1 -#define KEYMAP_2 2 -#define KEYMAP_3 3 -#define KEYMAP_4 4 -#define KEYMAP_5 5 -#define KEYMAP_6 6 -#define KEYMAP_7 7 - -// Previous/next layer key constants -#define KEYMAP_PREVIOUS 33 -#define KEYMAP_NEXT 34 // Layer lock keys #define Key_Keymap0 Key(KEYMAP_0, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP) diff --git a/src/kaleidoscope/key_defs/legacy.h b/src/kaleidoscope/key_defs/legacy.h index f34fd9548b..363190b54e 100644 --- a/src/kaleidoscope/key_defs/legacy.h +++ b/src/kaleidoscope/key_defs/legacy.h @@ -20,11 +20,7 @@ // keymaps. They should not be used in new keymaps. // We should formally remove them. -#define KEY_BACKLIGHT_DOWN 0xf1 -#define KEY_BACKLIGHT_UP 0xf2 #define Key_BacklightDown Key(KEY_BACKLIGHT_DOWN, KEY_FLAGS) #define Key_BacklightUp Key(KEY_BACKLIGHT_UP, KEY_FLAGS) -#define KEY_RIGHT_FN2 0xfe #define Key_RFN2 Key(KEY_RIGHT_FN2, KEY_FLAGS) -#define KEY_LEFT_FN2 0xff #define Key_LFN2 Key(KEY_LEFT_FN2, KEY_FLAGS) diff --git a/src/kaleidoscope/key_defs/sysctl.h b/src/kaleidoscope/key_defs/sysctl.h index 1947b458e4..a4b0c09e7a 100644 --- a/src/kaleidoscope/key_defs/sysctl.h +++ b/src/kaleidoscope/key_defs/sysctl.h @@ -16,6 +16,7 @@ #pragma once +#include "kaleidoscope/key_def_constants.h" // System control mappings #define System_PowerDown SYSTEM_KEY(HID_SYSTEM_POWER_DOWN, HID_TYPE_OSC)