From fef41044c29b258ec7aec99750375e86e6aa15ed Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sun, 31 Oct 2021 17:45:29 +0000 Subject: [PATCH] examples: screencopy: Use zext-screencopy-unstable-v1 --- examples/meson.build | 2 +- examples/screencopy.c | 109 +++++++++++++++++++++++++++++++----------- 2 files changed, 83 insertions(+), 28 deletions(-) diff --git a/examples/meson.build b/examples/meson.build index 1e428e81b2..44c925435a 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -136,7 +136,7 @@ clients = { 'screencopy': { 'src': 'screencopy.c', 'dep': [libpng, rt], - 'proto': ['wlr-screencopy-unstable-v1'], + 'proto': ['screencopy-unstable-v1'], }, 'screencopy-dmabuf': { 'src': 'screencopy-dmabuf.c', diff --git a/examples/screencopy.c b/examples/screencopy.c index 42123e55eb..1bb64eb80a 100644 --- a/examples/screencopy.c +++ b/examples/screencopy.c @@ -36,8 +36,9 @@ #include #include #include +#include #include -#include "wlr-screencopy-unstable-v1-client-protocol.h" +#include "screencopy-unstable-v1-client-protocol.h" struct format { enum wl_shm_format wl_format; @@ -45,7 +46,7 @@ struct format { }; static struct wl_shm *shm = NULL; -static struct zwlr_screencopy_manager_v1 *screencopy_manager = NULL; +static struct zext_screencopy_manager_v1 *screencopy_manager = NULL; static struct wl_output *output = NULL; static struct { @@ -66,6 +67,15 @@ static const struct format formats[] = { {WL_SHM_FORMAT_ABGR8888, false}, }; +static enum wl_shm_format drm_format_to_wl_shm(uint32_t in) { + switch (in) { + case DRM_FORMAT_ARGB8888: return WL_SHM_FORMAT_ARGB8888; + case DRM_FORMAT_XRGB8888: return WL_SHM_FORMAT_XRGB8888; + } + + return in; +} + static struct wl_buffer *create_shm_buffer(enum wl_shm_format fmt, int width, int height, int stride, void **data_out) { int size = stride * height; @@ -105,10 +115,35 @@ static struct wl_buffer *create_shm_buffer(enum wl_shm_format fmt, return buffer; } -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) { - buffer.format = format; +static void commit_buffer(struct zext_screencopy_surface_v1 *surface) { + if (buffer.wl_buffer == NULL) { + fprintf(stderr, "no supported buffer format reported\n"); + exit(EXIT_FAILURE); + } + + zext_screencopy_surface_v1_attach_buffer(surface, buffer.wl_buffer); + zext_screencopy_surface_v1_damage_buffer(surface, 0, 0, buffer.width, + buffer.height); + + zext_screencopy_surface_v1_commit(surface, + ZEXT_SCREENCOPY_SURFACE_V1_OPTIONS_SCHEDULE_FRAME); +} + +static void surface_handle_buffer_info(void *data, + struct zext_screencopy_surface_v1 *surface, + enum zext_screencopy_surface_v1_buffer_type type, + uint32_t format, uint32_t width, uint32_t height, + uint32_t stride) { + if (type == ZEXT_SCREENCOPY_SURFACE_V1_BUFFER_TYPE_NONE) { + commit_buffer(surface); + return; + } + + if (type != ZEXT_SCREENCOPY_SURFACE_V1_BUFFER_TYPE_WL_SHM) { + return; + } + + buffer.format = drm_format_to_wl_shm(format); buffer.width = width; buffer.height = height; buffer.stride = stride; @@ -116,37 +151,57 @@ static void frame_handle_buffer(void *data, // Make sure the buffer is not allocated assert(!buffer.wl_buffer); buffer.wl_buffer = - create_shm_buffer(format, width, height, stride, &buffer.data); + create_shm_buffer(buffer.format, width, height, stride, &buffer.data); if (buffer.wl_buffer == NULL) { fprintf(stderr, "failed to create buffer\n"); exit(EXIT_FAILURE); } - - zwlr_screencopy_frame_v1_copy(frame, buffer.wl_buffer); } -static void frame_handle_flags(void *data, - struct zwlr_screencopy_frame_v1 *frame, uint32_t flags) { - buffer.y_invert = flags & ZWLR_SCREENCOPY_FRAME_V1_FLAGS_Y_INVERT; +static void surface_handle_transform(void *data, + struct zext_screencopy_surface_v1 *surface, + int32_t transform) { + if (transform == WL_OUTPUT_TRANSFORM_FLIPPED_180) { + buffer.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) { +static void surface_handle_ready(void *data, + struct zext_screencopy_surface_v1 *surface) { buffer_copy_done = true; } -static void frame_handle_failed(void *data, - struct zwlr_screencopy_frame_v1 *frame) { +static void surface_handle_failed(void *data, + struct zext_screencopy_surface_v1 *surface, + enum zext_screencopy_surface_v1_failure_reason reason) { fprintf(stderr, "failed to copy frame\n"); exit(EXIT_FAILURE); } -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 surface_handle_damage(void *data, + struct zext_screencopy_surface_v1 *surface, + uint32_t x, uint32_t y, uint32_t width, uint32_t height) { +} + +static void surface_handle_cursor_info(void *data, + struct zext_screencopy_surface_v1 *surface, + int32_t pos_x, int32_t pos_y, + int32_t hotspot_x, int32_t hotspot_y) { +} + +static void surface_handle_presentation_time(void *data, + struct zext_screencopy_surface_v1 *surface, + uint32_t sec_hi, uint32_t sec_lo, uint32_t nsec) { +} + +static const struct zext_screencopy_surface_v1_listener frame_listener = { + .buffer_info = surface_handle_buffer_info, + .damage = surface_handle_damage, + .cursor_info = surface_handle_cursor_info, + .presentation_time = surface_handle_presentation_time, + .transform = surface_handle_transform, + .ready = surface_handle_ready, + .failed = surface_handle_failed, }; static void handle_global(void *data, struct wl_registry *registry, @@ -156,9 +211,9 @@ static void handle_global(void *data, struct wl_registry *registry, } else if (strcmp(interface, wl_shm_interface.name) == 0) { shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); } else if (strcmp(interface, - zwlr_screencopy_manager_v1_interface.name) == 0) { + zext_screencopy_manager_v1_interface.name) == 0) { screencopy_manager = wl_registry_bind(registry, name, - &zwlr_screencopy_manager_v1_interface, 1); + &zext_screencopy_manager_v1_interface, 1); } } @@ -249,9 +304,9 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - struct zwlr_screencopy_frame_v1 *frame = - zwlr_screencopy_manager_v1_capture_output(screencopy_manager, 0, output); - zwlr_screencopy_frame_v1_add_listener(frame, &frame_listener, NULL); + struct zext_screencopy_surface_v1 *surface = + zext_screencopy_manager_v1_capture_output(screencopy_manager, output); + zext_screencopy_surface_v1_add_listener(surface, &frame_listener, NULL); while (!buffer_copy_done && wl_display_dispatch(display) != -1) { // This space is intentionally left blank