Process combos earlier & overlapping combos (#8591)

* Combo processing improvements.

Now it is possible to use ModTap and LayerTap keys as part of combos.
Overlapping combos also don't trigger all the combos, just exactly the
one that you press.

New settings:
- COMBO_MUST_HOLD_MODS
- COMBO_MOD_TERM
- COMBO_TERM_PER_COMBO
- COMBO_MUST_HOLD_PER_COMBO
- COMBO_STRICT_TIMER
- COMBO_NO_TIMER

* Remove the size flags from combo_t struct boolean members.

This in the end actually saves space as the members are accessed so many
times. The amount of operations needed to access the bits uses more
memory than setting the size saves.

* Fix `process_combo_key_release` not called correctly with tap-only combos

* Fix not passing a pointer when NO_ACTION_TAPPING is defined.

* Docs for `COMBO_ONLY_FROM_LAYER`

* Update docs/feature_combo.md

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>

* Update quantum/process_keycode/process_combo.c

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>

* Add `EXTRA_SHORT_COMBOS` option.

Stuff combo's `disabled` and `active` flags into `state`. Possibly can
save some space.

* Add more examples and clarify things with dict management system.

- Simple examples now has a combo that has modifiers included.
- The slightly more advanced examples now are actually more advanced
  instead of just `tap_code16(<modded-keycode>)`.
- Added a note that `COMBO_ACTION`s are not needed anymore as you can
  just use custom keycodes.
- Added a note that the `g/keymap_combo.h` macros use the
  `process_combo_event` function and that it is not usable in one's
  keymap afterwards.

* Update docs/feature_combo.md

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>

* Update docs/feature_combo.md

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>

* Update docs/feature_combo.md

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>

* Update docs/feature_combo.md

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>

* Update docs/feature_combo.md

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>

* Change "the" combo action example to "email" example.

* Update docs/feature_combo.md

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>

* Fix sneaky infinite loop with `combo_disable()`

No need to call `dump_key_buffer` when disabling combos because the
buffer is either being dumped if a combo-key was pressed, or the buffer is empty
if a non-combo-key is pressed.

* Update docs/feature_combo.md

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>

* Update docs/feature_combo.md

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>

Co-authored-by: precondition <57645186+precondition@users.noreply.github.com>
Co-authored-by: Drashna Jaelre <drashna@live.com>
This commit is contained in:
Pete Sevander 2021-08-06 02:44:57 +03:00 committed by GitHub
parent 07553b41f0
commit 7e983796e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 845 additions and 169 deletions

View file

@ -188,9 +188,21 @@ If you define these options you will enable the associated feature, which may in
few ms of delay from this. But if you're doing chording on something with 3-4ms few ms of delay from this. But if you're doing chording on something with 3-4ms
scan times? You probably want this. scan times? You probably want this.
* `#define COMBO_COUNT 2` * `#define COMBO_COUNT 2`
* Set this to the number of combos that you're using in the [Combo](feature_combo.md) feature. * Set this to the number of combos that you're using in the [Combo](feature_combo.md) feature. Or leave it undefined and programmatically set the count.
* `#define COMBO_TERM 200` * `#define COMBO_TERM 200`
* how long for the Combo keys to be detected. Defaults to `TAPPING_TERM` if not defined. * how long for the Combo keys to be detected. Defaults to `TAPPING_TERM` if not defined.
* `#define COMBO_MUST_HOLD_MODS`
* Flag for enabling extending timeout on Combos containing modifers
* `#define COMBO_MOD_TERM 200`
* Allows for extending COMBO_TERM for mod keys while mid-combo.
* `#define COMBO_MUST_HOLD_PER_COMBO`
* Flag to enable per-combo COMBO_TERM extension and `get_combo_must_hold()` function
* `#define COMBO_TERM_PER_COMBO`
* Flag to enable per-combo COMBO_TERM extension and `get_combo_term()` function
* `#define COMBO_STRICT_TIMER`
* Only start the combo timer on the first key press instead of on all key presses.
* `#define COMBO_NO_TIMER`
* Disable the combo timer completely for relaxed combos.
* `#define TAP_CODE_DELAY 100` * `#define TAP_CODE_DELAY 100`
* Sets the delay between `register_code` and `unregister_code`, if you're having issues with it registering properly (common on VUSB boards). The value is in milliseconds. * Sets the delay between `register_code` and `unregister_code`, if you're having issues with it registering properly (common on VUSB boards). The value is in milliseconds.
* `#define TAP_HOLD_CAPS_DELAY 80` * `#define TAP_HOLD_CAPS_DELAY 80`

View file

@ -1,24 +1,39 @@
# Combos # Combos
The Combo feature is a chording type solution for adding custom actions. It lets you hit multiple keys at once and produce a different effect. For instance, hitting `A` and `S` within the tapping term would hit `ESC` instead, or have it perform even more complex tasks. The Combo feature is a chording type solution for adding custom actions. It lets you hit multiple keys at once and produce a different effect. For instance, hitting `A` and `S` within the combo term would hit `ESC` instead, or have it perform even more complex tasks.
To enable this feature, you need to add `COMBO_ENABLE = yes` to your `rules.mk`. To enable this feature, you need to add `COMBO_ENABLE = yes` to your `rules.mk`.
Additionally, in your `config.h`, you'll need to specify the number of combos that you'll be using, by adding `#define COMBO_COUNT 1` (replacing 1 with the number that you're using). Additionally, in your `config.h`, you'll need to specify the number of combos that you'll be using, by adding `#define COMBO_COUNT 1` (replacing 1 with the number that you're using). It is also possible to not define this and instead set the variable `COMBO_LEN` yourself. There's a trick where we don't need to think about this variable at all. More on this later.
<!-- At this time, this is necessary -->
Also, by default, the tapping term for the Combos is set to the same value as `TAPPING_TERM` (200 by default on most boards). But you can specify a different value by defining it in your `config.h`. For instance: `#define COMBO_TERM 300` would set the time out period for combos to 300ms.
Then, your `keymap.c` file, you'll need to define a sequence of keys, terminated with `COMBO_END`, and a structure to list the combination of keys, and it's resulting action. Then, in your `keymap.c` file, you'll need to define a sequence of keys, terminated with `COMBO_END`, and a structure to list the combination of keys, and its resulting action.
```c ```c
const uint16_t PROGMEM test_combo[] = {KC_A, KC_B, COMBO_END}; const uint16_t PROGMEM test_combo1[] = {KC_A, KC_B, COMBO_END};
combo_t key_combos[COMBO_COUNT] = {COMBO(test_combo, KC_ESC)}; const uint16_t PROGMEM test_combo2[] = {KC_C, KC_D, COMBO_END};
combo_t key_combos[COMBO_COUNT] = {
COMBO(test_combo1, KC_ESC),
COMBO(test_combo2, LCTL(KC_Z)), // keycodes with modifiers are possible too!
};
``` ```
This will send "Escape" if you hit the A and B keys. This will send "Escape" if you hit the A and B keys, and Ctrl+Z when you hit the C and D keys.
!> This method only supports [basic keycodes](keycodes_basic.md). See the examples for more control. As of [PR#8591](https://github.com/qmk/qmk_firmware/pull/8591/), it is possible to fire combos from ModTap keys and LayerTap keys. So in the above example you could have keys `LSFT_T(KC_A)` and `LT(_LAYER, KC_B)` and it would work. So Home Row Mods and Home Row Combos at same time is now a thing!
It is also now possible to overlap combos. Before, with the example below both combos would activate when all three keys were pressed. Now only the three key combo will activate.
```c
const uint16_t PROGMEM test_combo1[] = {LSFT_T(KC_A), LT(_LAYER, KC_B), COMBO_END};
const uint16_t PROGMEM test_combo2[] = {LSFT_T(KC_A), LT(_LAYER, KC_B), KC_C, COMBO_END};
combo_t key_combos[COMBO_COUNT] = {
COMBO(test_combo1, KC_ESC)
COMBO(test_combo2, KC_TAB)
};
```
Executing more complex keycodes like ModTaps and LayerTaps is now also possible.
## Examples ## Examples
@ -27,63 +42,68 @@ If you want to add a list, then you'd use something like this:
```c ```c
enum combos { enum combos {
AB_ESC, AB_ESC,
JK_TAB JK_TAB,
QW_SFT,
SD_LAYER,
}; };
const uint16_t PROGMEM ab_combo[] = {KC_A, KC_B, COMBO_END}; const uint16_t PROGMEM ab_combo[] = {KC_A, KC_B, COMBO_END};
const uint16_t PROGMEM jk_combo[] = {KC_J, KC_K, COMBO_END}; const uint16_t PROGMEM jk_combo[] = {KC_J, KC_K, COMBO_END};
const uint16_t PROGMEM qw_combo[] = {KC_Q, KC_W, COMBO_END};
const uint16_t PROGMEM sd_combo[] = {KC_S, KC_D, COMBO_END};
combo_t key_combos[COMBO_COUNT] = { combo_t key_combos[COMBO_COUNT] = {
[AB_ESC] = COMBO(ab_combo, KC_ESC), [AB_ESC] = COMBO(ab_combo, KC_ESC),
[JK_TAB] = COMBO(jk_combo, KC_TAB) [JK_TAB] = COMBO(jk_combo, KC_TAB),
[QW_SFT] = COMBO(qw_combo, KC_LSFT)
[SD_LAYER] = COMBO(layer_combo, MO(_LAYER)),
}; };
``` ```
For a more complicated implementation, you can use the `process_combo_event` function to add custom handling. For a more complicated implementation, you can use the `process_combo_event` function to add custom handling.
Additionally, this example shows how you can leave `COMBO_COUNT` undefined.
```c ```c
enum combo_events { enum combo_events {
ZC_COPY, EM_EMAIL,
XV_PASTE BSPC_LSFT_CLEAR,
COMBO_LENGTH
}; };
uint16_t COMBO_LEN = COMBO_LENGTH; // remove the COMBO_COUNT define and use this instead!
const uint16_t PROGMEM copy_combo[] = {KC_Z, KC_C, COMBO_END}; const uint16_t PROGMEM email_combo[] = {KC_E, KC_M, COMBO_END};
const uint16_t PROGMEM paste_combo[] = {KC_X, KC_V, COMBO_END}; const uint16_t PROGMEM clear_line_combo[] = {KC_BSPC, KC_LSFT, COMBO_END};
combo_t key_combos[COMBO_COUNT] = { combo_t key_combos[] = {
[ZC_COPY] = COMBO_ACTION(copy_combo), [EM_EMAIL] = COMBO_ACTION(email_combo),
[XV_PASTE] = COMBO_ACTION(paste_combo), [BSPC_LSFT_CLEAR] = COMBO_ACTION(clear_line_combo),
}; };
/* COMBO_ACTION(x) is same as COMBO(x, KC_NO) */
void process_combo_event(uint16_t combo_index, bool pressed) { void process_combo_event(uint16_t combo_index, bool pressed) {
switch(combo_index) { switch(combo_index) {
case ZC_COPY: case EM_EMAIL:
if (pressed) { if (pressed) {
tap_code16(LCTL(KC_C)); SEND_STRING("john.doe@example.com");
} }
break; break;
case XV_PASTE: case BSPC_LSFT_CLEAR:
if (pressed) { if (pressed) {
tap_code16(LCTL(KC_V)); tap_code16(KC_END);
tap_code16(S(KC_HOME));
tap_code16(KC_BSPC);
} }
break; break;
} }
} }
``` ```
This will send Ctrl+C if you hit Z and C, and Ctrl+V if you hit X and V. But you could change this to do stuff like change layers, play sounds, or change settings. This will send "john.doe@example.com" if you chord E and M together, and clear the current line with Backspace and Left-Shift. You could change this to do stuff like play sounds or change settings.
## Additional Configuration It is worth noting that `COMBO_ACTION`s are not needed anymore. As of [PR#8591](https://github.com/qmk/qmk_firmware/pull/8591/), it is possible to run your own custom keycodes from combos. Just define the custom keycode, program its functionality in `process_record_user`, and define a combo with `COMBO(<key_array>, <your_custom_keycode>)`.
If you're using long combos, or even longer combos, you may run into issues with this, as the structure may not be large enough to accommodate what you're doing.
In this case, you can add either `#define EXTRA_LONG_COMBOS` or `#define EXTRA_EXTRA_LONG_COMBOS` in your `config.h` file.
You may also be able to enable action keys by defining `COMBO_ALLOW_ACTION_KEYS`.
## Keycodes ## Keycodes
You can enable, disable and toggle the Combo feature on the fly. This is useful if you need to disable them temporarily, such as for a game. The following keycodes are available for use in your `keymap.c`
You can enable, disable and toggle the Combo feature on the fly. This is useful if you need to disable them temporarily, such as for a game.
|Keycode |Description | |Keycode |Description |
|----------|---------------------------------| |----------|---------------------------------|
@ -91,6 +111,187 @@ You can enable, disable and toggle the Combo feature on the fly. This is useful
|`CMB_OFF` |Turns off Combo feature | |`CMB_OFF` |Turns off Combo feature |
|`CMB_TOG` |Toggles Combo feature on and off | |`CMB_TOG` |Toggles Combo feature on and off |
# Advanced Configuration
These configuration settings can be set in your `config.h` file.
## Combo Term
By default, the timeout for the Combos to be recognized is set to 50ms. This can be changed if accidental combo misfires are happening or if you're having difficulties pressing keys at the same time. For instance, `#define COMBO_TERM 40` would set the timeout period for combos to 40ms.
## Buffer and state sizes
If you're using long combos, or you have a lot of overlapping combos, you may run into issues with this, as the buffers may not be large enough to accommodate what you're doing. In this case, you can configure the sizes of the buffers used. Be aware, larger combo sizes and larger buffers will increase memory usage!
To configure the amount of keys a combo can be composed of, change the following:
| Keys | Define to be set |
|------|-----------------------------------|
| 6 | `#define EXTRA_SHORT_COMBOS` |
| 8 | QMK Default |
| 16 | `#define EXTRA_LONG_COMBOS` |
| 32 | `#define EXTRA_EXTRA_LONG_COMBOS` |
Defining `EXTRA_SHORT_COMBOS` combines a combo's internal state into just one byte. This can, in some cases, save some memory. If it doesn't, no point using it. If you do, you also have to make sure you don't define combos with more than 6 keys.
Processing combos has two buffers, one for the key presses, another for the combos being activated. Use the following options to configure the sizes of these buffers:
| Define | Default |
|-------------------------------------|------------------------------------------------------|
| `#define COMBO_KEY_BUFFER_LENGTH 8` | 8 (the key amount `(EXTRA_)EXTRA_LONG_COMBOS` gives) |
| `#define COMBO_BUFFER_LENGTH 4` | 4 |
## Modifier Combos
If a combo resolves to a Modifier, the window for processing the combo can be extended independently from normal combos. By default, this is disabled but can be enabled with `#define COMBO_MUST_HOLD_MODS`, and the time window can be configured with `#define COMBO_HOLD_TERM 150` (default: `TAPPING_TERM`). With `COMBO_MUST_HOLD_MODS`, you cannot tap the combo any more which makes the combo less prone to misfires.
## Per Combo Timing, Holding and Tapping
For each combo, it is possible to configure the time window it has to pressed in, if it needs to be held down, or if it needs to be tapped.
For example, tap-only combos are useful if any (or all) of the underlying keys is a Mod-Tap or a Layer-Tap key. When you tap the combo, you get the combo result. When you press the combo and hold it down, the combo doesn't actually activate. Instead the keys are processed separately as if the combo wasn't even there.
In order to use these features, the following configuration options and functions need to be defined. Coming up with useful timings and configuration is left as an exercise for the reader.
| Config Flag | Function | Description |
|-----------------------------|-----------------------------------------------------------|--------------------------------------------------------------------------------------------------------|
| `COMBO_TERM_PER_COMBO` | uint16_t get_combo_term(uint16_t index, combo_t \*combo) | Optional per-combo timeout window. (default: `COMBO_TERM`) |
| `COMBO_MUST_HOLD_PER_COMBO` | bool get_combo_must_hold(uint16_t index, combo_t \*combo) | Controls if a given combo should fire immediately on tap or if it needs to be held. (default: `false`) |
| `COMBO_MUST_TAP_PER_COMBO` | bool get_combo_must_tap(uint16_t index, combo_t \*combo) | Controls if a given combo should fire only if tapped within `COMBO_HOLD_TERM`. (default: `false`) |
Examples:
```c
uint16_t get_combo_term(uint16_t index, combo_t *combo) {
// decide by combo->keycode
switch (combo->keycode) {
case KC_X:
return 50;
}
// or with combo index, i.e. its name from enum.
switch (index) {
case COMBO_NAME_HERE:
return 9001;
}
// And if you're feeling adventurous, you can even decide by the keys in the chord,
// i.e. the exact array of keys you defined for the combo.
// This can be useful if your combos have a common key and you want to apply the
// same combo term for all of them.
if (combo->keys[0] == KC_ENTER) { // if first key in the array is KC_ENTER
return 150;
}
return COMBO_TERM;
}
bool get_combo_must_hold(uint16_t index, combo_t *combo) {
// Same as above, decide by keycode, the combo index, or by the keys in the chord.
if (KEYCODE_IS_MOD(combo->keycode) ||
(combo->keycode >= QK_MOMENTARY && combo->keycode <= QK_MOMENTARY_MAX) // MO(kc) keycodes
) {
return true;
}
switch (index) {
case COMBO_NAME_HERE:
return true;
}
return false;
}
bool get_combo_must_tap(uint16_t index, combo_t *combo) {
// If you want all combos to be tap-only, just uncomment the next line
// return true
// If you want *all* combos, that have Mod-Tap/Layer-Tap/Momentary keys in its chord, to be tap-only, this is for you:
uint16_t key;
uint8_t idx = 0;
while ((key = pgm_read_word(&combo->keys[idx])) != COMBO_END) {
switch (key) {
case QK_MOD_TAP...QK_MOD_TAP_MAX:
case QK_LAYER_TAP...QK_LAYER_TAP_MAX:
case QK_MOMENTARY...QK_MOMENTARY_MAX:
return true;
}
idx += 1;
}
return false;
}
```
## Variable Length Combos
If you leave `COMBO_COUNT` undefined in `config.h`, it allows you to programmatically declare the size of the Combo data structure and avoid updating `COMBO_COUNT`. Instead a variable called `COMBO_LEN` has to be set. It can be set with something similar to the following in `keymap.c`: `uint16_t COMBO_LEN = sizeof(key_combos) / sizeof(key_combos[0]);` or by adding `COMBO_LENGTH` as the *last* entry in the combo enum and then `uint16_t COMBO_LEN = COMBO_LENGTH;` as such:
```c
enum myCombos {
...,
COMBO_LENGTH
};
uint16_t COMBO_LEN = COMBO_LENGTH;
```
Regardless of the method used to declare `COMBO_LEN`, this also requires to convert the `combo_t key_combos[COMBO_COUNT] = {...};` line to `combo_t key_combos[] = {...};`.
## Combo timer
Normally, the timer is started on the first key press and then reset on every subsequent key press within the `COMBO_TERM`.
Inputting combos is relaxed like this, but also slightly more prone to accidental misfires.
The next two options alter the behaviour of the timer.
### `#define COMBO_STRICT_TIMER`
With `COMBO_STRICT_TIMER`, the timer is started only on the first key press.
Inputting combos is now less relaxed; you need to make sure the full chord is pressed within the `COMBO_TERM`.
Misfires are less common but if you type multiple combos fast, there is a
chance that the latter ones might not activate properly.
### `#define COMBO_NO_TIMER`
By defining `COMBO_NO_TIMER`, the timer is disabled completely and combos are activated on the first key release.
This also disables the "must hold" functionalities as they just wouldn't work at all.
## Customizable key releases
By defining `COMBO_PROCESS_KEY_RELEASE` and implementing the function `bool process_combo_key_release(uint16_t combo_index, combo_t *combo, uint8_t key_index, uint16_t keycode)`, you can run your custom code on each key release after a combo was activated. For example you could change the RGB colors, activate haptics, or alter the modifiers.
You can also release a combo early by returning `true` from the function.
Here's an example where a combo resolves to two modifiers, and on key releases the modifiers are unregistered one by one, depending on which key was released.
```c
enum combos {
AB_MODS,
COMBO_LENGTH
};
uint16_t COMBO_LEN = COMBO_LENGTH;
const uint16_t PROGMEM ab_combo[] = {KC_A, KC_B, COMBO_END};
combo_t key_combos[] = {
[AB_MODS] = COMBO(ab_combo, LCTL(KC_LSFT)),
};
bool process_combo_key_release(uint16_t combo_index, combo_t *combo, uint8_t key_index, uint16_t keycode) {
switch (combo_index) {
case AB_MODS:
switch(keycode) {
case KC_A:
unregister_mods(MOD_MASK_CTRL);
break;
case KC_B:
unregister_mods(MOD_MASK_SHIFT);
break;
}
return false; // do not release combo
}
return false;
}
```
## Layer independent combos
If you, for example, use multiple base layers for different key layouts, one for QWERTY, and another one for Colemak, you might want your combos to work from the same key positions on all layers. Defining the same combos again for another layout is redundant and takes more memory. The solution is to just check the keycodes from one layer.
With `#define COMBO_ONLY_FROM_LAYER _LAYER_A` the combos' keys are always checked from layer `_LAYER_A` even though the active layer would be `_LAYER_B`.
## User callbacks ## User callbacks
In addition to the keycodes, there are a few functions that you can use to set the status, or check it: In addition to the keycodes, there are a few functions that you can use to set the status, or check it:
@ -101,3 +302,28 @@ In addition to the keycodes, there are a few functions that you can use to set t
| `combo_disable()` | Disables the combo feature, and clears the combo buffer | | `combo_disable()` | Disables the combo feature, and clears the combo buffer |
| `combo_toggle()` | Toggles the state of the combo feature | | `combo_toggle()` | Toggles the state of the combo feature |
| `is_combo_enabled()` | Returns the status of the combo feature state (true or false) | | `is_combo_enabled()` | Returns the status of the combo feature state (true or false) |
# Dictionary Management
Having 3 places to update when adding new combos or altering old ones does become cumbersome when you have a lot of combos. We can alleviate this with some magic! ... If you consider C macros magic.
First, you need to add `VPATH += keyboards/gboards` to your `rules.mk`. Next, include the file `g/keymap_combo.h` in your `keymap.c`.
!> This functionality uses the same `process_combo_event` function as `COMBO_ACTION` macros do, so you cannot use the function yourself in your keymap. Instead, you have to define the `case`s of the `switch` statement by themselves within `inject.h`, which `g/keymap_combo.h` will then include into the function.
Then, write your combos in `combos.def` file in the following manner:
```c
// name result chord keys
COMB(AB_ESC, KC_ESC, KC_A, KC_B)
COMB(JK_TAB, KC_TAB, KC_J, KC_K)
COMB(JKL_SPC, KC_SPC, KC_J, KC_K, KC_L)
COMB(BSSL_CLR, KC_NO, KC_BSPC, KC_LSFT) // using KC_NO as the resulting keycode is the same as COMBO_ACTION before.
COMB(QW_UNDO, C(KC_Z), KC_Q, KC_W)
SUBS(TH_THE, "the", KC_T, KC_H) // SUBS uses SEND_STRING to output the given string.
...
```
Now, you can update only one place to add or alter combos. You don't even need to remember to update the `COMBO_COUNT` or the `COMBO_LEN` variables at all. Everything is taken care of. Magic!
For small to huge ready made dictionaries of combos, you can check out http://combos.gboards.ca/.

View file

@ -28,7 +28,10 @@
#define TOGG A_ENUM #define TOGG A_ENUM
enum combos { enum combos {
#include "combos.def" #include "combos.def"
COMBO_LENGTH
}; };
// Export length to combo module
uint16_t COMBO_LEN = COMBO_LENGTH;
// Bake combos into mem // Bake combos into mem
#undef COMB #undef COMB
@ -53,9 +56,6 @@ combo_t key_combos[] = {
#undef SUBS #undef SUBS
#undef TOGG #undef TOGG
// Export length to combo module
int COMBO_LEN = sizeof(key_combos) / sizeof(key_combos[0]);
// Fill QMK hook // Fill QMK hook
#define COMB BLANK #define COMB BLANK
#define SUBS A_ACTI #define SUBS A_ACTI

View file

@ -40,7 +40,10 @@ extern keymap_config_t keymap_config;
action_t action_for_key(uint8_t layer, keypos_t key) { action_t action_for_key(uint8_t layer, keypos_t key) {
// 16bit keycodes - important // 16bit keycodes - important
uint16_t keycode = keymap_key_to_keycode(layer, key); uint16_t keycode = keymap_key_to_keycode(layer, key);
return action_for_keycode(keycode);
};
action_t action_for_keycode(uint16_t keycode) {
// keycode remapping // keycode remapping
keycode = keycode_config(keycode); keycode = keycode_config(keycode);

View file

@ -16,114 +16,445 @@
#include "print.h" #include "print.h"
#include "process_combo.h" #include "process_combo.h"
#include "action_tapping.h"
#ifndef COMBO_VARIABLE_LEN
__attribute__((weak)) combo_t key_combos[COMBO_COUNT] = {}; #ifdef COMBO_COUNT
__attribute__((weak)) combo_t key_combos[COMBO_COUNT];
uint16_t COMBO_LEN = COMBO_COUNT;
#else #else
extern combo_t key_combos[]; extern combo_t key_combos[];
extern int COMBO_LEN; extern uint16_t COMBO_LEN;
#endif #endif
__attribute__((weak)) void process_combo_event(uint16_t combo_index, bool pressed) {} __attribute__((weak)) void process_combo_event(uint16_t combo_index, bool pressed) {}
static uint16_t timer = 0; #ifdef COMBO_MUST_HOLD_PER_COMBO
static uint16_t current_combo_index = 0; __attribute__((weak)) bool get_combo_must_hold(uint16_t index, combo_t *combo) { return false; }
static bool drop_buffer = false;
static bool is_active = false;
static bool b_combo_enable = true; // defaults to enabled
static uint8_t buffer_size = 0;
#ifdef COMBO_ALLOW_ACTION_KEYS
static keyrecord_t key_buffer[MAX_COMBO_LENGTH];
#else
static uint16_t key_buffer[MAX_COMBO_LENGTH];
#endif #endif
static inline void send_combo(uint16_t action, bool pressed) { #ifdef COMBO_MUST_TAP_PER_COMBO
if (action) { __attribute__((weak)) bool get_combo_must_tap(uint16_t index, combo_t *combo) { return false; }
if (pressed) { #endif
register_code16(action);
#ifdef COMBO_TERM_PER_COMBO
__attribute__((weak)) uint16_t get_combo_term(uint16_t index, combo_t *combo) { return COMBO_TERM; }
#endif
#ifdef COMBO_PROCESS_KEY_RELEASE
__attribute__((weak)) bool process_combo_key_release(uint16_t combo_index, combo_t *combo, uint8_t key_index, uint16_t keycode) { return false; }
#endif
#ifndef COMBO_NO_TIMER
static uint16_t timer = 0;
#endif
static bool b_combo_enable = true; // defaults to enabled
static uint16_t longest_term = 0;
typedef struct {
keyrecord_t record;
uint16_t combo_index;
uint16_t keycode;
} queued_record_t;
static uint8_t key_buffer_size = 0;
static queued_record_t key_buffer[COMBO_KEY_BUFFER_LENGTH];
typedef struct {
uint16_t combo_index;
} queued_combo_t;
static uint8_t combo_buffer_write= 0;
static uint8_t combo_buffer_read = 0;
static queued_combo_t combo_buffer[COMBO_BUFFER_LENGTH];
#define INCREMENT_MOD(i) i = (i + 1) % COMBO_BUFFER_LENGTH
#define COMBO_KEY_POS ((keypos_t){.col=254, .row=254})
#ifndef EXTRA_SHORT_COMBOS
/* flags are their own elements in combo_t struct. */
# define COMBO_ACTIVE(combo) (combo->active)
# define COMBO_DISABLED(combo) (combo->disabled)
# define COMBO_STATE(combo) (combo->state)
# define ACTIVATE_COMBO(combo) do {combo->active = true;}while(0)
# define DEACTIVATE_COMBO(combo) do {combo->active = false;}while(0)
# define DISABLE_COMBO(combo) do {combo->disabled = true;}while(0)
# define RESET_COMBO_STATE(combo) do { \
combo->disabled = false; \
combo->state = 0; \
}while(0)
#else
/* flags are at the two high bits of state. */
# define COMBO_ACTIVE(combo) (combo->state & 0x80)
# define COMBO_DISABLED(combo) (combo->state & 0x40)
# define COMBO_STATE(combo) (combo->state & 0x3F)
# define ACTIVATE_COMBO(combo) do {combo->state |= 0x80;}while(0)
# define DEACTIVATE_COMBO(combo) do {combo->state &= ~0x80;}while(0)
# define DISABLE_COMBO(combo) do {combo->state |= 0x40;}while(0)
# define RESET_COMBO_STATE(combo) do {combo->state &= ~0x7F;}while(0)
#endif
static inline void release_combo(uint16_t combo_index, combo_t *combo) {
if (combo->keycode) {
keyrecord_t record = {
.event = {
.key = COMBO_KEY_POS,
.time = timer_read()|1,
.pressed = false,
},
.keycode = combo->keycode,
};
#ifndef NO_ACTION_TAPPING
action_tapping_process(record);
#else
process_record(&record);
#endif
} else { } else {
unregister_code16(action); process_combo_event(combo_index, false);
}
DEACTIVATE_COMBO(combo);
}
static inline bool _get_combo_must_hold(uint16_t combo_index, combo_t *combo) {
#ifdef COMBO_NO_TIMER
return false;
#elif defined(COMBO_MUST_HOLD_PER_COMBO)
return get_combo_must_hold(combo_index, combo);
#elif defined(COMBO_MUST_HOLD_MODS)
return (KEYCODE_IS_MOD(combo->keycode) ||
(combo->keycode >= QK_MOMENTARY && combo->keycode <= QK_MOMENTARY_MAX));
#endif
return false;
}
static inline uint16_t _get_wait_time(uint16_t combo_index, combo_t *combo ) {
if (_get_combo_must_hold(combo_index, combo)
#ifdef COMBO_MUST_TAP_PER_COMBO
|| get_combo_must_tap(combo_index, combo)
#endif
) {
if (longest_term < COMBO_HOLD_TERM) {
return COMBO_HOLD_TERM;
}
}
return longest_term;
}
static inline uint16_t _get_combo_term(uint16_t combo_index, combo_t *combo) {
#if defined(COMBO_TERM_PER_COMBO)
return get_combo_term(combo_index, combo);
#endif
return COMBO_TERM;
}
void clear_combos(void) {
uint16_t index = 0;
longest_term = 0;
for (index = 0; index < COMBO_LEN; ++index) {
combo_t *combo = &key_combos[index];
if (!COMBO_ACTIVE(combo)) {
RESET_COMBO_STATE(combo);
} }
} else {
process_combo_event(current_combo_index, pressed);
} }
} }
static inline void dump_key_buffer(bool emit) { static inline void dump_key_buffer(void) {
if (buffer_size == 0) { if (key_buffer_size == 0) {
return; return;
} }
if (emit) { for (uint8_t key_buffer_i = 0; key_buffer_i < key_buffer_size; key_buffer_i++) {
for (uint8_t i = 0; i < buffer_size; i++) {
#ifdef COMBO_ALLOW_ACTION_KEYS queued_record_t *qrecord = &key_buffer[key_buffer_i];
const action_t action = store_or_get_action(key_buffer[i].event.pressed, key_buffer[i].event.key); keyrecord_t *record = &qrecord->record;
process_action(&(key_buffer[i]), action);
if (IS_NOEVENT(record->event)) {
continue;
}
if (!record->keycode && qrecord->combo_index != (uint16_t)-1) {
process_combo_event(qrecord->combo_index, true);
} else {
#ifndef NO_ACTION_TAPPING
action_tapping_process(*record);
#else #else
register_code16(key_buffer[i]); process_record(record);
send_keyboard_report();
#endif #endif
} }
record->event.time = 0;
} }
buffer_size = 0; key_buffer_size = 0;
} }
#define ALL_COMBO_KEYS_ARE_DOWN (((1 << count) - 1) == combo->state) #define NO_COMBO_KEYS_ARE_DOWN (0 == COMBO_STATE(combo))
#define KEY_STATE_DOWN(key) \ #define ALL_COMBO_KEYS_ARE_DOWN(state, key_count) (((1 << key_count) - 1) == state)
#define ONLY_ONE_KEY_IS_DOWN(state) !(state & (state - 1))
#define KEY_NOT_YET_RELEASED(state, key_index) ((1 << key_index) & state)
#define KEY_STATE_DOWN(state, key_index) \
do { \ do { \
combo->state |= (1 << key); \ state |= (1 << key_index); \
} while (0) } while (0)
#define KEY_STATE_UP(key) \ #define KEY_STATE_UP(state, key_index) \
do { \ do { \
combo->state &= ~(1 << key); \ state &= ~(1 << key_index); \
} while (0) } while (0)
static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record) { static inline void _find_key_index_and_count(const uint16_t *keys, uint16_t keycode, uint16_t *key_index, uint8_t *key_count) {
uint8_t count = 0; while (true) {
uint16_t index = -1; uint16_t key = pgm_read_word(&keys[*key_count]);
/* Find index of keycode and number of combo keys */ if (keycode == key) *key_index = *key_count;
for (const uint16_t *keys = combo->keys;; ++count) {
uint16_t key = pgm_read_word(&keys[count]);
if (keycode == key) index = count;
if (COMBO_END == key) break; if (COMBO_END == key) break;
(*key_count)++;
} }
/* Continue processing if not a combo key */
if (-1 == (int8_t)index) return false;
bool is_combo_active = is_active;
if (record->event.pressed) {
KEY_STATE_DOWN(index);
if (is_combo_active) {
if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was pressed */
send_combo(combo->keycode, true);
drop_buffer = true;
}
}
} else {
if (ALL_COMBO_KEYS_ARE_DOWN) { /* Combo was released */
send_combo(combo->keycode, false);
} else {
/* continue processing without immediately returning */
is_combo_active = false;
}
KEY_STATE_UP(index);
}
return is_combo_active;
} }
#define NO_COMBO_KEYS_ARE_DOWN (0 == combo->state) void drop_combo_from_buffer(uint16_t combo_index) {
/* Mark a combo as processed from the buffer. If the buffer is in the
* beginning of the buffer, drop it. */
uint8_t i = combo_buffer_read;
while (i != combo_buffer_write) {
queued_combo_t *qcombo = &combo_buffer[i];
if (qcombo->combo_index == combo_index) {
combo_t *combo = &key_combos[combo_index];
DISABLE_COMBO(combo);
if (i == combo_buffer_read) {
INCREMENT_MOD(combo_buffer_read);
}
break;
}
INCREMENT_MOD(i);
}
}
void apply_combo(uint16_t combo_index, combo_t *combo) {
/* Apply combo's result keycode to the last chord key of the combo and
* disable the other keys. */
if (COMBO_DISABLED(combo)) { return; }
// state to check against so we find the last key of the combo from the buffer
#if defined(EXTRA_EXTRA_LONG_COMBOS)
uint32_t state = 0;
#elif defined(EXTRA_LONG_COMBOS)
uint16_t state = 0;
#else
uint8_t state = 0;
#endif
for (uint8_t key_buffer_i = 0; key_buffer_i < key_buffer_size; key_buffer_i++) {
queued_record_t *qrecord = &key_buffer[key_buffer_i];
keyrecord_t *record = &qrecord->record;
uint16_t keycode = qrecord->keycode;
uint8_t key_count = 0;
uint16_t key_index = -1;
_find_key_index_and_count(combo->keys, keycode, &key_index, &key_count);
if (-1 == (int16_t)key_index) {
// key not part of this combo
continue;
}
KEY_STATE_DOWN(state, key_index);
if (ALL_COMBO_KEYS_ARE_DOWN(state, key_count)) {
// this in the end executes the combo when the key_buffer is dumped.
record->keycode = combo->keycode;
record->event.key = COMBO_KEY_POS;
qrecord->combo_index = combo_index;
ACTIVATE_COMBO(combo);
break;
} else {
// key was part of the combo but not the last one, "disable" it
// by making it a TICK event.
record->event.time = 0;
}
}
drop_combo_from_buffer(combo_index);
}
static inline void apply_combos(void) {
// Apply all buffered normal combos.
for (uint8_t i = combo_buffer_read;
i != combo_buffer_write;
INCREMENT_MOD(i)) {
queued_combo_t *buffered_combo = &combo_buffer[i];
combo_t *combo = &key_combos[buffered_combo->combo_index];
#ifdef COMBO_MUST_TAP_PER_COMBO
if (get_combo_must_tap(buffered_combo->combo_index, combo)) {
// Tap-only combos are applied on key release only, so let's drop 'em here.
drop_combo_from_buffer(buffered_combo->combo_index);
continue;
}
#endif
apply_combo(buffered_combo->combo_index, combo);
}
dump_key_buffer();
clear_combos();
}
combo_t* overlaps(combo_t *combo1, combo_t *combo2) {
/* Checks if the combos overlap and returns the combo that should be
* dropped from the combo buffer.
* The combo that has less keys will be dropped. If they have the same
* amount of keys, drop combo1. */
uint8_t idx1 = 0, idx2 = 0;
uint16_t key1, key2;
bool overlaps = false;
while ((key1 = pgm_read_word(&combo1->keys[idx1])) != COMBO_END) {
idx2 = 0;
while ((key2 = pgm_read_word(&combo2->keys[idx2])) != COMBO_END) {
if (key1 == key2) overlaps = true;
idx2 += 1;
}
idx1 += 1;
}
if (!overlaps) return NULL;
if (idx2 < idx1) return combo2;
return combo1;
}
static bool process_single_combo(combo_t *combo, uint16_t keycode, keyrecord_t *record, uint16_t combo_index) {
uint8_t key_count = 0;
uint16_t key_index = -1;
_find_key_index_and_count(combo->keys, keycode, &key_index, &key_count);
/* Continue processing if key isn't part of current combo. */
if (-1 == (int16_t)key_index) {
return false;
}
bool key_is_part_of_combo = !COMBO_DISABLED(combo);
if (record->event.pressed && !COMBO_DISABLED(combo)) {
uint16_t time = _get_combo_term(combo_index, combo);
if (!COMBO_ACTIVE(combo)) {
KEY_STATE_DOWN(combo->state, key_index);
if (longest_term < time) {
longest_term = time;
}
}
if (ALL_COMBO_KEYS_ARE_DOWN(COMBO_STATE(combo), key_count)) {
/* Combo was fully pressed */
/* Buffer the combo so we can fire it after COMBO_TERM */
#ifndef COMBO_NO_TIMER
/* Don't buffer this combo if its combo term has passed. */
if (timer && timer_elapsed(timer) > time) {
DISABLE_COMBO(combo);
return true;
} else
#endif
{
// disable readied combos that overlap with this combo
combo_t *drop = NULL;
for (uint8_t combo_buffer_i = combo_buffer_read;
combo_buffer_i != combo_buffer_write;
INCREMENT_MOD(combo_buffer_i)) {
queued_combo_t *qcombo = &combo_buffer[combo_buffer_i];
combo_t *buffered_combo = &key_combos[qcombo->combo_index];
if ((drop = overlaps(buffered_combo, combo))) {
DISABLE_COMBO(drop);
if (drop == combo) {
// stop checking for overlaps if dropped combo was current combo.
break;
} else if (combo_buffer_i == combo_buffer_read && drop == buffered_combo) {
/* Drop the disabled buffered combo from the buffer if
* it is in the beginning of the buffer. */
INCREMENT_MOD(combo_buffer_read);
}
}
}
if (drop != combo) {
// save this combo to buffer
combo_buffer[combo_buffer_write] = (queued_combo_t){
.combo_index=combo_index,
};
INCREMENT_MOD(combo_buffer_write);
// get possible longer waiting time for tap-/hold-only combos.
longest_term = _get_wait_time(combo_index, combo);
}
} // if timer elapsed end
}
} else {
// chord releases
if (!COMBO_ACTIVE(combo) && ALL_COMBO_KEYS_ARE_DOWN(COMBO_STATE(combo), key_count)) {
/* First key quickly released */
if (COMBO_DISABLED(combo) || _get_combo_must_hold(combo_index, combo)) {
// combo wasn't tappable, disable it and drop it from buffer.
drop_combo_from_buffer(combo_index);
key_is_part_of_combo = false;
}
#ifdef COMBO_MUST_TAP_PER_COMBO
else if (get_combo_must_tap(combo_index, combo)) {
// immediately apply tap-only combo
apply_combo(combo_index, combo);
apply_combos(); // also apply other prepared combos and dump key buffer
# ifdef COMBO_PROCESS_KEY_RELEASE
if (process_combo_key_release(combo_index, combo, key_index, keycode)) {
release_combo(combo_index, combo);
}
# endif
}
#endif
} else if (COMBO_ACTIVE(combo)
&& ONLY_ONE_KEY_IS_DOWN(COMBO_STATE(combo))
&& KEY_NOT_YET_RELEASED(COMBO_STATE(combo), key_index)
) {
/* last key released */
release_combo(combo_index, combo);
key_is_part_of_combo = true;
#ifdef COMBO_PROCESS_KEY_RELEASE
process_combo_key_release(combo_index, combo, key_index, keycode);
#endif
} else if (COMBO_ACTIVE(combo)
&& KEY_NOT_YET_RELEASED(COMBO_STATE(combo), key_index)
) {
/* first or middle key released */
key_is_part_of_combo = true;
#ifdef COMBO_PROCESS_KEY_RELEASE
if (process_combo_key_release(combo_index, combo, key_index, keycode)) {
release_combo(combo_index, combo);
}
#endif
} else {
/* The released key was part of an incomplete combo */
key_is_part_of_combo = false;
}
KEY_STATE_UP(combo->state, key_index);
}
return key_is_part_of_combo;
}
bool process_combo(uint16_t keycode, keyrecord_t *record) { bool process_combo(uint16_t keycode, keyrecord_t *record) {
bool is_combo_key = false; bool is_combo_key = false;
drop_buffer = false;
bool no_combo_keys_pressed = true; bool no_combo_keys_pressed = true;
if (keycode == CMB_ON && record->event.pressed) { if (keycode == CMB_ON && record->event.pressed) {
@ -144,62 +475,81 @@ bool process_combo(uint16_t keycode, keyrecord_t *record) {
if (!is_combo_enabled()) { if (!is_combo_enabled()) {
return true; return true;
} }
#ifndef COMBO_VARIABLE_LEN
for (current_combo_index = 0; current_combo_index < COMBO_COUNT; ++current_combo_index) { #ifdef COMBO_ONLY_FROM_LAYER
#else /* Only check keycodes from one layer. */
for (current_combo_index = 0; current_combo_index < COMBO_LEN; ++current_combo_index) { keycode = keymap_key_to_keycode(COMBO_ONLY_FROM_LAYER, record->event.key);
#endif #endif
combo_t *combo = &key_combos[current_combo_index];
is_combo_key |= process_single_combo(combo, keycode, record); for (uint16_t idx = 0; idx < COMBO_LEN; ++idx) {
no_combo_keys_pressed = no_combo_keys_pressed && NO_COMBO_KEYS_ARE_DOWN; combo_t *combo = &key_combos[idx];
is_combo_key |= process_single_combo(combo, keycode, record, idx);
no_combo_keys_pressed = no_combo_keys_pressed && (NO_COMBO_KEYS_ARE_DOWN || COMBO_ACTIVE(combo) || COMBO_DISABLED(combo));
} }
if (drop_buffer) { if (record->event.pressed && is_combo_key) {
/* buffer is only dropped when we complete a combo, so we refresh the timer #ifndef COMBO_NO_TIMER
* here */ # ifdef COMBO_STRICT_TIMER
if (!timer) {
// timer is set only on the first key
timer = timer_read(); timer = timer_read();
dump_key_buffer(false); }
} else if (!is_combo_key) { # else
/* if no combos claim the key we need to emit the keybuffer */ timer = timer_read();
dump_key_buffer(true); # endif
#endif
if (key_buffer_size < COMBO_KEY_BUFFER_LENGTH) {
key_buffer[key_buffer_size++] = (queued_record_t){
.record = *record,
.keycode = keycode,
.combo_index = -1, // this will be set when applying combos
};
}
} else {
if (combo_buffer_read != combo_buffer_write) {
// some combo is prepared
apply_combos();
} else {
// reset state if there are no combo keys pressed at all // reset state if there are no combo keys pressed at all
if (no_combo_keys_pressed) { dump_key_buffer();
#ifndef COMBO_NO_TIMER
timer = 0; timer = 0;
is_active = true;
}
} else if (record->event.pressed && is_active) {
/* otherwise the key is consumed and placed in the buffer */
timer = timer_read();
if (buffer_size < MAX_COMBO_LENGTH) {
#ifdef COMBO_ALLOW_ACTION_KEYS
key_buffer[buffer_size++] = *record;
#else
key_buffer[buffer_size++] = keycode;
#endif #endif
clear_combos();
} }
} }
return !is_combo_key; return !is_combo_key;
} }
void combo_task(void) { void combo_task(void) {
if (b_combo_enable && is_active && timer && timer_elapsed(timer) > COMBO_TERM) { if (!b_combo_enable) {
/* This disables the combo, meaning key events for this return;
* combo will be handled by the next processors in the chain
*/
is_active = false;
dump_key_buffer(true);
} }
#ifndef COMBO_NO_TIMER
if (timer && timer_elapsed(timer) > longest_term) {
if (combo_buffer_read != combo_buffer_write) {
apply_combos();
longest_term = 0;
timer = 0;
} else {
dump_key_buffer();
timer = 0;
clear_combos();
}
}
#endif
} }
void combo_enable(void) { b_combo_enable = true; } void combo_enable(void) { b_combo_enable = true; }
void combo_disable(void) { void combo_disable(void) {
b_combo_enable = is_active = false; #ifndef COMBO_NO_TIMER
timer = 0; timer = 0;
dump_key_buffer(true); #endif
b_combo_enable = false;
combo_buffer_read = combo_buffer_write;
} }
void combo_toggle(void) { void combo_toggle(void) {

View file

@ -20,23 +20,38 @@
#include "quantum.h" #include "quantum.h"
#include <stdint.h> #include <stdint.h>
#ifdef EXTRA_EXTRA_LONG_COMBOS #ifdef EXTRA_SHORT_COMBOS
# define MAX_COMBO_LENGTH 6
#elif defined(EXTRA_EXTRA_LONG_COMBOS)
# define MAX_COMBO_LENGTH 32 # define MAX_COMBO_LENGTH 32
#elif EXTRA_LONG_COMBOS #elif defined(EXTRA_LONG_COMBOS)
# define MAX_COMBO_LENGTH 16 # define MAX_COMBO_LENGTH 16
#else #else
# define MAX_COMBO_LENGTH 8 # define MAX_COMBO_LENGTH 8
#endif #endif
#ifndef COMBO_KEY_BUFFER_LENGTH
# define COMBO_KEY_BUFFER_LENGTH MAX_COMBO_LENGTH
#endif
#ifndef COMBO_BUFFER_LENGTH
# define COMBO_BUFFER_LENGTH 4
#endif
typedef struct { typedef struct {
const uint16_t *keys; const uint16_t *keys;
uint16_t keycode; uint16_t keycode;
#ifdef EXTRA_EXTRA_LONG_COMBOS #ifdef EXTRA_SHORT_COMBOS
uint32_t state;
#elif EXTRA_LONG_COMBOS
uint16_t state;
#else
uint8_t state; uint8_t state;
#else
bool disabled;
bool active;
# if defined(EXTRA_EXTRA_LONG_COMBOS)
uint32_t state;
# elif defined(EXTRA_LONG_COMBOS)
uint16_t state;
# else
uint8_t state;
# endif
#endif #endif
} combo_t; } combo_t;
@ -46,12 +61,15 @@ typedef struct {
{ .keys = &(ck)[0] } { .keys = &(ck)[0] }
#define COMBO_END 0 #define COMBO_END 0
#ifndef COMBO_COUNT
# define COMBO_COUNT 0
#endif
#ifndef COMBO_TERM #ifndef COMBO_TERM
# define COMBO_TERM TAPPING_TERM # define COMBO_TERM 50
#endif #endif
#ifndef COMBO_HOLD_TERM
# define COMBO_HOLD_TERM TAPPING_TERM
#endif
/* check if keycode is only modifiers */
#define KEYCODE_IS_MOD(code) (IS_MOD(code) || (code >= QK_MODS && code <= QK_MODS_MAX && !(code & QK_BASIC_MAX)))
bool process_combo(uint16_t keycode, keyrecord_t *record); bool process_combo(uint16_t keycode, keyrecord_t *record);
void combo_task(void); void combo_task(void);

View file

@ -143,7 +143,13 @@ void reset_keyboard(void) {
} }
/* Convert record into usable keycode via the contained event. */ /* Convert record into usable keycode via the contained event. */
uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) { return get_event_keycode(record->event, update_layer_cache); } uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) {
#ifdef COMBO_ENABLE
if (record->keycode) { return record->keycode; }
#endif
return get_event_keycode(record->event, update_layer_cache);
}
/* Convert event into usable keycode. Checks the layer cache to ensure that it /* Convert event into usable keycode. Checks the layer cache to ensure that it
* retains the correct keycode after a layer change, if the key is still pressed. * retains the correct keycode after a layer change, if the key is still pressed.
@ -169,6 +175,18 @@ uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key); return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
} }
/* Get keycode, and then process pre tapping functionality */
bool pre_process_record_quantum(keyrecord_t *record) {
if (!(
#ifdef COMBO_ENABLE
process_combo(get_record_keycode(record, true), record) &&
#endif
true)) {
return false;
}
return true; // continue processing
}
/* Get keycode, and then call keyboard function */ /* Get keycode, and then call keyboard function */
void post_process_record_quantum(keyrecord_t *record) { void post_process_record_quantum(keyrecord_t *record) {
uint16_t keycode = get_record_keycode(record, false); uint16_t keycode = get_record_keycode(record, false);
@ -254,9 +272,6 @@ bool process_record_quantum(keyrecord_t *record) {
#ifdef LEADER_ENABLE #ifdef LEADER_ENABLE
process_leader(keycode, record) && process_leader(keycode, record) &&
#endif #endif
#ifdef COMBO_ENABLE
process_combo(keycode, record) &&
#endif
#ifdef PRINTING_ENABLE #ifdef PRINTING_ENABLE
process_printer(keycode, record) && process_printer(keycode, record) &&
#endif #endif

View file

@ -55,6 +55,8 @@ __attribute__((weak)) bool get_ignore_mod_tap_interrupt(uint16_t keycode, keyrec
__attribute__((weak)) bool get_retro_tapping(uint16_t keycode, keyrecord_t *record) { return false; } __attribute__((weak)) bool get_retro_tapping(uint16_t keycode, keyrecord_t *record) { return false; }
#endif #endif
__attribute__((weak)) bool pre_process_record_quantum(keyrecord_t *record) { return true; }
#ifndef TAP_CODE_DELAY #ifndef TAP_CODE_DELAY
# define TAP_CODE_DELAY 0 # define TAP_CODE_DELAY 0
#endif #endif
@ -106,9 +108,13 @@ void action_exec(keyevent_t event) {
#endif #endif
#ifndef NO_ACTION_TAPPING #ifndef NO_ACTION_TAPPING
if (IS_NOEVENT(record.event) || pre_process_record_quantum(&record)) {
action_tapping_process(record); action_tapping_process(record);
}
#else #else
if (IS_NOEVENT(record.event) || pre_process_record_quantum(&record)) {
process_record(&record); process_record(&record);
}
if (!IS_NOEVENT(record.event)) { if (!IS_NOEVENT(record.event)) {
dprint("processed: "); dprint("processed: ");
debug_record(record); debug_record(record);
@ -206,7 +212,16 @@ void process_record(keyrecord_t *record) {
} }
void process_record_handler(keyrecord_t *record) { void process_record_handler(keyrecord_t *record) {
#ifdef COMBO_ENABLE
action_t action;
if (record->keycode) {
action = action_for_keycode(record->keycode);
} else {
action = store_or_get_action(record->event.pressed, record->event.key);
}
#else
action_t action = store_or_get_action(record->event.pressed, record->event.key); action_t action = store_or_get_action(record->event.pressed, record->event.key);
#endif
dprint("ACTION: "); dprint("ACTION: ");
debug_action(action); debug_action(action);
#ifndef NO_ACTION_LAYER #ifndef NO_ACTION_LAYER
@ -990,6 +1005,24 @@ bool is_tap_key(keypos_t key) {
return is_tap_action(action); return is_tap_action(action);
} }
/** \brief Utilities for actions. (FIXME: Needs better description)
*
* FIXME: Needs documentation.
*/
bool is_tap_record(keyrecord_t *record) {
#ifdef COMBO_ENABLE
action_t action;
if (record->keycode) {
action = action_for_keycode(record->keycode);
} else {
action = layer_switch_get_action(record->event.key);
}
#else
action_t action = layer_switch_get_action(record->event.key);
#endif
return is_tap_action(action);
}
/** \brief Utilities for actions. (FIXME: Needs better description) /** \brief Utilities for actions. (FIXME: Needs better description)
* *
* FIXME: Needs documentation. * FIXME: Needs documentation.

View file

@ -53,6 +53,9 @@ typedef struct {
#ifndef NO_ACTION_TAPPING #ifndef NO_ACTION_TAPPING
tap_t tap; tap_t tap;
#endif #endif
#ifdef COMBO_ENABLE
uint16_t keycode;
#endif
} keyrecord_t; } keyrecord_t;
/* Execute action per keyevent */ /* Execute action per keyevent */
@ -60,6 +63,7 @@ void action_exec(keyevent_t event);
/* action for key */ /* action for key */
action_t action_for_key(uint8_t layer, keypos_t key); action_t action_for_key(uint8_t layer, keypos_t key);
action_t action_for_keycode(uint16_t keycode);
/* macro */ /* macro */
const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt); const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt);
@ -111,6 +115,7 @@ void clear_keyboard_but_mods(void);
void clear_keyboard_but_mods_and_keys(void); void clear_keyboard_but_mods_and_keys(void);
void layer_switch(uint8_t new_layer); void layer_switch(uint8_t new_layer);
bool is_tap_key(keypos_t key); bool is_tap_key(keypos_t key);
bool is_tap_record(keyrecord_t *record);
bool is_tap_action(action_t action); bool is_tap_action(action_t action);
#ifndef NO_ACTION_TAPPING #ifndef NO_ACTION_TAPPING

View file

@ -18,11 +18,16 @@
# define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed) # define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
# define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed) # define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
# define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k))) # define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
#ifndef COMBO_ENABLE
# define IS_TAPPING_RECORD(r) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (r->event.key)))
#else
# define IS_TAPPING_RECORD(r) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (r->event.key)) && tapping_key.keycode == r->keycode)
#endif
__attribute__((weak)) uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) { return TAPPING_TERM; } __attribute__((weak)) uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) { return TAPPING_TERM; }
# ifdef TAPPING_TERM_PER_KEY # ifdef TAPPING_TERM_PER_KEY
# define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < get_tapping_term(get_event_keycode(tapping_key.event, false), &tapping_key)) # define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < get_tapping_term(get_record_keycode(&tapping_key, false), &tapping_key))
# else # else
# define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM) # define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
# endif # endif
@ -103,7 +108,7 @@ bool process_tapping(keyrecord_t *keyp) {
if (IS_TAPPING_PRESSED()) { if (IS_TAPPING_PRESSED()) {
if (WITHIN_TAPPING_TERM(event)) { if (WITHIN_TAPPING_TERM(event)) {
if (tapping_key.tap.count == 0) { if (tapping_key.tap.count == 0) {
if (IS_TAPPING_KEY(event.key) && !event.pressed) { if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
// first tap! // first tap!
debug("Tapping: First tap(0->1).\n"); debug("Tapping: First tap(0->1).\n");
tapping_key.tap.count = 1; tapping_key.tap.count = 1;
@ -122,14 +127,14 @@ bool process_tapping(keyrecord_t *keyp) {
# if defined(TAPPING_TERM_PER_KEY) || (TAPPING_TERM >= 500) || defined(PERMISSIVE_HOLD) || defined(PERMISSIVE_HOLD_PER_KEY) # if defined(TAPPING_TERM_PER_KEY) || (TAPPING_TERM >= 500) || defined(PERMISSIVE_HOLD) || defined(PERMISSIVE_HOLD_PER_KEY)
else if ((( else if (((
# ifdef TAPPING_TERM_PER_KEY # ifdef TAPPING_TERM_PER_KEY
get_tapping_term(get_event_keycode(tapping_key.event, false), keyp) get_tapping_term(get_record_keycode(&tapping_key, false), keyp)
# else # else
TAPPING_TERM TAPPING_TERM
# endif # endif
>= 500) >= 500)
# ifdef PERMISSIVE_HOLD_PER_KEY # ifdef PERMISSIVE_HOLD_PER_KEY
|| get_permissive_hold(get_event_keycode(tapping_key.event, false), keyp) || get_permissive_hold(get_record_keycode(&tapping_key, false), keyp)
# elif defined(PERMISSIVE_HOLD) # elif defined(PERMISSIVE_HOLD)
|| true || true
# endif # endif
@ -177,7 +182,7 @@ bool process_tapping(keyrecord_t *keyp) {
} }
// tap_count > 0 // tap_count > 0
else { else {
if (IS_TAPPING_KEY(event.key) && !event.pressed) { if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
debug("Tapping: Tap release("); debug("Tapping: Tap release(");
debug_dec(tapping_key.tap.count); debug_dec(tapping_key.tap.count);
debug(")\n"); debug(")\n");
@ -186,11 +191,15 @@ bool process_tapping(keyrecord_t *keyp) {
tapping_key = *keyp; tapping_key = *keyp;
debug_tapping_key(); debug_tapping_key();
return true; return true;
} else if (is_tap_key(event.key) && event.pressed) { } else if (is_tap_record(keyp) && event.pressed) {
if (tapping_key.tap.count > 1) { if (tapping_key.tap.count > 1) {
debug("Tapping: Start new tap with releasing last tap(>1).\n"); debug("Tapping: Start new tap with releasing last tap(>1).\n");
// unregister key // unregister key
process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false}); process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false,
#ifdef COMBO_ENABLE
.keycode = tapping_key.keycode,
#endif
});
} else { } else {
debug("Tapping: Start while last tap(1).\n"); debug("Tapping: Start while last tap(1).\n");
} }
@ -218,17 +227,21 @@ bool process_tapping(keyrecord_t *keyp) {
debug_tapping_key(); debug_tapping_key();
return false; return false;
} else { } else {
if (IS_TAPPING_KEY(event.key) && !event.pressed) { if (IS_TAPPING_RECORD(keyp) && !event.pressed) {
debug("Tapping: End. last timeout tap release(>0)."); debug("Tapping: End. last timeout tap release(>0).");
keyp->tap = tapping_key.tap; keyp->tap = tapping_key.tap;
process_record(keyp); process_record(keyp);
tapping_key = (keyrecord_t){}; tapping_key = (keyrecord_t){};
return true; return true;
} else if (is_tap_key(event.key) && event.pressed) { } else if (is_tap_record(keyp) && event.pressed) {
if (tapping_key.tap.count > 1) { if (tapping_key.tap.count > 1) {
debug("Tapping: Start new tap with releasing last timeout tap(>1).\n"); debug("Tapping: Start new tap with releasing last timeout tap(>1).\n");
// unregister key // unregister key
process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false}); process_record(&(keyrecord_t){.tap = tapping_key.tap, .event.key = tapping_key.event.key, .event.time = event.time, .event.pressed = false,
#ifdef COMBO_ENABLE
.keycode = tapping_key.keycode,
#endif
});
} else { } else {
debug("Tapping: Start while last timeout tap(1).\n"); debug("Tapping: Start while last timeout tap(1).\n");
} }
@ -248,12 +261,12 @@ bool process_tapping(keyrecord_t *keyp) {
} else if (IS_TAPPING_RELEASED()) { } else if (IS_TAPPING_RELEASED()) {
if (WITHIN_TAPPING_TERM(event)) { if (WITHIN_TAPPING_TERM(event)) {
if (event.pressed) { if (event.pressed) {
if (IS_TAPPING_KEY(event.key)) { if (IS_TAPPING_RECORD(keyp)) {
//# ifndef TAPPING_FORCE_HOLD //# ifndef TAPPING_FORCE_HOLD
# if !defined(TAPPING_FORCE_HOLD) || defined(TAPPING_FORCE_HOLD_PER_KEY) # if !defined(TAPPING_FORCE_HOLD) || defined(TAPPING_FORCE_HOLD_PER_KEY)
if ( if (
# ifdef TAPPING_FORCE_HOLD_PER_KEY # ifdef TAPPING_FORCE_HOLD_PER_KEY
!get_tapping_force_hold(get_event_keycode(tapping_key.event, false), keyp) && !get_tapping_force_hold(get_record_keycode(&tapping_key, false), keyp) &&
# endif # endif
!tapping_key.tap.interrupted && tapping_key.tap.count > 0) { !tapping_key.tap.interrupted && tapping_key.tap.count > 0) {
// sequential tap. // sequential tap.
@ -271,7 +284,7 @@ bool process_tapping(keyrecord_t *keyp) {
// FIX: start new tap again // FIX: start new tap again
tapping_key = *keyp; tapping_key = *keyp;
return true; return true;
} else if (is_tap_key(event.key)) { } else if (is_tap_record(keyp)) {
// Sequential tap can be interfered with other tap key. // Sequential tap can be interfered with other tap key.
debug("Tapping: Start with interfering other tap.\n"); debug("Tapping: Start with interfering other tap.\n");
tapping_key = *keyp; tapping_key = *keyp;
@ -303,7 +316,7 @@ bool process_tapping(keyrecord_t *keyp) {
} }
// not tapping state // not tapping state
else { else {
if (event.pressed && is_tap_key(event.key)) { if (event.pressed && is_tap_record(keyp)) {
debug("Tapping: Start(Press tap key).\n"); debug("Tapping: Start(Press tap key).\n");
tapping_key = *keyp; tapping_key = *keyp;
process_record_tap_hint(&tapping_key); process_record_tap_hint(&tapping_key);

View file

@ -30,6 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define WAITING_BUFFER_SIZE 8 #define WAITING_BUFFER_SIZE 8
#ifndef NO_ACTION_TAPPING #ifndef NO_ACTION_TAPPING
uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache);
uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache); uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache);
void action_tapping_process(keyrecord_t record); void action_tapping_process(keyrecord_t record);