refactor and rename wlr_geometry to wlr_box

This commit is contained in:
Tony Crisci 2017-08-29 12:08:49 -04:00
parent a51b76083e
commit 9b65d0b3f0
12 changed files with 159 additions and 178 deletions

View file

@ -16,7 +16,7 @@ lib_wlr_types = static_library('wlr_types', files(
'wlr_xdg_shell_v6.c',
'wlr_wl_shell.c',
'wlr_compositor.c',
'wlr_geometry.c',
'wlr_box.c',
),
include_directories: wlr_inc,
dependencies: [wayland_server, pixman, wlr_protos])

67
types/wlr_box.c Normal file
View file

@ -0,0 +1,67 @@
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <wlr/types/wlr_box.h>
#include <wlr/util/log.h>
void wlr_box_closest_point(struct wlr_box *box, double x, double y,
double *dest_x, double *dest_y) {
// find the closest x point
if (x < box->x) {
*dest_x = box->x;
} else if (x > box->x + box->width) {
*dest_x = box->x + box->width;
} else {
*dest_x = x;
}
// find closest y point
if (y < box->y) {
*dest_y = box->y;
} else if (y > box->y + box->height) {
*dest_y = box->y + box->height;
} else {
*dest_y = y;
}
}
bool wlr_box_empty(struct wlr_box *box) {
return box == NULL || box->width <= 0 || box->height <= 0;
}
bool wlr_box_intersection(struct wlr_box *box_a,
struct wlr_box *box_b, struct wlr_box **box_dest) {
struct wlr_box *dest = *box_dest;
bool a_empty = wlr_box_empty(box_a);
bool b_empty = wlr_box_empty(box_b);
if (a_empty || b_empty) {
dest->x = 0;
dest->y = 0;
dest->width = -100;
dest->height = -100;
return false;
}
int x1 = fmax(box_a->x, box_b->x);
int y1 = fmax(box_a->y, box_b->y);
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);
}
bool wlr_box_contains_point(struct wlr_box *box, double x, double y) {
if (wlr_box_empty(box)) {
return false;
} else {
return x >= box->x && x <= box->x + box->width &&
y >= box->y && y <= box->y + box->height;
}
}

View file

@ -13,7 +13,7 @@ struct wlr_cursor_device {
struct wlr_input_device *device;
struct wl_list link;
struct wlr_output *mapped_output;
struct wlr_geometry *mapped_geometry;
struct wlr_box *mapped_box;
struct wl_listener motion;
struct wl_listener motion_absolute;
@ -38,7 +38,7 @@ struct wlr_cursor_state {
struct wlr_output_layout *layout;
struct wlr_xcursor *xcursor;
struct wlr_output *mapped_output;
struct wlr_geometry *mapped_geometry;
struct wlr_box *mapped_box;
};
struct wlr_cursor *wlr_cursor_create() {
@ -149,26 +149,26 @@ static void wlr_cursor_warp_unchecked(struct wlr_cursor *cur,
* If none of these are set, returns NULL and absolute movement should be
* relative to the extents of the layout.
*/
static struct wlr_geometry *get_mapping(struct wlr_cursor *cur,
static struct wlr_box *get_mapping(struct wlr_cursor *cur,
struct wlr_input_device *dev) {
assert(cur->state->layout);
struct wlr_cursor_device *c_device = get_cursor_device(cur, dev);
if (c_device) {
if (c_device->mapped_geometry) {
return c_device->mapped_geometry;
if (c_device->mapped_box) {
return c_device->mapped_box;
}
if (c_device->mapped_output) {
return wlr_output_layout_get_geometry(cur->state->layout,
return wlr_output_layout_get_box(cur->state->layout,
c_device->mapped_output);
}
}
if (cur->state->mapped_geometry) {
return cur->state->mapped_geometry;
if (cur->state->mapped_box) {
return cur->state->mapped_box;
}
if (cur->state->mapped_output) {
return wlr_output_layout_get_geometry(cur->state->layout,
return wlr_output_layout_get_box(cur->state->layout,
cur->state->mapped_output);
}
@ -180,10 +180,10 @@ bool wlr_cursor_warp(struct wlr_cursor *cur, struct wlr_input_device *dev,
assert(cur->state->layout);
bool result = false;
struct wlr_geometry *mapping = get_mapping(cur, dev);
struct wlr_box *mapping = get_mapping(cur, dev);
if (mapping) {
if (wlr_geometry_contains_point(mapping, x, y)) {
if (wlr_box_contains_point(mapping, x, y)) {
wlr_cursor_warp_unchecked(cur, x, y);
result = true;
}
@ -200,9 +200,9 @@ void wlr_cursor_warp_absolute(struct wlr_cursor *cur,
struct wlr_input_device *dev, double x_mm, double y_mm) {
assert(cur->state->layout);
struct wlr_geometry *mapping = get_mapping(cur, dev);
struct wlr_box *mapping = get_mapping(cur, dev);
if (!mapping) {
mapping = wlr_output_layout_get_geometry(cur->state->layout, NULL);
mapping = wlr_output_layout_get_box(cur->state->layout, NULL);
}
double x = mapping->width * x_mm + mapping->x;
@ -218,15 +218,15 @@ void wlr_cursor_move(struct wlr_cursor *cur, struct wlr_input_device *dev,
double x = cur->x + delta_x;
double y = cur->y + delta_y;
struct wlr_geometry *mapping = get_mapping(cur, dev);
struct wlr_box *mapping = get_mapping(cur, dev);
if (mapping) {
int boundary_x, boundary_y;
if (!wlr_geometry_contains_point(mapping, x, y)) {
wlr_geometry_closest_boundary(mapping, x, y, &boundary_x,
&boundary_y, NULL);
x = boundary_x;
y = boundary_y;
double closest_x, closest_y;
if (!wlr_box_contains_point(mapping, x, y)) {
wlr_box_closest_point(mapping, x, y, &closest_x,
&closest_y);
x = closest_x;
y = closest_y;
}
} else {
if (!wlr_output_layout_contains_point(cur->state->layout, NULL, x, y)) {
@ -452,18 +452,18 @@ void wlr_cursor_map_input_to_output(struct wlr_cursor *cur,
}
void wlr_cursor_map_to_region(struct wlr_cursor *cur,
struct wlr_geometry *geo) {
if (geo && wlr_geometry_empty(geo)) {
struct wlr_box *box) {
if (box && wlr_box_empty(box)) {
wlr_log(L_ERROR, "cannot map cursor to an empty region");
return;
}
cur->state->mapped_geometry = geo;
cur->state->mapped_box = box;
}
void wlr_cursor_map_input_to_region(struct wlr_cursor *cur,
struct wlr_input_device *dev, struct wlr_geometry *geo) {
if (geo && wlr_geometry_empty(geo)) {
struct wlr_input_device *dev, struct wlr_box *box) {
if (box && wlr_box_empty(box)) {
wlr_log(L_ERROR, "cannot map device \"%s\" input to an empty region",
dev->name);
return;
@ -476,5 +476,5 @@ void wlr_cursor_map_input_to_region(struct wlr_cursor *cur,
return;
}
c_device->mapped_geometry = geo;
c_device->mapped_box = box;
}

View file

@ -1,86 +0,0 @@
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <wlr/types/wlr_geometry.h>
#include <wlr/util/log.h>
static double get_distance(double x1, double y1, double x2, double y2) {
double distance;
distance = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return distance;
}
void wlr_geometry_closest_boundary(struct wlr_geometry *geo, double x, double y,
int *dest_x, int *dest_y, double *distance) {
// find the closest x point
if (x < geo->x) {
*dest_x = geo->x;
} else if (x > geo->x + geo->width) {
*dest_x = geo->x + geo->width;
} else {
*dest_x = x;
}
// find closest y point
if (y < geo->y) {
*dest_y = geo->y;
} else if (y > geo->y + geo->height) {
*dest_y = geo->y + geo->height;
} else {
*dest_y = y;
}
// calculate distance
if (distance) {
*distance = get_distance(*dest_x, *dest_y, x, y);
}
}
#ifndef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
bool wlr_geometry_empty(struct wlr_geometry *geo) {
return geo == NULL || geo->width <= 0 || geo->height <= 0;
}
bool wlr_geometry_intersection(struct wlr_geometry *geo_a,
struct wlr_geometry *geo_b, struct wlr_geometry **geo_dest) {
struct wlr_geometry *dest = *geo_dest;
bool a_empty = wlr_geometry_empty(geo_a);
bool b_empty = wlr_geometry_empty(geo_b);
if (a_empty || b_empty) {
dest->x = 0;
dest->y = 0;
dest->width = -100;
dest->height = -100;
return false;
}
int x1 = max(geo_a->x, geo_b->x);
int y1 = max(geo_a->y, geo_b->y);
int x2 = min(geo_a->x + geo_a->width, geo_b->x + geo_b->width);
int y2 = min(geo_a->y + geo_a->height, geo_b->y + geo_b->height);
dest->x = x1;
dest->y = y1;
dest->width = x2 - x1;
dest->height = y2 - y1;
return !wlr_geometry_empty(dest);
}
bool wlr_geometry_contains_point(struct wlr_geometry *geo, int x, int y) {
if (wlr_geometry_empty(geo)) {
return false;
} else {
return x >= geo->x && x <= geo->x + geo->width &&
y >= geo->y && y <= geo->y + geo->height;
}
}

View file

@ -1,24 +1,24 @@
#include <wlr/util/log.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_geometry.h>
#include <wlr/types/wlr_box.h>
#include <limits.h>
#include <stdlib.h>
#include <assert.h>
struct wlr_output_layout_state {
struct wlr_geometry *_geo;
struct wlr_box *_box;
};
struct wlr_output_layout_output_state {
struct wlr_geometry *_geo;
struct wlr_box *_box;
};
struct wlr_output_layout *wlr_output_layout_init() {
struct wlr_output_layout *layout =
calloc(1, sizeof(struct wlr_output_layout));
layout->state = calloc(1, sizeof(struct wlr_output_layout_state));
layout->state->_geo = calloc(1, sizeof(struct wlr_geometry));
layout->state->_box = calloc(1, sizeof(struct wlr_box));
wl_list_init(&layout->outputs);
return layout;
}
@ -26,7 +26,7 @@ struct wlr_output_layout *wlr_output_layout_init() {
static void wlr_output_layout_output_destroy(
struct wlr_output_layout_output *l_output) {
wl_list_remove(&l_output->link);
free(l_output->state->_geo);
free(l_output->state->_box);
free(l_output->state);
free(l_output);
}
@ -41,7 +41,7 @@ void wlr_output_layout_destroy(struct wlr_output_layout *layout) {
wlr_output_layout_output_destroy(_output);
}
free(layout->state->_geo);
free(layout->state->_box);
free(layout->state);
free(layout);
}
@ -51,7 +51,7 @@ void wlr_output_layout_add(struct wlr_output_layout *layout,
struct wlr_output_layout_output *l_output;
l_output= calloc(1, sizeof(struct wlr_output_layout_output));
l_output->state = calloc(1, sizeof(struct wlr_output_layout_output_state));
l_output->state->_geo = calloc(1, sizeof(struct wlr_geometry));
l_output->state->_box = calloc(1, sizeof(struct wlr_box));
l_output->output = output;
l_output->x = x;
l_output->y = y;
@ -178,7 +178,7 @@ void wlr_output_layout_closest_boundary(struct wlr_output_layout *layout,
wlr_output_effective_resolution(l_output->output, &width, &height);
// find the closest x point
// TODO use wlr_geometry_closest_boundary
// TODO use wlr_box_closest_boundary
if (x < l_output->x) {
output_x = l_output->x;
} else if (x > l_output->x + width) {
@ -209,17 +209,17 @@ void wlr_output_layout_closest_boundary(struct wlr_output_layout *layout,
*dest_y = min_y;
}
struct wlr_geometry *wlr_output_layout_get_geometry(
struct wlr_box *wlr_output_layout_get_box(
struct wlr_output_layout *layout, struct wlr_output *reference) {
struct wlr_output_layout_output *l_output;
if (reference) {
// output extents
l_output= wlr_output_layout_get(layout, reference);
l_output->state->_geo->x = l_output->x;
l_output->state->_geo->y = l_output->y;
l_output->state->_box->x = l_output->x;
l_output->state->_box->y = l_output->y;
wlr_output_effective_resolution(reference,
&l_output->state->_geo->width, &l_output->state->_geo->height);
return l_output->state->_geo;
&l_output->state->_box->width, &l_output->state->_box->height);
return l_output->state->_box;
} else {
// layout extents
int min_x = INT_MAX, min_y = INT_MAX;
@ -241,12 +241,12 @@ struct wlr_geometry *wlr_output_layout_get_geometry(
}
}
layout->state->_geo->x = min_x;
layout->state->_geo->y = min_y;
layout->state->_geo->width = max_x - min_x;
layout->state->_geo->height = max_y - min_y;
layout->state->_box->x = min_x;
layout->state->_box->y = min_y;
layout->state->_box->width = max_x - min_x;
layout->state->_box->height = max_y - min_y;
return layout->state->_geo;
return layout->state->_box;
}
// not reached