commit 2e266a0d312462eb4a3aa506a359f3d9aa154eea
parent c7750409bb1c60b9618051bd0bb633fffdbd201a
Author: shrub <maybeshrub@gmail.com>
Date: Thu, 26 Feb 2026 21:38:29 +0000
change swcsnap protocol
a new version of swcsnap that hands off more to the client, which is
faster and makes screen recording possible. some people will tell you
this is similar functionality to the already existing screencopy
protocol. they might be right, but you should ignore them anyway.
Diffstat:
| M | Makefile | | | 21 | ++++++++++++++++++++- |
| A | extra/swcsnap.c | | | 544 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | libswc/compositor.c | | | 99 | +++++++++++++++++++++++++++++++++++++++++++++---------------------------------- |
| M | libswc/shm.c | | | 111 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- |
| M | libswc/shm.h | | | 15 | +++++++++++++++ |
| M | libswc/snap.c | | | 159 | ++++++++++++++++++++++++++++++++++++++++++++++--------------------------------- |
| M | protocol/swc_snap.xml | | | 72 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ |
7 files changed, 899 insertions(+), 122 deletions(-)
diff --git a/Makefile b/Makefile
@@ -68,6 +68,8 @@ PACKAGES+= xcb xcb-composite xcb-ewmh xcb-icccm
PKG_CFLAGS!= ${PKG_CONFIG} --cflags ${PACKAGES}
PKG_LIBS!= ${PKG_CONFIG} --libs ${PACKAGES}
+WAYLAND_CLIENT_CFLAGS!= ${PKG_CONFIG} --cflags wayland-client
+WAYLAND_CLIENT_LIBS!= ${PKG_CONFIG} --libs wayland-client
WAYLAND_PROTOCOLS_DATADIR!= ${PKG_CONFIG} --variable=pkgdatadir wayland-protocols
CPPFLAGS+= -D_GNU_SOURCE
@@ -267,6 +269,20 @@ libswc/libswc.a: libswc/libswc.o
@echo " AR $@"
@${AR} cru ${.TARGET} ${.ALLSRC}
+EXTRA_CAPTURE_TOOLS= \
+ extra/swcsnap
+
+.for ex in ${EXTRA_CAPTURE_TOOLS}
+${ex:R}.o: ${ex}.c protocol/swc_snap-client-protocol.h
+ @echo " CC $@"
+ @${CC} ${CPPFLAGS} ${CFLAGS} ${WAYLAND_CLIENT_CFLAGS} -I . -I protocol \
+ -c -o ${.TARGET} ${.IMPSRC}
+.endfor
+
+extra/swcsnap: extra/swcsnap.o protocol/swc_snap-protocol.o
+ @echo " CCLD $@"
+ @${CC} ${LDFLAGS} -o ${.TARGET} ${.ALLSRC} ${WAYLAND_CLIENT_LIBS}
+
swc.pc: swc.pc.in
@echo " GEN $@"
@sed -e 's:@VERSION@:${VERSION}:' \
@@ -281,6 +297,7 @@ swc.pc: swc.pc.in
.PHONY: all build clean install
all: build
build: libswc/libswc.a launch/swc-launch cursor/cursor_data.h swc.pc
+build: ${EXTRA_CAPTURE_TOOLS}
install: build
install -d ${DESTDIR}${BINDIR}
@@ -289,6 +306,7 @@ install: build
install -d ${DESTDIR}${DATADIR}/swc
install -d ${DESTDIR}${PKGCONFIGDIR}
install -m 4755 launch/swc-launch ${DESTDIR}${BINDIR}/
+ install -m 755 extra/swcsnap ${DESTDIR}${BINDIR}/
install -m 644 libswc/libswc.a ${DESTDIR}${LIBDIR}/
install -m 644 libswc/swc.h ${DESTDIR}${INCLUDEDIR}/
install -m 644 ${PROTO_GEN_H} ${DESTDIR}${INCLUDEDIR}/
@@ -301,4 +319,5 @@ clean:
cursor/cursor_data.h cursor/convert_font cursor/convert_font.o \
launch/*.o launch/swc-launch \
libswc/*.o libswc/libswc-internal.o libswc/libswc.o libswc/libswc.a \
- protocol/*.o
+ protocol/*.o \
+ extra/swcsnap.o extra/swcsnap
diff --git a/extra/swcsnap.c b/extra/swcsnap.c
@@ -0,0 +1,544 @@
+#define _POSIX_C_SOURCE 200809L
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <wayland-client.h>
+
+#include "swc_snap-client-protocol.h"
+
+enum { CAPTURE_FLAGS = SWC_SNAP_FLAGS_OVERLAY_CURSOR };
+
+struct shm_buffer {
+ struct wl_shm_pool *pool;
+ struct wl_buffer *buffer;
+ void *data;
+ size_t size;
+ int fd;
+ int32_t width;
+ int32_t height;
+ int32_t stride;
+};
+
+struct app {
+ struct wl_display *display;
+ struct wl_registry *registry;
+ struct wl_shm *shm;
+ struct swc_snap *snap;
+ uint32_t width;
+ uint32_t height;
+ bool have_buffer_info;
+ bool capture_ready;
+ bool capture_failed;
+ uint32_t failure_reason;
+};
+
+static void
+usage(FILE *stream, const char *argv0)
+{
+ fprintf(stream,
+ "usage:\n"
+ " %s image [output.ppm]\n"
+ " %s record [-o output.raw|-] [-n frames] [-r fps]\n",
+ argv0, argv0);
+}
+
+static ssize_t
+write_all(int fd, const void *data, size_t size)
+{
+ const uint8_t *p = data;
+ size_t written = 0;
+
+ while (written < size) {
+ ssize_t rc = write(fd, p + written, size - written);
+ if (rc < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ return -1;
+ }
+ written += (size_t)rc;
+ }
+
+ return (ssize_t)written;
+}
+
+static int
+create_shm_file(size_t size)
+{
+ char path[] = "/tmp/swcsnap-XXXXXX";
+ int fd = mkstemp(path);
+
+ if (fd < 0) {
+ return -1;
+ }
+
+ unlink(path);
+
+ if (ftruncate(fd, (off_t)size) < 0) {
+ close(fd);
+ return -1;
+ }
+
+ return fd;
+}
+
+static bool
+shm_buffer_create(struct shm_buffer *buffer, struct wl_shm *shm,
+ int32_t width, int32_t height, uint32_t format)
+{
+ size_t size;
+
+ memset(buffer, 0, sizeof(*buffer));
+ buffer->fd = -1;
+ buffer->width = width;
+ buffer->height = height;
+ buffer->stride = width * 4;
+ size = (size_t)buffer->stride * buffer->height;
+ buffer->size = size;
+
+ buffer->fd = create_shm_file(size);
+ if (buffer->fd < 0) {
+ return false;
+ }
+
+ buffer->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
+ buffer->fd, 0);
+ if (buffer->data == MAP_FAILED) {
+ close(buffer->fd);
+ buffer->fd = -1;
+ buffer->data = NULL;
+ return false;
+ }
+
+ buffer->pool = wl_shm_create_pool(shm, buffer->fd, (int32_t)size);
+ if (!buffer->pool) {
+ munmap(buffer->data, size);
+ close(buffer->fd);
+ buffer->fd = -1;
+ buffer->data = NULL;
+ return false;
+ }
+
+ buffer->buffer = wl_shm_pool_create_buffer(
+ buffer->pool, 0, width, height, buffer->stride, format);
+ if (!buffer->buffer) {
+ wl_shm_pool_destroy(buffer->pool);
+ munmap(buffer->data, size);
+ close(buffer->fd);
+ buffer->pool = NULL;
+ buffer->fd = -1;
+ buffer->data = NULL;
+ return false;
+ }
+
+ return true;
+}
+
+static void
+shm_buffer_destroy(struct shm_buffer *buffer)
+{
+ if (buffer->buffer) {
+ wl_buffer_destroy(buffer->buffer);
+ }
+ if (buffer->pool) {
+ wl_shm_pool_destroy(buffer->pool);
+ }
+ if (buffer->data) {
+ munmap(buffer->data, buffer->size);
+ }
+ if (buffer->fd >= 0) {
+ close(buffer->fd);
+ }
+ memset(buffer, 0, sizeof(*buffer));
+ buffer->fd = -1;
+}
+
+static bool
+write_ppm(const char *path, const struct shm_buffer *buffer)
+{
+ FILE *f;
+ uint8_t rgb[3];
+
+ f = fopen(path, "wb");
+ if (!f) {
+ perror("fopen");
+ return false;
+ }
+
+ if (fprintf(f, "P6\n%d %d\n255\n", buffer->width, buffer->height) < 0) {
+ fclose(f);
+ return false;
+ }
+
+ for (int32_t y = 0; y < buffer->height; y++) {
+ const uint8_t *row =
+ (const uint8_t *)buffer->data + (size_t)y * buffer->stride;
+ for (int32_t x = 0; x < buffer->width; x++) {
+ const uint8_t *px = row + (size_t)x * 4;
+
+ rgb[0] = px[2];
+ rgb[1] = px[1];
+ rgb[2] = px[0];
+ if (fwrite(rgb, 1, sizeof(rgb), f) != sizeof(rgb)) {
+ fclose(f);
+ return false;
+ }
+ }
+ }
+
+ return fclose(f) == 0;
+}
+
+static void
+handle_snap_buffer(void *data, struct swc_snap *snap, uint32_t width,
+ uint32_t height, uint32_t format)
+{
+ struct app *app = data;
+ (void)snap;
+
+ app->width = width;
+ app->height = height;
+ app->have_buffer_info = true;
+}
+
+static void
+handle_snap_ready(void *data, struct swc_snap *snap, uint32_t tv_sec,
+ uint32_t tv_nsec)
+{
+ struct app *app = data;
+ (void)snap;
+ (void)tv_sec;
+ (void)tv_nsec;
+
+ app->capture_ready = true;
+}
+
+static void
+handle_snap_failed(void *data, struct swc_snap *snap, uint32_t reason)
+{
+ struct app *app = data;
+ (void)snap;
+
+ app->capture_failed = true;
+ app->failure_reason = reason;
+}
+
+static const struct swc_snap_listener snap_listener = {
+ .buffer = handle_snap_buffer,
+ .ready = handle_snap_ready,
+ .failed = handle_snap_failed,
+};
+
+static void
+handle_global(void *data, struct wl_registry *registry, uint32_t name,
+ const char *interface, uint32_t version)
+{
+ struct app *app = data;
+
+ if (strcmp(interface, wl_shm_interface.name) == 0) {
+ app->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
+ return;
+ }
+
+ if (strcmp(interface, "swc_snap") == 0) {
+ uint32_t bind_version = version < 2 ? version : 2;
+
+ app->snap =
+ wl_registry_bind(registry, name, &swc_snap_interface, bind_version);
+ if (app->snap) {
+ swc_snap_add_listener(app->snap, &snap_listener, app);
+ }
+ }
+}
+
+static void
+handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
+{
+ (void)data;
+ (void)registry;
+ (void)name;
+}
+
+static const struct wl_registry_listener registry_listener = {
+ .global = handle_global,
+ .global_remove = handle_global_remove,
+};
+
+static bool
+app_connect(struct app *app)
+{
+ memset(app, 0, sizeof(*app));
+
+ app->display = wl_display_connect(NULL);
+ if (!app->display) {
+ fprintf(stderr, "swcsnap: failed to connect to Wayland display\n");
+ return false;
+ }
+
+ app->registry = wl_display_get_registry(app->display);
+ wl_registry_add_listener(app->registry, ®istry_listener, app);
+
+ if (wl_display_roundtrip(app->display) < 0 ||
+ wl_display_roundtrip(app->display) < 0) {
+ fprintf(stderr, "swcsnap: roundtrip failed\n");
+ return false;
+ }
+
+ if (!app->shm || !app->snap || !app->have_buffer_info) {
+ fprintf(stderr, "swcsnap: missing wl_shm/swc_snap metadata\n");
+ return false;
+ }
+
+ return true;
+}
+
+static void
+app_disconnect(struct app *app)
+{
+ if (app->snap) {
+ swc_snap_destroy(app->snap);
+ }
+ if (app->shm) {
+ wl_shm_destroy(app->shm);
+ }
+ if (app->registry) {
+ wl_registry_destroy(app->registry);
+ }
+ if (app->display) {
+ wl_display_disconnect(app->display);
+ }
+ memset(app, 0, sizeof(*app));
+}
+
+static bool
+capture_once(struct app *app, struct shm_buffer *buffer)
+{
+ app->capture_ready = false;
+ app->capture_failed = false;
+ app->failure_reason = 0;
+
+ swc_snap_capture(app->snap, buffer->buffer, CAPTURE_FLAGS);
+ wl_display_flush(app->display);
+
+ while (!app->capture_ready && !app->capture_failed) {
+ if (wl_display_dispatch(app->display) < 0) {
+ return false;
+ }
+ }
+
+ return !app->capture_failed;
+}
+
+static void
+timespec_add_ns(struct timespec *ts, long ns)
+{
+ ts->tv_nsec += ns;
+ while (ts->tv_nsec >= 1000000000L) {
+ ts->tv_nsec -= 1000000000L;
+ ts->tv_sec += 1;
+ }
+}
+
+static void
+sleep_until(const struct timespec *deadline)
+{
+ int rc;
+
+ while ((rc = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, deadline,
+ NULL)) != 0) {
+ if (rc != EINTR) {
+ return;
+ }
+ }
+}
+
+static int
+cmd_image(const char *prog, int argc, char **argv)
+{
+ struct app app;
+ struct shm_buffer buffer = {.fd = -1};
+ const char *output = "shot.ppm";
+ int ret = 1;
+
+ if (argc > 2) {
+ fprintf(stderr, "usage: %s image [output.ppm]\n", prog);
+ return 1;
+ }
+ if (argc == 2) {
+ output = argv[1];
+ }
+
+ if (!app_connect(&app)) {
+ goto out0;
+ }
+
+ if (!shm_buffer_create(&buffer, app.shm, (int32_t)app.width,
+ (int32_t)app.height, WL_SHM_FORMAT_ARGB8888)) {
+ fprintf(stderr, "swcsnap: failed to allocate shm buffer\n");
+ goto out1;
+ }
+
+ if (!capture_once(&app, &buffer)) {
+ fprintf(stderr, "swcsnap: capture failed (%u)\n", app.failure_reason);
+ goto out2;
+ }
+
+ if (!write_ppm(output, &buffer)) {
+ fprintf(stderr, "swcsnap: failed to write %s\n", output);
+ goto out2;
+ }
+
+ ret = 0;
+
+out2:
+ shm_buffer_destroy(&buffer);
+out1:
+ app_disconnect(&app);
+out0:
+ return ret;
+}
+
+static int
+cmd_record(const char *prog, int argc, char **argv)
+{
+ struct app app;
+ struct shm_buffer buffer = {.fd = -1};
+ unsigned fps = 60;
+ unsigned frames = 600;
+ const char *output_path = "-";
+ int out_fd = STDOUT_FILENO;
+ struct timespec deadline;
+ long frame_ns;
+ int ret = 1;
+ int opt;
+
+ optind = 1;
+ while ((opt = getopt(argc, argv, "o:n:r:")) != -1) {
+ switch (opt) {
+ case 'o':
+ output_path = optarg;
+ break;
+ case 'n':
+ frames = (unsigned)strtoul(optarg, NULL, 10);
+ break;
+ case 'r':
+ fps = (unsigned)strtoul(optarg, NULL, 10);
+ break;
+ default:
+ fprintf(stderr,
+ "usage: %s record [-o output.raw|-] [-n frames] [-r fps]\n",
+ prog);
+ return 1;
+ }
+ }
+
+ if (optind != argc) {
+ fprintf(stderr,
+ "usage: %s record [-o output.raw|-] [-n frames] [-r fps]\n",
+ prog);
+ return 1;
+ }
+
+ if (fps == 0) {
+ fprintf(stderr, "swcsnap: fps must be > 0\n");
+ return 1;
+ }
+
+ if (strcmp(output_path, "-") != 0) {
+ out_fd = open(output_path, O_CREAT | O_TRUNC | O_WRONLY, 0644);
+ if (out_fd < 0) {
+ perror("open");
+ return 1;
+ }
+ }
+
+ if (!app_connect(&app)) {
+ goto out0;
+ }
+
+ if (!shm_buffer_create(&buffer, app.shm, (int32_t)app.width,
+ (int32_t)app.height, WL_SHM_FORMAT_ARGB8888)) {
+ fprintf(stderr, "swcsnap: failed to allocate shm buffer\n");
+ goto out1;
+ }
+
+ fprintf(stderr, "swcsnap: rawvideo stream %ux%u bgra @ %u fps (%u frames)\n",
+ app.width, app.height, fps, frames);
+
+ frame_ns = 1000000000L / (long)fps;
+ clock_gettime(CLOCK_MONOTONIC, &deadline);
+
+ for (unsigned i = 0; i < frames;) {
+ if ((uint32_t)buffer.width != app.width ||
+ (uint32_t)buffer.height != app.height) {
+ shm_buffer_destroy(&buffer);
+ if (!shm_buffer_create(&buffer, app.shm, (int32_t)app.width,
+ (int32_t)app.height,
+ WL_SHM_FORMAT_ARGB8888)) {
+ fprintf(stderr, "swcsnap: failed to reallocate shm buffer\n");
+ goto out2;
+ }
+ fprintf(stderr, "swcsnap: resized to %ux%u\n", app.width,
+ app.height);
+ }
+
+ if (!capture_once(&app, &buffer)) {
+ if (app.capture_failed &&
+ app.failure_reason == SWC_SNAP_FAILURE_REASON_SIZE_MISMATCH) {
+ continue;
+ }
+ fprintf(stderr, "swcsnap: capture failed (%u)\n",
+ app.failure_reason);
+ goto out2;
+ }
+
+ if (write_all(out_fd, buffer.data, buffer.size) < 0) {
+ perror("write");
+ goto out2;
+ }
+
+ i++;
+ timespec_add_ns(&deadline, frame_ns);
+ sleep_until(&deadline);
+ }
+
+ ret = 0;
+
+out2:
+ shm_buffer_destroy(&buffer);
+out1:
+ app_disconnect(&app);
+out0:
+ if (out_fd != STDOUT_FILENO && out_fd >= 0) {
+ close(out_fd);
+ }
+ return ret;
+}
+
+int
+main(int argc, char **argv)
+{
+ if (argc < 2) {
+ usage(stderr, argv[0]);
+ return 1;
+ }
+
+ if (strcmp(argv[1], "image") == 0) {
+ return cmd_image(argv[0], argc - 1, argv + 1);
+ }
+ if (strcmp(argv[1], "record") == 0) {
+ return cmd_record(argv[0], argc - 1, argv + 1);
+ }
+
+ usage(stderr, argv[0]);
+ return 1;
+}
diff --git a/libswc/compositor.c b/libswc/compositor.c
@@ -334,6 +334,54 @@ repaint_view(struct target *target, struct compositor_view *view,
}
static void
+draw_overlays(struct wld_renderer *renderer, const struct swc_rectangle *target_geom)
+{
+ int32_t tx = (int32_t)target_geom->width;
+ int32_t ty = (int32_t)target_geom->height;
+
+/* draw box as 4 rectangles with wld */
+#define CLAMP_LOW(v, lo) ((v) < (lo) ? (lo) : (v))
+#define CLAMP_HIGH(v, hi) ((v) > (hi) ? (hi) : (v))
+#define DRAW_CLIPPED(rx, ry, rw, rh, clr) \
+ do { \
+ int32_t _x1 = CLAMP_LOW((rx), 0); \
+ int32_t _y1 = CLAMP_LOW((ry), 0); \
+ int32_t _x2 = CLAMP_HIGH((rx) + (int32_t)(rw), tx); \
+ int32_t _y2 = CLAMP_HIGH((ry) + (int32_t)(rh), ty); \
+ if (_x2 > _x1 && _y2 > _y1) \
+ wld_fill_rectangle(renderer, (clr), _x1, _y1, \
+ (uint32_t)(_x2 - _x1), (uint32_t)(_y2 - _y1)); \
+ } while (0)
+
+ if (overlay.active && overlay.border_width > 0) {
+ int32_t x = overlay.x - target_geom->x;
+ int32_t y = overlay.y - target_geom->y;
+ uint32_t w = overlay.width, h = overlay.height,
+ bw = overlay.border_width;
+
+ if (w > 0 && h > 0) {
+ if (bw > w) {
+ bw = w;
+ }
+ if (bw > h) {
+ bw = h;
+ }
+
+ DRAW_CLIPPED(x, y, (int32_t)w, (int32_t)bw, overlay.color);
+ DRAW_CLIPPED(x, y + (int32_t)h - (int32_t)bw, (int32_t)w,
+ (int32_t)bw, overlay.color);
+ DRAW_CLIPPED(x, y, (int32_t)bw, (int32_t)h, overlay.color);
+ DRAW_CLIPPED(x + (int32_t)w - (int32_t)bw, y, (int32_t)bw,
+ (int32_t)h, overlay.color);
+ }
+ }
+
+#undef DRAW_CLIPPED
+#undef CLAMP_HIGH
+#undef CLAMP_LOW
+}
+
+static void
renderer_repaint(struct target *target, pixman_region32_t *damage,
pixman_region32_t *base_damage, struct wl_list *views,
struct screen *screen)
@@ -369,48 +417,7 @@ renderer_repaint(struct target *target, pixman_region32_t *damage,
}
}
- if (overlay.active && overlay.border_width > 0) {
- int32_t x = overlay.x - target_geom->x;
- int32_t y = overlay.y - target_geom->y;
- uint32_t w = overlay.width, h = overlay.height,
- bw = overlay.border_width;
- int32_t tx = (int32_t)target_geom->width;
- int32_t ty = (int32_t)target_geom->height;
-
-/* draw box as 4 rectangles with wld */
-#define CLAMP_LOW(v, lo) ((v) < (lo) ? (lo) : (v))
-#define CLAMP_HIGH(v, hi) ((v) > (hi) ? (hi) : (v))
-#define DRAW_CLIPPED(rx, ry, rw, rh) \
- do { \
- int32_t _x1 = CLAMP_LOW((rx), 0); \
- int32_t _y1 = CLAMP_LOW((ry), 0); \
- int32_t _x2 = CLAMP_HIGH((rx) + (int32_t)(rw), tx); \
- int32_t _y2 = CLAMP_HIGH((ry) + (int32_t)(rh), ty); \
- if (_x2 > _x1 && _y2 > _y1) \
- wld_fill_rectangle(swc.drm->renderer, overlay.color, _x1, _y1, \
- (uint32_t)(_x2 - _x1), (uint32_t)(_y2 - _y1)); \
- } while (0)
-
- if (w > 0 && h > 0) {
- if (bw > w) {
- bw = w;
- }
- if (bw > h) {
- bw = h;
- }
-
- DRAW_CLIPPED(x, y, (int32_t)w, (int32_t)bw); /* top */
- DRAW_CLIPPED(x, y + (int32_t)h - (int32_t)bw, (int32_t)w,
- (int32_t)bw); /* bottom */
- DRAW_CLIPPED(x, y, (int32_t)bw, (int32_t)h); /* left */
- DRAW_CLIPPED(x + (int32_t)w - (int32_t)bw, y, (int32_t)bw,
- (int32_t)h); /* right */
- }
-
-#undef DRAW_CLIPPED
-#undef CLAMP_HIGH
-#undef CLAMP_LOW
- }
+ draw_overlays(swc.drm->renderer, target_geom);
wld_flush(swc.drm->renderer);
}
@@ -701,7 +708,7 @@ render_zoomed_to_shm(struct screen *screen, float zoom)
struct wld_buffer *background;
struct wld_buffer *buffer = wld_create_buffer(
- swc.shm->context, width, height, WLD_FORMAT_XRGB8888, WLD_FLAG_MAP);
+ swc.shm->context, width, height, WLD_FORMAT_ARGB8888, WLD_FLAG_MAP);
if (!buffer) {
return NULL;
}
@@ -1754,6 +1761,10 @@ compositor_get_buffer(struct screen *screen)
struct wld_buffer *
compositor_render_to_shm(struct screen *screen)
{
+ if (compositor.zoom != 1.0f) {
+ return render_zoomed_to_shm(screen, compositor.zoom);
+ }
+
uint32_t width = screen->base.geometry.width;
uint32_t height = screen->base.geometry.height;
struct wld_buffer *buffer;
@@ -1871,6 +1882,8 @@ compositor_render_to_shm(struct screen *screen)
}
}
+ draw_overlays(swc.shm->renderer, &screen->base.geometry);
+
wld_flush(swc.shm->renderer);
pixman_region32_fini(®ion);
pixman_region32_fini(&damage);
diff --git a/libswc/shm.c b/libswc/shm.c
@@ -46,6 +46,7 @@ struct pool {
void *data;
uint32_t size;
int fd;
+ bool writable;
unsigned references;
};
@@ -54,6 +55,41 @@ struct pool_reference {
struct pool *pool;
};
+struct shm_buffer_record {
+ struct wl_list link;
+ struct wl_listener destroy_listener;
+ struct wl_resource *resource;
+ struct pool *pool;
+ uint32_t offset;
+ int32_t width;
+ int32_t height;
+ int32_t stride;
+ uint32_t format;
+};
+
+static struct wl_list shm_buffer_records;
+static bool shm_buffer_records_initialized;
+
+static void
+ensure_shm_buffer_records(void)
+{
+ if (!shm_buffer_records_initialized) {
+ wl_list_init(&shm_buffer_records);
+ shm_buffer_records_initialized = true;
+ }
+}
+
+static void
+handle_shm_buffer_resource_destroy(struct wl_listener *listener, void *data)
+{
+ struct shm_buffer_record *record =
+ wl_container_of(listener, record, destroy_listener);
+ (void)data;
+
+ wl_list_remove(&record->link);
+ free(record);
+}
+
static void *
swc_mremap(struct pool *pool, void *oldp, size_t oldsize, size_t newsize)
{
@@ -121,6 +157,7 @@ create_buffer(struct wl_client *client, struct wl_resource *resource,
{
struct pool *pool = wl_resource_get_user_data(resource);
struct pool_reference *reference;
+ struct shm_buffer_record *record = NULL;
struct wld_buffer *buffer;
struct wl_resource *buffer_resource;
union wld_object object;
@@ -147,9 +184,25 @@ create_buffer(struct wl_client *client, struct wl_resource *resource,
goto error1;
}
- if (!(reference = malloc(sizeof(*reference)))) {
+ ensure_shm_buffer_records();
+ record = malloc(sizeof(*record));
+ if (!record) {
goto error2;
}
+ record->resource = buffer_resource;
+ record->pool = pool;
+ record->offset = (uint32_t)offset;
+ record->width = width;
+ record->height = height;
+ record->stride = stride;
+ record->format = format;
+ record->destroy_listener.notify = &handle_shm_buffer_resource_destroy;
+ wl_resource_add_destroy_listener(buffer_resource, &record->destroy_listener);
+ wl_list_insert(&shm_buffer_records, &record->link);
+
+ if (!(reference = malloc(sizeof(*reference)))) {
+ goto error3;
+ }
reference->pool = pool;
reference->destructor.destroy = &handle_buffer_destroy;
@@ -158,6 +211,12 @@ create_buffer(struct wl_client *client, struct wl_resource *resource,
return;
+error3:
+ if (record) {
+ wl_list_remove(&record->destroy_listener.link);
+ wl_list_remove(&record->link);
+ free(record);
+ }
error2:
wl_resource_destroy(buffer_resource);
error1:
@@ -230,11 +289,16 @@ create_pool(struct wl_client *client, struct wl_resource *resource, uint32_t id,
}
wl_resource_set_implementation(pool->resource, &shm_pool_impl, pool,
&destroy_pool_resource);
- pool->data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
+ pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ pool->writable = true;
if (pool->data == MAP_FAILED) {
- wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD,
- "mmap failed: %s", strerror(errno));
- goto error2;
+ pool->data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
+ pool->writable = false;
+ if (pool->data == MAP_FAILED) {
+ wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD,
+ "mmap failed: %s", strerror(errno));
+ goto error2;
+ }
}
/* close(fd); */
pool->size = size;
@@ -312,3 +376,40 @@ shm_destroy(struct swc_shm *shm)
wld_destroy_context(shm->context);
free(shm);
}
+
+bool
+shm_buffer_get_info(struct wl_resource *resource, struct swc_shm_buffer_info *info)
+{
+ struct shm_buffer_record *record;
+ uint64_t size;
+
+ if (!shm_buffer_records_initialized) {
+ return false;
+ }
+
+ wl_list_for_each(record, &shm_buffer_records, link)
+ {
+ if (record->resource != resource) {
+ continue;
+ }
+
+ if (record->width < 0 || record->height < 0 || record->stride < 0) {
+ return false;
+ }
+
+ size = (uint64_t)record->stride * (uint64_t)record->height;
+ if ((uint64_t)record->offset + size > record->pool->size) {
+ return false;
+ }
+
+ info->data = (uint8_t *)record->pool->data + record->offset;
+ info->width = record->width;
+ info->height = record->height;
+ info->stride = record->stride;
+ info->format = record->format;
+ info->writable = record->pool->writable;
+ return true;
+ }
+
+ return false;
+}
diff --git a/libswc/shm.h b/libswc/shm.h
@@ -24,7 +24,11 @@
#ifndef SWC_SHM_H
#define SWC_SHM_H
+#include <stdbool.h>
+#include <stdint.h>
+
struct wl_display;
+struct wl_resource;
struct swc_shm {
struct wl_global *global;
@@ -32,9 +36,20 @@ struct swc_shm {
struct wld_renderer *renderer;
};
+struct swc_shm_buffer_info {
+ void *data;
+ int32_t width;
+ int32_t height;
+ int32_t stride;
+ uint32_t format;
+ bool writable;
+};
+
struct swc_shm *
shm_create(struct wl_display *display);
void
shm_destroy(struct swc_shm *shm);
+bool
+shm_buffer_get_info(struct wl_resource *resource, struct swc_shm_buffer_info *info);
#endif
diff --git a/libswc/snap.c b/libswc/snap.c
@@ -5,62 +5,15 @@
#include "screen.h"
#include "seat.h"
#include "shm.h"
+#include "util.h"
#include "swc_snap-server-protocol.h"
#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
+#include <time.h>
#include <wayland-server.h>
#include <wld/wld.h>
-static unsigned char *ppm_rgb_buffer;
-static size_t ppm_rgb_buffer_size;
-
-static void
-ppm(int fd, const uint8_t *pixels, uint32_t width, uint32_t height,
- uint32_t pitch)
-{
- FILE *f = fdopen(fd, "wb");
- size_t rgb_size = (size_t)width * height * 3;
-
- if (!f) {
- close(fd);
- return;
- }
-
- if (rgb_size > ppm_rgb_buffer_size) {
- unsigned char *buffer = realloc(ppm_rgb_buffer, rgb_size);
- if (!buffer) {
- fclose(f);
- return;
- }
-
- ppm_rgb_buffer = buffer;
- ppm_rgb_buffer_size = rgb_size;
- }
-
- /* ppm header */
- fprintf(f, "P6\n%u %u\n255\n", width, height);
-
- /* pixel data convert argb8888 to rgb) */
- unsigned char *dst = ppm_rgb_buffer;
- for (uint32_t y = 0; y < height; y++) {
- const uint32_t *row = (const uint32_t *)(pixels + ((size_t)y * pitch));
-
- for (uint32_t x = 0; x < width; x++) {
- uint32_t pixel = row[x];
- *dst++ = (pixel >> 16) & 0xFF;
- *dst++ = (pixel >> 8) & 0xFF;
- *dst++ = pixel & 0xFF;
- }
- }
-
- fwrite(ppm_rgb_buffer, 1, rgb_size, f);
- fclose(f);
-}
-
/* get cursor */
static void
cursor(uint8_t *dst, uint32_t dst_width, uint32_t dst_height,
@@ -154,50 +107,114 @@ cursor(uint8_t *dst, uint32_t dst_width, uint32_t dst_height,
}
static void
-capture(struct wl_client *client, struct wl_resource *resource, int32_t fd)
+send_buffer_metadata(struct wl_resource *resource, uint32_t width,
+ uint32_t height)
+{
+ swc_snap_send_buffer(resource, width, height,
+ SWC_SNAP_PIXEL_FORMAT_ARGB8888);
+}
+
+static bool
+get_screen(struct screen **screen, uint32_t *width, uint32_t *height)
+{
+ struct screen *s;
+
+ if (wl_list_empty(&swc.screens)) {
+ return false;
+ }
+
+ s = wl_container_of(swc.screens.next, s, link);
+ *screen = s;
+ *width = s->base.geometry.width;
+ *height = s->base.geometry.height;
+ return true;
+}
+
+static void
+capture(struct wl_client *client, struct wl_resource *resource,
+ struct wl_resource *buffer_resource, uint32_t flags)
{
struct screen *screen;
+ struct swc_shm_buffer_info target;
struct wld_buffer *shm_buffer;
- uint8_t *pixels;
+ uint8_t *src_pixels;
+ uint8_t *dst_pixels;
uint32_t width, height;
+ uint32_t target_width, target_height, target_stride, target_format;
+ uint32_t row_bytes;
+ struct timespec ts;
+ (void)client;
- if (wl_list_empty(&swc.screens)) {
- fprintf(stderr, "snap: no screens available\n");
- close(fd);
+ if (!get_screen(&screen, &width, &height)) {
+ swc_snap_send_failed(resource, SWC_SNAP_FAILURE_REASON_NO_SCREEN);
return;
}
- screen = wl_container_of(swc.screens.next, screen, link);
- width = screen->base.geometry.width;
- height = screen->base.geometry.height;
+ send_buffer_metadata(resource, width, height);
+
+ if (!shm_buffer_get_info(buffer_resource, &target)) {
+ swc_snap_send_failed(resource,
+ SWC_SNAP_FAILURE_REASON_UNSUPPORTED_BUFFER);
+ return;
+ }
+
+ target_format = target.format;
+ if (target_format != WL_SHM_FORMAT_XRGB8888 &&
+ target_format != WL_SHM_FORMAT_ARGB8888) {
+ swc_snap_send_failed(resource,
+ SWC_SNAP_FAILURE_REASON_UNSUPPORTED_BUFFER);
+ return;
+ }
+
+ target_width = (uint32_t)target.width;
+ target_height = (uint32_t)target.height;
+ target_stride = (uint32_t)target.stride;
+ row_bytes = width * 4;
+
+ if (target_width < width || target_height < height ||
+ target_stride < row_bytes) {
+ swc_snap_send_failed(resource, SWC_SNAP_FAILURE_REASON_SIZE_MISMATCH);
+ return;
+ }
+ if (!target.writable) {
+ swc_snap_send_failed(resource, SWC_SNAP_FAILURE_REASON_UNSUPPORTED_BUFFER);
+ return;
+ }
- /* put compositor in shm*/
shm_buffer = compositor_render_to_shm(screen);
if (!shm_buffer) {
- fprintf(stderr, "snap: failed to render to SHM\n");
- close(fd);
+ swc_snap_send_failed(resource, SWC_SNAP_FAILURE_REASON_INTERNAL);
return;
}
- /* get pixel data from shm */
if (!wld_map(shm_buffer) || !shm_buffer->map) {
- fprintf(stderr, "snap: failed to map buffer data\n");
wld_buffer_unreference(shm_buffer);
- close(fd);
+ swc_snap_send_failed(resource, SWC_SNAP_FAILURE_REASON_INTERNAL);
return;
}
- pixels = shm_buffer->map;
+ src_pixels = shm_buffer->map;
+
+ if (flags & SWC_SNAP_FLAGS_OVERLAY_CURSOR) {
+ cursor(src_pixels, width, height, shm_buffer->pitch, screen);
+ }
- cursor(pixels, width, height, shm_buffer->pitch, screen);
+ dst_pixels = target.data;
- ppm(fd, pixels, width, height, shm_buffer->pitch);
+ for (uint32_t y = 0; y < height; y++) {
+ memcpy(dst_pixels + (size_t)y * target_stride,
+ src_pixels + (size_t)y * shm_buffer->pitch, row_bytes);
+ }
wld_unmap(shm_buffer);
wld_buffer_unreference(shm_buffer);
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ swc_snap_send_ready(resource, (uint32_t)ts.tv_sec, (uint32_t)ts.tv_nsec);
}
static const struct swc_snap_interface snap_impl = {
+ .destroy = destroy_resource,
.capture = capture,
};
@@ -212,10 +229,18 @@ bind_snap(struct wl_client *client, void *data, uint32_t version, uint32_t id)
return;
}
wl_resource_set_implementation(resource, &snap_impl, NULL, NULL);
+
+ if (!wl_list_empty(&swc.screens)) {
+ struct screen *screen;
+
+ screen = wl_container_of(swc.screens.next, screen, link);
+ send_buffer_metadata(resource, screen->base.geometry.width,
+ screen->base.geometry.height);
+ }
}
struct wl_global *
snap_manager_create(struct wl_display *display)
{
- return wl_global_create(display, &swc_snap_interface, 1, NULL, &bind_snap);
+ return wl_global_create(display, &swc_snap_interface, 2, NULL, &bind_snap);
}
diff --git a/protocol/swc_snap.xml b/protocol/swc_snap.xml
@@ -1,15 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="swc_snap">
- <interface name="swc_snap" version="1">
- <description summary="simple screenshot to file descriptor">
- provides a minimal screenshot capability.
+ <interface name="swc_snap" version="2">
+ <description summary="raw screen capture into client buffers">
+ gives a screen capture capability where the client supplies a
+ wl_shm buffer and the compositor fills it with a raw frame.
</description>
+ <enum name="flags" bitfield="true">
+ <entry name="overlay_cursor" value="1"
+ summary="composite the current cursor into the frame"/>
+ </enum>
+
+ <enum name="pixel_format">
+ <entry name="xrgb8888" value="0"
+ summary="WL_SHM_FORMAT_XRGB8888"/>
+ <entry name="argb8888" value="1"
+ summary="WL_SHM_FORMAT_ARGB8888"/>
+ </enum>
+
+ <enum name="failure_reason">
+ <entry name="no_screen" value="0"
+ summary="no captureable screen is available"/>
+ <entry name="unsupported_buffer" value="1"
+ summary="buffer is not a wl_shm xrgb/argb8888 buffer"/>
+ <entry name="size_mismatch" value="2"
+ summary="buffer dimensions are too small for the screen"/>
+ <entry name="internal" value="3"
+ summary="compositor capture failed"/>
+ </enum>
+
+ <request name="destroy" type="destructor">
+ <description summary="destroy the capture object"/>
+ </request>
+
<request name="capture">
- <description summary="capture screenshot to fd">
- captures the current screen and writes it as ppm formay
+ <description summary="capture a frame into a client buffer">
+ captures the current primary screen into the supplied wl_shm
+ buffer. The compositor writes raw pixels and reports ready or
+ failed state
</description>
- <arg name="fd" type="fd" summary="file descriptor to write ppm data"/>
+ <arg name="buffer" type="object" interface="wl_buffer"
+ summary="client-provided wl_shm buffer to fill"/>
+ <arg name="flags" type="uint" summary="bitmask of swc_snap.flags"/>
</request>
+
+ <event name="buffer">
+ <description summary="current capture frame metadata">
+ describes the current frame geometry and pixel format expected
+ by the capture stream
+ </description>
+ <arg name="width" type="uint"/>
+ <arg name="height" type="uint"/>
+ <arg name="format" type="uint"
+ summary="swc_snap.pixel_format value"/>
+ </event>
+
+ <event name="ready">
+ <description summary="capture complete">
+ the requested buffer has been filled and is ready for client
+ </description>
+ <arg name="tv_sec" type="uint" summary="CLOCK_MONOTONIC seconds"/>
+ <arg name="tv_nsec" type="uint" summary="CLOCK_MONOTONIC nanoseconds"/>
+ </event>
+
+ <event name="failed">
+ <description summary="capture failed">
+ the capture request failed, the object still usable and the
+ client may retry with another buffer or after a mode change
+ </description>
+ <arg name="reason" type="uint"
+ summary="swc_snap.failure_reason value"/>
+ </event>
</interface>
</protocol>