Skip to content

Commit

Permalink
Refactor key_defs.h to try to pull our actual key definitions out into
Browse files Browse the repository at this point in the history
files ontaining only key definitions to make introspection and codegen
around lists of keys more straightforward
  • Loading branch information
obra committed Mar 21, 2024
1 parent 7bcbdd6 commit eeb83fa
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 51 deletions.
32 changes: 32 additions & 0 deletions src/kaleidoscope/key_def_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
29 changes: 29 additions & 0 deletions src/kaleidoscope/key_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
11 changes: 2 additions & 9 deletions src/kaleidoscope/key_defs/aliases.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 0 additions & 20 deletions src/kaleidoscope/key_defs/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
18 changes: 0 additions & 18 deletions src/kaleidoscope/key_defs/keymaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,6 @@

#include <stdint.h> // 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)
Expand Down
4 changes: 0 additions & 4 deletions src/kaleidoscope/key_defs/legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions src/kaleidoscope/key_defs/sysctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit eeb83fa

Please sign in to comment.