Browse Source
* 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
19 changed files with 249 additions and 112 deletions
@ -1,9 +0,0 @@
@@ -1,9 +0,0 @@
|
||||
/ { |
||||
behaviors { |
||||
keymap_behavior: behavior_keymap { |
||||
compatible = "zmk,behavior-keymap", "zmk,behavior-global"; |
||||
label = "KEYMAP"; |
||||
#binding-cells = <0>; |
||||
}; |
||||
}; |
||||
}; |
@ -1,8 +0,0 @@
@@ -1,8 +0,0 @@
|
||||
# Copyright (c) 2020, Pete Johanson |
||||
# SPDX-License-Identifier: MIT |
||||
|
||||
description: Keymap Behavior |
||||
|
||||
compatible: "zmk,behavior-keymap" |
||||
|
||||
include: zero_param.yaml |
@ -0,0 +1,16 @@
@@ -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) |
||||
|
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
|
||||
|
||||
#define→EVENT_TYPE_SECTIONS()→ → → → \ |
||||
→ → __event_type_start = .;→ → \
|
||||
→ → KEEP(*(".event_type_*"));→ → \
|
||||
→ → __event_type_end = .;→→ → \
|
@ -0,0 +1,65 @@
@@ -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); |
@ -0,0 +1,13 @@
@@ -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); |
@ -0,0 +1,12 @@
@@ -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); |
@ -1,50 +0,0 @@
@@ -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); |
@ -0,0 +1,32 @@
@@ -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; |
||||
} |
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
|
||||
|
||||
#include <kernel.h> |
||||
#include <zmk/events/keycode-state-changed.h> |
||||
|
||||
ZMK_EVENT_IMPL(keycode_state_changed); |
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
|
||||
|
||||
#include <kernel.h> |
||||
#include <zmk/events/position-state-changed.h> |
||||
|
||||
ZMK_EVENT_IMPL(position_state_changed); |
Loading…
Reference in new issue