Merge pull request #68 from petejohanson/core/ev-manager-capture-release-events
Add the ability to capture event and release later
This commit is contained in:
commit
f269b26ea1
2 changed files with 40 additions and 8 deletions
|
@ -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);
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,3 +49,13 @@ 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…
Reference in a new issue