feature(events): add behavior-state-changed event

This commit is contained in:
Okke Formsma 2020-12-19 14:28:32 +01:00
parent dcd665999a
commit 65de1038ae
6 changed files with 136 additions and 14 deletions

View file

@ -31,6 +31,7 @@ target_sources(app PRIVATE src/sensors.c)
target_sources(app PRIVATE src/event_manager.c) target_sources(app PRIVATE src/event_manager.c)
target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/ext_power_generic.c) target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/ext_power_generic.c)
target_sources(app PRIVATE src/events/activity_state_changed.c) target_sources(app PRIVATE src/events/activity_state_changed.c)
target_sources(app PRIVATE src/events/behavior_state_changed.c)
target_sources(app PRIVATE src/events/position_state_changed.c) target_sources(app PRIVATE src/events/position_state_changed.c)
target_sources(app PRIVATE src/events/layer_state_changed.c) target_sources(app PRIVATE src/events/layer_state_changed.c)
target_sources(app PRIVATE src/events/keycode_state_changed.c) target_sources(app PRIVATE src/events/keycode_state_changed.c)
@ -70,6 +71,7 @@ target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/hog.c)
target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/rgb_underglow.c) target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/rgb_underglow.c)
target_sources(app PRIVATE src/endpoints.c) target_sources(app PRIVATE src/endpoints.c)
target_sources(app PRIVATE src/hid_listener.c) target_sources(app PRIVATE src/hid_listener.c)
target_sources(app PRIVATE src/behavior.c)
target_sources(app PRIVATE src/main.c) target_sources(app PRIVATE src/main.c)
add_subdirectory(src/display/) add_subdirectory(src/display/)

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <zephyr.h>
#include <zmk/event_manager.h>
#include <zmk/behavior.h>
struct behavior_state_changed {
struct zmk_event_header header;
char *behavior_dev;
uint32_t param1;
uint32_t param2;
bool pressed;
int layer;
uint32_t position;
int64_t timestamp;
};
ZMK_EVENT_DECLARE(behavior_state_changed);
static inline struct behavior_state_changed *
create_behavior_state_changed_from_binding(struct zmk_behavior_binding *binding, bool pressed,
int layer, uint32_t position, int64_t timestamp) {
struct behavior_state_changed *ev = new_behavior_state_changed();
ev->behavior_dev = binding->behavior_dev;
ev->param1 = binding->param1;
ev->param2 = binding->param2;
ev->pressed = pressed;
ev->layer = layer;
ev->position = position;
ev->timestamp = timestamp;
return ev;
}
static inline struct behavior_state_changed *
create_behavior_state_changed(char *behavior_dev, uint32_t param1, uint32_t param2, bool pressed,
int layer, uint32_t position, int64_t timestamp) {
struct behavior_state_changed *ev = new_behavior_state_changed();
ev->behavior_dev = behavior_dev;
ev->param1 = param1;
ev->param2 = param2;
ev->pressed = pressed;
ev->layer = layer;
ev->position = position;
ev->timestamp = timestamp;
return ev;
}

60
app/src/behavior.c Normal file
View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <sys/util.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/matrix.h>
#include <zmk/sensors.h>
#include <zmk/keymap.h>
#include <drivers/behavior.h>
#include <zmk/behavior.h>
#include <zmk/event_manager.h>
#include <zmk/events/position_state_changed.h>
#include <zmk/events/behavior_state_changed.h>
#include <zmk/events/sensor_event.h>
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;
if (ev->pressed) {
ret = behavior_keymap_binding_pressed(&binding, event);
} else {
ret = behavior_keymap_binding_released(&binding, event);
}
if (ret < 0) {
return ret;
}
switch (ret) {
case ZMK_BEHAVIOR_TRANSPARENT:
return ZMK_EV_EVENT_BUBBLE;
case ZMK_BEHAVIOR_OPAQUE:
return ZMK_EV_EVENT_HANDLED;
default:
return -ENOTSUP;
}
}
int behavior_listener(const struct zmk_event_header *eh) {
if (is_behavior_state_changed(eh)) {
const struct behavior_state_changed *ev = cast_position_state_changed(eh);
return zmk_behavior_state_changed(ev);
}
}
ZMK_LISTENER(behavior, behavior_listener);
ZMK_SUBSCRIPTION(behavior, behavior_state_changed);

View file

@ -17,6 +17,12 @@ extern struct zmk_event_type *__event_type_end[];
extern struct zmk_event_subscription __event_subscriptions_start[]; extern struct zmk_event_subscription __event_subscriptions_start[];
extern struct zmk_event_subscription __event_subscriptions_end[]; extern struct zmk_event_subscription __event_subscriptions_end[];
/*
* Returns negative values for errors.
* Returns ZMK_EV_EVENT_HANDLED if the event was handled
* Returns ZMK_EV_EVENT_CAPTURED if the event was captured
* Returns ZMK_EV_EVENT_BUBBLE if no handler handled the event
*/
int zmk_event_manager_handle_from(struct zmk_event_header *event, uint8_t start_index) { int zmk_event_manager_handle_from(struct zmk_event_header *event, uint8_t start_index) {
int ret = 0; int ret = 0;
uint8_t len = __event_subscriptions_end - __event_subscriptions_start; uint8_t len = __event_subscriptions_end - __event_subscriptions_start;
@ -31,13 +37,12 @@ int zmk_event_manager_handle_from(struct zmk_event_header *event, uint8_t start_
switch (ret) { switch (ret) {
case ZMK_EV_EVENT_HANDLED: case ZMK_EV_EVENT_HANDLED:
LOG_DBG("Listener handled the event"); LOG_DBG("Listener handled the event");
ret = 0;
goto release; goto release;
case ZMK_EV_EVENT_CAPTURED: case ZMK_EV_EVENT_CAPTURED:
LOG_DBG("Listener captured the event"); LOG_DBG("Listener captured the event");
event->last_listener_index = i; event->last_listener_index = i;
// Listeners are expected to free events they capture // Listeners are expected to free events they capture
return 0; return ret;
} }
} }
} }

View file

@ -0,0 +1,10 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <kernel.h>
#include <zmk/events/behavior_state_changed.h>
ZMK_EVENT_IMPL(behavior_state_changed);

View file

@ -16,6 +16,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#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/behavior_state_changed.h>
#include <zmk/events/layer_state_changed.h> #include <zmk/events/layer_state_changed.h>
#include <zmk/events/sensor_event.h> #include <zmk/events/sensor_event.h>
@ -146,11 +147,6 @@ int zmk_keymap_layer_to(uint8_t layer) {
int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed, int64_t timestamp) { int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed, int64_t timestamp) {
struct zmk_behavior_binding *binding = &zmk_keymap[layer][position]; struct zmk_behavior_binding *binding = &zmk_keymap[layer][position];
const struct device *behavior; const struct device *behavior;
struct zmk_behavior_binding_event event = {
.layer = layer,
.position = position,
.timestamp = timestamp,
};
LOG_DBG("layer: %d position: %d, binding name: %s", layer, position, LOG_DBG("layer: %d position: %d, binding name: %s", layer, position,
log_strdup(binding->behavior_dev)); log_strdup(binding->behavior_dev));
@ -159,14 +155,11 @@ int zmk_keymap_apply_position_state(int layer, uint32_t position, bool pressed,
if (!behavior) { if (!behavior) {
LOG_DBG("No behavior assigned to %d on layer %d", position, layer); LOG_DBG("No behavior assigned to %d on layer %d", position, layer);
return 1; return ZMK_EV_EVENT_HANDLED;
} }
if (pressed) { return ZMK_EVENT_RAISE(
return behavior_keymap_binding_pressed(binding, event); create_behavior_state_changed_from_binding(binding, pressed, layer, position, timestamp));
} else {
return behavior_keymap_binding_released(binding, event);
}
} }
int zmk_keymap_position_state_changed(uint32_t position, bool pressed, int64_t timestamp) { int zmk_keymap_position_state_changed(uint32_t position, bool pressed, int64_t timestamp) {
@ -176,7 +169,7 @@ int zmk_keymap_position_state_changed(uint32_t position, bool pressed, int64_t t
for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= _zmk_keymap_layer_default; layer--) { for (int layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer >= _zmk_keymap_layer_default; layer--) {
if (zmk_keymap_layer_active_with_state(layer, zmk_keymap_active_behavior_layer[position])) { if (zmk_keymap_layer_active_with_state(layer, zmk_keymap_active_behavior_layer[position])) {
int ret = zmk_keymap_apply_position_state(layer, position, pressed, timestamp); int ret = zmk_keymap_apply_position_state(layer, position, pressed, timestamp);
if (ret > 0) { if (ret == ZMK_EV_EVENT_BUBBLE) {
LOG_DBG("behavior processing to continue to next layer"); LOG_DBG("behavior processing to continue to next layer");
continue; continue;
} else if (ret < 0) { } else if (ret < 0) {