[Concept,3/6] sandbox: sdl: Add support for mouse input

Message ID 20250825204022.3655799-4-sjg@u-boot.org
State New
Headers
Series Provide basic support for a mouse |

Commit Message

Simon Glass Aug. 25, 2025, 8:40 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Allow mouse input to be reported from sandbox using SDL.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 arch/sandbox/cpu/sdl.c         | 48 ++++++++++++++++++++++++++++++----
 arch/sandbox/include/asm/sdl.h |  4 +++
 2 files changed, 47 insertions(+), 5 deletions(-)
  

Patch

diff --git a/arch/sandbox/cpu/sdl.c b/arch/sandbox/cpu/sdl.c
index ed84646bdab..b705e15cf9c 100644
--- a/arch/sandbox/cpu/sdl.c
+++ b/arch/sandbox/cpu/sdl.c
@@ -4,6 +4,7 @@ 
  */
 
 #include <errno.h>
+#include <mouse.h>
 #include <unistd.h>
 #include <stdbool.h>
 #include <sysreset.h>
@@ -67,15 +68,11 @@  static struct sdl_info {
 	SDL_Renderer *renderer;
 	SDL_Window *screen;
 	int src_depth;
+	struct mouse_event mouse;
 } sdl;
 
 static void sandbox_sdl_poll_events(void)
 {
-	/*
-	 * We don't want to include cpu_func.h in this file since it uses
-	 * system headers. So add a declation here.
-	 */
-	extern void reset_cpu(void);
 	SDL_Event event;
 
 	while (SDL_PollEvent(&event)) {
@@ -84,10 +81,50 @@  static void sandbox_sdl_poll_events(void)
 			puts("LCD window closed - quitting\n");
 			sysreset_walk(SYSRESET_POWER_OFF);
 			break;
+		case SDL_MOUSEMOTION: {
+			struct mouse_event *m = &sdl.mouse;
+
+			m->type = MOUSE_EV_MOTION;
+			m->motion.state = 0;
+			m->motion.x = event.motion.x;
+			m->motion.y = event.motion.y;
+			m->motion.xrel = event.motion.xrel;
+			m->motion.yrel = event.motion.yrel;
+			break;
+		}
+		case SDL_MOUSEBUTTONDOWN:
+		case SDL_MOUSEBUTTONUP: {
+			struct mouse_event *m = &sdl.mouse;
+
+			m->type = MOUSE_EV_BUTTON;
+			if (event.button.button == SDL_BUTTON_LEFT)
+				m->button.button = BUTTON_LEFT;
+			else if (event.button.button == SDL_BUTTON_MIDDLE)
+				m->button.button = BUTTON_MIDDLE;
+			else if (event.button.button == SDL_BUTTON_RIGHT)
+				m->button.button = BUTTON_RIGHT;
+			m->button.press_state = event.type ==
+				SDL_MOUSEBUTTONDOWN ?
+				BUTTON_PRESSED : BUTTON_RELEASED;
+			m->button.x = event.button.x;
+			m->button.y = event.motion.y;
+			break;
+		}
 		}
 	}
 }
 
+int sandbox_sdl_get_mouse_event(struct mouse_event *evt)
+{
+	if (sdl.mouse.type == MOUSE_EV_NULL)
+		return -EAGAIN;
+
+	*evt = sdl.mouse;
+	sdl.mouse.type = MOUSE_EV_NULL;
+
+	return 0;
+}
+
 static int sandbox_sdl_ensure_init(void)
 {
 	if (!sdl.inited) {
@@ -521,6 +558,7 @@  int sandbox_sdl_sound_init(int rate, int channels)
 	sdl.sample_rate = wanted.freq;
 	sdl.cur_buf = 0;
 	sdl.running = false;
+	sdl.mouse.type = MOUSE_EV_NULL;
 
 	return 0;
 
diff --git a/arch/sandbox/include/asm/sdl.h b/arch/sandbox/include/asm/sdl.h
index ee4991f7c24..190b52bd407 100644
--- a/arch/sandbox/include/asm/sdl.h
+++ b/arch/sandbox/include/asm/sdl.h
@@ -9,6 +9,8 @@ 
 #include <errno.h>
 #include <video.h>
 
+struct mouse_event;
+
 #ifdef CONFIG_SANDBOX_SDL
 
 /**
@@ -104,6 +106,8 @@  int sandbox_sdl_sound_init(int rate, int channels);
  */
 int sandbox_sdl_set_bpp(struct udevice *dev, enum video_log2_bpp l2bpp);
 
+int sandbox_sdl_get_mouse_event(struct mouse_event *evt);
+
 #else
 static inline int sandbox_sdl_init_display(int width, int height, int log2_bpp,
 					   bool double_size)