Browse Source

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.
xmkb
Pete Johanson 4 years ago
parent
commit
3368a81057
  1. 9
      app/include/zmk/event_manager.h
  2. 24
      app/src/behaviors/behavior_hold_tap.c
  3. 4
      app/src/behaviors/behavior_sticky_key.c
  4. 8
      app/src/combo.c
  5. 6
      app/src/display/main.c
  6. 4
      app/src/hid_listener.c
  7. 18
      app/src/keymap.c
  8. 4
      app/src/split_listener.c

9
app/include/zmk/event_manager.h

@ -39,8 +39,7 @@ struct zmk_event_subscription { @@ -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 { @@ -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};

24
app/src/behaviors/behavior_hold_tap.c

@ -88,10 +88,11 @@ static struct zmk_position_state_changed *find_captured_keydown_event(uint32_t p @@ -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() { @@ -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 = { @@ -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) { @@ -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) { @@ -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;

4
app/src/behaviors/behavior_sticky_key.c

@ -176,10 +176,10 @@ static const struct behavior_driver_api behavior_sticky_key_driver_api = { @@ -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) {

8
app/src/combo.c

@ -291,7 +291,7 @@ static void activate_combo(struct combo_cfg *combo) { @@ -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) { @@ -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) { @@ -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

6
app/src/display/main.c

@ -76,7 +76,11 @@ int zmk_display_init() { @@ -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();

4
app/src/hid_listener.c

@ -71,8 +71,8 @@ static int hid_listener_keycode_released(uint16_t usage_page, uint32_t keycode, @@ -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 {

18
app/src/keymap.c

@ -247,15 +247,19 @@ int zmk_keymap_sensor_triggered(uint8_t sensor_number, const struct device *sens @@ -247,15 +247,19 @@ 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);
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
} 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_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;
}

4
app/src/split_listener.c

@ -21,8 +21,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -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 {

Loading…
Cancel
Save