[Concept,06/14] mouse: Track last mouse position

Message ID 20251006165452.1675349-7-sjg@u-boot.org
State New
Headers
Series expo: Continue development of expo with mouse |

Commit Message

Simon Glass Oct. 6, 2025, 4:54 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Add tracking of the last mouse position received, separate from the
click position. This allows callers to query the current mouse position
using mouse_get_pos() regardless of whether a click occurred.

The position is updated in mouse_get_event() for both motion and button
events, ensuring it always reflects the most recent mouse coordinates.

This avoid the problem of mouse_get_click() 'swallowing' motion events
so that a position change is not noticed, e.g. for showing a pointer.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/input/mouse-uclass.c | 22 ++++++++++++++++++++++
 include/mouse.h              | 11 +++++++++++
 2 files changed, 33 insertions(+)
  

Patch

diff --git a/drivers/input/mouse-uclass.c b/drivers/input/mouse-uclass.c
index 1bb836ff59d..ee983397ef3 100644
--- a/drivers/input/mouse-uclass.c
+++ b/drivers/input/mouse-uclass.c
@@ -10,6 +10,7 @@ 
 
 int mouse_get_event(struct udevice *dev, struct mouse_event *evt)
 {
+	struct mouse_uc_priv *uc_priv = dev_get_uclass_priv(dev);
 	struct mouse_ops *ops = mouse_get_ops(dev);
 	int ret;
 
@@ -20,6 +21,18 @@  int mouse_get_event(struct udevice *dev, struct mouse_event *evt)
 	if (ret)
 		return ret;
 
+	/* Update last position for motion events */
+	if (evt->type == MOUSE_EV_MOTION) {
+		uc_priv->last_pos.x = evt->motion.x;
+		uc_priv->last_pos.y = evt->motion.y;
+	}
+
+	/* Update last position for button events */
+	if (evt->type == MOUSE_EV_BUTTON) {
+		uc_priv->last_pos.x = evt->button.x;
+		uc_priv->last_pos.y = evt->button.y;
+	}
+
 	return 0;
 }
 
@@ -61,6 +74,15 @@  int mouse_get_click(struct udevice *dev, struct vid_pos *pos)
 	return -EAGAIN;
 }
 
+int mouse_get_pos(struct udevice *dev, struct vid_pos *pos)
+{
+	struct mouse_uc_priv *uc_priv = dev_get_uclass_priv(dev);
+
+	*pos = uc_priv->last_pos;
+
+	return 0;
+}
+
 UCLASS_DRIVER(mouse) = {
 	.id		= UCLASS_MOUSE,
 	.name		= "mouse",
diff --git a/include/mouse.h b/include/mouse.h
index 76f9c789b7b..7fe263b289f 100644
--- a/include/mouse.h
+++ b/include/mouse.h
@@ -37,10 +37,12 @@  enum mouse_press_state_t {
  *
  * @left_button_state: Current state of left button (BUTTON_PRESSED/BUTTON_RELEASED)
  * @click_pos: Position where the click occurred
+ * @last_pos: Last position received from mouse
  */
 struct mouse_uc_priv {
 	enum mouse_press_state_t left_button_state;
 	struct vid_pos click_pos;
+	struct vid_pos last_pos;
 };
 
 /**
@@ -100,4 +102,13 @@  int mouse_get_event(struct udevice *dev, struct mouse_event *event);
  */
 int mouse_get_click(struct udevice *dev, struct vid_pos *pos);
 
+/**
+ * mouse_get_pos() - Get the current mouse position
+ *
+ * @dev: Mouse device
+ * @pos: Returns last position
+ * Returns: 0 if position is available, -ve on error
+ */
+int mouse_get_pos(struct udevice *dev, struct vid_pos *pos);
+
 #endif