@@ 40,6 40,25 @@ enum event_loop_fd {
EVENT_LOOP_PIPEWIRE,
};
+struct shm_buf {
+ uint32_t *data;
+ int width;
+ int height;
+ int size;
+ bool inuse;
+
+ struct wl_buffer *buffer;
+};
+
+static void wl_buffer_release(void *data, struct wl_buffer *wl_buffer) {
+ struct shm_buf *buf = data;
+ buf->inuse = false;
+}
+
+static const struct wl_buffer_listener wl_buffer_listener = {
+ .release = wl_buffer_release,
+};
+
static void randname(char *buf) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
@@ 82,29 101,8 @@ static int create_shm_file(off_t size) {
return fd;
}
-struct shm_buf {
- uint32_t *data;
- int width;
- int height;
- int size;
- bool inuse;
-
- struct wl_buffer *buffer;
-};
-
-static void wl_buffer_release(void *data, struct wl_buffer *wl_buffer) {
- struct shm_buf *buf = data;
- buf->inuse = false;
-}
-
-static const struct wl_buffer_listener wl_buffer_listener = {
- .release = wl_buffer_release,
-};
-
-static int prepare_buffer(struct shm_buf *buf, struct wl_shm *shm, int width, int height) {
- if (buf == NULL) {
- return -1;
- }
+static int shm_buf_prepare(struct shm_buf *buf, struct wl_shm *shm, int width, int height) {
+ assert(!buf->inuse);
if (buf->data != NULL && buf->width == width && buf->height == height) {
return 0;
}
@@ 139,7 137,8 @@ static int prepare_buffer(struct shm_buf *buf, struct wl_shm *shm, int width, in
return 0;
}
-static void use_buf(struct shm_buf *buf) {
+static void shm_buf_acquire(struct shm_buf *buf) {
+ assert(!buf->inuse);
buf->inuse = true;
}
@@ 148,7 147,7 @@ struct shm_bufs {
int next;
};
-static struct shm_buf *next_buf(struct shm_bufs *bufs) {
+static struct shm_buf *shm_buf_get_next_available(struct shm_bufs *bufs) {
if (!bufs->bufs[0].inuse) {
return &bufs->bufs[0];
} else if (!bufs->bufs[1].inuse) {
@@ 156,7 155,6 @@ static struct shm_buf *next_buf(struct shm_bufs *bufs) {
} else if (!bufs->bufs[2].inuse) {
return &bufs->bufs[2];
} else {
- fprintf(stderr, "all buffers in use\n");
return NULL;
}
}
@@ 411,12 409,15 @@ static int draw(struct context *ctx) {
}
ctx->dirty = false;
- struct shm_buf *buf = next_buf(&ctx->bufs);
- if (prepare_buffer(buf, ctx->shm, ctx->width, ctx->height) == -1) {
+ struct shm_buf *buf = shm_buf_get_next_available(&ctx->bufs);
+ if (buf == NULL) {
+ return -1;
+ }
+ if (shm_buf_prepare(buf, ctx->shm, ctx->width, ctx->height) == -1) {
return -1;
}
- use_buf(buf);
+ shm_buf_acquire(buf);
struct wl_callback *callback = wl_surface_frame(ctx->surface);
wl_callback_add_listener(callback, &frame_listener, ctx);