Skip to content

Commit 4d7cad3

Browse files
committed
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.
1 parent d0016b3 commit 4d7cad3

File tree

6 files changed

+43
-19
lines changed

6 files changed

+43
-19
lines changed

app/include/drivers/input_processor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include <zephyr/device.h>
1414
#include <zephyr/input/input.h>
1515

16+
#define ZMK_INPUT_PROC_CONTINUE 0
17+
#define ZMK_INPUT_PROC_STOP 1
18+
1619
struct zmk_input_processor_entry {
1720
const struct device *dev;
1821
uint32_t param1;

app/src/pointing/input_listener.c

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ static inline bool is_y_data(const struct input_event *evt) {
152152
return evt->type == INPUT_EV_REL && evt->code == INPUT_REL_Y;
153153
}
154154

155-
static void apply_config(const struct input_listener_config_entry *cfg,
156-
struct input_listener_processor_data *processor_data,
157-
struct input_listener_data *data, struct input_event *evt) {
155+
static int apply_config(const struct input_listener_config_entry *cfg,
156+
struct input_listener_processor_data *processor_data,
157+
struct input_listener_data *data, struct input_event *evt) {
158158
size_t remainder_index = 0;
159159
for (size_t p = 0; p < cfg->processors_len; p++) {
160160
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,
185185

186186
struct zmk_input_processor_state state = {.remainder = remainder};
187187

188-
zmk_input_processor_handle_event(proc_e->dev, evt, proc_e->param1, proc_e->param2, &state);
188+
int ret = zmk_input_processor_handle_event(proc_e->dev, evt, proc_e->param1, proc_e->param2,
189+
&state);
190+
switch (ret) {
191+
case ZMK_INPUT_PROC_CONTINUE:
192+
continue;
193+
default:
194+
return ret;
195+
}
189196
}
197+
198+
return ZMK_INPUT_PROC_CONTINUE;
190199
}
191-
static void filter_with_input_config(const struct input_listener_config *cfg,
192-
struct input_listener_data *data, struct input_event *evt) {
200+
201+
static int filter_with_input_config(const struct input_listener_config *cfg,
202+
struct input_listener_data *data, struct input_event *evt) {
193203
if (!evt->dev) {
194-
return;
204+
return -ENODEV;
195205
}
196206

197207
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,
201211
uint8_t layer = 0;
202212
while (mask != 0) {
203213
if (mask & BIT(0) && zmk_keymap_layer_active(layer)) {
204-
apply_config(&override->config, override_data, data, evt);
214+
int ret = apply_config(&override->config, override_data, data, evt);
215+
216+
if (ret < 0) {
217+
return ret;
218+
}
205219
if (!override->process_next) {
206-
return;
220+
return 0;
207221
}
208222
}
209223

@@ -212,7 +226,7 @@ static void filter_with_input_config(const struct input_listener_config *cfg,
212226
}
213227
}
214228

215-
apply_config(&cfg->base, &data->base_processor_data, data, evt);
229+
return apply_config(&cfg->base, &data->base_processor_data, data, evt);
216230
}
217231

218232
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
247261

248262
static void input_handler(const struct input_listener_config *config,
249263
struct input_listener_data *data, struct input_event *evt) {
250-
// First, filter to update the event data as needed.
251-
filter_with_input_config(config, data, evt);
264+
// First, process to update the event data as needed.
265+
int ret = filter_with_input_config(config, data, evt);
266+
267+
if (ret < 0) {
268+
LOG_ERR("Error applying input processors: %d", ret);
269+
return;
270+
} else if (ret == ZMK_INPUT_PROC_STOP) {
271+
return;
272+
}
252273

253274
#if IS_ENABLED(CONFIG_ZMK_POINTING_SMOOTH_SCROLLING)
254275
apply_resolution_scaling(data, evt);

app/src/pointing/input_processor_code_mapper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static int cm_handle_event(const struct device *dev, struct input_event *event,
2525
const struct cm_config *cfg = dev->config;
2626

2727
if (event->type != cfg->type) {
28-
return 0;
28+
return ZMK_INPUT_PROC_CONTINUE;
2929
}
3030

3131
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,
3737
}
3838
}
3939

40-
return 0;
40+
return ZMK_INPUT_PROC_CONTINUE;
4141
}
4242

4343
static struct zmk_input_processor_driver_api cm_driver_api = {

app/src/pointing/input_processor_scaler.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static int scaler_handle_event(const struct device *dev, struct input_event *eve
4747
const struct scaler_config *cfg = dev->config;
4848

4949
if (event->type != cfg->type) {
50-
return 0;
50+
return ZMK_INPUT_PROC_CONTINUE;
5151
}
5252

5353
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
5656
}
5757
}
5858

59-
return 0;
59+
return ZMK_INPUT_PROC_CONTINUE;
6060
}
6161

6262
static struct zmk_input_processor_driver_api scaler_driver_api = {

app/src/pointing/input_processor_temp_layer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static int temp_layer_handle_event(const struct device *dev, struct input_event
162162
k_work_reschedule(&layer_disable_works[param1], K_MSEC(param2));
163163
}
164164

165-
return 0;
165+
return ZMK_INPUT_PROC_CONTINUE;
166166
}
167167

168168
static int temp_layer_init(const struct device *dev) {

app/src/pointing/input_processor_transform.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static int ipt_handle_event(const struct device *dev, struct input_event *event,
4242
const struct ipt_config *cfg = dev->config;
4343

4444
if (event->type != cfg->type) {
45-
return 0;
45+
return ZMK_INPUT_PROC_CONTINUE;
4646
}
4747

4848
if (param1 & INPUT_TRANSFORM_XY_SWAP) {
@@ -65,7 +65,7 @@ static int ipt_handle_event(const struct device *dev, struct input_event *event,
6565
event->value = -event->value;
6666
}
6767

68-
return 0;
68+
return ZMK_INPUT_PROC_CONTINUE;
6969
}
7070

7171
static struct zmk_input_processor_driver_api ipt_driver_api = {

0 commit comments

Comments
 (0)