forked from zmkfirmware/zmk
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pointing): Add behavior input processor
Add the ability to intercept certain input events and trigger behaviors when they occur. Co-authored-by: Jorge Villalobos <[email protected]>
- Loading branch information
1 parent
4d7cad3
commit 64c4ba5
Showing
21 changed files
with
403 additions
and
20 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
app/dts/bindings/input_processors/zmk,input-processor-behaviors.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright (c) 2024, The ZMK Contributors | ||
# SPDX-License-Identifier: MIT | ||
|
||
description: Input Processor for invoking behaviors on certain events | ||
|
||
compatible: "zmk,input-processor-behaviors" | ||
|
||
include: ip_zero_param.yaml | ||
|
||
properties: | ||
type: | ||
type: int | ||
codes: | ||
type: array | ||
required: true | ||
bindings: | ||
type: phandle-array | ||
required: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright (c) 2024 The ZMK Contributors | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
#include <zephyr/dt-bindings/input/input-event-codes.h> | ||
|
||
/ { | ||
zip_button_behaviors: zip_button_behaviors { | ||
compatible = "zmk,input-processor-behaviors"; | ||
#input-processor-cells = <0>; | ||
codes = <INPUT_BTN_0 INPUT_BTN_1 INPUT_BTN_2>; | ||
bindings = <&none &none &none>; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright (c) 2020 The ZMK Contributors | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <zephyr/devicetree.h> | ||
|
||
#define ZMK_COMBOS_UTIL_ONE(n) 1 | ||
|
||
#define ZMK_COMBOS_LEN \ | ||
COND_CODE_1( \ | ||
DT_HAS_COMPAT_STATUS_OKAY(zmk_combos), \ | ||
(DT_FOREACH_CHILD_STATUS_OKAY_SEP(DT_INST(0, zmk_combos), ZMK_COMBOS_UTIL_ONE, (+))), (0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright (c) 2020 The ZMK Contributors | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <zephyr/devicetree.h> | ||
|
||
#define ZMK_INPUT_LISTENERS_UTIL_ONE(n) 1 + | ||
|
||
#define ZMK_INPUT_LISTENERS_LEN \ | ||
(DT_FOREACH_STATUS_OKAY(zmk_input_listener, ZMK_INPUT_LISTENERS_UTIL_ONE) 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2024 The ZMK Contributors | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
#define DT_DRV_COMPAT zmk_input_processor_behaviors | ||
|
||
#include <zephyr/dt-bindings/input/input-event-codes.h> | ||
|
||
#include <zephyr/kernel.h> | ||
#include <zephyr/device.h> | ||
#include <drivers/input_processor.h> | ||
|
||
#include <zephyr/logging/log.h> | ||
|
||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); | ||
|
||
#include <zmk/keymap.h> | ||
#include <zmk/behavior.h> | ||
#include <zmk/virtual_key_position.h> | ||
|
||
struct ip_behaviors_config { | ||
uint8_t index; | ||
size_t size; | ||
uint16_t type; | ||
|
||
const uint16_t *codes; | ||
const struct zmk_behavior_binding *bindings; | ||
}; | ||
|
||
static int ip_behaviors_handle_event(const struct device *dev, struct input_event *event, | ||
uint32_t param1, uint32_t param2, | ||
struct zmk_input_processor_state *state) { | ||
const struct ip_behaviors_config *cfg = dev->config; | ||
|
||
if (event->type != cfg->type) { | ||
return 0; | ||
} | ||
|
||
for (size_t i = 0; i < cfg->size; i++) { | ||
if (cfg->codes[i] == event->code) { | ||
struct zmk_behavior_binding_event behavior_event = { | ||
.position = ZMK_VIRTUAL_KEY_POSITION_BEHAVIOR_INPUT_PROCESSOR( | ||
state->input_device_index, cfg->index), | ||
.timestamp = k_uptime_get(), | ||
#if IS_ENABLED(CONFIG_ZMK_SPLIT) | ||
.source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, | ||
#endif | ||
}; | ||
|
||
LOG_DBG("FOUND A MATCHING CODE, invoke %s for position %d with %d listeners", | ||
cfg->bindings[i].behavior_dev, behavior_event.position, | ||
ZMK_INPUT_LISTENERS_LEN); | ||
int ret = zmk_behavior_invoke_binding(&cfg->bindings[i], behavior_event, event->value); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
|
||
return ZMK_INPUT_PROC_STOP; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static struct zmk_input_processor_driver_api ip_behaviors_driver_api = { | ||
.handle_event = ip_behaviors_handle_event, | ||
}; | ||
|
||
static int ip_behaviors_init(const struct device *dev) { return 0; } | ||
|
||
#define ENTRY(i, node) ZMK_KEYMAP_EXTRACT_BINDING(i, node) | ||
|
||
#define IP_BEHAVIORS_INST(n) \ | ||
static const uint16_t ip_behaviors_codes_##n[] = DT_INST_PROP(n, codes); \ | ||
static const struct zmk_behavior_binding ip_behaviors_bindings_##n[] = { \ | ||
LISTIFY(DT_INST_PROP_LEN(n, bindings), ZMK_KEYMAP_EXTRACT_BINDING, (, ), DT_DRV_INST(n))}; \ | ||
BUILD_ASSERT(ARRAY_SIZE(ip_behaviors_codes_##n) == ARRAY_SIZE(ip_behaviors_bindings_##n), \ | ||
"codes and bindings need to be the same length"); \ | ||
static const struct ip_behaviors_config ip_behaviors_config_##n = { \ | ||
.index = n, \ | ||
.type = DT_INST_PROP_OR(n, type, INPUT_EV_KEY), \ | ||
.size = DT_INST_PROP_LEN(n, codes), \ | ||
.codes = ip_behaviors_codes_##n, \ | ||
.bindings = ip_behaviors_bindings_##n, \ | ||
}; \ | ||
DEVICE_DT_INST_DEFINE(n, &ip_behaviors_init, NULL, NULL, &ip_behaviors_config_##n, \ | ||
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ | ||
&ip_behaviors_driver_api); | ||
|
||
DT_INST_FOREACH_STATUS_OKAY(IP_BEHAVIORS_INST) |
2 changes: 2 additions & 0 deletions
2
app/tests/pointing/mouse-move/processors/behaviors_basic/events.patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
s/.*hid_mouse_//p | ||
s/.*hid_listener_keycode_//p |
6 changes: 6 additions & 0 deletions
6
app/tests/pointing/mouse-move/processors/behaviors_basic/keycode_events.snapshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
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 | ||
pressed: usage_page 0x07 keycode 0x06 implicit_mods 0x00 explicit_mods 0x00 | ||
released: usage_page 0x07 keycode 0x06 implicit_mods 0x00 explicit_mods 0x00 |
6 changes: 6 additions & 0 deletions
6
app/tests/pointing/mouse-move/processors/behaviors_basic/native_posix_64.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CONFIG_GPIO=n | ||
CONFIG_ZMK_BLE=n | ||
CONFIG_LOG=y | ||
CONFIG_LOG_BACKEND_SHOW_COLOR=n | ||
CONFIG_ZMK_LOG_LEVEL_DBG=y | ||
CONFIG_ZMK_POINTING=y |
44 changes: 44 additions & 0 deletions
44
app/tests/pointing/mouse-move/processors/behaviors_basic/native_posix_64.keymap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
#include <dt-bindings/zmk/input_transform.h> | ||
#include <zephyr/dt-bindings/input/input-event-codes.h> | ||
|
||
#include <behaviors.dtsi> | ||
#include <input/processors.dtsi> | ||
#include <dt-bindings/zmk/keys.h> | ||
#include <dt-bindings/zmk/kscan_mock.h> | ||
#include <dt-bindings/zmk/pointing.h> | ||
|
||
|
||
&zip_button_behaviors { | ||
bindings = <&kp A &kp B &kp C>; | ||
}; | ||
|
||
&mkp_input_listener { | ||
input-processors = <&zip_button_behaviors>; | ||
}; | ||
|
||
/ { | ||
keymap { | ||
compatible = "zmk,keymap"; | ||
label ="Default keymap"; | ||
|
||
default_layer { | ||
bindings = < | ||
&mkp LCLK &mkp RCLK | ||
&mkp MCLK &none | ||
>; | ||
}; | ||
}; | ||
}; | ||
|
||
|
||
&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) | ||
ZMK_MOCK_PRESS(1,0,10) | ||
ZMK_MOCK_RELEASE(1,0,10) | ||
>; | ||
}; |
2 changes: 2 additions & 0 deletions
2
app/tests/pointing/mouse-move/processors/behaviors_hold_tap/events.patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
s/.*hid_mouse_//p | ||
s/.*hid_listener_keycode_//p |
16 changes: 16 additions & 0 deletions
16
app/tests/pointing/mouse-move/processors/behaviors_hold_tap/keycode_events.snapshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
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 | ||
pressed: usage_page 0x07 keycode 0x06 implicit_mods 0x00 explicit_mods 0x00 | ||
released: usage_page 0x07 keycode 0x06 implicit_mods 0x00 explicit_mods 0x00 | ||
pressed: usage_page 0x07 keycode 0xE0 implicit_mods 0x00 explicit_mods 0x00 | ||
released: usage_page 0x07 keycode 0xE0 implicit_mods 0x00 explicit_mods 0x00 | ||
pressed: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00 | ||
released: usage_page 0x07 keycode 0xE1 implicit_mods 0x00 explicit_mods 0x00 | ||
pressed: usage_page 0x07 keycode 0xE2 implicit_mods 0x00 explicit_mods 0x00 | ||
released: usage_page 0x07 keycode 0xE2 implicit_mods 0x00 explicit_mods 0x00 | ||
pressed: usage_page 0x07 keycode 0xE0 implicit_mods 0x00 explicit_mods 0x00 | ||
pressed: usage_page 0x07 keycode 0x05 implicit_mods 0x00 explicit_mods 0x00 | ||
released: usage_page 0x07 keycode 0xE0 implicit_mods 0x00 explicit_mods 0x00 | ||
released: usage_page 0x07 keycode 0x05 implicit_mods 0x00 explicit_mods 0x00 |
6 changes: 6 additions & 0 deletions
6
app/tests/pointing/mouse-move/processors/behaviors_hold_tap/native_posix_64.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CONFIG_GPIO=n | ||
CONFIG_ZMK_BLE=n | ||
CONFIG_LOG=y | ||
CONFIG_LOG_BACKEND_SHOW_COLOR=n | ||
CONFIG_ZMK_LOG_LEVEL_DBG=y | ||
CONFIG_ZMK_POINTING=y |
Oops, something went wrong.