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