From 687f407f2910dd78640e2a01840b4059f7c271a5 Mon Sep 17 00:00:00 2001 From: Pavlo Hamov Date: Mon, 3 Feb 2020 16:07:06 +0200 Subject: [PATCH] drivers: kscan: Introduce SDL mouse driver Introduces a new SDL mouse driver for the keyboard scan (kscan) interface. Driver is implemented as SDL event filter Signed-off-by: Pavlo Hamov --- boards/posix/native_posix/Kconfig.defconfig | 4 + drivers/kscan/CMakeLists.txt | 1 + drivers/kscan/Kconfig | 1 + drivers/kscan/Kconfig.sdl | 14 +++ drivers/kscan/kscan_sdl.c | 108 ++++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 drivers/kscan/Kconfig.sdl create mode 100644 drivers/kscan/kscan_sdl.c diff --git a/boards/posix/native_posix/Kconfig.defconfig b/boards/posix/native_posix/Kconfig.defconfig index 59f937abc0..38a507bebc 100644 --- a/boards/posix/native_posix/Kconfig.defconfig +++ b/boards/posix/native_posix/Kconfig.defconfig @@ -69,6 +69,10 @@ config SDL_DISPLAY default y depends on DISPLAY +config KSCAN_SDL + default y + depends on KSCAN + config FLASH_SIMULATOR default y depends on FLASH diff --git a/drivers/kscan/CMakeLists.txt b/drivers/kscan/CMakeLists.txt index 4e88cf6e71..8e1271076d 100644 --- a/drivers/kscan/CMakeLists.txt +++ b/drivers/kscan/CMakeLists.txt @@ -4,5 +4,6 @@ zephyr_library() zephyr_library_sources_ifdef(CONFIG_KSCAN_FT5336 kscan_ft5336.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_USERSPACE kscan_handlers.c) diff --git a/drivers/kscan/Kconfig b/drivers/kscan/Kconfig index 34b223c613..6af903aeb5 100644 --- a/drivers/kscan/Kconfig +++ b/drivers/kscan/Kconfig @@ -12,6 +12,7 @@ if KSCAN source "drivers/kscan/Kconfig.ft5336" source "drivers/kscan/Kconfig.xec" +source "drivers/kscan/Kconfig.sdl" module = KSCAN module-str = kscan diff --git a/drivers/kscan/Kconfig.sdl b/drivers/kscan/Kconfig.sdl new file mode 100644 index 0000000000..b594e7aa2c --- /dev/null +++ b/drivers/kscan/Kconfig.sdl @@ -0,0 +1,14 @@ +# Copyright (c) 2020 Jabil Inc. +# SPDX-License-Identifier: Apache-2.0 + +config KSCAN_SDL + bool "SDL event filter for touch panel emulation" + depends on HAS_SDL + help + Enable driver for the SDL mouse event filter. + +config SDL_POINTER_KSCAN_DEV_NAME + string "SDL kscan device name" + depends on KSCAN_SDL + default LVGL_POINTER_KSCAN_DEV_NAME if LVGL + default "SDL_KSCAN" diff --git a/drivers/kscan/kscan_sdl.c b/drivers/kscan/kscan_sdl.c new file mode 100644 index 0000000000..471aa2dae8 --- /dev/null +++ b/drivers/kscan/kscan_sdl.c @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2020 Jabil Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include + +LOG_MODULE_REGISTER(kscan, CONFIG_KSCAN_LOG_LEVEL); + +struct sdl_data { + kscan_callback_t callback; + bool enabled; +}; + +static int sdl_filter(void *arg, SDL_Event *event) +{ + struct device *dev = arg; + struct sdl_data *data = dev->driver_data; + u32_t row = 0; + u32_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(dev, row, column, pressed); + } + return 1; +} + +static int sdl_configure(struct device *dev, kscan_callback_t callback) +{ + struct sdl_data *data = dev->driver_data; + + if (!callback) { + LOG_ERR("Callback is null"); + return -EINVAL; + } + LOG_DBG("%s: set callback", dev->config->name); + + data->callback = callback; + + return 0; +} + +static int sdl_enable_callback(struct device *dev) +{ + struct sdl_data *data = dev->driver_data; + + LOG_DBG("%s: enable cb", dev->config->name); + data->enabled = true; + return 0; +} + +static int sdl_disable_callback(struct device *dev) +{ + struct sdl_data *data = dev->driver_data; + + LOG_DBG("%s: disable cb", dev->config->name); + data->enabled = false; + return 0; +} + +static int sdl_init(struct device *dev) +{ + LOG_INF("Init '%s' device", dev->config->name); + SDL_AddEventWatch(sdl_filter, dev); + + 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_AND_API_INIT(sdl, CONFIG_SDL_POINTER_KSCAN_DEV_NAME, sdl_init, + &sdl_data, NULL, + POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY, + &sdl_driver_api);