diff options
Diffstat (limited to 'swaybg/main.c')
-rw-r--r-- | swaybg/main.c | 80 |
1 files changed, 61 insertions, 19 deletions
diff --git a/swaybg/main.c b/swaybg/main.c index 203082f6..c282a707 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -43,10 +43,12 @@ struct swaybg_state { struct wl_output *output; struct wl_surface *surface; + struct wl_region *input_region; struct zwlr_layer_surface_v1 *layer_surface; bool run_display; uint32_t width, height; + int32_t scale; struct pool_buffer buffers[2]; struct pool_buffer *current_buffer; }; @@ -74,52 +76,53 @@ static void render_image(struct swaybg_state *state) { cairo_surface_t *image = state->context.image; double width = cairo_image_surface_get_width(image); double height = cairo_image_surface_get_height(image); - int wwidth = state->width; - int wheight = state->height; + int buffer_width = state->width * state->scale; + int buffer_height = state->height * state->scale; switch (state->args->mode) { case BACKGROUND_MODE_STRETCH: - cairo_scale(cairo, (double)wwidth / width, (double)wheight / height); + cairo_scale(cairo, (double)buffer_width / width, + (double)buffer_height / height); cairo_set_source_surface(cairo, image, 0, 0); break; case BACKGROUND_MODE_FILL: { - double window_ratio = (double)wwidth / wheight; + double window_ratio = (double)buffer_width / buffer_height; double bg_ratio = width / height; if (window_ratio > bg_ratio) { - double scale = (double)wwidth / width; + double scale = (double)buffer_width / width; cairo_scale(cairo, scale, scale); cairo_set_source_surface(cairo, image, - 0, (double)wheight / 2 / scale - height / 2); + 0, (double)buffer_height / 2 / scale - height / 2); } else { - double scale = (double)wheight / height; + double scale = (double)buffer_height / height; cairo_scale(cairo, scale, scale); cairo_set_source_surface(cairo, image, - (double)wwidth / 2 / scale - width / 2, 0); + (double)buffer_width / 2 / scale - width / 2, 0); } break; } case BACKGROUND_MODE_FIT: { - double window_ratio = (double)wwidth / wheight; + double window_ratio = (double)buffer_width / buffer_height; double bg_ratio = width / height; if (window_ratio > bg_ratio) { - double scale = (double)wheight / height; + double scale = (double)buffer_height / height; cairo_scale(cairo, scale, scale); cairo_set_source_surface(cairo, image, - (double)wwidth / 2 / scale - width / 2, 0); + (double)buffer_width / 2 / scale - width / 2, 0); } else { - double scale = (double)wwidth / width; + double scale = (double)buffer_width / width; cairo_scale(cairo, scale, scale); cairo_set_source_surface(cairo, image, - 0, (double)wheight / 2 / scale - height / 2); + 0, (double)buffer_height / 2 / scale - height / 2); } break; } case BACKGROUND_MODE_CENTER: cairo_set_source_surface(cairo, image, - (double)wwidth / 2 - width / 2, - (double)wheight / 2 - height / 2); + (double)buffer_width / 2 - width / 2, + (double)buffer_height / 2 - height / 2); break; case BACKGROUND_MODE_TILE: { cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); @@ -135,8 +138,8 @@ static void render_image(struct swaybg_state *state) { } static void render_frame(struct swaybg_state *state) { - state->current_buffer = get_next_buffer(state->shm, - state->buffers, state->width, state->height); + state->current_buffer = get_next_buffer(state->shm, state->buffers, + state->width * state->scale, state->height * state->scale); cairo_t *cairo = state->current_buffer->cairo; switch (state->args->mode) { @@ -149,6 +152,7 @@ static void render_frame(struct swaybg_state *state) { break; } + wl_surface_set_buffer_scale(state->surface, state->scale); wl_surface_attach(state->surface, state->current_buffer->buffer, 0, 0); wl_surface_damage(state->surface, 0, 0, state->width, state->height); wl_surface_commit(state->surface); @@ -204,6 +208,7 @@ static void layer_surface_closed(void *data, struct swaybg_state *state = data; zwlr_layer_surface_v1_destroy(state->layer_surface); wl_surface_destroy(state->surface); + wl_region_destroy(state->input_region); state->run_display = false; } @@ -212,12 +217,42 @@ struct zwlr_layer_surface_v1_listener layer_surface_listener = { .closed = layer_surface_closed, }; +static void output_geometry(void *data, struct wl_output *output, int32_t x, + int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel, + const char *make, const char *model, int32_t transform) { + // Who cares +} + +static void output_mode(void *data, struct wl_output *output, uint32_t flags, + int32_t width, int32_t height, int32_t refresh) { + // Who cares +} + +static void output_done(void *data, struct wl_output *output) { + // Who cares +} + +static void output_scale(void *data, struct wl_output *output, int32_t factor) { + struct swaybg_state *state = data; + state->scale = factor; + if (state->run_display) { + render_frame(state); + } +} + +struct wl_output_listener output_listener = { + .geometry = output_geometry, + .mode = output_mode, + .done = output_done, + .scale = output_scale, +}; + static void handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { struct swaybg_state *state = data; if (strcmp(interface, wl_compositor_interface.name) == 0) { state->compositor = wl_registry_bind(registry, name, - &wl_compositor_interface, 1); + &wl_compositor_interface, 3); } else if (strcmp(interface, wl_shm_interface.name) == 0) { state->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); @@ -225,7 +260,8 @@ static void handle_global(void *data, struct wl_registry *registry, static int output_idx = 0; if (output_idx == state->args->output_idx) { state->output = wl_registry_bind(registry, name, - &wl_output_interface, 1); + &wl_output_interface, 3); + wl_output_add_listener(state->output, &output_listener, state); } output_idx++; } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { @@ -287,8 +323,14 @@ int main(int argc, const char **argv) { wl_display_roundtrip(state.display); assert(state.compositor && state.layer_shell && state.output && state.shm); + // Second roundtrip to get output properties + wl_display_roundtrip(state.display); + assert(state.surface = wl_compositor_create_surface(state.compositor)); + assert(state.input_region = wl_compositor_create_region(state.compositor)); + wl_surface_set_input_region(state.surface, state.input_region); + state.layer_surface = zwlr_layer_shell_v1_get_layer_surface( state.layer_shell, state.surface, state.output, ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND, "wallpaper"); |