text: share code for modifiers Add static common functions which take enum mod_type, and change the existing ones to use them. 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
diff --git a/src/text.c b/src/text.c
index eea3059..843c945 100644
--- a/src/text.c
+++ b/src/text.c
@@ -231,36 +231,32 @@ GetBuffer(size_t size)
return rtrn;
}
-/* Get a mod mask's text, where the mask is in rmods+vmods format. */
-const char *
-VModMaskText(struct xkb_keymap *keymap, xkb_mod_mask_t mask)
+static const char *
+mod_mask_text(const struct xkb_keymap *keymap, xkb_mod_mask_t mask,
+ enum mod_type type)
{
xkb_mod_index_t i;
xkb_mod_mask_t rmask, vmask;
- int len, rem;
- const char *mm = NULL;
- char *rtrn, *str;
+ size_t len;
+ ssize_t rem;
+ char *str;
char buf[BUFFER_SIZE];
const struct xkb_mod *mod;
- rmask = mask & 0xff;
- vmask = mask & (~0xff);
+ rmask = (type & MOD_REAL ? mask : 0) & 0xff;
+ vmask = (type & MOD_VIRT ? mask : 0) & (~0xff);
if (rmask == 0 && vmask == 0)
return "none";
- if (rmask != 0)
- mm = ModMaskText(keymap, rmask);
-
- if (vmask == 0)
- return mm;
+ if (rmask == 0xff && vmask == 0)
+ return "all";
str = buf;
buf[0] = '\0';
rem = BUFFER_SIZE;
-
darray_enumerate(i, mod, keymap->mods) {
- if (mod->type != MOD_VIRT || !(vmask & (1 << i)))
+ if (!(mod->type & type) || !(mask & (1 << i)))
continue;
len = snprintf(str, rem, "%s%s",
@@ -272,94 +268,85 @@ VModMaskText(struct xkb_keymap *keymap, xkb_mod_mask_t mask)
if (rem <= 1)
break;
}
-
str = buf;
- len = (str ? strlen(str) : 0) + (mm ? strlen(mm) : 0) +
- (str && mm ? 1 : 0);
+ len = strlen(str);
if (len >= BUFFER_SIZE)
len = BUFFER_SIZE - 1;
- rtrn = GetBuffer(len + 1);
- rtrn[0] = '\0';
+ return strcpy(GetBuffer(len + 1), str);
- snprintf(rtrn, len + 1, "%s%s%s", (mm ? mm : ""),
- (mm && str ? "+" : ""), (str ? str : ""));
+}
- return rtrn;
+const char *
+ModMaskText(const struct xkb_keymap *keymap, xkb_mod_mask_t mask)
+{
+ return mod_mask_text(keymap, mask, MOD_REAL);
}
-xkb_mod_index_t
-ModNameToIndex(const struct xkb_keymap *keymap, xkb_atom_t name)
+const char *
+VModMaskText(const struct xkb_keymap *keymap, xkb_mod_mask_t mask)
+{
+ return mod_mask_text(keymap, mask, MOD_BOTH);
+}
+
+static xkb_mod_index_t
+mod_name_to_index(const struct xkb_keymap *keymap, xkb_atom_t name,
+ enum mod_type type)
{
xkb_mod_index_t i;
const struct xkb_mod *mod;
darray_enumerate(i, mod, keymap->mods)
- if (mod->type == MOD_REAL && name == mod->name)
+ if ((mod->type & type) && name == mod->name)
return i;
return XKB_MOD_INVALID;
}
-xkb_atom_t
-ModIndexToName(struct xkb_keymap *keymap, xkb_mod_index_t ndx)
+xkb_mod_index_t
+ModNameToIndex(const struct xkb_keymap *keymap, xkb_atom_t name)
+{
+ return mod_name_to_index(keymap, name, MOD_REAL);
+}
+
+static xkb_mod_index_t
+mod_index_to_name(const struct xkb_keymap *keymap, xkb_mod_index_t ndx,
+ enum mod_type type)
{
if (ndx >= darray_size(keymap->mods) ||
- darray_item(keymap->mods, ndx).type != MOD_REAL)
+ !(darray_item(keymap->mods, ndx).type & type))
return XKB_ATOM_NONE;
return darray_item(keymap->mods, ndx).name;
}
-const char *
-ModIndexText(struct xkb_keymap *keymap, xkb_mod_index_t ndx)
+xkb_atom_t
+ModIndexToName(const struct xkb_keymap *keymap, xkb_mod_index_t ndx)
+{
+ return mod_index_to_name(keymap, ndx, MOD_REAL);
+}
+
+static const char *
+mod_index_text(const struct xkb_keymap *keymap, xkb_mod_index_t ndx,
+ enum mod_type type)
{
xkb_atom_t name;
- name = ModIndexToName(keymap, ndx);
+ name = mod_index_to_name(keymap, ndx, type);
if (name)
return xkb_atom_text(keymap->ctx, name);
if (ndx == XKB_MOD_INVALID)
return "none";
- return "illegal";
+ return NULL;
}
const char *
-ModMaskText(struct xkb_keymap *keymap, xkb_mod_mask_t mask)
+ModIndexText(const struct xkb_keymap *keymap, xkb_mod_index_t ndx)
{
- xkb_mod_index_t i;
- int len, rem;
- char *str, *buf;
- const struct xkb_mod *mod;
-
- if ((mask & 0xff) == 0xff)
- return "all";
-
- if ((mask & 0xff) == 0)
- return "none";
-
- rem = 64;
- buf = GetBuffer(rem);
- str = buf;
- buf[0] = '\0';
- darray_enumerate(i, mod, keymap->mods) {
- if (mod->type != MOD_REAL || !(mask & (1 << i)))
- continue;
-
- len = snprintf(str, rem, "%s%s",
- (str != buf ? "+" : ""),
- ModIndexText(keymap, i));
- rem -= len;
- str += len;
-
- if (rem <= 1)
- break;
- }
-
- return buf;
+ return mod_index_text(keymap, ndx, MOD_REAL);
}
const char *
diff --git a/src/text.h b/src/text.h
index 5c257d1..ee6acf0 100644
--- a/src/text.h
+++ b/src/text.h
@@ -48,19 +48,19 @@ extern const LookupEntry actionTypeNames[];
extern const LookupEntry symInterpretMatchMaskNames[];
const char *
-VModMaskText(struct xkb_keymap *keymap, xkb_mod_mask_t mask);
+VModMaskText(const struct xkb_keymap *keymap, xkb_mod_mask_t mask);
xkb_mod_index_t
ModNameToIndex(const struct xkb_keymap *keymap, xkb_atom_t name);
xkb_atom_t
-ModIndexToName(struct xkb_keymap *keymap, xkb_mod_index_t ndx);
+ModIndexToName(const struct xkb_keymap *keymap, xkb_mod_index_t ndx);
const char *
-ModIndexText(struct xkb_keymap *keymap, xkb_mod_index_t ndx);
+ModIndexText(const struct xkb_keymap *keymap, xkb_mod_index_t ndx);
const char *
-ModMaskText(struct xkb_keymap *keymap, xkb_mod_mask_t mask);
+ModMaskText(const struct xkb_keymap *keymap, xkb_mod_mask_t mask);
const char *
ActionTypeText(enum xkb_action_type type);