From 720421c1930e27e95fab10b6b0e62b5fa497a354 Mon Sep 17 00:00:00 2001 From: Derek Foreman Date: Tue, 18 Jul 2023 14:25:27 -0500 Subject: [PATCH] libweston: Add and use weston_coord_surface_add/sub helpers We already have these for global coordinates, now we have them for surface coordinates too. In addition to removing some unsightly unadorned coordinate usage, this also adds appropriate coordinate space id checks at runtime. Signed-off-by: Derek Foreman --- include/libweston/matrix.h | 26 ++++++++++++++++++++++++++ libweston/compositor.c | 7 ++++--- libweston/data-device.c | 4 +--- libweston/input.c | 6 +++--- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/include/libweston/matrix.h b/include/libweston/matrix.h index a77cb2cad..fa2aac825 100644 --- a/include/libweston/matrix.h +++ b/include/libweston/matrix.h @@ -167,6 +167,19 @@ weston_coord_global_add(struct weston_coord_global a, return (struct weston_coord_global){ .c = weston_coord_add(a.c, b.c) }; } +static inline struct weston_coord_surface __attribute__ ((warn_unused_result)) +weston_coord_surface_add(struct weston_coord_surface a, + struct weston_coord_surface b) +{ + assert(a.coordinate_space_id && + a.coordinate_space_id == b.coordinate_space_id); + + return (struct weston_coord_surface){ + .c = weston_coord_add(a.c, b.c), + .coordinate_space_id = a.coordinate_space_id, + }; +} + static inline struct weston_coord __attribute__ ((warn_unused_result)) weston_coord_sub(struct weston_coord a, struct weston_coord b) { @@ -180,6 +193,19 @@ weston_coord_global_sub(struct weston_coord_global a, return (struct weston_coord_global){ .c = weston_coord_sub(a.c, b.c) }; } +static inline struct weston_coord_surface __attribute__ ((warn_unused_result)) +weston_coord_surface_sub(struct weston_coord_surface a, + struct weston_coord_surface b) +{ + assert(a.coordinate_space_id && + a.coordinate_space_id == b.coordinate_space_id); + + return (struct weston_coord_surface){ + .c = weston_coord_sub(a.c, b.c), + .coordinate_space_id = a.coordinate_space_id, + }; +} + static inline struct weston_coord __attribute__ ((warn_unused_result)) weston_coord_truncate(struct weston_coord in) { diff --git a/libweston/compositor.c b/libweston/compositor.c index 9d5b98eea..82e3a43fa 100644 --- a/libweston/compositor.c +++ b/libweston/compositor.c @@ -4816,8 +4816,8 @@ weston_subsurface_commit_to_cache(struct weston_subsurface *sub) sub->cached.protection_mode = surface->pending.protection_mode; assert(surface->pending.acquire_fence_fd == -1); assert(surface->pending.buffer_release_ref.buffer_release == NULL); - sub->cached.buf_offset.c = weston_coord_add(sub->cached.buf_offset.c, - surface->pending.buf_offset.c); + sub->cached.buf_offset = weston_coord_surface_add(sub->cached.buf_offset, + surface->pending.buf_offset); sub->cached.buffer_viewport.buffer = surface->pending.buffer_viewport.buffer; @@ -4958,8 +4958,9 @@ subsurface_committed(struct weston_surface *surface, continue; } + new_origin.coordinate_space_id = view->geometry.parent->surface; tmp = weston_view_get_pos_offset_rel(view); - tmp.c = weston_coord_add(tmp.c, new_origin.c); + tmp = weston_coord_surface_add(tmp, new_origin); weston_view_set_rel_position(view, tmp); } /* No need to check parent mappedness, because if parent is not diff --git a/libweston/data-device.c b/libweston/data-device.c index 1c6698756..3565d5b63 100644 --- a/libweston/data-device.c +++ b/libweston/data-device.c @@ -435,9 +435,7 @@ drag_surface_configure(struct weston_drag *drag, drag->icon->is_mapped = true; } - assert(drag->offset.coordinate_space_id && - drag->offset.coordinate_space_id == new_origin.coordinate_space_id); - drag->offset.c = weston_coord_add(drag->offset.c, new_origin.c); + drag->offset = weston_coord_surface_add(drag->offset, new_origin); /* init to 0 for avoiding a compile warning */ pos.c = weston_coord(0, 0); diff --git a/libweston/input.c b/libweston/input.c index b70120fab..25e3280e8 100644 --- a/libweston/input.c +++ b/libweston/input.c @@ -3215,7 +3215,7 @@ tablet_tool_cursor_surface_committed(struct weston_surface *es, assert(es == tool->sprite->surface); - tool->hotspot.c = weston_coord_sub(tool->hotspot.c, new_origin.c); + tool->hotspot = weston_coord_surface_sub(tool->hotspot, new_origin); hotspot_inv = weston_coord_surface_invert(tool->hotspot); weston_view_set_position_with_offset(tool->sprite, tool->pos, hotspot_inv); @@ -3510,8 +3510,8 @@ pointer_cursor_surface_committed(struct weston_surface *es, assert(es == pointer->sprite->surface); - pointer->hotspot.c = weston_coord_sub(pointer->hotspot.c, - new_origin.c); + pointer->hotspot = weston_coord_surface_sub(pointer->hotspot, + new_origin); hotspot_inv = weston_coord_surface_invert(pointer->hotspot); weston_view_set_position_with_offset(pointer->sprite, pointer->pos, hotspot_inv);