Skip to content

Commit

Permalink
examples: screencopy: Use zext-screencopy-unstable-v1
Browse files Browse the repository at this point in the history
  • Loading branch information
any1 committed Oct 31, 2021
1 parent 84bbbef commit fef4104
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
109 changes: 82 additions & 27 deletions examples/screencopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@
#include <sys/wait.h>
#include <unistd.h>
#include <assert.h>
#include <libdrm/drm_fourcc.h>
#include <wayland-client-protocol.h>
#include "wlr-screencopy-unstable-v1-client-protocol.h"
#include "screencopy-unstable-v1-client-protocol.h"

struct format {
enum wl_shm_format wl_format;
bool is_bgr;
};

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 {
Expand All @@ -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;
Expand Down Expand Up @@ -105,48 +115,93 @@ 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;

// 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,
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fef4104

Please sign in to comment.