util: add support for generating UUIDs

Co-authored-by: Jason Francis <cycl0ps@tuta.io>
This commit is contained in:
Ilia Bozhinov 2021-01-05 20:11:23 +01:00 committed by Simon Ser
parent 3721dbfddb
commit bf4e2e0eac
7 changed files with 55 additions and 0 deletions

View File

@ -7,6 +7,7 @@ packages:
- mesa-dev
- meson
- pixman-dev
- util-linux-dev
- wayland-dev
- wayland-protocols
- xcb-util-image-dev

View File

@ -10,6 +10,7 @@ packages:
- graphics/png
- graphics/wayland
- graphics/wayland-protocols
- misc/e2fsprogs-libuuid
- multimedia/ffmpeg
- x11/libX11
- x11/libinput

View File

@ -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:

8
include/util/uuid.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef UTIL_UUID_H
#define UTIL_UUID_H
#include <stdbool.h>
bool generate_uuid(char out[static 37]);
#endif

View File

@ -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,

View File

@ -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')

33
util/uuid.c Normal file
View File

@ -0,0 +1,33 @@
#include <uuid.h>
#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 <string.h>
#include <stdlib.h>
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