| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-dlopen.h" |
| 4 |
|
| 5 |
#include "dlfcn-util.h" |
| 6 |
#include "log.h" |
| 7 |
#include "string-util.h" |
| 8 |
#include "xkbcommon-util.h" |
| 9 |
|
| 10 |
#if HAVE_XKBCOMMON |
| 11 |
DLSYM_PROTOTYPE(xkb_context_new) = NULL; |
| 12 |
DLSYM_PROTOTYPE(xkb_context_unref) = NULL; |
| 13 |
DLSYM_PROTOTYPE(xkb_context_set_log_fn) = NULL; |
| 14 |
DLSYM_PROTOTYPE(xkb_keymap_new_from_names) = NULL; |
| 15 |
DLSYM_PROTOTYPE(xkb_keymap_unref) = NULL; |
| 16 |
|
| 17 |
static int dlopen_xkbcommon(int log_level) { |
| 18 |
static void *xkbcommon_dl = NULL; |
| 19 |
|
| 20 |
SD_ELF_NOTE_DLOPEN( |
| 21 |
"xkbcommon", |
| 22 |
"Support for keyboard locale descriptions", |
| 23 |
SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED, "libxkbcommon.so.0"); |
| 24 |
|
| 25 |
return dlopen_many_sym_or_warn( |
| 26 |
&xkbcommon_dl, "libxkbcommon.so.0", log_level, |
| 27 |
DLSYM_ARG(xkb_context_new), |
| 28 |
DLSYM_ARG(xkb_context_unref), |
| 29 |
DLSYM_ARG(xkb_context_set_log_fn), |
| 30 |
DLSYM_ARG(xkb_keymap_new_from_names), |
| 31 |
DLSYM_ARG(xkb_keymap_unref)); |
| 32 |
} |
| 33 |
|
| 34 |
_printf_(3, 0) |
| 35 |
static void log_xkb(struct xkb_context *ctx, enum xkb_log_level lvl, const char *format, va_list args) { |
| 36 |
const char *fmt; |
| 37 |
|
| 38 |
fmt = strjoina("libxkbcommon: ", format); |
| 39 |
DISABLE_WARNING_FORMAT_NONLITERAL; |
| 40 |
log_internalv(LOG_DEBUG, 0, PROJECT_FILE, __LINE__, __func__, fmt, args); |
| 41 |
REENABLE_WARNING; |
| 42 |
} |
| 43 |
|
| 44 |
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(struct xkb_context *, sym_xkb_context_unref, xkb_context_unrefp, NULL); |
| 45 |
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(struct xkb_keymap *, sym_xkb_keymap_unref, xkb_keymap_unrefp, NULL); |
| 46 |
|
| 47 |
int verify_xkb_rmlvo(const char *model, const char *layout, const char *variant, const char *options) { |
| 48 |
_cleanup_(xkb_context_unrefp) struct xkb_context *ctx = NULL; |
| 49 |
_cleanup_(xkb_keymap_unrefp) struct xkb_keymap *km = NULL; |
| 50 |
const struct xkb_rule_names rmlvo = { |
| 51 |
.model = model, |
| 52 |
.layout = layout, |
| 53 |
.variant = variant, |
| 54 |
.options = options, |
| 55 |
}; |
| 56 |
int r; |
| 57 |
|
| 58 |
/* Compile keymap from RMLVO information to check out its validity */ |
| 59 |
|
| 60 |
r = dlopen_xkbcommon(LOG_DEBUG); |
| 61 |
if (r < 0) |
| 62 |
return r; |
| 63 |
|
| 64 |
ctx = sym_xkb_context_new(XKB_CONTEXT_NO_ENVIRONMENT_NAMES); |
| 65 |
if (!ctx) |
| 66 |
return -ENOMEM; |
| 67 |
|
| 68 |
sym_xkb_context_set_log_fn(ctx, log_xkb); |
| 69 |
|
| 70 |
km = sym_xkb_keymap_new_from_names(ctx, &rmlvo, XKB_KEYMAP_COMPILE_NO_FLAGS); |
| 71 |
if (!km) |
| 72 |
return -EINVAL; |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
#endif |