From 40b44012649fa0aac428787a80abacf899296c29 Mon Sep 17 00:00:00 2001 From: Nicolas Munnich Date: Mon, 13 Jan 2025 16:35:33 +0100 Subject: [PATCH] feat: Add array of behaviors --- app/CMakeLists.txt | 1 + app/Kconfig.behaviors | 5 ++ .../behaviors/zmk,behavior-array.yaml | 13 +++ app/src/behaviors/behavior_array.c | 79 +++++++++++++++++++ app/tests/array/1-indicies/events.patterns | 1 + .../array/1-indicies/keycode_events.snapshot | 4 + .../array/1-indicies/native_posix_64.keymap | 10 +++ app/tests/array/2a-nesting/events.patterns | 1 + .../array/2a-nesting/keycode_events.snapshot | 4 + .../array/2a-nesting/native_posix_64.keymap | 10 +++ app/tests/array/2b-nesting/events.patterns | 1 + .../array/2b-nesting/keycode_events.snapshot | 4 + .../array/2b-nesting/native_posix_64.keymap | 10 +++ app/tests/array/behavior_keymap.dtsi | 32 ++++++++ .../native_posix_64.keymap | 30 ++----- 15 files changed, 182 insertions(+), 23 deletions(-) create mode 100644 app/dts/bindings/behaviors/zmk,behavior-array.yaml create mode 100644 app/src/behaviors/behavior_array.c create mode 100644 app/tests/array/1-indicies/events.patterns create mode 100644 app/tests/array/1-indicies/keycode_events.snapshot create mode 100644 app/tests/array/1-indicies/native_posix_64.keymap create mode 100644 app/tests/array/2a-nesting/events.patterns create mode 100644 app/tests/array/2a-nesting/keycode_events.snapshot create mode 100644 app/tests/array/2a-nesting/native_posix_64.keymap create mode 100644 app/tests/array/2b-nesting/events.patterns create mode 100644 app/tests/array/2b-nesting/keycode_events.snapshot create mode 100644 app/tests/array/2b-nesting/native_posix_64.keymap create mode 100644 app/tests/array/behavior_keymap.dtsi diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 270fafff30d..7da5b9eedf4 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -65,6 +65,7 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL) target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_MOUSE_KEY_PRESS app PRIVATE src/behaviors/behavior_mouse_key_press.c) target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_STUDIO_UNLOCK app PRIVATE src/behaviors/behavior_studio_unlock.c) target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_INPUT_TWO_AXIS app PRIVATE src/behaviors/behavior_input_two_axis.c) + target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_ARRAY app PRIVATE src/behaviors/behavior_array.c) target_sources(app PRIVATE src/combo.c) target_sources(app PRIVATE src/behaviors/behavior_combo_trigger.c) target_sources(app PRIVATE src/behaviors/behavior_tap_dance.c) diff --git a/app/Kconfig.behaviors b/app/Kconfig.behaviors index 5002bcac4b6..815122a81fe 100644 --- a/app/Kconfig.behaviors +++ b/app/Kconfig.behaviors @@ -122,3 +122,8 @@ config ZMK_BEHAVIOR_MACRO bool default y depends on DT_HAS_ZMK_BEHAVIOR_MACRO_ENABLED || DT_HAS_ZMK_BEHAVIOR_MACRO_ONE_PARAM_ENABLED || DT_HAS_ZMK_BEHAVIOR_MACRO_TWO_PARAM_ENABLED + +config ZMK_BEHAVIOR_ARRAY + bool + default y + depends on DT_HAS_ZMK_BEHAVIOR_ARRAY_ENABLED diff --git a/app/dts/bindings/behaviors/zmk,behavior-array.yaml b/app/dts/bindings/behaviors/zmk,behavior-array.yaml new file mode 100644 index 00000000000..cd227a0ccea --- /dev/null +++ b/app/dts/bindings/behaviors/zmk,behavior-array.yaml @@ -0,0 +1,13 @@ +# Copyright (c) 2025 The ZMK Contributors +# SPDX-License-Identifier: MIT + +description: Array of Behaviors + +compatible: "zmk,behavior-array" + +include: one_param.yaml + +properties: + bindings: + type: phandle-array + required: true diff --git a/app/src/behaviors/behavior_array.c b/app/src/behaviors/behavior_array.c new file mode 100644 index 00000000000..7ccd3f1854f --- /dev/null +++ b/app/src/behaviors/behavior_array.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2025 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_array + +#include +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + +struct behavior_array_config { + size_t behavior_count; + struct zmk_behavior_binding *behaviors; +}; + +static int on_array_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + const struct device *dev = zmk_behavior_get_binding(binding->behavior_dev); + const struct behavior_array_config *cfg = dev->config; + int index = binding->param1; + + if (index >= cfg->behavior_count || index < 0) { + LOG_ERR("Trying to trigger an index beyond the size of the behavior array."); + return -ENOTSUP; + } + return zmk_behavior_invoke_binding((struct zmk_behavior_binding *)&cfg->behaviors[index], event, + true); +} + +static int on_array_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + const struct device *dev = zmk_behavior_get_binding(binding->behavior_dev); + const struct behavior_array_config *cfg = dev->config; + int index = binding->param1; + + if (index >= cfg->behavior_count || index < 0) { + LOG_ERR("Trying to trigger an index beyond the size of the behavior array."); + return -ENOTSUP; + } + return zmk_behavior_invoke_binding((struct zmk_behavior_binding *)&cfg->behaviors[index], event, + false); +} + +static const struct behavior_driver_api behavior_array_driver_api = { + .binding_pressed = on_array_binding_pressed, + .binding_released = on_array_binding_released, +#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA) + .get_parameter_metadata = zmk_behavior_get_empty_param_metadata, +#endif // IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA) +}; + +static int behavior_array_init(const struct device *dev) { return 0; } + +#define _TRANSFORM_ENTRY(idx, node) ZMK_KEYMAP_EXTRACT_BINDING(idx, node) + +#define TRANSFORMED_BINDINGS(node) \ + {LISTIFY(DT_INST_PROP_LEN(node, bindings), _TRANSFORM_ENTRY, (, ), DT_DRV_INST(node))} + +#define ARR_INST(n) \ + static struct zmk_behavior_binding \ + behavior_array_config_##n##_bindings[DT_INST_PROP_LEN(n, bindings)] = \ + TRANSFORMED_BINDINGS(n); \ + static struct behavior_array_config behavior_array_config_##n = { \ + .behaviors = behavior_array_config_##n##_bindings, \ + .behavior_count = DT_INST_PROP_LEN(n, bindings)}; \ + BEHAVIOR_DT_INST_DEFINE(n, behavior_array_init, NULL, NULL, &behavior_array_config_##n, \ + POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + &behavior_array_driver_api); + +DT_INST_FOREACH_STATUS_OKAY(ARR_INST) + +#endif diff --git a/app/tests/array/1-indicies/events.patterns b/app/tests/array/1-indicies/events.patterns new file mode 100644 index 00000000000..833100f6ac4 --- /dev/null +++ b/app/tests/array/1-indicies/events.patterns @@ -0,0 +1 @@ +s/.*hid_listener_keycode_//p \ No newline at end of file diff --git a/app/tests/array/1-indicies/keycode_events.snapshot b/app/tests/array/1-indicies/keycode_events.snapshot new file mode 100644 index 00000000000..b809163ddca --- /dev/null +++ b/app/tests/array/1-indicies/keycode_events.snapshot @@ -0,0 +1,4 @@ +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 0x05 implicit_mods 0x00 explicit_mods 0x00 +released: usage_page 0x07 keycode 0x05 implicit_mods 0x00 explicit_mods 0x00 diff --git a/app/tests/array/1-indicies/native_posix_64.keymap b/app/tests/array/1-indicies/native_posix_64.keymap new file mode 100644 index 00000000000..eed210634e0 --- /dev/null +++ b/app/tests/array/1-indicies/native_posix_64.keymap @@ -0,0 +1,10 @@ +#include "../behavior_keymap.dtsi" + +&kscan { + events = < + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_RELEASE(0,0,10) + ZMK_MOCK_PRESS(0,1,10) + ZMK_MOCK_RELEASE(0,1,10) + >; +}; \ No newline at end of file diff --git a/app/tests/array/2a-nesting/events.patterns b/app/tests/array/2a-nesting/events.patterns new file mode 100644 index 00000000000..833100f6ac4 --- /dev/null +++ b/app/tests/array/2a-nesting/events.patterns @@ -0,0 +1 @@ +s/.*hid_listener_keycode_//p \ No newline at end of file diff --git a/app/tests/array/2a-nesting/keycode_events.snapshot b/app/tests/array/2a-nesting/keycode_events.snapshot new file mode 100644 index 00000000000..ad11d37958c --- /dev/null +++ b/app/tests/array/2a-nesting/keycode_events.snapshot @@ -0,0 +1,4 @@ +pressed: usage_page 0x07 keycode 0x29 implicit_mods 0x00 explicit_mods 0x00 +released: usage_page 0x07 keycode 0x29 implicit_mods 0x00 explicit_mods 0x00 +pressed: usage_page 0x07 keycode 0x06 implicit_mods 0x00 explicit_mods 0x00 +released: usage_page 0x07 keycode 0x06 implicit_mods 0x00 explicit_mods 0x00 diff --git a/app/tests/array/2a-nesting/native_posix_64.keymap b/app/tests/array/2a-nesting/native_posix_64.keymap new file mode 100644 index 00000000000..a9e61e5626c --- /dev/null +++ b/app/tests/array/2a-nesting/native_posix_64.keymap @@ -0,0 +1,10 @@ +#include "../behavior_keymap.dtsi" + +&kscan { + events = < + ZMK_MOCK_PRESS(1,0,10) + ZMK_MOCK_RELEASE(1,0,10) + ZMK_MOCK_PRESS(1,0,500) + ZMK_MOCK_RELEASE(1,0,10) + >; +}; \ No newline at end of file diff --git a/app/tests/array/2b-nesting/events.patterns b/app/tests/array/2b-nesting/events.patterns new file mode 100644 index 00000000000..833100f6ac4 --- /dev/null +++ b/app/tests/array/2b-nesting/events.patterns @@ -0,0 +1 @@ +s/.*hid_listener_keycode_//p \ No newline at end of file diff --git a/app/tests/array/2b-nesting/keycode_events.snapshot b/app/tests/array/2b-nesting/keycode_events.snapshot new file mode 100644 index 00000000000..cc718d3de70 --- /dev/null +++ b/app/tests/array/2b-nesting/keycode_events.snapshot @@ -0,0 +1,4 @@ +pressed: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00 +pressed: usage_page 0x07 keycode 0x35 implicit_mods 0x00 explicit_mods 0x00 +released: usage_page 0x07 keycode 0x35 implicit_mods 0x00 explicit_mods 0x00 +released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00 diff --git a/app/tests/array/2b-nesting/native_posix_64.keymap b/app/tests/array/2b-nesting/native_posix_64.keymap new file mode 100644 index 00000000000..c68f894c6fd --- /dev/null +++ b/app/tests/array/2b-nesting/native_posix_64.keymap @@ -0,0 +1,10 @@ +#include "../behavior_keymap.dtsi" + +&kscan { + events = < + ZMK_MOCK_PRESS(0,0,10) + ZMK_MOCK_PRESS(1,1,10) + ZMK_MOCK_RELEASE(1,1,10) + ZMK_MOCK_RELEASE(0,0,10) + >; +}; \ No newline at end of file diff --git a/app/tests/array/behavior_keymap.dtsi b/app/tests/array/behavior_keymap.dtsi new file mode 100644 index 00000000000..5747cc83642 --- /dev/null +++ b/app/tests/array/behavior_keymap.dtsi @@ -0,0 +1,32 @@ +#include +#include +#include + +/ { + behaviors { + arr: behavior_array { + compatible = "zmk,behavior-array"; + #binding-cells = <1>; + bindings = <&mt LEFT_SHIFT A &kp B &kp C &gresc>; + }; + + ht_bal: behavior_hold_tap_balanced { + compatible = "zmk,behavior-hold-tap"; + #binding-cells = <2>; + flavor = "balanced"; + tapping-term-ms = <200>; + bindings = <&arr>, <&arr>; + }; + }; + + keymap { + compatible = "zmk,keymap"; + + default_layer { + bindings = < + &arr 0 &arr 1 + &ht_bal 2 3 &arr 3 + >; + }; + }; +}; diff --git a/app/tests/combo/combos-and-holdtaps-2/native_posix_64.keymap b/app/tests/combo/combos-and-holdtaps-2/native_posix_64.keymap index 26f57ac3cde..b2281f67333 100644 --- a/app/tests/combo/combos-and-holdtaps-2/native_posix_64.keymap +++ b/app/tests/combo/combos-and-holdtaps-2/native_posix_64.keymap @@ -24,29 +24,16 @@ }; }; - macros { - ZMK_MACRO(combo1_macro, bindings - = <¯o_press &mt LEFT_CONTROL A> - , <¯o_pause_for_release> - , <¯o_release &mt LEFT_CONTROL A>; - ) - ZMK_MACRO(combo2_macro, bindings - = <¯o_press &mt RIGHT_CONTROL B> - , <¯o_pause_for_release> - , <¯o_release &mt RIGHT_CONTROL B>; - ) - }; - behaviors { - combo1: combo_trigger1{ - compatible = "zmk,behavior-combo-trigger"; - #binding-cells = <2>; - fallback-behavior = <&combo1_macro>; + arr: behavior_array { + compatible = "zmk,behavior-array"; + #binding-cells = <1>; + bindings = <&mt LEFT_CONTROL A &mt RIGHT_CONTROL B &none>; }; - combo2: combo_trigger2{ + combo: combo_trigger1{ compatible = "zmk,behavior-combo-trigger"; #binding-cells = <2>; - fallback-behavior = <&combo2_macro>; + fallback-behavior = <&arr>; }; }; @@ -55,16 +42,13 @@ default_layer { bindings = < - &combo1 0 0 &combo2 1 0 &none &none + &combo 0 0 &combo 1 1 &combo 2 2 &combo 3 2 >; }; }; }; &kscan { - // There is an issue with the test setup where it exits - // before timeout of the second press - // The third events = < ZMK_MOCK_PRESS(0,0,0) ZMK_MOCK_PRESS(0,1,300)