From 4d7cad3680c699bbadf614ecff105541002f75bd Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Sun, 15 Dec 2024 14:15:23 -0700 Subject: [PATCH] refactor(pointing): Allow stopping event propagation Allow input processors to return a special value if a given input event should not be further processed/propagated. --- app/include/drivers/input_processor.h | 3 ++ app/src/pointing/input_listener.c | 45 ++++++++++++++----- .../pointing/input_processor_code_mapper.c | 4 +- app/src/pointing/input_processor_scaler.c | 4 +- app/src/pointing/input_processor_temp_layer.c | 2 +- app/src/pointing/input_processor_transform.c | 4 +- 6 files changed, 43 insertions(+), 19 deletions(-) diff --git a/app/include/drivers/input_processor.h b/app/include/drivers/input_processor.h index aea57476ae4..38ada12af90 100644 --- a/app/include/drivers/input_processor.h +++ b/app/include/drivers/input_processor.h @@ -13,6 +13,9 @@ #include #include +#define ZMK_INPUT_PROC_CONTINUE 0 +#define ZMK_INPUT_PROC_STOP 1 + struct zmk_input_processor_entry { const struct device *dev; uint32_t param1; diff --git a/app/src/pointing/input_listener.c b/app/src/pointing/input_listener.c index 97e39285d33..ee8809f4240 100644 --- a/app/src/pointing/input_listener.c +++ b/app/src/pointing/input_listener.c @@ -152,9 +152,9 @@ static inline bool is_y_data(const struct input_event *evt) { return evt->type == INPUT_EV_REL && evt->code == INPUT_REL_Y; } -static void apply_config(const struct input_listener_config_entry *cfg, - struct input_listener_processor_data *processor_data, - struct input_listener_data *data, struct input_event *evt) { +static int apply_config(const struct input_listener_config_entry *cfg, + struct input_listener_processor_data *processor_data, + struct input_listener_data *data, struct input_event *evt) { size_t remainder_index = 0; for (size_t p = 0; p < cfg->processors_len; p++) { const struct zmk_input_processor_entry *proc_e = &cfg->processors[p]; @@ -185,13 +185,23 @@ static void apply_config(const struct input_listener_config_entry *cfg, struct zmk_input_processor_state state = {.remainder = remainder}; - zmk_input_processor_handle_event(proc_e->dev, evt, proc_e->param1, proc_e->param2, &state); + int ret = zmk_input_processor_handle_event(proc_e->dev, evt, proc_e->param1, proc_e->param2, + &state); + switch (ret) { + case ZMK_INPUT_PROC_CONTINUE: + continue; + default: + return ret; + } } + + return ZMK_INPUT_PROC_CONTINUE; } -static void filter_with_input_config(const struct input_listener_config *cfg, - struct input_listener_data *data, struct input_event *evt) { + +static int filter_with_input_config(const struct input_listener_config *cfg, + struct input_listener_data *data, struct input_event *evt) { if (!evt->dev) { - return; + return -ENODEV; } for (size_t oi = 0; oi < cfg->layer_overrides_len; oi++) { @@ -201,9 +211,13 @@ static void filter_with_input_config(const struct input_listener_config *cfg, uint8_t layer = 0; while (mask != 0) { if (mask & BIT(0) && zmk_keymap_layer_active(layer)) { - apply_config(&override->config, override_data, data, evt); + int ret = apply_config(&override->config, override_data, data, evt); + + if (ret < 0) { + return ret; + } if (!override->process_next) { - return; + return 0; } } @@ -212,7 +226,7 @@ static void filter_with_input_config(const struct input_listener_config *cfg, } } - apply_config(&cfg->base, &data->base_processor_data, data, evt); + return apply_config(&cfg->base, &data->base_processor_data, data, evt); } static void clear_xy_data(struct input_listener_xy_data *data) { @@ -247,8 +261,15 @@ static void apply_resolution_scaling(struct input_listener_data *data, struct in static void input_handler(const struct input_listener_config *config, struct input_listener_data *data, struct input_event *evt) { - // First, filter to update the event data as needed. - filter_with_input_config(config, data, evt); + // First, process to update the event data as needed. + int ret = filter_with_input_config(config, data, evt); + + if (ret < 0) { + LOG_ERR("Error applying input processors: %d", ret); + return; + } else if (ret == ZMK_INPUT_PROC_STOP) { + return; + } #if IS_ENABLED(CONFIG_ZMK_POINTING_SMOOTH_SCROLLING) apply_resolution_scaling(data, evt); diff --git a/app/src/pointing/input_processor_code_mapper.c b/app/src/pointing/input_processor_code_mapper.c index 20b5744ea96..b42d3478e83 100644 --- a/app/src/pointing/input_processor_code_mapper.c +++ b/app/src/pointing/input_processor_code_mapper.c @@ -25,7 +25,7 @@ static int cm_handle_event(const struct device *dev, struct input_event *event, const struct cm_config *cfg = dev->config; if (event->type != cfg->type) { - return 0; + return ZMK_INPUT_PROC_CONTINUE; } for (int i = 0; i < cfg->mapping_size / 2; i++) { @@ -37,7 +37,7 @@ static int cm_handle_event(const struct device *dev, struct input_event *event, } } - return 0; + return ZMK_INPUT_PROC_CONTINUE; } static struct zmk_input_processor_driver_api cm_driver_api = { diff --git a/app/src/pointing/input_processor_scaler.c b/app/src/pointing/input_processor_scaler.c index eb006cd2540..c7e08ecdb0e 100644 --- a/app/src/pointing/input_processor_scaler.c +++ b/app/src/pointing/input_processor_scaler.c @@ -47,7 +47,7 @@ static int scaler_handle_event(const struct device *dev, struct input_event *eve const struct scaler_config *cfg = dev->config; if (event->type != cfg->type) { - return 0; + return ZMK_INPUT_PROC_CONTINUE; } for (int i = 0; i < cfg->codes_len; i++) { @@ -56,7 +56,7 @@ static int scaler_handle_event(const struct device *dev, struct input_event *eve } } - return 0; + return ZMK_INPUT_PROC_CONTINUE; } static struct zmk_input_processor_driver_api scaler_driver_api = { diff --git a/app/src/pointing/input_processor_temp_layer.c b/app/src/pointing/input_processor_temp_layer.c index 85a394a693f..b1065271150 100644 --- a/app/src/pointing/input_processor_temp_layer.c +++ b/app/src/pointing/input_processor_temp_layer.c @@ -162,7 +162,7 @@ static int temp_layer_handle_event(const struct device *dev, struct input_event k_work_reschedule(&layer_disable_works[param1], K_MSEC(param2)); } - return 0; + return ZMK_INPUT_PROC_CONTINUE; } static int temp_layer_init(const struct device *dev) { diff --git a/app/src/pointing/input_processor_transform.c b/app/src/pointing/input_processor_transform.c index 10828de3f39..c214baee269 100644 --- a/app/src/pointing/input_processor_transform.c +++ b/app/src/pointing/input_processor_transform.c @@ -42,7 +42,7 @@ static int ipt_handle_event(const struct device *dev, struct input_event *event, const struct ipt_config *cfg = dev->config; if (event->type != cfg->type) { - return 0; + return ZMK_INPUT_PROC_CONTINUE; } if (param1 & INPUT_TRANSFORM_XY_SWAP) { @@ -65,7 +65,7 @@ static int ipt_handle_event(const struct device *dev, struct input_event *event, event->value = -event->value; } - return 0; + return ZMK_INPUT_PROC_CONTINUE; } static struct zmk_input_processor_driver_api ipt_driver_api = {