2020-07-01 06:13:03 +10:00
|
|
|
/*
|
2020-09-10 13:57:44 +10:00
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
2020-07-01 06:13:03 +10:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
2020-09-14 12:53:24 +10:00
|
|
|
|
2020-06-30 14:31:09 +10:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <zephyr/types.h>
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct zmk_event_type {
|
2020-06-30 14:31:09 +10:00
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
2021-01-18 16:35:56 +11:00
|
|
|
typedef struct {
|
2020-09-14 12:53:24 +10:00
|
|
|
const struct zmk_event_type *event;
|
2020-12-03 03:41:57 +11:00
|
|
|
uint8_t last_listener_index;
|
2021-01-18 16:35:56 +11:00
|
|
|
} zmk_event_t;
|
2020-06-30 14:31:09 +10:00
|
|
|
|
2020-12-17 05:41:53 +11:00
|
|
|
#define ZMK_EV_EVENT_BUBBLE 0
|
2020-07-30 14:13:32 +10:00
|
|
|
#define ZMK_EV_EVENT_HANDLED 1
|
|
|
|
#define ZMK_EV_EVENT_CAPTURED 2
|
|
|
|
|
2021-01-18 16:35:56 +11:00
|
|
|
typedef int (*zmk_listener_callback_t)(const zmk_event_t *eh);
|
2020-09-14 12:53:24 +10:00
|
|
|
struct zmk_listener {
|
2020-06-30 14:31:09 +10:00
|
|
|
zmk_listener_callback_t callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct zmk_event_subscription {
|
|
|
|
const struct zmk_event_type *event_type;
|
|
|
|
const struct zmk_listener *listener;
|
|
|
|
};
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define ZMK_EVENT_DECLARE(event_type) \
|
2021-01-18 16:35:56 +11:00
|
|
|
struct event_type##_event { \
|
|
|
|
zmk_event_t header; \
|
|
|
|
struct event_type data; \
|
|
|
|
}; \
|
|
|
|
struct event_type##_event *new_##event_type(struct event_type); \
|
2021-01-20 06:21:00 +11:00
|
|
|
struct event_type *as_##event_type(const zmk_event_t *eh); \
|
2020-06-30 14:31:09 +10:00
|
|
|
extern const struct zmk_event_type zmk_event_##event_type;
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#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; \
|
2021-01-18 16:35:56 +11:00
|
|
|
struct event_type##_event *new_##event_type(struct event_type data) { \
|
|
|
|
struct event_type##_event *ev = \
|
|
|
|
(struct event_type##_event *)k_malloc(sizeof(struct event_type##_event)); \
|
2020-09-14 12:53:24 +10:00
|
|
|
ev->header.event = &zmk_event_##event_type; \
|
2021-01-18 16:35:56 +11:00
|
|
|
ev->data = data; \
|
2020-09-14 12:53:24 +10:00
|
|
|
return ev; \
|
|
|
|
}; \
|
2021-01-20 06:21:00 +11:00
|
|
|
struct event_type *as_##event_type(const zmk_event_t *eh) { \
|
|
|
|
return (eh->event == &zmk_event_##event_type) ? &((struct event_type##_event *)eh)->data \
|
|
|
|
: NULL; \
|
2020-06-30 14:31:09 +10:00
|
|
|
};
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define ZMK_LISTENER(mod, cb) const struct zmk_listener zmk_listener_##mod = {.callback = cb};
|
2020-06-30 14:31:09 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#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, \
|
2020-06-30 14:31:09 +10:00
|
|
|
};
|
|
|
|
|
2021-01-18 16:35:56 +11:00
|
|
|
#define ZMK_EVENT_RAISE(ev) zmk_event_manager_raise((zmk_event_t *)ev);
|
2020-06-30 14:31:09 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define ZMK_EVENT_RAISE_AFTER(ev, mod) \
|
2021-01-18 16:35:56 +11:00
|
|
|
zmk_event_manager_raise_after((zmk_event_t *)ev, &zmk_listener_##mod);
|
2020-07-31 14:07:16 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
#define ZMK_EVENT_RAISE_AT(ev, mod) \
|
2021-01-18 16:35:56 +11:00
|
|
|
zmk_event_manager_raise_at((zmk_event_t *)ev, &zmk_listener_##mod);
|
|
|
|
|
|
|
|
#define ZMK_EVENT_RELEASE(ev) zmk_event_manager_release((zmk_event_t *)ev);
|
2020-09-02 23:11:56 +10:00
|
|
|
|
2021-01-18 16:35:56 +11:00
|
|
|
#define ZMK_EVENT_FREE(ev) k_free((void *)ev);
|
2020-07-30 14:13:32 +10:00
|
|
|
|
2021-01-18 16:35:56 +11:00
|
|
|
int zmk_event_manager_raise(zmk_event_t *event);
|
|
|
|
int zmk_event_manager_raise_after(zmk_event_t *event, const struct zmk_listener *listener);
|
|
|
|
int zmk_event_manager_raise_at(zmk_event_t *event, const struct zmk_listener *listener);
|
|
|
|
int zmk_event_manager_release(zmk_event_t *event);
|