|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <wlr/util/edges.h>
|
|
|
|
|
#include "types/wlr_xdg_shell.h"
|
|
|
|
|
|
|
|
|
|
static const struct xdg_positioner_interface xdg_positioner_implementation;
|
|
|
|
@ -139,74 +140,79 @@ void create_xdg_positioner(struct wlr_xdg_client *client, uint32_t id) {
|
|
|
|
|
positioner, xdg_positioner_handle_resource_destroy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool positioner_anchor_has_edge(enum xdg_positioner_anchor anchor,
|
|
|
|
|
enum xdg_positioner_anchor edge) {
|
|
|
|
|
switch (edge) {
|
|
|
|
|
static uint32_t xdg_positioner_anchor_to_wlr_edges(
|
|
|
|
|
enum xdg_positioner_anchor anchor) {
|
|
|
|
|
switch (anchor) {
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_NONE:
|
|
|
|
|
return WLR_EDGE_NONE;
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_TOP:
|
|
|
|
|
return anchor == XDG_POSITIONER_ANCHOR_TOP ||
|
|
|
|
|
anchor == XDG_POSITIONER_ANCHOR_TOP_LEFT ||
|
|
|
|
|
anchor == XDG_POSITIONER_ANCHOR_TOP_RIGHT;
|
|
|
|
|
return WLR_EDGE_TOP;
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_TOP_LEFT:
|
|
|
|
|
return WLR_EDGE_TOP | WLR_EDGE_LEFT;
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_TOP_RIGHT:
|
|
|
|
|
return WLR_EDGE_TOP | WLR_EDGE_RIGHT;
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_BOTTOM:
|
|
|
|
|
return anchor == XDG_POSITIONER_ANCHOR_BOTTOM ||
|
|
|
|
|
anchor == XDG_POSITIONER_ANCHOR_BOTTOM_LEFT ||
|
|
|
|
|
anchor == XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT;
|
|
|
|
|
return WLR_EDGE_BOTTOM;
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_BOTTOM_LEFT:
|
|
|
|
|
return WLR_EDGE_BOTTOM | WLR_EDGE_LEFT;
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT:
|
|
|
|
|
return WLR_EDGE_BOTTOM | WLR_EDGE_RIGHT;
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_LEFT:
|
|
|
|
|
return anchor == XDG_POSITIONER_ANCHOR_LEFT ||
|
|
|
|
|
anchor == XDG_POSITIONER_ANCHOR_TOP_LEFT ||
|
|
|
|
|
anchor == XDG_POSITIONER_ANCHOR_BOTTOM_LEFT;
|
|
|
|
|
return WLR_EDGE_LEFT;
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_RIGHT:
|
|
|
|
|
return anchor == XDG_POSITIONER_ANCHOR_RIGHT ||
|
|
|
|
|
anchor == XDG_POSITIONER_ANCHOR_TOP_RIGHT ||
|
|
|
|
|
anchor == XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT;
|
|
|
|
|
default:
|
|
|
|
|
abort(); // unreachable
|
|
|
|
|
return WLR_EDGE_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abort(); // Unreachable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool positioner_gravity_has_edge(enum xdg_positioner_gravity gravity,
|
|
|
|
|
enum xdg_positioner_gravity edge) {
|
|
|
|
|
// gravity and edge enums are the same
|
|
|
|
|
return positioner_anchor_has_edge((enum xdg_positioner_anchor)gravity,
|
|
|
|
|
(enum xdg_positioner_anchor)edge);
|
|
|
|
|
static uint32_t xdg_positioner_gravity_to_wlr_edges(
|
|
|
|
|
enum xdg_positioner_gravity gravity) {
|
|
|
|
|
// Gravity and edge enums are the same
|
|
|
|
|
return xdg_positioner_anchor_to_wlr_edges((enum xdg_positioner_anchor)gravity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void wlr_xdg_positioner_rules_get_geometry(
|
|
|
|
|
struct wlr_xdg_positioner_rules *rules, struct wlr_box *box) {
|
|
|
|
|
const 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)) {
|
|
|
|
|
uint32_t edges = xdg_positioner_anchor_to_wlr_edges(rules->anchor);
|
|
|
|
|
|
|
|
|
|
if (edges & WLR_EDGE_TOP) {
|
|
|
|
|
box->y += rules->anchor_rect.y;
|
|
|
|
|
} else if (positioner_anchor_has_edge(rules->anchor, XDG_POSITIONER_ANCHOR_BOTTOM)) {
|
|
|
|
|
} else if (edges & WLR_EDGE_BOTTOM) {
|
|
|
|
|
box->y += rules->anchor_rect.y + rules->anchor_rect.height;
|
|
|
|
|
} else {
|
|
|
|
|
box->y += rules->anchor_rect.y + rules->anchor_rect.height / 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (positioner_anchor_has_edge(rules->anchor, XDG_POSITIONER_ANCHOR_LEFT)) {
|
|
|
|
|
if (edges & WLR_EDGE_LEFT) {
|
|
|
|
|
box->x += rules->anchor_rect.x;
|
|
|
|
|
} else if (positioner_anchor_has_edge(rules->anchor, XDG_POSITIONER_ANCHOR_RIGHT)) {
|
|
|
|
|
} else if (edges & WLR_EDGE_RIGHT) {
|
|
|
|
|
box->x += rules->anchor_rect.x + rules->anchor_rect.width;
|
|
|
|
|
} else {
|
|
|
|
|
box->x += rules->anchor_rect.x + rules->anchor_rect.width / 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (positioner_gravity_has_edge(rules->gravity, XDG_POSITIONER_GRAVITY_TOP)) {
|
|
|
|
|
edges = xdg_positioner_gravity_to_wlr_edges(rules->gravity);
|
|
|
|
|
|
|
|
|
|
if (edges & WLR_EDGE_TOP) {
|
|
|
|
|
box->y -= box->height;
|
|
|
|
|
} else if (!positioner_gravity_has_edge(rules->gravity, XDG_POSITIONER_GRAVITY_BOTTOM)) {
|
|
|
|
|
} else if (~edges & WLR_EDGE_BOTTOM) {
|
|
|
|
|
box->y -= box->height / 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (positioner_gravity_has_edge(rules->gravity, XDG_POSITIONER_GRAVITY_LEFT)) {
|
|
|
|
|
if (edges & WLR_EDGE_LEFT) {
|
|
|
|
|
box->x -= box->width;
|
|
|
|
|
} else if (!positioner_gravity_has_edge(rules->gravity, XDG_POSITIONER_GRAVITY_RIGHT)) {
|
|
|
|
|
} else if (~edges & WLR_EDGE_RIGHT) {
|
|
|
|
|
box->x -= box->width / 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum xdg_positioner_anchor positioner_anchor_invert_x(
|
|
|
|
|
static enum xdg_positioner_anchor xdg_positioner_anchor_invert_x(
|
|
|
|
|
enum xdg_positioner_anchor anchor) {
|
|
|
|
|
switch (anchor) {
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_LEFT:
|
|
|
|
@ -226,14 +232,14 @@ static enum xdg_positioner_anchor positioner_anchor_invert_x(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum xdg_positioner_gravity positioner_gravity_invert_x(
|
|
|
|
|
static enum xdg_positioner_gravity xdg_positioner_gravity_invert_x(
|
|
|
|
|
enum xdg_positioner_gravity gravity) {
|
|
|
|
|
// gravity and edge enums are the same
|
|
|
|
|
return (enum xdg_positioner_gravity)positioner_anchor_invert_x(
|
|
|
|
|
return (enum xdg_positioner_gravity)xdg_positioner_anchor_invert_x(
|
|
|
|
|
(enum xdg_positioner_anchor)gravity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum xdg_positioner_anchor positioner_anchor_invert_y(
|
|
|
|
|
static enum xdg_positioner_anchor xdg_positioner_anchor_invert_y(
|
|
|
|
|
enum xdg_positioner_anchor anchor) {
|
|
|
|
|
switch (anchor) {
|
|
|
|
|
case XDG_POSITIONER_ANCHOR_TOP:
|
|
|
|
@ -253,19 +259,213 @@ static enum xdg_positioner_anchor positioner_anchor_invert_y(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static enum xdg_positioner_gravity positioner_gravity_invert_y(
|
|
|
|
|
static enum xdg_positioner_gravity xdg_positioner_gravity_invert_y(
|
|
|
|
|
enum xdg_positioner_gravity gravity) {
|
|
|
|
|
// gravity and edge enums are the same
|
|
|
|
|
return (enum xdg_positioner_gravity)positioner_anchor_invert_y(
|
|
|
|
|
return (enum xdg_positioner_gravity)xdg_positioner_anchor_invert_y(
|
|
|
|
|
(enum xdg_positioner_anchor)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);
|
|
|
|
|
/**
|
|
|
|
|
* Distances from each edge of the box to the corresponding edge of
|
|
|
|
|
* the anchor rect. Each distance is positive if the edge is outside
|
|
|
|
|
* the anchor rect, and negative if the edge is inside it.
|
|
|
|
|
*/
|
|
|
|
|
struct constraint_offsets {
|
|
|
|
|
int top;
|
|
|
|
|
int bottom;
|
|
|
|
|
int left;
|
|
|
|
|
int right;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static bool is_unconstrained(const struct constraint_offsets *offsets) {
|
|
|
|
|
return offsets->top <= 0 && offsets->bottom <= 0 &&
|
|
|
|
|
offsets->left <= 0 && offsets->right <= 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void get_constrained_box_offsets(const struct wlr_box *constraint,
|
|
|
|
|
const struct wlr_box *box, struct constraint_offsets *offsets) {
|
|
|
|
|
offsets->left = constraint->x - box->x;
|
|
|
|
|
offsets->right = box->x + box->width - constraint->x - constraint->width;
|
|
|
|
|
offsets->top = constraint->y - box->y;
|
|
|
|
|
offsets->bottom = box->y + box->height - constraint->y - constraint->height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool xdg_positioner_rules_unconstrain_by_flip(
|
|
|
|
|
const struct wlr_xdg_positioner_rules *rules,
|
|
|
|
|
const struct wlr_box *constraint, struct wlr_box *box,
|
|
|
|
|
struct constraint_offsets *offsets) {
|
|
|
|
|
// If none of the edges are constrained, no need to flip.
|
|
|
|
|
// If both edges are constrained, the box is bigger than
|
|
|
|
|
// the anchor rect and flipping won't help anyway.
|
|
|
|
|
bool flip_x = ((offsets->left > 0) ^ (offsets->right > 0)) &&
|
|
|
|
|
(rules->constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X);
|
|
|
|
|
bool flip_y = ((offsets->top > 0) ^ (offsets->bottom > 0)) &&
|
|
|
|
|
(rules->constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y);
|
|
|
|
|
|
|
|
|
|
if (!flip_x && !flip_y) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct wlr_xdg_positioner_rules flipped = *rules;
|
|
|
|
|
if (flip_x) {
|
|
|
|
|
flipped.anchor = xdg_positioner_anchor_invert_x(flipped.anchor);
|
|
|
|
|
flipped.gravity = xdg_positioner_gravity_invert_x(flipped.gravity);
|
|
|
|
|
}
|
|
|
|
|
if (flip_y) {
|
|
|
|
|
flipped.anchor = xdg_positioner_anchor_invert_y(flipped.anchor);
|
|
|
|
|
flipped.gravity = xdg_positioner_gravity_invert_y(flipped.gravity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct wlr_box flipped_box;
|
|
|
|
|
wlr_xdg_positioner_rules_get_geometry(&flipped, &flipped_box);
|
|
|
|
|
struct constraint_offsets flipped_offsets;
|
|
|
|
|
get_constrained_box_offsets(constraint, &flipped_box, &flipped_offsets);
|
|
|
|
|
|
|
|
|
|
// Only apply flipping if it helps
|
|
|
|
|
if (flipped_offsets.left <= 0 && flipped_offsets.right <= 0) {
|
|
|
|
|
box->x = flipped_box.x;
|
|
|
|
|
offsets->left = flipped_offsets.left;
|
|
|
|
|
offsets->right = flipped_offsets.right;
|
|
|
|
|
}
|
|
|
|
|
if (flipped_offsets.top <= 0 && flipped_offsets.bottom <= 0) {
|
|
|
|
|
box->y = flipped_box.y;
|
|
|
|
|
offsets->top = flipped_offsets.top;
|
|
|
|
|
offsets->bottom = flipped_offsets.bottom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return is_unconstrained(offsets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool xdg_positioner_rules_unconstrain_by_slide(
|
|
|
|
|
const struct wlr_xdg_positioner_rules *rules,
|
|
|
|
|
const struct wlr_box *constraint, struct wlr_box *box,
|
|
|
|
|
struct constraint_offsets *offsets) {
|
|
|
|
|
uint32_t gravity = xdg_positioner_gravity_to_wlr_edges(rules->gravity);
|
|
|
|
|
|
|
|
|
|
// We can only slide if there is gravity on this axis
|
|
|
|
|
bool slide_x = (offsets->left > 0 || offsets->right > 0) &&
|
|
|
|
|
(rules->constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X) &&
|
|
|
|
|
(gravity & (WLR_EDGE_LEFT | WLR_EDGE_RIGHT));
|
|
|
|
|
bool slide_y = (offsets->top > 0 || offsets->bottom > 0) &&
|
|
|
|
|
(rules->constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y) &&
|
|
|
|
|
(gravity & (WLR_EDGE_TOP | WLR_EDGE_BOTTOM));
|
|
|
|
|
|
|
|
|
|
if (!slide_x && !slide_y) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (slide_x) {
|
|
|
|
|
if (offsets->left > 0 && offsets->right > 0) {
|
|
|
|
|
// The protocol states: "First try to slide towards the direction of
|
|
|
|
|
// the gravity [...] Then try to slide towards the opposite direction
|
|
|
|
|
// of the gravity". The only situation where this order matters is when
|
|
|
|
|
// the box is bigger than the anchor rect and completely includes it.
|
|
|
|
|
// In this case, the second slide will fail immediately, so simply
|
|
|
|
|
// slide towards the direction of the gravity.
|
|
|
|
|
if (gravity & WLR_EDGE_LEFT) {
|
|
|
|
|
box->x -= offsets->right;
|
|
|
|
|
} else if (gravity & WLR_EDGE_RIGHT) {
|
|
|
|
|
box->x += offsets->left;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// If at least one edge is already unconstrained, the order of slide
|
|
|
|
|
// attempts doesn't matter. Slide for the minimal distance needed to
|
|
|
|
|
// satisfy the requirement of constraining one edge or unconstraining
|
|
|
|
|
// another.
|
|
|
|
|
int abs_left = offsets->left > 0 ? offsets->left : -offsets->left;
|
|
|
|
|
int abs_right = offsets->right > 0 ? offsets->right : -offsets->right;
|
|
|
|
|
if (abs_left < abs_right) {
|
|
|
|
|
box->x += offsets->left;
|
|
|
|
|
} else {
|
|
|
|
|
box->x -= offsets->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (slide_y) {
|
|
|
|
|
if (offsets->top > 0 && offsets->bottom > 0) {
|
|
|
|
|
if (gravity & WLR_EDGE_TOP) {
|
|
|
|
|
box->y -= offsets->bottom;
|
|
|
|
|
} else if (gravity & WLR_EDGE_BOTTOM) {
|
|
|
|
|
box->y += offsets->top;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
int abs_top = offsets->top > 0 ? offsets->top : -offsets->top;
|
|
|
|
|
int abs_bottom = offsets->bottom > 0 ? offsets->bottom : -offsets->bottom;
|
|
|
|
|
if (abs_top < abs_bottom) {
|
|
|
|
|
box->y += offsets->top;
|
|
|
|
|
} else {
|
|
|
|
|
box->y -= offsets->bottom;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get_constrained_box_offsets(constraint, box, offsets);
|
|
|
|
|
return is_unconstrained(offsets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
static bool xdg_positioner_rules_unconstrain_by_resize(
|
|
|
|
|
const struct wlr_xdg_positioner_rules *rules,
|
|
|
|
|
const struct wlr_box *constraint, struct wlr_box *box,
|
|
|
|
|
struct constraint_offsets *offsets) {
|
|
|
|
|
bool resize_x = (offsets->left > 0 || offsets->right > 0) &&
|
|
|
|
|
(rules->constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X);
|
|
|
|
|
bool resize_y = (offsets->top > 0 || offsets->bottom > 0) &&
|
|
|
|
|
(rules->constraint_adjustment & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y);
|
|
|
|
|
|
|
|
|
|
if (!resize_x && !resize_y) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (offsets->left < 0) {
|
|
|
|
|
offsets->left = 0;
|
|
|
|
|
}
|
|
|
|
|
if (offsets->right < 0) {
|
|
|
|
|
offsets->right = 0;
|
|
|
|
|
}
|
|
|
|
|
if (offsets->top < 0) {
|
|
|
|
|
offsets->top = 0;
|
|
|
|
|
}
|
|
|
|
|
if (offsets->bottom < 0) {
|
|
|
|
|
offsets->bottom = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to satisfy the constraints by clipping the box.
|
|
|
|
|
struct wlr_box resized_box = *box;
|
|
|
|
|
if (resize_x) {
|
|
|
|
|
resized_box.x += offsets->left;
|
|
|
|
|
resized_box.width -= offsets->left + offsets->right;
|
|
|
|
|
}
|
|
|
|
|
if (resize_y) {
|
|
|
|
|
resized_box.y += offsets->top;
|
|
|
|
|
resized_box.height -= offsets->top + offsets->bottom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (wlr_box_empty(&resized_box)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*box = resized_box;
|
|
|
|
|
get_constrained_box_offsets(constraint, box, offsets);
|
|
|
|
|
return is_unconstrained(offsets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void wlr_xdg_positioner_rules_unconstrain_box(
|
|
|
|
|
const struct wlr_xdg_positioner_rules *rules,
|
|
|
|
|
const struct wlr_box *constraint, struct wlr_box *box) {
|
|
|
|
|
struct constraint_offsets offsets;
|
|
|
|
|
get_constrained_box_offsets(constraint, box, &offsets);
|
|
|
|
|
if (is_unconstrained(&offsets)) {
|
|
|
|
|
// Already unconstrained
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (xdg_positioner_rules_unconstrain_by_flip(rules, constraint, box, &offsets)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (xdg_positioner_rules_unconstrain_by_slide(rules, constraint, box, &offsets)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (xdg_positioner_rules_unconstrain_by_resize(rules, constraint, box, &offsets)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|