Run source tree through uncrustify
[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;
40
41     info->newlyDefined = info->defined = info->available = 0;
42
43     if (XkbcAllocNames(keymap, 0, 0) != Success)
44         return;
45
46     if (XkbcAllocServerMap(keymap, 0, 0) != Success)
47         return;
48
49     info->keymap = keymap;
50     if (keymap && keymap->names) {
51         int bit;
52         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
53             if (keymap->names->vmods[i] != NULL)
54                 info->defined |= bit;
55         }
56     }
57 }
58
59 /***====================================================================***/
60
61 /**
62  * Handle one entry in the virtualModifiers line (e.g. NumLock).
63  * If the entry is e.g. NumLock=Mod1, stmt->value is not NULL, and the
64  * XkbServerMap's vmod is set to the given modifier. Otherwise, the vmod is 0.
65  *
66  * @param stmt The statement specifying the name and (if any the value).
67  * @param mergeMode Merge strategy (e.g. MERGE_OVERRIDE)
68  */
69 bool
70 HandleVModDef(VModDef *stmt, struct xkb_keymap *keymap,
71               enum merge_mode mergeMode,
72               VModInfo *info)
73 {
74     int i, bit, nextFree;
75     ExprResult mod;
76     struct xkb_server_map *srv = keymap->server;
77     struct xkb_names *names = keymap->names;
78
79     for (i = 0, bit = 1, nextFree = -1; i < XkbNumVirtualMods; i++, bit <<=
80              1) {
81         if (info->defined & bit) {
82             if (names->vmods[i] &&
83                 strcmp(names->vmods[i],
84                        xkb_atom_text(keymap->ctx, stmt->name)) == 0) { /* already defined */
85                 info->available |= bit;
86                 if (stmt->value == NULL)
87                     return true;
88                 else {
89                     const char *str1;
90                     const char *str2 = "";
91                     if (!ExprResolveModMask(keymap->ctx, stmt->value,
92                                             &mod)) {
93                         str1 = xkb_atom_text(keymap->ctx, stmt->name);
94                         ACTION("Declaration of %s ignored\n", str1);
95                         return false;
96                     }
97                     if (mod.uval == srv->vmods[i])
98                         return true;
99
100                     str1 = xkb_atom_text(keymap->ctx, stmt->name);
101                     WARN("Virtual modifier %s multiply defined\n", str1);
102                     str1 = XkbcModMaskText(srv->vmods[i], true);
103                     if (mergeMode == MERGE_OVERRIDE) {
104                         str2 = str1;
105                         str1 = XkbcModMaskText(mod.uval, true);
106                     }
107                     ACTION("Using %s, ignoring %s\n", str1, str2);
108                     if (mergeMode == MERGE_OVERRIDE)
109                         srv->vmods[i] = mod.uval;
110                     return true;
111                 }
112             }
113         }
114         else if (nextFree < 0)
115             nextFree = i;
116     }
117     if (nextFree < 0) {
118         ERROR("Too many virtual modifiers defined (maximum %d)\n",
119               XkbNumVirtualMods);
120         return false;
121     }
122     info->defined |= (1 << nextFree);
123     info->newlyDefined |= (1 << nextFree);
124     info->available |= (1 << nextFree);
125     names->vmods[nextFree] = xkb_atom_strdup(keymap->ctx, stmt->name);
126     if (stmt->value == NULL)
127         return true;
128     if (ExprResolveModMask(keymap->ctx, stmt->value, &mod)) {
129         srv->vmods[nextFree] = mod.uval;
130         return true;
131     }
132     ACTION("Declaration of %s ignored\n",
133            xkb_atom_text(keymap->ctx, stmt->name));
134     return false;
135 }
136
137 /**
138  * Returns the index of the given modifier in the keymap->names->vmods array.
139  *
140  * @param keymap Pointer to the xkb data structure.
141  * @param field The Atom of the modifier's name (e.g. Atom for LAlt)
142  * @param type Must be TypeInt, otherwise return false.
143  * @param val_rtrn Set to the index of the modifier that matches.
144  *
145  * @return true on success, false otherwise. If false is returned, val_rtrn is
146  * undefined.
147  */
148 static int
149 LookupVModIndex(const struct xkb_keymap *keymap, xkb_atom_t field,
150                 unsigned type, ExprResult * val_rtrn)
151 {
152     int i;
153     const char *name = xkb_atom_text(keymap->ctx, field);
154
155     if ((keymap == NULL) || (keymap->names == NULL) || (type != TypeInt)) {
156         return false;
157     }
158     /* For each named modifier, get the name and compare it to the one passed
159      * in. If we get a match, return the index of the modifier.
160      * The order of modifiers is the same as in the virtual_modifiers line in
161      * the xkb_types section.
162      */
163     for (i = 0; i < XkbNumVirtualMods; i++) {
164         if (keymap->names->vmods[i] &&
165             strcmp(keymap->names->vmods[i], name) == 0) {
166             val_rtrn->uval = i;
167             return true;
168         }
169     }
170     return false;
171 }
172
173 /**
174  * Get the mask for the given (virtual or core) modifier and set
175  * val_rtrn.uval to the mask value.
176  *
177  * @param priv Pointer to xkb data structure.
178  * @param val_rtrn Member uval is set to the mask returned.
179  *
180  * @return true on success, false otherwise. If false is returned, val_rtrn is
181  * undefined.
182  */
183 bool
184 LookupVModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
185                unsigned type, ExprResult *val_rtrn)
186 {
187     if (LookupModMask(ctx, NULL, field, type, val_rtrn)) {
188         return true;
189     }
190     else if (LookupVModIndex(priv, field, type, val_rtrn)) {
191         unsigned ndx = val_rtrn->uval;
192         val_rtrn->uval = (1 << (XkbNumModifiers + ndx));
193         return true;
194     }
195     return false;
196 }
197
198 int
199 FindKeypadVMod(struct xkb_keymap *keymap)
200 {
201     xkb_atom_t name;
202     ExprResult rtrn;
203
204     name = xkb_atom_intern(keymap->ctx, "NumLock");
205     if ((keymap) && LookupVModIndex(keymap, name, TypeInt, &rtrn)) {
206         return rtrn.ival;
207     }
208     return -1;
209 }
210
211 bool
212 ResolveVirtualModifier(ExprDef *def, struct xkb_keymap *keymap,
213                        ExprResult *val_rtrn, VModInfo *info)
214 {
215     struct xkb_names *names = keymap->names;
216
217     if (def->op == ExprIdent) {
218         int i, bit;
219         const char *name = xkb_atom_text(keymap->ctx, def->value.str);
220         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
221             if ((info->available & bit) && names->vmods[i] &&
222                 strcmp(names->vmods[i], name) == 0) {
223                 val_rtrn->uval = i;
224                 return true;
225             }
226         }
227     }
228     if (ExprResolveInteger(keymap->ctx, def, val_rtrn)) {
229         if (val_rtrn->uval < XkbNumVirtualMods)
230             return true;
231         ERROR("Illegal virtual modifier %d (must be 0..%d inclusive)\n",
232               val_rtrn->uval, XkbNumVirtualMods - 1);
233     }
234     return false;
235 }