#include #include #include #include #include "dulcepan.h" #include "viewporter-protocol.h" #include "wlr-layer-shell-unstable-v1-protocol.h" #include "wlr-screencopy-unstable-v1-protocol.h" // Initialiation flow: // - Wait for mode+transform info // - Wait for done, compute transformed size // - Prepare surfaces, get a frame, wait for it to be ready // - Commit the main surface, wait for a configure event // - Save effective size, put the frame buffer on the main surface // - The output is initialized static void layer_surface_handle_configure(void *data, struct zwlr_layer_surface_v1 *layer_surface, uint32_t serial, uint32_t width, uint32_t height) { struct dp_output *output = data; if (!output->has_frame) { // Ignore configures that came too early return; } // Thanks layer-shell int i_width = (int)width, i_height = (int)height; zwlr_layer_surface_v1_ack_configure(layer_surface, serial); if (output->initialized) { if (i_width != output->effective_width || i_height != output->effective_height) { dp_log_fatal("Layer surface size has changed: %dx%d => %dx%d", output->effective_width, output->effective_height, i_width, i_height); } wl_surface_commit(output->main_surface); return; } output->effective_width = (int)width; output->effective_height = (int)height; wp_viewport_set_destination( output->main_viewport, output->effective_width, output->effective_height); wp_viewport_set_destination( output->select_viewport, output->effective_width, output->effective_height); if (output->y_invert) { wl_surface_set_buffer_transform(output->main_surface, WL_OUTPUT_TRANSFORM_FLIPPED_180); } wl_surface_attach(output->main_surface, output->frame_buffer, 0, 0); wl_surface_commit(output->main_surface); output->initialized = true; } static void layer_surface_handle_closed(void *data, struct zwlr_layer_surface_v1 *layer_surface) { dp_log_fatal("A layer surface was closed"); } static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { .configure = layer_surface_handle_configure, .closed = layer_surface_handle_closed, }; static void frame_handle_buffer(void *data, struct zwlr_screencopy_frame_v1 *frame, uint32_t format, uint32_t width, uint32_t height, uint32_t stride) { struct dp_output *output = data; if (output->frame_buffer != NULL) { wl_buffer_destroy(output->frame_buffer); } output->frame_format = format; output->frame_stride = (int)stride; output->frame_buffer = dp_buffer_create(output->state, output->width, output->height, output->frame_stride, output->frame_format, &output->frame_data, &output->frame_size); zwlr_screencopy_frame_v1_copy(output->frame, output->frame_buffer); } static void frame_handle_flags(void *data, struct zwlr_screencopy_frame_v1 *frame, uint32_t flags) { struct dp_output *output = data; if ((flags & ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT) != 0) { output->y_invert = true; } } static void frame_handle_ready(void *data, struct zwlr_screencopy_frame_v1 *frame, uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec) { struct dp_output *output = data; zwlr_screencopy_frame_v1_destroy(output->frame); output->frame = NULL; output->has_frame = true; wl_surface_commit(output->main_surface); } static void frame_handle_failed(void *data, struct zwlr_screencopy_frame_v1 *frame) { dp_log_fatal("Failed to copy a frame"); } static const struct zwlr_screencopy_frame_v1_listener frame_listener = { .buffer = frame_handle_buffer, .flags = frame_handle_flags, .ready = frame_handle_ready, .failed = frame_handle_failed, }; static void buffer_handle_release(void *data, struct wl_buffer *wl_buffer) { struct dp_buffer *buffer = data; buffer->used = false; } static const struct wl_buffer_listener buffer_listener = { .release = buffer_handle_release, }; static void output_handle_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, int32_t phys_width, int32_t phys_height, int32_t subpixel, const char *make, const char *model, int32_t transform) { struct dp_output *output = data; if (output->has_geom) { if (transform != output->transform) { dp_log_fatal("Output transform has changed: %d (%#x) => %d (%#x)", output->transform, output->transform, transform, transform); } } output->transform = transform; } static void output_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) { struct dp_output *output = data; if (output->has_geom) { if (width != output->width || height != output->height) { dp_log_fatal("Output mode has changed: %dx%d => %dx%d", output->width, output->height, width, height); } } output->width = width; output->height = height; } static void output_handle_done(void *data, struct wl_output *wl_output) { struct dp_output *output = data; if (output->has_geom) { return; } if ((output->transform & WL_OUTPUT_TRANSFORM_90) != 0) { output->transformed_width = output->height; output->transformed_height = output->width; } else { output->transformed_width = output->width; output->transformed_height = output->height; } output->has_geom = true; struct dp_state *state = output->state; output->frame = zwlr_screencopy_manager_v1_capture_output( state->screencopy_manager, state->show_cursors, output->wl_output); zwlr_screencopy_frame_v1_add_listener(output->frame, &frame_listener, output); output->main_surface = wl_compositor_create_surface(state->compositor); output->main_layer_surface = zwlr_layer_shell_v1_get_layer_surface(state->layer_shell, output->main_surface, output->wl_output, ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, "dulcepan"); output->main_viewport = wp_viewporter_get_viewport(state->viewporter, output->main_surface); wl_surface_set_buffer_transform(output->main_surface, output->transform); struct wl_region *empty_region = wl_compositor_create_region(state->compositor); wl_surface_set_input_region(output->main_surface, empty_region); wl_region_destroy(empty_region); zwlr_layer_surface_v1_add_listener(output->main_layer_surface, &layer_surface_listener, output); zwlr_layer_surface_v1_set_anchor(output->main_layer_surface, ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT); zwlr_layer_surface_v1_set_keyboard_interactivity( output->main_layer_surface, ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE); zwlr_layer_surface_v1_set_exclusive_zone(output->main_layer_surface, -1); output->select_surface = wl_compositor_create_surface(state->compositor); output->select_subsurface = wl_subcompositor_get_subsurface( state->subcompositor, output->select_surface, output->main_surface); output->select_viewport = wp_viewporter_get_viewport(state->viewporter, output->select_surface); wl_subsurface_set_desync(output->select_subsurface); wl_surface_set_user_data(output->select_surface, output); for (size_t i = 0; i < DP_SWAPCHAIN_LEN; i++) { struct dp_buffer *buffer = &output->swapchain[i]; int stride = output->transformed_width * 4; buffer->wl_buffer = dp_buffer_create(state, output->transformed_width, output->transformed_height, stride, WL_SHM_FORMAT_ARGB8888, &buffer->data, &buffer->size); buffer->cairo_surface = cairo_image_surface_create_for_data(buffer->data, CAIRO_FORMAT_ARGB32, output->transformed_width, output->transformed_height, stride); buffer->cairo = cairo_create(buffer->cairo_surface); wl_buffer_add_listener(buffer->wl_buffer, &buffer_listener, buffer); } } static void output_handle_scale(void *data, struct wl_output *wl_output, int32_t scale) { // Ignored } static const struct wl_output_listener output_listener = { .geometry = output_handle_geometry, .mode = output_handle_mode, .done = output_handle_done, .scale = output_handle_scale, }; static void redraw(struct dp_output *output); static void redraw_callback_handle_done( void *data, struct wl_callback *callback, uint32_t callback_data) { struct dp_output *output = data; wl_callback_destroy(output->redraw_callback); output->redraw_callback = NULL; if (output->needs_redraw) { redraw(output); } } static const struct wl_callback_listener redraw_callback_listener = { .done = redraw_callback_handle_done, }; static inline void set_cairo_color(cairo_t *cairo, float color[static 4]) { cairo_set_source_rgba(cairo, color[0], color[1], color[2], color[3]); } static void redraw(struct dp_output *output) { assert(output->redraw_callback == NULL); struct dp_buffer *buffer = NULL; for (size_t i = 0; i < DP_SWAPCHAIN_LEN; i++) { struct dp_buffer *iter = &output->swapchain[i]; if (!iter->used) { buffer = iter; break; } } if (buffer == NULL) { dp_log_error("No free buffers in a swapchain\n"); return; } buffer->used = true; struct dp_state *state = output->state; struct dp_config *config = &state->config; cairo_set_operator(buffer->cairo, CAIRO_OPERATOR_SOURCE); set_cairo_color(buffer->cairo, config->unselected_color); cairo_paint(buffer->cairo); struct dp_selection *selection = &state->selection; if (output == selection->output) { int border_size = config->border_size; if (border_size != 0) { cairo_set_line_width(buffer->cairo, border_size); double off = border_size / 2.0; cairo_rectangle(buffer->cairo, selection->x - off, selection->y - off, selection->width + border_size, selection->height + border_size); set_cairo_color(buffer->cairo, config->border_color); cairo_stroke(buffer->cairo); } cairo_rectangle( buffer->cairo, selection->x, selection->y, selection->width, selection->height); set_cairo_color(buffer->cairo, config->selected_color); cairo_fill(buffer->cairo); } wl_surface_attach(output->select_surface, buffer->wl_buffer, 0, 0); wl_surface_damage( output->select_surface, 0, 0, output->transformed_width, output->transformed_height); output->redraw_callback = wl_surface_frame(output->select_surface); wl_callback_add_listener(output->redraw_callback, &redraw_callback_listener, output); output->needs_redraw = false; wl_surface_commit(output->select_surface); } void dp_output_redraw(struct dp_output *output) { if (output->redraw_callback != NULL) { output->needs_redraw = true; return; } redraw(output); } void dp_output_hide_surface(struct dp_output *output) { wl_surface_attach(output->main_surface, NULL, 0, 0); wl_surface_commit(output->main_surface); } void dp_output_create(struct dp_state *state, uint32_t name, struct wl_output *wl_output) { struct dp_output *output = dp_zalloc(sizeof(*output)); output->state = state; output->name = name; output->wl_output = wl_output; wl_output_add_listener(output->wl_output, &output_listener, output); wl_list_insert(&state->outputs, &output->link); } void dp_output_destroy(struct dp_output *output) { wl_output_release(output->wl_output); assert(output->frame == NULL); wl_buffer_destroy(output->frame_buffer); munmap(output->frame_data, output->frame_size); if (output->redraw_callback != NULL) { wl_callback_destroy(output->redraw_callback); } for (size_t i = 0; i < DP_SWAPCHAIN_LEN; i++) { struct dp_buffer *buffer = &output->swapchain[i]; wl_buffer_destroy(buffer->wl_buffer); cairo_destroy(buffer->cairo); cairo_surface_destroy(buffer->cairo_surface); munmap(buffer->data, buffer->size); } wl_subsurface_destroy(output->select_subsurface); wp_viewport_destroy(output->select_viewport); wl_surface_destroy(output->select_surface); zwlr_layer_surface_v1_destroy(output->main_layer_surface); wp_viewport_destroy(output->main_viewport); wl_surface_destroy(output->main_surface); wl_list_remove(&output->link); free(output); }