Use only one set of core mod name-to-index functions
authorRan Benita <ran234@gmail.com>
Wed, 1 Aug 2012 08:25:34 +0000 (11:25 +0300)
committerRan Benita <ran234@gmail.com>
Tue, 7 Aug 2012 08:09:42 +0000 (11:09 +0300)
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>
src/keymap-dump.c
src/map.c
src/text.c
src/text.h
src/xkbcomp/expr.c
xkbcommon/xkbcommon-names.h

index 2186066..01f7572 100644 (file)
@@ -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));
         }
     }
 
index dc96287..1108a81 100644 (file)
--- 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]))
index 022d284..5286ba9 100644 (file)
@@ -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);
index 9cff03c..ed677b9 100644 (file)
@@ -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);
 
index 6a02c2a..3f2ee89 100644 (file)
@@ -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
index c302a35..8034f3d 100644 (file)
@@ -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"