[Concept,5/6] input: Add a command to show mouse input

Message ID 20250825144018.5.Ifebe6452f38904689e3e2142fc08a623131ed0de@changeid
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>

This reads mouse input and shows it on the terminal. It is useful for
testing mice.

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

 cmd/Kconfig  |  9 +++++++
 cmd/Makefile |  1 +
 cmd/mouse.c  | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 80 insertions(+)
 create mode 100644 cmd/mouse.c
  

Patch

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 882a4ee02e3..1a879d1fe90 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -2934,6 +2934,15 @@  config CMD_LOG
 	  maximum log level for emitting of records). It also provides access
 	  to a command used for testing the log system.
 
+config CMD_MOUSE
+	bool "mouse - Show mouse input"
+	default y if MOUSE
+	help
+	  This shows the data produced by a mouse. If a mouse device is
+	  available records are printed when the mouse is moved. This can be
+	  useful for checking that a mouse is working correctly within
+	  U-Boot.
+
 config CMD_TRACE
 	bool "trace - Support tracing of function calls and timing"
 	depends on TRACE
diff --git a/cmd/Makefile b/cmd/Makefile
index 407056b778b..2a9c39f18e2 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -128,6 +128,7 @@  obj-$(CONFIG_CMD_CLONE) += clone.o
 ifneq ($(CONFIG_CMD_NAND)$(CONFIG_CMD_SF),)
 obj-y += legacy-mtd-utils.o
 endif
+obj-$(CONFIG_CMD_MOUSE) += mouse.o
 obj-$(CONFIG_CMD_MUX) += mux.o
 obj-$(CONFIG_CMD_NAND) += nand.o
 ifdef CONFIG_NET
diff --git a/cmd/mouse.c b/cmd/mouse.c
new file mode 100644
index 00000000000..94434afa0c4
--- /dev/null
+++ b/cmd/mouse.c
@@ -0,0 +1,70 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Mouse testing
+ *
+ * Copyright 2020 Google LLC
+ * Written by Simon Glass <sjg@chromium.org>
+ */
+
+#include <command.h>
+#include <console.h>
+#include <dm.h>
+#include <mouse.h>
+#include <u-boot/schedule.h>
+
+static int do_mouse_dump(struct cmd_tbl *cmdtp, int flag, int argc,
+			 char *const argv[])
+{
+	struct udevice *dev;
+	bool running;
+	int count;
+	int ret;
+
+	ret = uclass_first_device_err(UCLASS_MOUSE, &dev);
+	if (ret) {
+		printf("Mouse not found (err=%d)\n", ret);
+		return CMD_RET_FAILURE;
+	}
+	for (running = true, count = 0; running;) {
+		struct mouse_event evt;
+
+		ret = mouse_get_event(dev, &evt);
+		if (!ret) {
+			switch (evt.type) {
+			case MOUSE_EV_BUTTON: {
+				struct mouse_button *but = &evt.button;
+
+				printf("button: button==%d, press=%d, clicks=%d, X=%d, Y=%d\n",
+				       but->button, but->press_state,
+				       but->clicks, but->x, but->y);
+				break;
+			}
+			case MOUSE_EV_MOTION: {
+				struct mouse_motion *motion = &evt.motion;
+
+				printf("motion: Xrel=%d, Yrel=%d, X=%d, Y=%d, but=%d\n",
+				       motion->xrel, motion->yrel, motion->x,
+				       motion->y, motion->state);
+				break;
+			}
+			case MOUSE_EV_NULL:
+				break;
+			}
+			count++;
+		} else if (ret != -EAGAIN) {
+			return log_msg_ret("get_event", ret);
+		}
+		schedule();
+		if (ctrlc())
+			running = false;
+	}
+	printf("%d events received\n", count);
+
+	return 0;
+}
+
+static char mouse_help_text[] =
+	"dump - Dump input from a mouse";
+
+U_BOOT_CMD_WITH_SUBCMDS(mouse, "Mouse input", mouse_help_text,
+	U_BOOT_SUBCMD_MKENT(dump, 1, 1, do_mouse_dump));