Add flags to keymap compilation entrypoints
[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 * xkb)
31 {
32     ClearVModInfo(info, xkb);
33     info->errorCount = 0;
34 }
35
36 void
37 ClearVModInfo(VModInfo * info, struct xkb_keymap * xkb)
38 {
39     int i;
40
41     info->newlyDefined = info->defined = info->available = 0;
42
43     if (XkbcAllocNames(xkb, 0, 0) != Success)
44         return;
45
46     if (XkbcAllocServerMap(xkb, 0, 0) != Success)
47         return;
48
49     info->xkb = xkb;
50     if (xkb && xkb->names)
51     {
52         int bit;
53         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
54         {
55             if (xkb->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 *xkb, unsigned mergeMode,
73               VModInfo * info)
74 {
75     int i, bit, nextFree;
76     ExprResult mod;
77     struct xkb_server_map * srv;
78     struct xkb_names * names;
79
80     srv = xkb->server;
81     names = xkb->names;
82     for (i = 0, bit = 1, nextFree = -1; i < XkbNumVirtualMods; i++, bit <<= 1)
83     {
84         if (info->defined & bit)
85         {
86             if (names->vmods[i] &&
87                 strcmp(names->vmods[i], XkbcAtomText(stmt->name)) == 0)
88             {                   /* already defined */
89                 info->available |= bit;
90                 if (stmt->value == NULL)
91                     return true;
92                 else
93                 {
94                     const char *str1;
95                     const char *str2 = "";
96                     if (!ExprResolveModMask(stmt->value, &mod))
97                     {
98                         str1 = XkbcAtomText(stmt->name);
99                         ACTION("Declaration of %s ignored\n", str1);
100                         return false;
101                     }
102                     if (mod.uval == srv->vmods[i])
103                         return true;
104
105                     str1 = XkbcAtomText(stmt->name);
106                     WARN("Virtual modifier %s multiply defined\n", str1);
107                     str1 = XkbcModMaskText(srv->vmods[i], true);
108                     if (mergeMode == MergeOverride)
109                     {
110                         str2 = str1;
111                         str1 = XkbcModMaskText(mod.uval, true);
112                     }
113                     ACTION("Using %s, ignoring %s\n", str1, str2);
114                     if (mergeMode == MergeOverride)
115                         srv->vmods[i] = mod.uval;
116                     return true;
117                 }
118             }
119         }
120         else if (nextFree < 0)
121             nextFree = i;
122     }
123     if (nextFree < 0)
124     {
125         ERROR("Too many virtual modifiers defined (maximum %d)\n",
126                XkbNumVirtualMods);
127         return false;
128     }
129     info->defined |= (1 << nextFree);
130     info->newlyDefined |= (1 << nextFree);
131     info->available |= (1 << nextFree);
132     names->vmods[nextFree] = XkbcAtomGetString(stmt->name);
133     if (stmt->value == NULL)
134         return true;
135     if (ExprResolveModMask(stmt->value, &mod))
136     {
137         srv->vmods[nextFree] = mod.uval;
138         return true;
139     }
140     ACTION("Declaration of %s ignored\n", XkbcAtomText(stmt->name));
141     return false;
142 }
143
144 /**
145  * Returns the index of the given modifier in the xkb->names->vmods array.
146  *
147  * @param priv Pointer to the xkb data structure.
148  * @param field The Atom of the modifier's name (e.g. Atom for LAlt)
149  * @param type Must be TypeInt, otherwise return false.
150  * @param val_rtrn Set to the index of the modifier that matches.
151  *
152  * @return true on success, false otherwise. If false is returned, val_rtrn is
153  * undefined.
154  */
155 static int
156 LookupVModIndex(const struct xkb_keymap *xkb, xkb_atom_t field, unsigned type,
157                 ExprResult * val_rtrn)
158 {
159     int i;
160     const char *name = XkbcAtomText(field);
161
162     if ((xkb == NULL) || (xkb->names == NULL) || (type != TypeInt))
163     {
164         return false;
165     }
166     /* For each named modifier, get the name and compare it to the one passed
167      * in. If we get a match, return the index of the modifier.
168      * The order of modifiers is the same as in the virtual_modifiers line in
169      * the xkb_types section.
170      */
171     for (i = 0; i < XkbNumVirtualMods; i++)
172     {
173         if (xkb->names->vmods[i] && strcmp(xkb->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(const void * priv, xkb_atom_t field, unsigned type,
194                ExprResult * val_rtrn)
195 {
196     if (LookupModMask(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 * xkb)
211 {
212     xkb_atom_t name;
213     ExprResult rtrn;
214
215     name = xkb_intern_atom("NumLock");
216     if ((xkb) && LookupVModIndex(xkb, name, TypeInt, &rtrn))
217     {
218         return rtrn.ival;
219     }
220     return -1;
221 }
222
223 bool
224 ResolveVirtualModifier(ExprDef * def, struct xkb_keymap *xkb,
225                        ExprResult * val_rtrn, VModInfo * info)
226 {
227     struct xkb_names * names;
228
229     names = xkb->names;
230     if (def->op == ExprIdent)
231     {
232         int i, bit;
233         const char *name = XkbcAtomText(def->value.str);
234         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
235         {
236             if ((info->available & bit) && names->vmods[i] &&
237                 strcmp(names->vmods[i], name) == 0)
238             {
239                 val_rtrn->uval = i;
240                 return true;
241             }
242         }
243     }
244     if (ExprResolveInteger(def, val_rtrn))
245     {
246         if (val_rtrn->uval < XkbNumVirtualMods)
247             return true;
248         ERROR("Illegal virtual modifier %d (must be 0..%d inclusive)\n",
249                val_rtrn->uval, XkbNumVirtualMods - 1);
250     }
251     return false;
252 }