summaryrefslogtreecommitdiffstats
path: root/wayland/buffers.c
diff options
context:
space:
mode:
Diffstat (limited to 'wayland/buffers.c')
-rw-r--r--wayland/buffers.c134
1 files changed, 0 insertions, 134 deletions
diff --git a/wayland/buffers.c b/wayland/buffers.c
deleted file mode 100644
index e9780997..00000000
--- a/wayland/buffers.c
+++ /dev/null
@@ -1,134 +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
15static 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
44static void buffer_release(void *data, struct wl_buffer *wl_buffer) {
45 struct buffer *buffer = data;
46 buffer->busy = false;
47}
48
49static const struct wl_buffer_listener buffer_listener = {
50 .release = buffer_release
51};
52
53static struct buffer *create_buffer(struct window *window, struct buffer *buf,
54 int32_t width, int32_t height, int32_t scale, uint32_t format) {
55
56 width *= scale;
57 height *= scale;
58 uint32_t stride = width * 4;
59 uint32_t size = stride * height;
60
61 char *name;
62 int fd = create_pool_file(size, &name);
63 if (fd == -1) {
64 sway_abort("Unable to allocate buffer");
65 return NULL; // never reached
66 }
67 void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
68 struct wl_shm_pool *pool = wl_shm_create_pool(window->registry->shm, fd, size);
69 buf->buffer = wl_shm_pool_create_buffer(pool, 0,
70 width, height, stride, format);
71 wl_shm_pool_destroy(pool);
72 close(fd);
73 unlink(name);
74 free(name);
75 fd = -1;
76
77 buf->width = width;
78 buf->height = height;
79 buf->surface = cairo_image_surface_create_for_data(data,
80 CAIRO_FORMAT_ARGB32, width, height, stride);
81 buf->cairo = cairo_create(buf->surface);
82 buf->pango = pango_cairo_create_context(buf->cairo);
83
84 wl_buffer_add_listener(buf->buffer, &buffer_listener, buf);
85 return buf;
86}
87
88static void destroy_buffer(struct buffer *buffer) {
89 if (buffer->buffer) {
90 wl_buffer_destroy(buffer->buffer);
91 }
92 if (buffer->cairo) {
93 cairo_destroy(buffer->cairo);
94 }
95 if (buffer->surface) {
96 cairo_surface_destroy(buffer->surface);
97 }
98 if (buffer->pango) {
99 g_object_unref(buffer->pango);
100 }
101 memset(buffer, 0, sizeof(struct buffer));
102}
103
104struct buffer *get_next_buffer(struct window *window) {
105 struct buffer *buffer = NULL;
106
107 int i;
108 for (i = 0; i < 2; ++i) {
109 if (window->buffers[i].busy) {
110 continue;
111 }
112 buffer = &window->buffers[i];
113 }
114
115 if (!buffer) {
116 return NULL;
117 }
118
119 if (buffer->width != window->width || buffer->height != window->height) {
120 destroy_buffer(buffer);
121 }
122
123 if (!buffer->buffer) {
124 if (!create_buffer(window, buffer,
125 window->width, window->height, window->scale,
126 WL_SHM_FORMAT_ARGB8888)) {
127 return NULL;
128 }
129 }
130
131 window->cairo = buffer->cairo;
132 window->buffer = buffer;
133 return buffer;
134}