Debian Codegraph @

src/xkbcomp/vmod.c

raw
1 /*
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3 * SPDX-License-Identifier: HPND
4 */
5
6 #include "config.h"
7
8 #include <stdint.h>
9
10 #include "xkbcommon/xkbcommon.h"
11 #include "atom.h"
12 #include "keymap.h"
13 #include "text.h"
14 #include "expr.h"
15 #include "xkbcomp/ast.h"
16 #include "vmod.h"
17
18 void
19 InitVMods(struct xkb_mod_set *info, const struct xkb_mod_set *mods, bool reset)
20 {
21 *info = *mods;
22
23 if (!reset)
24 return;
25
26 struct xkb_mod *mod;
27 xkb_mod_index_t vmod = 0;
28 xkb_mods_enumerate(vmod, mod, info) {
29 mod->mapping = 0;
30 }
31 info->explicit_vmods = 0;
32 }
33
34 void
35 MergeModSets(struct xkb_context *ctx, struct xkb_mod_set *into,
36 const struct xkb_mod_set *from, enum merge_mode merge) {
37 const bool clobber = (merge != MERGE_AUGMENT);
38 xkb_mod_index_t vmod;
39 const struct xkb_mod *mod;
40 assert(into->num_mods <= from->num_mods);
41 xkb_mods_enumerate(vmod, mod, from) {
42 const xkb_mod_mask_t mask = UINT32_C(1) << vmod;
43
44 if ((mod->type != MOD_VIRT)) {
45 /* No modifier in `from` or real modifier: nothing to do */
46 assert((mod->type == 0 &&
47 mod->name == XKB_ATOM_NONE) ||
48 ((mod->type & MOD_REAL) &&
49 into->mods[vmod].type == mod->type &&
50 mod->name != XKB_ATOM_NONE &&
51 into->mods[vmod].name == mod->name));
52 continue;
53 } else if (into->mods[vmod].type == 0) {
54 /* No modifier in `into`: copy the whole modifier definition */
55 assert(into->mods[vmod].name == XKB_ATOM_NONE);
56 assert(mod->type == MOD_VIRT);
57 assert(mod->name != XKB_ATOM_NONE);
58 assert(vmod >= into->num_mods);
59 into->mods[vmod] = *mod;
60 if (from->explicit_vmods & mask)
61 into->explicit_vmods |= mask;
62 } else {
63 /* Modifier exists in both */
64 assert(mod->type == MOD_VIRT);
65 assert(mod->name != XKB_ATOM_NONE);
66 assert(into->mods[vmod].type == mod->type);
67 assert(into->mods[vmod].name == mod->name);
68
69 if (!(from->explicit_vmods & mask)) {
70 /* Implicit mapping in `from`: do nothing */
71 assert(mod->mapping == 0);
72 } else if (!(into->explicit_vmods & mask)) {
73 /* Implicit mapping in `into`: replace */
74 assert(into->mods[vmod].mapping == 0);
75 into->mods[vmod].mapping = mod->mapping;
76 into->explicit_vmods |= mask;
77 } else if (mod->mapping != into->mods[vmod].mapping) {
78 /* Handle conflicting mappings */
79 const xkb_mod_mask_t use = (clobber)
80 ? mod->mapping
81 : into->mods[vmod].mapping;
82 const xkb_mod_mask_t ignore = (clobber)
83 ? into->mods[vmod].mapping
84 : mod->mapping;
85
86 log_warn(ctx, XKB_LOG_MESSAGE_NO_ID,
87 "Virtual modifier %s mapping defined multiple times; "
88 "Using %s, ignoring %s\n",
89 xkb_atom_text(ctx, mod->name),
90 ModMaskText(ctx, MOD_REAL, from, use),
91 ModMaskText(ctx, MOD_REAL, from, ignore));
92
93 into->mods[vmod].mapping = use;
94 }
95 }
96 }
97 into->num_mods = from->num_mods;
98 }
99
100 bool
101 HandleVModDef(struct xkb_context *ctx, struct xkb_mod_set *mods, VModDef *stmt)
102 {
103 xkb_mod_mask_t mapping = 0;
104 if (stmt->value) {
105 /*
106 * This is a statement such as 'virtualModifiers NumLock = Mod1';
107 * it initialize the vmod-to-real-mod[s] mapping before going
108 * through modifier_map.
109 */
110 if (!ExprResolveModMask(ctx, stmt->value, MOD_REAL, mods, &mapping)) {
111 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
112 "Declaration of %s ignored\n",
113 xkb_atom_text(ctx, stmt->name));
114 return false;
115 }
116 }
117
118 xkb_mod_index_t vmod;
119 struct xkb_mod *mod;
120 xkb_mods_enumerate(vmod, mod, mods) {
121 if (mod->name == stmt->name) {
122 if (mod->type != MOD_VIRT) {
123 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
124 "Can't add a virtual modifier named \"%s\"; there is "
125 "already a non-virtual modifier with this name! "
126 "Ignored\n",
127 xkb_atom_text(ctx, mod->name));
128 return false;
129 }
130
131 const xkb_mod_mask_t mask = UINT32_C(1) << vmod;
132
133 if (!stmt->value) {
134 /* No new explicit mapping: do nothing */
135 return true;
136 } else if (!(mods->explicit_vmods & mask)) {
137 /* No previous explicit mapping: add mapping */
138 mod->mapping = mapping;
139 } else if (mod->mapping != mapping) {
140 /* Handle conflicting mappings */
141 const bool clobber = (stmt->merge != MERGE_AUGMENT);
142 const xkb_mod_mask_t use =
143 (clobber ? mapping : mod->mapping);
144 const xkb_mod_mask_t ignore =
145 (clobber ? mod->mapping : mapping);
146
147 log_warn(ctx, XKB_LOG_MESSAGE_NO_ID,
148 "Virtual modifier %s mapping defined multiple times; "
149 "Using %s, ignoring %s\n",
150 xkb_atom_text(ctx, stmt->name),
151 ModMaskText(ctx, MOD_REAL, mods, use),
152 ModMaskText(ctx, MOD_REAL, mods, ignore));
153
154 mod->mapping = use;
155 }
156
157 mods->explicit_vmods |= mask;
158
159 return true;
160 }
161 }
162
163 if (mods->num_mods >= XKB_MAX_MODS) {
164 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
165 "Cannot define virtual modifier %s: "
166 "too many modifiers defined (maximum %u)\n",
167 xkb_atom_text(ctx, stmt->name), XKB_MAX_MODS);
168 return false;
169 }
170
171 mods->mods[mods->num_mods].name = stmt->name;
172 mods->mods[mods->num_mods].type = MOD_VIRT;
173 mods->mods[mods->num_mods].mapping = mapping;
174 if (stmt->value) {
175 const xkb_mod_mask_t mask = UINT32_C(1) << mods->num_mods;
176 mods->explicit_vmods |= mask;
177 }
178 mods->num_mods++;
179 return true;
180 }