keytypes: use darray for level names 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
diff --git a/src/xkbcomp/keytypes.c b/src/xkbcomp/keytypes.c
index 1e15812..bd6354f 100644
--- a/src/xkbcomp/keytypes.c
+++ b/src/xkbcomp/keytypes.c
@@ -55,8 +55,7 @@ typedef struct _KeyTypeInfo
unsigned numLevels;
darray(struct xkb_kt_map_entry) entries;
PreserveInfo *preserve;
- unsigned szNames;
- xkb_atom_t *lvlNames;
+ darray(xkb_atom_t) lvlNames;
} KeyTypeInfo;
typedef struct _KeyTypesInfo
@@ -121,8 +120,7 @@ InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap,
info->dflt.groupInfo = false;
info->dflt.numLevels = 1;
darray_init(info->dflt.entries);
- info->dflt.szNames = 0;
- info->dflt.lvlNames = NULL;
+ darray_init(info->dflt.lvlNames);
info->dflt.preserve = NULL;
InitVModInfo(&info->vmods, keymap);
if (from != NULL)
@@ -134,15 +132,11 @@ InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap,
&darray_item(from->dflt.entries, 0),
darray_size(from->dflt.entries));
- if (from->dflt.lvlNames)
- {
- info->dflt.lvlNames = uTypedCalloc(from->dflt.szNames, xkb_atom_t);
- if (info->dflt.lvlNames)
- {
- unsigned sz = from->dflt.szNames * sizeof(xkb_atom_t);
- memcpy(info->dflt.lvlNames, from->dflt.lvlNames, sz);
- }
- }
+ darray_init(info->dflt.lvlNames);
+ darray_from_items(info->dflt.lvlNames,
+ &darray_item(from->dflt.lvlNames, 0),
+ darray_size(from->dflt.lvlNames));
+
if (from->dflt.preserve)
{
PreserveInfo *old, *new, *last;
@@ -170,8 +164,8 @@ FreeKeyTypeInfo(KeyTypeInfo * type)
{
darray_free(type->entries);
darray_init(type->entries);
- free(type->lvlNames);
- type->lvlNames = NULL;
+ darray_free(type->lvlNames);
+ darray_init(type->lvlNames);
if (type->preserve != NULL)
{
ClearCommonInfo(&type->preserve->defs);
@@ -288,8 +282,8 @@ AddKeyType(struct xkb_keymap *keymap, KeyTypesInfo *info, KeyTypeInfo *new)
FreeKeyTypeInfo(old);
*old = *new;
darray_init(new->entries);
+ darray_init(new->lvlNames);
new->preserve = NULL;
- new->lvlNames = NULL;
old->defs.next = &next->defs;
return true;
}
@@ -309,8 +303,7 @@ AddKeyType(struct xkb_keymap *keymap, KeyTypesInfo *info, KeyTypeInfo *new)
*old = *new;
old->defs.next = NULL;
darray_init(new->entries);
- new->szNames = 0;
- new->lvlNames = NULL;
+ darray_init(new->lvlNames);
new->preserve = NULL;
return true;
}
@@ -690,50 +683,36 @@ static bool
AddLevelName(struct xkb_keymap *keymap, KeyTypeInfo *type,
unsigned level, xkb_atom_t name, bool clobber)
{
- if ((type->lvlNames == NULL) || (type->szNames <= level))
- {
- type->lvlNames =
- uTypedRecalloc(type->lvlNames, type->szNames, level + 1, xkb_atom_t);
- if (type->lvlNames == NULL)
- {
- ERROR("Couldn't allocate level names for type %s\n",
- TypeTxt(keymap, type));
- ACTION("Level names lost\n");
- type->szNames = 0;
- return false;
- }
- type->szNames = level + 1;
- }
- else if (type->lvlNames[level] == name)
- {
- if (warningLevel > 9)
- {
+ if (level >= darray_size(type->lvlNames))
+ darray_resize0(type->lvlNames, level + 1);
+
+ if (darray_item(type->lvlNames, level) == name) {
+ if (warningLevel > 9) {
WARN("Duplicate names for level %d of key type %s\n",
- level + 1, TypeTxt(keymap, type));
+ level + 1, TypeTxt(keymap, type));
ACTION("Ignored\n");
}
return true;
}
- else if (type->lvlNames[level] != XKB_ATOM_NONE)
- {
- if (warningLevel > 0)
- {
+ else if (darray_item(type->lvlNames, level) != XKB_ATOM_NONE) {
+ if (warningLevel > 0) {
const char *old, *new;
- old = xkb_atom_text(keymap->ctx, type->lvlNames[level]);
+ old = xkb_atom_text(keymap->ctx,
+ darray_item(type->lvlNames, level));
new = xkb_atom_text(keymap->ctx, name);
WARN("Multiple names for level %d of key type %s\n",
- level + 1, TypeTxt(keymap, type));
+ level + 1, TypeTxt(keymap, type));
if (clobber)
ACTION("Using %s, ignoring %s\n", new, old);
else
ACTION("Using %s, ignoring %s\n", old, new);
}
+
if (!clobber)
return true;
}
- if (level >= type->numLevels)
- type->numLevels = level + 1;
- type->lvlNames[level] = name;
+
+ darray_item(type->lvlNames, level) = name;
return true;
}
@@ -902,8 +881,7 @@ HandleKeyTypeDef(KeyTypeDef *def, struct xkb_keymap *keymap,
type.groupInfo = info->dflt.groupInfo;
type.numLevels = 1;
darray_init(type.entries);
- type.szNames = 0;
- type.lvlNames = NULL;
+ darray_init(type.lvlNames);
type.preserve = NULL;
/* Parse the actual content. */
@@ -933,13 +911,16 @@ HandleKeyTypeDef(KeyTypeDef *def, struct xkb_keymap *keymap,
dflt = (PreserveInfo *) dflt->defs.next;
}
}
- for (i = 0; i < info->dflt.szNames; i++)
- {
- if ((i < type.numLevels) && (info->dflt.lvlNames[i] != XKB_ATOM_NONE))
+
+ for (i = 0; i < darray_size(info->dflt.lvlNames); i++) {
+ if (i < type.numLevels &&
+ darray_item(info->dflt.lvlNames, i) != XKB_ATOM_NONE)
{
- AddLevelName(keymap, &type, i, info->dflt.lvlNames[i], false);
+ AddLevelName(keymap, &type, i,
+ darray_item(info->dflt.lvlNames, i), false);
}
}
+
/* Now add the new keytype to the info struct */
if (!AddKeyType(keymap, info, &type))
{
@@ -1070,19 +1051,17 @@ CopyDefToKeyType(struct xkb_keymap *keymap, struct xkb_key_type *type,
else
type->preserve = NULL;
type->name = xkb_atom_strdup(keymap->ctx, def->name);
- if (def->szNames > 0)
- {
- type->level_names = uTypedCalloc(def->numLevels, const char *);
+
+ if (!darray_empty(def->lvlNames)) {
+ type->level_names = uTypedCalloc(darray_size(def->lvlNames),
+ const char *);
/* assert def->szNames<=def->numLevels */
- for (i = 0; i < def->szNames; i++)
- {
- type->level_names[i] = xkb_atom_strdup(keymap->ctx,
- def->lvlNames[i]);
- }
+ for (i = 0; i < darray_size(def->lvlNames); i++)
+ type->level_names[i] =
+ xkb_atom_strdup(keymap->ctx, darray_item(def->lvlNames, i));
}
- else
- {
+ else {
type->level_names = NULL;
}