From f402b2e180ea5f82432c867d7367f1f5d0a54a1e Mon Sep 17 00:00:00 2001 From: Vivienne Watermeier Date: Mon, 14 Feb 2022 14:22:29 +0100 Subject: [PATCH] navigation: Add coordinate helper functions 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: --- .../gst-libs/gst/video/navigation.c | 75 +++++++++++++++++++ .../gst-libs/gst/video/navigation.h | 8 ++ 2 files changed, 83 insertions(+) diff --git a/subprojects/gst-plugins-base/gst-libs/gst/video/navigation.c b/subprojects/gst-plugins-base/gst-libs/gst/video/navigation.c index 4cad1de393..7f0d5519af 100644 --- a/subprojects/gst-plugins-base/gst-libs/gst/video/navigation.c +++ b/subprojects/gst-plugins-base/gst-libs/gst/video/navigation.c @@ -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; +} diff --git a/subprojects/gst-plugins-base/gst-libs/gst/video/navigation.h b/subprojects/gst-plugins-base/gst-libs/gst/video/navigation.h index a7dd93ed0b..c4fb47a6d7 100644 --- a/subprojects/gst-plugins-base/gst-libs/gst/video/navigation.h +++ b/subprojects/gst-plugins-base/gst-libs/gst/video/navigation.h @@ -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)