update drwl

This commit is contained in:
sewn 2024-08-04 23:17:42 +03:00
parent 7a07b728c4
commit 02c3ac09a6
No known key found for this signature in database
4 changed files with 38 additions and 77 deletions

View file

@ -22,14 +22,16 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <wayland-client.h>
#include "drwl.h"
#ifdef __linux__
#include <linux/memfd.h>
#endif
typedef struct {
struct wl_buffer *wl_buf;
@ -38,44 +40,6 @@ typedef struct {
void *data;
} PoolBuf;
#if defined(__linux__) || defined(___FreeBSD__)
#ifdef __linux__
#include <linux/memfd.h>
#endif
#define __POOLBUF_HAS_MEMFD_CREATE
int memfd_create(const char *, unsigned);
#else
static void
randname(char *buf)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
long r = ts.tv_nsec;
for (int i = 0; i < 6; ++i) {
buf[i] = 'A'+(r&15)+(r&16)*2;
r >>= 5;
}
}
static int
create_shm(void)
{
char name[] = "/drwl-XXXXXX";
int retries = 100;
do {
randname(name + sizeof(name) - 7);
--retries;
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd >= 0) {
shm_unlink(name);
return fd;
}
} while (retries > 0 && errno == EEXIST);
return -1;
}
#endif
static void
poolbuf_destroy(PoolBuf *buf)
{
@ -95,23 +59,35 @@ static struct wl_buffer_listener poolbuf_buffer_listener = {
.release = poolbuf_buffer_release,
};
/* Caller must call poolbuf_destroy after finalizing usage if norelease is passed */
static PoolBuf *
poolbuf_create(struct wl_shm *shm, int32_t width, int32_t height, int norelease)
poolbuf_create(struct wl_shm *shm,
int32_t width, int32_t height, int32_t stride, int norelease)
{
int fd;
void *data;
struct wl_shm_pool *shm_pool;
struct wl_buffer *wl_buf;
int32_t stride = drwl_stride(width);
int32_t size = stride * height;
PoolBuf *buf;
#ifdef __POOLBUF_HAS_MEMFD_CREATE
fd = memfd_create("drwl-shm-buffer-pool",
MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_NOEXEC_SEAL);
#if defined(__linux__) || \
((defined(__FreeBSD__) && (__FreeBSD_version >= 1300048)))
fd = memfd_create("poolbuf-shm-buffer-pool",
MFD_CLOEXEC | MFD_ALLOW_SEALING |
#if defined(MFD_NOEXEC_SEAL)
MFD_NOEXEC_SEAL
#else
fd = create_shm();
0
#endif
);
#else
char template[] = "/tmp/poolbuf-XXXXXX";
#if defined(__OpenBSD__)
fd = shm_mkstemp(template);
#else
fd = mkostemp(template, O_CLOEXEC);
#endif
unlink(template);
#endif
if (fd < 0)
return NULL;
@ -121,8 +97,7 @@ poolbuf_create(struct wl_shm *shm, int32_t width, int32_t height, int norelease)
return NULL;
}
data = mmap(NULL, size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
close(fd);
return NULL;
@ -130,7 +105,7 @@ poolbuf_create(struct wl_shm *shm, int32_t width, int32_t height, int norelease)
shm_pool = wl_shm_create_pool(shm, fd, size);
wl_buf = wl_shm_pool_create_buffer(shm_pool, 0,
width, height, stride, WL_SHM_FORMAT_ARGB8888);
width, height, stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(shm_pool);
close(fd);