diff options
Diffstat (limited to 'wayland/buffers.c')
-rw-r--r-- | wayland/buffers.c | 127 |
1 files changed, 0 insertions, 127 deletions
diff --git a/wayland/buffers.c b/wayland/buffers.c deleted file mode 100644 index 3b809e6f..00000000 --- a/wayland/buffers.c +++ /dev/null | |||
@@ -1,127 +0,0 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <wayland-client.h> | ||
3 | #include <cairo/cairo.h> | ||
4 | #include <pango/pangocairo.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <string.h> | ||
7 | #include <stdio.h> | ||
8 | #include <unistd.h> | ||
9 | #include <errno.h> | ||
10 | #include <sys/mman.h> | ||
11 | #include "client/buffer.h" | ||
12 | #include "list.h" | ||
13 | #include "log.h" | ||
14 | |||
15 | static int create_pool_file(size_t size, char **name) { | ||
16 | static const char template[] = "sway-client-XXXXXX"; | ||
17 | const char *path = getenv("XDG_RUNTIME_DIR"); | ||
18 | if (!path) { | ||
19 | return -1; | ||
20 | } | ||
21 | |||
22 | int ts = (path[strlen(path) - 1] == '/'); | ||
23 | |||
24 | *name = malloc( | ||
25 | strlen(template) + | ||
26 | strlen(path) + | ||
27 | (ts ? 0 : 1) + 1); | ||
28 | sprintf(*name, "%s%s%s", path, ts ? "" : "/", template); | ||
29 | |||
30 | int fd = mkstemp(*name); | ||
31 | |||
32 | if (fd < 0) { | ||
33 | return -1; | ||
34 | } | ||
35 | |||
36 | if (ftruncate(fd, size) < 0) { | ||
37 | close(fd); | ||
38 | return -1; | ||
39 | } | ||
40 | |||
41 | return fd; | ||
42 | } | ||
43 | |||
44 | static void buffer_release(void *data, struct wl_buffer *wl_buffer) { | ||
45 | struct buffer *buffer = data; | ||
46 | buffer->busy = false; | ||
47 | } | ||
48 | |||
49 | static const struct wl_buffer_listener buffer_listener = { | ||
50 | .release = buffer_release | ||
51 | }; | ||
52 | |||
53 | static struct buffer *create_buffer(struct wl_shm *shm, struct buffer *buf, | ||
54 | int32_t width, int32_t height, uint32_t format) { | ||
55 | uint32_t stride = width * 4; | ||
56 | uint32_t size = stride * height; | ||
57 | |||
58 | char *name; | ||
59 | int fd = create_pool_file(size, &name); | ||
60 | if (fd == -1) { | ||
61 | sway_abort("Unable to allocate buffer"); | ||
62 | return NULL; // never reached | ||
63 | } | ||
64 | void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | ||
65 | struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size); | ||
66 | buf->buffer = wl_shm_pool_create_buffer(pool, 0, | ||
67 | width, height, stride, format); | ||
68 | wl_shm_pool_destroy(pool); | ||
69 | close(fd); | ||
70 | unlink(name); | ||
71 | free(name); | ||
72 | fd = -1; | ||
73 | |||
74 | buf->width = width; | ||
75 | buf->height = height; | ||
76 | buf->surface = cairo_image_surface_create_for_data(data, | ||
77 | CAIRO_FORMAT_ARGB32, width, height, stride); | ||
78 | buf->cairo = cairo_create(buf->surface); | ||
79 | buf->pango = pango_cairo_create_context(buf->cairo); | ||
80 | |||
81 | wl_buffer_add_listener(buf->buffer, &buffer_listener, buf); | ||
82 | return buf; | ||
83 | } | ||
84 | |||
85 | static void destroy_buffer(struct buffer *buffer) { | ||
86 | if (buffer->buffer) { | ||
87 | wl_buffer_destroy(buffer->buffer); | ||
88 | } | ||
89 | if (buffer->cairo) { | ||
90 | cairo_destroy(buffer->cairo); | ||
91 | } | ||
92 | if (buffer->surface) { | ||
93 | cairo_surface_destroy(buffer->surface); | ||
94 | } | ||
95 | if (buffer->pango) { | ||
96 | g_object_unref(buffer->pango); | ||
97 | } | ||
98 | memset(buffer, 0, sizeof(struct buffer)); | ||
99 | } | ||
100 | |||
101 | struct pool_buffer *get_next_buffer(struct wl_shm *shm, | ||
102 | struct pool_buffer pool[2], uint32_t width, uint32_t height) { | ||
103 | struct buffer *buffer = NULL; | ||
104 | |||
105 | for (size_t i = 0; i < sizeof(pool) / sizeof(pool[0]); ++i) { | ||
106 | if (buffers[i].busy) { | ||
107 | continue; | ||
108 | } | ||
109 | buffer = &buffers[i]; | ||
110 | } | ||
111 | |||
112 | if (!buffer) { | ||
113 | return NULL; | ||
114 | } | ||
115 | |||
116 | if (buffer->width != width || buffer->height != height) { | ||
117 | destroy_buffer(buffer); | ||
118 | } | ||
119 | |||
120 | if (!buffer->buffer) { | ||
121 | if (!create_buffer(shm, buffer, width, height, | ||
122 | WL_SHM_FORMAT_ARGB8888)) { | ||
123 | return NULL; | ||
124 | } | ||
125 | } | ||
126 | return buffer; | ||
127 | } | ||