Browse Source

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.
xmkb
Pete Johanson 4 years ago
parent
commit
9a991bf019
  1. 6
      app/CMakeLists.txt
  2. 3
      app/Kconfig
  3. 1
      app/dts/behaviors.dtsi
  4. 9
      app/dts/behaviors/keymap.dtsi
  5. 8
      app/dts/bindings/behaviors/zmk,behavior-keymap.yaml
  6. 16
      app/include/linker/zmk-events.ld
  7. 6
      app/include/linker/zmk-linker-defs.h
  8. 65
      app/include/zmk/event-manager.h
  9. 13
      app/include/zmk/events/keycode-state-changed.h
  10. 12
      app/include/zmk/events/position-state-changed.h
  11. 35
      app/src/behaviors/behavior_hid.c
  12. 19
      app/src/behaviors/behavior_key_press.c
  13. 50
      app/src/behaviors/behavior_keymap.c
  14. 41
      app/src/behaviors/behavior_mod_tap.c
  15. 32
      app/src/event_manager.c
  16. 6
      app/src/events/keycode_state_changed.c
  17. 6
      app/src/events/position_state_changed.c
  18. 16
      app/src/keymap.c
  19. 13
      app/src/kscan.c

6
app/CMakeLists.txt

@ -18,11 +18,11 @@ include(cmake/zmk_config.cmake)
find_package(Zephyr REQUIRED HINTS ../zephyr) find_package(Zephyr REQUIRED HINTS ../zephyr)
project(zmk) project(zmk)
if(EXISTS ${KEYMAP_DIR}/keymap.c) if(EXISTS ${KEYMAP_DIR}/keymap.c)
target_sources(app PRIVATE ${KEYMAP_DIR}/keymap.c) target_sources(app PRIVATE ${KEYMAP_DIR}/keymap.c)
endif() endif()
zephyr_linker_sources(RODATA include/linker/zmk-events.ld)
# Add your source file to the "app" target. This must come after # Add your source file to the "app" target. This must come after
# find_package(Zephyr) which defines the target. # 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/events.c)
target_sources(app PRIVATE src/keymap.c) target_sources(app PRIVATE src/keymap.c)
target_sources(app PRIVATE src/hid.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_hid.c)
target_sources(app PRIVATE src/behaviors/behavior_key_press.c) target_sources(app PRIVATE src/behaviors/behavior_key_press.c)
target_sources(app PRIVATE src/behaviors/behavior_reset.c) target_sources(app PRIVATE src/behaviors/behavior_reset.c)

3
app/Kconfig

@ -73,6 +73,9 @@ config ZMK_ACTION_MOD_TAP
endmenu endmenu
config HEAP_MEM_POOL_SIZE
default 200
module = ZMK module = ZMK
module-str = zmk module-str = zmk
source "subsys/logging/Kconfig.template.log_config" source "subsys/logging/Kconfig.template.log_config"

1
app/dts/behaviors.dtsi

@ -3,5 +3,4 @@
#include <behaviors/mod_tap.dtsi> #include <behaviors/mod_tap.dtsi>
#include <behaviors/momentary_layer.dtsi> #include <behaviors/momentary_layer.dtsi>
#include <behaviors/reset.dtsi> #include <behaviors/reset.dtsi>
#include <behaviors/keymap.dtsi>
#include <behaviors/hid.dtsi> #include <behaviors/hid.dtsi>

9
app/dts/behaviors/keymap.dtsi

@ -1,9 +0,0 @@
/ {
behaviors {
keymap_behavior: behavior_keymap {
compatible = "zmk,behavior-keymap", "zmk,behavior-global";
label = "KEYMAP";
#binding-cells = <0>;
};
};
};

8
app/dts/bindings/behaviors/zmk,behavior-keymap.yaml

@ -1,8 +0,0 @@
# Copyright (c) 2020, Pete Johanson
# SPDX-License-Identifier: MIT
description: Keymap Behavior
compatible: "zmk,behavior-keymap"
include: zero_param.yaml

16
app/include/linker/zmk-events.ld

@ -0,0 +1,16 @@
#include <linker/linker-defs.h>
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)

6
app/include/linker/zmk-linker-defs.h

@ -0,0 +1,6 @@
#define→EVENT_TYPE_SECTIONS()→ → → → \
__event_type_start = .; \
KEEP(*(".event_type_*")); \
__event_type_end = .; \

65
app/include/zmk/event-manager.h

@ -0,0 +1,65 @@
#pragma once
#include <stddef.h>
#include <kernel.h>
#include <zephyr/types.h>
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);

13
app/include/zmk/events/keycode-state-changed.h

@ -0,0 +1,13 @@
#pragma once
#include <zephyr.h>
#include <zmk/event-manager.h>
struct keycode_state_changed {
struct zmk_event_header header;
u8_t usage_page;
u32_t keycode;
bool state;
};
ZMK_EVENT_DECLARE(keycode_state_changed);

12
app/include/zmk/events/position-state-changed.h

@ -0,0 +1,12 @@
#pragma once
#include <zephyr.h>
#include <zmk/event-manager.h>
struct position_state_changed {
struct zmk_event_header header;
u32_t position;
bool state;
};
ZMK_EVENT_DECLARE(position_state_changed);

35
app/src/behaviors/behavior_hid.c

@ -13,18 +13,16 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event-manager.h>
#include <zmk/events/keycode-state-changed.h>
#include <zmk/hid.h> #include <zmk/hid.h>
#include <zmk/endpoints.h> #include <zmk/endpoints.h>
struct behavior_hid_config { }; struct behavior_hid_config { };
struct behavior_hid_data { }; 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; int err;
LOG_DBG("keycode %d", keycode); 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); 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; int err;
LOG_DBG("keycode %d", keycode); 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); 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) static int on_modifiers_pressed(struct device *dev, zmk_mod_flags modifiers)
{ {
LOG_DBG("modifiers %d", 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 = { 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_pressed = on_modifiers_pressed,
.modifiers_released = on_modifiers_released .modifiers_released = on_modifiers_released
}; };
static const struct behavior_hid_config behavior_hid_config = {}; static const struct behavior_hid_config behavior_hid_config = {};
static struct behavior_hid_data behavior_hid_data; static struct behavior_hid_data behavior_hid_data;

19
app/src/behaviors/behavior_key_press.c

@ -10,7 +10,8 @@
#include <drivers/behavior.h> #include <drivers/behavior.h>
#include <logging/log.h> #include <logging/log.h>
#include <zmk/events.h> #include <zmk/event-manager.h>
#include <zmk/events/keycode-state-changed.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); 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 _) 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; 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); 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 _) 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; 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); 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 = { static const struct behavior_driver_api behavior_key_press_driver_api = {

50
app/src/behaviors/behavior_keymap.c

@ -1,50 +0,0 @@
/*
* Copyright (c) 2020 Peter Johanson <peter@peterjohanson.com>
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_keymap
#include <device.h>
#include <power/reboot.h>
#include <drivers/behavior.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/keymap.h>
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);

41
app/src/behaviors/behavior_mod_tap.c

@ -10,6 +10,8 @@
#include <drivers/behavior.h> #include <drivers/behavior.h>
#include <logging/log.h> #include <logging/log.h>
#include <zmk/event-manager.h>
#include <zmk/events/keycode-state-changed.h>
#include <zmk/events.h> #include <zmk/events.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@ -19,6 +21,22 @@ struct behavior_mod_tap_data {
u16_t pending_press_positions; 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) static int behavior_mod_tap_init(struct device *dev)
{ {
return 0; return 0;
@ -50,30 +68,9 @@ zmk_events_modifiers_released(mods);
return 0; 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 = { 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_pressed = on_keymap_binding_pressed,
.binding_released = on_keymap_binding_released, .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; 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_data,
&behavior_mod_tap_config, &behavior_mod_tap_config,
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,

32
app/src/event_manager.c

@ -0,0 +1,32 @@
#include <zephyr.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event-manager.h>
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;
}

6
app/src/events/keycode_state_changed.c

@ -0,0 +1,6 @@
#include <kernel.h>
#include <zmk/events/keycode-state-changed.h>
ZMK_EVENT_IMPL(keycode_state_changed);

6
app/src/events/position_state_changed.c

@ -0,0 +1,6 @@
#include <kernel.h>
#include <zmk/events/position-state-changed.h>
ZMK_EVENT_IMPL(position_state_changed);

16
app/src/keymap.c

@ -9,6 +9,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <drivers/behavior.h> #include <drivers/behavior.h>
#include <zmk/behavior.h> #include <zmk/behavior.h>
#include <zmk/event-manager.h>
#include <zmk/events/position-state-changed.h>
static u32_t zmk_keymap_layer_state = 0; static u32_t zmk_keymap_layer_state = 0;
static u8_t zmk_keymap_layer_default = 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; 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);

13
app/src/kscan.c

@ -12,7 +12,8 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/matrix_transform.h> #include <zmk/matrix_transform.h>
#include <zmk/events.h> #include <zmk/event-manager.h>
#include <zmk/events/position-state-changed.h>
#define ZMK_KSCAN_EVENT_STATE_PRESSED 0 #define ZMK_KSCAN_EVENT_STATE_PRESSED 0
#define ZMK_KSCAN_EVENT_STATE_RELEASED 1 #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); bool pressed = (ev.state == ZMK_KSCAN_EVENT_STATE_PRESSED);
u32_t position = zmk_matrix_transform_row_column_to_position(ev.row, ev.column); 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")); LOG_DBG("Row: %d, col: %d, position: %d, pressed: %s\n", ev.row, ev.column, position, (pressed ? "true" : "false"));
if (pressed) { pos_ev = new_position_state_changed();
zmk_events_position_pressed(position); pos_ev->state = pressed;
} else { pos_ev->position = position;
zmk_events_position_released(position); ZMK_EVENT_RAISE(pos_ev);
}
} }
} }

Loading…
Cancel
Save