diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-03-21 21:17:17 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-03-21 21:17:17 +0100 |
| commit | c0a7c5ddcb9e0e98f6d981448e7c54fab6621327 (patch) | |
| tree | d0307493efe92924ad267f6a0c5efb0fc1e16bc3 /wayland.c | |
first sketch
Diffstat (limited to 'wayland.c')
| -rw-r--r-- | wayland.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/wayland.c b/wayland.c new file mode 100644 index 0000000..0787ec3 --- /dev/null +++ b/wayland.c @@ -0,0 +1,66 @@ +#include <errno.h> +#include <time.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <unistd.h> + +#include "wayland.h" + +static void randname(char *buf); +static int create_shm_file(void); + + +void +randname(char *buf) +{ + struct timespec ts; + long r; + int i; + + clock_gettime(CLOCK_REALTIME, &ts); + r = ts.tv_nsec; + for (i = 0; i < 6; ++i) { + buf[i] = 'A'+(r&15)+(r&16)*2; + r >>= 5; + } +} + +int +create_shm_file(void) +{ + int fd; + int retries = 100; + char name[] = "/wl_shm-XXXXXX"; + + do { + randname(name + sizeof(name) - 7); + --retries; + 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; +} + +int +allocate_shm_file(size_t size) +{ + int ret; + int fd = create_shm_file(); + + if (fd < 0) + return -1; + + do { + ret = ftruncate(fd, size); + } while (ret < 0 && errno == EINTR); + + if (ret < 0) { + close(fd); + return -1; + } + + return fd; +} |