From b76ac9a851c4af3452ccacd4b971148457924633 Mon Sep 17 00:00:00 2001 From: Fabio Baltieri Date: Tue, 4 Apr 2023 12:17:38 +0100 Subject: [PATCH] input: convert the kscan_sdl driver from kscan to input Convert the SDL driver to use the input subsystem. This is specifically meant to emulate touchscreen drivers, so it's setup to send triplet of x, y, touch for touch-on events and just touch off on touch off events. Renamed the driver to input-sdl-touch since now we can also develop an sdl driver for simulating key events. Signed-off-by: Fabio Baltieri --- boards/posix/native_posix/native_posix.dts | 11 +- drivers/input/CMakeLists.txt | 1 + drivers/input/Kconfig | 1 + drivers/{kscan => input}/Kconfig.sdl | 4 +- drivers/input/input_sdl_touch.c | 67 ++++++++++ drivers/kscan/CMakeLists.txt | 1 - drivers/kscan/Kconfig | 1 - drivers/kscan/kscan_sdl.c | 114 ------------------ .../input/zephyr,input-sdl-touch.yaml | 6 + dts/bindings/kscan/zephyr,sdl-kscan.yaml | 8 -- .../kscan_input/boards/native_posix.overlay | 2 + 11 files changed, 86 insertions(+), 130 deletions(-) rename drivers/{kscan => input}/Kconfig.sdl (74%) create mode 100644 drivers/input/input_sdl_touch.c delete mode 100644 drivers/kscan/kscan_sdl.c create mode 100644 dts/bindings/input/zephyr,input-sdl-touch.yaml delete mode 100644 dts/bindings/kscan/zephyr,sdl-kscan.yaml diff --git a/boards/posix/native_posix/native_posix.dts b/boards/posix/native_posix/native_posix.dts index 15e442356b..db0a46526c 100644 --- a/boards/posix/native_posix/native_posix.dts +++ b/boards/posix/native_posix/native_posix.dts @@ -22,7 +22,7 @@ zephyr,flash-controller = &flashcontroller0; zephyr,display = &sdl_dc; zephyr,canbus = &can_loopback0; - zephyr,keyboard-scan = &sdl_kscan; + zephyr,keyboard-scan = &kscan_input; }; aliases { @@ -30,7 +30,7 @@ i2c-0 = &i2c0; spi-0 = &spi0; led0 = &led0; - kscan0 = &sdl_kscan; + kscan0 = &kscan_input; rtc = &rtc; }; @@ -178,8 +178,11 @@ width = <320>; }; - sdl_kscan: sdl_kscan { - compatible = "zephyr,sdl-kscan"; + input-sdl-touch { + compatible = "zephyr,input-sdl-touch"; + kscan_input: kscan-input { + compatible = "zephyr,kscan-input"; + }; }; can_loopback0: can_loopback0 { diff --git a/drivers/input/CMakeLists.txt b/drivers/input/CMakeLists.txt index d929bf82a0..016588c5ee 100644 --- a/drivers/input/CMakeLists.txt +++ b/drivers/input/CMakeLists.txt @@ -5,3 +5,4 @@ zephyr_library_property(ALLOW_EMPTY TRUE) 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_SDL_TOUCH input_sdl_touch.c) diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig index 3d83fc31dd..559cacbee3 100644 --- a/drivers/input/Kconfig +++ b/drivers/input/Kconfig @@ -7,6 +7,7 @@ menu "Input drivers" source "drivers/input/Kconfig.ft5336" source "drivers/input/Kconfig.gpio_keys" +source "drivers/input/Kconfig.sdl" endmenu # Input Drivers diff --git a/drivers/kscan/Kconfig.sdl b/drivers/input/Kconfig.sdl similarity index 74% rename from drivers/kscan/Kconfig.sdl rename to drivers/input/Kconfig.sdl index 67f8138c59..923306dadd 100644 --- a/drivers/kscan/Kconfig.sdl +++ b/drivers/input/Kconfig.sdl @@ -1,10 +1,10 @@ # Copyright (c) 2020 Jabil Inc. # SPDX-License-Identifier: Apache-2.0 -config KSCAN_SDL +config INPUT_SDL_TOUCH bool "SDL event filter for touch panel emulation" default y - depends on DT_HAS_ZEPHYR_SDL_KSCAN_ENABLED + depends on DT_HAS_ZEPHYR_INPUT_SDL_TOUCH_ENABLED depends on HAS_SDL help Enable driver for the SDL mouse event filter. diff --git a/drivers/input/input_sdl_touch.c b/drivers/input/input_sdl_touch.c new file mode 100644 index 0000000000..c1a95b7559 --- /dev/null +++ b/drivers/input/input_sdl_touch.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2020 Jabil Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT zephyr_input_sdl_touch + +#include +#include + +#include + +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) +{ + struct sdl_data *data = arg; + + switch (event->type) { + case SDL_MOUSEBUTTONUP: + data->pressed = false; + 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; + } + + if (data->pressed) { + input_report_abs(data->dev, INPUT_ABS_X, data->x, false, K_FOREVER); + 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; + + LOG_INF("Init '%s' device", dev->name); + SDL_AddEventWatch(sdl_filter, data); + + return 0; +} + +static struct sdl_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/kscan/CMakeLists.txt b/drivers/kscan/CMakeLists.txt index 18dce90780..919d3da9ef 100644 --- a/drivers/kscan/CMakeLists.txt +++ b/drivers/kscan/CMakeLists.txt @@ -5,7 +5,6 @@ zephyr_library() zephyr_library_sources_ifdef(CONFIG_KSCAN_GT911 kscan_gt911.c) zephyr_library_sources_ifdef(CONFIG_KSCAN_ITE_IT8XXX2 kscan_ite_it8xxx2.c) zephyr_library_sources_ifdef(CONFIG_KSCAN_XEC kscan_mchp_xec.c) -zephyr_library_sources_ifdef(CONFIG_KSCAN_SDL kscan_sdl.c) zephyr_library_sources_ifdef(CONFIG_KSCAN_HT16K33 kscan_ht16k33.c) zephyr_library_sources_ifdef(CONFIG_KSCAN_CST816S kscan_cst816s.c) zephyr_library_sources_ifdef(CONFIG_KSCAN_CAP1203 kscan_cap1203.c) diff --git a/drivers/kscan/Kconfig b/drivers/kscan/Kconfig index bd9f114c59..dbe5ab7405 100644 --- a/drivers/kscan/Kconfig +++ b/drivers/kscan/Kconfig @@ -13,7 +13,6 @@ if KSCAN source "drivers/kscan/Kconfig.gt911" source "drivers/kscan/Kconfig.it8xxx2" source "drivers/kscan/Kconfig.xec" -source "drivers/kscan/Kconfig.sdl" source "drivers/kscan/Kconfig.ht16k33" source "drivers/kscan/Kconfig.cst816s" source "drivers/kscan/Kconfig.cap1203" diff --git a/drivers/kscan/kscan_sdl.c b/drivers/kscan/kscan_sdl.c deleted file mode 100644 index c00a0a5e10..0000000000 --- a/drivers/kscan/kscan_sdl.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2020 Jabil Inc. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#define DT_DRV_COMPAT zephyr_sdl_kscan - -#include -#include - -#include - -LOG_MODULE_REGISTER(kscan, CONFIG_KSCAN_LOG_LEVEL); - -struct sdl_data { - const struct device *dev; - kscan_callback_t callback; - bool enabled; -}; - -static int sdl_filter(void *arg, SDL_Event *event) -{ - struct sdl_data *data = arg; - uint32_t row = 0; - uint32_t column = 0; - bool pressed = 0; - - switch (event->type) { - case SDL_MOUSEBUTTONDOWN: { - pressed = 1; - column = event->button.x; - row = event->button.y; - } break; - case SDL_MOUSEBUTTONUP: { - pressed = 0; - column = event->button.x; - row = event->button.y; - } break; - case SDL_MOUSEMOTION: { - if (!event->motion.state) - break; - pressed = 1; - column = event->button.x; - row = event->button.y; - } break; - default: - return 1; - } - - if (data->enabled && data->callback) { - data->callback(data->dev, row, column, pressed); - } - return 1; -} - -static int sdl_configure(const struct device *dev, kscan_callback_t callback) -{ - struct sdl_data *data = dev->data; - - if (!callback) { - LOG_ERR("Callback is null"); - return -EINVAL; - } - LOG_DBG("%s: set callback", dev->name); - - data->callback = callback; - - return 0; -} - -static int sdl_enable_callback(const struct device *dev) -{ - struct sdl_data *data = dev->data; - - LOG_DBG("%s: enable cb", dev->name); - data->enabled = true; - return 0; -} - -static int sdl_disable_callback(const struct device *dev) -{ - struct sdl_data *data = dev->data; - - LOG_DBG("%s: disable cb", dev->name); - data->enabled = false; - return 0; -} - -static int sdl_init(const struct device *dev) -{ - struct sdl_data *data = dev->data; - - data->dev = dev; - - LOG_INF("Init '%s' device", dev->name); - SDL_AddEventWatch(sdl_filter, data); - - return 0; -} - - -static const struct kscan_driver_api sdl_driver_api = { - .config = sdl_configure, - .enable_callback = sdl_enable_callback, - .disable_callback = sdl_disable_callback, -}; - -static struct sdl_data sdl_data; - -DEVICE_DT_INST_DEFINE(0, sdl_init, - NULL, &sdl_data, NULL, - POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY, - &sdl_driver_api); diff --git a/dts/bindings/input/zephyr,input-sdl-touch.yaml b/dts/bindings/input/zephyr,input-sdl-touch.yaml new file mode 100644 index 0000000000..8248334e4f --- /dev/null +++ b/dts/bindings/input/zephyr,input-sdl-touch.yaml @@ -0,0 +1,6 @@ +# Copyright (c) 2022 Kumar Gala +# SPDX-License-Identifier: Apache-2.0 + +description: SDL based emulated touch panel + +compatible: "zephyr,input-sdl-touch" diff --git a/dts/bindings/kscan/zephyr,sdl-kscan.yaml b/dts/bindings/kscan/zephyr,sdl-kscan.yaml deleted file mode 100644 index 29c7d3db0d..0000000000 --- a/dts/bindings/kscan/zephyr,sdl-kscan.yaml +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright (c) 2022 Kumar Gala -# SPDX-License-Identifier: Apache-2.0 - -description: SDL based emulated keyboard matrix devices - -compatible: "zephyr,sdl-kscan" - -include: kscan.yaml diff --git a/tests/drivers/kscan/kscan_input/boards/native_posix.overlay b/tests/drivers/kscan/kscan_input/boards/native_posix.overlay index 8ed528d6a5..a88bdf5da9 100644 --- a/tests/drivers/kscan/kscan_input/boards/native_posix.overlay +++ b/tests/drivers/kscan/kscan_input/boards/native_posix.overlay @@ -5,6 +5,8 @@ */ / { + /delete-node/ input-sdl-touch; + fake_input_device: fake-device { compatible = "vnd,input-device"; kscan_input: kscan-input {