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