mirror of
https://github.com/morgan9e/systemd
synced 2026-04-15 00:47:10 +09:00
locale: split out XKB settings to X11Context
No functional changes, just refactoring and preparation for later commits.
This commit is contained in:
committed by
Luca Boccassi
parent
c2ddaed483
commit
b41ec10ae4
@@ -56,13 +56,99 @@ static const char* systemd_language_fallback_map(void) {
|
||||
return SYSTEMD_LANGUAGE_FALLBACK_MAP;
|
||||
}
|
||||
|
||||
static void x11_context_clear(X11Context *xc) {
|
||||
assert(xc);
|
||||
|
||||
xc->layout = mfree(xc->layout);
|
||||
xc->options = mfree(xc->options);
|
||||
xc->model = mfree(xc->model);
|
||||
xc->variant = mfree(xc->variant);
|
||||
}
|
||||
|
||||
static bool x11_context_isempty(const X11Context *xc) {
|
||||
assert(xc);
|
||||
|
||||
return
|
||||
isempty(xc->layout) &&
|
||||
isempty(xc->model) &&
|
||||
isempty(xc->variant) &&
|
||||
isempty(xc->options);
|
||||
}
|
||||
|
||||
void x11_context_empty_to_null(X11Context *xc) {
|
||||
assert(xc);
|
||||
|
||||
/* Do not call x11_context_clear() for the passed object. */
|
||||
|
||||
xc->layout = empty_to_null(xc->layout);
|
||||
xc->model = empty_to_null(xc->model);
|
||||
xc->variant = empty_to_null(xc->variant);
|
||||
xc->options = empty_to_null(xc->options);
|
||||
}
|
||||
|
||||
bool x11_context_is_safe(const X11Context *xc) {
|
||||
assert(xc);
|
||||
|
||||
return
|
||||
(!xc->layout || string_is_safe(xc->layout)) &&
|
||||
(!xc->model || string_is_safe(xc->model)) &&
|
||||
(!xc->variant || string_is_safe(xc->variant)) &&
|
||||
(!xc->options || string_is_safe(xc->options));
|
||||
}
|
||||
|
||||
bool x11_context_equal(const X11Context *a, const X11Context *b) {
|
||||
assert(a);
|
||||
assert(b);
|
||||
|
||||
return
|
||||
streq_ptr(a->layout, b->layout) &&
|
||||
streq_ptr(a->model, b->model) &&
|
||||
streq_ptr(a->variant, b->variant) &&
|
||||
streq_ptr(a->options, b->options);
|
||||
}
|
||||
|
||||
int x11_context_copy(X11Context *dest, const X11Context *src) {
|
||||
bool modified;
|
||||
int r;
|
||||
|
||||
assert(dest);
|
||||
|
||||
if (dest == src)
|
||||
return 0;
|
||||
|
||||
if (!src) {
|
||||
modified = !x11_context_isempty(dest);
|
||||
x11_context_clear(dest);
|
||||
return modified;
|
||||
}
|
||||
|
||||
r = free_and_strdup(&dest->layout, src->layout);
|
||||
if (r < 0)
|
||||
return r;
|
||||
modified = r > 0;
|
||||
|
||||
r = free_and_strdup(&dest->model, src->model);
|
||||
if (r < 0)
|
||||
return r;
|
||||
modified = modified || r > 0;
|
||||
|
||||
r = free_and_strdup(&dest->variant, src->variant);
|
||||
if (r < 0)
|
||||
return r;
|
||||
modified = modified || r > 0;
|
||||
|
||||
r = free_and_strdup(&dest->options, src->options);
|
||||
if (r < 0)
|
||||
return r;
|
||||
modified = modified || r > 0;
|
||||
|
||||
return modified;
|
||||
}
|
||||
|
||||
static void context_clear_x11(Context *c) {
|
||||
assert(c);
|
||||
|
||||
c->x11_layout = mfree(c->x11_layout);
|
||||
c->x11_options = mfree(c->x11_options);
|
||||
c->x11_model = mfree(c->x11_model);
|
||||
c->x11_variant = mfree(c->x11_variant);
|
||||
x11_context_clear(&c->x11_from_xorg);
|
||||
}
|
||||
|
||||
static void context_clear_vconsole(Context *c) {
|
||||
@@ -86,6 +172,19 @@ void context_clear(Context *c) {
|
||||
c->polkit_registry = bus_verify_polkit_async_registry_free(c->polkit_registry);
|
||||
};
|
||||
|
||||
static X11Context *context_get_x11_context(Context *c) {
|
||||
assert(c);
|
||||
|
||||
if (!x11_context_isempty(&c->x11_from_xorg))
|
||||
return &c->x11_from_xorg;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
X11Context *context_get_x11_context_safe(Context *c) {
|
||||
return &ASSERT_PTR(c)->x11_from_xorg;
|
||||
}
|
||||
|
||||
int locale_read_data(Context *c, sd_bus_message *m) {
|
||||
assert(c);
|
||||
|
||||
@@ -175,7 +274,7 @@ int x11_read_data(Context *c, sd_bus_message *m) {
|
||||
return 0;
|
||||
|
||||
c->x11_stat = st;
|
||||
context_clear_x11(c);
|
||||
x11_context_clear(&c->x11_from_xorg);
|
||||
|
||||
fd_ro = fd_reopen(fd, O_CLOEXEC | O_RDONLY);
|
||||
if (fd_ro < 0)
|
||||
@@ -212,13 +311,13 @@ int x11_read_data(Context *c, sd_bus_message *m) {
|
||||
char **p = NULL;
|
||||
|
||||
if (streq(a[1], "XkbLayout"))
|
||||
p = &c->x11_layout;
|
||||
p = &c->x11_from_xorg.layout;
|
||||
else if (streq(a[1], "XkbModel"))
|
||||
p = &c->x11_model;
|
||||
p = &c->x11_from_xorg.model;
|
||||
else if (streq(a[1], "XkbVariant"))
|
||||
p = &c->x11_variant;
|
||||
p = &c->x11_from_xorg.variant;
|
||||
else if (streq(a[1], "XkbOptions"))
|
||||
p = &c->x11_options;
|
||||
p = &c->x11_from_xorg.options;
|
||||
|
||||
if (p)
|
||||
free_and_replace(*p, a[2]);
|
||||
@@ -238,6 +337,9 @@ int x11_read_data(Context *c, sd_bus_message *m) {
|
||||
in_section = false;
|
||||
}
|
||||
|
||||
if (!x11_context_isempty(&c->x11_from_xorg))
|
||||
log_debug("XKB settings loaded from xorg.conf.d/00-keyboard.conf.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -280,15 +382,13 @@ int vconsole_write_data(Context *c) {
|
||||
int x11_write_data(Context *c) {
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
_cleanup_(unlink_and_freep) char *temp_path = NULL;
|
||||
const X11Context *xc;
|
||||
int r;
|
||||
|
||||
assert(c);
|
||||
|
||||
if (isempty(c->x11_layout) &&
|
||||
isempty(c->x11_model) &&
|
||||
isempty(c->x11_variant) &&
|
||||
isempty(c->x11_options)) {
|
||||
|
||||
xc = context_get_x11_context(c);
|
||||
if (!xc) {
|
||||
if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
|
||||
return errno == ENOENT ? 0 : -errno;
|
||||
|
||||
@@ -310,17 +410,17 @@ int x11_write_data(Context *c) {
|
||||
" Identifier \"system-keyboard\"\n"
|
||||
" MatchIsKeyboard \"on\"\n", f);
|
||||
|
||||
if (!isempty(c->x11_layout))
|
||||
fprintf(f, " Option \"XkbLayout\" \"%s\"\n", c->x11_layout);
|
||||
if (!isempty(xc->layout))
|
||||
fprintf(f, " Option \"XkbLayout\" \"%s\"\n", xc->layout);
|
||||
|
||||
if (!isempty(c->x11_model))
|
||||
fprintf(f, " Option \"XkbModel\" \"%s\"\n", c->x11_model);
|
||||
if (!isempty(xc->model))
|
||||
fprintf(f, " Option \"XkbModel\" \"%s\"\n", xc->model);
|
||||
|
||||
if (!isempty(c->x11_variant))
|
||||
fprintf(f, " Option \"XkbVariant\" \"%s\"\n", c->x11_variant);
|
||||
if (!isempty(xc->variant))
|
||||
fprintf(f, " Option \"XkbVariant\" \"%s\"\n", xc->variant);
|
||||
|
||||
if (!isempty(c->x11_options))
|
||||
fprintf(f, " Option \"XkbOptions\" \"%s\"\n", c->x11_options);
|
||||
if (!isempty(xc->options))
|
||||
fprintf(f, " Option \"XkbOptions\" \"%s\"\n", xc->options);
|
||||
|
||||
fputs("EndSection\n", f);
|
||||
|
||||
@@ -389,16 +489,14 @@ static int read_next_mapping(
|
||||
|
||||
int vconsole_convert_to_x11(Context *c) {
|
||||
int r, modified = -1;
|
||||
X11Context *xc;
|
||||
|
||||
assert(c);
|
||||
|
||||
if (isempty(c->vc_keymap)) {
|
||||
modified =
|
||||
!isempty(c->x11_layout) ||
|
||||
!isempty(c->x11_model) ||
|
||||
!isempty(c->x11_variant) ||
|
||||
!isempty(c->x11_options);
|
||||
xc = context_get_x11_context_safe(c);
|
||||
|
||||
if (isempty(c->vc_keymap)) {
|
||||
modified = !x11_context_isempty(xc);
|
||||
context_clear_x11(c);
|
||||
} else {
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
@@ -421,19 +519,16 @@ int vconsole_convert_to_x11(Context *c) {
|
||||
if (!streq(c->vc_keymap, a[0]))
|
||||
continue;
|
||||
|
||||
if (!streq_ptr(c->x11_layout, empty_or_dash_to_null(a[1])) ||
|
||||
!streq_ptr(c->x11_model, empty_or_dash_to_null(a[2])) ||
|
||||
!streq_ptr(c->x11_variant, empty_or_dash_to_null(a[3])) ||
|
||||
!streq_ptr(c->x11_options, empty_or_dash_to_null(a[4]))) {
|
||||
|
||||
if (free_and_strdup(&c->x11_layout, empty_or_dash_to_null(a[1])) < 0 ||
|
||||
free_and_strdup(&c->x11_model, empty_or_dash_to_null(a[2])) < 0 ||
|
||||
free_and_strdup(&c->x11_variant, empty_or_dash_to_null(a[3])) < 0 ||
|
||||
free_and_strdup(&c->x11_options, empty_or_dash_to_null(a[4])) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
modified = true;
|
||||
}
|
||||
r = x11_context_copy(xc,
|
||||
&(X11Context) {
|
||||
.layout = empty_or_dash_to_null(a[1]),
|
||||
.model = empty_or_dash_to_null(a[2]),
|
||||
.variant = empty_or_dash_to_null(a[3]),
|
||||
.options = empty_or_dash_to_null(a[4]),
|
||||
});
|
||||
if (r < 0)
|
||||
return r;
|
||||
modified = r > 0;
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -441,10 +536,10 @@ int vconsole_convert_to_x11(Context *c) {
|
||||
|
||||
if (modified > 0)
|
||||
log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
|
||||
strempty(c->x11_layout),
|
||||
strempty(c->x11_model),
|
||||
strempty(c->x11_variant),
|
||||
strempty(c->x11_options));
|
||||
strempty(xc->layout),
|
||||
strempty(xc->model),
|
||||
strempty(xc->variant),
|
||||
strempty(xc->options));
|
||||
else if (modified < 0)
|
||||
log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".",
|
||||
c->vc_keymap);
|
||||
@@ -454,16 +549,17 @@ int vconsole_convert_to_x11(Context *c) {
|
||||
return modified > 0;
|
||||
}
|
||||
|
||||
int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **ret) {
|
||||
int find_converted_keymap(const X11Context *xc, char **ret) {
|
||||
_cleanup_free_ char *n = NULL;
|
||||
|
||||
assert(x11_layout);
|
||||
assert(xc);
|
||||
assert(!isempty(xc->layout));
|
||||
assert(ret);
|
||||
|
||||
if (x11_variant)
|
||||
n = strjoin(x11_layout, "-", x11_variant);
|
||||
if (xc->variant)
|
||||
n = strjoin(xc->layout, "-", xc->variant);
|
||||
else
|
||||
n = strdup(x11_layout);
|
||||
n = strdup(xc->layout);
|
||||
if (!n)
|
||||
return -ENOMEM;
|
||||
|
||||
@@ -488,15 +584,15 @@ int find_converted_keymap(const char *x11_layout, const char *x11_variant, char
|
||||
return 0;
|
||||
}
|
||||
|
||||
int find_legacy_keymap(Context *c, char **ret) {
|
||||
int find_legacy_keymap(const X11Context *xc, char **ret) {
|
||||
const char *map;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
_cleanup_free_ char *new_keymap = NULL;
|
||||
unsigned best_matching = 0;
|
||||
int r;
|
||||
|
||||
assert(c);
|
||||
assert(!isempty(c->x11_layout));
|
||||
assert(xc);
|
||||
assert(!isempty(xc->layout));
|
||||
|
||||
map = systemd_kbd_model_map();
|
||||
f = fopen(map, "re");
|
||||
@@ -514,14 +610,14 @@ int find_legacy_keymap(Context *c, char **ret) {
|
||||
break;
|
||||
|
||||
/* Determine how well matching this entry is */
|
||||
if (streq(c->x11_layout, a[1]))
|
||||
if (streq(xc->layout, a[1]))
|
||||
/* If we got an exact match, this is the best */
|
||||
matching = 10;
|
||||
else {
|
||||
/* We have multiple X layouts, look for an
|
||||
* entry that matches our key with everything
|
||||
* but the first layout stripped off. */
|
||||
if (startswith_comma(c->x11_layout, a[1]))
|
||||
if (startswith_comma(xc->layout, a[1]))
|
||||
matching = 5;
|
||||
else {
|
||||
_cleanup_free_ char *x = NULL;
|
||||
@@ -531,19 +627,19 @@ int find_legacy_keymap(Context *c, char **ret) {
|
||||
x = strdupcspn(a[1], ",");
|
||||
if (!x)
|
||||
return -ENOMEM;
|
||||
if (startswith_comma(c->x11_layout, x))
|
||||
if (startswith_comma(xc->layout, x))
|
||||
matching = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (matching > 0) {
|
||||
if (isempty(c->x11_model) || streq_ptr(c->x11_model, a[2])) {
|
||||
if (isempty(xc->model) || streq_ptr(xc->model, a[2])) {
|
||||
matching++;
|
||||
|
||||
if (streq_ptr(c->x11_variant, a[3])) {
|
||||
if (streq_ptr(xc->variant, a[3])) {
|
||||
matching++;
|
||||
|
||||
if (streq_ptr(c->x11_options, a[4]))
|
||||
if (streq_ptr(xc->options, a[4]))
|
||||
matching++;
|
||||
}
|
||||
}
|
||||
@@ -563,7 +659,7 @@ int find_legacy_keymap(Context *c, char **ret) {
|
||||
}
|
||||
}
|
||||
|
||||
if (best_matching < 10 && !isempty(c->x11_layout)) {
|
||||
if (best_matching < 10 && !isempty(xc->layout)) {
|
||||
_cleanup_free_ char *l = NULL, *v = NULL, *converted = NULL;
|
||||
|
||||
/* The best match is only the first part of the X11
|
||||
@@ -571,17 +667,22 @@ int find_legacy_keymap(Context *c, char **ret) {
|
||||
* matches just the first layout.
|
||||
*/
|
||||
|
||||
l = strndup(c->x11_layout, strcspn(c->x11_layout, ","));
|
||||
l = strndup(xc->layout, strcspn(xc->layout, ","));
|
||||
if (!l)
|
||||
return -ENOMEM;
|
||||
|
||||
if (!isempty(c->x11_variant)) {
|
||||
v = strndup(c->x11_variant, strcspn(c->x11_variant, ","));
|
||||
if (!isempty(xc->variant)) {
|
||||
v = strndup(xc->variant, strcspn(xc->variant, ","));
|
||||
if (!v)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
r = find_converted_keymap(l, v, &converted);
|
||||
r = find_converted_keymap(
|
||||
&(X11Context) {
|
||||
.layout = l,
|
||||
.variant = v,
|
||||
},
|
||||
&converted);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0)
|
||||
@@ -623,10 +724,13 @@ int find_language_fallback(const char *lang, char **ret) {
|
||||
|
||||
int x11_convert_to_vconsole(Context *c) {
|
||||
bool modified = false;
|
||||
const X11Context *xc;
|
||||
|
||||
assert(c);
|
||||
|
||||
if (isempty(c->x11_layout)) {
|
||||
xc = context_get_x11_context_safe(c);
|
||||
|
||||
if (isempty(xc->layout)) {
|
||||
modified =
|
||||
!isempty(c->vc_keymap) ||
|
||||
!isempty(c->vc_keymap_toggle);
|
||||
@@ -636,16 +740,16 @@ int x11_convert_to_vconsole(Context *c) {
|
||||
_cleanup_free_ char *new_keymap = NULL;
|
||||
int r;
|
||||
|
||||
r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap);
|
||||
r = find_converted_keymap(xc, &new_keymap);
|
||||
if (r == 0)
|
||||
r = find_legacy_keymap(c, &new_keymap);
|
||||
r = find_legacy_keymap(xc, &new_keymap);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
/* We search for layout-variant match first, but then we also look
|
||||
* for anything which matches just the layout. So it's accurate to say
|
||||
* that we couldn't find anything which matches the layout. */
|
||||
log_notice("No conversion to virtual console map found for \"%s\".", c->x11_layout);
|
||||
log_notice("No conversion to virtual console map found for \"%s\".", xc->layout);
|
||||
|
||||
if (!streq_ptr(c->vc_keymap, new_keymap)) {
|
||||
context_clear_vconsole(c);
|
||||
|
||||
@@ -8,16 +8,20 @@
|
||||
#include "hashmap.h"
|
||||
#include "locale-setup.h"
|
||||
|
||||
typedef struct X11Context {
|
||||
char *layout;
|
||||
char *model;
|
||||
char *variant;
|
||||
char *options;
|
||||
} X11Context;
|
||||
|
||||
typedef struct Context {
|
||||
sd_bus_message *locale_cache;
|
||||
LocaleContext locale_context;
|
||||
|
||||
sd_bus_message *x11_cache;
|
||||
struct stat x11_stat;
|
||||
char *x11_layout;
|
||||
char *x11_model;
|
||||
char *x11_variant;
|
||||
char *x11_options;
|
||||
X11Context x11_from_xorg;
|
||||
|
||||
sd_bus_message *vc_cache;
|
||||
struct stat vc_stat;
|
||||
@@ -27,8 +31,15 @@ typedef struct Context {
|
||||
Hashmap *polkit_registry;
|
||||
} Context;
|
||||
|
||||
int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **ret);
|
||||
int find_legacy_keymap(Context *c, char **ret);
|
||||
void x11_context_empty_to_null(X11Context *xc);
|
||||
bool x11_context_is_safe(const X11Context *xc);
|
||||
bool x11_context_equal(const X11Context *a, const X11Context *b);
|
||||
int x11_context_copy(X11Context *dest, const X11Context *src);
|
||||
|
||||
X11Context *context_get_x11_context_safe(Context *c);
|
||||
|
||||
int find_converted_keymap(const X11Context *xc, char **ret);
|
||||
int find_legacy_keymap(const X11Context *xc, char **ret);
|
||||
int find_language_fallback(const char *lang, char **ret);
|
||||
|
||||
int locale_read_data(Context *c, sd_bus_message *m);
|
||||
|
||||
@@ -189,6 +189,7 @@ static int property_get_xkb(
|
||||
sd_bus_error *error) {
|
||||
|
||||
Context *c = ASSERT_PTR(userdata);
|
||||
const X11Context *xc;
|
||||
int r;
|
||||
|
||||
assert(property);
|
||||
@@ -197,14 +198,16 @@ static int property_get_xkb(
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
xc = context_get_x11_context_safe(c);
|
||||
|
||||
if (streq(property, "X11Layout"))
|
||||
return sd_bus_message_append_basic(reply, 's', c->x11_layout);
|
||||
return sd_bus_message_append_basic(reply, 's', xc->layout);
|
||||
if (streq(property, "X11Model"))
|
||||
return sd_bus_message_append_basic(reply, 's', c->x11_model);
|
||||
return sd_bus_message_append_basic(reply, 's', xc->model);
|
||||
if (streq(property, "X11Variant"))
|
||||
return sd_bus_message_append_basic(reply, 's', c->x11_variant);
|
||||
return sd_bus_message_append_basic(reply, 's', xc->variant);
|
||||
if (streq(property, "X11Options"))
|
||||
return sd_bus_message_append_basic(reply, 's', c->x11_options);
|
||||
return sd_bus_message_append_basic(reply, 's', xc->options);
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -597,19 +600,16 @@ static int verify_xkb_rmlvo(const char *model, const char *layout, const char *v
|
||||
|
||||
static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_error *error) {
|
||||
Context *c = ASSERT_PTR(userdata);
|
||||
const char *layout, *model, *variant, *options;
|
||||
int convert, interactive, r;
|
||||
X11Context *xc, in;
|
||||
|
||||
assert(m);
|
||||
|
||||
r = sd_bus_message_read(m, "ssssbb", &layout, &model, &variant, &options, &convert, &interactive);
|
||||
r = sd_bus_message_read(m, "ssssbb", &in.layout, &in.model, &in.variant, &in.options, &convert, &interactive);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
layout = empty_to_null(layout);
|
||||
model = empty_to_null(model);
|
||||
variant = empty_to_null(variant);
|
||||
options = empty_to_null(options);
|
||||
x11_context_empty_to_null(&in);
|
||||
|
||||
r = x11_read_data(c, m);
|
||||
if (r < 0) {
|
||||
@@ -617,22 +617,18 @@ static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_err
|
||||
return sd_bus_error_set(error, SD_BUS_ERROR_FAILED, "Failed to read x11 keyboard layout data");
|
||||
}
|
||||
|
||||
if (streq_ptr(layout, c->x11_layout) &&
|
||||
streq_ptr(model, c->x11_model) &&
|
||||
streq_ptr(variant, c->x11_variant) &&
|
||||
streq_ptr(options, c->x11_options))
|
||||
xc = context_get_x11_context_safe(c);
|
||||
|
||||
if (x11_context_equal(xc, &in))
|
||||
return sd_bus_reply_method_return(m, NULL);
|
||||
|
||||
if ((layout && !string_is_safe(layout)) ||
|
||||
(model && !string_is_safe(model)) ||
|
||||
(variant && !string_is_safe(variant)) ||
|
||||
(options && !string_is_safe(options)))
|
||||
if (!x11_context_is_safe(&in))
|
||||
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Received invalid keyboard data");
|
||||
|
||||
r = verify_xkb_rmlvo(model, layout, variant, options);
|
||||
r = verify_xkb_rmlvo(in.model, in.layout, in.variant, in.options);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Cannot compile XKB keymap for new x11 keyboard layout ('%s' / '%s' / '%s' / '%s'): %m",
|
||||
strempty(model), strempty(layout), strempty(variant), strempty(options));
|
||||
strempty(in.model), strempty(in.layout), strempty(in.variant), strempty(in.options));
|
||||
|
||||
if (r == -EOPNOTSUPP)
|
||||
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Local keyboard configuration not supported on this system.");
|
||||
@@ -654,11 +650,9 @@ static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_err
|
||||
if (r == 0)
|
||||
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
|
||||
|
||||
if (free_and_strdup(&c->x11_layout, layout) < 0 ||
|
||||
free_and_strdup(&c->x11_model, model) < 0 ||
|
||||
free_and_strdup(&c->x11_variant, variant) < 0 ||
|
||||
free_and_strdup(&c->x11_options, options) < 0)
|
||||
return -ENOMEM;
|
||||
r = x11_context_copy(xc, &in);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
r = x11_write_data(c);
|
||||
if (r < 0) {
|
||||
@@ -667,10 +661,10 @@ static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_err
|
||||
}
|
||||
|
||||
log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
|
||||
strempty(c->x11_layout),
|
||||
strempty(c->x11_model),
|
||||
strempty(c->x11_variant),
|
||||
strempty(c->x11_options));
|
||||
strempty(in.layout),
|
||||
strempty(in.model),
|
||||
strempty(in.variant),
|
||||
strempty(in.options));
|
||||
|
||||
(void) sd_bus_emit_properties_changed(
|
||||
sd_bus_message_get_bus(m),
|
||||
|
||||
@@ -26,10 +26,17 @@ TEST(find_converted_keymap) {
|
||||
_cleanup_free_ char *ans = NULL, *ans2 = NULL;
|
||||
int r;
|
||||
|
||||
assert_se(find_converted_keymap("pl", "foobar", &ans) == 0);
|
||||
assert_se(find_converted_keymap(
|
||||
&(X11Context) {
|
||||
.layout = (char*) "pl",
|
||||
.variant = (char*) "foobar",
|
||||
}, &ans) == 0);
|
||||
assert_se(ans == NULL);
|
||||
|
||||
r = find_converted_keymap("pl", NULL, &ans);
|
||||
r = find_converted_keymap(
|
||||
&(X11Context) {
|
||||
.layout = (char*) "pl",
|
||||
}, &ans);
|
||||
if (r == 0) {
|
||||
log_info("Skipping rest of %s: keymaps are not installed", __func__);
|
||||
return;
|
||||
@@ -38,81 +45,87 @@ TEST(find_converted_keymap) {
|
||||
assert_se(r == 1);
|
||||
assert_se(streq(ans, "pl"));
|
||||
|
||||
assert_se(find_converted_keymap("pl", "dvorak", &ans2) == 1);
|
||||
assert_se(find_converted_keymap(
|
||||
&(X11Context) {
|
||||
.layout = (char*) "pl",
|
||||
.variant = (char*) "dvorak",
|
||||
}, &ans2) == 1);
|
||||
assert_se(streq(ans2, "pl-dvorak"));
|
||||
}
|
||||
|
||||
TEST(find_legacy_keymap) {
|
||||
Context c = {};
|
||||
X11Context xc = {};
|
||||
_cleanup_free_ char *ans = NULL, *ans2 = NULL;
|
||||
|
||||
c.x11_layout = (char*) "foobar";
|
||||
assert_se(find_legacy_keymap(&c, &ans) == 0);
|
||||
xc.layout = (char*) "foobar";
|
||||
assert_se(find_legacy_keymap(&xc, &ans) == 0);
|
||||
assert_se(ans == NULL);
|
||||
|
||||
c.x11_layout = (char*) "pl";
|
||||
assert_se(find_legacy_keymap(&c, &ans) == 1);
|
||||
xc.layout = (char*) "pl";
|
||||
assert_se(find_legacy_keymap(&xc, &ans) == 1);
|
||||
assert_se(streq(ans, "pl2"));
|
||||
|
||||
c.x11_layout = (char*) "pl,ru";
|
||||
assert_se(find_legacy_keymap(&c, &ans2) == 1);
|
||||
xc.layout = (char*) "pl,ru";
|
||||
assert_se(find_legacy_keymap(&xc, &ans2) == 1);
|
||||
assert_se(streq(ans, "pl2"));
|
||||
}
|
||||
|
||||
TEST(vconsole_convert_to_x11) {
|
||||
_cleanup_(context_clear) Context c = {};
|
||||
X11Context *xc = &c.x11_from_xorg;
|
||||
|
||||
log_info("/* test emptying first (:) */");
|
||||
assert_se(free_and_strdup(&c.x11_layout, "foo") >= 0);
|
||||
assert_se(free_and_strdup(&c.x11_variant, "bar") >= 0);
|
||||
assert_se(free_and_strdup(&xc->layout, "foo") >= 0);
|
||||
assert_se(free_and_strdup(&xc->variant, "bar") >= 0);
|
||||
assert_se(vconsole_convert_to_x11(&c) == 1);
|
||||
assert_se(c.x11_layout == NULL);
|
||||
assert_se(c.x11_variant == NULL);
|
||||
assert_se(xc->layout == NULL);
|
||||
assert_se(xc->variant == NULL);
|
||||
|
||||
log_info("/* test emptying second (:) */");
|
||||
|
||||
assert_se(vconsole_convert_to_x11(&c) == 0);
|
||||
assert_se(c.x11_layout == NULL);
|
||||
assert_se(c.x11_variant == NULL);
|
||||
assert_se(xc->layout == NULL);
|
||||
assert_se(xc->variant == NULL);
|
||||
|
||||
log_info("/* test without variant, new mapping (es:) */");
|
||||
assert_se(free_and_strdup(&c.vc_keymap, "es") >= 0);
|
||||
|
||||
assert_se(vconsole_convert_to_x11(&c) == 1);
|
||||
assert_se(streq(c.x11_layout, "es"));
|
||||
assert_se(c.x11_variant == NULL);
|
||||
assert_se(streq(xc->layout, "es"));
|
||||
assert_se(xc->variant == NULL);
|
||||
|
||||
log_info("/* test with known variant, new mapping (es:dvorak) */");
|
||||
assert_se(free_and_strdup(&c.vc_keymap, "es-dvorak") >= 0);
|
||||
|
||||
assert_se(vconsole_convert_to_x11(&c) == 1);
|
||||
assert_se(streq(c.x11_layout, "es"));
|
||||
assert_se(streq(c.x11_variant, "dvorak"));
|
||||
assert_se(streq(xc->layout, "es"));
|
||||
assert_se(streq(xc->variant, "dvorak"));
|
||||
|
||||
log_info("/* test with old mapping (fr:latin9) */");
|
||||
assert_se(free_and_strdup(&c.vc_keymap, "fr-latin9") >= 0);
|
||||
|
||||
assert_se(vconsole_convert_to_x11(&c) == 1);
|
||||
assert_se(streq(c.x11_layout, "fr"));
|
||||
assert_se(streq(c.x11_variant, "latin9"));
|
||||
assert_se(streq(xc->layout, "fr"));
|
||||
assert_se(streq(xc->variant, "latin9"));
|
||||
|
||||
log_info("/* test with a compound mapping (ru,us) */");
|
||||
assert_se(free_and_strdup(&c.vc_keymap, "ru") >= 0);
|
||||
|
||||
assert_se(vconsole_convert_to_x11(&c) == 1);
|
||||
assert_se(streq(c.x11_layout, "ru,us"));
|
||||
assert_se(c.x11_variant == NULL);
|
||||
assert_se(streq(xc->layout, "ru,us"));
|
||||
assert_se(xc->variant == NULL);
|
||||
|
||||
log_info("/* test with a simple mapping (us) */");
|
||||
assert_se(free_and_strdup(&c.vc_keymap, "us") >= 0);
|
||||
|
||||
assert_se(vconsole_convert_to_x11(&c) == 1);
|
||||
assert_se(streq(c.x11_layout, "us"));
|
||||
assert_se(c.x11_variant == NULL);
|
||||
assert_se(streq(xc->layout, "us"));
|
||||
assert_se(xc->variant == NULL);
|
||||
}
|
||||
|
||||
TEST(x11_convert_to_vconsole) {
|
||||
_cleanup_(context_clear) Context c = {};
|
||||
X11Context *xc = &c.x11_from_xorg;
|
||||
int r;
|
||||
|
||||
log_info("/* test emptying first (:) */");
|
||||
@@ -126,19 +139,19 @@ TEST(x11_convert_to_vconsole) {
|
||||
assert_se(c.vc_keymap == NULL);
|
||||
|
||||
log_info("/* test without variant, new mapping (es:) */");
|
||||
assert_se(free_and_strdup(&c.x11_layout, "es") >= 0);
|
||||
assert_se(free_and_strdup(&xc->layout, "es") >= 0);
|
||||
|
||||
assert_se(x11_convert_to_vconsole(&c) == 1);
|
||||
assert_se(streq(c.vc_keymap, "es"));
|
||||
|
||||
log_info("/* test with unknown variant, new mapping (es:foobar) */");
|
||||
assert_se(free_and_strdup(&c.x11_variant, "foobar") >= 0);
|
||||
assert_se(free_and_strdup(&xc->variant, "foobar") >= 0);
|
||||
|
||||
assert_se(x11_convert_to_vconsole(&c) == 0);
|
||||
assert_se(streq(c.vc_keymap, "es"));
|
||||
|
||||
log_info("/* test with known variant, new mapping (es:dvorak) */");
|
||||
assert_se(free_and_strdup(&c.x11_variant, "dvorak") >= 0);
|
||||
assert_se(free_and_strdup(&xc->variant, "dvorak") >= 0);
|
||||
|
||||
r = x11_convert_to_vconsole(&c);
|
||||
if (r == 0) {
|
||||
@@ -150,30 +163,30 @@ TEST(x11_convert_to_vconsole) {
|
||||
assert_se(streq(c.vc_keymap, "es-dvorak"));
|
||||
|
||||
log_info("/* test with old mapping (fr:latin9) */");
|
||||
assert_se(free_and_strdup(&c.x11_layout, "fr") >= 0);
|
||||
assert_se(free_and_strdup(&c.x11_variant, "latin9") >= 0);
|
||||
assert_se(free_and_strdup(&xc->layout, "fr") >= 0);
|
||||
assert_se(free_and_strdup(&xc->variant, "latin9") >= 0);
|
||||
|
||||
assert_se(x11_convert_to_vconsole(&c) == 1);
|
||||
assert_se(streq(c.vc_keymap, "fr-latin9"));
|
||||
|
||||
log_info("/* test with a compound mapping (us,ru:) */");
|
||||
assert_se(free_and_strdup(&c.x11_layout, "us,ru") >= 0);
|
||||
assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
|
||||
assert_se(free_and_strdup(&xc->layout, "us,ru") >= 0);
|
||||
assert_se(free_and_strdup(&xc->variant, NULL) >= 0);
|
||||
|
||||
assert_se(x11_convert_to_vconsole(&c) == 1);
|
||||
assert_se(streq(c.vc_keymap, "us"));
|
||||
|
||||
log_info("/* test with a compound mapping (ru,us:) */");
|
||||
assert_se(free_and_strdup(&c.x11_layout, "ru,us") >= 0);
|
||||
assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
|
||||
assert_se(free_and_strdup(&xc->layout, "ru,us") >= 0);
|
||||
assert_se(free_and_strdup(&xc->variant, NULL) >= 0);
|
||||
|
||||
assert_se(x11_convert_to_vconsole(&c) == 1);
|
||||
assert_se(streq(c.vc_keymap, "ru"));
|
||||
|
||||
/* https://bugzilla.redhat.com/show_bug.cgi?id=1333998 */
|
||||
log_info("/* test with a simple new mapping (ru:) */");
|
||||
assert_se(free_and_strdup(&c.x11_layout, "ru") >= 0);
|
||||
assert_se(free_and_strdup(&c.x11_variant, NULL) >= 0);
|
||||
assert_se(free_and_strdup(&xc->layout, "ru") >= 0);
|
||||
assert_se(free_and_strdup(&xc->variant, NULL) >= 0);
|
||||
|
||||
assert_se(x11_convert_to_vconsole(&c) == 0);
|
||||
assert_se(streq(c.vc_keymap, "ru"));
|
||||
|
||||
Reference in New Issue
Block a user