1
0
mirror of https://codeberg.org/vyivel/dulcepan/ synced 2026-04-26 21:14:40 +03:00

Switch to ext-image-* protocols

This commit is contained in:
Kirill Primak
2026-04-12 11:59:52 +03:00
parent 3189f7c769
commit 435de1c6b6
8 changed files with 139 additions and 292 deletions

View File

@@ -6,6 +6,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdnoreturn.h>
#include <wayland-client-protocol.h>
#include <wayland-util.h>
#include <xkbcommon/xkbcommon.h>
@@ -60,17 +61,19 @@ struct dp_output {
struct wl_callback *redraw_callback;
bool needs_redraw;
struct zwlr_screencopy_frame_v1 *frame;
bool has_frame;
// XXX: this is completely untested
bool y_invert;
struct ext_image_capture_source_v1 *source;
struct ext_image_copy_capture_session_v1 *session;
struct ext_image_copy_capture_frame_v1 *frame;
enum wl_shm_format frame_format;
bool has_frame_format;
struct wl_buffer *frame_buffer;
int32_t frame_stride;
uint32_t frame_format;
void *frame_data;
size_t frame_size;
bool captured;
// wl_buffer is NULL if uninitialized
struct dp_buffer swapchain[DP_SWAPCHAIN_LEN];
@@ -186,7 +189,9 @@ struct dp_state {
struct wp_viewporter *viewporter;
struct wl_shm *shm;
struct zwlr_layer_shell_v1 *layer_shell;
struct zwlr_screencopy_manager_v1 *screencopy_manager;
struct ext_output_image_capture_source_manager_v1 *output_image_capture_source_manager;
struct ext_image_copy_capture_manager_v1 *image_copy_capture_manager;
// Optional
struct wp_cursor_shape_manager_v1 *cursor_shape_manager;
@@ -242,6 +247,8 @@ void dp_select_notify_pointer_position(
void dp_select_toggle_whole(struct dp_selection *selection, struct dp_output *output);
bool dp_format_supported(enum wl_shm_format format);
void dp_save(struct dp_state *state);
void dp_log_error(const char *fmt, ...);

View File

@@ -8,9 +8,10 @@
#include "cursor-shape-v1-protocol.h"
#include "dulcepan.h"
#include "ext-image-capture-source-v1-protocol.h"
#include "ext-image-copy-capture-v1-protocol.h"
#include "viewporter-protocol.h"
#include "wlr-layer-shell-unstable-v1-protocol.h"
#include "wlr-screencopy-unstable-v1-protocol.h"
static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
const char *interface, uint32_t version) {
@@ -27,9 +28,12 @@ static void registry_handle_global(void *data, struct wl_registry *registry, uin
state->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
state->layer_shell = wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1);
} else if (strcmp(interface, zwlr_screencopy_manager_v1_interface.name) == 0) {
state->screencopy_manager =
wl_registry_bind(registry, name, &zwlr_screencopy_manager_v1_interface, 1);
} else if (strcmp(interface, ext_output_image_capture_source_manager_v1_interface.name) == 0) {
state->output_image_capture_source_manager = wl_registry_bind(
registry, name, &ext_output_image_capture_source_manager_v1_interface, 1);
} else if (strcmp(interface, ext_image_copy_capture_manager_v1_interface.name) == 0) {
state->image_copy_capture_manager =
wl_registry_bind(registry, name, &ext_image_copy_capture_manager_v1_interface, 1);
} else if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) {
state->cursor_shape_manager =
wl_registry_bind(registry, name, &wp_cursor_shape_manager_v1_interface, 1);
@@ -88,8 +92,10 @@ static void run(struct dp_state *state) {
dp_log_fatal("The compositor has no wl_shm");
} else if (state->layer_shell == NULL) {
dp_log_fatal("The compositor has no zwlr_layer_shell_v1");
} else if (state->screencopy_manager == NULL) {
dp_log_fatal("The compositor has no zwlr_screencopy_manager_v1");
} else if (state->output_image_capture_source_manager == NULL) {
dp_log_fatal("The compositor has no ext_output_image_capture_source_manager_v1");
} else if (state->image_copy_capture_manager == NULL) {
dp_log_fatal("The compositor has no ext_image_copy_capture_manager_v1");
}
if (wl_list_empty(&state->outputs)) {
@@ -148,7 +154,9 @@ static void run(struct dp_state *state) {
wp_viewporter_destroy(state->viewporter);
wl_shm_destroy(state->shm);
zwlr_layer_shell_v1_destroy(state->layer_shell);
zwlr_screencopy_manager_v1_destroy(state->screencopy_manager);
ext_output_image_capture_source_manager_v1_destroy(state->output_image_capture_source_manager);
ext_image_copy_capture_manager_v1_destroy(state->image_copy_capture_manager);
if (state->cursor_shape_manager) {
wp_cursor_shape_manager_v1_destroy(state->cursor_shape_manager);

View File

@@ -5,14 +5,17 @@
#include <wayland-client-protocol.h>
#include "dulcepan.h"
#include "ext-image-capture-source-v1-protocol.h"
#include "ext-image-copy-capture-v1-protocol.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
// - Wait for the output configuration, compute transformed size
// - Create a source, a session, and a frame
// - Create and prepare surfaces
// - Wait for the session information, capture the frame
// - Wait for the frame
// - Commit the main surface, wait for a configure event
// - Save effective size, put the frame buffer on the main surface
// - The output is initialized
@@ -21,7 +24,7 @@ static void layer_surface_handle_configure(void *data, struct zwlr_layer_surface
uint32_t serial, uint32_t width, uint32_t height) {
struct dp_output *output = data;
if (!output->has_frame) {
if (!output->captured) {
// Ignore configures that came too early
return;
}
@@ -51,10 +54,6 @@ static void layer_surface_handle_configure(void *data, struct zwlr_layer_surface
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);
@@ -71,54 +70,100 @@ static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
.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) {
static void session_handle_buffer_size(void *data,
struct ext_image_copy_capture_session_v1 *session, uint32_t width, uint32_t height) {
struct dp_output *output = data;
if (output->frame_buffer != NULL) {
wl_buffer_destroy(output->frame_buffer);
}
if ((int32_t)width != output->width || (int32_t)height != output->height) {
dp_log_fatal("Output/buffer size mismatch: mode=%" PRIi32 "x%" PRIi32 ", buffer=%" PRIu32
"x%" PRIu32,
output->width, output->height, width, height);
}
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) {
static void session_handle_shm_format(
void *data, struct ext_image_copy_capture_session_v1 *session, uint32_t format) {
struct dp_output *output = data;
if ((flags & ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT) != 0) {
output->y_invert = true;
if (!output->has_frame_format && dp_format_supported(format)) {
output->frame_format = format;
output->has_frame_format = 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) {
static void session_handle_dmabuf_device(
void *data, struct ext_image_copy_capture_session_v1 *session, struct wl_array *device) {
// Ignored
}
static void session_handle_dmabuf_format(void *data,
struct ext_image_copy_capture_session_v1 *session, uint32_t format,
struct wl_array *modifiers) {
// Ignored
}
static void session_handle_done(void *data, struct ext_image_copy_capture_session_v1 *session) {
struct dp_output *output = data;
if (!output->has_frame_format) {
dp_log_fatal("Output %s: no suitable SHM format", output->name);
}
output->frame_buffer = dp_buffer_create(output->state, output->width, output->height,
output->width * 4, output->frame_format, &output->frame_data, &output->frame_size);
ext_image_copy_capture_frame_v1_attach_buffer(output->frame, output->frame_buffer);
ext_image_copy_capture_frame_v1_capture(output->frame);
}
static void session_handle_stopped(void *data, struct ext_image_copy_capture_session_v1 *session) {
// Ignored
}
static const struct ext_image_copy_capture_session_v1_listener session_listener = {
.buffer_size = session_handle_buffer_size,
.shm_format = session_handle_shm_format,
.dmabuf_device = session_handle_dmabuf_device,
.dmabuf_format = session_handle_dmabuf_format,
.done = session_handle_done,
.stopped = session_handle_stopped,
};
static void frame_handle_transform(
void *data, struct ext_image_copy_capture_frame_v1 *frame, uint32_t transform) {
// Ignored
}
static void frame_handle_damage(void *data, struct ext_image_copy_capture_frame_v1 *frame,
int32_t x, int32_t y, int32_t width, int32_t height) {
// Ignored
}
static void frame_handle_presentation_time(void *data,
struct ext_image_copy_capture_frame_v1 *frame, uint32_t tv_sec_hi, uint32_t tv_sec_lo,
uint32_t tv_nsec) {
// Ignored
}
static void frame_handle_ready(void *data, struct ext_image_copy_capture_frame_v1 *frame) {
struct dp_output *output = data;
zwlr_screencopy_frame_v1_destroy(output->frame);
ext_image_copy_capture_frame_v1_destroy(output->frame);
output->frame = NULL;
output->has_frame = true;
output->captured = true;
wl_surface_commit(output->main_surface);
}
static void frame_handle_failed(void *data, struct zwlr_screencopy_frame_v1 *frame) {
static void frame_handle_failed(
void *data, struct ext_image_copy_capture_frame_v1 *frame, uint32_t reason) {
// Shouldn't happen?
struct dp_output *output = data;
dp_log_fatal("Output %s: failed to copy a frame", output->name);
dp_log_fatal("Output %s: failed to capture a frame, reason %" PRIu32, output->name, reason);
}
static const struct zwlr_screencopy_frame_v1_listener frame_listener = {
.buffer = frame_handle_buffer,
.flags = frame_handle_flags,
static const struct ext_image_copy_capture_frame_v1_listener frame_listener = {
.transform = frame_handle_transform,
.damage = frame_handle_damage,
.presentation_time = frame_handle_presentation_time,
.ready = frame_handle_ready,
.failed = frame_handle_failed,
};
@@ -201,9 +246,16 @@ static void output_handle_done(void *data, struct wl_output *wl_output) {
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->source = ext_output_image_capture_source_manager_v1_create_source(
state->output_image_capture_source_manager, output->wl_output);
output->session =
ext_image_copy_capture_manager_v1_create_session(state->image_copy_capture_manager,
output->source, EXT_IMAGE_COPY_CAPTURE_MANAGER_V1_OPTIONS_PAINT_CURSORS);
ext_image_copy_capture_session_v1_add_listener(output->session, &session_listener, output);
output->frame = ext_image_copy_capture_session_v1_create_frame(output->session);
ext_image_copy_capture_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,
@@ -459,6 +511,9 @@ void dp_output_destroy(struct dp_output *output) {
}
}
ext_image_copy_capture_session_v1_destroy(output->session);
ext_image_capture_source_v1_destroy(output->source);
wl_subsurface_destroy(output->select_subsurface);
wp_viewport_destroy(output->select_viewport);
wl_surface_destroy(output->select_surface);

View File

@@ -58,7 +58,7 @@ static pixman_format_code_t shm_to_pixman(enum wl_shm_format shm_format) {
return formats[i].pixman;
}
}
dp_log_fatal("failed to get a matching Pixman format for wl_shm format");
return 0;
}
static void write_png(FILE *fp, pixman_image_t *image, int width, int height, int level) {
@@ -106,6 +106,10 @@ static void write_ppm(FILE *fp, pixman_image_t *image, int width, int height) {
}
}
bool dp_format_supported(enum wl_shm_format format) {
return shm_to_pixman(format) != 0;
}
void dp_save(struct dp_state *state) {
struct dp_selection *selection = &state->selection;
struct dp_output *output = selection->output;
@@ -117,14 +121,14 @@ void dp_save(struct dp_state *state) {
pixman_format_code_t pixman_format = shm_to_pixman(output->frame_format);
pixman_image_t *frame_image = pixman_image_create_bits(
pixman_format, output->width, output->height, output->frame_data, output->frame_stride);
pixman_format, output->width, output->height, output->frame_data, output->width * 4);
static const pixman_fixed_t cosines[] = {pixman_fixed_1, 0, -pixman_fixed_1, 0};
pixman_fixed_t scale_x = (output->transform & WL_OUTPUT_TRANSFORM_FLIPPED) != 0
? -pixman_fixed_1
: pixman_fixed_1;
pixman_fixed_t scale_y = output->y_invert ? -pixman_fixed_1 : pixman_fixed_1;
pixman_fixed_t scale_y = pixman_fixed_1;
struct pixman_transform frame_transform;
pixman_transform_init_identity(&frame_transform);