Some atom related optimizations
[platform/upstream/libxkbcommon.git] / src / xkbcomp / vmod.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of Silicon Graphics not be
10  * used in advertising or publicity pertaining to distribution
11  * of the software without specific prior written permission.
12  * Silicon Graphics makes no representation about the suitability
13  * of this software for any purpose. It is provided "as is"
14  * without any express or implied warranty.
15  *
16  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  *
25  ********************************************************/
26
27 #include "vmod.h"
28
29 void
30 InitVModInfo(VModInfo *info, struct xkb_keymap *keymap)
31 {
32     ClearVModInfo(info, keymap);
33     info->errorCount = 0;
34 }
35
36 void
37 ClearVModInfo(VModInfo *info, struct xkb_keymap *keymap)
38 {
39     int i, bit;
40
41     info->newlyDefined = info->defined = info->available = 0;
42
43     for (i = 0; i < XkbNumVirtualMods; i++)
44         keymap->vmods[i] = XkbNoModifierMask;
45
46     for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
47         if (keymap->vmod_names[i])
48             info->defined |= bit;
49 }
50
51 /***====================================================================***/
52
53 /**
54  * Handle one entry in the virtualModifiers line (e.g. NumLock).
55  * If the entry is e.g. NumLock=Mod1, stmt->value is not NULL, and the
56  * XkbServerMap's vmod is set to the given modifier. Otherwise, the vmod is 0.
57  *
58  * @param stmt The statement specifying the name and (if any the value).
59  * @param mergeMode Merge strategy (e.g. MERGE_OVERRIDE)
60  */
61 bool
62 HandleVModDef(VModDef *stmt, struct xkb_keymap *keymap,
63               enum merge_mode mergeMode,
64               VModInfo *info)
65 {
66     int i, bit, nextFree;
67     ExprResult mod;
68
69     for (i = 0, bit = 1, nextFree = -1; i < XkbNumVirtualMods; i++, bit <<=
70              1) {
71         if (!(info->defined & bit)) {
72             if (nextFree < 0)
73                 nextFree = i;
74             continue;
75         }
76
77         if (keymap->vmod_names[i] &&
78             strcmp(keymap->vmod_names[i],
79                    xkb_atom_text(keymap->ctx, stmt->name)) == 0) { /* already defined */
80             info->available |= bit;
81             if (stmt->value == NULL)
82                 return true;
83             else {
84                 const char *str1;
85                 const char *str2 = "";
86
87                 if (!ExprResolveModMask(keymap->ctx, stmt->value,
88                                         &mod)) {
89                     log_err(keymap->ctx, "Declaration of %s ignored\n",
90                             xkb_atom_text(keymap->ctx, stmt->name));
91                     return false;
92                 }
93
94                 if (mod.uval == keymap->vmods[i])
95                     return true;
96
97                 str1 = XkbcModMaskText(keymap->vmods[i], true);
98                 if (mergeMode == MERGE_OVERRIDE) {
99                     str2 = str1;
100                     str1 = XkbcModMaskText(mod.uval, true);
101                 }
102                 log_warn(keymap->ctx,
103                          "Virtual modifier %s multiply defined; "
104                          "Using %s, ignoring %s\n",
105                          xkb_atom_text(keymap->ctx, stmt->name), str1, str2);
106
107                 if (mergeMode == MERGE_OVERRIDE)
108                     keymap->vmods[i] = mod.uval;
109                 return true;
110             }
111         }
112     }
113
114     if (nextFree < 0) {
115         log_err(keymap->ctx,
116                 "Too many virtual modifiers defined (maximum %d)\n",
117                 XkbNumVirtualMods);
118         return false;
119     }
120     info->defined |= (1 << nextFree);
121     info->newlyDefined |= (1 << nextFree);
122     info->available |= (1 << nextFree);
123     keymap->vmod_names[nextFree] = xkb_atom_text(keymap->ctx, stmt->name);
124     if (stmt->value == NULL)
125         return true;
126     if (ExprResolveModMask(keymap->ctx, stmt->value, &mod)) {
127         keymap->vmods[nextFree] = mod.uval;
128         return true;
129     }
130     log_err(keymap->ctx, "Declaration of %s ignored\n",
131             xkb_atom_text(keymap->ctx, stmt->name));
132     return false;
133 }
134
135 /**
136  * Returns the index of the given modifier in the keymap->vmod_names array.
137  *
138  * @param keymap Pointer to the xkb data structure.
139  * @param field The Atom of the modifier's name (e.g. Atom for LAlt)
140  * @param type Must be TypeInt, otherwise return false.
141  * @param val_rtrn Set to the index of the modifier that matches.
142  *
143  * @return true on success, false otherwise. If false is returned, val_rtrn is
144  * undefined.
145  */
146 static int
147 LookupVModIndex(const struct xkb_keymap *keymap, xkb_atom_t field,
148                 unsigned type, ExprResult * val_rtrn)
149 {
150     int i;
151     const char *name = xkb_atom_text(keymap->ctx, field);
152
153     if (type != TypeInt)
154         return false;
155
156     /* For each named modifier, get the name and compare it to the one passed
157      * in. If we get a match, return the index of the modifier.
158      * The order of modifiers is the same as in the virtual_modifiers line in
159      * the xkb_types section.
160      */
161     for (i = 0; i < XkbNumVirtualMods; i++)
162         if (keymap->vmod_names[i] &&
163             strcmp(keymap->vmod_names[i], name) == 0) {
164             val_rtrn->uval = i;
165             return true;
166         }
167
168     return false;
169 }
170
171 /**
172  * Get the mask for the given (virtual or core) modifier and set
173  * val_rtrn.uval to the mask value.
174  *
175  * @param priv Pointer to xkb data structure.
176  * @param val_rtrn Member uval is set to the mask returned.
177  *
178  * @return true on success, false otherwise. If false is returned, val_rtrn is
179  * undefined.
180  */
181 bool
182 LookupVModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
183                unsigned type, ExprResult *val_rtrn)
184 {
185     if (LookupModMask(ctx, NULL, field, type, val_rtrn)) {
186         return true;
187     }
188     else if (LookupVModIndex(priv, field, type, val_rtrn)) {
189         unsigned ndx = val_rtrn->uval;
190         val_rtrn->uval = (1 << (XkbNumModifiers + ndx));
191         return true;
192     }
193     return false;
194 }
195
196 int
197 FindKeypadVMod(struct xkb_keymap *keymap)
198 {
199     xkb_atom_t name;
200     ExprResult rtrn;
201
202     name = xkb_atom_intern(keymap->ctx, "NumLock");
203     if ((keymap) && LookupVModIndex(keymap, name, TypeInt, &rtrn)) {
204         return rtrn.ival;
205     }
206     return -1;
207 }
208
209 bool
210 ResolveVirtualModifier(ExprDef *def, struct xkb_keymap *keymap,
211                        ExprResult *val_rtrn, VModInfo *info)
212 {
213     if (def->op == ExprIdent) {
214         int i, bit;
215         const char *name = xkb_atom_text(keymap->ctx, def->value.str);
216         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
217             if ((info->available & bit) && keymap->vmod_names[i] &&
218                 strcmp(keymap->vmod_names[i], name) == 0) {
219                 val_rtrn->uval = i;
220                 return true;
221             }
222         }
223     }
224     if (ExprResolveInteger(keymap->ctx, def, val_rtrn)) {
225         if (val_rtrn->uval < XkbNumVirtualMods)
226             return true;
227         log_err(keymap->ctx,
228                 "Illegal virtual modifier %d (must be 0..%d inclusive)\n",
229                 val_rtrn->uval, XkbNumVirtualMods - 1);
230     }
231     return false;
232 }