2017-08-16 15:51:22 +00:00
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include <wlr/types/wlr_output.h>
|
|
|
|
#include <wlr/types/wlr_output_layout.h>
|
2017-08-29 16:08:49 +00:00
|
|
|
#include <wlr/types/wlr_box.h>
|
2017-08-24 14:11:57 +00:00
|
|
|
#include <limits.h>
|
2017-08-29 16:32:17 +00:00
|
|
|
#include <float.h>
|
2017-08-16 15:51:22 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2017-08-27 21:35:12 +00:00
|
|
|
struct wlr_output_layout_state {
|
2017-08-29 16:08:49 +00:00
|
|
|
struct wlr_box *_box;
|
2017-08-27 21:35:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct wlr_output_layout_output_state {
|
2017-08-29 16:08:49 +00:00
|
|
|
struct wlr_box *_box;
|
2017-08-27 21:35:12 +00:00
|
|
|
};
|
|
|
|
|
2017-08-16 15:51:22 +00:00
|
|
|
struct wlr_output_layout *wlr_output_layout_init() {
|
2017-08-29 13:59:03 +00:00
|
|
|
struct wlr_output_layout *layout =
|
|
|
|
calloc(1, sizeof(struct wlr_output_layout));
|
2017-08-27 21:35:12 +00:00
|
|
|
layout->state = calloc(1, sizeof(struct wlr_output_layout_state));
|
2017-08-29 16:08:49 +00:00
|
|
|
layout->state->_box = calloc(1, sizeof(struct wlr_box));
|
2017-08-16 15:51:22 +00:00
|
|
|
wl_list_init(&layout->outputs);
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2017-08-27 21:35:12 +00:00
|
|
|
static void wlr_output_layout_output_destroy(
|
|
|
|
struct wlr_output_layout_output *l_output) {
|
2017-08-29 13:59:03 +00:00
|
|
|
wl_list_remove(&l_output->link);
|
2017-08-29 16:08:49 +00:00
|
|
|
free(l_output->state->_box);
|
2017-08-29 13:59:03 +00:00
|
|
|
free(l_output->state);
|
|
|
|
free(l_output);
|
2017-08-27 21:35:12 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 15:51:22 +00:00
|
|
|
void wlr_output_layout_destroy(struct wlr_output_layout *layout) {
|
|
|
|
if (!layout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_output_layout_output *_output, *temp = NULL;
|
|
|
|
wl_list_for_each_safe(_output, temp, &layout->outputs, link) {
|
2017-08-27 21:35:12 +00:00
|
|
|
wlr_output_layout_output_destroy(_output);
|
2017-08-16 15:51:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 16:08:49 +00:00
|
|
|
free(layout->state->_box);
|
2017-08-27 21:35:12 +00:00
|
|
|
free(layout->state);
|
2017-08-16 15:51:22 +00:00
|
|
|
free(layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_output_layout_add(struct wlr_output_layout *layout,
|
|
|
|
struct wlr_output *output, int x, int y) {
|
2017-08-27 21:35:12 +00:00
|
|
|
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));
|
2017-08-29 16:08:49 +00:00
|
|
|
l_output->state->_box = calloc(1, sizeof(struct wlr_box));
|
2017-08-27 21:35:12 +00:00
|
|
|
l_output->output = output;
|
|
|
|
l_output->x = x;
|
|
|
|
l_output->y = y;
|
|
|
|
wl_list_insert(&layout->outputs, &l_output->link);
|
2017-08-16 15:51:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 19:00:15 +00:00
|
|
|
struct wlr_output_layout_output *wlr_output_layout_get(
|
2017-08-16 15:51:22 +00:00
|
|
|
struct wlr_output_layout *layout, struct wlr_output *reference) {
|
2017-08-29 13:59:03 +00:00
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
|
|
|
if (l_output->output == reference) {
|
|
|
|
return l_output;
|
2017-08-16 15:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-18 01:04:05 +00:00
|
|
|
return NULL;
|
2017-08-16 15:51:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 14:12:36 +00:00
|
|
|
static bool output_contains_point( struct wlr_output_layout_output *l_output,
|
|
|
|
int x, int y, int width, int height) {
|
|
|
|
return x >= l_output->x && x <= l_output->x + width &&
|
|
|
|
y >= l_output->y && y <= l_output->y + height;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wlr_output_layout_contains_point(struct wlr_output_layout *layout,
|
|
|
|
struct wlr_output *reference, int x, int y) {
|
2017-08-28 00:10:46 +00:00
|
|
|
if (reference) {
|
2017-08-29 13:59:03 +00:00
|
|
|
struct wlr_output_layout_output *layout_output =
|
|
|
|
wlr_output_layout_get(layout, reference);
|
2017-08-28 00:10:46 +00:00
|
|
|
int width, height;
|
|
|
|
wlr_output_effective_resolution(layout_output->output, &width, &height);
|
|
|
|
return output_contains_point(layout_output, x, y, width, height);
|
|
|
|
} else {
|
|
|
|
return !!wlr_output_layout_output_at(layout, x, y);
|
|
|
|
}
|
2017-08-17 14:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool wlr_output_layout_intersects(struct wlr_output_layout *layout,
|
|
|
|
struct wlr_output *reference, int x1, int y1, int x2, int y2) {
|
2017-08-29 13:59:03 +00:00
|
|
|
struct wlr_output_layout_output *l_output =
|
|
|
|
wlr_output_layout_get(layout, reference);
|
2017-08-17 14:12:36 +00:00
|
|
|
if (!l_output) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int width, height;
|
|
|
|
wlr_output_effective_resolution(l_output->output, &width, &height);
|
|
|
|
|
|
|
|
// the output must contain one of the points
|
|
|
|
return output_contains_point(l_output, x1, y1, width, height) ||
|
|
|
|
output_contains_point(l_output, x2, y2, width, height) ||
|
|
|
|
output_contains_point(l_output, x2, y1, width, height) ||
|
|
|
|
output_contains_point(l_output, y2, x1, width, height);
|
|
|
|
}
|
|
|
|
|
2017-08-16 15:51:22 +00:00
|
|
|
struct wlr_output *wlr_output_layout_output_at(struct wlr_output_layout *layout,
|
|
|
|
double x, double y) {
|
2017-08-29 13:59:03 +00:00
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
|
|
|
if (l_output->output) {
|
2017-08-16 15:51:22 +00:00
|
|
|
int width, height;
|
2017-08-29 13:59:03 +00:00
|
|
|
wlr_output_effective_resolution(l_output->output, &width, &height);
|
|
|
|
bool has_x = x >= l_output->x && x <= l_output->x + width;
|
|
|
|
bool has_y = y >= l_output->y && y <= l_output->y + height;
|
2017-08-16 15:51:22 +00:00
|
|
|
if (has_x && has_y) {
|
2017-08-29 13:59:03 +00:00
|
|
|
return l_output->output;
|
2017-08-16 15:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 01:04:05 +00:00
|
|
|
return NULL;
|
2017-08-16 15:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_output_layout_move(struct wlr_output_layout *layout,
|
|
|
|
struct wlr_output *output, int x, int y) {
|
2017-08-29 13:59:03 +00:00
|
|
|
struct wlr_output_layout_output *l_output =
|
|
|
|
wlr_output_layout_get(layout, output);
|
|
|
|
if (l_output) {
|
|
|
|
l_output->x = x;
|
|
|
|
l_output->y = y;
|
2017-08-16 15:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_output_layout_remove(struct wlr_output_layout *layout,
|
|
|
|
struct wlr_output *output) {
|
2017-08-27 21:35:12 +00:00
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
l_output= wlr_output_layout_get(layout, output);
|
|
|
|
if (l_output) {
|
|
|
|
wlr_output_layout_output_destroy(l_output);
|
2017-08-16 15:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_output_layout_output_coords(struct wlr_output_layout *layout,
|
2017-08-24 15:46:40 +00:00
|
|
|
struct wlr_output *reference, double *x, double *y) {
|
2017-08-16 15:51:22 +00:00
|
|
|
assert(layout && reference);
|
2017-08-24 15:46:40 +00:00
|
|
|
double src_x = *x;
|
|
|
|
double src_y = *y;
|
2017-08-16 15:51:22 +00:00
|
|
|
|
2017-08-29 13:59:03 +00:00
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
|
|
|
if (l_output->output == reference) {
|
|
|
|
*x = src_x - (double)l_output->x;
|
|
|
|
*y = src_y - (double)l_output->y;
|
2017-08-16 15:51:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-24 14:11:57 +00:00
|
|
|
|
2017-08-29 16:32:17 +00:00
|
|
|
static struct wlr_box *wlr_output_layout_output_get_box(
|
|
|
|
struct wlr_output_layout_output *l_output) {
|
|
|
|
l_output->state->_box->x = l_output->x;
|
|
|
|
l_output->state->_box->y = l_output->y;
|
|
|
|
wlr_output_effective_resolution(l_output->output,
|
|
|
|
&l_output->state->_box->width, &l_output->state->_box->height);
|
|
|
|
return l_output->state->_box;
|
2017-08-24 14:11:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 16:32:17 +00:00
|
|
|
void wlr_output_layout_closest_point(struct wlr_output_layout *layout,
|
2017-08-24 16:30:34 +00:00
|
|
|
struct wlr_output *reference, double x, double y, double *dest_x,
|
|
|
|
double *dest_y) {
|
2017-08-29 16:32:17 +00:00
|
|
|
double min_x = DBL_MAX, min_y = DBL_MAX, min_distance = DBL_MAX;
|
2017-08-24 14:11:57 +00:00
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
2017-08-24 16:30:34 +00:00
|
|
|
if (reference != NULL && reference != l_output->output) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
double output_x, output_y, output_distance;
|
2017-08-29 16:32:17 +00:00
|
|
|
struct wlr_box *box = wlr_output_layout_output_get_box(l_output);
|
|
|
|
wlr_box_closest_point(box, x, y, &output_x, &output_y);
|
2017-08-24 14:11:57 +00:00
|
|
|
|
2017-08-29 16:32:17 +00:00
|
|
|
// calculate squared distance suitable for comparison
|
|
|
|
output_distance =
|
|
|
|
(x - output_x) * (x - output_x) + (y - output_y) * (y - output_y);
|
2017-08-24 14:11:57 +00:00
|
|
|
|
|
|
|
if (output_distance < min_distance) {
|
|
|
|
min_x = output_x;
|
|
|
|
min_y = output_y;
|
|
|
|
min_distance = output_distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest_x = min_x;
|
|
|
|
*dest_y = min_y;
|
|
|
|
}
|
2017-08-27 21:35:12 +00:00
|
|
|
|
2017-08-29 16:08:49 +00:00
|
|
|
struct wlr_box *wlr_output_layout_get_box(
|
2017-08-27 21:35:12 +00:00
|
|
|
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);
|
2017-08-29 16:32:17 +00:00
|
|
|
return wlr_output_layout_output_get_box(l_output);
|
2017-08-27 21:35:12 +00:00
|
|
|
} else {
|
|
|
|
// layout extents
|
|
|
|
int min_x = INT_MAX, min_y = INT_MAX;
|
|
|
|
int max_x = INT_MIN, max_y = INT_MIN;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
|
|
|
int width, height;
|
|
|
|
wlr_output_effective_resolution(l_output->output, &width, &height);
|
|
|
|
if (l_output->x < min_x) {
|
|
|
|
min_x = l_output->x;
|
|
|
|
}
|
|
|
|
if (l_output->y < min_y) {
|
|
|
|
min_y = l_output->y;
|
|
|
|
}
|
|
|
|
if (l_output->x + width > max_x) {
|
|
|
|
max_x = l_output->x + width;
|
|
|
|
}
|
|
|
|
if (l_output->y + height > max_y) {
|
|
|
|
max_y = l_output->y + height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:08:49 +00:00
|
|
|
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;
|
2017-08-27 21:35:12 +00:00
|
|
|
|
2017-08-29 16:08:49 +00:00
|
|
|
return layout->state->_box;
|
2017-08-27 21:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// not reached
|
|
|
|
}
|