GHOST/wayland: create mmap-ed file manually if memfd_create is unavailable

This commit is contained in:
Christian Rauch 2021-07-12 23:28:41 +01:00
parent ebf7673f83
commit 4e65b1ef6c
2 changed files with 39 additions and 2 deletions

View File

@ -288,6 +288,13 @@ elseif(WITH_GHOST_X11 OR WITH_GHOST_WAYLAND)
${dbus_INCLUDE_DIRS}
)
include(CheckSymbolExists)
set(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
check_symbol_exists(memfd_create "sys/mman.h" HAVE_MEMFD_CREATE)
if (HAVE_MEMFD_CREATE)
add_definitions(-DHAVE_MEMFD_CREATE)
endif()
list(APPEND SRC
intern/GHOST_SystemWayland.cpp
intern/GHOST_WindowWayland.cpp

View File

@ -1804,13 +1804,43 @@ GHOST_TSuccess GHOST_SystemWayland::setCustomCursorShape(uint8_t *bitmap,
static const int32_t stride = sizex * 4; /* ARGB */
cursor->file_buffer->size = size_t(stride * sizey);
#ifdef HAVE_MEMFD_CREATE
const int fd = memfd_create("blender-cursor-custom", MFD_CLOEXEC | MFD_ALLOW_SEALING);
fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK);
posix_fallocate(fd, 0, int32_t(cursor->file_buffer->size));
if (fd >= 0) {
fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL);
}
#else
char *path = getenv("XDG_RUNTIME_DIR");
if (!path) {
errno = ENOENT;
return GHOST_kFailure;
}
char *tmpname;
asprintf(&tmpname, "%s/%s", path, "blender-XXXXXX");
const int fd = mkostemp(tmpname, O_CLOEXEC);
if (fd >= 0) {
unlink(tmpname);
}
free(tmpname);
#endif
if (fd < 0) {
return GHOST_kFailure;
}
if (posix_fallocate(fd, 0, int32_t(cursor->file_buffer->size)) != 0) {
return GHOST_kFailure;
}
cursor->file_buffer->data = mmap(
nullptr, cursor->file_buffer->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (cursor->file_buffer->data == MAP_FAILED) {
close(fd);
return GHOST_kFailure;
}
struct wl_shm_pool *pool = wl_shm_create_pool(d->shm, fd, int32_t(cursor->file_buffer->size));
wl_buffer *buffer = wl_shm_pool_create_buffer(