#include "keymap.h"
#include "text.h"
-static struct xkb_keymap *
-xkb_keymap_new(struct xkb_context *ctx,
- enum xkb_keymap_format format,
- enum xkb_keymap_compile_flags flags)
-{
- struct xkb_keymap *keymap;
-
- keymap = calloc(1, sizeof(*keymap));
- if (!keymap)
- return NULL;
-
- keymap->refcnt = 1;
- keymap->ctx = xkb_context_ref(ctx);
-
- keymap->format = format;
- keymap->flags = flags;
-
- return keymap;
-}
-
XKB_EXPORT struct xkb_keymap *
xkb_keymap_ref(struct xkb_keymap *keymap)
{
XKB_EXPORT void
xkb_keymap_unref(struct xkb_keymap *keymap)
{
- unsigned int i, j;
- struct xkb_key *key;
-
if (!keymap || --keymap->refcnt > 0)
return;
if (keymap->keys) {
- xkb_foreach_key(key, keymap) {
- for (i = 0; i < key->num_groups; i++) {
- for (j = 0; j < XkbKeyGroupWidth(key, i); j++)
- if (key->groups[i].levels[j].num_syms > 1)
- free(key->groups[i].levels[j].u.syms);
- free(key->groups[i].levels);
+ struct xkb_key *key;
+ xkb_keys_foreach(key, keymap) {
+ if (key->groups) {
+ for (unsigned i = 0; i < key->num_groups; i++) {
+ if (key->groups[i].levels) {
+ for (unsigned j = 0; j < XkbKeyNumLevels(key, i); j++)
+ if (key->groups[i].levels[j].num_syms > 1)
+ free(key->groups[i].levels[j].u.syms);
+ free(key->groups[i].levels);
+ }
+ }
+ free(key->groups);
}
- free(key->groups);
}
free(keymap->keys);
}
- for (i = 0; i < keymap->num_types; i++) {
- free(keymap->types[i].entries);
- free(keymap->types[i].level_names);
+ if (keymap->types) {
+ for (unsigned i = 0; i < keymap->num_types; i++) {
+ free(keymap->types[i].entries);
+ free(keymap->types[i].level_names);
+ }
+ free(keymap->types);
}
- free(keymap->types);
free(keymap->sym_interprets);
free(keymap->key_aliases);
free(keymap->group_names);
- darray_free(keymap->mods);
- darray_free(keymap->leds);
free(keymap->keycodes_section_name);
free(keymap->symbols_section_name);
free(keymap->types_section_name);
[XKB_KEYMAP_FORMAT_TEXT_V1] = &text_v1_keymap_format_ops,
};
- if ((int) format < 0 || (int) format >= ARRAY_SIZE(keymap_format_ops))
+ if ((int) format < 0 || (int) format >= (int) ARRAY_SIZE(keymap_format_ops))
return NULL;
- return keymap_format_ops[format];
+ return keymap_format_ops[(int) format];
}
XKB_EXPORT struct xkb_keymap *
return NULL;
}
- if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
+ if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
log_err_func(ctx, "unrecognized flags: %#x\n", flags);
return NULL;
}
+ keymap = xkb_keymap_new(ctx, format, flags);
+ if (!keymap)
+ return NULL;
+
if (rmlvo_in)
rmlvo = *rmlvo_in;
else
memset(&rmlvo, 0, sizeof(rmlvo));
-
- if (isempty(rmlvo.rules))
- rmlvo.rules = xkb_context_get_default_rules(ctx);
- if (isempty(rmlvo.model))
- rmlvo.model = xkb_context_get_default_model(ctx);
- /* Layout and variant are tied together, so don't try to use one from
- * the caller and one from the environment. */
- if (isempty(rmlvo.layout)) {
- rmlvo.layout = xkb_context_get_default_layout(ctx);
- rmlvo.variant = xkb_context_get_default_variant(ctx);
- }
- /* Options can be empty, so respect that if passed in. */
- if (rmlvo.options == NULL)
- rmlvo.options = xkb_context_get_default_options(ctx);
-
- keymap = xkb_keymap_new(ctx, format, flags);
- if (!keymap)
- return NULL;
+ xkb_context_sanitize_rule_names(ctx, &rmlvo);
if (!ops->keymap_new_from_names(keymap, &rmlvo)) {
xkb_keymap_unref(keymap);
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags)
{
- return xkb_keymap_new_from_buffer(ctx, string, SIZE_MAX, format, flags);
+ return xkb_keymap_new_from_buffer(ctx, string, strlen(string),
+ format, flags);
}
XKB_EXPORT struct xkb_keymap *
return NULL;
}
- if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
+ if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
log_err_func(ctx, "unrecognized flags: %#x\n", flags);
return NULL;
}
return NULL;
}
- if (flags & ~(XKB_MAP_COMPILE_PLACEHOLDER)) {
+ if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
log_err_func(ctx, "unrecognized flags: %#x\n", flags);
return NULL;
}
XKB_EXPORT xkb_mod_index_t
xkb_keymap_num_mods(struct xkb_keymap *keymap)
{
- return darray_size(keymap->mods);
+ return keymap->mods.num_mods;
}
/**
XKB_EXPORT const char *
xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
{
- if (idx >= darray_size(keymap->mods))
+ if (idx >= keymap->mods.num_mods)
return NULL;
- return xkb_atom_text(keymap->ctx, darray_item(keymap->mods, idx).name);
+ return xkb_atom_text(keymap->ctx, keymap->mods.mods[idx].name);
}
/**
XKB_EXPORT xkb_mod_index_t
xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
{
- xkb_mod_index_t i;
xkb_atom_t atom;
- const struct xkb_mod *mod;
atom = xkb_atom_lookup(keymap->ctx, name);
if (atom == XKB_ATOM_NONE)
return XKB_MOD_INVALID;
- darray_enumerate(i, mod, keymap->mods)
- if (mod->name == atom)
- return i;
-
- return XKB_MOD_INVALID;
+ return XkbModNameToIndex(&keymap->mods, atom, MOD_BOTH);
}
/**
if (!key)
return 0;
- layout = wrap_group_into_range(layout, key->num_groups,
+ layout = XkbWrapGroupIntoRange(layout, key->num_groups,
key->out_of_range_group_action,
key->out_of_range_group_number);
if (layout == XKB_LAYOUT_INVALID)
return 0;
- return XkbKeyGroupWidth(key, layout);
+ return XkbKeyNumLevels(key, layout);
}
/**
XKB_EXPORT xkb_led_index_t
xkb_keymap_num_leds(struct xkb_keymap *keymap)
{
- return darray_size(keymap->leds);
+ return keymap->num_leds;
}
/**
XKB_EXPORT const char *
xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
{
- if (idx >= darray_size(keymap->leds))
+ if (idx >= keymap->num_leds)
return NULL;
- return xkb_atom_text(keymap->ctx, darray_item(keymap->leds, idx).name);
+ return xkb_atom_text(keymap->ctx, keymap->leds[idx].name);
}
/**
if (atom == XKB_ATOM_NONE)
return XKB_LED_INVALID;
- darray_enumerate(i, led, keymap->leds)
+ xkb_leds_enumerate(i, led, keymap)
if (led->name == atom)
return i;
if (!key)
goto err;
- layout = wrap_group_into_range(layout, key->num_groups,
+ layout = XkbWrapGroupIntoRange(layout, key->num_groups,
key->out_of_range_group_action,
key->out_of_range_group_number);
if (layout == XKB_LAYOUT_INVALID)
goto err;
- if (level >= XkbKeyGroupWidth(key, layout))
+ if (level >= XkbKeyNumLevels(key, layout))
goto err;
num_syms = key->groups[layout].levels[level].num_syms;
{
struct xkb_key *key;
- xkb_foreach_key(key, keymap)
+ xkb_keys_foreach(key, keymap)
iter(keymap, key->keycode, data);
}
-/**
- * Simple boolean specifying whether or not the key should repeat.
- */
-XKB_EXPORT int
-xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
+XKB_EXPORT const char *
+xkb_keymap_key_get_name(struct xkb_keymap *keymap, xkb_keycode_t kc)
{
const struct xkb_key *key = XkbKey(keymap, kc);
if (!key)
- return 0;
+ return NULL;
- return key->repeats;
+ return xkb_atom_text(keymap->ctx, key->name);
}
-struct xkb_key *
-XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
+XKB_EXPORT xkb_keycode_t
+xkb_keymap_key_by_name(struct xkb_keymap *keymap, const char *name)
{
struct xkb_key *key;
+ xkb_atom_t atom;
- xkb_foreach_key(key, keymap)
- if (key->name == name)
- return key;
+ atom = xkb_atom_lookup(keymap->ctx, name);
+ if (atom) {
+ xkb_atom_t ratom = XkbResolveKeyAlias(keymap, atom);
+ if (ratom)
+ atom = ratom;
+ }
+ if (!atom)
+ return XKB_KEYCODE_INVALID;
- if (use_aliases) {
- xkb_atom_t new_name = XkbResolveKeyAlias(keymap, name);
- if (new_name != XKB_ATOM_NONE)
- return XkbKeyByName(keymap, new_name, false);
+ xkb_keys_foreach(key, keymap) {
+ if (key->name == atom)
+ return key->keycode;
}
- return NULL;
+ return XKB_KEYCODE_INVALID;
}
-xkb_atom_t
-XkbResolveKeyAlias(struct xkb_keymap *keymap, xkb_atom_t name)
+/**
+ * Simple boolean specifying whether or not the key should repeat.
+ */
+XKB_EXPORT int
+xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
{
- for (unsigned i = 0; i < keymap->num_key_aliases; i++)
- if (keymap->key_aliases[i].alias == name)
- return keymap->key_aliases[i].real;
+ const struct xkb_key *key = XkbKey(keymap, kc);
+
+ if (!key)
+ return 0;
- return XKB_ATOM_NONE;
+ return key->repeats;
}