| 1 |
/* |
| 2 |
* Copyright © 2012 Intel Corporation |
| 3 |
* Copyright © 2012 Ran Benita <[email protected]> |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
* Author: Daniel Stone <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "config.h" |
| 10 |
|
| 11 |
#include <assert.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#include "xkbcommon/xkbcommon-names.h" |
| 16 |
#include "keymap.h" |
| 17 |
|
| 18 |
static void |
| 19 |
update_builtin_keymap_fields(struct xkb_keymap *keymap) |
| 20 |
{ |
| 21 |
/* Predefined (AKA real, core, X11) modifiers. The order is important! */ |
| 22 |
static const char *const builtin_mods[] = { |
| 23 |
[XKB_MOD_INDEX_SHIFT] = XKB_MOD_NAME_SHIFT, |
| 24 |
[XKB_MOD_INDEX_CAPS] = XKB_MOD_NAME_CAPS, |
| 25 |
[XKB_MOD_INDEX_CTRL] = XKB_MOD_NAME_CTRL, |
| 26 |
[XKB_MOD_INDEX_MOD1] = XKB_MOD_NAME_MOD1, |
| 27 |
[XKB_MOD_INDEX_MOD2] = XKB_MOD_NAME_MOD2, |
| 28 |
[XKB_MOD_INDEX_MOD3] = XKB_MOD_NAME_MOD3, |
| 29 |
[XKB_MOD_INDEX_MOD4] = XKB_MOD_NAME_MOD4, |
| 30 |
[XKB_MOD_INDEX_MOD5] = XKB_MOD_NAME_MOD5 |
| 31 |
}; |
| 32 |
|
| 33 |
for (xkb_mod_index_t i = 0; i < ARRAY_SIZE(builtin_mods); i++) { |
| 34 |
keymap->mods.mods[i].name = xkb_atom_intern(keymap->ctx, |
| 35 |
builtin_mods[i], |
| 36 |
strlen(builtin_mods[i])); |
| 37 |
keymap->mods.mods[i].type = MOD_REAL; |
| 38 |
/* Real modifiers have a canonical mapping */ |
| 39 |
keymap->mods.mods[i].mapping = UINT32_C(1) << i; |
| 40 |
} |
| 41 |
keymap->mods.num_mods = ARRAY_SIZE(builtin_mods); |
| 42 |
keymap->canonical_state_mask = MOD_REAL_MASK_ALL; |
| 43 |
} |
| 44 |
|
| 45 |
struct xkb_keymap * |
| 46 |
xkb_keymap_new(struct xkb_context *ctx, |
| 47 |
enum xkb_keymap_format format, |
| 48 |
enum xkb_keymap_compile_flags flags) |
| 49 |
{ |
| 50 |
struct xkb_keymap *keymap; |
| 51 |
|
| 52 |
keymap = calloc(1, sizeof(*keymap)); |
| 53 |
if (!keymap) |
| 54 |
return NULL; |
| 55 |
|
| 56 |
keymap->refcnt = 1; |
| 57 |
keymap->ctx = xkb_context_ref(ctx); |
| 58 |
|
| 59 |
keymap->format = format; |
| 60 |
keymap->flags = flags; |
| 61 |
|
| 62 |
update_builtin_keymap_fields(keymap); |
| 63 |
|
| 64 |
return keymap; |
| 65 |
} |
| 66 |
|
| 67 |
void |
| 68 |
XkbEscapeMapName(char *name) |
| 69 |
{ |
| 70 |
/* |
| 71 |
* All latin-1 alphanumerics, plus parens, slash, minus, underscore and |
| 72 |
* wildcards. |
| 73 |
*/ |
| 74 |
static const unsigned char legal[] = { |
| 75 |
0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83, |
| 76 |
0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07, |
| 77 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 78 |
0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff |
| 79 |
}; |
| 80 |
|
| 81 |
if (!name) |
| 82 |
return; |
| 83 |
|
| 84 |
while (*name) { |
| 85 |
unsigned char c = *name; |
| 86 |
if (!(legal[c / 8] & (1u << (c % 8)))) |
| 87 |
*name = '_'; |
| 88 |
name++; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
xkb_mod_index_t |
| 93 |
XkbModNameToIndex(const struct xkb_mod_set *mods, xkb_atom_t name, |
| 94 |
enum mod_type type) |
| 95 |
{ |
| 96 |
xkb_mod_index_t i; |
| 97 |
const struct xkb_mod *mod; |
| 98 |
|
| 99 |
xkb_mods_enumerate(i, mod, mods) |
| 100 |
if ((mod->type & type) && name == mod->name) |
| 101 |
return i; |
| 102 |
|
| 103 |
return XKB_MOD_INVALID; |
| 104 |
} |
| 105 |
|
| 106 |
bool |
| 107 |
XkbLevelsSameSyms(const struct xkb_level *a, const struct xkb_level *b) |
| 108 |
{ |
| 109 |
if (a->num_syms != b->num_syms) |
| 110 |
return false; |
| 111 |
if (a->num_syms <= 1) |
| 112 |
return a->s.sym == b->s.sym; |
| 113 |
return memcmp(a->s.syms, b->s.syms, sizeof(*a->s.syms) * a->num_syms) == 0; |
| 114 |
} |
| 115 |
|
| 116 |
bool |
| 117 |
action_equal(const union xkb_action *a, const union xkb_action *b) |
| 118 |
{ |
| 119 |
if (a->type != b->type) |
| 120 |
return false; |
| 121 |
|
| 122 |
/* Ensure we support all action types */ |
| 123 |
static_assert(ACTION_TYPE_INTERNAL == 18 && |
| 124 |
ACTION_TYPE_INTERNAL + 1 == _ACTION_TYPE_NUM_ENTRIES, |
| 125 |
"Missing action type"); |
| 126 |
|
| 127 |
switch (a->type) { |
| 128 |
case ACTION_TYPE_NONE: |
| 129 |
case ACTION_TYPE_VOID: |
| 130 |
return true; |
| 131 |
case ACTION_TYPE_MOD_SET: |
| 132 |
case ACTION_TYPE_MOD_LATCH: |
| 133 |
case ACTION_TYPE_MOD_LOCK: |
| 134 |
return (a->mods.flags == b->mods.flags && |
| 135 |
a->mods.mods.mask == b->mods.mods.mask && |
| 136 |
a->mods.mods.mods == b->mods.mods.mods); |
| 137 |
case ACTION_TYPE_GROUP_SET: |
| 138 |
case ACTION_TYPE_GROUP_LATCH: |
| 139 |
case ACTION_TYPE_GROUP_LOCK: |
| 140 |
return (a->group.flags == b->group.flags && |
| 141 |
a->group.group == b->group.group); |
| 142 |
case ACTION_TYPE_PTR_MOVE: |
| 143 |
return (a->ptr.flags == b->ptr.flags && |
| 144 |
a->ptr.x == b->ptr.x && |
| 145 |
a->ptr.y == b->ptr.y); |
| 146 |
case ACTION_TYPE_PTR_BUTTON: |
| 147 |
case ACTION_TYPE_PTR_LOCK: |
| 148 |
return (a->btn.flags == b->btn.flags && |
| 149 |
a->btn.button == b->btn.button && |
| 150 |
a->btn.count == b->btn.count); |
| 151 |
case ACTION_TYPE_PTR_DEFAULT: |
| 152 |
return (a->dflt.flags == b->dflt.flags && |
| 153 |
a->dflt.value == b->dflt.value); |
| 154 |
case ACTION_TYPE_TERMINATE: |
| 155 |
return true; |
| 156 |
case ACTION_TYPE_SWITCH_VT: |
| 157 |
return (a->screen.flags == b->screen.flags && |
| 158 |
a->screen.screen == b->screen.screen); |
| 159 |
case ACTION_TYPE_CTRL_SET: |
| 160 |
case ACTION_TYPE_CTRL_LOCK: |
| 161 |
return (a->ctrls.flags == b->ctrls.flags && |
| 162 |
a->ctrls.ctrls == b->ctrls.ctrls); |
| 163 |
case ACTION_TYPE_UNSUPPORTED_LEGACY: |
| 164 |
return true; |
| 165 |
/* ACTION_TYPE_PRIVATE processed in the default case */ |
| 166 |
case ACTION_TYPE_INTERNAL: |
| 167 |
return (a->internal.flags == b->internal.flags) && |
| 168 |
(a->internal.clear_latched_mods == b->internal.clear_latched_mods); |
| 169 |
default: |
| 170 |
{} /* Label followed by declaration requires C23 */ |
| 171 |
/* Ensure to not miss `xkb_action_type` updates */ |
| 172 |
static_assert(ACTION_TYPE_INTERNAL == 18 && |
| 173 |
ACTION_TYPE_INTERNAL + 1 == _ACTION_TYPE_NUM_ENTRIES, |
| 174 |
"Missing action type"); |
| 175 |
/* Private/custom action */ |
| 176 |
return memcmp(a->priv.data, b->priv.data, sizeof(a->priv.data)) == 0; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
bool |
| 181 |
XkbLevelsSameActions(const struct xkb_level *a, const struct xkb_level *b) |
| 182 |
{ |
| 183 |
if (a->num_actions != b->num_actions) |
| 184 |
return false; |
| 185 |
if (a->num_actions <= 1) |
| 186 |
return action_equal(&a->a.action, &b->a.action); |
| 187 |
for (xkb_action_count_t k = 0; k < a->num_actions; k++) { |
| 188 |
if (!action_equal(&a->a.actions[k], &b->a.actions[k])) |
| 189 |
return false; |
| 190 |
} |
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
/* See: XkbAdjustGroup in Xorg xserver */ |
| 195 |
xkb_layout_index_t |
| 196 |
XkbWrapGroupIntoRange(int32_t group, |
| 197 |
xkb_layout_index_t num_groups, |
| 198 |
enum xkb_range_exceed_type out_of_range_group_action, |
| 199 |
xkb_layout_index_t out_of_range_group_number) |
| 200 |
{ |
| 201 |
if (num_groups == 0) |
| 202 |
return XKB_LAYOUT_INVALID; |
| 203 |
|
| 204 |
if (group >= 0 && (xkb_layout_index_t) group < num_groups) |
| 205 |
return group; |
| 206 |
|
| 207 |
switch (out_of_range_group_action) { |
| 208 |
case RANGE_REDIRECT: |
| 209 |
if (out_of_range_group_number >= num_groups) |
| 210 |
return 0; |
| 211 |
return out_of_range_group_number; |
| 212 |
|
| 213 |
case RANGE_SATURATE: |
| 214 |
if (group < 0) |
| 215 |
return 0; |
| 216 |
else |
| 217 |
return num_groups - 1; |
| 218 |
|
| 219 |
case RANGE_WRAP: |
| 220 |
default: { |
| 221 |
/* |
| 222 |
* Ensure conversion xkb_layout_index_t → int32_t is lossless. |
| 223 |
* |
| 224 |
* NOTE: C11 requires a compound statement because it does not allow |
| 225 |
* a declaration after a label (C23 does allow it). |
| 226 |
*/ |
| 227 |
static_assert(XKB_MAX_GROUPS < INT32_MAX, "Max groups max don't fit"); |
| 228 |
/* |
| 229 |
* % returns the *remainder* of the division, not the modulus (see C11 |
| 230 |
* standard 6.5.5 “Multiplicative operators”: a % b = a - (a/b)*b. While |
| 231 |
* both operators return the same result for positive operands, they do |
| 232 |
* not for e.g. a negative dividend: remainder may be negative (in the |
| 233 |
* open interval ]-num_groups, num_groups[) while the modulus is always |
| 234 |
* positive. So if the remainder is negative, we must add `num_groups` |
| 235 |
* to get the modulus. |
| 236 |
*/ |
| 237 |
const int32_t rem = group % (int32_t) num_groups; |
| 238 |
return (rem >= 0) ? rem : rem + (int32_t) num_groups; |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
xkb_action_count_t |
| 244 |
xkb_keymap_key_get_actions_by_level(struct xkb_keymap *keymap, |
| 245 |
const struct xkb_key *key, |
| 246 |
xkb_layout_index_t layout, |
| 247 |
xkb_level_index_t level, |
| 248 |
const union xkb_action **actions) |
| 249 |
{ |
| 250 |
if (!key) |
| 251 |
goto err; |
| 252 |
|
| 253 |
layout = XkbWrapGroupIntoRange((int32_t) layout, key->num_groups, |
| 254 |
key->out_of_range_group_action, |
| 255 |
key->out_of_range_group_number); |
| 256 |
if (layout == XKB_LAYOUT_INVALID) |
| 257 |
goto err; |
| 258 |
|
| 259 |
if (level >= XkbKeyNumLevels(key, layout)) |
| 260 |
goto err; |
| 261 |
|
| 262 |
const xkb_action_count_t count = |
| 263 |
key->groups[layout].levels[level].num_actions; |
| 264 |
switch (count) { |
| 265 |
case 0: |
| 266 |
goto err; |
| 267 |
case 1: |
| 268 |
*actions = &key->groups[layout].levels[level].a.action; |
| 269 |
break; |
| 270 |
default: |
| 271 |
*actions = key->groups[layout].levels[level].a.actions; |
| 272 |
} |
| 273 |
return count; |
| 274 |
|
| 275 |
err: |
| 276 |
*actions = NULL; |
| 277 |
return 0; |
| 278 |
} |