Skip to content

Commit

Permalink
refactor(pointing): Allow stopping event propagation
Browse files Browse the repository at this point in the history
Allow input processors to return a special value if a given input event
should not be further processed/propagated.
  • Loading branch information
petejohanson committed Dec 15, 2024
1 parent d0016b3 commit 4d7cad3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
3 changes: 3 additions & 0 deletions app/include/drivers/input_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <zephyr/device.h>
#include <zephyr/input/input.h>

#define ZMK_INPUT_PROC_CONTINUE 0
#define ZMK_INPUT_PROC_STOP 1

struct zmk_input_processor_entry {
const struct device *dev;
uint32_t param1;
Expand Down
45 changes: 33 additions & 12 deletions app/src/pointing/input_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions app/src/pointing/input_processor_code_mapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions app/src/pointing/input_processor_scaler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion app/src/pointing/input_processor_temp_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/pointing/input_processor_transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 = {
Expand Down

0 comments on commit 4d7cad3

Please sign in to comment.