From 183d348d54b7e6c0af733789a234ba915703e132 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Wed, 13 Feb 2019 11:43:34 +0100 Subject: [PATCH] use xdg-output to determine geometry when available --- include/slurp.h | 4 ++++ main.c | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/include/slurp.h b/include/slurp.h index a457c2f..a12c036 100644 --- a/include/slurp.h +++ b/include/slurp.h @@ -7,6 +7,7 @@ #include "pool-buffer.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" +#include "xdg-output-unstable-v1-client-protocol.h" struct slurp_box { int32_t x, y; @@ -21,6 +22,7 @@ struct slurp_state { struct wl_shm *shm; struct wl_compositor *compositor; struct zwlr_layer_shell_v1 *layer_shell; + struct zxdg_output_manager_v1 *xdg_output_manager; struct wl_list outputs; // slurp_output::link struct wl_list seats; // slurp_seat::link @@ -47,6 +49,8 @@ struct slurp_output { struct wl_surface *surface; struct zwlr_layer_surface_v1 *layer_surface; + struct zxdg_output_v1 *xdg_output; + struct wl_callback *frame_callback; bool configured; bool dirty; diff --git a/main.c b/main.c index 3275ce5..a47b6f2 100644 --- a/main.c +++ b/main.c @@ -223,6 +223,27 @@ static const struct wl_output_listener output_listener = { .scale = output_handle_scale, }; +static void xdg_output_handle_logical_position(void *data, + struct zxdg_output_v1 *xdg_output, int32_t x, int32_t y) { + struct slurp_output *output = data; + output->geometry.x = x; + output->geometry.y = y; +} +static void xdg_output_handle_logical_size(void *data, + struct zxdg_output_v1 *xdg_output, int32_t width, int32_t height) { + struct slurp_output *output = data; + output->geometry.width = width; + output->geometry.height = height; +} + +static const struct zxdg_output_v1_listener xdg_output_listener = { + .logical_position = xdg_output_handle_logical_position, + .logical_size = xdg_output_handle_logical_size, + .done = noop, + .name = noop, + .description = noop, +}; + static void create_output(struct slurp_state *state, struct wl_output *wl_output) { struct slurp_output *output = calloc(1, sizeof(struct slurp_output)); @@ -235,7 +256,12 @@ static void create_output(struct slurp_state *state, output->scale = 1; wl_list_insert(&state->outputs, &output->link); - wl_output_add_listener(wl_output, &output_listener, output); + if (state->xdg_output_manager) { + output->xdg_output = zxdg_output_manager_v1_get_xdg_output(state->xdg_output_manager, output->wl_output); + zxdg_output_v1_add_listener(output->xdg_output, &xdg_output_listener, output); + } else { + wl_output_add_listener(wl_output, &output_listener, output); + } } static void destroy_output(struct slurp_output *output) { @@ -247,6 +273,9 @@ static void destroy_output(struct slurp_output *output) { finish_buffer(&output->buffers[1]); wl_cursor_theme_destroy(output->cursor_theme); zwlr_layer_surface_v1_destroy(output->layer_surface); + if (output->xdg_output) { + zxdg_output_v1_destroy(output->xdg_output); + } wl_surface_destroy(output->surface); if (output->frame_callback) { wl_callback_destroy(output->frame_callback); @@ -371,6 +400,8 @@ static void handle_global(void *data, struct wl_registry *registry, struct wl_output *wl_output = wl_registry_bind(registry, name, &wl_output_interface, 3); create_output(state, wl_output); + } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) { + state->xdg_output_manager = wl_registry_bind(registry, name, &zxdg_output_manager_v1_interface, (version > 2 ? 2 : version)); } } @@ -478,6 +509,9 @@ int main(int argc, char *argv[]) { fprintf(stderr, "compositor doesn't support zwlr_layer_shell_v1\n"); return EXIT_FAILURE; } + if (state.xdg_output_manager == NULL) { + fprintf(stderr, "compositor doesn't support xdg-output. Guessing geometry from physical output size.\n"); + } if (wl_list_empty(&state.outputs)) { fprintf(stderr, "no wl_output\n"); return EXIT_FAILURE;