From 9a991bf019d6f723bc4230822852efa78a05be49 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Tue, 30 Jun 2020 00:31:09 -0400 Subject: [PATCH] Initial event manager work, and two first events. * Add initial event manager implementation, roughly mimicking Nordic's API. * Add `position_state_changed` and `keycode_state_changed` events. * Hook up HID and keymap to new events instead of using behaviour global event crazy. --- app/CMakeLists.txt | 8 ++- app/Kconfig | 3 + app/dts/behaviors.dtsi | 1 - app/dts/behaviors/keymap.dtsi | 9 --- .../behaviors/zmk,behavior-keymap.yaml | 8 --- app/include/linker/zmk-events.ld | 16 +++++ app/include/linker/zmk-linker-defs.h | 6 ++ app/include/zmk/event-manager.h | 65 +++++++++++++++++++ .../zmk/events/keycode-state-changed.h | 13 ++++ .../zmk/events/position-state-changed.h | 12 ++++ app/src/behaviors/behavior_hid.c | 35 +++++++--- app/src/behaviors/behavior_key_press.c | 19 +++++- app/src/behaviors/behavior_keymap.c | 50 -------------- app/src/behaviors/behavior_mod_tap.c | 43 ++++++------ app/src/event_manager.c | 32 +++++++++ app/src/events/keycode_state_changed.c | 6 ++ app/src/events/position_state_changed.c | 6 ++ app/src/keymap.c | 16 +++++ app/src/kscan.c | 13 ++-- 19 files changed, 249 insertions(+), 112 deletions(-) delete mode 100644 app/dts/behaviors/keymap.dtsi delete mode 100644 app/dts/bindings/behaviors/zmk,behavior-keymap.yaml create mode 100644 app/include/linker/zmk-events.ld create mode 100644 app/include/linker/zmk-linker-defs.h create mode 100644 app/include/zmk/event-manager.h create mode 100644 app/include/zmk/events/keycode-state-changed.h create mode 100644 app/include/zmk/events/position-state-changed.h delete mode 100644 app/src/behaviors/behavior_keymap.c create mode 100644 app/src/event_manager.c create mode 100644 app/src/events/keycode_state_changed.c create mode 100644 app/src/events/position_state_changed.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 4dcd2f6a..a3c20df0 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -18,11 +18,11 @@ include(cmake/zmk_config.cmake) find_package(Zephyr REQUIRED HINTS ../zephyr) project(zmk) - if(EXISTS ${KEYMAP_DIR}/keymap.c) - target_sources(app PRIVATE ${KEYMAP_DIR}/keymap.c) + target_sources(app PRIVATE ${KEYMAP_DIR}/keymap.c) endif() +zephyr_linker_sources(RODATA include/linker/zmk-events.ld) # Add your source file to the "app" target. This must come after # find_package(Zephyr) which defines the target. @@ -32,7 +32,9 @@ target_sources(app PRIVATE src/matrix_transform.c) target_sources(app PRIVATE src/events.c) target_sources(app PRIVATE src/keymap.c) target_sources(app PRIVATE src/hid.c) -target_sources(app PRIVATE src/behaviors/behavior_keymap.c) +target_sources(app PRIVATE src/event_manager.c) +target_sources(app PRIVATE src/events/keycode_state_changed.c) +target_sources(app PRIVATE src/events/position_state_changed.c) target_sources(app PRIVATE src/behaviors/behavior_hid.c) target_sources(app PRIVATE src/behaviors/behavior_key_press.c) target_sources(app PRIVATE src/behaviors/behavior_reset.c) diff --git a/app/Kconfig b/app/Kconfig index 7871b919..45481b15 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -73,6 +73,9 @@ config ZMK_ACTION_MOD_TAP endmenu +config HEAP_MEM_POOL_SIZE + default 200 + module = ZMK module-str = zmk source "subsys/logging/Kconfig.template.log_config" diff --git a/app/dts/behaviors.dtsi b/app/dts/behaviors.dtsi index cd5a3ab0..7a4f8fbc 100644 --- a/app/dts/behaviors.dtsi +++ b/app/dts/behaviors.dtsi @@ -3,5 +3,4 @@ #include #include #include -#include #include \ No newline at end of file diff --git a/app/dts/behaviors/keymap.dtsi b/app/dts/behaviors/keymap.dtsi deleted file mode 100644 index ec434d5f..00000000 --- a/app/dts/behaviors/keymap.dtsi +++ /dev/null @@ -1,9 +0,0 @@ -/ { - behaviors { - keymap_behavior: behavior_keymap { - compatible = "zmk,behavior-keymap", "zmk,behavior-global"; - label = "KEYMAP"; - #binding-cells = <0>; - }; - }; -}; diff --git a/app/dts/bindings/behaviors/zmk,behavior-keymap.yaml b/app/dts/bindings/behaviors/zmk,behavior-keymap.yaml deleted file mode 100644 index 1d8a51fc..00000000 --- a/app/dts/bindings/behaviors/zmk,behavior-keymap.yaml +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright (c) 2020, Pete Johanson -# SPDX-License-Identifier: MIT - -description: Keymap Behavior - -compatible: "zmk,behavior-keymap" - -include: zero_param.yaml diff --git a/app/include/linker/zmk-events.ld b/app/include/linker/zmk-events.ld new file mode 100644 index 00000000..f1ee9f76 --- /dev/null +++ b/app/include/linker/zmk-events.ld @@ -0,0 +1,16 @@ +#include + + SECTION_PROLOGUE(event_types,,) + { + __event_type_start = .; \ + KEEP(*(".event_type")); \ + __event_type_end = .; \ + } GROUP_LINK_IN(ROMABLE_REGION) + + SECTION_PROLOGUE(event_subscriptions,,) + { + __event_subscriptions_start = .; \ + KEEP(*(".event_subscription")); \ + __event_subscriptions_end = .; \ + } GROUP_LINK_IN(ROMABLE_REGION) + diff --git a/app/include/linker/zmk-linker-defs.h b/app/include/linker/zmk-linker-defs.h new file mode 100644 index 00000000..61b67414 --- /dev/null +++ b/app/include/linker/zmk-linker-defs.h @@ -0,0 +1,6 @@ + + +#define→EVENT_TYPE_SECTIONS()→ → → → \ +→ → __event_type_start = .;→ → \ +→ → KEEP(*(".event_type_*"));→ → \ +→ → __event_type_end = .;→→ → \ diff --git a/app/include/zmk/event-manager.h b/app/include/zmk/event-manager.h new file mode 100644 index 00000000..8c05a1bc --- /dev/null +++ b/app/include/zmk/event-manager.h @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include + +struct zmk_event_type +{ + const char *name; +}; + +struct zmk_event_header { + const struct zmk_event_type* event; +}; + +typedef int (*zmk_listener_callback_t)(const struct zmk_event_header *eh); +struct zmk_listener +{ + zmk_listener_callback_t callback; +}; + +struct zmk_event_subscription { + const struct zmk_event_type *event_type; + const struct zmk_listener *listener; +}; + +#define ZMK_EVENT_DECLARE(event_type) \ + struct event_type* new_##event_type(); \ + bool is_##event_type(const struct zmk_event_header *eh); \ + const struct event_type* cast_##event_type(const struct zmk_event_header *eh); \ + extern const struct zmk_event_type zmk_event_##event_type; + +#define ZMK_EVENT_IMPL(event_type) \ + const struct zmk_event_type zmk_event_##event_type = { \ + .name = STRINGIFY(event_type) \ + }; \ + const struct zmk_event_type* zmk_event_ref_##event_type __used __attribute__((__section__(".event_type"))) = &zmk_event_##event_type; \ + struct event_type* new_##event_type() { \ + struct event_type* ev = (struct event_type *) k_malloc(sizeof(struct event_type)); \ + ev->header.event = &zmk_event_##event_type; \ + return ev; \ + }; \ + bool is_##event_type(const struct zmk_event_header *eh) { \ + return eh->event == &zmk_event_##event_type; \ + }; \ + const struct event_type* cast_##event_type(const struct zmk_event_header *eh) {\ + return (const struct event_type*)eh; \ + }; + + +#define ZMK_LISTENER(mod, cb) \ + const struct zmk_listener zmk_listener_##mod = { \ + .callback = cb \ + }; + +#define ZMK_SUBSCRIPTION(mod, ev_type) \ + const Z_DECL_ALIGN(struct zmk_event_subscription) _CONCAT(_CONCAT(zmk_event_sub_,mod),ev_type) __used __attribute__((__section__(".event_subscription"))) = { \ + .event_type = &zmk_event_##ev_type, \ + .listener = &zmk_listener_##mod, \ + }; + +#define ZMK_EVENT_RAISE(ev) \ + zmk_event_manager_raise((struct zmk_event_header *)ev); + +int zmk_event_manager_raise(struct zmk_event_header *event); diff --git a/app/include/zmk/events/keycode-state-changed.h b/app/include/zmk/events/keycode-state-changed.h new file mode 100644 index 00000000..48cdddc1 --- /dev/null +++ b/app/include/zmk/events/keycode-state-changed.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +struct keycode_state_changed { + struct zmk_event_header header; + u8_t usage_page; + u32_t keycode; + bool state; +}; + +ZMK_EVENT_DECLARE(keycode_state_changed); \ No newline at end of file diff --git a/app/include/zmk/events/position-state-changed.h b/app/include/zmk/events/position-state-changed.h new file mode 100644 index 00000000..190b59db --- /dev/null +++ b/app/include/zmk/events/position-state-changed.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +struct position_state_changed { + struct zmk_event_header header; + u32_t position; + bool state; +}; + +ZMK_EVENT_DECLARE(position_state_changed); \ No newline at end of file diff --git a/app/src/behaviors/behavior_hid.c b/app/src/behaviors/behavior_hid.c index 1ac7404a..b9014502 100644 --- a/app/src/behaviors/behavior_hid.c +++ b/app/src/behaviors/behavior_hid.c @@ -13,18 +13,16 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#include +#include #include #include struct behavior_hid_config { }; struct behavior_hid_data { }; -static int behavior_hid_init(struct device *dev) -{ - return 0; -}; -static int on_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode) +static int behaviour_hid_keycode_pressed(u8_t usage_page, u32_t keycode) { int err; LOG_DBG("keycode %d", keycode); @@ -49,7 +47,7 @@ static int on_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode return zmk_endpoints_send_report(usage_page); } -static int on_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode) +static int behaviour_hid_keycode_released(u8_t usage_page, u32_t keycode) { int err; LOG_DBG("keycode %d", keycode); @@ -73,6 +71,28 @@ static int on_keycode_released(struct device *dev, u8_t usage_page, u32_t keycod return zmk_endpoints_send_report(usage_page); } + +int behavior_hid_listener(const struct zmk_event_header *eh) +{ + if (is_keycode_state_changed(eh)) { + const struct keycode_state_changed *ev = cast_keycode_state_changed(eh); + if (ev->state) { + behaviour_hid_keycode_pressed(ev->usage_page, ev->keycode); + } else { + behaviour_hid_keycode_released(ev->usage_page, ev->keycode); + } + } + return 0; +} + +ZMK_LISTENER(behavior_hid, behavior_hid_listener); +ZMK_SUBSCRIPTION(behavior_hid, keycode_state_changed); + +static int behavior_hid_init(struct device *dev) +{ + return 0; +}; + static int on_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers) { LOG_DBG("modifiers %d", modifiers); @@ -90,13 +110,10 @@ static int on_modifiers_released(struct device *dev, zmk_mod_flags modifiers) } static const struct behavior_driver_api behavior_hid_driver_api = { - .keycode_pressed = on_keycode_pressed, - .keycode_released = on_keycode_released, .modifiers_pressed = on_modifiers_pressed, .modifiers_released = on_modifiers_released }; - static const struct behavior_hid_config behavior_hid_config = {}; static struct behavior_hid_data behavior_hid_data; diff --git a/app/src/behaviors/behavior_key_press.c b/app/src/behaviors/behavior_key_press.c index 7213b3c2..34df1c0b 100644 --- a/app/src/behaviors/behavior_key_press.c +++ b/app/src/behaviors/behavior_key_press.c @@ -10,7 +10,8 @@ #include #include -#include +#include +#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -27,15 +28,27 @@ static int behavior_key_press_init(struct device *dev) static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t keycode, u32_t _) { const struct behavior_key_press_config *cfg = dev->config_info; + struct keycode_state_changed *ev; LOG_DBG("position %d usage_page 0x%02X keycode 0x%02X", position, cfg->usage_page, keycode); - return zmk_events_keycode_pressed(cfg->usage_page, keycode); + + ev = new_keycode_state_changed(); + ev->usage_page = cfg->usage_page; + ev->keycode = keycode; + ev->state = true; + return ZMK_EVENT_RAISE(ev); } static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t keycode, u32_t _) { const struct behavior_key_press_config *cfg = dev->config_info; + struct keycode_state_changed *ev; LOG_DBG("position %d usage_page 0x%02X keycode 0x%02X", position, cfg->usage_page, keycode); - return zmk_events_keycode_released(cfg->usage_page, keycode); + + ev = new_keycode_state_changed(); + ev->usage_page = cfg->usage_page; + ev->keycode = keycode; + ev->state = false; + return ZMK_EVENT_RAISE(ev); } static const struct behavior_driver_api behavior_key_press_driver_api = { diff --git a/app/src/behaviors/behavior_keymap.c b/app/src/behaviors/behavior_keymap.c deleted file mode 100644 index 7a06b2fd..00000000 --- a/app/src/behaviors/behavior_keymap.c +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2020 Peter Johanson - * - * SPDX-License-Identifier: MIT - */ - -#define DT_DRV_COMPAT zmk_behavior_keymap - -#include -#include -#include -#include - -LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); - -#include - -struct behavior_keymap_config { }; -struct behavior_keymap_data { }; - -static int behavior_keymap_init(struct device *dev) -{ - return 0; -}; - -static int on_position_pressed(struct device *dev, u32_t position) -{ - return zmk_keymap_position_state_changed(position, true); -} - -static int on_position_released(struct device *dev, u32_t position) -{ - return zmk_keymap_position_state_changed(position, false); -} - -static const struct behavior_driver_api behavior_keymap_driver_api = { - .position_pressed = on_position_pressed, - .position_released = on_position_released, -}; - - -static const struct behavior_keymap_config behavior_keymap_config = {}; - -static struct behavior_keymap_data behavior_keymap_data; - -DEVICE_AND_API_INIT(behavior_keymap, DT_INST_LABEL(0), behavior_keymap_init, - &behavior_keymap_data, - &behavior_keymap_config, - APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &behavior_keymap_driver_api); diff --git a/app/src/behaviors/behavior_mod_tap.c b/app/src/behaviors/behavior_mod_tap.c index 2e6e3391..c9baff4e 100644 --- a/app/src/behaviors/behavior_mod_tap.c +++ b/app/src/behaviors/behavior_mod_tap.c @@ -10,6 +10,8 @@ #include #include +#include +#include #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -19,6 +21,22 @@ struct behavior_mod_tap_data { u16_t pending_press_positions; }; +int behavior_mod_tap_listener(const struct zmk_event_header *eh) +{ + if (is_keycode_state_changed(eh)) { + struct device *dev = device_get_binding(DT_INST_LABEL(0)); + const struct keycode_state_changed *ev = cast_keycode_state_changed(eh); + if (ev->state) { + struct behavior_mod_tap_data *data = dev->driver_data; + data->pending_press_positions = 0; + } + } + return 0; +} + +ZMK_LISTENER(behavior_mod_tap, behavior_mod_tap_listener); +ZMK_SUBSCRIPTION(behavior_mod_tap, keycode_state_changed); + static int behavior_mod_tap_init(struct device *dev) { return 0; @@ -40,7 +58,7 @@ static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t struct behavior_mod_tap_data *data = dev->driver_data; LOG_DBG("mods: %d, keycode: %d", mods, keycode); -zmk_events_modifiers_released(mods); + zmk_events_modifiers_released(mods); if (data->pending_press_positions & BIT(position)) { zmk_events_keycode_pressed(USAGE_KEYPAD, keycode); k_msleep(10); @@ -50,30 +68,9 @@ zmk_events_modifiers_released(mods); return 0; } -static int on_keycode_pressed(struct device *dev, u8_t usage_page, u32_t keycode) -{ - struct behavior_mod_tap_data *data = dev->driver_data; - data->pending_press_positions = 0; - LOG_DBG("pressing: %d", keycode); - return 0; -} - -static int on_keycode_released(struct device *dev, u8_t usage_page, u32_t keycode) -{ - LOG_DBG("releasing: %d", keycode); - return 0; -} - static const struct behavior_driver_api behavior_mod_tap_driver_api = { - // These callbacks are all optional, and define which kinds of events the behavior can handle. - // They can reference local functions defined here, or shared event handlers. .binding_pressed = on_keymap_binding_pressed, .binding_released = on_keymap_binding_released, - .keycode_pressed = on_keycode_pressed, - .keycode_released = on_keycode_released - // Other optional callbacks a behavior can implement - // .on_mouse_moved - // .on_sensor_data - Any behaviour that wants to be linked to a censor can implement this behavior }; @@ -81,7 +78,7 @@ static const struct behavior_mod_tap_config behavior_mod_tap_config = {}; static struct behavior_mod_tap_data behavior_mod_tap_data; -DEVICE_AND_API_INIT(behavior_key_press, DT_INST_LABEL(0), behavior_mod_tap_init, +DEVICE_AND_API_INIT(behavior_mod_tap, DT_INST_LABEL(0), behavior_mod_tap_init, &behavior_mod_tap_data, &behavior_mod_tap_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, diff --git a/app/src/event_manager.c b/app/src/event_manager.c new file mode 100644 index 00000000..012d285f --- /dev/null +++ b/app/src/event_manager.c @@ -0,0 +1,32 @@ + +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include + +extern struct zmk_event_type* __event_type_start[]; +extern struct zmk_event_type* __event_type_end[]; + +extern struct zmk_event_subscription __event_subscriptions_start[]; +extern struct zmk_event_subscription __event_subscriptions_end[]; + +int zmk_event_manager_raise(struct zmk_event_header *event) +{ + int ret; + struct zmk_event_subscription *ev_sub; + for (ev_sub = __event_subscriptions_start; ev_sub != __event_subscriptions_end; ev_sub++) { + if (ev_sub->event_type == event->event) { + ret = ev_sub->listener->callback(event); + if (ret) { + LOG_DBG("Listener returned an error: %d", ret); + goto release; + } + } + } + +release: + k_free(event); + return ret; +} \ No newline at end of file diff --git a/app/src/events/keycode_state_changed.c b/app/src/events/keycode_state_changed.c new file mode 100644 index 00000000..ae1b4c90 --- /dev/null +++ b/app/src/events/keycode_state_changed.c @@ -0,0 +1,6 @@ + + +#include +#include + +ZMK_EVENT_IMPL(keycode_state_changed); \ No newline at end of file diff --git a/app/src/events/position_state_changed.c b/app/src/events/position_state_changed.c new file mode 100644 index 00000000..d67eedd4 --- /dev/null +++ b/app/src/events/position_state_changed.c @@ -0,0 +1,6 @@ + + +#include +#include + +ZMK_EVENT_IMPL(position_state_changed); \ No newline at end of file diff --git a/app/src/keymap.c b/app/src/keymap.c index f951dd7f..25bcf80e 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -9,6 +9,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include +#include +#include + static u32_t zmk_keymap_layer_state = 0; static u8_t zmk_keymap_layer_default = 0; @@ -116,3 +119,16 @@ int zmk_keymap_position_state_changed(u32_t position, bool pressed) return -ENOTSUP; } + +int keymap_listener(const struct zmk_event_header *eh) +{ + if (is_position_state_changed(eh)) { + const struct position_state_changed *ev = cast_position_state_changed(eh); + zmk_keymap_position_state_changed(ev->position, ev->state); + } + return 0; +} + +ZMK_LISTENER(keymap, keymap_listener); +ZMK_SUBSCRIPTION(keymap, position_state_changed); + diff --git a/app/src/kscan.c b/app/src/kscan.c index 29a60199..63713f1f 100644 --- a/app/src/kscan.c +++ b/app/src/kscan.c @@ -12,7 +12,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include -#include +#include +#include #define ZMK_KSCAN_EVENT_STATE_PRESSED 0 #define ZMK_KSCAN_EVENT_STATE_RELEASED 1 @@ -50,12 +51,12 @@ void zmk_kscan_process_msgq(struct k_work *item) { bool pressed = (ev.state == ZMK_KSCAN_EVENT_STATE_PRESSED); u32_t position = zmk_matrix_transform_row_column_to_position(ev.row, ev.column); + struct position_state_changed *pos_ev; LOG_DBG("Row: %d, col: %d, position: %d, pressed: %s\n", ev.row, ev.column, position, (pressed ? "true" : "false")); - if (pressed) { - zmk_events_position_pressed(position); - } else { - zmk_events_position_released(position); - } + pos_ev = new_position_state_changed(); + pos_ev->state = pressed; + pos_ev->position = position; + ZMK_EVENT_RAISE(pos_ev); } }