Skip to content

Commit 17c7089

Browse files
committed
boards: posix: native_posix: Handle SDL events in zephyr thread
This commit introduces a zephyr thread for SDL events. This allows to use zephyr mutex locking with SDL callbacks. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent 7e11b98 commit 17c7089

File tree

4 files changed

+40
-47
lines changed

4 files changed

+40
-47
lines changed

boards/posix/native_posix/Kconfig

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,18 @@ config HAS_SDL
2626
help
2727
This option specifies that the target board has SDL support
2828

29-
endif
29+
config SDL_THREAD_INTERVAL
30+
int "SDL thread sleep interval"
31+
default 10
32+
depends on HAS_SDL
33+
help
34+
Sleep interval time of SDL thread to handle events in milliseconds.
35+
36+
config SDL_THREAD_PRIORITY
37+
int "SDL thread priority"
38+
default 0
39+
depends on HAS_SDL
40+
help
41+
Priority of SDL thread to handle events.
42+
43+
endif # BOARD_NATIVE_POSIX

boards/posix/native_posix/hw_models_top.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "hw_counter.h"
2222
#include <zephyr/arch/posix/posix_soc_if.h>
2323
#include "posix_arch_internal.h"
24-
#include "sdl_events.h"
2524
#include <zephyr/sys/util.h>
2625

2726

@@ -32,17 +31,11 @@ static uint64_t end_of_time = NEVER; /* When will this device stop */
3231
extern uint64_t hw_timer_timer; /* When should this timer_model be called */
3332
extern uint64_t irq_ctrl_timer;
3433
extern uint64_t hw_counter_timer;
35-
#ifdef CONFIG_HAS_SDL
36-
extern uint64_t sdl_event_timer;
37-
#endif
3834

3935
static enum {
4036
HWTIMER = 0,
4137
IRQCNT,
4238
HW_COUNTER,
43-
#ifdef CONFIG_HAS_SDL
44-
SDLEVENTTIMER,
45-
#endif
4639
NUMBER_OF_TIMERS,
4740
NONE
4841
} next_timer_index = NONE;
@@ -51,9 +44,6 @@ static uint64_t *Timer_list[NUMBER_OF_TIMERS] = {
5144
&hw_timer_timer,
5245
&irq_ctrl_timer,
5346
&hw_counter_timer,
54-
#ifdef CONFIG_HAS_SDL
55-
&sdl_event_timer,
56-
#endif
5747
};
5848

5949
static uint64_t next_timer_time;
@@ -154,11 +144,6 @@ void hwm_one_event(void)
154144
case HW_COUNTER:
155145
hw_counter_triggered();
156146
break;
157-
#ifdef CONFIG_HAS_SDL
158-
case SDLEVENTTIMER:
159-
sdl_handle_events();
160-
break;
161-
#endif
162147
default:
163148
/* LCOV_EXCL_START */
164149
posix_print_error_and_exit(

boards/posix/native_posix/sdl_events.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include <SDL.h>
87
#include "posix_board_if.h"
9-
#include <zephyr/arch/posix/posix_trace.h>
10-
#include "posix_arch_internal.h"
118
#include "soc.h"
12-
#include "hw_models_top.h"
9+
#include <zephyr/arch/posix/posix_trace.h>
10+
#include <zephyr/kernel.h>
1311

14-
uint64_t sdl_event_timer;
12+
#include <SDL.h>
1513

1614
static void sdl_handle_window_event(const SDL_Event *event)
1715
{
@@ -37,25 +35,30 @@ static void sdl_handle_window_event(const SDL_Event *event)
3735
}
3836
}
3937

40-
void sdl_handle_events(void)
38+
static void sdl_handle_events(void *p1, void *p2, void *p3)
4139
{
4240
SDL_Event event;
4341

44-
sdl_event_timer = hwm_get_time() + 10000;
42+
ARG_UNUSED(p1);
43+
ARG_UNUSED(p2);
44+
ARG_UNUSED(p3);
4545

46-
while (SDL_PollEvent(&event)) {
47-
switch (event.type) {
48-
case SDL_WINDOWEVENT:
49-
sdl_handle_window_event(&event);
50-
break;
51-
case SDL_QUIT:
52-
posix_exit(0);
53-
break;
54-
default:
55-
break;
46+
for (;;) {
47+
while (SDL_PollEvent(&event)) {
48+
switch (event.type) {
49+
case SDL_WINDOWEVENT:
50+
sdl_handle_window_event(&event);
51+
break;
52+
case SDL_QUIT:
53+
posix_exit(0);
54+
break;
55+
default:
56+
break;
57+
}
5658
}
57-
}
5859

60+
k_msleep(CONFIG_SDL_THREAD_INTERVAL);
61+
}
5962
}
6063

6164
static void sdl_init(void)
@@ -73,3 +76,7 @@ static void sdl_cleanup(void)
7376

7477
NATIVE_TASK(sdl_init, PRE_BOOT_2, 1);
7578
NATIVE_TASK(sdl_cleanup, ON_EXIT, 2);
79+
80+
K_THREAD_DEFINE(sdl, CONFIG_ARCH_POSIX_RECOMMENDED_STACK_SIZE,
81+
sdl_handle_events, NULL, NULL, NULL,
82+
CONFIG_SDL_THREAD_PRIORITY, K_ESSENTIAL, 0);

boards/posix/native_posix/sdl_events.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)