Skip to content

Commit

Permalink
input SDL: Split in top and bottom
Browse files Browse the repository at this point in the history
Split the SDL input driver in a top and bottom
to enable using it with embedded libCs.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
  • Loading branch information
aescolar committed Jul 5, 2023
1 parent 3824c51 commit 09b876c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 33 deletions.
9 changes: 8 additions & 1 deletion drivers/input/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ 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)
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()
zephyr_library_sources_ifdef(CONFIG_INPUT_XPT2046 input_xpt2046.c)
44 changes: 12 additions & 32 deletions drivers/input/input_sdl_touch.c
Original file line number Diff line number Diff line change
@@ -1,67 +1,47 @@
/*
* 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 <stdbool.h>
#include <zephyr/input/input.h>
#include <zephyr/logging/log.h>

#include <SDL.h>
#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) {
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;
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);
40 changes: 40 additions & 0 deletions drivers/input/input_sdl_touch_bottom.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 Jabil Inc.
* Copyright (c) 2023 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <SDL.h>
#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);

}
38 changes: 38 additions & 0 deletions drivers/input/input_sdl_touch_bottom.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
#include <stdbool.h>

#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 */

0 comments on commit 09b876c

Please sign in to comment.