Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

native SDL drivers refactor so they can work with embedded libCs #60036

Merged
merged 5 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions boards/posix/common/sdl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ zephyr_library_compile_definitions(NO_POSIX_CHEATS)

find_package(PkgConfig REQUIRED)
pkg_search_module(SDL2 REQUIRED sdl2)
zephyr_include_directories(${SDL2_INCLUDE_DIRS})

if (CONFIG_NATIVE_APPLICATION)
zephyr_link_libraries(${SDL2_LIBRARIES})
zephyr_include_directories(${SDL2_INCLUDE_DIRS})
zephyr_compile_options(${SDL2_CFLAGS_OTHER})
else()
target_link_options(native_simulator INTERFACE "-l${SDL2_LIBRARIES}")
target_compile_options(native_simulator INTERFACE "-I${SDL2_INCLUDE_DIRS}" ${SDL2_CFLAGS_OTHER})
endif()
zephyr_compile_options(${SDL2_CFLAGS_OTHER})

zephyr_library_sources(sdl_events.c)
if (CONFIG_NATIVE_APPLICATION)
zephyr_library_sources(sdl_events_bottom.c)
else()
target_sources(native_simulator INTERFACE sdl_events_bottom.c)
endif()
51 changes: 8 additions & 43 deletions boards/posix/common/sdl/sdl_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,19 @@
#include "soc.h"
#include <zephyr/arch/posix/posix_trace.h>
#include <zephyr/kernel.h>

#include <SDL.h>

static void sdl_handle_window_event(const SDL_Event *event)
{
SDL_Window *window;
SDL_Renderer *renderer;

switch (event->window.event) {
case SDL_WINDOWEVENT_EXPOSED:

window = SDL_GetWindowFromID(event->window.windowID);
if (window == NULL) {
return;
}

renderer = SDL_GetRenderer(window);
if (renderer == NULL) {
return;
}
SDL_RenderPresent(renderer);
break;
default:
break;
}
}
#include "sdl_events_bottom.h"

static void sdl_handle_events(void *p1, void *p2, void *p3)
{
SDL_Event event;

ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);

for (;;) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_WINDOWEVENT:
sdl_handle_window_event(&event);
break;
case SDL_QUIT:
posix_exit(0);
break;
default:
break;
}
int rc = sdl_handle_pending_events();

if (rc != 0) {
posix_exit(0);
}

k_msleep(CONFIG_SDL_THREAD_INTERVAL);
Expand All @@ -63,15 +29,14 @@ static void sdl_handle_events(void *p1, void *p2, void *p3)

static void sdl_init(void)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
posix_print_error_and_exit("Error on SDL_Init (%s)\n",
SDL_GetError());
if (sdl_init_video() != 0) {
posix_print_error_and_exit("Error on SDL_Init (%s)\n", sdl_get_error());
}
}

static void sdl_cleanup(void)
{
SDL_Quit();
sdl_quit();
}

NATIVE_TASK(sdl_init, PRE_BOOT_2, 1);
Expand Down
86 changes: 86 additions & 0 deletions boards/posix/common/sdl/sdl_events_bottom.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2018 Jan Van Winkel <jan.van_winkel@dxplore.eu>
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*
* "Bottom" of the SDL event handler for the POSIX architecture.
* 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.
*
* Therefore it cannot include Zephyr headers
*/

#include <SDL.h>

static void sdl_handle_window_event(const SDL_Event *event)
{
SDL_Window *window;
SDL_Renderer *renderer;

switch (event->window.event) {
case SDL_WINDOWEVENT_EXPOSED:

window = SDL_GetWindowFromID(event->window.windowID);
if (window == NULL) {
return;
}

renderer = SDL_GetRenderer(window);
if (renderer == NULL) {
return;
}
SDL_RenderPresent(renderer);
break;
default:
break;
}
}

/*
* Handle all pending display events
* Return 1 if the window was closed, 0 otherwise.
*/
int sdl_handle_pending_events(void)
{
SDL_Event event;

while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_WINDOWEVENT:
sdl_handle_window_event(&event);
break;
case SDL_QUIT:
return 1;
default:
break;
}
}
return 0;
}

/*
* Initialize the SDL library
*
* Returns 0 on success, something else on failure.
*/
int sdl_init_video(void)
{
return SDL_Init(SDL_INIT_VIDEO);
}

/*
* Trampoline to SDL_GetError
*/
const char *sdl_get_error(void)
{
return SDL_GetError();
}

/*
* Trampoline to SDL_Quit()
*/
void sdl_quit(void)
{
SDL_Quit();
}
25 changes: 25 additions & 0 deletions boards/posix/common/sdl/sdl_events_bottom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef BOARDS_POSIX_COMMON_SDL_SDL_EVENTS_BOTTOM_H
#define BOARDS_POSIX_COMMON_SDL_SDL_EVENTS_BOTTOM_H

#ifdef __cplusplus
extern "C" {
#endif

/* Note: None of these functions are public interfaces. But internal to the SDL event handling */

int sdl_handle_pending_events(void);
int sdl_init_video(void);
void sdl_quit(void);
const char *sdl_get_error(void);

#ifdef __cplusplus
}
#endif

#endif /* BOARDS_POSIX_COMMON_SDL_SDL_EVENTS_BOTTOM_H */
6 changes: 3 additions & 3 deletions boards/posix/native_sim/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ host libC (:kconfig:option:`CONFIG_EXTERNAL_LIBC`).
bluetooth, userchan, :kconfig:option:`CONFIG_BT_USERCHAN`, host libC
can, can native posix, :kconfig:option:`CONFIG_CAN_NATIVE_POSIX_LINUX`, host libC
console backend, POSIX arch console, :kconfig:option:`CONFIG_POSIX_ARCH_CONSOLE`, all
display, display SDL, :kconfig:option:`CONFIG_SDL_DISPLAY`, host libC
display, display SDL, :kconfig:option:`CONFIG_SDL_DISPLAY`, all
entropy, native posix entropy, :kconfig:option:`CONFIG_FAKE_ENTROPY_NATIVE_POSIX`, host libC
eprom, eprom emulator, :kconfig:option:`CONFIG_EEPROM_EMULATOR`, host libC
ethernet, eth native_posix, :kconfig:option:`CONFIG_ETH_NATIVE_POSIX`, host libC
flash, flash simulator, :kconfig:option:`CONFIG_FLASH_SIMULATOR`, host libC
flash, host based flash access, :kconfig:option:`CONFIG_FUSE_FS_ACCESS`, host libC
gpio, GPIO emulator, :kconfig:option:`CONFIG_GPIO_EMUL`, all
gpio, SDL GPIO emulator, :kconfig:option:`CONFIG_GPIO_EMUL_SDL`, host libC
gpio, SDL GPIO emulator, :kconfig:option:`CONFIG_GPIO_EMUL_SDL`, all
i2c, I2C emulator, :kconfig:option:`CONFIG_I2C_EMUL`, all
input, input SDL touch, :kconfig:option:`CONFIG_INPUT_SDL_TOUCH`, host libC
input, input SDL touch, :kconfig:option:`CONFIG_INPUT_SDL_TOUCH`, all
log backend, native backend, :kconfig:option:`CONFIG_LOG_BACKEND_NATIVE_POSIX`, all
rtc, RTC emul, :kconfig:option:`CONFIG_RTC_EMUL`, all
serial, uart native posix/PTTY, :kconfig:option:`CONFIG_UART_NATIVE_POSIX`, host libC
Expand Down
10 changes: 9 additions & 1 deletion drivers/display/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ zephyr_library_sources_ifdef(CONFIG_ILI9488 display_ili9488.c)
zephyr_library_sources_ifdef(CONFIG_LS0XX ls0xx.c)
zephyr_library_sources_ifdef(CONFIG_MAX7219 display_max7219.c)
zephyr_library_sources_ifdef(CONFIG_OTM8009A display_otm8009a.c)
zephyr_library_sources_ifdef(CONFIG_SDL_DISPLAY display_sdl.c)
zephyr_library_sources_ifdef(CONFIG_SSD1306 ssd1306.c)
zephyr_library_sources_ifdef(CONFIG_SSD16XX ssd16xx.c)
zephyr_library_sources_ifdef(CONFIG_ST7789V display_st7789v.c)
Expand All @@ -27,3 +26,12 @@ zephyr_library_sources_ifdef(CONFIG_MICROBIT_DISPLAY
mb_display.c
mb_font.c
)

if (CONFIG_SDL_DISPLAY)
zephyr_library_sources(display_sdl.c)
if (CONFIG_NATIVE_APPLICATION)
zephyr_library_sources(display_sdl_bottom.c)
else()
target_sources(native_simulator INTERFACE display_sdl_bottom.c)
endif()
endif()
1 change: 0 additions & 1 deletion drivers/display/Kconfig.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ menuconfig SDL_DISPLAY
bool "SDL based emulated display"
default y
depends on DT_HAS_ZEPHYR_SDL_DC_ENABLED
depends on EXTERNAL_LIBC
select HAS_SDL
help
Enable SDL based emulated display compliant with display driver API.
Expand Down
Loading
Loading