Dunst
Lightweight notification daemon
Loading...
Searching...
No Matches
pool-buffer.c
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause */
7
8#define _POSIX_C_SOURCE 200112L
9#include <cairo/cairo.h>
10#include <errno.h>
11#include <fcntl.h>
12#include <sys/mman.h>
13#include <time.h>
14#include <unistd.h>
15#include <wayland-client.h>
16#include <string.h>
17
18#include "pool-buffer.h"
19
20static void randname(char *buf) {
21 struct timespec ts;
22 clock_gettime(CLOCK_REALTIME, &ts);
23 long r = ts.tv_nsec;
24 for (int i = 0; i < 6; ++i) {
25 buf[i] = 'A'+(r&15)+(r&16)*2;
26 r >>= 5;
27 }
28}
29
30static int anonymous_shm_open(void) {
31 char name[] = "/dunst-XXXXXX";
32 int retries = 100;
33
34 do {
35 randname(name + strlen(name) - 6);
36
37 --retries;
38 // shm_open guarantees that O_CLOEXEC is set
39 int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
40 if (fd >= 0) {
41 shm_unlink(name);
42 return fd;
43 }
44 } while (retries > 0 && errno == EEXIST);
45
46 return -1;
47}
48
49static int create_shm_file(off_t size) {
50 int fd = anonymous_shm_open();
51 if (fd < 0) {
52 return fd;
53 }
54
55 if (ftruncate(fd, size) < 0) {
56 close(fd);
57 return -1;
58 }
59
60 return fd;
61}
62
63static void buffer_handle_release(void *data, struct wl_buffer *wl_buffer) {
64 struct pool_buffer *buffer = data;
65 buffer->busy = false;
66}
67
68static const struct wl_buffer_listener buffer_listener = {
69 .release = buffer_handle_release,
70};
71
72static struct pool_buffer *create_buffer(struct wl_shm *shm,
73 struct pool_buffer *buf, int32_t width, int32_t height) {
74 const enum wl_shm_format wl_fmt = WL_SHM_FORMAT_ARGB8888;
75 const cairo_format_t cairo_fmt = CAIRO_FORMAT_ARGB32;
76
77 uint32_t stride = cairo_format_stride_for_width(cairo_fmt, width);
78 size_t size = stride * height;
79
80 void *data = NULL;
81 if (size > 0) {
82 int fd = create_shm_file(size);
83 if (fd == -1) {
84 return NULL;
85 }
86
87 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
88 if (data == MAP_FAILED) {
89 close(fd);
90 return NULL;
91 }
92
93 struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
94 buf->buffer =
95 wl_shm_pool_create_buffer(pool, 0, width, height, stride, wl_fmt);
96 wl_buffer_add_listener(buf->buffer, &buffer_listener, buf);
97 wl_shm_pool_destroy(pool);
98
99 close(fd);
100 }
101
102 buf->data = data;
103 buf->size = size;
104 buf->width = width;
105 buf->height = height;
106 buf->surface = cairo_image_surface_create_for_data(data, cairo_fmt, width,
107 height, stride);
108 buf->cairo = cairo_create(buf->surface);
109 buf->pango = pango_cairo_create_context(buf->cairo);
110 return buf;
111}
112
113void finish_buffer(struct pool_buffer *buffer) {
114 if (buffer->buffer) {
115 wl_buffer_destroy(buffer->buffer);
116 }
117 if (buffer->cairo) {
118 cairo_destroy(buffer->cairo);
119 }
120 if (buffer->surface) {
121 cairo_surface_destroy(buffer->surface);
122 }
123 if (buffer->pango) {
124 g_object_unref(buffer->pango);
125 }
126 if (buffer->data) {
127 munmap(buffer->data, buffer->size);
128 }
129 memset(buffer, 0, sizeof(struct pool_buffer));
130}
131
132struct pool_buffer *get_next_buffer(struct wl_shm *shm,
133 struct pool_buffer pool[static 2], uint32_t width, uint32_t height) {
134 struct pool_buffer *buffer = NULL;
135 for (size_t i = 0; i < 2; ++i) {
136 if (pool[i].busy) {
137 continue;
138 }
139 buffer = &pool[i];
140 }
141 if (!buffer) {
142 return NULL;
143 }
144
145 if (buffer->width != width || buffer->height != height) {
146 finish_buffer(buffer);
147 }
148
149 if (!buffer->buffer) {
150 if (!create_buffer(shm, buffer, width, height)) {
151 return NULL;
152 }
153 }
154
155 return buffer;
156}
Wayland rendering buffer pool.