navigation: Add coordinate helper functions
authorVivienne Watermeier <vwatermeier@igalia.com>
Mon, 14 Feb 2022 13:22:29 +0000 (14:22 +0100)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Wed, 23 Mar 2022 13:14:51 +0000 (13:14 +0000)
Add a function to get x/y coordinates from suitable navigation events,
and one to create a copy with given coordinate values.

For e.g. translating event coordinates, this avoids having to either
switch on the event type to select the right parse function, or
having to rely on implementation details of the underlying event
structure.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1633>

subprojects/gst-plugins-base/gst-libs/gst/video/navigation.c
subprojects/gst-plugins-base/gst-libs/gst/video/navigation.h

index 4cad1de..7f0d551 100644 (file)
@@ -1103,3 +1103,78 @@ gst_navigation_event_parse_command (GstEvent * event,
 
   return ret;
 }
+
+/**
+ * gst_navigation_event_get_coordinates:
+ * @event: The #GstEvent to inspect.
+ * @x: (out) (optional): Pointer to a gdouble to receive the x coordinate of the
+ *     navigation event.
+ * @y: (out) (optional): Pointer to a gdouble to receive the y coordinate of the
+ *     navigation event.
+ *
+ * Try to retrieve x and y coordinates of a #GstNavigation event.
+ *
+ * Returns: A boolean indicating success.
+ *
+ * Since: 1.22
+ */
+gboolean
+gst_navigation_event_get_coordinates (GstEvent * event,
+    gdouble * x, gdouble * y)
+{
+  GstNavigationEventType e_type;
+  const GstStructure *s;
+  gboolean ret = TRUE;
+
+  e_type = gst_navigation_event_get_type (event);
+  if (e_type != GST_NAVIGATION_EVENT_MOUSE_MOVE
+      && e_type != GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS
+      && e_type != GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE) {
+    return FALSE;
+  }
+
+  s = gst_event_get_structure (event);
+  if (x)
+    ret &= gst_structure_get_double (s, "pointer_x", x);
+  if (y)
+    ret &= gst_structure_get_double (s, "pointer_y", y);
+
+  WARN_IF_FAIL (ret, "Couldn't extract coordinates from the event");
+
+  return ret;
+}
+
+/**
+ * gst_navigation_event_set_coordinates:
+ * @event: The #GstEvent to modify.
+ * @x: The x coordinate to set.
+ * @y: The y coordinate to set.
+ *
+ * Try to set x and y coordinates on a #GstNavigation event. The event must
+ * be writable.
+ *
+ * Returns: A boolean indicating success.
+ *
+ * Since: 1.22
+ */
+gboolean
+gst_navigation_event_set_coordinates (GstEvent * event, gdouble x, gdouble y)
+{
+  GstNavigationEventType e_type;
+  GstStructure *s;
+
+  g_return_val_if_fail (gst_event_is_writable (event), FALSE);
+
+  e_type = gst_navigation_event_get_type (event);
+  if (e_type != GST_NAVIGATION_EVENT_MOUSE_MOVE
+      && e_type != GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS
+      && e_type != GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE) {
+    return FALSE;
+  }
+
+  s = gst_event_writable_structure (event);
+  gst_structure_set (s, "pointer_x", G_TYPE_DOUBLE, x,
+      "pointer_y", G_TYPE_DOUBLE, y, NULL);
+
+  return TRUE;
+}
index a7dd93e..c4fb47a 100644 (file)
@@ -353,6 +353,14 @@ GST_VIDEO_API
 gboolean        gst_navigation_event_parse_command            (GstEvent *event,
                                                                GstNavigationCommand *command);
 
+GST_VIDEO_API
+gboolean  gst_navigation_event_get_coordinates (GstEvent * event,
+                                                gdouble * x, gdouble * y);
+
+GST_VIDEO_API
+gboolean  gst_navigation_event_set_coordinates (GstEvent * event,
+                                                gdouble x, gdouble y);
+
 /* interface virtual function wrappers */
 
 GST_VIDEO_DEPRECATED_FOR(gst_navigation_send_event_simple)