Skip to content

Commit

Permalink
feat(screencopy): add buffer creation through screencopy
Browse files Browse the repository at this point in the history
  • Loading branch information
jtheoof committed Dec 3, 2019
1 parent 4e7623e commit bff8687
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/screencopy.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ void screencopy_frame_handle_ready(void *data,
void screencopy_frame_handle_failed(void *data,
struct zwlr_screencopy_frame_v1 *frame);
bool screencopy_init(struct swappy_state *state);
void screencopy_destroy_buffer(struct swappy_buffer *buffer);
bool screencopy_parse_geometry(struct swappy_state *state);
8 changes: 7 additions & 1 deletion include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ struct swappy_state {
char **argv;
};

struct swappy_buffer;
struct swappy_buffer {
struct wl_buffer *wl_buffer;
void *data;
int32_t width, height, stride;
size_t size;
enum wl_shm_format format;
};

struct swappy_output {
struct swappy_state *state;
Expand Down
90 changes: 88 additions & 2 deletions src/screencopy.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,104 @@
#include "screencopy.h"

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <wayland-client.h>

#include "box.h"
#include "swappy.h"
#include "wlr-screencopy-unstable-v1-client-protocol.h"

static void randname(char *buf) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
long r = ts.tv_nsec;
for (int i = 0; i < 6; ++i) {
buf[i] = 'A' + (r & 15) + (r & 16) * 2;
r >>= 5;
}
}

static int anonymous_shm_open(void) {
char name[] = "/swappy-XXXXXX";
int retries = 100;

do {
randname(name + strlen(name) - 6);

--retries;
// shm_open guarantees that O_CLOEXEC is set
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd >= 0) {
shm_unlink(name);
return fd;
}
} while (retries > 0 && errno == EEXIST);

return -1;
}

static int create_shm_file(off_t size) {
int fd = anonymous_shm_open();
if (fd < 0) {
return fd;
}

if (ftruncate(fd, size) < 0) {
close(fd);
return -1;
}

return fd;
}

static struct swappy_buffer *create_buffer(struct wl_shm *shm,
enum wl_shm_format format,
int32_t width, int32_t height,
int32_t stride) {
return NULL;
size_t size = stride * height;
g_debug("creating buffer with dimensions: %dx%d", width, height);

int fd = create_shm_file(size);
if (fd == -1) {
return NULL;
}

void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
close(fd);
return NULL;
}

struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
struct wl_buffer *wl_buffer =
wl_shm_pool_create_buffer(pool, 0, width, height, stride, format);
wl_shm_pool_destroy(pool);

close(fd);

struct swappy_buffer *buffer = calloc(1, sizeof(struct swappy_buffer));
buffer->wl_buffer = wl_buffer;
buffer->data = data;
buffer->width = width;
buffer->height = height;
buffer->stride = stride;
buffer->size = size;
buffer->format = format;

return buffer;
}

void screencopy_destroy_buffer(struct swappy_buffer *buffer) {
if (buffer == NULL) {
return;
}
munmap(buffer->data, buffer->size);
wl_buffer_destroy(buffer->wl_buffer);
free(buffer);
}

void screencopy_frame_handle_buffer(void *data,
Expand All @@ -29,7 +115,7 @@ void screencopy_frame_handle_buffer(void *data,
exit(EXIT_FAILURE);
}

// zwlr_screencopy_frame_v1_copy(frame, output->buffer->wl_buffer);
zwlr_screencopy_frame_v1_copy(frame, output->buffer->wl_buffer);
}

void screencopy_frame_handle_flags(void *data,
Expand Down
1 change: 1 addition & 0 deletions src/wayland.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ void wayland_finish(struct swappy_state *state) {
zxdg_output_v1_destroy(output->xdg_output);
}
wl_output_release(output->wl_output);
screencopy_destroy_buffer(output->buffer);
free(output);
}

Expand Down

0 comments on commit bff8687

Please sign in to comment.