1 /*
2  * Copyright (c) 2018 Jan Van Winkel <jan.van_winkel@dxplore.eu>
3  * Copyright (c) 2023 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * "Bottom" of the SDL event handler for the POSIX architecture.
8  * When built with the native_simulator this will be built in the runner context,
9  * that is, with the host C library, and with the host include paths.
10  *
11  * Therefore it cannot include Zephyr headers
12  */
13 
14 #include <SDL.h>
15 
sdl_handle_window_event(const SDL_Event * event)16 static void sdl_handle_window_event(const SDL_Event *event)
17 {
18 	SDL_Window *window;
19 	SDL_Renderer *renderer;
20 
21 	switch (event->window.event) {
22 	case SDL_WINDOWEVENT_EXPOSED:
23 
24 		window = SDL_GetWindowFromID(event->window.windowID);
25 		if (window == NULL) {
26 			return;
27 		}
28 
29 		renderer = SDL_GetRenderer(window);
30 		if (renderer == NULL) {
31 			return;
32 		}
33 		SDL_RenderPresent(renderer);
34 		break;
35 	default:
36 		break;
37 	}
38 }
39 
40 /*
41  * Handle all pending display events
42  * Return 1 if the window was closed, 0 otherwise.
43  */
sdl_handle_pending_events(void)44 int sdl_handle_pending_events(void)
45 {
46 	SDL_Event event;
47 
48 	while (SDL_PollEvent(&event)) {
49 		switch (event.type) {
50 		case SDL_WINDOWEVENT:
51 			sdl_handle_window_event(&event);
52 			break;
53 		case SDL_QUIT:
54 			return 1;
55 		default:
56 			break;
57 		}
58 	}
59 	return 0;
60 }
61 
62 /*
63  * Initialize the SDL library
64  *
65  * Returns 0 on success, something else on failure.
66  */
sdl_init_video(void)67 int sdl_init_video(void)
68 {
69 	return SDL_Init(SDL_INIT_VIDEO);
70 }
71 
72 /*
73  * Trampoline to SDL_GetError
74  */
sdl_get_error(void)75 const char *sdl_get_error(void)
76 {
77 	return SDL_GetError();
78 }
79 
80 /*
81  * Trampoline to SDL_Quit()
82  */
sdl_quit(void)83 void sdl_quit(void)
84 {
85 	SDL_Quit();
86 }
87