Skip to content

Commit

Permalink
input: add a user_data pointer to the callback
Browse files Browse the repository at this point in the history
Add a void *user_data pointer to the input callback structure. This is
useful for driver to get back the driver data structure and avoid
defining wrapper functions.

Signed-off-by: Fabio Baltieri <[email protected]>
  • Loading branch information
fabiobaltieri authored and carlescufi committed Jul 31, 2024
1 parent 8bf604e commit 716fa26
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 60 deletions.
11 changes: 4 additions & 7 deletions drivers/kscan/kscan_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ struct kscan_input_data {
bool pressed;
};

static void kscan_input_cb(const struct device *dev, struct input_event *evt)
static void kscan_input_cb(struct input_event *evt, void *user_data)
{
const struct device *dev = user_data;
struct kscan_input_data *data = dev->data;

switch (evt->code) {
Expand Down Expand Up @@ -100,13 +101,9 @@ static const struct kscan_driver_api kscan_input_driver_api = {
};

#define KSCAN_INPUT_INIT(index) \
static void kscan_input_cb_##index(struct input_event *evt) \
{ \
kscan_input_cb(DEVICE_DT_GET(DT_INST(index, DT_DRV_COMPAT)), \
evt); \
} \
INPUT_CALLBACK_DEFINE(DEVICE_DT_GET(DT_INST_PARENT(index)), \
kscan_input_cb_##index); \
kscan_input_cb, \
(void *)DEVICE_DT_INST_GET(index)); \
static const struct kscan_input_config kscan_input_config_##index = { \
.input_dev = DEVICE_DT_GET(DT_INST_PARENT(index)), \
}; \
Expand Down
8 changes: 6 additions & 2 deletions include/zephyr/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ struct input_callback {
/** @ref device pointer or NULL. */
const struct device *dev;
/** The callback function. */
void (*callback)(struct input_event *evt);
void (*callback)(struct input_event *evt, void *user_data);
/** User data pointer. */
void *user_data;
};

/**
Expand All @@ -136,12 +138,14 @@ struct input_callback {
*
* @param _dev @ref device pointer or NULL.
* @param _callback The callback function.
* @param _user_data Pointer to user specified data.
*/
#define INPUT_CALLBACK_DEFINE(_dev, _callback) \
#define INPUT_CALLBACK_DEFINE(_dev, _callback, _user_data) \
static const STRUCT_SECTION_ITERABLE(input_callback, \
_input_callback__##_callback) = { \
.dev = _dev, \
.callback = _callback, \
.user_data = _user_data, \
}

#ifdef __cplusplus
Expand Down
7 changes: 2 additions & 5 deletions modules/lvgl/include/lvgl_common_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ int lvgl_init_input_devices(void);
#define LVGL_KEY_VALID(key) IN_RANGE(key, 0, UINT8_MAX)

#define LVGL_INPUT_DEFINE(inst, type, msgq_size, process_evt_cb) \
static void lvgl_input_cb_##_##inst(struct input_event *evt) \
{ \
process_evt_cb(DEVICE_DT_INST_GET(inst), evt); \
} \
INPUT_CALLBACK_DEFINE(LVGL_INPUT_DEVICE(inst), lvgl_input_cb_##_##inst); \
INPUT_CALLBACK_DEFINE(LVGL_INPUT_DEVICE(inst), process_evt_cb, \
(void *)DEVICE_DT_INST_GET(inst)); \
K_MSGQ_DEFINE(lvgl_input_msgq_##type##_##inst, sizeof(lv_indev_data_t), msgq_size, 4)

#ifdef __cplusplus
Expand Down
3 changes: 2 additions & 1 deletion modules/lvgl/input/lvgl_button_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ struct lvgl_button_input_config {
const lv_coord_t *coordinates;
};

static void lvgl_button_process_event(const struct device *dev, struct input_event *evt)
static void lvgl_button_process_event(struct input_event *evt, void *user_data)
{
const struct device *dev = user_data;
struct lvgl_common_input_data *data = dev->data;
const struct lvgl_button_input_config *cfg = dev->config;
uint8_t i;
Expand Down
3 changes: 2 additions & 1 deletion modules/lvgl/input/lvgl_encoder_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ struct lvgl_encoder_input_config {
int button_input_code;
};

static void lvgl_encoder_process_event(const struct device *dev, struct input_event *evt)
static void lvgl_encoder_process_event(struct input_event *evt, void *user_data)
{
const struct device *dev = user_data;
struct lvgl_common_input_data *data = dev->data;
const struct lvgl_encoder_input_config *cfg = dev->config;

Expand Down
3 changes: 2 additions & 1 deletion modules/lvgl/input/lvgl_keypad_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ struct lvgl_keypad_input_config {
uint8_t num_codes;
};

static void lvgl_keypad_process_event(const struct device *dev, struct input_event *evt)
static void lvgl_keypad_process_event(struct input_event *evt, void *user_data)
{
const struct device *dev = user_data;
struct lvgl_common_input_data *data = dev->data;
const struct lvgl_keypad_input_config *cfg = dev->config;
uint8_t i;
Expand Down
3 changes: 2 additions & 1 deletion modules/lvgl/input/lvgl_pointer_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ struct lvgl_pointer_input_data {
uint32_t point_y;
};

static void lvgl_pointer_process_event(const struct device *dev, struct input_event *evt)
static void lvgl_pointer_process_event(struct input_event *evt, void *user_data)
{
const struct device *dev = user_data;
const struct lvgl_pointer_input_config *cfg = dev->config;
struct lvgl_pointer_input_data *data = dev->data;
lv_disp_t *disp = lv_disp_get_default();
Expand Down
6 changes: 4 additions & 2 deletions samples/subsys/usb/hid-keyboard/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@ UDC_STATIC_BUF_DEFINE(report, KB_REPORT_COUNT);
static uint32_t kb_duration;
static bool kb_ready;

static void input_cb(struct input_event *evt)
static void input_cb(struct input_event *evt, void *user_data)
{
struct kb_event kb_evt;

ARG_UNUSED(user_data);

kb_evt.code = evt->code;
kb_evt.value = evt->value;
if (k_msgq_put(&kb_msgq, &kb_evt, K_NO_WAIT) != 0) {
LOG_ERR("Failed to put new input event");
}
}

INPUT_CALLBACK_DEFINE(NULL, input_cb);
INPUT_CALLBACK_DEFINE(NULL, input_cb, NULL);

static void kb_iface_ready(const struct device *dev, const bool ready)
{
Expand Down
6 changes: 4 additions & 2 deletions samples/subsys/usb/hid-mouse/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ static ALWAYS_INLINE void rwup_if_suspended(void)
}
}

static void input_cb(struct input_event *evt)
static void input_cb(struct input_event *evt, void *user_data)
{
static uint8_t tmp[MOUSE_REPORT_COUNT];

ARG_UNUSED(user_data);

switch (evt->code) {
case INPUT_KEY_0:
rwup_if_suspended();
Expand Down Expand Up @@ -95,7 +97,7 @@ static void input_cb(struct input_event *evt)

}

INPUT_CALLBACK_DEFINE(NULL, input_cb);
INPUT_CALLBACK_DEFINE(NULL, input_cb, NULL);

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
static int enable_usb_device_next(void)
Expand Down
2 changes: 1 addition & 1 deletion subsys/input/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static void input_process(struct input_event *evt)
{
STRUCT_SECTION_FOREACH(input_callback, callback) {
if (callback->dev == NULL || callback->dev == evt->dev) {
callback->callback(evt);
callback->callback(evt, callback->user_data);
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions subsys/input/input_keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ struct keymap_data {
bool pressed;
};

static void keymap_cb(const struct device *dev, struct input_event *evt)
static void keymap_cb(struct input_event *evt, void *user_data)
{
const struct device *dev = user_data;
const struct keymap_config *cfg = dev->config;
struct keymap_data *data = dev->data;
const uint16_t *codes = cfg->codes;
Expand Down Expand Up @@ -98,11 +99,8 @@ static int keymap_init(const struct device *dev)
KEYMAP_ENTRY_CODE(DT_PROP_BY_IDX(node_id, prop, idx)),

#define INPUT_KEYMAP_DEFINE(inst) \
static void keymap_cb_##inst(struct input_event *evt) \
{ \
keymap_cb(DEVICE_DT_INST_GET(inst), evt); \
} \
INPUT_CALLBACK_DEFINE(DEVICE_DT_GET(DT_INST_PARENT(inst)), keymap_cb_##inst); \
INPUT_CALLBACK_DEFINE(DEVICE_DT_GET(DT_INST_PARENT(inst)), keymap_cb, \
(void *)DEVICE_DT_INST_GET(inst)); \
\
DT_INST_FOREACH_PROP_ELEM(inst, keymap, KEYMAP_ENTRY_VALIDATE) \
\
Expand Down
11 changes: 4 additions & 7 deletions subsys/input/input_longpress.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ static void longpress_deferred(struct k_work *work)
entry->long_fired = true;
}

static void longpress_cb(const struct device *dev, struct input_event *evt)
static void longpress_cb(struct input_event *evt, void *user_data)
{
const struct device *dev = user_data;
const struct longpress_config *cfg = dev->config;
struct longpress_data_entry *entry;
int i;
Expand Down Expand Up @@ -105,15 +106,11 @@ static int longpress_init(const struct device *dev)
#define INPUT_LONGPRESS_DEFINE(inst) \
BUILD_ASSERT((DT_INST_PROP_LEN(inst, input_codes) == \
DT_INST_PROP_LEN_OR(inst, short_codes, 0)) || \
!DT_INST_NODE_HAS_PROP(inst, short_codes)); \
!DT_INST_NODE_HAS_PROP(inst, short_codes)); \
BUILD_ASSERT(DT_INST_PROP_LEN(inst, input_codes) == DT_INST_PROP_LEN(inst, long_codes)); \
\
static void longpress_cb_##inst(struct input_event *evt) \
{ \
longpress_cb(DEVICE_DT_INST_GET(inst), evt); \
} \
INPUT_CALLBACK_DEFINE(DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, input)), \
longpress_cb_##inst); \
longpress_cb, (void *)DEVICE_DT_INST_GET(inst)); \
\
static const uint16_t longpress_input_codes_##inst[] = DT_INST_PROP(inst, input_codes); \
\
Expand Down
12 changes: 8 additions & 4 deletions subsys/input/input_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ static bool input_dump_enabled(void)
}
#endif /* CONFIG_INPUT_SHELL */

static void input_dump_cb(struct input_event *evt)
static void input_dump_cb(struct input_event *evt, void *user_data)
{
ARG_UNUSED(user_data);

if (!input_dump_enabled()) {
return;
}
Expand All @@ -64,7 +66,7 @@ static void input_dump_cb(struct input_event *evt)
evt->code,
evt->value);
}
INPUT_CALLBACK_DEFINE(NULL, input_dump_cb);
INPUT_CALLBACK_DEFINE(NULL, input_dump_cb, NULL);
#endif /* CONFIG_INPUT_EVENT_DUMP */

#ifdef CONFIG_INPUT_SHELL
Expand Down Expand Up @@ -162,12 +164,14 @@ static void kbd_matrix_state_log_entry(char *header, kbd_row_t *data)
kbd_matrix_state_dev->name, header, kbd_matrix_buf, count);
}

static void kbd_matrix_state_log(struct input_event *evt)
static void kbd_matrix_state_log(struct input_event *evt, void *user_data)
{
const struct input_kbd_matrix_common_config *cfg;
static uint32_t row, col;
static bool val;

ARG_UNUSED(user_data);

if (kbd_matrix_state_dev == NULL || kbd_matrix_state_dev != evt->dev) {
return;
}
Expand Down Expand Up @@ -212,7 +216,7 @@ static void kbd_matrix_state_log(struct input_event *evt)

kbd_matrix_state_log_entry("state", kbd_matrix_state);
}
INPUT_CALLBACK_DEFINE(NULL, kbd_matrix_state_log);
INPUT_CALLBACK_DEFINE(NULL, kbd_matrix_state_log, NULL);

static int input_cmd_kbd_matrix_state_dump(const struct shell *sh,
size_t argc, char *argv[])
Expand Down
4 changes: 2 additions & 2 deletions tests/drivers/input/gpio_kbd_matrix/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ static int last_checked_event_count;
zassert_equal(_val, test_event_data.val); \
}

static void test_cb(struct input_event *evt)
static void test_cb(struct input_event *evt, void *user_data)
{
static int row, col, val;

Expand All @@ -220,7 +220,7 @@ static void test_cb(struct input_event *evt)
test_event_data.event_count, row, col, val);
}
}
INPUT_CALLBACK_DEFINE(NULL, test_cb);
INPUT_CALLBACK_DEFINE(NULL, test_cb, NULL);

/* actual tests */

Expand Down
4 changes: 2 additions & 2 deletions tests/drivers/input/gpio_keys/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ ZTEST_SUITE(gpio_keys, NULL, NULL, NULL, NULL, NULL);
static int event_count;
static uint16_t last_code;
static bool last_val;
static void test_gpio_keys_cb_handler(struct input_event *evt)
static void test_gpio_keys_cb_handler(struct input_event *evt, void *user_data)
{
TC_PRINT("GPIO_KEY %s pressed, zephyr_code=%u, value=%d\n",
evt->dev->name, evt->code, evt->value);
event_count++;
last_code = evt->code;
last_val = evt->value;
}
INPUT_CALLBACK_DEFINE(test_gpio_keys_dev, test_gpio_keys_cb_handler);
INPUT_CALLBACK_DEFINE(test_gpio_keys_dev, test_gpio_keys_cb_handler, NULL);

/**
* @brief TestPurpose: Verify gpio_keys_config pressed raw.
Expand Down
4 changes: 2 additions & 2 deletions tests/drivers/input/kbd_matrix/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static int last_checked_event_count;
zassert_equal(_val, test_event_data.val); \
}

static void test_cb(struct input_event *evt)
static void test_cb(struct input_event *evt, void *user_data)
{
static int row, col, val;

Expand All @@ -128,7 +128,7 @@ static void test_cb(struct input_event *evt)
test_event_data.event_count, row, col, val);
}
}
INPUT_CALLBACK_DEFINE(test_dev, test_cb);
INPUT_CALLBACK_DEFINE(test_dev, test_cb, NULL);

#define WAIT_FOR_IDLE_TIMEOUT_US (5 * USEC_PER_SEC)

Expand Down
20 changes: 10 additions & 10 deletions tests/subsys/input/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static int message_count_unfiltered;
static K_SEM_DEFINE(cb_start, 1, 1);
static K_SEM_DEFINE(cb_done, 1, 1);

static void input_cb_filtered(struct input_event *evt)
static void input_cb_filtered(struct input_event *evt, void *user_data)
{
TC_PRINT("%s: %d\n", __func__, message_count_filtered);

Expand All @@ -29,9 +29,9 @@ static void input_cb_filtered(struct input_event *evt)

k_sem_give(&cb_start);
}
INPUT_CALLBACK_DEFINE(&fake_dev, input_cb_filtered);
INPUT_CALLBACK_DEFINE(&fake_dev, input_cb_filtered, NULL);

static void input_cb_unfiltered(struct input_event *evt)
static void input_cb_unfiltered(struct input_event *evt, void *user_data)
{
TC_PRINT("%s: %d\n", __func__, message_count_unfiltered);

Expand All @@ -42,7 +42,7 @@ static void input_cb_unfiltered(struct input_event *evt)
k_sem_give(&cb_done);
}
}
INPUT_CALLBACK_DEFINE(NULL, input_cb_unfiltered);
INPUT_CALLBACK_DEFINE(NULL, input_cb_unfiltered, NULL);

ZTEST(input_api, test_sequence_thread)
{
Expand Down Expand Up @@ -85,19 +85,19 @@ ZTEST(input_api, test_sequence_thread)

#else /* CONFIG_INPUT_MODE_THREAD */

static void input_cb_filtered(struct input_event *evt)
static void input_cb_filtered(struct input_event *evt, void *user_data)
{
if (evt->dev == &fake_dev) {
message_count_filtered++;
}
}
INPUT_CALLBACK_DEFINE(&fake_dev, input_cb_filtered);
INPUT_CALLBACK_DEFINE(&fake_dev, input_cb_filtered, NULL);

static void input_cb_unfiltered(struct input_event *evt)
static void input_cb_unfiltered(struct input_event *evt, void *user_data)
{
message_count_unfiltered++;
}
INPUT_CALLBACK_DEFINE(NULL, input_cb_unfiltered);
INPUT_CALLBACK_DEFINE(NULL, input_cb_unfiltered, NULL);

ZTEST(input_api, test_synchronous)
{
Expand All @@ -118,11 +118,11 @@ ZTEST(input_api, test_synchronous)

static struct input_event last_event;

static void input_cb_last_event(struct input_event *evt)
static void input_cb_last_event(struct input_event *evt, void *user_data)
{
memcpy(&last_event, evt, sizeof(last_event));
}
INPUT_CALLBACK_DEFINE(NULL, input_cb_last_event);
INPUT_CALLBACK_DEFINE(NULL, input_cb_last_event, NULL);

ZTEST(input_api, test_report_apis)
{
Expand Down
Loading

0 comments on commit 716fa26

Please sign in to comment.