Remove KcCGST names from the map
[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] != None)
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] == stmt->name)
94             {                   /* already defined */
95                 info->available |= bit;
96                 if (stmt->value == NULL)
97                     return True;
98                 else
99                 {
100                     const char *str1;
101                     const char *str2 = "";
102                     if (!ExprResolveModMask(stmt->value, &mod))
103                     {
104                         str1 = XkbcAtomText(stmt->name);
105                         ACTION("Declaration of %s ignored\n", str1);
106                         return False;
107                     }
108                     if (mod.uval == srv->vmods[i])
109                         return True;
110
111                     str1 = XkbcAtomText(stmt->name);
112                     WARN("Virtual modifier %s multiply defined\n", str1);
113                     str1 = XkbcModMaskText(srv->vmods[i], True);
114                     if (mergeMode == MergeOverride)
115                     {
116                         str2 = str1;
117                         str1 = XkbcModMaskText(mod.uval, True);
118                     }
119                     ACTION("Using %s, ignoring %s\n", str1, str2);
120                     if (mergeMode == MergeOverride)
121                         srv->vmods[i] = mod.uval;
122                     return True;
123                 }
124             }
125         }
126         else if (nextFree < 0)
127             nextFree = i;
128     }
129     if (nextFree < 0)
130     {
131         ERROR("Too many virtual modifiers defined (maximum %d)\n",
132                XkbNumVirtualMods);
133         return False;
134     }
135     info->defined |= (1 << nextFree);
136     info->newlyDefined |= (1 << nextFree);
137     info->available |= (1 << nextFree);
138     names->vmods[nextFree] = stmt->name;
139     if (stmt->value == NULL)
140         return True;
141     if (ExprResolveModMask(stmt->value, &mod))
142     {
143         srv->vmods[nextFree] = mod.uval;
144         return True;
145     }
146     ACTION("Declaration of %s ignored\n", XkbcAtomText(stmt->name));
147     return False;
148 }
149
150 /**
151  * Returns the index of the given modifier in the xkb->names->vmods array.
152  *
153  * @param priv Pointer to the xkb data structure.
154  * @param field The Atom of the modifier's name (e.g. Atom for LAlt)
155  * @param type Must be TypeInt, otherwise return False.
156  * @param val_rtrn Set to the index of the modifier that matches.
157  *
158  * @return True on success, False otherwise. If False is returned, val_rtrn is
159  * undefined.
160  */
161 static int
162 LookupVModIndex(const struct xkb_desc *xkb, uint32_t field, unsigned type,
163                 ExprResult * val_rtrn)
164 {
165     int i;
166
167     if ((xkb == NULL) || (xkb->names == NULL) || (type != TypeInt))
168     {
169         return False;
170     }
171     /* For each named modifier, get the name and compare it to the one passed
172      * in. If we get a match, return the index of the modifier.
173      * The order of modifiers is the same as in the virtual_modifiers line in
174      * the xkb_types section.
175      */
176     for (i = 0; i < XkbNumVirtualMods; i++)
177     {
178         if (xkb->names->vmods[i] == field)
179         {
180             val_rtrn->uval = i;
181             return True;
182         }
183     }
184     return False;
185 }
186
187 /**
188  * Get the mask for the given (virtual or core) modifier and set
189  * val_rtrn.uval to the mask value.
190  *
191  * @param priv Pointer to xkb data structure.
192  * @param val_rtrn Member uval is set to the mask returned.
193  *
194  * @return True on success, False otherwise. If False is returned, val_rtrn is
195  * undefined.
196  */
197 int
198 LookupVModMask(const void * priv, uint32_t field, unsigned type,
199                ExprResult * val_rtrn)
200 {
201     if (LookupModMask(NULL, field, type, val_rtrn))
202     {
203         return True;
204     }
205     else if (LookupVModIndex(priv, field, type, val_rtrn))
206     {
207         unsigned ndx = val_rtrn->uval;
208         val_rtrn->uval = (1 << (XkbNumModifiers + ndx));
209         return True;
210     }
211     return False;
212 }
213
214 int
215 FindKeypadVMod(struct xkb_desc * xkb)
216 {
217     uint32_t name;
218     ExprResult rtrn;
219
220     name = xkb_intern_atom("NumLock");
221     if ((xkb) && LookupVModIndex(xkb, name, TypeInt, &rtrn))
222     {
223         return rtrn.ival;
224     }
225     return -1;
226 }
227
228 Bool
229 ResolveVirtualModifier(ExprDef * def, struct xkb_desc *xkb,
230                        ExprResult * val_rtrn, VModInfo * info)
231 {
232     struct xkb_names * names;
233
234     names = xkb->names;
235     if (def->op == ExprIdent)
236     {
237         int i, bit;
238         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
239         {
240             if ((info->available & bit) && names->vmods[i] == def->value.str)
241             {
242                 val_rtrn->uval = i;
243                 return True;
244             }
245         }
246     }
247     if (ExprResolveInteger(def, val_rtrn))
248     {
249         if (val_rtrn->uval < XkbNumVirtualMods)
250             return True;
251         ERROR("Illegal virtual modifier %d (must be 0..%d inclusive)\n",
252                val_rtrn->uval, XkbNumVirtualMods - 1);
253     }
254     return False;
255 }