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