refactor: Move zmk_invoke_behavior_binding to behavior.c
This commit is contained in:
parent
dfd8733f74
commit
d21ba18f0e
7 changed files with 81 additions and 73 deletions
|
@ -43,6 +43,19 @@ struct zmk_behavior_binding_event {
|
||||||
*/
|
*/
|
||||||
const struct device *zmk_behavior_get_binding(const char *name);
|
const struct device *zmk_behavior_get_binding(const char *name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Invoke a behavior given its binding and invoking event details.
|
||||||
|
*
|
||||||
|
* @param src_binding Behavior binding to invoke.
|
||||||
|
* @param event The binding event struct containing details of the event that invoked it.
|
||||||
|
* @param pressed Whether the binding is pressed or released.
|
||||||
|
*
|
||||||
|
* @retval 0 If successful.
|
||||||
|
* @retval Negative errno code if failure.
|
||||||
|
*/
|
||||||
|
int zmk_invoke_behavior_binding(const struct zmk_behavior_binding *src_binding,
|
||||||
|
struct zmk_behavior_binding_event event, bool pressed);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get a local ID for a behavior from its @p name field.
|
* @brief Get a local ID for a behavior from its @p name field.
|
||||||
*
|
*
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <zmk/events/position_state_changed.h>
|
#include <zmk/events/position_state_changed.h>
|
||||||
#include <zmk/behavior.h>
|
|
||||||
|
|
||||||
#define ZMK_LAYER_CHILD_LEN_PLUS_ONE(node) 1 +
|
#define ZMK_LAYER_CHILD_LEN_PLUS_ONE(node) 1 +
|
||||||
#define ZMK_KEYMAP_LAYERS_LEN \
|
#define ZMK_KEYMAP_LAYERS_LEN \
|
||||||
|
@ -28,9 +27,6 @@ const char *zmk_keymap_layer_name(uint8_t layer);
|
||||||
int zmk_keymap_position_state_changed(uint8_t source, uint32_t position, bool pressed,
|
int zmk_keymap_position_state_changed(uint8_t source, uint32_t position, bool pressed,
|
||||||
int64_t timestamp);
|
int64_t timestamp);
|
||||||
|
|
||||||
int zmk_invoke_behavior_binding(const struct zmk_behavior_binding *src_binding,
|
|
||||||
struct zmk_behavior_binding_event event, bool pressed);
|
|
||||||
|
|
||||||
#define ZMK_KEYMAP_EXTRACT_BINDING(idx, drv_inst) \
|
#define ZMK_KEYMAP_EXTRACT_BINDING(idx, drv_inst) \
|
||||||
{ \
|
{ \
|
||||||
.behavior_dev = DEVICE_DT_NAME(DT_PHANDLE_BY_IDX(drv_inst, bindings, idx)), \
|
.behavior_dev = DEVICE_DT_NAME(DT_PHANDLE_BY_IDX(drv_inst, bindings, idx)), \
|
||||||
|
|
|
@ -17,11 +17,18 @@
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <zmk/ble.h>
|
||||||
|
#if ZMK_BLE_IS_CENTRAL
|
||||||
|
#include <zmk/split/bluetooth/central.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <drivers/behavior.h>
|
#include <drivers/behavior.h>
|
||||||
#include <zmk/behavior.h>
|
#include <zmk/behavior.h>
|
||||||
#include <zmk/hid.h>
|
#include <zmk/hid.h>
|
||||||
#include <zmk/matrix.h>
|
#include <zmk/matrix.h>
|
||||||
|
|
||||||
|
#include <zmk/events/position_state_changed.h>
|
||||||
|
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
|
@ -49,6 +56,66 @@ const struct device *z_impl_behavior_get_binding(const char *name) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int invoke_locally(struct zmk_behavior_binding *binding,
|
||||||
|
struct zmk_behavior_binding_event event, bool pressed) {
|
||||||
|
if (pressed) {
|
||||||
|
return behavior_keymap_binding_pressed(binding, event);
|
||||||
|
} else {
|
||||||
|
return behavior_keymap_binding_released(binding, event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int zmk_invoke_behavior_binding(const struct zmk_behavior_binding *src_binding,
|
||||||
|
struct zmk_behavior_binding_event event, bool pressed) {
|
||||||
|
// We want to make a copy of this, since it may be converted from
|
||||||
|
// relative to absolute before being invoked
|
||||||
|
struct zmk_behavior_binding binding = *src_binding;
|
||||||
|
|
||||||
|
const struct device *behavior = zmk_behavior_get_binding(binding.behavior_dev);
|
||||||
|
|
||||||
|
if (!behavior) {
|
||||||
|
LOG_WRN("No behavior assigned to %d on layer %d", event.position, event.layer);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int err = behavior_keymap_binding_convert_central_state_dependent_params(&binding, event);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("Failed to convert relative to absolute behavior binding (err %d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum behavior_locality locality = BEHAVIOR_LOCALITY_CENTRAL;
|
||||||
|
err = behavior_get_locality(behavior, &locality);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("Failed to get behavior locality %d", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (locality) {
|
||||||
|
case BEHAVIOR_LOCALITY_CENTRAL:
|
||||||
|
return invoke_locally(&binding, event, pressed);
|
||||||
|
case BEHAVIOR_LOCALITY_EVENT_SOURCE:
|
||||||
|
#if ZMK_BLE_IS_CENTRAL
|
||||||
|
if (event.source == ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL) {
|
||||||
|
return invoke_locally(&binding, event, pressed);
|
||||||
|
} else {
|
||||||
|
return zmk_split_bt_invoke_behavior(event.source, &binding, event, pressed);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
return invoke_locally(&binding, event, pressed);
|
||||||
|
#endif
|
||||||
|
case BEHAVIOR_LOCALITY_GLOBAL:
|
||||||
|
#if ZMK_BLE_IS_CENTRAL
|
||||||
|
for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) {
|
||||||
|
zmk_split_bt_invoke_behavior(i, &binding, event, pressed);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return invoke_locally(&binding, event, pressed);
|
||||||
|
}
|
||||||
|
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
|
|
||||||
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
|
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
|
||||||
|
|
||||||
int zmk_behavior_get_empty_param_metadata(const struct device *dev,
|
int zmk_behavior_get_empty_param_metadata(const struct device *dev,
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <zmk/behavior_queue.h>
|
#include <zmk/behavior_queue.h>
|
||||||
#include <zmk/keymap.h>
|
#include <zmk/behavior.h>
|
||||||
|
|
||||||
#include <zephyr/kernel.h>
|
#include <zephyr/kernel.h>
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
#include <zmk/events/position_state_changed.h>
|
#include <zmk/events/position_state_changed.h>
|
||||||
#include <zmk/events/keycode_state_changed.h>
|
#include <zmk/events/keycode_state_changed.h>
|
||||||
#include <zmk/behavior.h>
|
#include <zmk/behavior.h>
|
||||||
#include <zmk/keymap.h>
|
|
||||||
|
|
||||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
#include <drivers/behavior.h>
|
#include <drivers/behavior.h>
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
#include <zmk/behavior.h>
|
#include <zmk/behavior.h>
|
||||||
#include <zmk/keymap.h>
|
|
||||||
|
|
||||||
#include <zmk/matrix.h>
|
#include <zmk/matrix.h>
|
||||||
#include <zmk/endpoints.h>
|
#include <zmk/endpoints.h>
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
#include <drivers/behavior.h>
|
#include <drivers/behavior.h>
|
||||||
#include <zephyr/sys/util.h>
|
#include <zephyr/sys/util.h>
|
||||||
#include <zephyr/bluetooth/bluetooth.h>
|
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
|
@ -16,11 +15,6 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
#include <zmk/sensors.h>
|
#include <zmk/sensors.h>
|
||||||
#include <zmk/virtual_key_position.h>
|
#include <zmk/virtual_key_position.h>
|
||||||
|
|
||||||
#include <zmk/ble.h>
|
|
||||||
#if ZMK_BLE_IS_CENTRAL
|
|
||||||
#include <zmk/split/bluetooth/central.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <zmk/event_manager.h>
|
#include <zmk/event_manager.h>
|
||||||
#include <zmk/events/position_state_changed.h>
|
#include <zmk/events/position_state_changed.h>
|
||||||
#include <zmk/events/layer_state_changed.h>
|
#include <zmk/events/layer_state_changed.h>
|
||||||
|
@ -163,15 +157,6 @@ const char *zmk_keymap_layer_name(uint8_t layer) {
|
||||||
return zmk_keymap_layer_names[layer];
|
return zmk_keymap_layer_names[layer];
|
||||||
}
|
}
|
||||||
|
|
||||||
int invoke_locally(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event,
|
|
||||||
bool pressed) {
|
|
||||||
if (pressed) {
|
|
||||||
return behavior_keymap_binding_pressed(binding, event);
|
|
||||||
} else {
|
|
||||||
return behavior_keymap_binding_released(binding, event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int zmk_keymap_apply_position_state(uint8_t source, int layer, uint32_t position, bool pressed,
|
int zmk_keymap_apply_position_state(uint8_t source, int layer, uint32_t position, bool pressed,
|
||||||
int64_t timestamp) {
|
int64_t timestamp) {
|
||||||
struct zmk_behavior_binding *binding = &zmk_keymap[layer][position];
|
struct zmk_behavior_binding *binding = &zmk_keymap[layer][position];
|
||||||
|
@ -187,57 +172,6 @@ int zmk_keymap_apply_position_state(uint8_t source, int layer, uint32_t position
|
||||||
return zmk_invoke_behavior_binding(binding, event, pressed);
|
return zmk_invoke_behavior_binding(binding, event, pressed);
|
||||||
}
|
}
|
||||||
|
|
||||||
int zmk_invoke_behavior_binding(const struct zmk_behavior_binding *src_binding,
|
|
||||||
struct zmk_behavior_binding_event event, bool pressed) {
|
|
||||||
// We want to make a copy of this, since it may be converted from
|
|
||||||
// relative to absolute before being invoked
|
|
||||||
struct zmk_behavior_binding binding = *src_binding;
|
|
||||||
|
|
||||||
const struct device *behavior = zmk_behavior_get_binding(binding.behavior_dev);
|
|
||||||
|
|
||||||
if (!behavior) {
|
|
||||||
LOG_WRN("No behavior assigned to %d on layer %d", event.position, event.layer);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int err = behavior_keymap_binding_convert_central_state_dependent_params(&binding, event);
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("Failed to convert relative to absolute behavior binding (err %d)", err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
enum behavior_locality locality = BEHAVIOR_LOCALITY_CENTRAL;
|
|
||||||
err = behavior_get_locality(behavior, &locality);
|
|
||||||
if (err) {
|
|
||||||
LOG_ERR("Failed to get behavior locality %d", err);
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (locality) {
|
|
||||||
case BEHAVIOR_LOCALITY_CENTRAL:
|
|
||||||
return invoke_locally(&binding, event, pressed);
|
|
||||||
case BEHAVIOR_LOCALITY_EVENT_SOURCE:
|
|
||||||
#if ZMK_BLE_IS_CENTRAL
|
|
||||||
if (event.source == ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL) {
|
|
||||||
return invoke_locally(&binding, event, pressed);
|
|
||||||
} else {
|
|
||||||
return zmk_split_bt_invoke_behavior(event.source, &binding, event, pressed);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
return invoke_locally(&binding, event, pressed);
|
|
||||||
#endif
|
|
||||||
case BEHAVIOR_LOCALITY_GLOBAL:
|
|
||||||
#if ZMK_BLE_IS_CENTRAL
|
|
||||||
for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) {
|
|
||||||
zmk_split_bt_invoke_behavior(i, &binding, event, pressed);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return invoke_locally(&binding, event, pressed);
|
|
||||||
}
|
|
||||||
|
|
||||||
return -ENOTSUP;
|
|
||||||
}
|
|
||||||
|
|
||||||
int zmk_keymap_position_state_changed(uint8_t source, uint32_t position, bool pressed,
|
int zmk_keymap_position_state_changed(uint8_t source, uint32_t position, bool pressed,
|
||||||
int64_t timestamp) {
|
int64_t timestamp) {
|
||||||
if (pressed) {
|
if (pressed) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue