70407f2dddec1ad88e6e43810e35127fed85fb5f
[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 "vmod.h"
28
29 void
30 InitVModInfo(VModInfo *info, struct xkb_keymap *keymap)
31 {
32     ClearVModInfo(info, keymap);
33     info->errorCount = 0;
34 }
35
36 void
37 ClearVModInfo(VModInfo *info, struct xkb_keymap *keymap)
38 {
39     xkb_mod_index_t i;
40     xkb_mod_mask_t bit;
41
42     info->newlyDefined = info->defined = info->available = 0;
43
44     for (i = 0; i < XkbNumVirtualMods; i++)
45         keymap->vmods[i] = XkbNoModifierMask;
46
47     for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1)
48         if (keymap->vmod_names[i])
49             info->defined |= bit;
50 }
51
52 /***====================================================================***/
53
54 /**
55  * Handle one entry in the virtualModifiers line (e.g. NumLock).
56  * If the entry is e.g. NumLock=Mod1, stmt->value is not NULL, and the
57  * XkbServerMap's vmod is set to the given modifier. Otherwise, the vmod is 0.
58  *
59  * @param stmt The statement specifying the name and (if any the value).
60  * @param mergeMode Merge strategy (e.g. MERGE_OVERRIDE)
61  */
62 bool
63 HandleVModDef(VModDef *stmt, struct xkb_keymap *keymap,
64               enum merge_mode mergeMode, VModInfo *info)
65 {
66     xkb_mod_index_t i;
67     int nextFree;
68     xkb_mod_mask_t bit;
69     ExprResult mod;
70
71     nextFree = -1;
72     for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
73         const char *str1;
74         const char *str2 = "";
75
76         if (!(info->defined & bit)) {
77             if (nextFree < 0)
78                 nextFree = i;
79             continue;
80         }
81
82         /* already defined */
83         if (!keymap->vmod_names[i])
84             continue;
85
86         if (strcmp(keymap->vmod_names[i],
87                    xkb_atom_text(keymap->ctx, stmt->name)) != 0)
88             continue;
89
90
91         info->available |= bit;
92
93         if (!stmt->value)
94             return true;
95
96         if (!ExprResolveModMask(keymap->ctx, stmt->value, &mod)) {
97             log_err(keymap->ctx, "Declaration of %s ignored\n",
98                     xkb_atom_text(keymap->ctx, stmt->name));
99             return false;
100         }
101
102         if (mod.uval == keymap->vmods[i])
103             return true;
104
105         str1 = XkbcModMaskText(keymap->vmods[i], true);
106         if (mergeMode == MERGE_OVERRIDE) {
107             str2 = str1;
108             str1 = XkbcModMaskText(mod.uval, true);
109         }
110
111         log_warn(keymap->ctx,
112                  "Virtual modifier %s defined multiple times; "
113                  "Using %s, ignoring %s\n",
114                  xkb_atom_text(keymap->ctx, stmt->name), str1, str2);
115
116         if (mergeMode == MERGE_OVERRIDE)
117             keymap->vmods[i] = mod.uval;
118
119         return true;
120     }
121
122     if (nextFree < 0) {
123         log_err(keymap->ctx,
124                 "Too many virtual modifiers defined (maximum %d)\n",
125                 XkbNumVirtualMods);
126         return false;
127     }
128
129     info->defined |= (1 << nextFree);
130     info->newlyDefined |= (1 << nextFree);
131     info->available |= (1 << nextFree);
132
133     keymap->vmod_names[nextFree] = xkb_atom_text(keymap->ctx, stmt->name);
134
135     if (!stmt->value)
136         return true;
137
138     if (!ExprResolveModMask(keymap->ctx, stmt->value, &mod)) {
139         log_err(keymap->ctx, "Declaration of %s ignored\n",
140                 xkb_atom_text(keymap->ctx, stmt->name));
141         return false;
142     }
143
144     keymap->vmods[nextFree] = mod.uval;
145     return true;
146 }
147
148 /**
149  * Returns the index of the given modifier in the keymap->vmod_names array.
150  *
151  * @param keymap Pointer to the xkb data structure.
152  * @param field The Atom of the modifier's name (e.g. Atom for LAlt)
153  * @param type Must be TypeInt, otherwise return false.
154  * @param val_rtrn Set to the index of the modifier that matches.
155  *
156  * @return true on success, false otherwise. If false is returned, val_rtrn is
157  * undefined.
158  */
159 static int
160 LookupVModIndex(const struct xkb_keymap *keymap, xkb_atom_t field,
161                 unsigned type, ExprResult * val_rtrn)
162 {
163     xkb_mod_index_t i;
164     const char *name = xkb_atom_text(keymap->ctx, field);
165
166     if (type != TypeInt)
167         return false;
168
169     /* For each named modifier, get the name and compare it to the one passed
170      * in. If we get a match, return the index of the modifier.
171      * The order of modifiers is the same as in the virtual_modifiers line in
172      * the xkb_types section.
173      */
174     for (i = 0; i < XkbNumVirtualMods; i++)
175         if (keymap->vmod_names[i] &&
176             strcmp(keymap->vmod_names[i], name) == 0) {
177             val_rtrn->uval = i;
178             return true;
179         }
180
181     return false;
182 }
183
184 /**
185  * Get the mask for the given (virtual or core) modifier and set
186  * val_rtrn.uval to the mask value.
187  *
188  * @param priv Pointer to xkb data structure.
189  * @param val_rtrn Member uval is set to the mask returned.
190  *
191  * @return true on success, false otherwise. If false is returned, val_rtrn is
192  * undefined.
193  */
194 bool
195 LookupVModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
196                unsigned type, ExprResult *val_rtrn)
197 {
198     if (LookupModMask(ctx, NULL, field, type, val_rtrn)) {
199         return true;
200     }
201     else if (LookupVModIndex(priv, field, type, val_rtrn)) {
202         unsigned ndx = val_rtrn->uval;
203         val_rtrn->uval = (1 << (XkbNumModifiers + ndx));
204         return true;
205     }
206     return false;
207 }
208
209 int
210 FindKeypadVMod(struct xkb_keymap *keymap)
211 {
212     xkb_atom_t name;
213     ExprResult rtrn;
214
215     name = xkb_atom_intern(keymap->ctx, "NumLock");
216     if ((keymap) && LookupVModIndex(keymap, name, TypeInt, &rtrn)) {
217         return rtrn.ival;
218     }
219     return -1;
220 }
221
222 bool
223 ResolveVirtualModifier(ExprDef *def, struct xkb_keymap *keymap,
224                        ExprResult *val_rtrn, VModInfo *info)
225 {
226     if (def->op == ExprIdent) {
227         xkb_mod_index_t i;
228         xkb_mod_mask_t bit;
229         const char *name = xkb_atom_text(keymap->ctx, def->value.str);
230
231         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
232             if ((info->available & bit) && keymap->vmod_names[i] &&
233                 strcmp(keymap->vmod_names[i], name) == 0) {
234                 val_rtrn->uval = i;
235                 return true;
236             }
237         }
238     }
239     if (ExprResolveInteger(keymap->ctx, def, val_rtrn)) {
240         if (val_rtrn->uval < XkbNumVirtualMods)
241             return true;
242         log_err(keymap->ctx,
243                 "Illegal virtual modifier %d (must be 0..%d inclusive)\n",
244                 val_rtrn->uval, XkbNumVirtualMods - 1);
245     }
246     return false;
247 }