Browse Source

Merge pull request #68 from petejohanson/core/ev-manager-capture-release-events

Add the ability to capture event and release later
xmkb
Pete Johanson 4 years ago committed by GitHub
parent
commit
f269b26ea1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      app/include/zmk/event-manager.h
  2. 34
      app/src/event_manager.c

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

@ -17,8 +17,12 @@ struct zmk_event_type
struct zmk_event_header { struct zmk_event_header {
const struct zmk_event_type* event; const struct zmk_event_type* event;
u8_t last_listener_index;
}; };
#define ZMK_EV_EVENT_HANDLED 1
#define ZMK_EV_EVENT_CAPTURED 2
typedef int (*zmk_listener_callback_t)(const struct zmk_event_header *eh); typedef int (*zmk_listener_callback_t)(const struct zmk_event_header *eh);
struct zmk_listener struct zmk_listener
{ {
@ -33,7 +37,7 @@ struct zmk_event_subscription {
#define ZMK_EVENT_DECLARE(event_type) \ #define ZMK_EVENT_DECLARE(event_type) \
struct event_type* new_##event_type(); \ struct event_type* new_##event_type(); \
bool is_##event_type(const struct zmk_event_header *eh); \ bool is_##event_type(const struct zmk_event_header *eh); \
const struct event_type* cast_##event_type(const struct zmk_event_header *eh); \ struct event_type* cast_##event_type(const struct zmk_event_header *eh); \
extern const struct zmk_event_type zmk_event_##event_type; extern const struct zmk_event_type zmk_event_##event_type;
#define ZMK_EVENT_IMPL(event_type) \ #define ZMK_EVENT_IMPL(event_type) \
@ -49,8 +53,8 @@ struct zmk_event_subscription {
bool is_##event_type(const struct zmk_event_header *eh) { \ bool is_##event_type(const struct zmk_event_header *eh) { \
return eh->event == &zmk_event_##event_type; \ return eh->event == &zmk_event_##event_type; \
}; \ }; \
const struct event_type* cast_##event_type(const struct zmk_event_header *eh) {\ struct event_type* cast_##event_type(const struct zmk_event_header *eh) {\
return (const struct event_type*)eh; \ return (struct event_type*)eh; \
}; };
@ -68,4 +72,8 @@ struct zmk_event_subscription {
#define ZMK_EVENT_RAISE(ev) \ #define ZMK_EVENT_RAISE(ev) \
zmk_event_manager_raise((struct zmk_event_header *)ev); zmk_event_manager_raise((struct zmk_event_header *)ev);
#define ZMK_EVENT_RELEASE(ev) \
zmk_event_manager_release((struct zmk_event_header *)ev);
int zmk_event_manager_raise(struct zmk_event_header *event); int zmk_event_manager_raise(struct zmk_event_header *event);
int zmk_event_manager_release(struct zmk_event_header *event);

34
app/src/event_manager.c

@ -17,16 +17,30 @@ 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[];
int zmk_event_manager_raise(struct zmk_event_header *event)
int zmk_event_manager_handle_from(struct zmk_event_header *event, u8_t start_index)
{ {
int ret; int ret = 0;
struct zmk_event_subscription *ev_sub; u8_t len = __event_subscriptions_end - __event_subscriptions_start;
for (ev_sub = __event_subscriptions_start; ev_sub != __event_subscriptions_end; ev_sub++) { for (int i = start_index + 1; i < len; i++) {
struct zmk_event_subscription *ev_sub = __event_subscriptions_start + i;
if (ev_sub->event_type == event->event) { if (ev_sub->event_type == event->event) {
ret = ev_sub->listener->callback(event); ret = ev_sub->listener->callback(event);
if (ret) { if (ret < 0) {
LOG_DBG("Listener returned an error: %d", ret); LOG_DBG("Listener returned an error: %d", ret);
goto release; goto release;
} else if (ret > 0) {
switch (ret) {
case ZMK_EV_EVENT_HANDLED:
LOG_DBG("Listener handled the event");
ret = 0;
goto release;
case ZMK_EV_EVENT_CAPTURED:
LOG_DBG("Listener captured the event");
event->last_listener_index = i;
// Listeners are expected to free events they capture
return 0;
}
} }
} }
} }
@ -34,4 +48,14 @@ int zmk_event_manager_raise(struct zmk_event_header *event)
release: release:
k_free(event); k_free(event);
return ret; return ret;
}
int zmk_event_manager_raise(struct zmk_event_header *event)
{
return zmk_event_manager_handle_from(event, 0);
}
int zmk_event_manager_release(struct zmk_event_header *event)
{
return zmk_event_manager_handle_from(event, event->last_listener_index + 1);
} }
Loading…
Cancel
Save