From 3368a81057d4981aa259c5548050d95739d99d51 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Tue, 19 Jan 2021 14:21:00 -0500 Subject: [PATCH] refactor(core): Combine `is_` and `cast_` event functions. * Use a single `as_foo` generated function to conditionally return a certain event type from a generic `zmk_event_t*` pointer. --- app/include/zmk/event_manager.h | 9 ++++----- app/src/behaviors/behavior_hold_tap.c | 24 ++++++++++++------------ app/src/behaviors/behavior_sticky_key.c | 4 ++-- app/src/combo.c | 8 ++++---- app/src/display/main.c | 6 +++++- app/src/hid_listener.c | 4 ++-- app/src/keymap.c | 20 ++++++++++++-------- app/src/split_listener.c | 4 ++-- 8 files changed, 43 insertions(+), 36 deletions(-) diff --git a/app/include/zmk/event_manager.h b/app/include/zmk/event_manager.h index 8fc3f197..5acb26eb 100644 --- a/app/include/zmk/event_manager.h +++ b/app/include/zmk/event_manager.h @@ -39,8 +39,7 @@ struct zmk_event_subscription { struct event_type data; \ }; \ struct event_type##_event *new_##event_type(struct event_type); \ - bool is_##event_type(const zmk_event_t *eh); \ - struct event_type *cast_##event_type(const zmk_event_t *eh); \ + struct event_type *as_##event_type(const zmk_event_t *eh); \ extern const struct zmk_event_type zmk_event_##event_type; #define ZMK_EVENT_IMPL(event_type) \ @@ -54,9 +53,9 @@ struct zmk_event_subscription { ev->data = data; \ return ev; \ }; \ - bool is_##event_type(const zmk_event_t *eh) { return eh->event == &zmk_event_##event_type; }; \ - struct event_type *cast_##event_type(const zmk_event_t *eh) { \ - return &((struct event_type##_event *)eh)->data; \ + 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; \ }; #define ZMK_LISTENER(mod, cb) const struct zmk_listener zmk_listener_##mod = {.callback = cb}; diff --git a/app/src/behaviors/behavior_hold_tap.c b/app/src/behaviors/behavior_hold_tap.c index 01851e91..de356337 100644 --- a/app/src/behaviors/behavior_hold_tap.c +++ b/app/src/behaviors/behavior_hold_tap.c @@ -88,10 +88,11 @@ static struct zmk_position_state_changed *find_captured_keydown_event(uint32_t p if (eh == NULL) { return last_match; } - if (!is_zmk_position_state_changed(eh)) { + struct zmk_position_state_changed *position_event = as_zmk_position_state_changed(eh); + if (position_event == NULL) { continue; } - struct zmk_position_state_changed *position_event = cast_zmk_position_state_changed(eh); + if (position_event->position == position && position_event->state) { last_match = position_event; } @@ -140,14 +141,13 @@ static void release_captured_events() { if (undecided_hold_tap != NULL) { k_msleep(10); } - if (is_zmk_position_state_changed(captured_event)) { - struct zmk_position_state_changed *position_event = - cast_zmk_position_state_changed(captured_event); + + 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 { - struct zmk_keycode_state_changed *modifier_event = - cast_zmk_keycode_state_changed(captured_event); + } 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")); } @@ -388,7 +388,7 @@ static const struct behavior_driver_api behavior_hold_tap_driver_api = { }; static int position_state_changed_listener(const zmk_event_t *eh) { - struct zmk_position_state_changed *ev = cast_zmk_position_state_changed(eh); + struct zmk_position_state_changed *ev = as_zmk_position_state_changed(eh); if (undecided_hold_tap == NULL) { LOG_DBG("%d bubble (no undecided hold_tap active)", ev->position); @@ -435,7 +435,7 @@ static inline bool only_mods(struct zmk_keycode_state_changed *ev) { static int keycode_state_changed_listener(const zmk_event_t *eh) { // we want to catch layer-up events too... how? - struct zmk_keycode_state_changed *ev = cast_zmk_keycode_state_changed(eh); + struct zmk_keycode_state_changed *ev = as_zmk_keycode_state_changed(eh); if (undecided_hold_tap == NULL) { // LOG_DBG("0x%02X bubble (no undecided hold_tap active)", ev->keycode); @@ -456,9 +456,9 @@ static int keycode_state_changed_listener(const zmk_event_t *eh) { } int behavior_hold_tap_listener(const zmk_event_t *eh) { - if (is_zmk_position_state_changed(eh)) { + if (as_zmk_position_state_changed(eh) != NULL) { return position_state_changed_listener(eh); - } else if (is_zmk_keycode_state_changed(eh)) { + } else if (as_zmk_keycode_state_changed(eh) != NULL) { return keycode_state_changed_listener(eh); } return ZMK_EV_EVENT_BUBBLE; diff --git a/app/src/behaviors/behavior_sticky_key.c b/app/src/behaviors/behavior_sticky_key.c index ee33d381..fe05e067 100644 --- a/app/src/behaviors/behavior_sticky_key.c +++ b/app/src/behaviors/behavior_sticky_key.c @@ -176,10 +176,10 @@ static const struct behavior_driver_api behavior_sticky_key_driver_api = { }; static int sticky_key_keycode_state_changed_listener(const zmk_event_t *eh) { - if (!is_zmk_keycode_state_changed(eh)) { + struct zmk_keycode_state_changed *ev = as_zmk_keycode_state_changed(eh); + if (ev == NULL) { return ZMK_EV_EVENT_BUBBLE; } - struct zmk_keycode_state_changed *ev = cast_zmk_keycode_state_changed(eh); for (int i = 0; i < ZMK_BHV_STICKY_KEY_MAX_HELD; i++) { struct active_sticky_key *sticky_key = &active_sticky_keys[i]; if (sticky_key->position == ZMK_BHV_STICKY_KEY_POSITION_FREE) { diff --git a/app/src/combo.c b/app/src/combo.c index a08a2f57..82f6538f 100644 --- a/app/src/combo.c +++ b/app/src/combo.c @@ -291,7 +291,7 @@ static void activate_combo(struct combo_cfg *combo) { } move_pressed_keys_to_active_combo(active_combo); press_combo_behavior( - combo, cast_zmk_position_state_changed(active_combo->key_positions_pressed[0])->timestamp); + combo, as_zmk_position_state_changed(active_combo->key_positions_pressed[0])->timestamp); } static void deactivate_combo(int active_combo_index) { @@ -315,7 +315,7 @@ static bool release_combo_key(int32_t position, int64_t timestamp) { 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 (cast_zmk_position_state_changed(active_combo->key_positions_pressed[i]) + } else if (as_zmk_position_state_changed(active_combo->key_positions_pressed[i]) ->position != position) { all_keys_released = false; } else { // not null and position matches @@ -418,11 +418,11 @@ static void combo_timeout_handler(struct k_work *item) { } static int position_state_changed_listener(const zmk_event_t *ev) { - if (!is_zmk_position_state_changed(ev)) { + struct zmk_position_state_changed *data = as_zmk_position_state_changed(ev); + if (data == NULL) { return 0; } - struct zmk_position_state_changed *data = cast_zmk_position_state_changed(ev); if (data->state) { // keydown return position_state_down(ev, data); } else { // keyup diff --git a/app/src/display/main.c b/app/src/display/main.c index 90789f94..55dd1ce2 100644 --- a/app/src/display/main.c +++ b/app/src/display/main.c @@ -76,7 +76,11 @@ int zmk_display_init() { } int display_event_handler(const zmk_event_t *eh) { - struct zmk_activity_state_changed *ev = cast_zmk_activity_state_changed(eh); + struct zmk_activity_state_changed *ev = as_zmk_activity_state_changed(eh); + if (ev == NULL) { + return -ENOTSUP; + } + switch (ev->state) { case ZMK_ACTIVITY_ACTIVE: start_display_updates(); diff --git a/app/src/hid_listener.c b/app/src/hid_listener.c index c5143db7..cd7acc0e 100644 --- a/app/src/hid_listener.c +++ b/app/src/hid_listener.c @@ -71,8 +71,8 @@ static int hid_listener_keycode_released(uint16_t usage_page, uint32_t keycode, } int hid_listener(const zmk_event_t *eh) { - if (is_zmk_keycode_state_changed(eh)) { - const struct zmk_keycode_state_changed *ev = cast_zmk_keycode_state_changed(eh); + const struct zmk_keycode_state_changed *ev = as_zmk_keycode_state_changed(eh); + if (ev) { if (ev->state) { hid_listener_keycode_pressed(ev->usage_page, ev->keycode, ev->implicit_modifiers); } else { diff --git a/app/src/keymap.c b/app/src/keymap.c index 169cf9c4..31258e0d 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -247,16 +247,20 @@ int zmk_keymap_sensor_triggered(uint8_t sensor_number, const struct device *sens #endif /* ZMK_KEYMAP_HAS_SENSORS */ int keymap_listener(const zmk_event_t *eh) { - if (is_zmk_position_state_changed(eh)) { - const struct zmk_position_state_changed *ev = cast_zmk_position_state_changed(eh); - return zmk_keymap_position_state_changed(ev->position, ev->state, ev->timestamp); -#if ZMK_KEYMAP_HAS_SENSORS - } else if (is_zmk_sensor_event(eh)) { - const struct zmk_sensor_event *ev = cast_zmk_sensor_event(eh); - return zmk_keymap_sensor_triggered(ev->sensor_number, ev->sensor, ev->timestamp); -#endif /* ZMK_KEYMAP_HAS_SENSORS */ + const struct zmk_position_state_changed *pos_ev; + if ((pos_ev = as_zmk_position_state_changed(eh)) != NULL) { + return zmk_keymap_position_state_changed(pos_ev->position, pos_ev->state, + pos_ev->timestamp); } +#if ZMK_KEYMAP_HAS_SENSORS + const struct zmk_sensor_event *sensor_ev; + if ((sensor_ev = as_zmk_sensor_event(eh)) != NULL) { + return zmk_keymap_sensor_triggered(sensor_ev->sensor_number, sensor_ev->sensor, + sensor_ev->timestamp); + } +#endif /* ZMK_KEYMAP_HAS_SENSORS */ + return -ENOTSUP; } diff --git a/app/src/split_listener.c b/app/src/split_listener.c index 54e893ed..8ac56c9f 100644 --- a/app/src/split_listener.c +++ b/app/src/split_listener.c @@ -21,8 +21,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); int split_listener(const zmk_event_t *eh) { LOG_DBG(""); - if (is_zmk_position_state_changed(eh)) { - const struct zmk_position_state_changed *ev = cast_zmk_position_state_changed(eh); + const struct zmk_position_state_changed *ev = as_zmk_position_state_changed(eh); + if (ev != NULL) { if (ev->state) { return zmk_split_bt_position_pressed(ev->position); } else {