Commit 7c7e43415128229c97b2f9efa756c485774caa3b

Ran Benita 2012-08-01T11:25:34

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>

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"