d4f243bc8c87707e2d49272b183f755c50526a89
[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     return;
45 }
46
47 void
48 ClearVModInfo(VModInfo * info, struct xkb_desc * 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, struct xkb_desc *xkb, unsigned mergeMode,
82               VModInfo * info)
83 {
84     register int i, bit, nextFree;
85     ExprResult mod;
86     struct xkb_server_map * srv;
87     struct xkb_names * names;
88
89     srv = xkb->server;
90     names = xkb->names;
91     for (i = 0, bit = 1, nextFree = -1; i < XkbNumVirtualMods; i++, bit <<= 1)
92     {
93         if (info->defined & bit)
94         {
95             if (names->vmods[i] == stmt->name)
96             {                   /* already defined */
97                 info->available |= bit;
98                 if (stmt->value == NULL)
99                     return True;
100                 else
101                 {
102                     const char *str1;
103                     const char *str2 = "";
104                     if (!ExprResolveModMask(stmt->value, &mod))
105                     {
106                         str1 = XkbcAtomText(stmt->name);
107                         ACTION("Declaration of %s ignored\n", str1);
108                         return False;
109                     }
110                     if (mod.uval == srv->vmods[i])
111                         return True;
112
113                     str1 = XkbcAtomText(stmt->name);
114                     WARN("Virtual modifier %s multiply defined\n", str1);
115                     str1 = XkbcModMaskText(srv->vmods[i], True);
116                     if (mergeMode == MergeOverride)
117                     {
118                         str2 = str1;
119                         str1 = XkbcModMaskText(mod.uval, True);
120                     }
121                     ACTION("Using %s, ignoring %s\n", str1, str2);
122                     if (mergeMode == MergeOverride)
123                         srv->vmods[i] = mod.uval;
124                     return True;
125                 }
126             }
127         }
128         else if (nextFree < 0)
129             nextFree = i;
130     }
131     if (nextFree < 0)
132     {
133         ERROR("Too many virtual modifiers defined (maximum %d)\n",
134                XkbNumVirtualMods);
135         return False;
136     }
137     info->defined |= (1 << nextFree);
138     info->newlyDefined |= (1 << nextFree);
139     info->available |= (1 << nextFree);
140     names->vmods[nextFree] = stmt->name;
141     if (stmt->value == NULL)
142         return True;
143     if (ExprResolveModMask(stmt->value, &mod))
144     {
145         srv->vmods[nextFree] = mod.uval;
146         return True;
147     }
148     ACTION("Declaration of %s ignored\n", XkbcAtomText(stmt->name));
149     return False;
150 }
151
152 /**
153  * Returns the index of the given modifier in the xkb->names->vmods array.
154  *
155  * @param priv Pointer to the xkb data structure.
156  * @param field The Atom of the modifier's name (e.g. Atom for LAlt)
157  * @param type Must be TypeInt, otherwise return False.
158  * @param val_rtrn Set to the index of the modifier that matches.
159  *
160  * @return True on success, False otherwise. If False is returned, val_rtrn is
161  * undefined.
162  */
163 static int
164 LookupVModIndex(struct xkb_desc *xkb, uint32_t field, unsigned type,
165                 ExprResult * val_rtrn)
166 {
167     int i;
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] == field)
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(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         register 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         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
241         {
242             if ((info->available & bit) && names->vmods[i] == def->value.str)
243             {
244                 val_rtrn->uval = i;
245                 return True;
246             }
247         }
248     }
249     if (ExprResolveInteger(def, val_rtrn))
250     {
251         if (val_rtrn->uval < XkbNumVirtualMods)
252             return True;
253         ERROR("Illegal virtual modifier %d (must be 0..%d inclusive)\n",
254                val_rtrn->uval, XkbNumVirtualMods - 1);
255     }
256     return False;
257 }