diff --git a/include/types/wlr_xdg_shell.h b/include/types/wlr_xdg_shell.h index 1ad6b0ae..cfed6ee1 100644 --- a/include/types/wlr_xdg_shell.h +++ b/include/types/wlr_xdg_shell.h @@ -5,11 +5,6 @@ #include #include "xdg-shell-protocol.h" -struct wlr_xdg_positioner_resource { - struct wl_resource *resource; - struct wlr_xdg_positioner attrs; -}; - extern const struct wlr_surface_role xdg_toplevel_surface_role; extern const struct wlr_surface_role xdg_popup_surface_role; @@ -24,12 +19,10 @@ void xdg_surface_role_precommit(struct wlr_surface *wlr_surface, const struct wlr_surface_state *state); void create_xdg_positioner(struct wlr_xdg_client *client, uint32_t id); -struct wlr_xdg_positioner_resource *get_xdg_positioner_from_resource( - struct wl_resource *resource); void create_xdg_popup(struct wlr_xdg_surface *surface, struct wlr_xdg_surface *parent, - struct wlr_xdg_positioner_resource *positioner, uint32_t id); + struct wlr_xdg_positioner *positioner, uint32_t id); void unmap_xdg_popup(struct wlr_xdg_popup *popup); void destroy_xdg_popup(struct wlr_xdg_popup *popup); void handle_xdg_popup_committed(struct wlr_xdg_popup *popup); diff --git a/include/wlr/types/wlr_xdg_shell.h b/include/wlr/types/wlr_xdg_shell.h index d4a9f62b..72cd6f27 100644 --- a/include/wlr/types/wlr_xdg_shell.h +++ b/include/wlr/types/wlr_xdg_shell.h @@ -49,7 +49,7 @@ struct wlr_xdg_client { struct wl_event_source *ping_timer; }; -struct wlr_xdg_positioner { +struct wlr_xdg_positioner_rules { struct wlr_box anchor_rect; enum xdg_positioner_anchor anchor; enum xdg_positioner_gravity gravity; @@ -64,6 +64,11 @@ struct wlr_xdg_positioner { } offset; }; +struct wlr_xdg_positioner { + struct wl_resource *resource; + struct wlr_xdg_positioner_rules rules; +}; + struct wlr_xdg_popup { struct wlr_xdg_surface *base; struct wl_list link; @@ -77,7 +82,7 @@ struct wlr_xdg_popup { // geometry of the parent surface struct wlr_box geometry; - struct wlr_xdg_positioner positioner; + struct wlr_xdg_positioner_rules positioner_rules; struct wl_list grab_link; // wlr_xdg_popup_grab::popups }; @@ -276,6 +281,13 @@ struct wlr_xdg_popup *wlr_xdg_popup_from_resource( struct wlr_xdg_toplevel *wlr_xdg_toplevel_from_resource( struct wl_resource *resource); +/** Get the corresponding wlr_xdg_positioner from a resource. + * + * Aborts if the resource doesn't have the correct type. + */ +struct wlr_xdg_positioner *wlr_xdg_positioner_from_resource( + struct wl_resource *resource); + /** * Send a ping to the surface. If the surface does not respond in a reasonable * amount of time, the ping_timeout event will be emitted. @@ -346,12 +358,12 @@ void wlr_xdg_popup_destroy(struct wlr_xdg_popup *popup); */ void wlr_xdg_popup_get_position(struct wlr_xdg_popup *popup, double *popup_sx, double *popup_sy); + /** - * Get the geometry for this positioner based on the anchor rect, gravity, and - * size of this positioner. + * Get the geometry based on positioner rules. */ -struct wlr_box wlr_xdg_positioner_get_geometry( - struct wlr_xdg_positioner *positioner); +void wlr_xdg_positioner_rules_get_geometry( + struct wlr_xdg_positioner_rules *rules, struct wlr_box *box); /** * Get the anchor point for this popup in the toplevel parent's coordinate system. @@ -375,16 +387,18 @@ void wlr_xdg_popup_unconstrain_from_box(struct wlr_xdg_popup *popup, const struct wlr_box *toplevel_sx_box); /** - Invert the right/left anchor and gravity for this positioner. This can be - used to "flip" the positioner around the anchor rect in the x direction. + Invert the top/bottom anchor and gravity for those positioner rules. + This can be used to "flip" the positioner around the anchor rect in + the x direction. */ -void wlr_positioner_invert_x(struct wlr_xdg_positioner *positioner); +void wlr_xdg_positioner_rules_invert_x(struct wlr_xdg_positioner_rules *rules); /** - Invert the top/bottom anchor and gravity for this positioner. This can be - used to "flip" the positioner around the anchor rect in the y direction. + Invert the top/bottom anchor and gravity for those positioner rules. + This can be used to "flip" the positioner around the anchor rect in + the y direction. */ -void wlr_positioner_invert_y(struct wlr_xdg_positioner *positioner); +void wlr_xdg_positioner_rules_invert_y(struct wlr_xdg_positioner_rules *rules); /** * Find a surface within this xdg-surface tree at the given surface-local diff --git a/types/xdg_shell/wlr_xdg_popup.c b/types/xdg_shell/wlr_xdg_popup.c index eba829cc..e1e9653a 100644 --- a/types/xdg_shell/wlr_xdg_popup.c +++ b/types/xdg_shell/wlr_xdg_popup.c @@ -304,9 +304,9 @@ const struct wlr_surface_role xdg_popup_surface_role = { void create_xdg_popup(struct wlr_xdg_surface *surface, struct wlr_xdg_surface *parent, - struct wlr_xdg_positioner_resource *positioner, uint32_t id) { - if (positioner->attrs.size.width == 0 || - positioner->attrs.anchor_rect.width == 0) { + struct wlr_xdg_positioner *positioner, uint32_t id) { + if (positioner->rules.size.width == 0 || + positioner->rules.anchor_rect.width == 0) { wl_resource_post_error(surface->client->resource, XDG_WM_BASE_ERROR_INVALID_POSITIONER, "positioner object is not complete"); @@ -348,11 +348,10 @@ void create_xdg_popup(struct wlr_xdg_surface *surface, surface->role = WLR_XDG_SURFACE_ROLE_POPUP; - // positioner properties - memcpy(&surface->popup->positioner, &positioner->attrs, - sizeof(struct wlr_xdg_positioner)); - surface->popup->geometry = - wlr_xdg_positioner_get_geometry(&positioner->attrs); + memcpy(&surface->popup->positioner_rules, + &positioner->rules, sizeof(positioner->rules)); + wlr_xdg_positioner_rules_get_geometry( + &positioner->rules, &surface->popup->geometry); if (parent) { surface->popup->parent = parent->surface; @@ -412,8 +411,8 @@ void wlr_xdg_popup_destroy(struct wlr_xdg_popup *popup) { void wlr_xdg_popup_get_anchor_point(struct wlr_xdg_popup *popup, int *root_sx, int *root_sy) { - struct wlr_box rect = popup->positioner.anchor_rect; - enum xdg_positioner_anchor anchor = popup->positioner.anchor; + struct wlr_box rect = popup->positioner_rules.anchor_rect; + enum xdg_positioner_anchor anchor = popup->positioner_rules.anchor; int sx = 0, sy = 0; if (anchor == XDG_POSITIONER_ANCHOR_NONE) { @@ -511,22 +510,22 @@ static bool xdg_popup_unconstrain_flip(struct wlr_xdg_popup *popup, } bool flip_x = offset_x && - (popup->positioner.constraint_adjustment & + (popup->positioner_rules.constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X); bool flip_y = offset_y && - (popup->positioner.constraint_adjustment & + (popup->positioner_rules.constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y); if (flip_x) { - wlr_positioner_invert_x(&popup->positioner); + wlr_xdg_positioner_rules_invert_x(&popup->positioner_rules); } if (flip_y) { - wlr_positioner_invert_y(&popup->positioner); + wlr_xdg_positioner_rules_invert_y(&popup->positioner_rules); } - popup->geometry = - wlr_xdg_positioner_get_geometry(&popup->positioner); + wlr_xdg_positioner_rules_get_geometry( + &popup->positioner_rules, &popup->geometry); xdg_popup_box_constraints(popup, toplevel_sx_box, &offset_x, &offset_y); @@ -538,14 +537,14 @@ static bool xdg_popup_unconstrain_flip(struct wlr_xdg_popup *popup, // revert the positioner back if it didn't fix it and go to the next part if (offset_x && flip_x) { - wlr_positioner_invert_x(&popup->positioner); + wlr_xdg_positioner_rules_invert_x(&popup->positioner_rules); } if (offset_y && flip_y) { - wlr_positioner_invert_y(&popup->positioner); + wlr_xdg_positioner_rules_invert_y(&popup->positioner_rules); } - popup->geometry = - wlr_xdg_positioner_get_geometry(&popup->positioner); + wlr_xdg_positioner_rules_get_geometry( + &popup->positioner_rules, &popup->geometry); return false; } @@ -561,11 +560,11 @@ static bool xdg_popup_unconstrain_slide(struct wlr_xdg_popup *popup, } bool slide_x = offset_x && - (popup->positioner.constraint_adjustment & + (popup->positioner_rules.constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X); bool slide_y = offset_y && - (popup->positioner.constraint_adjustment & + (popup->positioner_rules.constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y); if (slide_x) { @@ -604,11 +603,11 @@ static bool xdg_popup_unconstrain_resize(struct wlr_xdg_popup *popup, } bool resize_x = offset_x && - (popup->positioner.constraint_adjustment & + (popup->positioner_rules.constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X); bool resize_y = offset_y && - (popup->positioner.constraint_adjustment & + (popup->positioner_rules.constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y); if (resize_x) { diff --git a/types/xdg_shell/wlr_xdg_positioner.c b/types/xdg_shell/wlr_xdg_positioner.c index aa149cc2..d4c6f03a 100644 --- a/types/xdg_shell/wlr_xdg_positioner.c +++ b/types/xdg_shell/wlr_xdg_positioner.c @@ -4,7 +4,7 @@ static const struct xdg_positioner_interface xdg_positioner_implementation; -struct wlr_xdg_positioner_resource *get_xdg_positioner_from_resource( +struct wlr_xdg_positioner *wlr_xdg_positioner_from_resource( struct wl_resource *resource) { assert(wl_resource_instance_of(resource, &xdg_positioner_interface, &xdg_positioner_implementation)); @@ -13,8 +13,8 @@ struct wlr_xdg_positioner_resource *get_xdg_positioner_from_resource( static void xdg_positioner_handle_set_size(struct wl_client *client, struct wl_resource *resource, int32_t width, int32_t height) { - struct wlr_xdg_positioner_resource *positioner = - get_xdg_positioner_from_resource(resource); + struct wlr_xdg_positioner *positioner = + wlr_xdg_positioner_from_resource(resource); if (width < 1 || height < 1) { wl_resource_post_error(resource, @@ -23,15 +23,15 @@ static void xdg_positioner_handle_set_size(struct wl_client *client, return; } - positioner->attrs.size.width = width; - positioner->attrs.size.height = height; + positioner->rules.size.width = width; + positioner->rules.size.height = height; } static void xdg_positioner_handle_set_anchor_rect(struct wl_client *client, struct wl_resource *resource, int32_t x, int32_t y, int32_t width, int32_t height) { - struct wlr_xdg_positioner_resource *positioner = - get_xdg_positioner_from_resource(resource); + struct wlr_xdg_positioner *positioner = + wlr_xdg_positioner_from_resource(resource); if (width < 0 || height < 0) { wl_resource_post_error(resource, @@ -40,16 +40,16 @@ static void xdg_positioner_handle_set_anchor_rect(struct wl_client *client, return; } - positioner->attrs.anchor_rect.x = x; - positioner->attrs.anchor_rect.y = y; - positioner->attrs.anchor_rect.width = width; - positioner->attrs.anchor_rect.height = height; + positioner->rules.anchor_rect.x = x; + positioner->rules.anchor_rect.y = y; + positioner->rules.anchor_rect.width = width; + positioner->rules.anchor_rect.height = height; } static void xdg_positioner_handle_set_anchor(struct wl_client *client, struct wl_resource *resource, uint32_t anchor) { - struct wlr_xdg_positioner_resource *positioner = - get_xdg_positioner_from_resource(resource); + struct wlr_xdg_positioner *positioner = + wlr_xdg_positioner_from_resource(resource); if (anchor > XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT) { wl_resource_post_error(resource, @@ -58,13 +58,13 @@ static void xdg_positioner_handle_set_anchor(struct wl_client *client, return; } - positioner->attrs.anchor = anchor; + positioner->rules.anchor = anchor; } static void xdg_positioner_handle_set_gravity(struct wl_client *client, struct wl_resource *resource, uint32_t gravity) { - struct wlr_xdg_positioner_resource *positioner = - get_xdg_positioner_from_resource(resource); + struct wlr_xdg_positioner *positioner = + wlr_xdg_positioner_from_resource(resource); if (gravity > XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT) { wl_resource_post_error(resource, @@ -73,25 +73,25 @@ static void xdg_positioner_handle_set_gravity(struct wl_client *client, return; } - positioner->attrs.gravity = gravity; + positioner->rules.gravity = gravity; } static void xdg_positioner_handle_set_constraint_adjustment( struct wl_client *client, struct wl_resource *resource, uint32_t constraint_adjustment) { - struct wlr_xdg_positioner_resource *positioner = - get_xdg_positioner_from_resource(resource); + struct wlr_xdg_positioner *positioner = + wlr_xdg_positioner_from_resource(resource); - positioner->attrs.constraint_adjustment = constraint_adjustment; + positioner->rules.constraint_adjustment = constraint_adjustment; } static void xdg_positioner_handle_set_offset(struct wl_client *client, struct wl_resource *resource, int32_t x, int32_t y) { - struct wlr_xdg_positioner_resource *positioner = - get_xdg_positioner_from_resource(resource); + struct wlr_xdg_positioner *positioner = + wlr_xdg_positioner_from_resource(resource); - positioner->attrs.offset.x = x; - positioner->attrs.offset.y = y; + positioner->rules.offset.x = x; + positioner->rules.offset.y = y; } static void xdg_positioner_handle_destroy(struct wl_client *client, @@ -113,14 +113,13 @@ static const struct xdg_positioner_interface static void xdg_positioner_handle_resource_destroy( struct wl_resource *resource) { - struct wlr_xdg_positioner_resource *positioner = - get_xdg_positioner_from_resource(resource); + struct wlr_xdg_positioner *positioner = + wlr_xdg_positioner_from_resource(resource); free(positioner); } void create_xdg_positioner(struct wlr_xdg_client *client, uint32_t id) { - struct wlr_xdg_positioner_resource *positioner = - calloc(1, sizeof(struct wlr_xdg_positioner_resource)); + struct wlr_xdg_positioner *positioner = calloc(1, sizeof(*positioner)); if (positioner == NULL) { wl_client_post_no_memory(client->client); return; @@ -171,60 +170,40 @@ static bool positioner_gravity_has_edge(enum xdg_positioner_gravity gravity, (enum xdg_positioner_anchor)edge); } -struct wlr_box wlr_xdg_positioner_get_geometry( - struct wlr_xdg_positioner *positioner) { - struct wlr_box geometry = { - .x = positioner->offset.x, - .y = positioner->offset.y, - .width = positioner->size.width, - .height = positioner->size.height, - }; - - if (positioner_anchor_has_edge(positioner->anchor, - XDG_POSITIONER_ANCHOR_TOP)) { - geometry.y += positioner->anchor_rect.y; - } else if (positioner_anchor_has_edge(positioner->anchor, - XDG_POSITIONER_ANCHOR_BOTTOM)) { - geometry.y += - positioner->anchor_rect.y + positioner->anchor_rect.height; +void wlr_xdg_positioner_rules_get_geometry( + struct wlr_xdg_positioner_rules *rules, struct wlr_box *box) { + box->x = rules->offset.x; + box->y = rules->offset.y; + box->width = rules->size.width; + box->height = rules->size.height; + + if (positioner_anchor_has_edge(rules->anchor, XDG_POSITIONER_ANCHOR_TOP)) { + box->y += rules->anchor_rect.y; + } else if (positioner_anchor_has_edge(rules->anchor, XDG_POSITIONER_ANCHOR_BOTTOM)) { + box->y += rules->anchor_rect.y + rules->anchor_rect.height; } else { - geometry.y += - positioner->anchor_rect.y + positioner->anchor_rect.height / 2; + box->y += rules->anchor_rect.y + rules->anchor_rect.height / 2; } - if (positioner_anchor_has_edge(positioner->anchor, - XDG_POSITIONER_ANCHOR_LEFT)) { - geometry.x += positioner->anchor_rect.x; - } else if (positioner_anchor_has_edge(positioner->anchor, - XDG_POSITIONER_ANCHOR_RIGHT)) { - geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width; + if (positioner_anchor_has_edge(rules->anchor, XDG_POSITIONER_ANCHOR_LEFT)) { + box->x += rules->anchor_rect.x; + } else if (positioner_anchor_has_edge(rules->anchor, XDG_POSITIONER_ANCHOR_RIGHT)) { + box->x += rules->anchor_rect.x + rules->anchor_rect.width; } else { - geometry.x += - positioner->anchor_rect.x + positioner->anchor_rect.width / 2; + box->x += rules->anchor_rect.x + rules->anchor_rect.width / 2; } - if (positioner_gravity_has_edge(positioner->gravity, - XDG_POSITIONER_GRAVITY_TOP)) { - geometry.y -= geometry.height; - } else if (!positioner_gravity_has_edge(positioner->gravity, - XDG_POSITIONER_GRAVITY_BOTTOM)) { - geometry.y -= geometry.height / 2; + if (positioner_gravity_has_edge(rules->gravity, XDG_POSITIONER_GRAVITY_TOP)) { + box->y -= box->height; + } else if (!positioner_gravity_has_edge(rules->gravity, XDG_POSITIONER_GRAVITY_BOTTOM)) { + box->y -= box->height / 2; } - if (positioner_gravity_has_edge(positioner->gravity, - XDG_POSITIONER_GRAVITY_LEFT)) { - geometry.x -= geometry.width; - } else if (!positioner_gravity_has_edge(positioner->gravity, - XDG_POSITIONER_GRAVITY_RIGHT)) { - geometry.x -= geometry.width / 2; + if (positioner_gravity_has_edge(rules->gravity, XDG_POSITIONER_GRAVITY_LEFT)) { + box->x -= box->width; + } else if (!positioner_gravity_has_edge(rules->gravity, XDG_POSITIONER_GRAVITY_RIGHT)) { + box->x -= box->width / 2; } - - if (positioner->constraint_adjustment == - XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE) { - return geometry; - } - - return geometry; } static enum xdg_positioner_anchor positioner_anchor_invert_x( @@ -281,13 +260,12 @@ static enum xdg_positioner_gravity positioner_gravity_invert_y( (enum xdg_positioner_anchor)gravity); } - -void wlr_positioner_invert_x(struct wlr_xdg_positioner *positioner) { - positioner->anchor = positioner_anchor_invert_x(positioner->anchor); - positioner->gravity = positioner_gravity_invert_x(positioner->gravity); +void wlr_xdg_positioner_rules_invert_x(struct wlr_xdg_positioner_rules *rules) { + rules->anchor = positioner_anchor_invert_x(rules->anchor); + rules->gravity = positioner_gravity_invert_x(rules->gravity); } -void wlr_positioner_invert_y(struct wlr_xdg_positioner *positioner) { - positioner->anchor = positioner_anchor_invert_y(positioner->anchor); - positioner->gravity = positioner_gravity_invert_y(positioner->gravity); +void wlr_xdg_positioner_rules_invert_y(struct wlr_xdg_positioner_rules *rules) { + rules->anchor = positioner_anchor_invert_y(rules->anchor); + rules->gravity = positioner_gravity_invert_y(rules->gravity); } diff --git a/types/xdg_shell/wlr_xdg_surface.c b/types/xdg_shell/wlr_xdg_surface.c index e65c6afd..73ed66c9 100644 --- a/types/xdg_shell/wlr_xdg_surface.c +++ b/types/xdg_shell/wlr_xdg_surface.c @@ -186,8 +186,8 @@ static void xdg_surface_handle_get_popup(struct wl_client *client, if (xdg_surface == NULL) { return; // TODO: create an inert xdg_popup } - struct wlr_xdg_positioner_resource *positioner = - get_xdg_positioner_from_resource(positioner_resource); + struct wlr_xdg_positioner *positioner = + wlr_xdg_positioner_from_resource(positioner_resource); create_xdg_popup(xdg_surface, parent, positioner, id); }