From 9b65d0b3f0030bf2103cd7d65448f727c62de468 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Tue, 29 Aug 2017 12:08:49 -0400 Subject: [PATCH] refactor and rename wlr_geometry to wlr_box --- examples/config.c | 34 +++++------ examples/config.h | 4 +- examples/pointer.c | 4 +- include/wlr/types/wlr_box.h | 20 +++++++ include/wlr/types/wlr_cursor.h | 6 +- include/wlr/types/wlr_geometry.h | 20 ------- include/wlr/types/wlr_output_layout.h | 6 +- types/meson.build | 2 +- types/wlr_box.c | 67 +++++++++++++++++++++ types/wlr_cursor.c | 52 ++++++++-------- types/wlr_geometry.c | 86 --------------------------- types/wlr_output_layout.c | 36 +++++------ 12 files changed, 159 insertions(+), 178 deletions(-) create mode 100644 include/wlr/types/wlr_box.h delete mode 100644 include/wlr/types/wlr_geometry.h create mode 100644 types/wlr_box.c delete mode 100644 types/wlr_geometry.c diff --git a/examples/config.c b/examples/config.c index b99f130d..a1ed5d5a 100644 --- a/examples/config.c +++ b/examples/config.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include "shared.h" #include "config.h" #include "ini.h" @@ -25,7 +25,7 @@ static void usage(const char *name, int ret) { exit(ret); } -static struct wlr_geometry *parse_geometry(const char *str) { +static struct wlr_box *parse_geometry(const char *str) { // format: {width}x{height}+{x}+{y} if (strlen(str) > 255) { wlr_log(L_ERROR, "cannot parse geometry string, too long"); @@ -33,7 +33,7 @@ static struct wlr_geometry *parse_geometry(const char *str) { } char *buf = strdup(str); - struct wlr_geometry *geo = calloc(1, sizeof(struct wlr_geometry)); + struct wlr_box *box = calloc(1, sizeof(struct wlr_box)); bool has_width = false; bool has_height = false; @@ -56,16 +56,16 @@ static struct wlr_geometry *parse_geometry(const char *str) { } if (!has_width) { - geo->width = val; + box->width = val; has_width = true; } else if (!has_height) { - geo->height = val; + box->height = val; has_height = true; } else if (!has_x) { - geo->x = val; + box->x = val; has_x = true; } else if (!has_y) { - geo->y = val; + box->y = val; has_y = true; } else { break; @@ -78,12 +78,12 @@ static struct wlr_geometry *parse_geometry(const char *str) { } free(buf); - return geo; + return box; invalid_input: wlr_log(L_ERROR, "could not parse geometry string: %s", str); free(buf); - free(geo); + free(box); return NULL; } @@ -140,8 +140,8 @@ static int config_ini_handler(void *user, const char *section, const char *name, free(config->cursor.mapped_output); config->cursor.mapped_output = strdup(value); } else if (strcmp(name, "geometry") == 0) { - free(config->cursor.mapped_geo); - config->cursor.mapped_geo = parse_geometry(value); + free(config->cursor.mapped_box); + config->cursor.mapped_box = parse_geometry(value); } else { wlr_log(L_ERROR, "got unknown cursor config: %s", name); } @@ -167,8 +167,8 @@ static int config_ini_handler(void *user, const char *section, const char *name, free(dc->mapped_output); dc->mapped_output = strdup(value); } else if (strcmp(name, "geometry") == 0) { - free(dc->mapped_geo); - dc->mapped_geo = parse_geometry(value); + free(dc->mapped_box); + dc->mapped_box = parse_geometry(value); } else { wlr_log(L_ERROR, "got unknown device config: %s", name); } @@ -239,8 +239,8 @@ void example_config_destroy(struct example_config *config) { if (dc->mapped_output) { free(dc->mapped_output); } - if (dc->mapped_geo) { - free(dc->mapped_geo); + if (dc->mapped_box) { + free(dc->mapped_box); } free(dc); } @@ -251,8 +251,8 @@ void example_config_destroy(struct example_config *config) { if (config->cursor.mapped_output) { free(config->cursor.mapped_output); } - if (config->cursor.mapped_geo) { - free(config->cursor.mapped_geo); + if (config->cursor.mapped_box) { + free(config->cursor.mapped_box); } free(config); } diff --git a/examples/config.h b/examples/config.h index cd19dc5e..2a69c4f4 100644 --- a/examples/config.h +++ b/examples/config.h @@ -15,14 +15,14 @@ struct output_config { struct device_config { char *name; char *mapped_output; - struct wlr_geometry *mapped_geo; + struct wlr_box *mapped_box; struct wl_list link; }; struct example_config { struct { char *mapped_output; - struct wlr_geometry *mapped_geo; + struct wlr_box *mapped_box; } cursor; struct wl_list outputs; diff --git a/examples/pointer.c b/examples/pointer.c index 5341c72c..d9a06339 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -105,7 +105,7 @@ static void configure_devices(struct sample_state *sample) { wl_list_for_each(dc, &sample->config->devices, link) { if (strcmp(dev->device->name, dc->name) == 0) { wlr_cursor_map_input_to_region(sample->cursor, dev->device, - dc->mapped_geo); + dc->mapped_box); } } } @@ -341,7 +341,7 @@ int main(int argc, char *argv[]) { state.config = parse_args(argc, argv); state.cursor = wlr_cursor_create(); - wlr_cursor_map_to_region(state.cursor, state.config->cursor.mapped_geo); + wlr_cursor_map_to_region(state.cursor, state.config->cursor.mapped_box); wl_list_init(&state.devices); // pointer events diff --git a/include/wlr/types/wlr_box.h b/include/wlr/types/wlr_box.h new file mode 100644 index 00000000..e2b1ab4e --- /dev/null +++ b/include/wlr/types/wlr_box.h @@ -0,0 +1,20 @@ +#ifndef _WLR_TYPES_GEOMETRY_H +#define _WLR_TYPES_GEOMETRY_H +#include + +struct wlr_box { + int x, y; + int width, height; +}; + +void wlr_box_closest_point(struct wlr_box *box, double x, double y, + double *dest_x, double *dest_y); + +bool wlr_box_intersection(struct wlr_box *box_a, + struct wlr_box *box_b, struct wlr_box **dest); + +bool wlr_box_contains_point(struct wlr_box *box, double x, double y); + +bool wlr_box_empty(struct wlr_box *box); + +#endif diff --git a/include/wlr/types/wlr_cursor.h b/include/wlr/types/wlr_cursor.h index ef3b8dbb..5fc0ec76 100644 --- a/include/wlr/types/wlr_cursor.h +++ b/include/wlr/types/wlr_cursor.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include struct wlr_cursor_state; @@ -100,13 +100,13 @@ void wlr_cursor_map_input_to_output(struct wlr_cursor *cur, /** * Maps this cursor to an arbitrary region on the associated wlr_output_layout. */ -void wlr_cursor_map_to_region(struct wlr_cursor *cur, struct wlr_geometry *geo); +void wlr_cursor_map_to_region(struct wlr_cursor *cur, struct wlr_box *box); /** * Maps inputs from this input device to an arbitrary region on the associated * wlr_output_layout. */ void wlr_cursor_map_input_to_region(struct wlr_cursor *cur, - struct wlr_input_device *dev, struct wlr_geometry *geo); + struct wlr_input_device *dev, struct wlr_box *box); #endif diff --git a/include/wlr/types/wlr_geometry.h b/include/wlr/types/wlr_geometry.h deleted file mode 100644 index cac75431..00000000 --- a/include/wlr/types/wlr_geometry.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef _WLR_TYPES_GEOMETRY_H -#define _WLR_TYPES_GEOMETRY_H -#include - -struct wlr_geometry { - int x, y; - int width, height; -}; - -void wlr_geometry_closest_boundary(struct wlr_geometry *geo, double x, double y, - int *dest_x, int *dest_y, double *distance); - -bool wlr_geometry_intersection(struct wlr_geometry *geo_a, - struct wlr_geometry *geo_b, struct wlr_geometry **dest); - -bool wlr_geometry_contains_point(struct wlr_geometry *geo, int x, int y); - -bool wlr_geometry_empty(struct wlr_geometry *geo); - -#endif diff --git a/include/wlr/types/wlr_output_layout.h b/include/wlr/types/wlr_output_layout.h index dfb5234d..794fc491 100644 --- a/include/wlr/types/wlr_output_layout.h +++ b/include/wlr/types/wlr_output_layout.h @@ -62,10 +62,10 @@ void wlr_output_layout_closest_boundary(struct wlr_output_layout *layout, double *dest_y); /** - * Get the geometry of the layout for the given reference output. If `reference` - * is NULL, the geometry will be for the extents of the entire layout. + * Get the box of the layout for the given reference output. If `reference` + * is NULL, the box will be for the extents of the entire layout. */ -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); #endif diff --git a/types/meson.build b/types/meson.build index 8b8b215b..56390475 100644 --- a/types/meson.build +++ b/types/meson.build @@ -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]) diff --git a/types/wlr_box.c b/types/wlr_box.c new file mode 100644 index 00000000..7e981833 --- /dev/null +++ b/types/wlr_box.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include + +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; + } +} diff --git a/types/wlr_cursor.c b/types/wlr_cursor.c index 4bbf897b..bc546b15 100644 --- a/types/wlr_cursor.c +++ b/types/wlr_cursor.c @@ -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; } diff --git a/types/wlr_geometry.c b/types/wlr_geometry.c deleted file mode 100644 index 4f532d6f..00000000 --- a/types/wlr_geometry.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include -#include -#include -#include - -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; - } -} diff --git a/types/wlr_output_layout.c b/types/wlr_output_layout.c index b097b1b8..ba2c158a 100644 --- a/types/wlr_output_layout.c +++ b/types/wlr_output_layout.c @@ -1,24 +1,24 @@ #include #include #include -#include +#include #include #include #include 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