keymap: don't use darray for key aliases With a little tweak to the copy-to-keymap routine in keycodes.c we can use a normal array. Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
diff --git a/src/keymap.c b/src/keymap.c
index 758a35b..480a4ce 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -107,7 +107,7 @@ xkb_keymap_unref(struct xkb_keymap *keymap)
}
free(keymap->types);
darray_free(keymap->sym_interprets);
- darray_free(keymap->key_aliases);
+ free(keymap->key_aliases);
free(keymap->group_names);
darray_free(keymap->mods);
darray_free(keymap->leds);
@@ -499,11 +499,9 @@ XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases)
xkb_atom_t
XkbResolveKeyAlias(struct xkb_keymap *keymap, xkb_atom_t name)
{
- const struct xkb_key_alias *alias;
-
- darray_foreach(alias, keymap->key_aliases)
- if (name == alias->alias)
- return alias->real;
+ for (unsigned i = 0; i < keymap->num_key_aliases; i++)
+ if (keymap->key_aliases[i].alias == name)
+ return keymap->key_aliases[i].real;
return XKB_ATOM_NONE;
}
diff --git a/src/keymap.h b/src/keymap.h
index f4b80eb..0f3d6bb 100644
--- a/src/keymap.h
+++ b/src/keymap.h
@@ -380,7 +380,8 @@ struct xkb_keymap {
struct xkb_key *keys;
/* aliases in no particular order */
- darray(struct xkb_key_alias) key_aliases;
+ unsigned int num_key_aliases;
+ struct xkb_key_alias *key_aliases;
struct xkb_key_type *types;
unsigned int num_types;
diff --git a/src/xkbcomp/keycodes.c b/src/xkbcomp/keycodes.c
index 53e3fff..d7d475e 100644
--- a/src/xkbcomp/keycodes.c
+++ b/src/xkbcomp/keycodes.c
@@ -90,7 +90,8 @@
* following members of struct xkb_keymap are finalized:
* xkb_keycode_t min_key_code;
* xkb_keycode_t max_key_code;
- * darray(struct xkb_key_alias) key_aliases;
+ * unsigned int num_aliases;
+ * struct xkb_key_alias *key_aliases;
* char *keycodes_section_name;
* The 'name' field of leds declared in xkb_keycodes:
* darray(struct xkb_led) leds;
@@ -617,75 +618,74 @@ HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file, enum merge_mode merge)
/***====================================================================***/
-static void
-CopyAliasesToKeymap(KeyNamesInfo *info, struct xkb_keymap *keymap)
+static bool
+CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
{
+ xkb_keycode_t kc;
+ xkb_led_index_t idx;
+ LedNameInfo *ledi;
AliasInfo *alias;
+ unsigned i;
- darray_foreach(alias, info->aliases) {
- struct xkb_key *key;
- struct xkb_key_alias new;
+ keymap->keycodes_section_name = strdup_safe(info->name);
+
+ keymap->min_key_code = info->min_key_code;
+ keymap->max_key_code = info->max_key_code;
+ /* Copy key names. */
+ keymap->keys = calloc(info->max_key_code + 1, sizeof(*keymap->keys));
+ for (kc = info->min_key_code; kc <= info->max_key_code; kc++) {
+ keymap->keys[kc].keycode = kc;
+ keymap->keys[kc].name = darray_item(info->key_names, kc).name;
+ }
+
+ /*
+ * Do some sanity checking on the aliases. We can't do it before
+ * because keys and their aliases may be added out-of-order.
+ */
+ keymap->num_key_aliases = 0;
+ darray_foreach(alias, info->aliases) {
/* Check that ->real is a key. */
- key = XkbKeyByName(keymap, alias->real, false);
- if (!key) {
+ if (!XkbKeyByName(keymap, alias->real, false)) {
log_vrb(info->ctx, 5,
"Attempt to alias %s to non-existent key %s; Ignored\n",
KeyNameText(info->ctx, alias->alias),
KeyNameText(info->ctx, alias->real));
+ alias->real = XKB_ATOM_NONE;
continue;
}
/* Check that ->alias is not a key. */
- key = XkbKeyByName(keymap, alias->alias, false);
- if (key) {
+ if (XkbKeyByName(keymap, alias->alias, false)) {
log_vrb(info->ctx, 5,
"Attempt to create alias with the name of a real key; "
"Alias \"%s = %s\" ignored\n",
KeyNameText(info->ctx, alias->alias),
KeyNameText(info->ctx, alias->real));
+ alias->real = XKB_ATOM_NONE;
continue;
}
- /* Add the alias. */
- new.alias = alias->alias;
- new.real = alias->real;
- darray_append(keymap->key_aliases, new);
+ keymap->num_key_aliases++;
}
- darray_free(info->aliases);
-}
-
-static bool
-CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info)
-{
- xkb_keycode_t kc;
- xkb_led_index_t idx;
- LedNameInfo *ledi;
-
- keymap->keys = calloc(info->max_key_code + 1, sizeof(*keymap->keys));
- if (!keymap->keys)
- return false;
-
- keymap->min_key_code = info->min_key_code;
- keymap->max_key_code = info->max_key_code;
-
- for (kc = info->min_key_code; kc <= info->max_key_code; kc++) {
- keymap->keys[kc].keycode = kc;
- keymap->keys[kc].name = darray_item(info->key_names, kc).name;
+ /* Copy key aliases. */
+ keymap->key_aliases = calloc(keymap->num_key_aliases,
+ sizeof(*keymap->key_aliases));
+ i = 0;
+ darray_foreach(alias, info->aliases) {
+ if (alias->real != XKB_ATOM_NONE) {
+ keymap->key_aliases[i].alias = alias->alias;
+ keymap->key_aliases[i].real = alias->real;
+ i++;
+ }
}
- keymap->keycodes_section_name = strdup_safe(info->name);
-
+ /* Copy LED names. */
darray_resize0(keymap->leds, darray_size(info->led_names));
- darray_enumerate(idx, ledi, info->led_names) {
- if (ledi->name == XKB_ATOM_NONE)
- continue;
-
- darray_item(keymap->leds, idx).name = ledi->name;
- }
-
- CopyAliasesToKeymap(info, keymap);
+ darray_enumerate(idx, ledi, info->led_names)
+ if (ledi->name != XKB_ATOM_NONE)
+ darray_item(keymap->leds, idx).name = ledi->name;
return true;
}
diff --git a/src/xkbcomp/keymap-dump.c b/src/xkbcomp/keymap-dump.c
index 5ca2a10..5c57e17 100644
--- a/src/xkbcomp/keymap-dump.c
+++ b/src/xkbcomp/keymap-dump.c
@@ -148,8 +148,7 @@ static bool
write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
{
const struct xkb_key *key;
- const struct xkb_key_alias *alias;
- xkb_led_index_t i;
+ xkb_led_index_t idx;
const struct xkb_led *led;
if (keymap->keycodes_section_name)
@@ -166,16 +165,16 @@ write_keycodes(struct xkb_keymap *keymap, struct buf *buf)
KeyNameText(keymap->ctx, key->name), key->keycode);
}
- darray_enumerate(i, led, keymap->leds)
+ darray_enumerate(idx, led, keymap->leds)
if (led->name != XKB_ATOM_NONE)
write_buf(buf, "\tindicator %d = \"%s\";\n",
- i + 1, xkb_atom_text(keymap->ctx, led->name));
+ idx + 1, xkb_atom_text(keymap->ctx, led->name));
- darray_foreach(alias, keymap->key_aliases)
+ for (unsigned i = 0; i < keymap->num_key_aliases; i++)
write_buf(buf, "\talias %-14s = %s;\n",
- KeyNameText(keymap->ctx, alias->alias),
- KeyNameText(keymap->ctx, alias->real));
+ KeyNameText(keymap->ctx, keymap->key_aliases[i].alias),
+ KeyNameText(keymap->ctx, keymap->key_aliases[i].real));
write_buf(buf, "};\n\n");
return true;