Skip to content

Commit d8a608f

Browse files
Wilba6582Wilba6582
Wilba6582
authored and
Wilba6582
committed
Keycode refactor
1 parent 223cffd commit d8a608f

File tree

4 files changed

+331
-314
lines changed

4 files changed

+331
-314
lines changed

quantum/keymap.h

+6-308
Original file line numberDiff line numberDiff line change
@@ -38,318 +38,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
3838
#define RESET QK_RESET
3939
#endif
4040

41-
/* translates key to keycode */
41+
#include "quantum_keycodes.h"
42+
43+
// translates key to keycode
4244
uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key);
4345

46+
// translates function id to action
47+
uint16_t keymap_function_id_to_action( uint16_t function_id );
48+
4449
extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
4550
extern const uint16_t fn_actions[];
4651

47-
enum quantum_keycodes {
48-
// Ranges used in shortucuts - not to be used directly
49-
QK_TMK = 0x0000,
50-
QK_TMK_MAX = 0x00FF,
51-
QK_MODS = 0x0100,
52-
QK_LCTL = 0x0100,
53-
QK_LSFT = 0x0200,
54-
QK_LALT = 0x0400,
55-
QK_LGUI = 0x0800,
56-
QK_RMODS_MIN = 0x1000,
57-
QK_RCTL = 0x1100,
58-
QK_RSFT = 0x1200,
59-
QK_RALT = 0x1400,
60-
QK_RGUI = 0x1800,
61-
QK_MODS_MAX = 0x1FFF,
62-
QK_FUNCTION = 0x2000,
63-
QK_FUNCTION_MAX = 0x2FFF,
64-
QK_MACRO = 0x3000,
65-
QK_MACRO_MAX = 0x3FFF,
66-
QK_LAYER_TAP = 0x4000,
67-
QK_LAYER_TAP_MAX = 0x4FFF,
68-
QK_TO = 0x5000,
69-
QK_TO_MAX = 0x50FF,
70-
QK_MOMENTARY = 0x5100,
71-
QK_MOMENTARY_MAX = 0x51FF,
72-
QK_DEF_LAYER = 0x5200,
73-
QK_DEF_LAYER_MAX = 0x52FF,
74-
QK_TOGGLE_LAYER = 0x5300,
75-
QK_TOGGLE_LAYER_MAX = 0x53FF,
76-
QK_ONE_SHOT_LAYER = 0x5400,
77-
QK_ONE_SHOT_LAYER_MAX = 0x54FF,
78-
QK_ONE_SHOT_MOD = 0x5500,
79-
QK_ONE_SHOT_MOD_MAX = 0x55FF,
80-
#ifndef DISABLE_CHORDING
81-
QK_CHORDING = 0x5600,
82-
QK_CHORDING_MAX = 0x56FF,
83-
#endif
84-
QK_MOD_TAP = 0x6000,
85-
QK_MOD_TAP_MAX = 0x6FFF,
86-
QK_TAP_DANCE = 0x7100,
87-
QK_TAP_DANCE_MAX = 0x71FF,
88-
#ifdef UNICODEMAP_ENABLE
89-
QK_UNICODE_MAP = 0x7800,
90-
QK_UNICODE_MAP_MAX = 0x7FFF,
91-
#endif
92-
#ifdef UNICODE_ENABLE
93-
QK_UNICODE = 0x8000,
94-
QK_UNICODE_MAX = 0xFFFF,
95-
#endif
96-
97-
// Loose keycodes - to be used directly
98-
99-
RESET = 0x7000,
100-
DEBUG,
101-
MAGIC_SWAP_CONTROL_CAPSLOCK,
102-
MAGIC_CAPSLOCK_TO_CONTROL,
103-
MAGIC_SWAP_LALT_LGUI,
104-
MAGIC_SWAP_RALT_RGUI,
105-
MAGIC_NO_GUI,
106-
MAGIC_SWAP_GRAVE_ESC,
107-
MAGIC_SWAP_BACKSLASH_BACKSPACE,
108-
MAGIC_HOST_NKRO,
109-
MAGIC_SWAP_ALT_GUI,
110-
MAGIC_UNSWAP_CONTROL_CAPSLOCK,
111-
MAGIC_UNCAPSLOCK_TO_CONTROL,
112-
MAGIC_UNSWAP_LALT_LGUI,
113-
MAGIC_UNSWAP_RALT_RGUI,
114-
MAGIC_UNNO_GUI,
115-
MAGIC_UNSWAP_GRAVE_ESC,
116-
MAGIC_UNSWAP_BACKSLASH_BACKSPACE,
117-
MAGIC_UNHOST_NKRO,
118-
MAGIC_UNSWAP_ALT_GUI,
119-
MAGIC_TOGGLE_NKRO,
120-
121-
// Leader key
122-
#ifndef DISABLE_LEADER
123-
KC_LEAD,
124-
#endif
125-
126-
// Audio on/off/toggle
127-
AU_ON,
128-
AU_OFF,
129-
AU_TOG,
130-
131-
// Music mode on/off/toggle
132-
MU_ON,
133-
MU_OFF,
134-
MU_TOG,
135-
136-
// Music voice iterate
137-
MUV_IN,
138-
MUV_DE,
139-
140-
// Midi mode on/off
141-
MIDI_ON,
142-
MIDI_OFF,
143-
144-
// Backlight functionality
145-
BL_0,
146-
BL_1,
147-
BL_2,
148-
BL_3,
149-
BL_4,
150-
BL_5,
151-
BL_6,
152-
BL_7,
153-
BL_8,
154-
BL_9,
155-
BL_10,
156-
BL_11,
157-
BL_12,
158-
BL_13,
159-
BL_14,
160-
BL_15,
161-
BL_DEC,
162-
BL_INC,
163-
BL_TOGG,
164-
BL_STEP,
165-
166-
// RGB functionality
167-
RGB_TOG,
168-
RGB_MOD,
169-
RGB_HUI,
170-
RGB_HUD,
171-
RGB_SAI,
172-
RGB_SAD,
173-
RGB_VAI,
174-
RGB_VAD,
175-
176-
// Left shift, open paren
177-
KC_LSPO,
178-
179-
// Right shift, close paren
180-
KC_RSPC,
181-
182-
// Printing
183-
PRINT_ON,
184-
PRINT_OFF,
185-
186-
// always leave at the end
187-
SAFE_RANGE
188-
};
189-
190-
// Ability to use mods in layouts
191-
#define LCTL(kc) (kc | QK_LCTL)
192-
#define LSFT(kc) (kc | QK_LSFT)
193-
#define LALT(kc) (kc | QK_LALT)
194-
#define LGUI(kc) (kc | QK_LGUI)
195-
#define RCTL(kc) (kc | QK_RCTL)
196-
#define RSFT(kc) (kc | QK_RSFT)
197-
#define RALT(kc) (kc | QK_RALT)
198-
#define RGUI(kc) (kc | QK_RGUI)
199-
200-
#define HYPR(kc) (kc | QK_LCTL | QK_LSFT | QK_LALT | QK_LGUI)
201-
#define MEH(kc) (kc | QK_LCTL | QK_LSFT | QK_LALT)
202-
#define LCAG(kc) (kc | QK_LCTL | QK_LALT | QK_LGUI)
203-
#define ALTG(kc) (kc | QK_RCTL | QK_RALT)
204-
205-
#define MOD_HYPR 0xf
206-
#define MOD_MEH 0x7
207-
208-
209-
// Aliases for shifted symbols
210-
// Each key has a 4-letter code, and some have longer aliases too.
211-
// While the long aliases are descriptive, the 4-letter codes
212-
// make for nicer grid layouts (everything lines up), and are
213-
// the preferred style for Quantum.
214-
#define KC_TILD LSFT(KC_GRV) // ~
215-
#define KC_TILDE KC_TILD
216-
217-
#define KC_EXLM LSFT(KC_1) // !
218-
#define KC_EXCLAIM KC_EXLM
219-
220-
#define KC_AT LSFT(KC_2) // @
221-
222-
#define KC_HASH LSFT(KC_3) // #
223-
224-
#define KC_DLR LSFT(KC_4) // $
225-
#define KC_DOLLAR KC_DLR
226-
227-
#define KC_PERC LSFT(KC_5) // %
228-
#define KC_PERCENT KC_PERC
229-
230-
#define KC_CIRC LSFT(KC_6) // ^
231-
#define KC_CIRCUMFLEX KC_CIRC
232-
233-
#define KC_AMPR LSFT(KC_7) // &
234-
#define KC_AMPERSAND KC_AMPR
235-
236-
#define KC_ASTR LSFT(KC_8) // *
237-
#define KC_ASTERISK KC_ASTR
238-
239-
#define KC_LPRN LSFT(KC_9) // (
240-
#define KC_LEFT_PAREN KC_LPRN
241-
242-
#define KC_RPRN LSFT(KC_0) // )
243-
#define KC_RIGHT_PAREN KC_RPRN
244-
245-
#define KC_UNDS LSFT(KC_MINS) // _
246-
#define KC_UNDERSCORE KC_UNDS
247-
248-
#define KC_PLUS LSFT(KC_EQL) // +
249-
250-
#define KC_LCBR LSFT(KC_LBRC) // {
251-
#define KC_LEFT_CURLY_BRACE KC_LCBR
252-
253-
#define KC_RCBR LSFT(KC_RBRC) // }
254-
#define KC_RIGHT_CURLY_BRACE KC_RCBR
255-
256-
#define KC_LABK LSFT(KC_COMM) // <
257-
#define KC_LEFT_ANGLE_BRACKET KC_LABK
258-
259-
#define KC_RABK LSFT(KC_DOT) // >
260-
#define KC_RIGHT_ANGLE_BRACKET KC_RABK
261-
262-
#define KC_COLN LSFT(KC_SCLN) // :
263-
#define KC_COLON KC_COLN
264-
265-
#define KC_PIPE LSFT(KC_BSLS) // |
266-
267-
#define KC_LT LSFT(KC_COMM) // <
268-
269-
#define KC_GT LSFT(KC_DOT) // >
270-
271-
#define KC_QUES LSFT(KC_SLSH) // ?
272-
#define KC_QUESTION KC_QUES
273-
274-
#define KC_DQT LSFT(KC_QUOT) // "
275-
#define KC_DOUBLE_QUOTE KC_DQT
276-
#define KC_DQUO KC_DQT
277-
278-
#define KC_DELT KC_DELETE // Del key (four letter code)
279-
280-
// Alias for function layers than expand past FN31
281-
#define FUNC(kc) (kc | QK_FUNCTION)
282-
283-
// Aliases
284-
#define S(kc) LSFT(kc)
285-
#define F(kc) FUNC(kc)
286-
287-
#define M(kc) (kc | QK_MACRO)
288-
289-
#define MACRODOWN(...) (record->event.pressed ? MACRO(__VA_ARGS__) : MACRO_NONE)
290-
291-
// L-ayer, T-ap - 256 keycode max, 16 layer max
292-
#define LT(layer, kc) (kc | QK_LAYER_TAP | ((layer & 0xF) << 8))
293-
294-
#define AG_SWAP MAGIC_SWAP_ALT_GUI
295-
#define AG_NORM MAGIC_UNSWAP_ALT_GUI
296-
297-
#define BL_ON BL_9
298-
#define BL_OFF BL_0
299-
300-
#define MI_ON MIDI_ON
301-
#define MI_OFF MIDI_OFF
302-
303-
// GOTO layer - 16 layers max
304-
// when:
305-
// ON_PRESS = 1
306-
// ON_RELEASE = 2
307-
// Unless you have a good reason not to do so, prefer ON_PRESS (1) as your default.
308-
// In fact, we changed it to assume ON_PRESS for sanity/simplicity. If needed, you can add your own
309-
// keycode modeled after the old version, kept below for this.
310-
/* #define TO(layer, when) (layer | QK_TO | (when << 0x4)) */
311-
#define TO(layer) (layer | QK_TO | (ON_PRESS << 0x4))
312-
313-
// Momentary switch layer - 256 layer max
314-
#define MO(layer) (layer | QK_MOMENTARY)
315-
316-
// Set default layer - 256 layer max
317-
#define DF(layer) (layer | QK_DEF_LAYER)
318-
319-
// Toggle to layer - 256 layer max
320-
#define TG(layer) (layer | QK_TOGGLE_LAYER)
321-
322-
// One-shot layer - 256 layer max
323-
#define OSL(layer) (layer | QK_ONE_SHOT_LAYER)
324-
325-
// One-shot mod
326-
#define OSM(mod) (mod | QK_ONE_SHOT_MOD)
327-
328-
// M-od, T-ap - 256 keycode max
329-
#define MT(mod, kc) (kc | QK_MOD_TAP | ((mod & 0xF) << 8))
330-
#define CTL_T(kc) MT(MOD_LCTL, kc)
331-
#define SFT_T(kc) MT(MOD_LSFT, kc)
332-
#define ALT_T(kc) MT(MOD_LALT, kc)
333-
#define GUI_T(kc) MT(MOD_LGUI, kc)
334-
#define C_S_T(kc) MT((MOD_LCTL | MOD_LSFT), kc) // Control + Shift e.g. for gnome-terminal
335-
#define MEH_T(kc) MT((MOD_LCTL | MOD_LSFT | MOD_LALT), kc) // Meh is a less hyper version of the Hyper key -- doesn't include Win or Cmd, so just alt+shift+ctrl
336-
#define LCAG_T(kc) MT((MOD_LCTL | MOD_LALT | MOD_LGUI), kc) // Left control alt and gui
337-
#define ALL_T(kc) MT((MOD_LCTL | MOD_LSFT | MOD_LALT | MOD_LGUI), kc) // see http://brettterpstra.com/2012/12/08/a-useful-caps-lock-key/
338-
339-
// Dedicated keycode versions for Hyper and Meh, if you want to use them as standalone keys rather than mod-tap
340-
#define KC_HYPR HYPR(KC_NO)
341-
#define KC_MEH MEH(KC_NO)
342-
343-
#ifdef UNICODE_ENABLE
344-
// For sending unicode codes.
345-
// You may not send codes over 7FFF -- this supports most of UTF8.
346-
// To have a key that sends out Œ, go UC(0x0152)
347-
#define UNICODE(n) (n | QK_UNICODE)
348-
#define UC(n) UNICODE(n)
349-
#endif
350-
351-
#ifdef UNICODEMAP_ENABLE
352-
#define X(n) (n | QK_UNICODE_MAP)
353-
#endif
35452

35553
#endif

quantum/keymap_common.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ action_t action_for_key(uint8_t layer, keypos_t key)
4848

4949
action_t action;
5050
uint8_t action_layer, when, mod;
51-
// The arm-none-eabi compiler generates out of bounds warnings when using the fn_actions directly for some reason
52-
const uint16_t* actions = fn_actions;
5351

5452
switch (keycode) {
5553
case KC_FN0 ... KC_FN31:
56-
action.code = pgm_read_word(&actions[FN_INDEX(keycode)]);
54+
action.code = keymap_function_id_to_action(FN_INDEX(keycode));
5755
break;
5856
case KC_A ... KC_EXSEL:
5957
case KC_LCTRL ... KC_RGUI:
@@ -79,7 +77,7 @@ action_t action_for_key(uint8_t layer, keypos_t key)
7977
case QK_FUNCTION ... QK_FUNCTION_MAX: ;
8078
// Is a shortcut for function action_layer, pull last 12bits
8179
// This means we have 4,096 FN macros at our disposal
82-
action.code = pgm_read_word(&actions[(int)keycode & 0xFFF]);
80+
action.code = keymap_function_id_to_action( (int)keycode & 0xFFF );
8381
break;
8482
case QK_MACRO ... QK_MACRO_MAX:
8583
action.code = ACTION_MACRO(keycode & 0xFF);
@@ -163,9 +161,17 @@ void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
163161
{
164162
}
165163

166-
/* translates key to keycode */
164+
// translates key to keycode
165+
__attribute__ ((weak))
167166
uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
168167
{
169168
// Read entire word (16bits)
170169
return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
171170
}
171+
172+
// translates function id to action
173+
__attribute__ ((weak))
174+
uint16_t keymap_function_id_to_action( uint16_t function_id )
175+
{
176+
return pgm_read_word(&fn_actions[function_id]);
177+
}

0 commit comments

Comments
 (0)