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
|
|
|
#include <zephyr.h>
|
|
|
|
#include <logging/log.h>
|
|
|
|
|
|
|
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
|
|
|
|
|
|
|
#include <zmk/event-manager.h>
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
extern struct zmk_event_type *__event_type_start[];
|
|
|
|
extern struct zmk_event_type *__event_type_end[];
|
2020-06-30 14:31:09 +10:00
|
|
|
|
|
|
|
extern struct zmk_event_subscription __event_subscriptions_start[];
|
|
|
|
extern struct zmk_event_subscription __event_subscriptions_end[];
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_event_manager_handle_from(struct zmk_event_header *event, u8_t start_index) {
|
2020-07-30 14:13:32 +10:00
|
|
|
int ret = 0;
|
|
|
|
u8_t len = __event_subscriptions_end - __event_subscriptions_start;
|
2020-08-03 04:10:56 +10:00
|
|
|
for (int i = start_index; i < len; i++) {
|
2020-07-30 14:13:32 +10:00
|
|
|
struct zmk_event_subscription *ev_sub = __event_subscriptions_start + i;
|
2020-06-30 14:31:09 +10:00
|
|
|
if (ev_sub->event_type == event->event) {
|
|
|
|
ret = ev_sub->listener->callback(event);
|
2020-07-30 14:13:32 +10:00
|
|
|
if (ret < 0) {
|
2020-06-30 14:31:09 +10:00
|
|
|
LOG_DBG("Listener returned an error: %d", ret);
|
|
|
|
goto release;
|
2020-07-30 14:13:32 +10:00
|
|
|
} else if (ret > 0) {
|
|
|
|
switch (ret) {
|
2020-09-14 12:53:24 +10:00
|
|
|
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;
|
2020-07-30 14:13:32 +10:00
|
|
|
}
|
2020-06-30 14:31:09 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
release:
|
|
|
|
k_free(event);
|
|
|
|
return ret;
|
2020-07-30 14:13:32 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_event_manager_raise(struct zmk_event_header *event) {
|
2020-07-30 14:13:32 +10:00
|
|
|
return zmk_event_manager_handle_from(event, 0);
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_event_manager_raise_after(struct zmk_event_header *event,
|
|
|
|
const struct zmk_listener *listener) {
|
2020-07-31 14:07:16 +10:00
|
|
|
u8_t len = __event_subscriptions_end - __event_subscriptions_start;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
struct zmk_event_subscription *ev_sub = __event_subscriptions_start + i;
|
|
|
|
|
|
|
|
if (ev_sub->event_type == event->event && ev_sub->listener == listener) {
|
2020-09-14 12:53:24 +10:00
|
|
|
return zmk_event_manager_handle_from(event, i + 1);
|
2020-07-31 14:07:16 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_WRN("Unable to find where to raise this after event");
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_event_manager_raise_at(struct zmk_event_header *event,
|
|
|
|
const struct zmk_listener *listener) {
|
2020-09-02 23:11:56 +10:00
|
|
|
u8_t len = __event_subscriptions_end - __event_subscriptions_start;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
struct zmk_event_subscription *ev_sub = __event_subscriptions_start + i;
|
|
|
|
|
|
|
|
if (ev_sub->event_type == event->event && ev_sub->listener == listener) {
|
|
|
|
return zmk_event_manager_handle_from(event, i);
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 22:37:37 +10:00
|
|
|
|
2020-09-02 23:11:56 +10:00
|
|
|
LOG_WRN("Unable to find where to raise this event");
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-09-01 22:37:37 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_event_manager_release(struct zmk_event_header *event) {
|
2020-09-02 23:11:56 +10:00
|
|
|
return zmk_event_manager_handle_from(event, event->last_listener_index + 1);
|
|
|
|
}
|