Skip to content

Commit

Permalink
fix(core): Address review comments from Joel.
Browse files Browse the repository at this point in the history
* Fix up some lingering events API tweaks for heap-less event manager.
  • Loading branch information
petejohanson committed Jan 14, 2024
1 parent 583cc1a commit dc0775c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
16 changes: 10 additions & 6 deletions app/include/zmk/event_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ struct zmk_event_subscription {
return *outer; \
}; \
int raise_##event_type(struct event_type data) { \
struct event_type##_event ev = {.data = data}; \
ev.header.event = &zmk_event_##event_type; \
struct event_type##_event ev = {.data = data, \
.header = {.event = &zmk_event_##event_type}}; \
return ZMK_EVENT_RAISE(ev); \
}; \
struct event_type *as_##event_type(const zmk_event_t *eh) { \
Expand All @@ -71,15 +71,19 @@ struct zmk_event_subscription {
.listener = &zmk_listener_##mod, \
};

#define ZMK_EVENT_RAISE(ev) zmk_event_manager_raise((zmk_event_t *)&ev);
#define ZMK_ASSERT_EVENT_LIKE(ev) \
(__ASSERT((uint8_t *)&(ev).header - (uint8_t *)&ev == 0, \
"header must be first element of event"))

#define ZMK_EVENT_RAISE(ev) (ZMK_ASSERT_EVENT_LIKE(ev), zmk_event_manager_raise(&(ev).header))

#define ZMK_EVENT_RAISE_AFTER(ev, mod) \
zmk_event_manager_raise_after((zmk_event_t *)&ev, &zmk_listener_##mod);
(ZMK_ASSERT_EVENT_LIKE(ev), zmk_event_manager_raise_after(&(ev).header, &zmk_listener_##mod))

#define ZMK_EVENT_RAISE_AT(ev, mod) \
zmk_event_manager_raise_at((zmk_event_t *)&ev, &zmk_listener_##mod);
(ZMK_ASSERT_EVENT_LIKE(ev), zmk_event_manager_raise_at(&(ev).header, &zmk_listener_##mod))

#define ZMK_EVENT_RELEASE(ev) zmk_event_manager_release((zmk_event_t *)&ev);
#define ZMK_EVENT_RELEASE(ev) (ZMK_ASSERT_EVENT_LIKE(ev), zmk_event_manager_release(&(ev).header))

int zmk_event_manager_raise(zmk_event_t *event);
int zmk_event_manager_raise_after(zmk_event_t *event, const struct zmk_listener *listener);
Expand Down
6 changes: 6 additions & 0 deletions app/include/zmk/events/keycode_state_changed.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ zmk_keycode_state_changed_from_encoded(uint32_t encoded, bool pressed, int64_t t
.state = pressed,
.timestamp = timestamp};
}

static inline int raise_zmk_keycode_state_changed_from_encoded(uint32_t encoded, bool pressed,
int64_t timestamp) {
return raise_zmk_keycode_state_changed(
zmk_keycode_state_changed_from_encoded(encoded, pressed, timestamp));
}
6 changes: 2 additions & 4 deletions app/src/behaviors/behavior_key_press.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ 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 raise_zmk_keycode_state_changed(
zmk_keycode_state_changed_from_encoded(binding->param1, true, event.timestamp));
return raise_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 raise_zmk_keycode_state_changed(
zmk_keycode_state_changed_from_encoded(binding->param1, false, event.timestamp));
return raise_zmk_keycode_state_changed_from_encoded(binding->param1, false, event.timestamp);
}

static const struct behavior_driver_api behavior_key_press_driver_api = {
Expand Down
3 changes: 1 addition & 2 deletions app/src/behaviors/behavior_key_toggle.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +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 raise_zmk_keycode_state_changed(
zmk_keycode_state_changed_from_encoded(binding->param1, !pressed, event.timestamp));
return raise_zmk_keycode_state_changed_from_encoded(binding->param1, !pressed, event.timestamp);
}

static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
Expand Down
4 changes: 2 additions & 2 deletions app/src/behaviors/behavior_sticky_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ 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) {
struct zmk_keycode_state_changed_event dupe_ev;
memcpy(&dupe_ev, eh, sizeof(struct zmk_keycode_state_changed_event));
struct zmk_keycode_state_changed_event dupe_ev =
copy_raised_zmk_keycode_state_changed(ev);
ZMK_EVENT_RAISE_AFTER(dupe_ev, behavior_sticky_key);
event_reraised = true;
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/combo.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ static int release_pressed_keys() {
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];
struct zmk_position_state_changed_event *ev = &pressed_keys[i];
if (i == 0) {
LOG_DBG("combo: releasing position event %d", ev.data.position);
ZMK_EVENT_RELEASE(ev)
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", ev.data.position);
ZMK_EVENT_RAISE(ev);
LOG_DBG("combo: reraising position event %d", ev->data.position);
ZMK_EVENT_RAISE(*ev);
}
}

Expand Down

0 comments on commit dc0775c

Please sign in to comment.