zmk_mf68/app/src/event_manager.c
Pete Johanson fc511e40cc
fix(behaviors): Fixing erroneous combo triggering, hold-taps sticking
* This is a very simple fix to a rather complicated issue. Essentially,
hold-taps will "release" (raise) their captured keys before actually
telling the event manager they have captured a key. This means the event
manager ends up assigning the `last_listener_index` to the hold-tap
subscription rather than the combo. So when the combo calls
`ZMK_EVENT_RELEASE` it raises after the hold-tap instead of after the
combo as the combo code expects.
* The corresponding test (which fails without this change) has also been added.
* An event can be captured and released in the same event handler, before
the last_listener_index would have been updated. This causes some handlers
to be triggered multiple times.
* The solution is to update the last_listener_index before calling the next
event handler, so capturing and releasing within an event handler is harmless.
* Also see discussion at https://github.com/zmkfirmware/zmk/pull/1401
* If our handler dedides our undedided hold-tap,
  return early before continuing.
* Fix incorrect pointer logic, resulting in combo
  candidate filtering leaving incorrect timeout details.

Co-authored-by: Andrew Rae <ajrae.nv@gmail.com>
Co-authored-by: okke <okke@formsma.nl>
2022-08-03 20:09:50 -04:00

86 lines
2.7 KiB
C

/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#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_handle_from(zmk_event_t *event, uint8_t start_index) {
int ret = 0;
uint8_t len = __event_subscriptions_end - __event_subscriptions_start;
for (int i = start_index; i < len; i++) {
struct zmk_event_subscription *ev_sub = __event_subscriptions_start + i;
if (ev_sub->event_type != event->event) {
continue;
}
event->last_listener_index = i;
ret = ev_sub->listener->callback(event);
switch (ret) {
case ZMK_EV_EVENT_BUBBLE:
continue;
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");
// Listeners are expected to free events they capture
return 0;
default:
LOG_DBG("Listener returned an error: %d", ret);
goto release;
}
}
release:
k_free(event);
return ret;
}
int zmk_event_manager_raise(zmk_event_t *event) { return zmk_event_manager_handle_from(event, 0); }
int zmk_event_manager_raise_after(zmk_event_t *event, const struct zmk_listener *listener) {
uint8_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 + 1);
}
}
LOG_WRN("Unable to find where to raise this after event");
return -EINVAL;
}
int zmk_event_manager_raise_at(zmk_event_t *event, const struct zmk_listener *listener) {
uint8_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);
}
}
LOG_WRN("Unable to find where to raise this event");
return -EINVAL;
}
int zmk_event_manager_release(zmk_event_t *event) {
return zmk_event_manager_handle_from(event, event->last_listener_index + 1);
}