refactor(behaviors): use behavior_state_changed event in behavior bindings
This commit is contained in:
parent
65de1038ae
commit
578e6a0417
15 changed files with 113 additions and 163 deletions
|
@ -11,6 +11,7 @@
|
||||||
#include <device.h>
|
#include <device.h>
|
||||||
#include <zmk/keys.h>
|
#include <zmk/keys.h>
|
||||||
#include <zmk/behavior.h>
|
#include <zmk/behavior.h>
|
||||||
|
#include <zmk/events/behavior_state_changed.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @cond INTERNAL_HIDDEN
|
* @cond INTERNAL_HIDDEN
|
||||||
|
@ -20,8 +21,7 @@
|
||||||
* (Internal use only.)
|
* (Internal use only.)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef int (*behavior_keymap_binding_callback_t)(struct zmk_behavior_binding *binding,
|
typedef int (*behavior_keymap_binding_callback_t)(const struct behavior_state_changed *ev);
|
||||||
struct zmk_behavior_binding_event event);
|
|
||||||
typedef int (*behavior_sensor_keymap_binding_callback_t)(struct zmk_behavior_binding *binding,
|
typedef int (*behavior_sensor_keymap_binding_callback_t)(struct zmk_behavior_binding *binding,
|
||||||
const struct device *sensor,
|
const struct device *sensor,
|
||||||
int64_t timestamp);
|
int64_t timestamp);
|
||||||
|
@ -44,19 +44,18 @@ __subsystem struct behavior_driver_api {
|
||||||
* @retval 0 If successful.
|
* @retval 0 If successful.
|
||||||
* @retval Negative errno code if failure.
|
* @retval Negative errno code if failure.
|
||||||
*/
|
*/
|
||||||
__syscall int behavior_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
__syscall int behavior_keymap_binding_pressed(const struct behavior_state_changed *ev);
|
||||||
struct zmk_behavior_binding_event event);
|
|
||||||
|
|
||||||
static inline int z_impl_behavior_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static inline int
|
||||||
struct zmk_behavior_binding_event event) {
|
z_impl_behavior_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
const struct device *dev = device_get_binding(binding->behavior_dev);
|
const struct device *dev = device_get_binding(event->behavior_dev);
|
||||||
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api;
|
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api;
|
||||||
|
|
||||||
if (api->binding_pressed == NULL) {
|
if (api->binding_pressed == NULL) {
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
return api->binding_pressed(binding, event);
|
return api->binding_pressed(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,19 +66,18 @@ static inline int z_impl_behavior_keymap_binding_pressed(struct zmk_behavior_bin
|
||||||
* @retval 0 If successful.
|
* @retval 0 If successful.
|
||||||
* @retval Negative errno code if failure.
|
* @retval Negative errno code if failure.
|
||||||
*/
|
*/
|
||||||
__syscall int behavior_keymap_binding_released(struct zmk_behavior_binding *binding,
|
__syscall int behavior_keymap_binding_released(const struct behavior_state_changed *ev);
|
||||||
struct zmk_behavior_binding_event event);
|
|
||||||
|
|
||||||
static inline int z_impl_behavior_keymap_binding_released(struct zmk_behavior_binding *binding,
|
static inline int
|
||||||
struct zmk_behavior_binding_event event) {
|
z_impl_behavior_keymap_binding_released(const struct behavior_state_changed *event) {
|
||||||
const struct device *dev = device_get_binding(binding->behavior_dev);
|
const struct device *dev = device_get_binding(event->behavior_dev);
|
||||||
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api;
|
const struct behavior_driver_api *api = (const struct behavior_driver_api *)dev->api;
|
||||||
|
|
||||||
if (api->binding_released == NULL) {
|
if (api->binding_released == NULL) {
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
return api->binding_released(binding, event);
|
return api->binding_released(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -20,21 +20,11 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
#include <zmk/events/sensor_event.h>
|
#include <zmk/events/sensor_event.h>
|
||||||
|
|
||||||
int zmk_behavior_state_changed(const struct behavior_state_changed *ev) {
|
int zmk_behavior_state_changed(const struct behavior_state_changed *ev) {
|
||||||
struct zmk_behavior_binding binding = {
|
|
||||||
.behavior_dev = ev->behavior_dev,
|
|
||||||
.param1 = ev->param1,
|
|
||||||
.param2 = ev->param2,
|
|
||||||
};
|
|
||||||
const struct zmk_behavior_binding_event event = {
|
|
||||||
.layer = ev->layer,
|
|
||||||
.position = ev->position,
|
|
||||||
.timestamp = ev->timestamp,
|
|
||||||
};
|
|
||||||
int ret;
|
int ret;
|
||||||
if (ev->pressed) {
|
if (ev->pressed) {
|
||||||
ret = behavior_keymap_binding_pressed(&binding, event);
|
ret = behavior_keymap_binding_pressed(ev);
|
||||||
} else {
|
} else {
|
||||||
ret = behavior_keymap_binding_released(&binding, event);
|
ret = behavior_keymap_binding_released(ev);
|
||||||
}
|
}
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -51,9 +41,10 @@ int zmk_behavior_state_changed(const struct behavior_state_changed *ev) {
|
||||||
|
|
||||||
int behavior_listener(const struct zmk_event_header *eh) {
|
int behavior_listener(const struct zmk_event_header *eh) {
|
||||||
if (is_behavior_state_changed(eh)) {
|
if (is_behavior_state_changed(eh)) {
|
||||||
const struct behavior_state_changed *ev = cast_position_state_changed(eh);
|
const struct behavior_state_changed *ev = cast_behavior_state_changed(eh);
|
||||||
return zmk_behavior_state_changed(ev);
|
return zmk_behavior_state_changed(ev);
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZMK_LISTENER(behavior, behavior_listener);
|
ZMK_LISTENER(behavior, behavior_listener);
|
||||||
|
|
|
@ -12,14 +12,14 @@
|
||||||
#include <bluetooth/conn.h>
|
#include <bluetooth/conn.h>
|
||||||
#include <logging/log.h>
|
#include <logging/log.h>
|
||||||
#include <zmk/behavior.h>
|
#include <zmk/behavior.h>
|
||||||
|
#include <zmk/events/behavior_state_changed.h>
|
||||||
|
|
||||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
#include <zmk/ble.h>
|
#include <zmk/ble.h>
|
||||||
|
|
||||||
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
switch (event->param1) {
|
||||||
switch (binding->param1) {
|
|
||||||
case BT_CLR_CMD:
|
case BT_CLR_CMD:
|
||||||
return zmk_ble_clear_bonds();
|
return zmk_ble_clear_bonds();
|
||||||
case BT_NXT_CMD:
|
case BT_NXT_CMD:
|
||||||
|
@ -27,9 +27,9 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
||||||
case BT_PRV_CMD:
|
case BT_PRV_CMD:
|
||||||
return zmk_ble_prof_prev();
|
return zmk_ble_prof_prev();
|
||||||
case BT_SEL_CMD:
|
case BT_SEL_CMD:
|
||||||
return zmk_ble_prof_select(binding->param2);
|
return zmk_ble_prof_select(event->param2);
|
||||||
default:
|
default:
|
||||||
LOG_ERR("Unknown BT command: %d", binding->param1);
|
LOG_ERR("Unknown BT command: %d", event->param1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
|
@ -37,8 +37,7 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
||||||
|
|
||||||
static int behavior_bt_init(const struct device *dev) { return 0; };
|
static int behavior_bt_init(const struct device *dev) { return 0; };
|
||||||
|
|
||||||
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,15 +16,14 @@
|
||||||
#include <logging/log.h>
|
#include <logging/log.h>
|
||||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
|
||||||
const struct device *ext_power = device_get_binding("EXT_POWER");
|
const struct device *ext_power = device_get_binding("EXT_POWER");
|
||||||
if (ext_power == NULL) {
|
if (ext_power == NULL) {
|
||||||
LOG_ERR("Unable to retrieve ext_power device: %d", binding->param1);
|
LOG_ERR("Unable to retrieve ext_power device: %d", event->param1);
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (binding->param1) {
|
switch (event->param1) {
|
||||||
case EXT_POWER_OFF_CMD:
|
case EXT_POWER_OFF_CMD:
|
||||||
return ext_power_disable(ext_power);
|
return ext_power_disable(ext_power);
|
||||||
case EXT_POWER_ON_CMD:
|
case EXT_POWER_ON_CMD:
|
||||||
|
@ -35,14 +34,13 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
||||||
else
|
else
|
||||||
return ext_power_enable(ext_power);
|
return ext_power_enable(ext_power);
|
||||||
default:
|
default:
|
||||||
LOG_ERR("Unknown ext_power command: %d", binding->param1);
|
LOG_ERR("Unknown ext_power command: %d", event->param1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -283,28 +283,23 @@ static void decide_hold_tap(struct active_hold_tap *hold_tap, enum decision_mome
|
||||||
flavor_str(hold_tap->config->flavor), event_type);
|
flavor_str(hold_tap->config->flavor), event_type);
|
||||||
undecided_hold_tap = NULL;
|
undecided_hold_tap = NULL;
|
||||||
|
|
||||||
struct zmk_behavior_binding_event event = {
|
// todo: store layer properly
|
||||||
.position = hold_tap->position,
|
int layer = 0;
|
||||||
.timestamp = hold_tap->timestamp,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct zmk_behavior_binding binding;
|
|
||||||
if (hold_tap->is_hold) {
|
if (hold_tap->is_hold) {
|
||||||
binding.behavior_dev = hold_tap->config->behaviors->hold.behavior_dev;
|
ZMK_EVENT_RAISE(create_behavior_state_changed(
|
||||||
binding.param1 = hold_tap->param_hold;
|
hold_tap->config->behaviors->hold.behavior_dev, hold_tap->param_hold, 0, true, layer,
|
||||||
binding.param2 = 0;
|
hold_tap->position, hold_tap->timestamp));
|
||||||
} else {
|
} else {
|
||||||
binding.behavior_dev = hold_tap->config->behaviors->tap.behavior_dev;
|
ZMK_EVENT_RAISE(create_behavior_state_changed(hold_tap->config->behaviors->tap.behavior_dev,
|
||||||
binding.param1 = hold_tap->param_tap;
|
hold_tap->param_tap, 0, true, layer,
|
||||||
binding.param2 = 0;
|
hold_tap->position, hold_tap->timestamp));
|
||||||
}
|
}
|
||||||
behavior_keymap_binding_pressed(&binding, event);
|
|
||||||
release_captured_events();
|
release_captured_events();
|
||||||
}
|
}
|
||||||
|
|
||||||
static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_hold_tap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
const struct device *dev = device_get_binding(event->behavior_dev);
|
||||||
const struct device *dev = device_get_binding(binding->behavior_dev);
|
|
||||||
const struct behavior_hold_tap_config *cfg = dev->config;
|
const struct behavior_hold_tap_config *cfg = dev->config;
|
||||||
|
|
||||||
if (undecided_hold_tap != NULL) {
|
if (undecided_hold_tap != NULL) {
|
||||||
|
@ -314,14 +309,14 @@ static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct active_hold_tap *hold_tap =
|
struct active_hold_tap *hold_tap =
|
||||||
store_hold_tap(event.position, binding->param1, binding->param2, event.timestamp, cfg);
|
store_hold_tap(event->position, event->param1, event->param2, event->timestamp, cfg);
|
||||||
if (hold_tap == NULL) {
|
if (hold_tap == NULL) {
|
||||||
LOG_ERR("unable to store hold-tap info, did you press more than %d hold-taps?",
|
LOG_ERR("unable to store hold-tap info, did you press more than %d hold-taps?",
|
||||||
ZMK_BHV_HOLD_TAP_MAX_HELD);
|
ZMK_BHV_HOLD_TAP_MAX_HELD);
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_DBG("%d new undecided hold_tap", event.position);
|
LOG_DBG("%d new undecided hold_tap", event->position);
|
||||||
undecided_hold_tap = hold_tap;
|
undecided_hold_tap = hold_tap;
|
||||||
|
|
||||||
// if this behavior was queued we have to adjust the timer to only
|
// if this behavior was queued we have to adjust the timer to only
|
||||||
|
@ -334,9 +329,8 @@ static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding,
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int on_hold_tap_binding_released(struct zmk_behavior_binding *binding,
|
static int on_hold_tap_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
struct active_hold_tap *hold_tap = find_hold_tap(event->position);
|
||||||
struct active_hold_tap *hold_tap = find_hold_tap(event.position);
|
|
||||||
if (hold_tap == NULL) {
|
if (hold_tap == NULL) {
|
||||||
LOG_ERR("ACTIVE_HOLD_TAP_CLEANED_UP_TOO_EARLY");
|
LOG_ERR("ACTIVE_HOLD_TAP_CLEANED_UP_TOO_EARLY");
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
|
@ -345,40 +339,38 @@ static int on_hold_tap_binding_released(struct zmk_behavior_binding *binding,
|
||||||
// If these events were queued, the timer event may be queued too late or not at all.
|
// If these events were queued, the timer event may be queued too late or not at all.
|
||||||
// We insert a timer event before the TH_KEY_UP event to verify.
|
// We insert a timer event before the TH_KEY_UP event to verify.
|
||||||
int work_cancel_result = k_delayed_work_cancel(&hold_tap->work);
|
int work_cancel_result = k_delayed_work_cancel(&hold_tap->work);
|
||||||
if (event.timestamp > (hold_tap->timestamp + hold_tap->config->tapping_term_ms)) {
|
if (event->timestamp > (hold_tap->timestamp + hold_tap->config->tapping_term_ms)) {
|
||||||
decide_hold_tap(hold_tap, HT_TIMER_EVENT);
|
decide_hold_tap(hold_tap, HT_TIMER_EVENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
decide_hold_tap(hold_tap, HT_KEY_UP);
|
decide_hold_tap(hold_tap, HT_KEY_UP);
|
||||||
|
|
||||||
// todo: set up the binding and data items inside of the active_hold_tap struct
|
// todo: store layer properly
|
||||||
struct zmk_behavior_binding_event sub_behavior_data = {
|
int ret;
|
||||||
.position = hold_tap->position,
|
int layer = 0;
|
||||||
.timestamp = hold_tap->timestamp,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct zmk_behavior_binding sub_behavior_binding;
|
|
||||||
if (hold_tap->is_hold) {
|
if (hold_tap->is_hold) {
|
||||||
sub_behavior_binding.behavior_dev = hold_tap->config->behaviors->hold.behavior_dev;
|
ret = ZMK_EVENT_RAISE(create_behavior_state_changed(
|
||||||
sub_behavior_binding.param1 = hold_tap->param_hold;
|
hold_tap->config->behaviors->hold.behavior_dev, hold_tap->param_hold, 0, false, layer,
|
||||||
sub_behavior_binding.param2 = 0;
|
hold_tap->position, hold_tap->timestamp));
|
||||||
} else {
|
} else {
|
||||||
sub_behavior_binding.behavior_dev = hold_tap->config->behaviors->tap.behavior_dev;
|
ret = ZMK_EVENT_RAISE(create_behavior_state_changed(
|
||||||
sub_behavior_binding.param1 = hold_tap->param_tap;
|
hold_tap->config->behaviors->tap.behavior_dev, hold_tap->param_tap, 0, false, layer,
|
||||||
sub_behavior_binding.param2 = 0;
|
hold_tap->position, hold_tap->timestamp));
|
||||||
}
|
}
|
||||||
behavior_keymap_binding_released(&sub_behavior_binding, sub_behavior_data);
|
|
||||||
|
|
||||||
if (work_cancel_result == -EINPROGRESS) {
|
if (work_cancel_result == -EINPROGRESS) {
|
||||||
// let the timer handler clean up
|
// let the timer handler clean up
|
||||||
// if we'd clear now, the timer may call back for an uninitialized active_hold_tap.
|
// if we'd clear now, the timer may call back for an uninitialized active_hold_tap.
|
||||||
LOG_DBG("%d hold-tap timer work in event queue", event.position);
|
LOG_DBG("%d hold-tap timer work in event queue", event->position);
|
||||||
hold_tap->work_is_cancelled = true;
|
hold_tap->work_is_cancelled = true;
|
||||||
} else {
|
} else {
|
||||||
LOG_DBG("%d cleaning up hold-tap", event.position);
|
LOG_DBG("%d cleaning up hold-tap", event->position);
|
||||||
clear_hold_tap(hold_tap);
|
clear_hold_tap(hold_tap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,18 +18,16 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
static int behavior_key_press_init(const struct device *dev) { return 0; };
|
static int behavior_key_press_init(const struct device *dev) { return 0; };
|
||||||
|
|
||||||
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
LOG_DBG("position %d keycode 0x%02X", event->position, event->param1);
|
||||||
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
|
|
||||||
return ZMK_EVENT_RAISE(
|
return ZMK_EVENT_RAISE(
|
||||||
keycode_state_changed_from_encoded(binding->param1, true, event.timestamp));
|
keycode_state_changed_from_encoded(event->param1, true, event->timestamp));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
LOG_DBG("position %d keycode 0x%02X", event->position, event->param1);
|
||||||
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
|
|
||||||
return ZMK_EVENT_RAISE(
|
return ZMK_EVENT_RAISE(
|
||||||
keycode_state_changed_from_encoded(binding->param1, false, event.timestamp));
|
keycode_state_changed_from_encoded(event->param1, false, event->timestamp));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct behavior_driver_api behavior_key_press_driver_api = {
|
static const struct behavior_driver_api behavior_key_press_driver_api = {
|
||||||
|
|
|
@ -20,16 +20,14 @@ struct behavior_mo_data {};
|
||||||
|
|
||||||
static int behavior_mo_init(const struct device *dev) { return 0; };
|
static int behavior_mo_init(const struct device *dev) { return 0; };
|
||||||
|
|
||||||
static int mo_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int mo_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
LOG_DBG("position %d layer %d", event->position, event->param1);
|
||||||
LOG_DBG("position %d layer %d", event.position, binding->param1);
|
return zmk_keymap_layer_activate(event->param1);
|
||||||
return zmk_keymap_layer_activate(binding->param1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int mo_keymap_binding_released(struct zmk_behavior_binding *binding,
|
static int mo_keymap_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
LOG_DBG("position %d layer %d", event->position, event->param1);
|
||||||
LOG_DBG("position %d layer %d", event.position, binding->param1);
|
return zmk_keymap_layer_deactivate(event->param1);
|
||||||
return zmk_keymap_layer_deactivate(binding->param1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct behavior_driver_api behavior_mo_driver_api = {
|
static const struct behavior_driver_api behavior_mo_driver_api = {
|
||||||
|
|
|
@ -20,13 +20,11 @@ struct behavior_none_data {};
|
||||||
|
|
||||||
static int behavior_none_init(const struct device *dev) { return 0; };
|
static int behavior_none_init(const struct device *dev) { return 0; };
|
||||||
|
|
||||||
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,9 +18,8 @@
|
||||||
#include <logging/log.h>
|
#include <logging/log.h>
|
||||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
switch (event->param1) {
|
||||||
switch (binding->param1) {
|
|
||||||
case OUT_TOG:
|
case OUT_TOG:
|
||||||
return zmk_endpoints_toggle();
|
return zmk_endpoints_toggle();
|
||||||
case OUT_USB:
|
case OUT_USB:
|
||||||
|
@ -28,7 +27,7 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
||||||
case OUT_BLE:
|
case OUT_BLE:
|
||||||
return zmk_endpoints_select(ZMK_ENDPOINT_BLE);
|
return zmk_endpoints_select(ZMK_ENDPOINT_BLE);
|
||||||
default:
|
default:
|
||||||
LOG_ERR("Unknown output command: %d", binding->param1);
|
LOG_ERR("Unknown output command: %d", event->param1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
|
|
|
@ -21,9 +21,8 @@ struct behavior_reset_config {
|
||||||
|
|
||||||
static int behavior_reset_init(const struct device *dev) { return 0; };
|
static int behavior_reset_init(const struct device *dev) { return 0; };
|
||||||
|
|
||||||
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
const struct device *dev = device_get_binding(event->behavior_dev);
|
||||||
const struct device *dev = device_get_binding(binding->behavior_dev);
|
|
||||||
const struct behavior_reset_config *cfg = dev->config;
|
const struct behavior_reset_config *cfg = dev->config;
|
||||||
|
|
||||||
// TODO: Correct magic code for going into DFU?
|
// TODO: Correct magic code for going into DFU?
|
||||||
|
|
|
@ -18,9 +18,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
static int behavior_rgb_underglow_init(const struct device *dev) { return 0; }
|
static int behavior_rgb_underglow_init(const struct device *dev) { return 0; }
|
||||||
|
|
||||||
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
switch (event->param1) {
|
||||||
switch (binding->param1) {
|
|
||||||
case RGB_TOG:
|
case RGB_TOG:
|
||||||
return zmk_rgb_underglow_toggle();
|
return zmk_rgb_underglow_toggle();
|
||||||
case RGB_HUI:
|
case RGB_HUI:
|
||||||
|
@ -48,8 +47,7 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
||||||
return -ENOTSUP;
|
return -ENOTSUP;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
#include <zmk/events/keycode_state_changed.h>
|
#include <zmk/events/keycode_state_changed.h>
|
||||||
#include <zmk/events/modifiers_state_changed.h>
|
#include <zmk/events/modifiers_state_changed.h>
|
||||||
#include <zmk/hid.h>
|
#include <zmk/hid.h>
|
||||||
|
#include <zmk/event-manager.h>
|
||||||
|
|
||||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
|
@ -87,32 +88,21 @@ static struct active_sticky_key *find_sticky_key(uint32_t position) {
|
||||||
|
|
||||||
static inline int press_sticky_key_behavior(struct active_sticky_key *sticky_key,
|
static inline int press_sticky_key_behavior(struct active_sticky_key *sticky_key,
|
||||||
int64_t timestamp) {
|
int64_t timestamp) {
|
||||||
struct zmk_behavior_binding binding = {
|
// todo: use correct layer
|
||||||
.behavior_dev = sticky_key->config->behavior.behavior_dev,
|
int layer = 0;
|
||||||
.param1 = sticky_key->param1,
|
return ZMK_EVENT_RAISE(create_behavior_state_changed(
|
||||||
.param2 = sticky_key->param2,
|
sticky_key->config->behavior.behavior_dev, sticky_key->param1, sticky_key->param2, true,
|
||||||
};
|
layer, sticky_key->position, timestamp));
|
||||||
struct zmk_behavior_binding_event event = {
|
|
||||||
.position = sticky_key->position,
|
|
||||||
.timestamp = timestamp,
|
|
||||||
};
|
|
||||||
return behavior_keymap_binding_pressed(&binding, event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int release_sticky_key_behavior(struct active_sticky_key *sticky_key,
|
static inline int release_sticky_key_behavior(struct active_sticky_key *sticky_key,
|
||||||
int64_t timestamp) {
|
int64_t timestamp) {
|
||||||
struct zmk_behavior_binding binding = {
|
|
||||||
.behavior_dev = sticky_key->config->behavior.behavior_dev,
|
|
||||||
.param1 = sticky_key->param1,
|
|
||||||
.param2 = sticky_key->param2,
|
|
||||||
};
|
|
||||||
struct zmk_behavior_binding_event event = {
|
|
||||||
.position = sticky_key->position,
|
|
||||||
.timestamp = timestamp,
|
|
||||||
};
|
|
||||||
|
|
||||||
clear_sticky_key(sticky_key);
|
clear_sticky_key(sticky_key);
|
||||||
return behavior_keymap_binding_released(&binding, event);
|
// todo: use correct layer
|
||||||
|
int layer = 0;
|
||||||
|
return ZMK_EVENT_RAISE(create_behavior_state_changed(
|
||||||
|
sticky_key->config->behavior.behavior_dev, sticky_key->param1, sticky_key->param2, false,
|
||||||
|
layer, sticky_key->position, timestamp));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int stop_timer(struct active_sticky_key *sticky_key) {
|
static int stop_timer(struct active_sticky_key *sticky_key) {
|
||||||
|
@ -124,31 +114,29 @@ static int stop_timer(struct active_sticky_key *sticky_key) {
|
||||||
return timer_cancel_result;
|
return timer_cancel_result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int on_sticky_key_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_sticky_key_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
const struct device *dev = device_get_binding(event->behavior_dev);
|
||||||
const struct device *dev = device_get_binding(binding->behavior_dev);
|
|
||||||
const struct behavior_sticky_key_config *cfg = dev->config;
|
const struct behavior_sticky_key_config *cfg = dev->config;
|
||||||
struct active_sticky_key *sticky_key;
|
struct active_sticky_key *sticky_key;
|
||||||
sticky_key = find_sticky_key(event.position);
|
sticky_key = find_sticky_key(event->position);
|
||||||
if (sticky_key != NULL) {
|
if (sticky_key != NULL) {
|
||||||
stop_timer(sticky_key);
|
stop_timer(sticky_key);
|
||||||
release_sticky_key_behavior(sticky_key, event.timestamp);
|
release_sticky_key_behavior(sticky_key, event->timestamp);
|
||||||
}
|
}
|
||||||
sticky_key = store_sticky_key(event.position, binding->param1, binding->param2, cfg);
|
sticky_key = store_sticky_key(event->position, event->param1, event->param2, cfg);
|
||||||
if (sticky_key == NULL) {
|
if (sticky_key == NULL) {
|
||||||
LOG_ERR("unable to store sticky key, did you press more than %d sticky_key?",
|
LOG_ERR("unable to store sticky key, did you press more than %d sticky_key?",
|
||||||
ZMK_BHV_STICKY_KEY_MAX_HELD);
|
ZMK_BHV_STICKY_KEY_MAX_HELD);
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
press_sticky_key_behavior(sticky_key, event.timestamp);
|
press_sticky_key_behavior(sticky_key, event->timestamp);
|
||||||
LOG_DBG("%d new sticky_key", event.position);
|
LOG_DBG("%d new sticky_key", event->position);
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int on_sticky_key_binding_released(struct zmk_behavior_binding *binding,
|
static int on_sticky_key_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
struct active_sticky_key *sticky_key = find_sticky_key(event->position);
|
||||||
struct active_sticky_key *sticky_key = find_sticky_key(event.position);
|
|
||||||
if (sticky_key == NULL) {
|
if (sticky_key == NULL) {
|
||||||
LOG_ERR("ACTIVE STICKY KEY CLEARED TOO EARLY");
|
LOG_ERR("ACTIVE STICKY KEY CLEARED TOO EARLY");
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
|
@ -156,12 +144,12 @@ static int on_sticky_key_binding_released(struct zmk_behavior_binding *binding,
|
||||||
|
|
||||||
if (sticky_key->modified_key_usage_page != 0 && sticky_key->modified_key_keycode != 0) {
|
if (sticky_key->modified_key_usage_page != 0 && sticky_key->modified_key_keycode != 0) {
|
||||||
LOG_DBG("Another key was pressed while the sticky key was pressed. Act like a normal key.");
|
LOG_DBG("Another key was pressed while the sticky key was pressed. Act like a normal key.");
|
||||||
return release_sticky_key_behavior(sticky_key, event.timestamp);
|
return release_sticky_key_behavior(sticky_key, event->timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// No other key was pressed. Start the timer.
|
// No other key was pressed. Start the timer.
|
||||||
sticky_key->timer_started = true;
|
sticky_key->timer_started = true;
|
||||||
sticky_key->release_at = event.timestamp + sticky_key->config->release_after_ms;
|
sticky_key->release_at = event->timestamp + sticky_key->config->release_after_ms;
|
||||||
// adjust timer in case this behavior was queued by a hold-tap
|
// adjust timer in case this behavior was queued by a hold-tap
|
||||||
int32_t ms_left = sticky_key->release_at - k_uptime_get();
|
int32_t ms_left = sticky_key->release_at - k_uptime_get();
|
||||||
if (ms_left > 0) {
|
if (ms_left > 0) {
|
||||||
|
|
|
@ -17,16 +17,14 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||||
|
|
||||||
static int behavior_to_init(const struct device *dev) { return 0; };
|
static int behavior_to_init(const struct device *dev) { return 0; };
|
||||||
|
|
||||||
static int to_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int to_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
LOG_DBG("position %d layer %d", event->position, event->param1);
|
||||||
LOG_DBG("position %d layer %d", event.position, binding->param1);
|
zmk_keymap_layer_to(event->param1);
|
||||||
zmk_keymap_layer_to(binding->param1);
|
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int to_keymap_binding_released(struct zmk_behavior_binding *binding,
|
static int to_keymap_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
LOG_DBG("position %d layer %d", event->position, event->param1);
|
||||||
LOG_DBG("position %d layer %d", event.position, binding->param1);
|
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,15 +20,13 @@ struct behavior_tog_data {};
|
||||||
|
|
||||||
static int behavior_tog_init(const struct device *dev) { return 0; };
|
static int behavior_tog_init(const struct device *dev) { return 0; };
|
||||||
|
|
||||||
static int tog_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int tog_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
LOG_DBG("position %d layer %d", event->position, event->param1);
|
||||||
LOG_DBG("position %d layer %d", event.position, binding->param1);
|
return zmk_keymap_layer_toggle(event->param1);
|
||||||
return zmk_keymap_layer_toggle(binding->param1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tog_keymap_binding_released(struct zmk_behavior_binding *binding,
|
static int tog_keymap_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
LOG_DBG("position %d layer %d", event->position, event->param1);
|
||||||
LOG_DBG("position %d layer %d", event.position, binding->param1);
|
|
||||||
return ZMK_BEHAVIOR_OPAQUE;
|
return ZMK_BEHAVIOR_OPAQUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,13 +20,11 @@ struct behavior_transparent_data {};
|
||||||
|
|
||||||
static int behavior_transparent_init(const struct device *dev) { return 0; };
|
static int behavior_transparent_init(const struct device *dev) { return 0; };
|
||||||
|
|
||||||
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_pressed(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
|
||||||
return ZMK_BEHAVIOR_TRANSPARENT;
|
return ZMK_BEHAVIOR_TRANSPARENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
|
static int on_keymap_binding_released(const struct behavior_state_changed *event) {
|
||||||
struct zmk_behavior_binding_event event) {
|
|
||||||
return ZMK_BEHAVIOR_TRANSPARENT;
|
return ZMK_BEHAVIOR_TRANSPARENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue