1 #define _POSIX_C_SOURCE 200809L
2 #include <assert.h>
3 #include <cairo/cairo.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/mman.h>
9 #include <unistd.h>
10 #include <wayland-client.h>
11 #include "pool-buffer.h"
12 
set_cloexec(int fd)13 static bool set_cloexec(int fd) {
14 	long flags = fcntl(fd, F_GETFD);
15 	if (flags == -1) {
16 		return false;
17 	}
18 
19 	if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
20 		return false;
21 	}
22 
23 	return true;
24 }
25 
create_pool_file(size_t size,char ** name)26 static int create_pool_file(size_t size, char **name) {
27 	static const char template[] = "sway-client-XXXXXX";
28 	const char *path = getenv("XDG_RUNTIME_DIR");
29 	if (path == NULL) {
30 		fprintf(stderr, "XDG_RUNTIME_DIR is not set\n");
31 		return -1;
32 	}
33 
34 	size_t name_size = strlen(template) + 1 + strlen(path) + 1;
35 	*name = malloc(name_size);
36 	if (*name == NULL) {
37 		fprintf(stderr, "allocation failed\n");
38 		return -1;
39 	}
40 	snprintf(*name, name_size, "%s/%s", path, template);
41 
42 	int fd = mkstemp(*name);
43 	if (fd < 0) {
44 		return -1;
45 	}
46 
47 	if (!set_cloexec(fd)) {
48 		close(fd);
49 		return -1;
50 	}
51 
52 	if (ftruncate(fd, size) < 0) {
53 		close(fd);
54 		return -1;
55 	}
56 
57 	return fd;
58 }
59 
buffer_release(void * data,struct wl_buffer * wl_buffer)60 static void buffer_release(void *data, struct wl_buffer *wl_buffer) {
61 	struct pool_buffer *buffer = data;
62 	buffer->busy = false;
63 }
64 
65 static const struct wl_buffer_listener buffer_listener = {
66 	.release = buffer_release
67 };
68 
create_buffer(struct wl_shm * shm,struct pool_buffer * buf,int32_t width,int32_t height,uint32_t format)69 static struct pool_buffer *create_buffer(struct wl_shm *shm,
70 		struct pool_buffer *buf, int32_t width, int32_t height,
71 		uint32_t format) {
72 	uint32_t stride = width * 4;
73 	size_t size = stride * height;
74 
75 	char *name;
76 	int fd = create_pool_file(size, &name);
77 	assert(fd != -1);
78 	void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
79 	struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
80 	buf->buffer = wl_shm_pool_create_buffer(pool, 0,
81 			width, height, stride, format);
82 	wl_shm_pool_destroy(pool);
83 	close(fd);
84 	unlink(name);
85 	free(name);
86 	fd = -1;
87 
88 	buf->size = size;
89 	buf->width = width;
90 	buf->height = height;
91 	buf->data = data;
92 	buf->surface = cairo_image_surface_create_for_data(data,
93 			CAIRO_FORMAT_ARGB32, width, height, stride);
94 	buf->cairo = cairo_create(buf->surface);
95 
96 	wl_buffer_add_listener(buf->buffer, &buffer_listener, buf);
97 	return buf;
98 }
99 
destroy_buffer(struct pool_buffer * buffer)100 void destroy_buffer(struct pool_buffer *buffer) {
101 	if (buffer->buffer) {
102 		wl_buffer_destroy(buffer->buffer);
103 	}
104 	if (buffer->cairo) {
105 		cairo_destroy(buffer->cairo);
106 	}
107 	if (buffer->surface) {
108 		cairo_surface_destroy(buffer->surface);
109 	}
110 	if (buffer->data) {
111 		munmap(buffer->data, buffer->size);
112 	}
113 	memset(buffer, 0, sizeof(struct pool_buffer));
114 }
115 
get_next_buffer(struct wl_shm * shm,struct pool_buffer pool[static2],uint32_t width,uint32_t height)116 struct pool_buffer *get_next_buffer(struct wl_shm *shm,
117 		struct pool_buffer pool[static 2], uint32_t width, uint32_t height) {
118 	struct pool_buffer *buffer = NULL;
119 
120 	for (size_t i = 0; i < 2; ++i) {
121 		if (pool[i].busy) {
122 			continue;
123 		}
124 		buffer = &pool[i];
125 	}
126 
127 	if (!buffer) {
128 		return NULL;
129 	}
130 
131 	if (buffer->width != width || buffer->height != height) {
132 		destroy_buffer(buffer);
133 	}
134 
135 	if (!buffer->buffer) {
136 		if (!create_buffer(shm, buffer, width, height,
137 					WL_SHM_FORMAT_ARGB8888)) {
138 			return NULL;
139 		}
140 	}
141 	buffer->busy = true;
142 	return buffer;
143 }
144