| 1 |
#include "internal.h" |
| 2 |
#include "wayland.h" |
| 3 |
|
| 4 |
#include <unistd.h> |
| 5 |
#include <errno.h> |
| 6 |
#include <fcntl.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <sys/mman.h> |
| 9 |
#include <math.h> |
| 10 |
|
| 11 |
static int |
| 12 |
set_cloexec_or_close(int fd) |
| 13 |
{ |
| 14 |
if (fd == -1) |
| 15 |
return -1; |
| 16 |
|
| 17 |
long flags = fcntl(fd, F_GETFD); |
| 18 |
if (flags == -1) |
| 19 |
goto err; |
| 20 |
|
| 21 |
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) |
| 22 |
goto err; |
| 23 |
|
| 24 |
return fd; |
| 25 |
|
| 26 |
err: |
| 27 |
close(fd); |
| 28 |
return -1; |
| 29 |
} |
| 30 |
|
| 31 |
static int |
| 32 |
create_tmpfile_cloexec(char *tmpname) |
| 33 |
{ |
| 34 |
int fd; |
| 35 |
|
| 36 |
#ifdef HAVE_MKOSTEMP |
| 37 |
if ((fd = mkostemp(tmpname, O_CLOEXEC)) >= 0) |
| 38 |
unlink(tmpname); |
| 39 |
#else |
| 40 |
if ((fd = mkstemp(tmpname)) >= 0) { |
| 41 |
fd = set_cloexec_or_close(fd); |
| 42 |
unlink(tmpname); |
| 43 |
} |
| 44 |
#endif |
| 45 |
|
| 46 |
return fd; |
| 47 |
} |
| 48 |
|
| 49 |
static int |
| 50 |
os_create_anonymous_file(off_t size) |
| 51 |
{ |
| 52 |
static const char template[] = "bemenu-shared-XXXXXX"; |
| 53 |
int fd; |
| 54 |
int ret; |
| 55 |
|
| 56 |
const char *path; |
| 57 |
if (!(path = getenv("XDG_RUNTIME_DIR")) || strlen(path) <= 0) { |
| 58 |
errno = ENOENT; |
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
char *name; |
| 63 |
int ts = (path[strlen(path) - 1] == '/'); |
| 64 |
if (!(name = bm_dprintf("%s%s%s", path, (ts ? "" : "/"), template))) |
| 65 |
return -1; |
| 66 |
|
| 67 |
fd = create_tmpfile_cloexec(name); |
| 68 |
free(name); |
| 69 |
|
| 70 |
if (fd < 0) |
| 71 |
return -1; |
| 72 |
|
| 73 |
#ifdef HAVE_POSIX_FALLOCATE |
| 74 |
if ((ret = posix_fallocate(fd, 0, size)) != 0) { |
| 75 |
close(fd); |
| 76 |
errno = ret; |
| 77 |
return -1; |
| 78 |
} |
| 79 |
#else |
| 80 |
if ((ret = ftruncate(fd, size)) < 0) { |
| 81 |
close(fd); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
#endif |
| 85 |
|
| 86 |
return fd; |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
buffer_release(void *data, struct wl_buffer *wl_buffer) |
| 91 |
{ |
| 92 |
(void)wl_buffer; |
| 93 |
struct buffer *buffer = data; |
| 94 |
buffer->busy = false; |
| 95 |
} |
| 96 |
|
| 97 |
static const struct wl_buffer_listener buffer_listener = { |
| 98 |
.release = buffer_release |
| 99 |
}; |
| 100 |
|
| 101 |
static void |
| 102 |
destroy_buffer(struct buffer *buffer) |
| 103 |
{ |
| 104 |
if (buffer->buffer) |
| 105 |
wl_buffer_destroy(buffer->buffer); |
| 106 |
bm_cairo_destroy(&buffer->cairo); |
| 107 |
memset(buffer, 0, sizeof(struct buffer)); |
| 108 |
} |
| 109 |
|
| 110 |
static bool |
| 111 |
create_buffer(struct wl_shm *shm, struct buffer *buffer, int32_t width, int32_t height, uint32_t format, double scale, bool antialiasing) |
| 112 |
{ |
| 113 |
int fd = -1; |
| 114 |
struct wl_shm_pool *pool = NULL; |
| 115 |
uint32_t stride = width * 4; |
| 116 |
uint32_t size = stride * height; |
| 117 |
|
| 118 |
if ((fd = os_create_anonymous_file(size)) < 0) { |
| 119 |
fprintf(stderr, "wayland: creating a buffer file for %d B failed\n", size); |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
void *data; |
| 124 |
if ((data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) { |
| 125 |
fprintf(stderr, "wayland: mmap failed\n"); |
| 126 |
close(fd); |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
if (!(pool = wl_shm_create_pool(shm, fd, size))) { |
| 131 |
close(fd); |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
if (!(buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, format))) |
| 136 |
goto fail; |
| 137 |
|
| 138 |
wl_shm_pool_destroy(pool); |
| 139 |
pool = NULL; |
| 140 |
|
| 141 |
close(fd); |
| 142 |
fd = -1; |
| 143 |
|
| 144 |
wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer); |
| 145 |
|
| 146 |
cairo_surface_t *surf; |
| 147 |
if (!(surf = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride))) |
| 148 |
goto fail; |
| 149 |
|
| 150 |
const char *env_scale = getenv("BEMENU_SCALE"); |
| 151 |
if (env_scale) { |
| 152 |
buffer->cairo.scale = fmax(strtof(env_scale, NULL), 1.0f); |
| 153 |
} else { |
| 154 |
buffer->cairo.scale = scale; |
| 155 |
} |
| 156 |
|
| 157 |
if (antialiasing) { |
| 158 |
buffer->cairo.antialiasing = true; |
| 159 |
} else { |
| 160 |
buffer->cairo.antialiasing = false; |
| 161 |
} |
| 162 |
|
| 163 |
if (!bm_cairo_create_for_surface(&buffer->cairo, surf)) { |
| 164 |
cairo_surface_destroy(surf); |
| 165 |
goto fail; |
| 166 |
} |
| 167 |
|
| 168 |
buffer->width = width; |
| 169 |
buffer->height = height; |
| 170 |
return true; |
| 171 |
|
| 172 |
fail: |
| 173 |
if (fd > -1) |
| 174 |
close(fd); |
| 175 |
if (pool) |
| 176 |
wl_shm_pool_destroy(pool); |
| 177 |
destroy_buffer(buffer); |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
static struct buffer* |
| 182 |
next_buffer(struct window *window) |
| 183 |
{ |
| 184 |
assert(window); |
| 185 |
|
| 186 |
struct buffer *buffer = NULL; |
| 187 |
for (int32_t i = 0; i < 2; ++i) { |
| 188 |
if (window->buffers[i].busy) |
| 189 |
continue; |
| 190 |
|
| 191 |
buffer = &window->buffers[i]; |
| 192 |
break; |
| 193 |
} |
| 194 |
|
| 195 |
if (!buffer) |
| 196 |
return NULL; |
| 197 |
|
| 198 |
if ((uint32_t) ceil(window->width * window->scale) != buffer->width || (uint32_t) ceil(window->height * window->scale) != buffer->height) |
| 199 |
destroy_buffer(buffer); |
| 200 |
|
| 201 |
if (!buffer->buffer && !create_buffer(window->shm, buffer, ceil(window->width * window->scale), ceil(window->height * window->scale), WL_SHM_FORMAT_ARGB8888, window->scale, !window->wayland->fractional_scaling)) |
| 202 |
return NULL; |
| 203 |
|
| 204 |
return buffer; |
| 205 |
} |
| 206 |
|
| 207 |
static void |
| 208 |
frame_callback(void *data, struct wl_callback *callback, uint32_t time) |
| 209 |
{ |
| 210 |
(void)time; |
| 211 |
struct window *window = data; |
| 212 |
wl_callback_destroy(callback); |
| 213 |
window->frame_cb = NULL; |
| 214 |
window->render_pending = true; |
| 215 |
} |
| 216 |
|
| 217 |
static const struct wl_callback_listener listener = { |
| 218 |
frame_callback |
| 219 |
}; |
| 220 |
|
| 221 |
static uint32_t |
| 222 |
get_align_anchor(enum bm_align align) |
| 223 |
{ |
| 224 |
uint32_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; |
| 225 |
|
| 226 |
if(align == BM_ALIGN_TOP) { |
| 227 |
anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP; |
| 228 |
} else if(align == BM_ALIGN_CENTER) { |
| 229 |
anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; |
| 230 |
} else { |
| 231 |
anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; |
| 232 |
} |
| 233 |
|
| 234 |
return anchor; |
| 235 |
} |
| 236 |
|
| 237 |
void |
| 238 |
bm_wl_window_schedule_render(struct window *window) |
| 239 |
{ |
| 240 |
assert(window); |
| 241 |
if (window->frame_cb) |
| 242 |
return; |
| 243 |
|
| 244 |
window->frame_cb = wl_surface_frame(window->surface); |
| 245 |
wl_callback_add_listener(window->frame_cb, &listener, window); |
| 246 |
wl_surface_commit(window->surface); |
| 247 |
} |
| 248 |
|
| 249 |
void |
| 250 |
bm_wl_window_render(struct window *window, struct wl_display *display, struct bm_menu *menu) |
| 251 |
{ |
| 252 |
(void)display; |
| 253 |
assert(window && menu); |
| 254 |
struct buffer *buffer; |
| 255 |
for (int tries = 0; tries < 2; ++tries) { |
| 256 |
if (!(buffer = next_buffer(window))) { |
| 257 |
fprintf(stderr, "could not get next buffer"); |
| 258 |
exit(EXIT_FAILURE); |
| 259 |
} |
| 260 |
|
| 261 |
if (!window->notify.render) |
| 262 |
break; |
| 263 |
|
| 264 |
struct cairo_paint_result result; |
| 265 |
window->notify.render(&buffer->cairo, buffer->width, window->max_height, menu, &result); |
| 266 |
window->displayed = result.displayed; |
| 267 |
|
| 268 |
if (window->height == (uint32_t) ceil(result.height / window->scale)) |
| 269 |
break; |
| 270 |
|
| 271 |
window->height = ceil(result.height / window->scale); |
| 272 |
zwlr_layer_surface_v1_set_size(window->layer_surface, window->width, window->height); |
| 273 |
destroy_buffer(buffer); |
| 274 |
} |
| 275 |
|
| 276 |
assert(ceil(window->width * window->scale) == buffer->width); |
| 277 |
assert(ceil(window->height * window->scale) == buffer->height); |
| 278 |
|
| 279 |
if (window->wayland->fractional_scaling) { |
| 280 |
assert(window->viewport_surface); |
| 281 |
wp_viewport_set_destination(window->viewport_surface, window->width, window->height); |
| 282 |
} else { |
| 283 |
wl_surface_set_buffer_scale(window->surface, window->scale); |
| 284 |
} |
| 285 |
|
| 286 |
wl_surface_damage_buffer(window->surface, 0, 0, buffer->width, buffer->height); |
| 287 |
wl_surface_attach(window->surface, buffer->buffer, 0, 0); |
| 288 |
wl_surface_commit(window->surface); |
| 289 |
buffer->busy = true; |
| 290 |
window->render_pending = false; |
| 291 |
} |
| 292 |
|
| 293 |
void |
| 294 |
bm_wl_window_destroy(struct window *window) |
| 295 |
{ |
| 296 |
assert(window); |
| 297 |
|
| 298 |
for (int32_t i = 0; i < 2; ++i) |
| 299 |
destroy_buffer(&window->buffers[i]); |
| 300 |
|
| 301 |
if (window->layer_surface) |
| 302 |
zwlr_layer_surface_v1_destroy(window->layer_surface); |
| 303 |
|
| 304 |
if (window->surface) |
| 305 |
wl_surface_destroy(window->surface); |
| 306 |
|
| 307 |
struct surf_output *surf_output, *surf_output_tmp; |
| 308 |
wl_list_for_each_safe(surf_output, surf_output_tmp, &window->surf_outputs, link) { |
| 309 |
wl_list_remove(&surf_output->link); |
| 310 |
free(surf_output); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
static void |
| 315 |
layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *layer_surface, uint32_t serial, uint32_t width, uint32_t height) |
| 316 |
{ |
| 317 |
struct window *window = data; |
| 318 |
window->width = width; |
| 319 |
window->height = height; |
| 320 |
zwlr_layer_surface_v1_ack_configure(layer_surface, serial); |
| 321 |
} |
| 322 |
|
| 323 |
static void |
| 324 |
layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *layer_surface) |
| 325 |
{ |
| 326 |
struct window *window = data; |
| 327 |
zwlr_layer_surface_v1_destroy(layer_surface); |
| 328 |
wl_surface_destroy(window->surface); |
| 329 |
exit(1); |
| 330 |
} |
| 331 |
|
| 332 |
static uint32_t |
| 333 |
get_window_width(struct window *window) |
| 334 |
{ |
| 335 |
uint32_t width = window->width * ((window->width_factor != 0) ? window->width_factor : 1); |
| 336 |
|
| 337 |
if(width > window->width - 2 * window->hmargin_size) |
| 338 |
width = window->width - 2 * window->hmargin_size; |
| 339 |
|
| 340 |
if(width < WINDOW_MIN_WIDTH || 2 * window->hmargin_size > window->width) |
| 341 |
width = WINDOW_MIN_WIDTH; |
| 342 |
|
| 343 |
return width; |
| 344 |
} |
| 345 |
|
| 346 |
static void |
| 347 |
fractional_scale_preferred_scale( |
| 348 |
void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1, |
| 349 |
uint32_t scale) |
| 350 |
{ |
| 351 |
(void)wp_fractional_scale_v1; |
| 352 |
struct window *window = data; |
| 353 |
|
| 354 |
window->scale = (double)scale / 120; |
| 355 |
} |
| 356 |
|
| 357 |
static const struct wp_fractional_scale_v1_listener fractional_scale_listener = { |
| 358 |
.preferred_scale = fractional_scale_preferred_scale, |
| 359 |
}; |
| 360 |
|
| 361 |
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { |
| 362 |
.configure = layer_surface_configure, |
| 363 |
.closed = layer_surface_closed, |
| 364 |
}; |
| 365 |
|
| 366 |
void |
| 367 |
bm_wl_window_set_width(struct window *window, struct wl_display *display, uint32_t margin, float factor) |
| 368 |
{ |
| 369 |
if(window->hmargin_size == margin && window->width_factor == factor) |
| 370 |
return; |
| 371 |
|
| 372 |
window->hmargin_size = margin; |
| 373 |
window->width_factor = factor; |
| 374 |
|
| 375 |
zwlr_layer_surface_v1_set_anchor(window->layer_surface, window->align_anchor); |
| 376 |
zwlr_layer_surface_v1_set_size(window->layer_surface, get_window_width(window), window->height); |
| 377 |
|
| 378 |
wl_surface_commit(window->surface); |
| 379 |
wl_display_roundtrip(display); |
| 380 |
} |
| 381 |
|
| 382 |
void |
| 383 |
bm_wl_window_set_align(struct window *window, struct wl_display *display, enum bm_align align) |
| 384 |
{ |
| 385 |
if(window->align == align) |
| 386 |
return; |
| 387 |
|
| 388 |
window->align = align; |
| 389 |
|
| 390 |
window->align_anchor = get_align_anchor(window->align); |
| 391 |
|
| 392 |
zwlr_layer_surface_v1_set_anchor(window->layer_surface, window->align_anchor); |
| 393 |
wl_surface_commit(window->surface); |
| 394 |
wl_display_roundtrip(display); |
| 395 |
} |
| 396 |
|
| 397 |
void |
| 398 |
bm_wl_window_set_y_offset(struct window *window, struct wl_display *display, int32_t y_offset) |
| 399 |
{ |
| 400 |
if(window->y_offset == y_offset) |
| 401 |
return; |
| 402 |
|
| 403 |
window->y_offset = y_offset; |
| 404 |
|
| 405 |
zwlr_layer_surface_v1_set_margin(window->layer_surface, window->y_offset, 0, 0, 0); |
| 406 |
wl_surface_commit(window->surface); |
| 407 |
wl_display_roundtrip(display); |
| 408 |
} |
| 409 |
|
| 410 |
void |
| 411 |
bm_wl_window_grab_keyboard(struct window *window, struct wl_display *display, bool grab) |
| 412 |
{ |
| 413 |
zwlr_layer_surface_v1_set_keyboard_interactivity(window->layer_surface, grab); |
| 414 |
wl_surface_commit(window->surface); |
| 415 |
wl_display_roundtrip(display); |
| 416 |
} |
| 417 |
|
| 418 |
void |
| 419 |
bm_wl_window_set_overlap(struct window *window, struct wl_display *display, bool overlap) |
| 420 |
{ |
| 421 |
zwlr_layer_surface_v1_set_exclusive_zone(window->layer_surface, overlap ? -1 : 0); |
| 422 |
wl_surface_commit(window->surface); |
| 423 |
wl_display_roundtrip(display); |
| 424 |
} |
| 425 |
|
| 426 |
bool |
| 427 |
bm_wl_window_create(struct window *window, struct wl_display *display, struct wl_shm *shm, struct wl_output *output, struct zwlr_layer_shell_v1 *layer_shell, struct wl_surface *surface) |
| 428 |
{ |
| 429 |
assert(window); |
| 430 |
|
| 431 |
struct wayland *wayland = window->wayland; |
| 432 |
|
| 433 |
if (wayland->fractional_scaling) { |
| 434 |
assert(wayland->wfs_mgr && wayland->viewporter); |
| 435 |
|
| 436 |
struct wp_fractional_scale_v1 *wfs_surf = wp_fractional_scale_manager_v1_get_fractional_scale(wayland->wfs_mgr, surface); |
| 437 |
wp_fractional_scale_v1_add_listener( |
| 438 |
wfs_surf, &fractional_scale_listener, window); |
| 439 |
window->viewport_surface = wp_viewporter_get_viewport(wayland->viewporter, surface); |
| 440 |
} |
| 441 |
|
| 442 |
enum zwlr_layer_shell_v1_layer layer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY; |
| 443 |
if (layer_shell && (window->layer_surface = zwlr_layer_shell_v1_get_layer_surface(layer_shell, surface, output, layer, "menu"))) { |
| 444 |
zwlr_layer_surface_v1_add_listener(window->layer_surface, &layer_surface_listener, window); |
| 445 |
window->align_anchor = get_align_anchor(window->align); |
| 446 |
zwlr_layer_surface_v1_set_anchor(window->layer_surface, window->align_anchor); |
| 447 |
zwlr_layer_surface_v1_set_size(window->layer_surface, 0, 32); |
| 448 |
|
| 449 |
wl_surface_commit(surface); |
| 450 |
wl_display_roundtrip(display); |
| 451 |
|
| 452 |
zwlr_layer_surface_v1_set_size(window->layer_surface, get_window_width(window), 32); |
| 453 |
} else { |
| 454 |
return false; |
| 455 |
} |
| 456 |
|
| 457 |
window->shm = shm; |
| 458 |
window->surface = surface; |
| 459 |
return true; |
| 460 |
} |
| 461 |
|
| 462 |
/* vim: set ts=8 sw=4 tw=0 :*/ |