libxkbcomp: s/XkbDescPtr/XkbcDescPtr/
[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 (XkbAllocNames(xkb, XkbVirtualModNamesMask, 0, 0) != Success)
53         return;
54     if (XkbAllocServerMap(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 =
92         XkbInternAtom(info->xkb->dpy, XkbAtomGetString(NULL, stmt->name),
93                       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 = XkbAtomText(NULL, stmt->name, XkbMessage);
110                         ACTION1("Declaration of %s ignored\n", str1);
111                         return False;
112                     }
113                     if (mod.uval == srv->vmods[i])
114                         return True;
115
116                     str1 = XkbAtomText(NULL, stmt->name, XkbMessage);
117                     WARN1("Virtual modifier %s multiply defined\n", str1);
118                     str1 = XkbModMaskText(srv->vmods[i], XkbCFile);
119                     if (mergeMode == MergeOverride)
120                     {
121                         str2 = str1;
122                         str1 = XkbModMaskText(mod.uval, XkbCFile);
123                     }
124                     ACTION2("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         ERROR1("Too many virtual modifiers defined (maximum %d)\n",
137                XkbNumVirtualMods);
138         ACTION("Exiting\n");
139         return False;
140     }
141     info->defined |= (1 << nextFree);
142     info->newlyDefined |= (1 << nextFree);
143     info->available |= (1 << nextFree);
144     names->vmods[nextFree] = stmtName;
145     if (stmt->value == NULL)
146         return True;
147     if (ExprResolveModMask(stmt->value, &mod, NULL, NULL))
148     {
149         srv->vmods[nextFree] = mod.uval;
150         return True;
151     }
152     ACTION1("Declaration of %s ignored\n",
153             XkbAtomText(NULL, stmt->name, XkbMessage));
154     return False;
155 }
156
157 /**
158  * Returns the index of the given modifier in the xkb->names->vmods array.
159  *
160  * @param priv Pointer to the xkb data structure.
161  * @param elem Must be None, otherwise return False.
162  * @param field The Atom of the modifier's name (e.g. Atom for LAlt)
163  * @param type Must be TypeInt, otherwise return False.
164  * @param val_rtrn Set to the index of the modifier that matches.
165  *
166  * @return True on success, False otherwise. If False is returned, val_rtrn is
167  * undefined.
168  */
169 int
170 LookupVModIndex(XPointer priv,
171                 Atom elem, Atom field, unsigned type, ExprResult * val_rtrn)
172 {
173     register int i;
174     register char *fieldStr;
175     register char *modStr;
176     XkbcDescPtr xkb;
177
178     xkb = (XkbcDescPtr) priv;
179     if ((xkb == NULL) || (xkb->names == NULL) || (elem != None)
180         || (type != TypeInt))
181     {
182         return False;
183     }
184     /* get the actual name */
185     fieldStr = XkbAtomGetString(xkb->dpy, field);
186     if (fieldStr == NULL)
187         return False;
188     /* For each named modifier, get the name and compare it to the one passed
189      * in. If we get a match, return the index of the modifier.
190      * The order of modifiers is the same as in the virtual_modifiers line in
191      * the xkb_types section.
192      */
193     for (i = 0; i < XkbNumVirtualMods; i++)
194     {
195         modStr = XkbAtomGetString(xkb->dpy, xkb->names->vmods[i]);
196         if ((modStr != NULL) && (uStrCaseCmp(fieldStr, modStr) == 0))
197         {
198             val_rtrn->uval = i;
199             return True;
200         }
201     }
202     return False;
203 }
204
205 /**
206  * Get the mask for the given modifier and set val_rtrn.uval to the mask.
207  * Note that the mask returned is always > 512.
208  *
209  * @param priv Pointer to xkb data structure.
210  * @param val_rtrn Set to the mask returned.
211  *
212  * @return True on success, False otherwise. If False is returned, val_rtrn is
213  * undefined.
214  */
215 int
216 LookupVModMask(XPointer priv,
217                Atom elem, Atom field, unsigned type, ExprResult * val_rtrn)
218 {
219     if (LookupVModIndex(priv, elem, field, type, val_rtrn))
220     {
221         register unsigned ndx = val_rtrn->uval;
222         val_rtrn->uval = (1 << (XkbNumModifiers + ndx));
223         return True;
224     }
225     return False;
226 }
227
228 int
229 FindKeypadVMod(XkbcDescPtr xkb)
230 {
231     Atom name;
232     ExprResult rtrn;
233
234     name = XkbInternAtom(xkb->dpy, "NumLock", False);
235     if ((xkb) && LookupVModIndex((XPointer) xkb, None, name, TypeInt, &rtrn))
236     {
237         return rtrn.ival;
238     }
239     return -1;
240 }
241
242 Bool
243 ResolveVirtualModifier(ExprDef * def, ExprResult * val_rtrn, VModInfo * info)
244 {
245     XkbNamesPtr names;
246
247     names = info->xkb->names;
248     if (def->op == ExprIdent)
249     {
250         register int i, bit;
251         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
252         {
253             char *str1, *str2;
254             str1 = XkbAtomGetString(info->xkb->dpy, names->vmods[i]);
255             str2 = XkbAtomGetString(NULL, def->value.str);
256             if ((info->available & bit) && (uStrCaseCmp(str1, str2) == Equal))
257             {
258                 val_rtrn->uval = i;
259                 return True;
260             }
261         }
262     }
263     if (ExprResolveInteger(def, val_rtrn, NULL, NULL))
264     {
265         if (val_rtrn->uval < XkbNumVirtualMods)
266             return True;
267         ERROR2("Illegal virtual modifier %d (must be 0..%d inclusive)\n",
268                val_rtrn->uval, XkbNumVirtualMods - 1);
269     }
270     return False;
271 }