Use only one set of core mod name-to-index functions These were repeated 5 times. Note that this changes the ABI slightly: XKB_MOD_NAME_CAPS is changed from "Caps Lock" to "Lock", which is the ordinary legacy mod name for it. Since its hidden behind a #define, it's best to stay compatible with the old names (as I think was intended, given that "Mod1", etc. are the same). 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
diff --git a/src/keymap-dump.c b/src/keymap-dump.c
index 2186066..01f7572 100644
--- a/src/keymap-dump.c
+++ b/src/keymap-dump.c
@@ -160,24 +160,6 @@ write_vmods(struct xkb_keymap *keymap, struct buf *buf)
return NULL; \
} while (0)
-/* FIXME: Merge with src/xkbcomp/expr.c::modIndexNames. */
-static const char *core_mod_names[] = {
- "Shift",
- "Lock",
- "Control",
- "Mod1",
- "Mod2",
- "Mod3",
- "Mod4",
- "Mod5",
-};
-
-static const char *
-get_mod_index_text(uint8_t real_mod)
-{
- return core_mod_names[real_mod];
-}
-
static char *
get_mod_mask_text(struct xkb_keymap *keymap, uint8_t real_mods,
uint32_t vmods)
@@ -204,10 +186,10 @@ get_mod_mask_text(struct xkb_keymap *keymap, uint8_t real_mods,
continue;
if (ret[0] != '\0') {
strcpy(ret2, ret);
- append_get_text("%s+%s", ret2, core_mod_names[i]);
+ append_get_text("%s+%s", ret2, ModIndexToName(i));
}
else {
- append_get_text("%s", core_mod_names[i]);
+ append_get_text("%s", ModIndexToName(i));
}
}
}
@@ -848,7 +830,7 @@ write_symbols(struct xkb_keymap *keymap, struct buf *buf)
continue;
write_buf(buf, "\t\tmodifier_map %s { %s };\n",
- get_mod_index_text(mod), KeyNameText(key->name));
+ ModIndexToName(mod), KeyNameText(key->name));
}
}
diff --git a/src/map.c b/src/map.c
index dc96287..1108a81 100644
--- a/src/map.c
+++ b/src/map.c
@@ -50,6 +50,7 @@
* ********************************************************/
#include "xkb-priv.h"
+#include "text.h"
/**
* Returns the total number of modifiers active in the keymap.
@@ -74,33 +75,18 @@ xkb_map_num_mods(struct xkb_keymap *keymap)
XKB_EXPORT const char *
xkb_map_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
{
+ const char *name;
+
if (idx >= xkb_map_num_mods(keymap))
return NULL;
- /* First try to find a legacy modifier name. */
- switch (idx) {
- case ShiftMapIndex:
- return "Shift";
- case ControlMapIndex:
- return "Control";
- case LockMapIndex:
- return "Caps Lock";
- case Mod1MapIndex:
- return "Mod1";
- case Mod2MapIndex:
- return "Mod2";
- case Mod3MapIndex:
- return "Mod3";
- case Mod4MapIndex:
- return "Mod4";
- case Mod5MapIndex:
- return "Mod5";
- default:
- break;
- }
+ /* First try to find a legacy modifier name. If that fails, try to
+ * find a virtual mod name. */
+ name = ModIndexToName(idx);
+ if (!name)
+ name = keymap->vmod_names[idx - XkbNumModifiers];
- /* If that fails, try to find a virtual mod name. */
- return keymap->vmod_names[idx - XkbNumModifiers];
+ return name;
}
/**
@@ -111,22 +97,9 @@ xkb_map_mod_get_index(struct xkb_keymap *keymap, const char *name)
{
xkb_mod_index_t i;
- if (istreq(name, "Shift"))
- return ShiftMapIndex;
- if (istreq(name, "Control"))
- return ControlMapIndex;
- if (istreq(name, "Caps Lock"))
- return LockMapIndex;
- if (istreq(name, "Mod1"))
- return Mod1MapIndex;
- if (istreq(name, "Mod2"))
- return Mod2MapIndex;
- if (istreq(name, "Mod3"))
- return Mod3MapIndex;
- if (istreq(name, "Mod4"))
- return Mod4MapIndex;
- if (istreq(name, "Mod5"))
- return Mod5MapIndex;
+ i = ModNameToIndex(name);
+ if (i != XKB_MOD_INVALID)
+ return i;
for (i = 0; i < XkbNumVirtualMods && keymap->vmod_names[i]; i++) {
if (istreq(name, keymap->vmod_names[i]))
diff --git a/src/text.c b/src/text.c
index 022d284..5286ba9 100644
--- a/src/text.c
+++ b/src/text.c
@@ -129,24 +129,47 @@ VModMaskText(struct xkb_keymap *keymap, xkb_mod_mask_t modMask,
}
static const char *modNames[XkbNumModifiers] = {
- "Shift",
- "Lock",
- "Control",
- "Mod1",
- "Mod2",
- "Mod3",
- "Mod4",
- "Mod5"
+ [ShiftMapIndex] = "Shift",
+ [LockMapIndex] = "Lock",
+ [ControlMapIndex] = "Control",
+ [Mod1MapIndex] = "Mod1",
+ [Mod2MapIndex] = "Mod2",
+ [Mod3MapIndex] = "Mod3",
+ [Mod4MapIndex] = "Mod4",
+ [Mod5MapIndex] = "Mod5",
};
+xkb_mod_index_t
+ModNameToIndex(const char *name)
+{
+ xkb_mod_index_t i;
+
+ for (i = 0; i < XkbNumModifiers; i++)
+ if (istreq(name, modNames[i]))
+ return i;
+
+ return XKB_MOD_INVALID;
+}
+
+const char *
+ModIndexToName(xkb_mod_index_t ndx)
+{
+ if (ndx < XkbNumModifiers)
+ return modNames[ndx];
+ return NULL;
+}
+
const char *
ModIndexText(xkb_mod_index_t ndx)
{
+ const char *name;
char *buf;
- if (ndx < XkbNumModifiers)
- return modNames[ndx];
- else if (ndx == XkbNoModifier)
+ name = ModIndexToName(ndx);
+ if (name)
+ return name;
+
+ if (ndx == XkbNoModifier)
return "none";
buf = GetBuffer(32);
diff --git a/src/text.h b/src/text.h
index 9cff03c..ed677b9 100644
--- a/src/text.h
+++ b/src/text.h
@@ -33,6 +33,12 @@ const char *
VModMaskText(struct xkb_keymap *keymap, xkb_mod_mask_t modMask,
xkb_mod_mask_t mask);
+xkb_mod_index_t
+ModNameToIndex(const char *name);
+
+const char *
+ModIndexToName(xkb_mod_index_t ndx);
+
const char *
ModIndexText(xkb_mod_index_t ndx);
diff --git a/src/xkbcomp/expr.c b/src/xkbcomp/expr.c
index 6a02c2a..3f2ee89 100644
--- a/src/xkbcomp/expr.c
+++ b/src/xkbcomp/expr.c
@@ -170,24 +170,19 @@ SimpleLookup(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
return false;
}
-static const LookupEntry modIndexNames[] = {
- { "shift", ShiftMapIndex },
- { "control", ControlMapIndex },
- { "lock", LockMapIndex },
- { "mod1", Mod1MapIndex },
- { "mod2", Mod2MapIndex },
- { "mod3", Mod3MapIndex },
- { "mod4", Mod4MapIndex },
- { "mod5", Mod5MapIndex },
- { "none", XkbNoModifier },
- { NULL, 0 }
-};
-
bool
LookupModIndex(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
enum expr_value_type type, xkb_mod_index_t *val_rtrn)
{
- return SimpleLookup(ctx, modIndexNames, field, type, val_rtrn);
+ const char *name = xkb_atom_text(ctx, field);
+
+ if (istreq(name, "none")) {
+ *val_rtrn = XkbNoModifier;
+ return true;
+ }
+
+ *val_rtrn = ModNameToIndex(name);
+ return (*val_rtrn != XKB_MOD_INVALID);
}
bool
diff --git a/xkbcommon/xkbcommon-names.h b/xkbcommon/xkbcommon-names.h
index c302a35..8034f3d 100644
--- a/xkbcommon/xkbcommon-names.h
+++ b/xkbcommon/xkbcommon-names.h
@@ -27,7 +27,7 @@
#define _XKBCOMMON_NAMES_H
#define XKB_MOD_NAME_SHIFT "Shift"
-#define XKB_MOD_NAME_CAPS "Caps Lock"
+#define XKB_MOD_NAME_CAPS "Lock"
#define XKB_MOD_NAME_CTRL "Control"
#define XKB_MOD_NAME_ALT "Mod1"
#define XKB_MOD_NAME_LOGO "Mod4"