feat(combos): add layer filtering

This commit is contained in:
KemoNine 2021-02-03 00:55:41 +00:00
parent 9a7908b632
commit 1dcb2f905c
10 changed files with 171 additions and 4 deletions

View file

@ -301,6 +301,10 @@ config ZMK_COMBO_MAX_KEYS_PER_COMBO
int "Maximum number of keys per combo" int "Maximum number of keys per combo"
default 4 default 4
config ZMK_COMBO_MAX_LAYER_FILTERS_PER_COMBO
int "Maximum number of layers that can be specified per combo"
default 4
#Display/LED Options #Display/LED Options
endmenu endmenu

View file

@ -20,3 +20,6 @@ child-binding:
default: 50 default: 50
slow-release: slow-release:
type: boolean type: boolean
layers:
type: array
default: [-1]

View file

@ -17,6 +17,7 @@
#include <zmk/events/position_state_changed.h> #include <zmk/events/position_state_changed.h>
#include <zmk/hid.h> #include <zmk/hid.h>
#include <zmk/matrix.h> #include <zmk/matrix.h>
#include <zmk/keymap.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@ -33,6 +34,8 @@ struct combo_cfg {
// the virtual key position is a key position outside the range used by the keyboard. // the virtual key position is a key position outside the range used by the keyboard.
// it is necessary so hold-taps can uniquely identify a behavior. // it is necessary so hold-taps can uniquely identify a behavior.
int32_t virtual_key_position; int32_t virtual_key_position;
int8_t layers[CONFIG_ZMK_COMBO_MAX_LAYER_FILTERS_PER_COMBO];
int32_t layers_len;
}; };
struct active_combo { struct active_combo {
@ -104,17 +107,35 @@ static int initialize_combo(struct combo_cfg *new_combo) {
return 0; return 0;
} }
static bool combo_active_on_layer(struct combo_cfg *combo, uint8_t layer) {
if (combo->layers[0] == -1) {
// -1 in the first layer position is global layer scope
return true;
}
for (int j = 0; j < combo->layers_len; j++) {
if (combo->layers[j] == layer) {
return true;
}
}
return false;
}
static int setup_candidates_for_first_keypress(int32_t position, int64_t timestamp) { static int setup_candidates_for_first_keypress(int32_t position, int64_t timestamp) {
int number_of_combo_candidates = 0;
uint8_t highest_active_layer = zmk_keymap_highest_layer_active();
for (int i = 0; i < CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY; i++) { for (int i = 0; i < CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY; i++) {
struct combo_cfg *combo = combo_lookup[position][i]; struct combo_cfg *combo = combo_lookup[position][i];
if (combo == NULL) { if (combo == NULL) {
return i; return number_of_combo_candidates;
}
if (combo_active_on_layer(combo, highest_active_layer)) {
candidates[number_of_combo_candidates].combo = combo;
candidates[number_of_combo_candidates].timeout_at = timestamp + combo->timeout_ms;
number_of_combo_candidates++;
} }
candidates[i].combo = combo;
candidates[i].timeout_at = timestamp + combo->timeout_ms;
// LOG_DBG("combo timeout %d %d %d", position, i, candidates[i].timeout_at); // LOG_DBG("combo timeout %d %d %d", position, i, candidates[i].timeout_at);
} }
return CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY; return number_of_combo_candidates;
} }
static int filter_candidates(int32_t position) { static int filter_candidates(int32_t position) {
@ -451,6 +472,8 @@ ZMK_SUBSCRIPTION(combo, zmk_position_state_changed);
.behavior = KEY_BINDING_TO_STRUCT(0, n), \ .behavior = KEY_BINDING_TO_STRUCT(0, n), \
.virtual_key_position = ZMK_KEYMAP_LEN + __COUNTER__, \ .virtual_key_position = ZMK_KEYMAP_LEN + __COUNTER__, \
.slow_release = DT_PROP(n, slow_release), \ .slow_release = DT_PROP(n, slow_release), \
.layers = DT_PROP(n, layers), \
.layers_len = DT_PROP_LEN(n, layers), \
}; };
#define INITIALIZE_COMBO(n) initialize_combo(&combo_config_##n); #define INITIALIZE_COMBO(n) initialize_combo(&combo_config_##n);

View file

@ -0,0 +1,2 @@
s/.*hid_listener_keycode_//p
s/.*combo//p

View file

@ -0,0 +1,8 @@
pressed: usage_page 0x07 keycode 0x1b implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0x1b implicit_mods 0x00 explicit_mods 0x00
pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
pressed: usage_page 0x07 keycode 0x1c implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0x1c implicit_mods 0x00 explicit_mods 0x00
pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00

View file

@ -0,0 +1,78 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan-mock.h>
/* it is useful to set timeout to a large value when attaching a debugger. */
#define TIMEOUT (60*60*1000)
/ {
combos {
compatible = "zmk,combos";
combo_one {
timeout-ms = <TIMEOUT>;
key-positions = <0 1>;
bindings = <&kp X>;
layers = <0>;
};
combo_two {
timeout-ms = <TIMEOUT>;
key-positions = <0 1>;
bindings = <&kp Y>;
layers = <1>;
};
combo_three {
timeout-ms = <TIMEOUT>;
key-positions = <0 2>;
bindings = <&kp Z>;
};
};
keymap {
compatible = "zmk,keymap";
label ="Default keymap";
default_layer {
bindings = <
&kp A &kp B
&kp C &tog 1
>;
};
filtered_layer {
bindings = <
&kp A &kp B
&kp C &tog 0
>;
};
};
};
&kscan {
events = <
/* Combo One */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_PRESS(0,1,10)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_RELEASE(0,1,10)
/* Combo Three */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_PRESS(1,1,10)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_RELEASE(1,1,10)
/* Toggle Layer */
ZMK_MOCK_PRESS(1,1,10)
ZMK_MOCK_RELEASE(1,1,10)
/* Combo Two */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_PRESS(0,1,10)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_RELEASE(0,1,10)
/* Combo Three */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_PRESS(1,1,10)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_RELEASE(1,1,10)
>;
};

View file

@ -0,0 +1,2 @@
s/.*hid_listener_keycode_//p
s/.*combo//p

View file

@ -0,0 +1,4 @@
pressed: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
pressed: usage_page 0x07 keycode 0x05 implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0x04 implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0x05 implicit_mods 0x00 explicit_mods 0x00

View file

@ -0,0 +1,40 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan-mock.h>
/* it is useful to set timeout to a large value when attaching a debugger. */
#define TIMEOUT (60*60*1000)
/ {
combos {
compatible = "zmk,combos";
combo_one {
timeout-ms = <TIMEOUT>;
key-positions = <0 1>;
bindings = <&kp X>;
layers = <1>;
};
};
keymap {
compatible = "zmk,keymap";
label ="Default keymap";
default_layer {
bindings = <
&kp A &kp B
&kp C &tog 1
>;
};
};
};
&kscan {
events = <
/* Combo One */
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_PRESS(0,1,10)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_RELEASE(0,1,10)
>;
};

View file

@ -18,6 +18,7 @@ Combos configured in your `.keymap` file, but are separate from the `keymap` nod
timeout-ms = <50>; timeout-ms = <50>;
key-positions = <0 1>; key-positions = <0 1>;
bindings = <&kp ESC>; bindings = <&kp ESC>;
layers = <-1>;
}; };
}; };
}; };
@ -27,6 +28,7 @@ Combos configured in your `.keymap` file, but are separate from the `keymap` nod
- The `compatible` property should always be `"zmk,combos"` for combos. - The `compatible` property should always be `"zmk,combos"` for combos.
- `timeout-ms` is the number of milliseconds that all keys of the combo must be pressed. - `timeout-ms` is the number of milliseconds that all keys of the combo must be pressed.
- `key-positions` is an array of key positions. See the info section below about how to figure out the positions on your board. - `key-positions` is an array of key positions. See the info section below about how to figure out the positions on your board.
- `layers = <0 1...>` will allow limiting a combo to specific layers. this is an _optional_ parameter and defaults to `-1` which is global scope.
- `bindings` is the behavior that is activated when the behavior is pressed. - `bindings` is the behavior that is activated when the behavior is pressed.
- (advanced) you can specify `slow-release` if you want the combo binding to be released when all key-positions are released. The default is to release the combo as soon as any of the keys in the combo is released. - (advanced) you can specify `slow-release` if you want the combo binding to be released when all key-positions are released. The default is to release the combo as soon as any of the keys in the combo is released.
@ -49,3 +51,4 @@ There are three global combo parameters which are set through KConfig. You can s
- `CONFIG_ZMK_COMBO_MAX_PRESSED_COMBOS` is the number of combos that can be active at the same time. Default 4. - `CONFIG_ZMK_COMBO_MAX_PRESSED_COMBOS` is the number of combos that can be active at the same time. Default 4.
- `CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY` is the maximum number of combos that can be active on a key position. Defaults to 5. (So you can have 5 separate combos that use position `3` for example) - `CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY` is the maximum number of combos that can be active on a key position. Defaults to 5. (So you can have 5 separate combos that use position `3` for example)
- `CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO` is the maximum number of keys that need to be pressed to activate a combo. Default 4. If you want a combo that triggers when pressing 5 keys, you'd set this to 5 for example. - `CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO` is the maximum number of keys that need to be pressed to activate a combo. Default 4. If you want a combo that triggers when pressing 5 keys, you'd set this to 5 for example.
- `ZMK_COMBO_MAX_LAYER_FILTERS_PER_COMBO` is the maximum number of layer filters that can be specified. Default 4. If you want use layer filters with a combo that spans more than 4 layers, you'll need to change this value.