Remove useless stuff from utils
[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 #include <stdio.h>
28 #include "xkbcomp.h"
29 #include "xkballoc.h"
30 #include "xkbmisc.h"
31 #include "expr.h"
32 #include "misc.h"
33
34 #include <X11/extensions/XKB.h>
35
36 #include "vmod.h"
37
38 void
39 InitVModInfo(VModInfo * info, struct xkb_desc * xkb)
40 {
41     ClearVModInfo(info, xkb);
42     info->errorCount = 0;
43 }
44
45 void
46 ClearVModInfo(VModInfo * info, struct xkb_desc * xkb)
47 {
48     int i;
49
50     if (XkbcAllocNames(xkb, XkbVirtualModNamesMask, 0) != Success)
51         return;
52     if (XkbcAllocServerMap(xkb, XkbVirtualModsMask, 0) != Success)
53         return;
54     info->xkb = xkb;
55     info->newlyDefined = info->defined = info->available = 0;
56     if (xkb && xkb->names)
57     {
58         int bit;
59         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
60         {
61             if (xkb->names->vmods[i] != NULL)
62                 info->defined |= bit;
63         }
64     }
65 }
66
67 /***====================================================================***/
68
69 /**
70  * Handle one entry in the virtualModifiers line (e.g. NumLock).
71  * If the entry is e.g. NumLock=Mod1, stmt->value is not NULL, and the
72  * XkbServerMap's vmod is set to the given modifier. Otherwise, the vmod is 0.
73  *
74  * @param stmt The statement specifying the name and (if any the value).
75  * @param mergeMode Merge strategy (e.g. MergeOverride)
76  */
77 Bool
78 HandleVModDef(VModDef * stmt, struct xkb_desc *xkb, unsigned mergeMode,
79               VModInfo * info)
80 {
81     int i, bit, nextFree;
82     ExprResult mod;
83     struct xkb_server_map * srv;
84     struct xkb_names * names;
85
86     srv = xkb->server;
87     names = xkb->names;
88     for (i = 0, bit = 1, nextFree = -1; i < XkbNumVirtualMods; i++, bit <<= 1)
89     {
90         if (info->defined & bit)
91         {
92             if (names->vmods[i] &&
93                 strcmp(names->vmods[i], XkbcAtomText(stmt->name)) == 0)
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] = XkbcAtomGetString(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, xkb_atom_t field, unsigned type,
163                 ExprResult * val_rtrn)
164 {
165     int i;
166     const char *name = XkbcAtomText(field);
167
168     if ((xkb == NULL) || (xkb->names == NULL) || (type != TypeInt))
169     {
170         return False;
171     }
172     /* For each named modifier, get the name and compare it to the one passed
173      * in. If we get a match, return the index of the modifier.
174      * The order of modifiers is the same as in the virtual_modifiers line in
175      * the xkb_types section.
176      */
177     for (i = 0; i < XkbNumVirtualMods; i++)
178     {
179         if (xkb->names->vmods[i] && strcmp(xkb->names->vmods[i], name) == 0)
180         {
181             val_rtrn->uval = i;
182             return True;
183         }
184     }
185     return False;
186 }
187
188 /**
189  * Get the mask for the given (virtual or core) modifier and set
190  * val_rtrn.uval to the mask value.
191  *
192  * @param priv Pointer to xkb data structure.
193  * @param val_rtrn Member uval is set to the mask returned.
194  *
195  * @return True on success, False otherwise. If False is returned, val_rtrn is
196  * undefined.
197  */
198 int
199 LookupVModMask(const void * priv, xkb_atom_t field, unsigned type,
200                ExprResult * val_rtrn)
201 {
202     if (LookupModMask(NULL, field, type, val_rtrn))
203     {
204         return True;
205     }
206     else if (LookupVModIndex(priv, field, type, val_rtrn))
207     {
208         unsigned ndx = val_rtrn->uval;
209         val_rtrn->uval = (1 << (XkbNumModifiers + ndx));
210         return True;
211     }
212     return False;
213 }
214
215 int
216 FindKeypadVMod(struct xkb_desc * xkb)
217 {
218     xkb_atom_t name;
219     ExprResult rtrn;
220
221     name = xkb_intern_atom("NumLock");
222     if ((xkb) && LookupVModIndex(xkb, name, TypeInt, &rtrn))
223     {
224         return rtrn.ival;
225     }
226     return -1;
227 }
228
229 Bool
230 ResolveVirtualModifier(ExprDef * def, struct xkb_desc *xkb,
231                        ExprResult * val_rtrn, VModInfo * info)
232 {
233     struct xkb_names * names;
234
235     names = xkb->names;
236     if (def->op == ExprIdent)
237     {
238         int i, bit;
239         const char *name = XkbcAtomText(def->value.str);
240         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
241         {
242             if ((info->available & bit) && strcmp(names->vmods[i], name) == 0)
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 }