wlroots-hyprland/util/box.c

231 lines
5.6 KiB
C
Raw Normal View History

#include <math.h>
#include <stdbool.h>
2018-02-12 21:29:23 +01:00
#include <stdlib.h>
2021-07-01 22:36:01 +02:00
#include <wlr/util/box.h>
#include <wlr/util/log.h>
2017-12-31 12:49:06 +01:00
void wlr_box_closest_point(const struct wlr_box *box, double x, double y,
double *dest_x, double *dest_y) {
// if box is empty, then it contains no points, so no closest point either
if (wlr_box_empty(box)) {
*dest_x = NAN;
*dest_y = NAN;
return;
}
util: fix non-linear behavior of wlr_box_closest_point() Per comments in util/box.h, the width and height of a wlr_box are exclusive; that is, for a 100x100 box at (0,0), the point (99,99) is inside it while the point (100,100) is outside it. Thus mathematically, there exists no single closest point to the bottom-right corner of the box while remaining inside it. You can construct an infinite series approaching the limit, such as {(99,99), (99.9,99.9), (99.99,99.99)...}, but since the intervals are half-open, there is no "last" point. wlr_box_closest_point() must therefore define an arbitrary "closest" point. For points below and to the right of the box, the current implementation returns (box.x + width - 1, box.y + height - 1). Let's continue to do this. However, the current implementation is non-linear: with the example 100x100 box, it will return an input point of (99.9,99.9) unchanged, but for an input point (100.1,100.1) the returned point will jump back to (99.0,99.0). In practice, this non-linearity results in strange behaviors when driving the mouse cursor to a screen corner. On a 1920x1080 display for example, driving the cursor quickly to the bottom-left corner results in a position of exactly (0,1079). Continuing to slowly nudge the cursor downward results in the position jumping between (0,1079) and other, fractional coordinates such as (0,1079.88). The fractional coordinates expose some client/toolkit-side bugs (which, to be clear, should be fixed on the client side), but IMHO the wlroots behavior is also inconsistent and wrong -- when I drive the mouse cursor into the corner of the screen, it should come to a stop at a fixed position, not jitter around.
2024-03-20 04:54:24 +01:00
// Note: the width and height of the box are exclusive; that is,
// for a 100x100 box at (0,0), the point (99,99) is inside it
// while the point (100,100) is outside it.
//
// Mathematically, there exists no single closest point to the
// bottom-right corner of the box while remaining inside it. You
// can construct an infinite series approaching the limit, such
// as {(99,99), (99.9,99.9), (99.99,99.99)...}, but since the
// intervals are half-open, there is no "last" point.
//
// This function must therefore define an arbitrary "closest"
// point. For simplicity and consistency, this is defined to be
// (box.x + width - 1, box.y + height - 1).
//
// (The previous implementation was non-linear: with the example
// 100x100 box, it would return an input point of (99.9,99.9)
// unchanged, but for an input point (100.1,100.1) the returned
// point would jump back to (99.0,99.0). This is now fixed.)
// find the closest x point
if (x < box->x) {
*dest_x = box->x;
util: fix non-linear behavior of wlr_box_closest_point() Per comments in util/box.h, the width and height of a wlr_box are exclusive; that is, for a 100x100 box at (0,0), the point (99,99) is inside it while the point (100,100) is outside it. Thus mathematically, there exists no single closest point to the bottom-right corner of the box while remaining inside it. You can construct an infinite series approaching the limit, such as {(99,99), (99.9,99.9), (99.99,99.99)...}, but since the intervals are half-open, there is no "last" point. wlr_box_closest_point() must therefore define an arbitrary "closest" point. For points below and to the right of the box, the current implementation returns (box.x + width - 1, box.y + height - 1). Let's continue to do this. However, the current implementation is non-linear: with the example 100x100 box, it will return an input point of (99.9,99.9) unchanged, but for an input point (100.1,100.1) the returned point will jump back to (99.0,99.0). In practice, this non-linearity results in strange behaviors when driving the mouse cursor to a screen corner. On a 1920x1080 display for example, driving the cursor quickly to the bottom-left corner results in a position of exactly (0,1079). Continuing to slowly nudge the cursor downward results in the position jumping between (0,1079) and other, fractional coordinates such as (0,1079.88). The fractional coordinates expose some client/toolkit-side bugs (which, to be clear, should be fixed on the client side), but IMHO the wlroots behavior is also inconsistent and wrong -- when I drive the mouse cursor into the corner of the screen, it should come to a stop at a fixed position, not jitter around.
2024-03-20 04:54:24 +01:00
} else if (x > box->x + box->width - 1) {
*dest_x = box->x + box->width - 1;
} else {
*dest_x = x;
}
// find closest y point
if (y < box->y) {
*dest_y = box->y;
util: fix non-linear behavior of wlr_box_closest_point() Per comments in util/box.h, the width and height of a wlr_box are exclusive; that is, for a 100x100 box at (0,0), the point (99,99) is inside it while the point (100,100) is outside it. Thus mathematically, there exists no single closest point to the bottom-right corner of the box while remaining inside it. You can construct an infinite series approaching the limit, such as {(99,99), (99.9,99.9), (99.99,99.99)...}, but since the intervals are half-open, there is no "last" point. wlr_box_closest_point() must therefore define an arbitrary "closest" point. For points below and to the right of the box, the current implementation returns (box.x + width - 1, box.y + height - 1). Let's continue to do this. However, the current implementation is non-linear: with the example 100x100 box, it will return an input point of (99.9,99.9) unchanged, but for an input point (100.1,100.1) the returned point will jump back to (99.0,99.0). In practice, this non-linearity results in strange behaviors when driving the mouse cursor to a screen corner. On a 1920x1080 display for example, driving the cursor quickly to the bottom-left corner results in a position of exactly (0,1079). Continuing to slowly nudge the cursor downward results in the position jumping between (0,1079) and other, fractional coordinates such as (0,1079.88). The fractional coordinates expose some client/toolkit-side bugs (which, to be clear, should be fixed on the client side), but IMHO the wlroots behavior is also inconsistent and wrong -- when I drive the mouse cursor into the corner of the screen, it should come to a stop at a fixed position, not jitter around.
2024-03-20 04:54:24 +01:00
} else if (y > box->y + box->height - 1) {
*dest_y = box->y + box->height - 1;
} else {
*dest_y = y;
}
}
2017-12-31 12:49:06 +01:00
bool wlr_box_empty(const struct wlr_box *box) {
return box == NULL || box->width <= 0 || box->height <= 0;
}
bool wlr_box_intersection(struct wlr_box *dest, const struct wlr_box *box_a,
const struct wlr_box *box_b) {
bool a_empty = wlr_box_empty(box_a);
bool b_empty = wlr_box_empty(box_b);
if (a_empty || b_empty) {
*dest = (struct wlr_box){0};
return false;
}
int x1 = fmax(box_a->x, box_b->x);
int y1 = fmax(box_a->y, box_b->y);
2018-06-06 09:47:04 +02:00
int x2 = fmin(box_a->x + box_a->width, box_b->x + box_b->width);
int y2 = fmin(box_a->y + box_a->height, box_b->y + box_b->height);
dest->x = x1;
dest->y = y1;
dest->width = x2 - x1;
dest->height = y2 - y1;
return !wlr_box_empty(dest);
}
2017-12-31 12:49:06 +01:00
bool wlr_box_contains_point(const struct wlr_box *box, double x, double y) {
if (wlr_box_empty(box)) {
return false;
} else {
2018-05-29 15:05:01 +02:00
return x >= box->x && x < box->x + box->width &&
y >= box->y && y < box->y + box->height;
}
}
void wlr_box_transform(struct wlr_box *dest, const struct wlr_box *box,
enum wl_output_transform transform, int width, int height) {
struct wlr_box src = {0};
if (box != NULL) {
src = *box;
}
if (transform % 2 == 0) {
dest->width = src.width;
dest->height = src.height;
} else {
dest->width = src.height;
dest->height = src.width;
}
switch (transform) {
case WL_OUTPUT_TRANSFORM_NORMAL:
dest->x = src.x;
dest->y = src.y;
break;
case WL_OUTPUT_TRANSFORM_90:
dest->x = height - src.y - src.height;
dest->y = src.x;
break;
case WL_OUTPUT_TRANSFORM_180:
dest->x = width - src.x - src.width;
dest->y = height - src.y - src.height;
break;
case WL_OUTPUT_TRANSFORM_270:
dest->x = src.y;
dest->y = width - src.x - src.width;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED:
dest->x = width - src.x - src.width;
dest->y = src.y;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
dest->x = src.y;
dest->y = src.x;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
dest->x = src.x;
dest->y = height - src.y - src.height;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
dest->x = height - src.y - src.height;
dest->y = width - src.x - src.width;
break;
}
}
bool wlr_fbox_empty(const struct wlr_fbox *box) {
return box == NULL || box->width <= 0 || box->height <= 0;
}
void wlr_fbox_transform(struct wlr_fbox *dest, const struct wlr_fbox *box,
enum wl_output_transform transform, double width, double height) {
struct wlr_fbox src = {0};
if (box != NULL) {
src = *box;
}
if (transform % 2 == 0) {
dest->width = src.width;
dest->height = src.height;
} else {
dest->width = src.height;
dest->height = src.width;
}
switch (transform) {
case WL_OUTPUT_TRANSFORM_NORMAL:
dest->x = src.x;
dest->y = src.y;
break;
case WL_OUTPUT_TRANSFORM_90:
dest->x = height - src.y - src.height;
dest->y = src.x;
break;
case WL_OUTPUT_TRANSFORM_180:
dest->x = width - src.x - src.width;
dest->y = height - src.y - src.height;
break;
case WL_OUTPUT_TRANSFORM_270:
dest->x = src.y;
dest->y = width - src.x - src.width;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED:
dest->x = width - src.x - src.width;
dest->y = src.y;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
dest->x = src.y;
dest->y = src.x;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
dest->x = src.x;
dest->y = height - src.y - src.height;
break;
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
dest->x = height - src.y - src.height;
dest->y = width - src.x - src.width;
break;
}
}
2022-08-15 12:14:28 +02:00
#ifdef WLR_USE_UNSTABLE
bool wlr_box_equal(const struct wlr_box *a, const struct wlr_box *b) {
if (wlr_box_empty(a)) {
a = NULL;
}
if (wlr_box_empty(b)) {
b = NULL;
}
2023-05-20 23:12:36 +02:00
if (a == NULL || b == NULL) {
return a == b;
}
2022-08-15 12:14:28 +02:00
return a->x == b->x && a->y == b->y &&
a->width == b->width && a->height == b->height;
}
2022-08-15 12:04:42 +02:00
bool wlr_fbox_equal(const struct wlr_fbox *a, const struct wlr_fbox *b) {
if (wlr_fbox_empty(a)) {
a = NULL;
}
if (wlr_fbox_empty(b)) {
b = NULL;
}
2023-05-20 23:12:36 +02:00
if (a == NULL || b == NULL) {
return a == b;
}
2022-08-15 12:04:42 +02:00
return a->x == b->x && a->y == b->y &&
a->width == b->width && a->height == b->height;
}
2022-08-15 12:14:28 +02:00
#endif