aboutsummaryrefslogtreecommitdiff
path: root/quantum/process_keycode
diff options
context:
space:
mode:
authorJonas Gessner <JonasGessner@users.noreply.github.com>2021-07-13 19:13:51 +0200
committerGitHub <noreply@github.com>2021-07-13 10:13:51 -0700
commit52cfc9259b58a3a11a244fbe35c49c7dd1a9cae0 (patch)
tree71cbd4b8e2c622a33cbb4d080a08e035901f30c8 /quantum/process_keycode
parent1ae4d52013c9f38bdc5c208ff8bbfdf173e1dddd (diff)
downloadqmk_firmware-52cfc9259b58a3a11a244fbe35c49c7dd1a9cae0.tar.gz
qmk_firmware-52cfc9259b58a3a11a244fbe35c49c7dd1a9cae0.zip
[Feature] Key Overrides (#11422)
Diffstat (limited to 'quantum/process_keycode')
-rw-r--r--quantum/process_keycode/process_key_override.c518
-rw-r--r--quantum/process_keycode/process_key_override.h147
-rw-r--r--quantum/process_keycode/process_key_override_private.h24
3 files changed, 689 insertions, 0 deletions
diff --git a/quantum/process_keycode/process_key_override.c b/quantum/process_keycode/process_key_override.c
new file mode 100644
index 000000000..fe43eacc4
--- /dev/null
+++ b/quantum/process_keycode/process_key_override.c
@@ -0,0 +1,518 @@
1/*
2 * Copyright 2021 Jonas Gessner
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "quantum.h"
19#include "report.h"
20#include "timer.h"
21#include "process_key_override_private.h"
22
23#include <debug.h>
24
25#ifndef KEY_OVERRIDE_REPEAT_DELAY
26# define KEY_OVERRIDE_REPEAT_DELAY 500
27#endif
28
29// For benchmarking the time it takes to call process_key_override on every key press (needs keyboard debugging enabled as well)
30// #define BENCH_KEY_OVERRIDE
31
32// For debug output (needs keyboard debugging enabled as well)
33// #define DEBUG_KEY_OVERRIDE
34
35#ifdef DEBUG_KEY_OVERRIDE
36# define key_override_printf dprintf
37#else
38# define key_override_printf(str, ...) \
39 {}
40#endif
41
42// Helpers
43
44// Private functions implemented elsewhere in qmk/tmk
45extern uint8_t extract_mod_bits(uint16_t code);
46extern void set_weak_override_mods(uint8_t mods);
47extern void clear_weak_override_mods(void);
48extern void set_suppressed_override_mods(uint8_t mods);
49extern void clear_suppressed_override_mods(void);
50
51static uint16_t clear_mods_from(uint16_t keycode) {
52 switch (keycode) {
53 case QK_MODS ... QK_MODS_MAX:
54 break;
55 default:
56 return keycode;
57 }
58
59 static const uint16_t all_mods = QK_LCTL | QK_LSFT | QK_LALT | QK_LGUI | QK_RCTL | QK_RSFT | QK_RALT | QK_RGUI;
60
61 return (keycode & ~(all_mods));
62}
63
64// Internal variables
65static const key_override_t *active_override = NULL;
66static bool active_override_trigger_is_down = false;
67
68// Used to keep track of what non-modifier key was last pressed down. We never want to activate an override for a trigger key that is not the last non-mod key that was pressed down. OSes internally completely unregister a key that is held when a different key is held down after. We want to respect this here.
69static uint16_t last_key_down = 0;
70// When was the last key pressed down?
71static uint32_t last_key_down_time = 0;
72
73// What timestamp are we comparing to when waiting to register a deferred key?
74static uint32_t defer_reference_time = 0;
75// What delay should pass until deferred key is registered?
76static uint32_t defer_delay = 0;
77
78// Holds the keycode that should be registered at a later time, in order to not get false key presses
79static uint16_t deferred_register = 0;
80
81// TODO: in future maybe save in EEPROM?
82static bool enabled = true;
83
84// Public variables
85__attribute__((weak)) const key_override_t **key_overrides = NULL;
86
87// Forward decls
88static const key_override_t *clear_active_override(const bool allow_reregister);
89
90void key_override_on(void) {
91 enabled = true;
92 key_override_printf("Key override ON\n");
93}
94
95void key_override_off(void) {
96 enabled = false;
97 clear_active_override(false);
98 key_override_printf("Key override OFF\n");
99}
100
101void key_override_toggle(void) {
102 if (key_override_is_enabled()) {
103 key_override_off();
104 } else {
105 key_override_on();
106 }
107}
108
109bool key_override_is_enabled(void) { return enabled; }
110
111// Returns whether the modifiers that are pressed are such that the override should activate
112static bool key_override_matches_active_modifiers(const key_override_t *override, const uint8_t mods) {
113 // Check that negative keys pass
114 if ((override->negative_mod_mask & mods) != 0) {
115 return false;
116 }
117
118 // Immediately return true if the override requires no mods down
119 if (override->trigger_mods == 0) {
120 return true;
121 }
122
123 if ((override->options & ko_option_one_mod) != 0) {
124 // At least one of the trigger modifiers must be down
125 return (override->trigger_mods & mods) != 0;
126 } else {
127 // All trigger modifiers must be down, but each mod can be active on either side (if both sides are specified).
128
129 // Which mods, regardless of side, are required?
130 uint8_t one_sided_required_mods = (override->trigger_mods & 0b1111) | (override->trigger_mods >> 4);
131
132 // Which of the required modifiers are active?
133 uint8_t active_required_mods = override->trigger_mods & mods;
134
135 // Move the active requird mods to one side
136 uint8_t one_sided_active_required_mods = (active_required_mods & 0b1111) | (active_required_mods >> 4);
137
138 // Check that there is a full match between the required one-sided mods and active required one sided mods
139 return one_sided_active_required_mods == one_sided_required_mods;
140 }
141
142 return false;
143}
144
145static void schedule_deferred_register(const uint16_t keycode) {
146 if (timer_elapsed32(last_key_down_time) < KEY_OVERRIDE_REPEAT_DELAY) {
147 // Defer until KEY_OVERRIDE_REPEAT_DELAY has passed since the trigger key was pressed down. This emulates the behavior as holding down a key x, then holding down shift shortly after. Usually the shifted key X is not immediately produced, but rather a 'key repeat delay' passes before any repeated character is output.
148 defer_reference_time = last_key_down_time;
149 defer_delay = KEY_OVERRIDE_REPEAT_DELAY;
150 } else {
151 // Wait a very short time when a modifier event triggers the override to avoid false activations when e.g. a modifier is pressed just before a key is released (with the intention of pairing the modifier with a different key), or a modifier is lifted shortly before the trigger key is lifted. Operating systems by default reject modifier-events that happen very close to a non-modifier event.
152 defer_reference_time = timer_read32();
153 defer_delay = 50; // 50ms
154 }
155 deferred_register = keycode;
156}
157
158const key_override_t *clear_active_override(const bool allow_reregister) {
159 if (active_override == NULL) {
160 return NULL;
161 }
162
163 key_override_printf("Deactivating override\n");
164
165 deferred_register = 0;
166
167 // Clear the suppressed mods
168 clear_suppressed_override_mods();
169
170 // Unregister the replacement. First remove the weak override mods
171 clear_weak_override_mods();
172
173 const key_override_t *const old = active_override;
174
175 const uint8_t mod_free_replacement = clear_mods_from(active_override->replacement);
176
177 bool unregister_replacement = mod_free_replacement != KC_NO && // KC_NO is never registered
178 mod_free_replacement < SAFE_RANGE; // Custom keycodes are never registered
179
180 // Try firing the custom handler
181 if (active_override->custom_action != NULL) {
182 unregister_replacement &= active_override->custom_action(false, active_override->context);
183 }
184
185 // Then unregister the mod-free replacement key if desired
186 if (unregister_replacement) {
187 if (IS_KEY(mod_free_replacement)) {
188 del_key(mod_free_replacement);
189 } else {
190 key_override_printf("NOT KEY 1\n");
191 send_keyboard_report();
192 unregister_code(mod_free_replacement);
193 }
194 }
195
196 const uint16_t trigger = active_override->trigger;
197
198 const bool reregister_trigger = allow_reregister && // Check if allowed from caller
199 (active_override->options & ko_option_no_reregister_trigger) == 0 && // Check if override allows
200 active_override_trigger_is_down && // Check if trigger is even down
201 trigger != KC_NO && // KC_NO is never registered
202 trigger < SAFE_RANGE; // A custom keycode should not be registered
203
204 // Optionally re-register the trigger if it is still down
205 if (reregister_trigger) {
206 key_override_printf("Re-registering trigger deferred: %u\n", trigger);
207
208 // This will always be a modifier event, so defer always
209 schedule_deferred_register(trigger);
210 }
211
212 send_keyboard_report();
213
214 active_override = NULL;
215 active_override_trigger_is_down = false;
216
217 return old;
218}
219
220/** Checks if the key event is an allowed activation event for the provided override. Does not check things like whether the correct mods or correct trigger key is down. */
221static bool check_activation_event(const key_override_t *override, const bool key_down, const bool is_mod) {
222 ko_option_t options = override->options;
223
224 if ((options & ko_options_all_activations) == 0) {
225 // No activation option provided at all. This is wrong, but let's assume the default activations (ko_options_all_activations) were meant...
226 options = ko_options_all_activations;
227 }
228
229 if (is_mod) {
230 if (key_down) {
231 return (options & ko_option_activation_required_mod_down) != 0;
232 } else {
233 return (options & ko_option_activation_negative_mod_up) != 0;
234 }
235 } else {
236 if (key_down) {
237 return (options & ko_option_activation_trigger_down) != 0;
238 } else {
239 return false;
240 }
241 }
242}
243
244/** Iterates through the list of key overrides and tries activating each, until it finds one that activates or reaches the end of overrides. Returns true if the key action for `keycode` should be sent */
245static bool try_activating_override(const uint16_t keycode, const uint8_t layer, const bool key_down, const bool is_mod, const uint8_t active_mods, bool *activated) {
246 if (key_overrides == NULL) {
247 return true;
248 }
249
250 for (uint8_t i = 0;; i++) {
251 const key_override_t *const override = key_overrides[i];
252
253 // End of array
254 if (override == NULL) {
255 break;
256 }
257
258 // Fast, but not full mods check. Most key presses will not have any mods down, and most overrides will require mods. Hence here we filter overrides that require mods to be down while no mods are down
259 if (active_mods == 0 && override->trigger_mods != 0) {
260 key_override_printf("Not activating override: Modifiers don't match\n");
261 continue;
262 }
263
264 // Check layer
265 if ((override->layers & (1 << layer)) == 0) {
266 key_override_printf("Not activating override: Not set to activate on pressed layer\n");
267 continue;
268 }
269
270 // Check allowed activation events
271 if (!check_activation_event(override, key_down, is_mod)) {
272 key_override_printf("Not activating override: Activation event not allowed\n");
273 continue;
274 }
275
276 const bool is_trigger = override->trigger == keycode;
277
278 // Check if trigger lifted. This is a small optimization in order to skip the remaining checks
279 if (is_trigger && !key_down) {
280 key_override_printf("Not activating override: Trigger lifted\n");
281 continue;
282 }
283
284 // If the trigger is KC_NO it means 'no key', so only the required modifiers need to be down.
285 const bool no_trigger = override->trigger == KC_NO;
286
287 // Check if aleady active
288 if (override == active_override) {
289 key_override_printf("Not activating override: Alerady actived\n");
290 continue;
291 }
292
293 // Check if enabled
294 if (override->enabled != NULL && !((*(override->enabled) & 1))) {
295 key_override_printf("Not activating override: Not enabled\n");
296 continue;
297 }
298
299 // Check mods precisely
300 if (!key_override_matches_active_modifiers(override, active_mods)) {
301 key_override_printf("Not activating override: Modifiers don't match\n");
302 continue;
303 }
304
305 // Check if trigger key is down.
306 const bool trigger_down = is_trigger && key_down;
307
308 // At this point, all requirements for activation are checked, except whether the trigger key is pressed. Now we check if the required trigger is down
309 // If no trigger key is required, yes.
310 // If the trigger was just pressed, yes.
311 // If the last non-mod key that was pressed down is the trigger key, yes.
312 bool should_activate = no_trigger || trigger_down || last_key_down == override->trigger;
313
314 if (!should_activate) {
315 key_override_printf("Not activating override. Trigger not down\n");
316 continue;
317 }
318
319 key_override_printf("Activating override\n");
320
321 clear_active_override(false);
322
323 active_override = override;
324 active_override_trigger_is_down = true;
325
326 set_suppressed_override_mods(override->suppressed_mods);
327
328 if (!trigger_down && !no_trigger) {
329 // When activating a key override the trigger is is always unregistered. In the case where the key that newly pressed is not the trigger key, we have to explicitly remove the trigger key from the keyboard report. If the trigger was just pressed down we simply suppress the event which also has the effect of the trigger key not being registered in the keyboard report.
330 if (IS_KEY(override->trigger)) {
331 del_key(override->trigger);
332 } else {
333 unregister_code(override->trigger);
334 }
335 }
336
337 const uint16_t mod_free_replacement = clear_mods_from(override->replacement);
338
339 bool register_replacement = mod_free_replacement != KC_NO && // KC_NO is never registered
340 mod_free_replacement < SAFE_RANGE; // Custom keycodes are never registered
341
342 // Try firing the custom handler
343 if (override->custom_action != NULL) {
344 register_replacement &= override->custom_action(true, override->context);
345 }
346
347 if (register_replacement) {
348 const uint8_t override_mods = extract_mod_bits(override->replacement);
349 set_weak_override_mods(override_mods);
350
351 // If this is a modifier event that activates the key override we _always_ defer the actual full activation of the override
352 if (is_mod) {
353 key_override_printf("Deferring register replacement key\n");
354 schedule_deferred_register(mod_free_replacement);
355 send_keyboard_report();
356 } else {
357 if (IS_KEY(mod_free_replacement)) {
358 add_key(mod_free_replacement);
359 } else {
360 key_override_printf("NOT KEY 2\n");
361 send_keyboard_report();
362 // On macOS there seems to be a race condition when it comes to the keyboard report and consumer keycodes. It seems the OS may recognize a consumer keycode before an updated keyboard report, even if the keyboard report is actually sent before the consumer key. I assume it is some sort of race condition because it happens infrequently and very irregularly. Waiting for about at least 10ms between sending the keyboard report and sending the consumer code has shown to fix this.
363 wait_ms(10);
364 register_code(mod_free_replacement);
365 }
366 }
367 } else {
368 // If not registering the replacement key send keyboard report to update the unregistered keys.
369 send_keyboard_report();
370 }
371
372 *activated = true;
373
374 // If the trigger is down, suppress the event so that it does not get added to the keyboard report.
375 return !trigger_down;
376 }
377
378 *activated = false;
379
380 return true;
381}
382
383void matrix_scan_key_override(void) {
384 if (deferred_register == 0) {
385 return;
386 }
387
388 if (timer_elapsed32(defer_reference_time) >= defer_delay) {
389 key_override_printf("Registering deferred key\n");
390 register_code16(deferred_register);
391 deferred_register = 0;
392 defer_reference_time = 0;
393 defer_delay = 0;
394 }
395}
396
397bool process_key_override(const uint16_t keycode, const keyrecord_t *const record) {
398#ifdef BENCH_KEY_OVERRIDE
399 uint16_t start = timer_read();
400#endif
401
402 const bool key_down = record->event.pressed;
403 const bool is_mod = IS_MOD(keycode);
404
405 if (key_down) {
406 switch (keycode) {
407 case KEY_OVERRIDE_TOGGLE:
408 key_override_toggle();
409 return false;
410
411 case KEY_OVERRIDE_ON:
412 key_override_on();
413 return false;
414
415 case KEY_OVERRIDE_OFF:
416 key_override_off();
417 return false;
418
419 default:
420 break;
421 }
422 }
423
424 if (!enabled) {
425 return true;
426 }
427
428 uint8_t effective_mods = get_mods();
429
430#ifdef KEY_OVERRIDE_INCLUDE_WEAK_MODS
431 effective_mods |= get_weak_mods();
432#endif
433
434#ifndef NO_ACTION_ONESHOT
435 // Locked one shot mods are added to get_mods(), I think (why??) while oneshot mods are in get_oneshot_mods(). Still OR with get_locked_oneshot_mods because that's where those mods _should_ be saved.
436 effective_mods |= get_oneshot_locked_mods() | get_oneshot_mods();
437#endif
438
439 if (is_mod) {
440 // The mods returned from get_mods() will be updated with this new event _after_ this code runs. Hence we manually update the effective mods here to really know the effective mods.
441 if (key_down) {
442 effective_mods |= MOD_BIT(keycode);
443 } else {
444 effective_mods &= ~MOD_BIT(keycode);
445 }
446 } else {
447 if (key_down) {
448 last_key_down = keycode;
449 last_key_down_time = timer_read32();
450 deferred_register = 0;
451 }
452
453 // The last key that was pressed was just released. No more keys are therefore sending input
454 if (!key_down && keycode == last_key_down) {
455 last_key_down = 0;
456 last_key_down_time = 0;
457 // We also cancel any deferred registers because, again, no keys are sending any input. Only the last key that is pressed creates an input – this key was just lifted.
458 deferred_register = 0;
459 }
460 }
461
462 key_override_printf("key down: %u keycode: %u is mod: %u effective mods: %u\n", key_down, keycode, is_mod, effective_mods);
463
464 bool send_key_action = true;
465 bool activated = false;
466
467 // Non-mod key up events never activate a key override
468 if (is_mod || key_down) {
469 // Get the exact layer that was hit. It will be cached at this point
470 const uint8_t layer = read_source_layers_cache(record->event.key);
471
472 // Use blocked to ensure the same override is not activated again immediately after it is deactivated
473 send_key_action = try_activating_override(keycode, layer, key_down, is_mod, effective_mods, &activated);
474
475 if (!send_key_action) {
476 send_keyboard_report();
477 }
478 }
479
480 if (!activated && active_override != NULL) {
481 if (is_mod) {
482 // Check if necessary modifier of current override goes up or a negative mod goes down
483 if (!key_override_matches_active_modifiers(active_override, effective_mods)) {
484 key_override_printf("Deactivating override because necessary modifier lifted or negative mod pressed\n");
485 clear_active_override(true);
486 }
487 } else {
488 // Check if trigger of current override goes up or if override does not allow additional keys to be down and another key goes down
489 const bool is_trigger = keycode == active_override->trigger;
490 bool should_deactivate = false;
491
492 // Check if trigger key lifted
493 if (is_trigger && !key_down) {
494 should_deactivate = true;
495 active_override_trigger_is_down = false;
496 key_override_printf("Deactivating override because trigger key up\n");
497 }
498
499 // Check if another key was pressed
500 if (key_down && (active_override->options & ko_option_no_unregister_on_other_key_down) == 0) {
501 should_deactivate = true;
502 key_override_printf("Deactivating override because another key was pressed\n");
503 }
504
505 if (should_deactivate) {
506 clear_active_override(false);
507 }
508 }
509 }
510
511#ifdef BENCH_KEY_OVERRIDE
512 uint16_t elapsed = timer_elapsed(start);
513
514 dprintf("Processing key overrides took: %u ms\n", elapsed);
515#endif
516
517 return send_key_action;
518}
diff --git a/quantum/process_keycode/process_key_override.h b/quantum/process_keycode/process_key_override.h
new file mode 100644
index 000000000..9ba59e4e9
--- /dev/null
+++ b/quantum/process_keycode/process_key_override.h
@@ -0,0 +1,147 @@
1/*
2 * Copyright 2021 Jonas Gessner
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <stdbool.h>
21#include <stddef.h>
22#include <stdint.h>
23
24#include "action_layer.h"
25
26/**
27 * Key overrides allow you to send a different key-modifier combination or perform a custom action when a certain modifier-key combination is pressed.
28 *
29 * For example, you may configure a key override to send the delete key when shift + backspace are pressed together, or that your volume keys become screen brightness keys when holding ctrl. The possibilities are quite vast and the documentation contains a few examples for inspiration.
30 *
31 * See the documentation and examples here: https://docs.qmk.fm/#/feature_key_overrides
32 */
33
34/** Bitfield with various options controlling the behavior of a key override. */
35typedef enum {
36 /** Allow activating when the trigger key is pressed down. */
37 ko_option_activation_trigger_down = (1 << 0),
38 /** Allow activating when a necessary modifier is pressed down. */
39 ko_option_activation_required_mod_down = (1 << 1),
40 /** Allow activating when a negative modifier is released. */
41 ko_option_activation_negative_mod_up = (1 << 2),
42
43 ko_options_all_activations = ko_option_activation_negative_mod_up | ko_option_activation_required_mod_down | ko_option_activation_trigger_down,
44
45 /** If set, any of the modifiers in trigger_mods will be enough to activate the override (logical OR of modifiers). If not set, all the modifiers in trigger_mods have to be pressed (logical AND of modifiers). */
46 ko_option_one_mod = (1 << 3),
47
48 /** If set, the trigger key will never be registered again after the override is deactivated. */
49 ko_option_no_reregister_trigger = (1 << 4),
50
51 /** If set, the override will not deactivate when another key is pressed down. Use only if you really know you need this. */
52 ko_option_no_unregister_on_other_key_down = (1 << 5),
53
54 /** The default options used by the ko_make_xxx functions. */
55 ko_options_default = ko_options_all_activations,
56} ko_option_t;
57
58/** Defines a single key override */
59typedef struct {
60 // The non-modifier keycode that triggers the override. This keycode, and the necessary modifiers (trigger_mods) must be pressed to activate this override. Set this to the keycode of the key that should activate the override. Set to KC_NO to require only the necessary modifiers to be pressed and no non-modifier.
61 uint16_t trigger;
62
63 // Which mods need to be down for activation. If both sides of a modifier are set (e.g. left ctrl and right ctrl) then only one is required to be pressed (e.g. left ctrl suffices). Use the MOD_MASK_XXX and MOD_BIT() macros for this.
64 uint8_t trigger_mods;
65
66 // This is a BITMASK (!), defining which layers this override applies to. To use this override on layer i set the ith bit (1 << i).
67 layer_state_t layers;
68
69 // Which modifiers cannot be down. It must hold that (active_mods & negative_mod_mask) == 0, otherwise the key override will not be activated. An active override will be deactivated once this is no longer true.
70 uint8_t negative_mod_mask;
71
72 // Modifiers to 'suppress' while the override is active. To suppress a modifier means that even though the modifier key is held down, the host OS sees the modifier as not pressed. Can be used to suppress the trigger modifiers, as a trivial example.
73 uint8_t suppressed_mods;
74
75 // The complex keycode to send as replacement when this override is triggered. This can be a simple keycode, a key-modifier combination (e.g. C(KC_A)), or KC_NO (to register no replacement keycode). Use in combination with suppressed_mods to get the correct modifiers to be sent.
76 uint16_t replacement;
77
78 // Options controlling the behavior of the override, such as what actions are allowed to activate the override.
79 ko_option_t options;
80
81 // If not NULL, this function will be called right before the replacement key is registered, along with the provided context and a flag indicating whether the override was activated or deactivated. This function allows you to run some custom actions for specific key overrides. If you return `false`, the replacement key is not registered/unregistered as it would normally. Return `true` to register and unregister the override normally.
82 bool (*custom_action)(bool activated, void *context);
83
84 // A context that will be passed to the custom action function.
85 void *context;
86
87 // If this points to false this override will not be used. Set to NULL to always have this override enabled.
88 bool *enabled;
89} key_override_t;
90
91/** Define this as a null-terminated array of pointers to key overrides. These key overrides will be used by qmk. */
92extern const key_override_t **key_overrides;
93
94/** Turns key overrides on */
95extern void key_override_on(void);
96
97/** Turns key overrides off */
98extern void key_override_off(void);
99
100/** Toggles key overrides on */
101extern void key_override_toggle(void);
102
103/** Returns whether key overrides are enabled */
104extern bool key_override_is_enabled(void);
105
106/**
107 * Preferrably use these macros to create key overrides. They fix many of the options to a standard setting that should satisfy most basic use-cases. Only directly create a key_override_t struct when you really need to.
108 */
109
110// clang-format off
111
112/**
113 * Convenience initializer to create a basic key override. Activates the override on all layers.
114 */
115#define ko_make_basic(trigger_mods, trigger_key, replacement_key) \
116 ko_make_with_layers(trigger_mods, trigger_key, replacement_key, ~0)
117
118/**
119 * Convenience initializer to create a basic key override. Provide a bitmap (of type layer_state_t) with the bits set for each layer on which the override should activate.
120 */
121#define ko_make_with_layers(trigger_mods, trigger_key, replacement_key, layers) \
122 ko_make_with_layers_and_negmods(trigger_mods, trigger_key, replacement_key, layers, 0)
123
124/**
125 * Convenience initializer to create a basic key override. Provide a bitmap with the bits set for each layer on which the override should activate. Also provide a negative modifier mask, that is used to define which modifiers may not be pressed.
126 */
127#define ko_make_with_layers_and_negmods(trigger_mods, trigger_key, replacement_key, layers, negative_mask) \
128 ko_make_with_layers_negmods_and_options(trigger_mods, trigger_key, replacement_key, layers, negative_mask, ko_options_default)
129
130 /**
131 * Convenience initializer to create a basic key override. Provide a bitmap with the bits set for each layer on which the override should activate. Also provide a negative modifier mask, that is used to define which modifiers may not be pressed. Provide options for additional control of the behavior of the override.
132 */
133#define ko_make_with_layers_negmods_and_options(trigger_mods_, trigger_key, replacement_key, layer_mask, negative_mask, options_) \
134 ((const key_override_t){ \
135 .trigger_mods = (trigger_mods_), \
136 .layers = (layer_mask), \
137 .suppressed_mods = (trigger_mods_), \
138 .options = (options_), \
139 .negative_mod_mask = (negative_mask), \
140 .custom_action = NULL, \
141 .context = NULL, \
142 .trigger = (trigger_key), \
143 .replacement = (replacement_key), \
144 .enabled = NULL \
145 })
146
147// clang-format on
diff --git a/quantum/process_keycode/process_key_override_private.h b/quantum/process_keycode/process_key_override_private.h
new file mode 100644
index 000000000..1d0e134a1
--- /dev/null
+++ b/quantum/process_keycode/process_key_override_private.h
@@ -0,0 +1,24 @@
1/*
2 * Copyright 2021 Jonas Gessner
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <stdint.h>
21#include "action.h"
22
23bool process_key_override(const uint16_t keycode, const keyrecord_t *const record);
24void matrix_scan_key_override(void);