2018-02-12 21:29:23 +01:00
|
|
|
#include <assert.h>
|
2017-08-29 18:32:17 +02:00
|
|
|
#include <float.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <limits.h>
|
2017-08-16 17:51:22 +02:00
|
|
|
#include <stdlib.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <wlr/types/wlr_output_layout.h>
|
|
|
|
#include <wlr/types/wlr_output.h>
|
2021-07-01 22:36:01 +02:00
|
|
|
#include <wlr/util/box.h>
|
2018-02-12 09:12:31 +01:00
|
|
|
#include <wlr/util/log.h>
|
2017-08-16 17:51:22 +02:00
|
|
|
|
2021-08-09 19:19:17 +02:00
|
|
|
static const struct wlr_addon_interface addon_impl;
|
|
|
|
|
2018-05-31 02:11:57 +02:00
|
|
|
struct wlr_output_layout *wlr_output_layout_create(void) {
|
2017-08-29 15:59:03 +02:00
|
|
|
struct wlr_output_layout *layout =
|
|
|
|
calloc(1, sizeof(struct wlr_output_layout));
|
2017-10-31 11:29:53 +01:00
|
|
|
if (layout == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-16 17:51:22 +02:00
|
|
|
wl_list_init(&layout->outputs);
|
2017-09-05 13:48:28 +02:00
|
|
|
|
2017-10-29 11:20:11 +01:00
|
|
|
wl_signal_init(&layout->events.add);
|
2017-09-05 13:48:28 +02:00
|
|
|
wl_signal_init(&layout->events.change);
|
|
|
|
wl_signal_init(&layout->events.destroy);
|
|
|
|
|
2017-08-16 17:51:22 +02:00
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2018-04-26 00:51:00 +02:00
|
|
|
static void output_layout_output_destroy(
|
2017-08-27 23:35:12 +02:00
|
|
|
struct wlr_output_layout_output *l_output) {
|
2022-08-18 13:16:16 +02:00
|
|
|
wl_signal_emit_mutable(&l_output->events.destroy, l_output);
|
2018-03-01 17:53:15 +01:00
|
|
|
wlr_output_destroy_global(l_output->output);
|
2022-04-29 17:23:35 +02:00
|
|
|
wl_list_remove(&l_output->commit.link);
|
2017-08-29 15:59:03 +02:00
|
|
|
wl_list_remove(&l_output->link);
|
2021-08-09 19:19:17 +02:00
|
|
|
wlr_addon_finish(&l_output->addon);
|
2017-08-29 15:59:03 +02:00
|
|
|
free(l_output);
|
2017-08-27 23:35:12 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 17:51:22 +02:00
|
|
|
void wlr_output_layout_destroy(struct wlr_output_layout *layout) {
|
|
|
|
if (!layout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-18 13:16:16 +02:00
|
|
|
wl_signal_emit_mutable(&layout->events.destroy, layout);
|
2017-09-05 13:48:28 +02:00
|
|
|
|
2018-03-01 17:53:15 +01:00
|
|
|
struct wlr_output_layout_output *l_output, *temp;
|
2017-10-31 12:01:49 +01:00
|
|
|
wl_list_for_each_safe(l_output, temp, &layout->outputs, link) {
|
2018-04-26 00:51:00 +02:00
|
|
|
output_layout_output_destroy(l_output);
|
2017-08-16 17:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(layout);
|
|
|
|
}
|
|
|
|
|
2022-01-29 21:06:09 +01:00
|
|
|
static void output_layout_output_get_box(
|
|
|
|
struct wlr_output_layout_output *l_output,
|
|
|
|
struct wlr_box *box) {
|
|
|
|
box->x = l_output->x;
|
|
|
|
box->y = l_output->y;
|
|
|
|
wlr_output_effective_resolution(l_output->output,
|
|
|
|
&box->width, &box->height);
|
2017-08-30 16:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This must be called whenever the layout changes to reconfigure the auto
|
2017-09-05 13:48:28 +02:00
|
|
|
* configured outputs and emit the `changed` event.
|
2017-08-30 16:39:22 +02:00
|
|
|
*
|
|
|
|
* Auto configured outputs are placed to the right of the north east corner of
|
|
|
|
* the rightmost output in the layout in a horizontal line.
|
|
|
|
*/
|
2018-04-26 00:51:00 +02:00
|
|
|
static void output_layout_reconfigure(struct wlr_output_layout *layout) {
|
2017-08-30 16:39:22 +02:00
|
|
|
int max_x = INT_MIN;
|
|
|
|
int max_x_y = INT_MIN; // y value for the max_x output
|
|
|
|
|
|
|
|
// find the rightmost x coordinate occupied by a manually configured output
|
|
|
|
// in the layout
|
|
|
|
struct wlr_output_layout_output *l_output;
|
2022-01-29 21:06:09 +01:00
|
|
|
struct wlr_box output_box;
|
|
|
|
|
2017-08-30 16:39:22 +02:00
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
2022-04-29 17:23:35 +02:00
|
|
|
if (l_output->auto_configured) {
|
2017-08-30 16:39:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-29 21:06:09 +01:00
|
|
|
output_layout_output_get_box(l_output, &output_box);
|
|
|
|
if (output_box.x + output_box.width > max_x) {
|
|
|
|
max_x = output_box.x + output_box.width;
|
|
|
|
max_x_y = output_box.y;
|
2017-08-30 16:39:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_x == INT_MIN) {
|
|
|
|
// there are no manually configured outputs
|
|
|
|
max_x = 0;
|
|
|
|
max_x_y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
2022-04-29 17:23:35 +02:00
|
|
|
if (!l_output->auto_configured) {
|
2017-08-30 16:39:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-29 21:06:09 +01:00
|
|
|
output_layout_output_get_box(l_output, &output_box);
|
2017-08-30 16:39:22 +02:00
|
|
|
l_output->x = max_x;
|
|
|
|
l_output->y = max_x_y;
|
2022-01-29 21:06:09 +01:00
|
|
|
max_x += output_box.width;
|
2017-08-30 16:39:22 +02:00
|
|
|
}
|
2017-09-05 13:48:28 +02:00
|
|
|
|
2022-08-18 13:16:16 +02:00
|
|
|
wl_signal_emit_mutable(&layout->events.change, layout);
|
2017-08-30 16:39:22 +02:00
|
|
|
}
|
|
|
|
|
2018-09-17 15:22:33 +02:00
|
|
|
static void output_update_global(struct wlr_output *output) {
|
|
|
|
// Don't expose the output if it doesn't have a current mode
|
|
|
|
if (wl_list_empty(&output->modes) || output->current_mode != NULL) {
|
|
|
|
wlr_output_create_global(output);
|
|
|
|
} else {
|
|
|
|
wlr_output_destroy_global(output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 16:55:14 +01:00
|
|
|
static void handle_output_commit(struct wl_listener *listener, void *data) {
|
2022-04-29 17:23:35 +02:00
|
|
|
struct wlr_output_layout_output *l_output =
|
|
|
|
wl_container_of(listener, l_output, commit);
|
2021-01-10 16:55:14 +01:00
|
|
|
struct wlr_output_event_commit *event = data;
|
2017-12-12 21:58:00 +01:00
|
|
|
|
2022-12-01 10:13:02 +01:00
|
|
|
if (event->committed & (WLR_OUTPUT_STATE_SCALE | WLR_OUTPUT_STATE_TRANSFORM |
|
|
|
|
WLR_OUTPUT_STATE_MODE)) {
|
2022-04-29 17:23:35 +02:00
|
|
|
output_layout_reconfigure(l_output->layout);
|
|
|
|
output_update_global(l_output->output);
|
2021-01-10 16:55:14 +01:00
|
|
|
}
|
2017-12-12 21:58:00 +01:00
|
|
|
}
|
|
|
|
|
2021-08-09 19:19:17 +02:00
|
|
|
static void addon_destroy(struct wlr_addon *addon) {
|
|
|
|
assert(addon->impl == &addon_impl);
|
|
|
|
struct wlr_output_layout_output *l_output =
|
|
|
|
wl_container_of(addon, l_output, addon);
|
2022-04-29 17:23:35 +02:00
|
|
|
struct wlr_output_layout *layout = l_output->layout;
|
2021-08-09 19:19:17 +02:00
|
|
|
output_layout_output_destroy(l_output);
|
2018-04-26 00:51:00 +02:00
|
|
|
output_layout_reconfigure(layout);
|
2017-08-30 19:28:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 19:19:17 +02:00
|
|
|
static const struct wlr_addon_interface addon_impl = {
|
|
|
|
.name = "wlr_output_layout_output",
|
|
|
|
.destroy = addon_destroy,
|
|
|
|
};
|
|
|
|
|
2018-04-26 00:51:00 +02:00
|
|
|
static struct wlr_output_layout_output *output_layout_output_create(
|
2017-08-30 16:39:22 +02:00
|
|
|
struct wlr_output_layout *layout, struct wlr_output *output) {
|
2017-10-31 11:29:53 +01:00
|
|
|
struct wlr_output_layout_output *l_output =
|
|
|
|
calloc(1, sizeof(struct wlr_output_layout_output));
|
|
|
|
if (l_output == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-04-29 17:23:35 +02:00
|
|
|
l_output->layout = layout;
|
2017-08-27 23:35:12 +02:00
|
|
|
l_output->output = output;
|
2017-10-29 11:20:11 +01:00
|
|
|
wl_signal_init(&l_output->events.destroy);
|
2021-11-29 14:12:45 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert at the end of the list so that auto-configuring the
|
|
|
|
* new output doesn't change the layout of other outputs
|
|
|
|
*/
|
|
|
|
wl_list_insert(layout->outputs.prev, &l_output->link);
|
2017-08-30 16:39:22 +02:00
|
|
|
|
2022-04-29 17:23:35 +02:00
|
|
|
wl_signal_add(&output->events.commit, &l_output->commit);
|
|
|
|
l_output->commit.notify = handle_output_commit;
|
2021-08-09 19:19:17 +02:00
|
|
|
|
|
|
|
wlr_addon_init(&l_output->addon, &output->addons, layout, &addon_impl);
|
2017-08-30 19:28:50 +02:00
|
|
|
|
2017-08-30 16:39:22 +02:00
|
|
|
return l_output;
|
|
|
|
}
|
|
|
|
|
2022-08-30 18:05:38 +02:00
|
|
|
static bool output_layout_add(struct wlr_output_layout *layout,
|
|
|
|
struct wlr_output *output, int lx, int ly,
|
|
|
|
bool auto_configured) {
|
2017-08-30 16:39:22 +02:00
|
|
|
struct wlr_output_layout_output *l_output =
|
|
|
|
wlr_output_layout_get(layout, output);
|
2018-10-19 14:50:35 +02:00
|
|
|
bool is_new = l_output == NULL;
|
2022-08-30 18:05:38 +02:00
|
|
|
if (is_new) {
|
2018-04-26 00:51:00 +02:00
|
|
|
l_output = output_layout_output_create(layout, output);
|
2022-08-30 18:05:38 +02:00
|
|
|
if (l_output == NULL) {
|
|
|
|
return false;
|
2017-10-31 11:29:53 +01:00
|
|
|
}
|
2017-08-30 16:39:22 +02:00
|
|
|
}
|
2018-10-19 14:50:35 +02:00
|
|
|
|
2018-04-01 06:17:27 +02:00
|
|
|
l_output->x = lx;
|
|
|
|
l_output->y = ly;
|
2022-08-30 18:05:38 +02:00
|
|
|
l_output->auto_configured = auto_configured;
|
|
|
|
|
2018-04-26 00:51:00 +02:00
|
|
|
output_layout_reconfigure(layout);
|
2018-09-17 15:22:33 +02:00
|
|
|
output_update_global(output);
|
2018-10-19 14:50:35 +02:00
|
|
|
|
|
|
|
if (is_new) {
|
2022-08-18 13:16:16 +02:00
|
|
|
wl_signal_emit_mutable(&layout->events.add, l_output);
|
2018-10-19 14:50:35 +02:00
|
|
|
}
|
2022-08-30 18:05:38 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wlr_output_layout_add(struct wlr_output_layout *layout,
|
|
|
|
struct wlr_output *output, int lx, int ly) {
|
|
|
|
return output_layout_add(layout, output, lx, ly, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wlr_output_layout_add_auto(struct wlr_output_layout *layout,
|
|
|
|
struct wlr_output *output) {
|
|
|
|
return output_layout_add(layout, output, 0, 0, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_output_layout_remove(struct wlr_output_layout *layout,
|
|
|
|
struct wlr_output *output) {
|
|
|
|
struct wlr_output_layout_output *l_output =
|
|
|
|
wlr_output_layout_get(layout, output);
|
|
|
|
if (l_output != NULL) {
|
|
|
|
output_layout_output_destroy(l_output);
|
|
|
|
output_layout_reconfigure(layout);
|
|
|
|
}
|
2017-08-16 17:51:22 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 21:00:15 +02:00
|
|
|
struct wlr_output_layout_output *wlr_output_layout_get(
|
2017-08-16 17:51:22 +02:00
|
|
|
struct wlr_output_layout *layout, struct wlr_output *reference) {
|
2021-08-09 19:19:17 +02:00
|
|
|
struct wlr_output_layout_output *l_output = NULL;
|
|
|
|
struct wlr_addon *addon =
|
2021-08-11 12:35:20 +02:00
|
|
|
wlr_addon_find(&reference->addons, layout, &addon_impl);
|
2021-08-09 19:19:17 +02:00
|
|
|
if (addon) {
|
|
|
|
l_output = wl_container_of(addon, l_output, addon);
|
2017-08-16 17:51:22 +02:00
|
|
|
}
|
2021-08-09 19:19:17 +02:00
|
|
|
return l_output;
|
2017-08-16 17:51:22 +02:00
|
|
|
}
|
|
|
|
|
2017-08-17 16:12:36 +02:00
|
|
|
bool wlr_output_layout_contains_point(struct wlr_output_layout *layout,
|
2018-04-01 06:17:27 +02:00
|
|
|
struct wlr_output *reference, int lx, int ly) {
|
2017-08-28 02:10:46 +02:00
|
|
|
if (reference) {
|
2017-08-30 17:11:31 +02:00
|
|
|
struct wlr_output_layout_output *l_output =
|
2017-08-29 15:59:03 +02:00
|
|
|
wlr_output_layout_get(layout, reference);
|
2022-03-05 08:00:39 +01:00
|
|
|
if (!l_output) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-29 21:06:09 +01:00
|
|
|
struct wlr_box output_box;
|
|
|
|
output_layout_output_get_box(l_output, &output_box);
|
|
|
|
return wlr_box_contains_point(&output_box, lx, ly);
|
2017-08-28 02:10:46 +02:00
|
|
|
} else {
|
2018-04-01 06:17:27 +02:00
|
|
|
return !!wlr_output_layout_output_at(layout, lx, ly);
|
2017-08-28 02:10:46 +02:00
|
|
|
}
|
2017-08-17 16:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool wlr_output_layout_intersects(struct wlr_output_layout *layout,
|
2018-04-01 06:17:27 +02:00
|
|
|
struct wlr_output *reference, const struct wlr_box *target_lbox) {
|
2017-12-31 12:49:06 +01:00
|
|
|
struct wlr_box out_box;
|
|
|
|
|
|
|
|
if (reference == NULL) {
|
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
2022-01-29 21:06:09 +01:00
|
|
|
struct wlr_box output_box;
|
|
|
|
output_layout_output_get_box(l_output, &output_box);
|
|
|
|
if (wlr_box_intersection(&out_box, &output_box, target_lbox)) {
|
2017-12-31 12:49:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 16:12:36 +02:00
|
|
|
return false;
|
2017-12-31 12:49:06 +01:00
|
|
|
} else {
|
|
|
|
struct wlr_output_layout_output *l_output =
|
|
|
|
wlr_output_layout_get(layout, reference);
|
|
|
|
if (!l_output) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-10 20:05:10 +02:00
|
|
|
|
2022-01-29 21:06:09 +01:00
|
|
|
struct wlr_box output_box;
|
|
|
|
output_layout_output_get_box(l_output, &output_box);
|
|
|
|
return wlr_box_intersection(&out_box, &output_box, target_lbox);
|
2017-12-31 12:49:06 +01:00
|
|
|
}
|
2017-08-17 16:12:36 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 17:51:22 +02:00
|
|
|
struct wlr_output *wlr_output_layout_output_at(struct wlr_output_layout *layout,
|
2018-04-01 06:17:27 +02:00
|
|
|
double lx, double ly) {
|
2017-08-29 15:59:03 +02:00
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
2022-01-29 21:06:09 +01:00
|
|
|
struct wlr_box output_box;
|
|
|
|
output_layout_output_get_box(l_output, &output_box);
|
|
|
|
if (wlr_box_contains_point(&output_box, lx, ly)) {
|
2017-12-31 12:49:06 +01:00
|
|
|
return l_output->output;
|
2017-08-16 17:51:22 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-18 03:04:05 +02:00
|
|
|
return NULL;
|
2017-08-16 17:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_output_layout_output_coords(struct wlr_output_layout *layout,
|
2018-04-01 06:17:27 +02:00
|
|
|
struct wlr_output *reference, double *lx, double *ly) {
|
2017-08-16 17:51:22 +02:00
|
|
|
assert(layout && reference);
|
2018-04-01 06:17:27 +02:00
|
|
|
double src_x = *lx;
|
|
|
|
double src_y = *ly;
|
2017-08-16 17:51:22 +02:00
|
|
|
|
2017-08-29 15:59:03 +02:00
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
|
|
|
if (l_output->output == reference) {
|
2018-04-01 06:17:27 +02:00
|
|
|
*lx = src_x - (double)l_output->x;
|
|
|
|
*ly = src_y - (double)l_output->y;
|
2017-08-16 17:51:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-24 16:11:57 +02:00
|
|
|
|
2017-08-29 18:32:17 +02:00
|
|
|
void wlr_output_layout_closest_point(struct wlr_output_layout *layout,
|
2018-04-01 06:17:27 +02:00
|
|
|
struct wlr_output *reference, double lx, double ly, double *dest_lx,
|
|
|
|
double *dest_ly) {
|
|
|
|
if (dest_lx == NULL && dest_ly == NULL) {
|
2018-03-29 00:40:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-21 12:02:27 +01:00
|
|
|
double min_x = lx, min_y = ly, min_distance = DBL_MAX;
|
2017-08-24 16:11:57 +02:00
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
2017-08-24 18:30:34 +02:00
|
|
|
if (reference != NULL && reference != l_output->output) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
double output_x, output_y, output_distance;
|
2022-01-29 21:06:09 +01:00
|
|
|
struct wlr_box output_box;
|
|
|
|
output_layout_output_get_box(l_output, &output_box);
|
|
|
|
wlr_box_closest_point(&output_box, lx, ly, &output_x, &output_y);
|
2017-08-24 16:11:57 +02:00
|
|
|
|
2017-08-29 18:32:17 +02:00
|
|
|
// calculate squared distance suitable for comparison
|
|
|
|
output_distance =
|
2018-04-01 06:17:27 +02:00
|
|
|
(lx - output_x) * (lx - output_x) + (ly - output_y) * (ly - output_y);
|
2017-08-24 16:11:57 +02:00
|
|
|
|
2018-02-01 20:29:03 +01:00
|
|
|
if (!isfinite(output_distance)) {
|
|
|
|
output_distance = DBL_MAX;
|
|
|
|
}
|
|
|
|
|
2019-10-11 21:45:06 +02:00
|
|
|
if (output_distance < min_distance) {
|
2017-08-24 16:11:57 +02:00
|
|
|
min_x = output_x;
|
|
|
|
min_y = output_y;
|
|
|
|
min_distance = output_distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 06:17:27 +02:00
|
|
|
if (dest_lx) {
|
|
|
|
*dest_lx = min_x;
|
2018-03-27 05:48:32 +02:00
|
|
|
}
|
2018-04-01 06:17:27 +02:00
|
|
|
if (dest_ly) {
|
|
|
|
*dest_ly = min_y;
|
2018-03-27 05:48:32 +02:00
|
|
|
}
|
2017-08-24 16:11:57 +02:00
|
|
|
}
|
2017-08-27 23:35:12 +02:00
|
|
|
|
2022-01-29 21:06:09 +01:00
|
|
|
void wlr_output_layout_get_box(struct wlr_output_layout *layout,
|
|
|
|
struct wlr_output *reference, struct wlr_box *dest_box) {
|
2022-08-30 18:19:47 +02:00
|
|
|
memset(dest_box, 0, sizeof(*dest_box));
|
|
|
|
|
2017-08-27 23:35:12 +02:00
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
if (reference) {
|
|
|
|
// output extents
|
2017-08-30 17:11:31 +02:00
|
|
|
l_output = wlr_output_layout_get(layout, reference);
|
|
|
|
|
|
|
|
if (l_output) {
|
2022-01-29 21:06:09 +01:00
|
|
|
output_layout_output_get_box(l_output, dest_box);
|
2017-08-30 17:11:31 +02:00
|
|
|
}
|
2017-08-27 23:35:12 +02:00
|
|
|
} else {
|
|
|
|
// layout extents
|
2019-03-08 07:30:51 +01:00
|
|
|
int min_x = 0, max_x = 0, min_y = 0, max_y = 0;
|
|
|
|
if (!wl_list_empty(&layout->outputs)) {
|
|
|
|
min_x = min_y = INT_MAX;
|
|
|
|
max_x = max_y = INT_MIN;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
2022-01-29 21:06:09 +01:00
|
|
|
struct wlr_box output_box;
|
|
|
|
output_layout_output_get_box(l_output, &output_box);
|
|
|
|
if (output_box.x < min_x) {
|
|
|
|
min_x = output_box.x;
|
2019-03-08 07:30:51 +01:00
|
|
|
}
|
2022-01-29 21:06:09 +01:00
|
|
|
if (output_box.y < min_y) {
|
|
|
|
min_y = output_box.y;
|
2019-03-08 07:30:51 +01:00
|
|
|
}
|
2022-01-29 21:06:09 +01:00
|
|
|
if (output_box.x + output_box.width > max_x) {
|
|
|
|
max_x = output_box.x + output_box.width;
|
2019-03-08 07:30:51 +01:00
|
|
|
}
|
2022-01-29 21:06:09 +01:00
|
|
|
if (output_box.y + output_box.height > max_y) {
|
|
|
|
max_y = output_box.y + output_box.height;
|
2019-03-08 07:30:51 +01:00
|
|
|
}
|
2017-08-27 23:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 21:06:09 +01:00
|
|
|
dest_box->x = min_x;
|
|
|
|
dest_box->y = min_y;
|
|
|
|
dest_box->width = max_x - min_x;
|
|
|
|
dest_box->height = max_y - min_y;
|
2017-08-27 23:35:12 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-30 16:39:22 +02:00
|
|
|
|
2017-10-16 20:35:16 +02:00
|
|
|
struct wlr_output *wlr_output_layout_get_center_output(
|
|
|
|
struct wlr_output_layout *layout) {
|
|
|
|
if (wl_list_empty(&layout->outputs)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-01-29 21:06:09 +01:00
|
|
|
struct wlr_box extents;
|
|
|
|
wlr_output_layout_get_box(layout, NULL, &extents);
|
|
|
|
double center_x = extents.width / 2. + extents.x;
|
|
|
|
double center_y = extents.height / 2. + extents.y;
|
2017-10-16 20:35:16 +02:00
|
|
|
|
|
|
|
double dest_x = 0, dest_y = 0;
|
|
|
|
wlr_output_layout_closest_point(layout, NULL, center_x, center_y,
|
|
|
|
&dest_x, &dest_y);
|
|
|
|
|
|
|
|
return wlr_output_layout_output_at(layout, dest_x, dest_y);
|
|
|
|
}
|
2018-02-14 20:10:31 +01:00
|
|
|
|
2018-08-25 09:14:55 +02:00
|
|
|
enum distance_selection_method {
|
|
|
|
NEAREST,
|
|
|
|
FARTHEST
|
|
|
|
};
|
2018-02-14 20:10:31 +01:00
|
|
|
|
2019-11-20 00:45:19 +01:00
|
|
|
static struct wlr_output *wlr_output_layout_output_in_direction(
|
2018-02-14 20:10:31 +01:00
|
|
|
struct wlr_output_layout *layout, enum wlr_direction direction,
|
2018-09-17 15:22:33 +02:00
|
|
|
struct wlr_output *reference, double ref_lx, double ref_ly,
|
2018-08-25 09:14:55 +02:00
|
|
|
enum distance_selection_method distance_method) {
|
2018-02-14 20:10:31 +01:00
|
|
|
assert(reference);
|
|
|
|
|
2022-01-29 21:06:09 +01:00
|
|
|
struct wlr_box ref_box;
|
|
|
|
wlr_output_layout_get_box(layout, reference, &ref_box);
|
2022-08-30 18:22:33 +02:00
|
|
|
if (wlr_box_empty(&ref_box)) {
|
|
|
|
// The output doesn't belong to the layout
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-14 20:10:31 +01:00
|
|
|
|
2018-08-25 09:14:55 +02:00
|
|
|
double min_distance = (distance_method == NEAREST) ? DBL_MAX : DBL_MIN;
|
2018-02-14 20:10:31 +01:00
|
|
|
struct wlr_output *closest_output = NULL;
|
|
|
|
struct wlr_output_layout_output *l_output;
|
|
|
|
wl_list_for_each(l_output, &layout->outputs, link) {
|
|
|
|
if (reference != NULL && reference == l_output->output) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-29 21:06:09 +01:00
|
|
|
|
|
|
|
struct wlr_box box;
|
|
|
|
output_layout_output_get_box(l_output, &box);
|
2018-02-14 20:10:31 +01:00
|
|
|
|
|
|
|
bool match = false;
|
|
|
|
// test to make sure this output is in the given direction
|
2018-02-17 23:54:57 +01:00
|
|
|
if (direction & WLR_DIRECTION_LEFT) {
|
2022-01-29 21:06:09 +01:00
|
|
|
match = box.x + box.width <= ref_box.x || match;
|
2018-02-14 20:10:31 +01:00
|
|
|
}
|
2018-02-17 23:54:57 +01:00
|
|
|
if (direction & WLR_DIRECTION_RIGHT) {
|
2022-01-29 21:06:09 +01:00
|
|
|
match = box.x >= ref_box.x + ref_box.width || match;
|
2018-02-14 20:10:31 +01:00
|
|
|
}
|
2018-02-17 23:54:57 +01:00
|
|
|
if (direction & WLR_DIRECTION_UP) {
|
2022-01-29 21:06:09 +01:00
|
|
|
match = box.y + box.height <= ref_box.y || match;
|
2018-02-14 20:10:31 +01:00
|
|
|
}
|
2018-02-17 23:54:57 +01:00
|
|
|
if (direction & WLR_DIRECTION_DOWN) {
|
2022-01-29 21:06:09 +01:00
|
|
|
match = box.y >= ref_box.y + ref_box.height || match;
|
2018-02-14 20:10:31 +01:00
|
|
|
}
|
|
|
|
if (!match) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate distance from the given reference point
|
|
|
|
double x, y;
|
|
|
|
wlr_output_layout_closest_point(layout, l_output->output,
|
2018-04-01 06:17:27 +02:00
|
|
|
ref_lx, ref_ly, &x, &y);
|
2018-02-14 20:10:31 +01:00
|
|
|
double distance =
|
2018-04-01 06:17:27 +02:00
|
|
|
(x - ref_lx) * (x - ref_lx) + (y - ref_ly) * (y - ref_ly);
|
2018-08-25 09:14:55 +02:00
|
|
|
|
|
|
|
if ((distance_method == NEAREST)
|
|
|
|
? distance < min_distance
|
|
|
|
: distance > min_distance) {
|
2018-02-14 20:10:31 +01:00
|
|
|
min_distance = distance;
|
|
|
|
closest_output = l_output->output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return closest_output;
|
|
|
|
}
|
2018-08-25 09:14:55 +02:00
|
|
|
|
|
|
|
struct wlr_output *wlr_output_layout_adjacent_output(
|
|
|
|
struct wlr_output_layout *layout, enum wlr_direction direction,
|
|
|
|
struct wlr_output *reference, double ref_lx, double ref_ly) {
|
2018-09-17 15:22:33 +02:00
|
|
|
return wlr_output_layout_output_in_direction(layout, direction,
|
2018-08-25 09:14:55 +02:00
|
|
|
reference, ref_lx, ref_ly, NEAREST);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_output *wlr_output_layout_farthest_output(
|
|
|
|
struct wlr_output_layout *layout, enum wlr_direction direction,
|
|
|
|
struct wlr_output *reference, double ref_lx, double ref_ly) {
|
2018-09-17 15:22:33 +02:00
|
|
|
return wlr_output_layout_output_in_direction(layout, direction,
|
2018-08-25 09:14:55 +02:00
|
|
|
reference, ref_lx, ref_ly, FARTHEST);
|
|
|
|
}
|