1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
#include "internal.h"
#include "wayland.h"
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
static int
set_cloexec_or_close(int fd)
{
if (fd == -1)
return -1;
long flags = fcntl(fd, F_GETFD);
if (flags == -1)
goto err;
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
goto err;
return fd;
err:
close(fd);
return -1;
}
static int
create_tmpfile_cloexec(char *tmpname)
{
int fd;
#ifdef HAVE_MKOSTEMP
if ((fd = mkostemp(tmpname, O_CLOEXEC)) >= 0)
unlink(tmpname);
#else
if ((fd = mkstemp(tmpname)) >= 0) {
fd = set_cloexec_or_close(fd);
unlink(tmpname);
}
#endif
return fd;
}
static int
os_create_anonymous_file(off_t size)
{
static const char template[] = "bemenu-shared-XXXXXX";
int fd;
int ret;
const char *path;
if (!(path = getenv("XDG_RUNTIME_DIR")) || strlen(path) <= 0) {
errno = ENOENT;
return -1;
}
char *name;
int ts = (path[strlen(path) - 1] == '/');
if (!(name = bm_dprintf("%s%s%s", path, (ts ? "" : "/"), template)))
return -1;
fd = create_tmpfile_cloexec(name);
free(name);
if (fd < 0)
return -1;
#ifdef HAVE_POSIX_FALLOCATE
if ((ret = posix_fallocate(fd, 0, size)) != 0) {
close(fd);
errno = ret;
return -1;
}
#else
if ((ret = ftruncate(fd, size)) < 0) {
close(fd);
return -1;
}
#endif
return fd;
}
static void
buffer_release(void *data, struct wl_buffer *wl_buffer)
{
(void)wl_buffer;
struct buffer *buffer = data;
buffer->busy = false;
}
static const struct wl_buffer_listener buffer_listener = {
.release = buffer_release
};
static void
destroy_buffer(struct buffer *buffer)
{
if (buffer->buffer)
wl_buffer_destroy(buffer->buffer);
bm_cairo_destroy(&buffer->cairo);
memset(buffer, 0, sizeof(struct buffer));
}
static bool
create_buffer(struct wl_shm *shm, struct buffer *buffer, int32_t width, int32_t height, uint32_t format)
{
int fd = -1;
struct wl_shm_pool *pool = NULL;
uint32_t stride = width * 4;
uint32_t size = stride * height;
if ((fd = os_create_anonymous_file(size)) < 0) {
fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
return false;
}
void *data;
if ((data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
return false;
}
if (!(pool = wl_shm_create_pool(shm, fd, size))) {
close(fd);
return false;
}
if (!(buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, format)))
goto fail;
wl_shm_pool_destroy(pool);
pool = NULL;
close(fd);
fd = -1;
wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
cairo_surface_t *surf;
if (!(surf = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride)))
goto fail;
if (!bm_cairo_create_for_surface(&buffer->cairo, surf)) {
cairo_surface_destroy(surf);
goto fail;
}
buffer->width = width;
buffer->height = height;
return true;
fail:
if (fd > -1)
close(fd);
if (pool)
wl_shm_pool_destroy(pool);
destroy_buffer(buffer);
return false;
}
static struct buffer*
next_buffer(struct window *window)
{
assert(window);
struct buffer *buffer = NULL;
for (int32_t i = 0; i < 2; ++i) {
if (window->buffers[i].busy)
continue;
buffer = &window->buffers[i];
break;
}
if (!buffer)
return NULL;
if (window->width != buffer->width || window->height != buffer->height)
destroy_buffer(buffer);
if (!buffer->buffer && !create_buffer(window->shm, buffer, window->width, window->height, WL_SHM_FORMAT_ARGB8888))
return NULL;
return buffer;
}
static void
shell_surface_ping(void *data, struct wl_shell_surface *surface, uint32_t serial)
{
(void)data;
wl_shell_surface_pong(surface, serial);
}
static void
shell_surface_configure(void *data, struct wl_shell_surface *surface, uint32_t edges, int32_t width, int32_t height)
{
(void)data, (void)surface, (void)edges, (void)width, (void)height;
}
static void
shell_surface_popup_done(void *data, struct wl_shell_surface *surface)
{
(void)data, (void)surface;
}
static const struct wl_shell_surface_listener shell_surface_listener = {
.ping = shell_surface_ping,
.configure = shell_surface_configure,
.popup_done = shell_surface_popup_done,
};
static void
frame_callback(void *data, struct wl_callback *callback, uint32_t time)
{
(void)time;
struct window *window = data;
wl_callback_destroy(callback);
window->frame_cb = NULL;
window->render_pending = true;
}
static const struct wl_callback_listener listener = {
frame_callback
};
void
bm_wl_window_schedule_render(struct window *window)
{
assert(window);
if (window->frame_cb)
return;
window->frame_cb = wl_surface_frame(window->surface);
wl_callback_add_listener(window->frame_cb, &listener, window);
wl_surface_commit(window->surface);
}
void
bm_wl_window_render(struct window *window, struct wl_display *display, const struct bm_menu *menu)
{
assert(window && menu);
struct buffer *buffer;
for (int tries = 0; tries < 2; ++tries) {
if (!(buffer = next_buffer(window))) {
fprintf(stderr, "could not get next buffer");
exit(EXIT_FAILURE);
}
if (!window->notify.render)
break;
struct cairo_paint_result result;
window->notify.render(&buffer->cairo, buffer->width, window->max_height, menu, &result);
window->displayed = result.displayed;
if (window->height == result.height)
break;
window->height = result.height;
destroy_buffer(buffer);
}
window->frame_cb = wl_surface_frame(window->surface);
wl_callback_add_listener(window->frame_cb, &listener, window);
wl_surface_damage(window->surface, 0, 0, buffer->width, buffer->height);
wl_surface_attach(window->surface, buffer->buffer, 0, 0);
wl_surface_commit(window->surface);
buffer->busy = true;
window->render_pending = false;
}
void
bm_wl_window_destroy(struct window *window)
{
assert(window);
for (int32_t i = 0; i < 2; ++i)
destroy_buffer(&window->buffers[i]);
if (window->xdg_surface)
xdg_surface_destroy(window->xdg_surface);
if (window->shell_surface)
wl_shell_surface_destroy(window->shell_surface);
if (window->surface)
wl_surface_destroy(window->surface);
}
bool
bm_wl_window_create(struct window *window, struct wl_shm *shm, struct wl_shell *shell, struct xdg_shell *xdg_shell, struct wl_surface *surface)
{
assert(window);
if (shell && (window->shell_surface = wl_shell_get_shell_surface(shell, surface))) {
wl_shell_surface_add_listener(window->shell_surface, &shell_surface_listener, window);
wl_shell_surface_set_title(window->shell_surface, "bemenu");
wl_shell_surface_set_class(window->shell_surface, "bemenu");
wl_shell_surface_set_toplevel(window->shell_surface);
} else {
return false;
}
window->shm = shm;
window->surface = surface;
return true;
}
/* vim: set ts=8 sw=4 tw=0 :*/
|