From bf4e2e0eac4b336747979dbda30d4f328ab0bf3d Mon Sep 17 00:00:00 2001 From: Ilia Bozhinov Date: Tue, 5 Jan 2021 20:11:23 +0100 Subject: [PATCH] util: add support for generating UUIDs Co-authored-by: Jason Francis --- .builds/alpine.yml | 1 + .builds/freebsd.yml | 1 + README.md | 1 + include/util/uuid.h | 8 ++++++++ meson.build | 3 +++ util/meson.build | 8 ++++++++ util/uuid.c | 33 +++++++++++++++++++++++++++++++++ 7 files changed, 55 insertions(+) create mode 100644 include/util/uuid.h create mode 100644 util/uuid.c diff --git a/.builds/alpine.yml b/.builds/alpine.yml index 402f72f6..7360e562 100644 --- a/.builds/alpine.yml +++ b/.builds/alpine.yml @@ -7,6 +7,7 @@ packages: - mesa-dev - meson - pixman-dev + - util-linux-dev - wayland-dev - wayland-protocols - xcb-util-image-dev diff --git a/.builds/freebsd.yml b/.builds/freebsd.yml index 74553f7b..3ebb2a46 100644 --- a/.builds/freebsd.yml +++ b/.builds/freebsd.yml @@ -10,6 +10,7 @@ packages: - graphics/png - graphics/wayland - graphics/wayland-protocols +- misc/e2fsprogs-libuuid - multimedia/ffmpeg - x11/libX11 - x11/libinput diff --git a/README.md b/README.md index bb534d82..5ee5aef2 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ Install dependencies: * pixman * systemd (optional, for logind support) * elogind (optional, for logind support on systems without systemd) +* libuuid (optional, for xdg-foreign support) If you choose to enable X11 support: diff --git a/include/util/uuid.h b/include/util/uuid.h new file mode 100644 index 00000000..2fecb407 --- /dev/null +++ b/include/util/uuid.h @@ -0,0 +1,8 @@ +#ifndef UTIL_UUID_H +#define UTIL_UUID_H + +#include + +bool generate_uuid(char out[static 37]); + +#endif diff --git a/meson.build b/meson.build index aa6598bf..c6835fbe 100644 --- a/meson.build +++ b/meson.build @@ -109,6 +109,9 @@ pixman = dependency('pixman-1') math = cc.find_library('m') rt = cc.find_library('rt') +uuid = dependency('uuid', required: false) +uuid_create = cc.has_function('uuid_create') + wlr_files = [] wlr_deps = [ wayland_server, diff --git a/util/meson.build b/util/meson.build index 14cd59c5..06996a4e 100644 --- a/util/meson.build +++ b/util/meson.build @@ -7,3 +7,11 @@ wlr_files += files( 'signal.c', 'time.c', ) + +if uuid.found() + wlr_deps += uuid + add_project_arguments('-DHAS_LIBUUID=1', language: 'c') +else + add_project_arguments('-DHAS_LIBUUID=0', language: 'c') +endif +wlr_files += files('uuid.c') diff --git a/util/uuid.c b/util/uuid.c new file mode 100644 index 00000000..a4523216 --- /dev/null +++ b/util/uuid.c @@ -0,0 +1,33 @@ +#include +#include "util/uuid.h" + +#if HAS_LIBUUID +bool generate_uuid(char out[static 37]) { + uuid_t uuid; + uuid_generate_random(uuid); + uuid_unparse(uuid, out); + return true; +} +#else +#include +#include + +bool generate_uuid(char out[static 37]) { + uuid_t uuid; + uint32_t status; + uuid_create(&uuid, &status); + if (status != uuid_s_ok) { + return false; + } + char *str; + uuid_to_string(&uuid, &str, &status); + if (status != uuid_s_ok) { + return false; + } + + assert(strlen(str) + 1 == 37); + memcpy(out, str, sizeof(out)); + free(str); + return true; +} +#endif