[Concept,05/17] sandbox: sdl: Add support for mouse input

Message ID 20250915104705.937780-6-sjg@u-boot.org
State New
Headers
Series mouse: Provide some support for using a mouse |

Commit Message

Simon Glass Sept. 15, 2025, 10:46 a.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         | 43 ++++++++++++++++++++++++++++++++++
 arch/sandbox/include/asm/sdl.h |  9 +++++++
 2 files changed, 52 insertions(+)
  

Patch

diff --git a/arch/sandbox/cpu/sdl.c b/arch/sandbox/cpu/sdl.c
index 5a323a607f1..fa431010b11 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,6 +68,7 @@  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)
@@ -84,10 +86,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) {
@@ -522,6 +564,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..86368c8a0a6 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)
@@ -157,6 +161,11 @@  static inline int sandbox_sdl_set_bpp(struct udevice *dev,
 	return -ENOSYS;
 }
 
+static inline int sandbox_sdl_get_mouse_event(struct mouse_event *evt)
+{
+	return -ENODEV;
+}
+
 #endif
 
 #endif