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