diff --git a/drivers/input/CMakeLists.txt b/drivers/input/CMakeLists.txt index a5483ffc76..32280c42e1 100644 --- a/drivers/input/CMakeLists.txt +++ b/drivers/input/CMakeLists.txt @@ -7,5 +7,13 @@ zephyr_library_sources_ifdef(CONFIG_INPUT_FT5336 input_ft5336.c) zephyr_library_sources_ifdef(CONFIG_INPUT_GPIO_KEYS input_gpio_keys.c) zephyr_library_sources_ifdef(CONFIG_INPUT_GPIO_QDEC input_gpio_qdec.c) zephyr_library_sources_ifdef(CONFIG_INPUT_NPCX_KBD input_npcx_kbd.c) -zephyr_library_sources_ifdef(CONFIG_INPUT_SDL_TOUCH input_sdl_touch.c) zephyr_library_sources_ifdef(CONFIG_INPUT_XPT2046 input_xpt2046.c) + +if (CONFIG_INPUT_SDL_TOUCH) + zephyr_library_sources(input_sdl_touch.c) + if (CONFIG_NATIVE_APPLICATION) + zephyr_library_sources(input_sdl_touch_bottom.c) + else() + target_sources(native_simulator INTERFACE input_sdl_touch_bottom.c) + endif() +endif() diff --git a/drivers/input/input_sdl_touch.c b/drivers/input/input_sdl_touch.c index c1a95b7559..14ed5469e1 100644 --- a/drivers/input/input_sdl_touch.c +++ b/drivers/input/input_sdl_touch.c @@ -1,43 +1,24 @@ /* * Copyright (c) 2020 Jabil Inc. + * Copyright (c) 2023 Nordic Semiconductor * * SPDX-License-Identifier: Apache-2.0 */ #define DT_DRV_COMPAT zephyr_input_sdl_touch +#include #include #include - -#include +#include "input_sdl_touch_bottom.h" LOG_MODULE_REGISTER(sdl_input, CONFIG_INPUT_LOG_LEVEL); -struct sdl_data { - const struct device *dev; - int x; - int y; - bool pressed; -}; - -static int sdl_filter(void *arg, SDL_Event *event) +static void sdl_input_callback(struct sdl_input_data *data) { - struct sdl_data *data = arg; - - switch (event->type) { - case SDL_MOUSEBUTTONUP: - data->pressed = false; + if (data->just_released == true) { input_report_key(data->dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER); - break; - case SDL_MOUSEBUTTONDOWN: - data->pressed = true; - break; - case SDL_MOUSEMOTION: - data->x = event->button.x; - data->y = event->button.y; - break; - default: - return 1; + data->just_released = false; } if (data->pressed) { @@ -45,23 +26,22 @@ static int sdl_filter(void *arg, SDL_Event *event) input_report_abs(data->dev, INPUT_ABS_Y, data->y, false, K_FOREVER); input_report_key(data->dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER); } - - return 1; } static int sdl_init(const struct device *dev) { - struct sdl_data *data = dev->data; - - data->dev = dev; + struct sdl_input_data *data = dev->data; LOG_INF("Init '%s' device", dev->name); - SDL_AddEventWatch(sdl_filter, data); + + data->dev = dev; + data->callback = sdl_input_callback; + sdl_input_init_bottom(data); return 0; } -static struct sdl_data sdl_data_0; +static struct sdl_input_data sdl_data_0; DEVICE_DT_INST_DEFINE(0, sdl_init, NULL, &sdl_data_0, NULL, POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, NULL); diff --git a/drivers/input/input_sdl_touch_bottom.c b/drivers/input/input_sdl_touch_bottom.c new file mode 100644 index 0000000000..eb03f89ca1 --- /dev/null +++ b/drivers/input/input_sdl_touch_bottom.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 Jabil Inc. + * Copyright (c) 2023 Nordic Semiconductor + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "input_sdl_touch_bottom.h" + +static int sdl_filter(void *arg, SDL_Event *event) +{ + struct sdl_input_data *data = arg; + + switch (event->type) { + case SDL_MOUSEBUTTONUP: + data->pressed = false; + data->just_released = true; + break; + case SDL_MOUSEBUTTONDOWN: + data->pressed = true; + break; + case SDL_MOUSEMOTION: + data->x = event->button.x; + data->y = event->button.y; + break; + default: + return 1; + } + + data->callback(data); + + return 1; +} + +void sdl_input_init_bottom(struct sdl_input_data *data) +{ + SDL_AddEventWatch(sdl_filter, data); +} diff --git a/drivers/input/input_sdl_touch_bottom.h b/drivers/input/input_sdl_touch_bottom.h new file mode 100644 index 0000000000..397715686c --- /dev/null +++ b/drivers/input/input_sdl_touch_bottom.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor + * + * SPDX-License-Identifier: Apache-2.0 + * + * "Bottom" of the SDL input driver. + * When built with the native_simulator this will be built in the runner context, + * that is, with the host C library, and with the host include paths. + */ + +#ifndef DRIVERS_INPUT_INPUT_SDL_TOUCH_BOTTOM_H +#define DRIVERS_INPUT_INPUT_SDL_TOUCH_BOTTOM_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Note: None of these are public interfaces. But internal to the SDL input driver */ + +struct sdl_input_data { + const void *dev; /* device structure pointer */ + void (*callback)(struct sdl_input_data *data); + int x; + int y; + bool pressed; + bool just_released; +}; + +void sdl_input_init_bottom(struct sdl_input_data *data); + +#ifdef __cplusplus +} +#endif + +#endif /* DRIVERS_INPUT_INPUT_SDL_TOUCH_BOTTOM_H */