From a827778ed25f2581aa9301a7ffd04fc3fea5f534 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 23 Mar 2023 08:56:02 +0000 Subject: [PATCH] refactor(core): Move to stack allocated events. * Move to local/stack allocated event API that doesn't require dynamic allocation/freeing. * Disable heap, we no longer use alloc/free unless using LVGL. * Tons of refactors all over to account for the new event approach. --- app/Kconfig | 2 +- app/include/zmk/event_manager.h | 25 +++--- .../zmk/events/keycode_state_changed.h | 15 ++-- app/include/zmk/events/layer_state_changed.h | 4 +- app/src/activity.c | 4 +- app/src/battery.c | 4 +- app/src/behaviors/behavior_hold_tap.c | 88 ++++++++++++------ app/src/behaviors/behavior_key_press.c | 4 +- app/src/behaviors/behavior_key_repeat.c | 4 +- app/src/behaviors/behavior_key_toggle.c | 2 +- app/src/behaviors/behavior_sticky_key.c | 4 +- app/src/ble.c | 4 +- app/src/combo.c | 90 +++++++++---------- app/src/endpoints.c | 4 +- app/src/event_manager.c | 10 +-- app/src/keymap.c | 3 +- app/src/kscan.c | 4 +- app/src/sensors.c | 4 +- app/src/split/bluetooth/central.c | 2 +- app/src/split/bluetooth/peripheral.c | 8 +- app/src/usb.c | 4 +- app/src/wpm.c | 3 +- 22 files changed, 158 insertions(+), 134 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 89a128b5a27e..9e860a10ecf6 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -519,7 +519,7 @@ endmenu endmenu config HEAP_MEM_POOL_SIZE - default 8192 + default 8192 if ZMK_DISPLAY config KERNEL_BIN_NAME default "zmk" diff --git a/app/include/zmk/event_manager.h b/app/include/zmk/event_manager.h index aa9942ea366a..100c25904396 100644 --- a/app/include/zmk/event_manager.h +++ b/app/include/zmk/event_manager.h @@ -38,7 +38,8 @@ struct zmk_event_subscription { zmk_event_t header; \ struct event_type data; \ }; \ - struct event_type##_event *new_##event_type(struct event_type); \ + struct event_type##_event new_##event_type(struct event_type); \ + int raise_new_##event_type(struct event_type); \ struct event_type *as_##event_type(const zmk_event_t *eh); \ extern const struct zmk_event_type zmk_event_##event_type; @@ -46,13 +47,15 @@ struct zmk_event_subscription { 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; \ - 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)); \ - ev->header.event = &zmk_event_##event_type; \ - ev->data = data; \ + struct event_type##_event new_##event_type(struct event_type data) { \ + struct event_type##_event ev = {.data = data}; \ + ev.header.event = &zmk_event_##event_type; \ return ev; \ }; \ + int raise_new_##event_type(struct event_type data) { \ + struct event_type##_event ev = new_##event_type(data); \ + return ZMK_EVENT_RAISE(ev); \ + }; \ 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; \ @@ -68,17 +71,15 @@ struct zmk_event_subscription { .listener = &zmk_listener_##mod, \ }; -#define ZMK_EVENT_RAISE(ev) zmk_event_manager_raise((zmk_event_t *)ev); +#define ZMK_EVENT_RAISE(ev) zmk_event_manager_raise((zmk_event_t *)&ev); #define ZMK_EVENT_RAISE_AFTER(ev, mod) \ - zmk_event_manager_raise_after((zmk_event_t *)ev, &zmk_listener_##mod); + zmk_event_manager_raise_after((zmk_event_t *)&ev, &zmk_listener_##mod); #define ZMK_EVENT_RAISE_AT(ev, mod) \ - 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); + zmk_event_manager_raise_at((zmk_event_t *)&ev, &zmk_listener_##mod); -#define ZMK_EVENT_FREE(ev) k_free((void *)ev); +#define ZMK_EVENT_RELEASE(ev) zmk_event_manager_release((zmk_event_t *)&ev); int zmk_event_manager_raise(zmk_event_t *event); int zmk_event_manager_raise_after(zmk_event_t *event, const struct zmk_listener *listener); diff --git a/app/include/zmk/events/keycode_state_changed.h b/app/include/zmk/events/keycode_state_changed.h index c3a3ed30d034..20a463515527 100644 --- a/app/include/zmk/events/keycode_state_changed.h +++ b/app/include/zmk/events/keycode_state_changed.h @@ -21,7 +21,7 @@ struct zmk_keycode_state_changed { ZMK_EVENT_DECLARE(zmk_keycode_state_changed); -static inline struct zmk_keycode_state_changed_event * +static inline struct zmk_keycode_state_changed zmk_keycode_state_changed_from_encoded(uint32_t encoded, bool pressed, int64_t timestamp) { uint16_t page = ZMK_HID_USAGE_PAGE(encoded); uint16_t id = ZMK_HID_USAGE_ID(encoded); @@ -38,11 +38,10 @@ zmk_keycode_state_changed_from_encoded(uint32_t encoded, bool pressed, int64_t t implicit_modifiers = SELECT_MODS(encoded); } - return new_zmk_keycode_state_changed( - (struct zmk_keycode_state_changed){.usage_page = page, - .keycode = id, - .implicit_modifiers = implicit_modifiers, - .explicit_modifiers = explicit_modifiers, - .state = pressed, - .timestamp = timestamp}); + return (struct zmk_keycode_state_changed){.usage_page = page, + .keycode = id, + .implicit_modifiers = implicit_modifiers, + .explicit_modifiers = explicit_modifiers, + .state = pressed, + .timestamp = timestamp}; } diff --git a/app/include/zmk/events/layer_state_changed.h b/app/include/zmk/events/layer_state_changed.h index 405d1365b7a9..cef779333261 100644 --- a/app/include/zmk/events/layer_state_changed.h +++ b/app/include/zmk/events/layer_state_changed.h @@ -17,8 +17,8 @@ struct zmk_layer_state_changed { ZMK_EVENT_DECLARE(zmk_layer_state_changed); -static inline struct zmk_layer_state_changed_event *create_layer_state_changed(uint8_t layer, - bool state) { +static inline struct zmk_layer_state_changed_event create_layer_state_changed(uint8_t layer, + bool state) { return new_zmk_layer_state_changed((struct zmk_layer_state_changed){ .layer = layer, .state = state, .timestamp = k_uptime_get()}); } diff --git a/app/src/activity.c b/app/src/activity.c index 41fe2e15dc41..6d1e6573447a 100644 --- a/app/src/activity.c +++ b/app/src/activity.c @@ -43,8 +43,8 @@ static uint32_t activity_last_uptime; #endif int raise_event() { - return ZMK_EVENT_RAISE(new_zmk_activity_state_changed( - (struct zmk_activity_state_changed){.state = activity_state})); + return raise_new_zmk_activity_state_changed( + (struct zmk_activity_state_changed){.state = activity_state}); } int set_state(enum zmk_activity_state state) { diff --git a/app/src/battery.c b/app/src/battery.c index 87a25e08a2dd..e9ac41167b9c 100644 --- a/app/src/battery.c +++ b/app/src/battery.c @@ -61,8 +61,8 @@ static int zmk_battery_update(const struct device *battery) { return rc; } - rc = ZMK_EVENT_RAISE(new_zmk_battery_state_changed( - (struct zmk_battery_state_changed){.state_of_charge = last_state_of_charge})); + rc = raise_new_zmk_battery_state_changed( + (struct zmk_battery_state_changed){.state_of_charge = last_state_of_charge}); } return rc; diff --git a/app/src/behaviors/behavior_hold_tap.c b/app/src/behaviors/behavior_hold_tap.c index 30350ef24cee..f4bbd362eaea 100644 --- a/app/src/behaviors/behavior_hold_tap.c +++ b/app/src/behaviors/behavior_hold_tap.c @@ -88,7 +88,24 @@ struct active_hold_tap { struct active_hold_tap *undecided_hold_tap = NULL; struct active_hold_tap active_hold_taps[ZMK_BHV_HOLD_TAP_MAX_HELD] = {}; // We capture most position_state_changed events and some modifiers_state_changed events. -const zmk_event_t *captured_events[ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS] = {}; + +enum captured_event_tag { + ET_NONE, + ET_POS_CHANGED, + ET_CODE_CHANGED, +}; + +union captured_event_data { + struct zmk_position_state_changed_event position; + struct zmk_keycode_state_changed_event keycode; +}; + +struct captured_event { + enum captured_event_tag tag; + union captured_event_data data; +}; + +struct captured_event captured_events[ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS] = {}; // Keep track of which key was tapped most recently for the standard, if it is a hold-tap // a position, will be given, if not it will just be INT32_MIN @@ -119,33 +136,32 @@ static bool is_quick_tap(struct active_hold_tap *hold_tap) { } } -static int capture_event(const zmk_event_t *event) { +static int capture_event(struct captured_event data) { for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS; i++) { - if (captured_events[i] == NULL) { - captured_events[i] = event; + if (captured_events[i].tag == ET_NONE) { + memcpy(&captured_events[i], &data, sizeof(struct captured_event)); return 0; } } return -ENOMEM; } -static struct zmk_position_state_changed *find_captured_keydown_event(uint32_t position) { - struct zmk_position_state_changed *last_match = NULL; +static bool have_captured_keydown_event(uint32_t position) { for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS; i++) { - const zmk_event_t *eh = captured_events[i]; - if (eh == NULL) { - return last_match; + struct captured_event ev = captured_events[i]; + if (ev.tag == ET_NONE) { + return false; } - struct zmk_position_state_changed *position_event = as_zmk_position_state_changed(eh); - if (position_event == NULL) { + + if (ev.tag != ET_POS_CHANGED) { continue; } - if (position_event->position == position && position_event->state) { - last_match = position_event; + if (ev.data.position.data.position == position && ev.data.position.data.state) { + return true; } } - return last_match; + return false; } const struct zmk_listener zmk_listener_behavior_hold_tap; @@ -181,25 +197,33 @@ static void release_captured_events() { // [k1_down, k1_up, null, null, null, ...] // now mt2 will start releasing it's own captured positions. for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS; i++) { - const zmk_event_t *captured_event = captured_events[i]; - if (captured_event == NULL) { + struct captured_event captured_event = captured_events[i]; + if (captured_event.tag == ET_NONE) { return; } - captured_events[i] = NULL; + + captured_events[i].tag = ET_NONE; if (undecided_hold_tap != NULL) { k_msleep(10); } - struct zmk_position_state_changed *position_event; - struct zmk_keycode_state_changed *modifier_event; - if ((position_event = as_zmk_position_state_changed(captured_event)) != NULL) { - LOG_DBG("Releasing key position event for position %d %s", position_event->position, - (position_event->state ? "pressed" : "released")); - } else if ((modifier_event = as_zmk_keycode_state_changed(captured_event)) != NULL) { - LOG_DBG("Releasing mods changed event 0x%02X %s", modifier_event->keycode, - (modifier_event->state ? "pressed" : "released")); + switch (captured_event.tag) { + case ET_CODE_CHANGED: + LOG_DBG("Releasing mods changed event 0x%02X %s", + captured_event.data.keycode.data.keycode, + (captured_event.data.keycode.data.state ? "pressed" : "released")); + ZMK_EVENT_RAISE_AT(captured_event.data.keycode, behavior_hold_tap); + break; + case ET_POS_CHANGED: + LOG_DBG("Releasing key position event for position %d %s", + captured_event.data.position.data.position, + (captured_event.data.position.data.state ? "pressed" : "released")); + ZMK_EVENT_RAISE_AT(captured_event.data.position, behavior_hold_tap); + break; + default: + LOG_ERR("Unhandled captured event type"); + break; } - ZMK_EVENT_RAISE_AT(captured_event, behavior_hold_tap); } } @@ -619,7 +643,7 @@ static int position_state_changed_listener(const zmk_event_t *eh) { return ZMK_EV_EVENT_BUBBLE; } - if (!ev->state && find_captured_keydown_event(ev->position) == NULL) { + if (!ev->state && !have_captured_keydown_event(ev->position)) { // no keydown event has been captured, let it bubble. // we'll catch modifiers later in modifier_state_changed_listener LOG_DBG("%d bubbling %d %s event", undecided_hold_tap->position, ev->position, @@ -629,7 +653,10 @@ static int position_state_changed_listener(const zmk_event_t *eh) { LOG_DBG("%d capturing %d %s event", undecided_hold_tap->position, ev->position, ev->state ? "down" : "up"); - capture_event(eh); + struct captured_event capture; + capture.tag = ET_POS_CHANGED; + memcpy(&capture.data.position, eh, sizeof(struct zmk_position_state_changed_event)); + capture_event(capture); decide_hold_tap(undecided_hold_tap, ev->state ? HT_OTHER_KEY_DOWN : HT_OTHER_KEY_UP); return ZMK_EV_EVENT_CAPTURED; } @@ -656,7 +683,10 @@ static int keycode_state_changed_listener(const zmk_event_t *eh) { // if a undecided_hold_tap is active. LOG_DBG("%d capturing 0x%02X %s event", undecided_hold_tap->position, ev->keycode, ev->state ? "down" : "up"); - capture_event(eh); + struct captured_event capture; + capture.tag = ET_CODE_CHANGED; + memcpy(&capture.data.keycode, eh, sizeof(struct zmk_keycode_state_changed_event)); + capture_event(capture); return ZMK_EV_EVENT_CAPTURED; } diff --git a/app/src/behaviors/behavior_key_press.c b/app/src/behaviors/behavior_key_press.c index 2765db9f3ca4..11bd6b99e9b4 100644 --- a/app/src/behaviors/behavior_key_press.c +++ b/app/src/behaviors/behavior_key_press.c @@ -21,14 +21,14 @@ static int behavior_key_press_init(const struct device *dev) { return 0; }; static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event) { LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1); - return ZMK_EVENT_RAISE( + return raise_new_zmk_keycode_state_changed( zmk_keycode_state_changed_from_encoded(binding->param1, true, event.timestamp)); } static int on_keymap_binding_released(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event) { LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1); - return ZMK_EVENT_RAISE( + return raise_new_zmk_keycode_state_changed( zmk_keycode_state_changed_from_encoded(binding->param1, false, event.timestamp)); } diff --git a/app/src/behaviors/behavior_key_repeat.c b/app/src/behaviors/behavior_key_repeat.c index 033f498b8751..c9bcf1b98e51 100644 --- a/app/src/behaviors/behavior_key_repeat.c +++ b/app/src/behaviors/behavior_key_repeat.c @@ -43,7 +43,7 @@ static int on_key_repeat_binding_pressed(struct zmk_behavior_binding *binding, sizeof(struct zmk_keycode_state_changed)); data->current_keycode_pressed.timestamp = k_uptime_get(); - ZMK_EVENT_RAISE(new_zmk_keycode_state_changed(data->current_keycode_pressed)); + raise_new_zmk_keycode_state_changed(data->current_keycode_pressed); return ZMK_BEHAVIOR_OPAQUE; } @@ -60,7 +60,7 @@ static int on_key_repeat_binding_released(struct zmk_behavior_binding *binding, data->current_keycode_pressed.timestamp = k_uptime_get(); data->current_keycode_pressed.state = false; - ZMK_EVENT_RAISE(new_zmk_keycode_state_changed(data->current_keycode_pressed)); + raise_new_zmk_keycode_state_changed(data->current_keycode_pressed); return ZMK_BEHAVIOR_OPAQUE; } diff --git a/app/src/behaviors/behavior_key_toggle.c b/app/src/behaviors/behavior_key_toggle.c index cbbdd0d91aca..a57bd28b2ea0 100644 --- a/app/src/behaviors/behavior_key_toggle.c +++ b/app/src/behaviors/behavior_key_toggle.c @@ -23,7 +23,7 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event) { LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1); bool pressed = zmk_hid_is_pressed(binding->param1); - return ZMK_EVENT_RAISE( + return raise_new_zmk_keycode_state_changed( zmk_keycode_state_changed_from_encoded(binding->param1, !pressed, event.timestamp)); } diff --git a/app/src/behaviors/behavior_sticky_key.c b/app/src/behaviors/behavior_sticky_key.c index 6697b9b1bbb1..659e58c59d40 100644 --- a/app/src/behaviors/behavior_sticky_key.c +++ b/app/src/behaviors/behavior_sticky_key.c @@ -234,7 +234,9 @@ static int sticky_key_keycode_state_changed_listener(const zmk_event_t *eh) { if (sticky_key->config->quick_release) { // immediately release the sticky key after the key press is handled. if (!event_reraised) { - ZMK_EVENT_RAISE_AFTER(eh, behavior_sticky_key); + struct zmk_keycode_state_changed_event dupe_ev; + memcpy(&dupe_ev, eh, sizeof(struct zmk_keycode_state_changed_event)); + ZMK_EVENT_RAISE_AFTER(dupe_ev, behavior_sticky_key); event_reraised = true; } release_sticky_key_behavior(sticky_key, ev_copy.timestamp); diff --git a/app/src/ble.c b/app/src/ble.c index 483bc9d79c1b..0987e2c012fc 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -83,8 +83,8 @@ static bt_addr_le_t peripheral_addrs[ZMK_SPLIT_BLE_PERIPHERAL_COUNT]; #endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) */ static void raise_profile_changed_event() { - ZMK_EVENT_RAISE(new_zmk_ble_active_profile_changed((struct zmk_ble_active_profile_changed){ - .index = active_profile, .profile = &profiles[active_profile]})); + raise_new_zmk_ble_active_profile_changed((struct zmk_ble_active_profile_changed){ + .index = active_profile, .profile = &profiles[active_profile]}); } static void raise_profile_changed_event_callback(struct k_work *work) { diff --git a/app/src/combo.c b/app/src/combo.c index 90c89c15e737..223edf60b933 100644 --- a/app/src/combo.c +++ b/app/src/combo.c @@ -45,7 +45,9 @@ struct active_combo { // key_positions_pressed is filled with key_positions when the combo is pressed. // The keys are removed from this array when they are released. // Once this array is empty, the behavior is released. - const zmk_event_t *key_positions_pressed[CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO]; + uint32_t key_positions_pressed_count; + struct zmk_position_state_changed_event + key_positions_pressed[CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO]; }; struct combo_candidate { @@ -56,8 +58,9 @@ struct combo_candidate { int64_t timeout_at; }; +uint32_t pressed_keys_count = 0; // set of keys pressed -const zmk_event_t *pressed_keys[CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO] = {NULL}; +struct zmk_position_state_changed_event pressed_keys[CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO] = {}; // the set of candidate combos based on the currently pressed_keys struct combo_candidate candidates[CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY]; // the last candidate that was completely pressed @@ -193,12 +196,7 @@ static inline bool candidate_is_completely_pressed(struct combo_cfg *candidate) // since events may have been reraised after clearing one or more slots at // the start of pressed_keys (see: release_pressed_keys), we have to check // that each key needed to trigger the combo was pressed, not just the last. - for (int i = 0; i < candidate->key_position_len; i++) { - if (pressed_keys[i] == NULL) { - return false; - } - } - return true; + return candidate->key_position_len == pressed_keys_count; } static int cleanup(); @@ -233,37 +231,34 @@ static int clear_candidates() { } static int capture_pressed_key(const zmk_event_t *ev) { - for (int i = 0; i < CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO; i++) { - if (pressed_keys[i] != NULL) { - continue; - } - pressed_keys[i] = ev; - return ZMK_EV_EVENT_CAPTURED; + if (pressed_keys_count == CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY) { + return 0; } - return 0; + + memcpy(&pressed_keys[pressed_keys_count++], ev, + sizeof(struct zmk_position_state_changed_event)); + return ZMK_EV_EVENT_CAPTURED; } const struct zmk_listener zmk_listener_combo; static int release_pressed_keys() { - for (int i = 0; i < CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO; i++) { - const zmk_event_t *captured_event = pressed_keys[i]; - if (pressed_keys[i] == NULL) { - return i; - } - pressed_keys[i] = NULL; + uint32_t count = pressed_keys_count; + pressed_keys_count = 0; + for (int i = 0; i < count; i++) { + struct zmk_position_state_changed_event ev = pressed_keys[i]; if (i == 0) { - LOG_DBG("combo: releasing position event %d", - as_zmk_position_state_changed(captured_event)->position); - ZMK_EVENT_RELEASE(captured_event) + LOG_DBG("combo: releasing position event %d", ev.data.position); + ZMK_EVENT_RELEASE(ev) } else { // reprocess events (see tests/combo/fully-overlapping-combos-3 for why this is needed) - LOG_DBG("combo: reraising position event %d", - as_zmk_position_state_changed(captured_event)->position); - ZMK_EVENT_RAISE(captured_event); + LOG_DBG("combo: reraising position event %d", ev.data.position); + ZMK_EVENT_RAISE(ev); } } - return CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO; + + // pressed_keys_count = 0; + return count; } static inline int press_combo_behavior(struct combo_cfg *combo, int32_t timestamp) { @@ -285,19 +280,19 @@ static inline int release_combo_behavior(struct combo_cfg *combo, int32_t timest } static void move_pressed_keys_to_active_combo(struct active_combo *active_combo) { - int combo_length = active_combo->combo->key_position_len; + + int combo_length = MIN(pressed_keys_count, active_combo->combo->key_position_len); for (int i = 0; i < combo_length; i++) { active_combo->key_positions_pressed[i] = pressed_keys[i]; - pressed_keys[i] = NULL; } + active_combo->key_positions_pressed_count = combo_length; + // move any other pressed keys up - for (int i = 0; i + combo_length < CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO; i++) { - if (pressed_keys[i + combo_length] == NULL) { - return; - } + for (int i = 0; i + combo_length < pressed_keys_count; i++) { pressed_keys[i] = pressed_keys[i + combo_length]; - pressed_keys[i + combo_length] = NULL; } + + pressed_keys_count -= combo_length; } static struct active_combo *store_active_combo(struct combo_cfg *combo) { @@ -322,8 +317,7 @@ static void activate_combo(struct combo_cfg *combo) { return; } move_pressed_keys_to_active_combo(active_combo); - press_combo_behavior( - combo, as_zmk_position_state_changed(active_combo->key_positions_pressed[0])->timestamp); + press_combo_behavior(combo, active_combo->key_positions_pressed[0].data.timestamp); } static void deactivate_combo(int active_combo_index) { @@ -342,22 +336,22 @@ static bool release_combo_key(int32_t position, int64_t timestamp) { struct active_combo *active_combo = &active_combos[combo_idx]; bool key_released = false; - bool all_keys_pressed = true; + bool all_keys_pressed = + active_combo->key_positions_pressed_count == active_combo->combo->key_position_len; bool all_keys_released = true; - for (int i = 0; i < active_combo->combo->key_position_len; i++) { - if (active_combo->key_positions_pressed[i] == NULL) { - all_keys_pressed = false; - } else if (as_zmk_position_state_changed(active_combo->key_positions_pressed[i]) - ->position != position) { + for (int i = 0; i < active_combo->key_positions_pressed_count; i++) { + if (key_released) { + active_combo->key_positions_pressed[i - 1] = active_combo->key_positions_pressed[i]; + all_keys_released = false; + } else if (active_combo->key_positions_pressed[i].data.position != position) { all_keys_released = false; - } else { // not null and position matches - ZMK_EVENT_FREE(active_combo->key_positions_pressed[i]); - active_combo->key_positions_pressed[i] = NULL; + } else { // position matches key_released = true; } } if (key_released) { + active_combo->key_positions_pressed_count--; if ((active_combo->combo->slow_release && all_keys_released) || (!active_combo->combo->slow_release && all_keys_pressed)) { release_combo_behavior(active_combo->combo, timestamp); @@ -438,7 +432,9 @@ static int position_state_up(const zmk_event_t *ev, struct zmk_position_state_ch if (released_keys > 1) { // The second and further key down events are re-raised. To preserve // correct order for e.g. hold-taps, reraise the key up event too. - ZMK_EVENT_RAISE(ev); + struct zmk_position_state_changed_event dupe_ev; + memcpy(&dupe_ev, ev, sizeof(struct zmk_position_state_changed_event)); + ZMK_EVENT_RAISE(dupe_ev); return ZMK_EV_EVENT_CAPTURED; } return 0; diff --git a/app/src/endpoints.c b/app/src/endpoints.c index dbd1a3e6c0e7..8ad64b3cf006 100644 --- a/app/src/endpoints.c +++ b/app/src/endpoints.c @@ -243,8 +243,8 @@ static void update_current_endpoint() { current_endpoint = new_endpoint; LOG_INF("Endpoint changed: %d", current_endpoint); - ZMK_EVENT_RAISE(new_zmk_endpoint_selection_changed( - (struct zmk_endpoint_selection_changed){.endpoint = current_endpoint})); + raise_new_zmk_endpoint_selection_changed( + (struct zmk_endpoint_selection_changed){.endpoint = current_endpoint}); } } diff --git a/app/src/event_manager.c b/app/src/event_manager.c index 0f4a5547cc22..c28da97f5944 100644 --- a/app/src/event_manager.c +++ b/app/src/event_manager.c @@ -32,21 +32,17 @@ int zmk_event_manager_handle_from(zmk_event_t *event, uint8_t start_index) { continue; case ZMK_EV_EVENT_HANDLED: LOG_DBG("Listener handled the event"); - ret = 0; - goto release; + return 0; 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; + return ret; } } -release: - k_free(event); - return ret; + return 0; } int zmk_event_manager_raise(zmk_event_t *event) { return zmk_event_manager_handle_from(event, 0); } diff --git a/app/src/keymap.c b/app/src/keymap.c index bda694276c84..33d50b82f1bc 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -93,7 +93,8 @@ static inline int set_layer_state(uint8_t layer, bool state) { // Don't send state changes unless there was an actual change if (old_state != _zmk_keymap_layer_state) { LOG_DBG("layer_changed: layer %d state %d", layer, state); - ZMK_EVENT_RAISE(create_layer_state_changed(layer, state)); + struct zmk_layer_state_changed_event ev = create_layer_state_changed(layer, state); + ZMK_EVENT_RAISE(ev); } return 0; diff --git a/app/src/kscan.c b/app/src/kscan.c index 62d0cf0756e0..c8d3fad178de 100644 --- a/app/src/kscan.c +++ b/app/src/kscan.c @@ -57,11 +57,11 @@ void zmk_kscan_process_msgq(struct k_work *item) { LOG_DBG("Row: %d, col: %d, position: %d, pressed: %s", ev.row, ev.column, position, (pressed ? "true" : "false")); - ZMK_EVENT_RAISE(new_zmk_position_state_changed( + raise_new_zmk_position_state_changed( (struct zmk_position_state_changed){.source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, .state = pressed, .position = position, - .timestamp = k_uptime_get()})); + .timestamp = k_uptime_get()}); } } diff --git a/app/src/sensors.c b/app/src/sensors.c index 60f2bd2a33f8..c03b38c5c37e 100644 --- a/app/src/sensors.c +++ b/app/src/sensors.c @@ -87,12 +87,12 @@ static void trigger_sensor_data_for_position(uint32_t sensor_index) { return; } - ZMK_EVENT_RAISE(new_zmk_sensor_event( + raise_new_zmk_sensor_event( (struct zmk_sensor_event){.sensor_index = item->sensor_index, .channel_data_size = 1, .channel_data = {(struct zmk_sensor_channel_data){ .value = value, .channel = item->trigger.chan}}, - .timestamp = k_uptime_get()})); + .timestamp = k_uptime_get()}); } static void run_sensors_data_trigger(struct k_work *work) { diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 860e89a5f3ef..089005d82c57 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -63,7 +63,7 @@ void peripheral_event_work_callback(struct k_work *work) { struct zmk_position_state_changed ev; while (k_msgq_get(&peripheral_event_msgq, &ev, K_NO_WAIT) == 0) { LOG_DBG("Trigger key position state change for %d", ev.position); - ZMK_EVENT_RAISE(new_zmk_position_state_changed(ev)); + raise_new_zmk_position_state_changed(ev); } } diff --git a/app/src/split/bluetooth/peripheral.c b/app/src/split/bluetooth/peripheral.c index 1d649f712219..3e63868637c4 100644 --- a/app/src/split/bluetooth/peripheral.c +++ b/app/src/split/bluetooth/peripheral.c @@ -79,8 +79,8 @@ K_WORK_DEFINE(advertising_work, advertising_cb); static void connected(struct bt_conn *conn, uint8_t err) { is_connected = (err == 0); - ZMK_EVENT_RAISE(new_zmk_split_peripheral_status_changed( - (struct zmk_split_peripheral_status_changed){.connected = is_connected})); + raise_new_zmk_split_peripheral_status_changed( + (struct zmk_split_peripheral_status_changed){.connected = is_connected}); if (err == BT_HCI_ERR_ADV_TIMEOUT) { low_duty_advertising = true; @@ -97,8 +97,8 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) { is_connected = false; - ZMK_EVENT_RAISE(new_zmk_split_peripheral_status_changed( - (struct zmk_split_peripheral_status_changed){.connected = is_connected})); + raise_new_zmk_split_peripheral_status_changed( + (struct zmk_split_peripheral_status_changed){.connected = is_connected}); low_duty_advertising = false; k_work_submit(&advertising_work); diff --git a/app/src/usb.c b/app/src/usb.c index cf04ef46c8fb..a7b365ded1cd 100644 --- a/app/src/usb.c +++ b/app/src/usb.c @@ -20,8 +20,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN; static void raise_usb_status_changed_event(struct k_work *_work) { - ZMK_EVENT_RAISE(new_zmk_usb_conn_state_changed( - (struct zmk_usb_conn_state_changed){.conn_state = zmk_usb_get_conn_state()})); + raise_new_zmk_usb_conn_state_changed( + (struct zmk_usb_conn_state_changed){.conn_state = zmk_usb_get_conn_state()}); } K_WORK_DEFINE(usb_status_notifier_work, raise_usb_status_changed_event); diff --git a/app/src/wpm.c b/app/src/wpm.c index 00a5942ecb8b..827a30d66676 100644 --- a/app/src/wpm.c +++ b/app/src/wpm.c @@ -54,8 +54,7 @@ void wpm_work_handler(struct k_work *work) { if (last_wpm_state != wpm_state) { LOG_DBG("Raised WPM state changed %d wpm_update_counter %d", wpm_state, wpm_update_counter); - ZMK_EVENT_RAISE( - new_zmk_wpm_state_changed((struct zmk_wpm_state_changed){.state = wpm_state})); + raise_new_zmk_wpm_state_changed((struct zmk_wpm_state_changed){.state = wpm_state}); last_wpm_state = wpm_state; }