-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
98 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |