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 22038b1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
5 changes: 4 additions & 1 deletion app/include/zmk/event_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ 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) \
((__ASSERT((uint8_t *)&ev.header - (uint8_t *)&ev == 0, \
"header must be first element of event")), \
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);
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 22038b1

Please sign in to comment.