593cd13169400e09100b61bf5a9aea59c7000858
[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     xkb_mod_mask_t mask;
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 (!streq(keymap->vmod_names[i],
87                    xkb_atom_text(keymap->ctx, stmt->name)))
88             continue;
89
90         info->available |= bit;
91
92         if (!stmt->value)
93             return true;
94
95         if (!ExprResolveModMask(keymap->ctx, stmt->value, &mask)) {
96             log_err(keymap->ctx, "Declaration of %s ignored\n",
97                     xkb_atom_text(keymap->ctx, stmt->name));
98             return false;
99         }
100
101         if (mask == keymap->vmods[i])
102             return true;
103
104         str1 = ModMaskText(keymap->vmods[i], true);
105         if (mergeMode == MERGE_OVERRIDE) {
106             str2 = str1;
107             str1 = ModMaskText(mask, true);
108         }
109
110         log_warn(keymap->ctx,
111                  "Virtual modifier %s defined multiple times; "
112                  "Using %s, ignoring %s\n",
113                  xkb_atom_text(keymap->ctx, stmt->name), str1, str2);
114
115         if (mergeMode == MERGE_OVERRIDE)
116             keymap->vmods[i] = mask;
117
118         return true;
119     }
120
121     if (nextFree < 0) {
122         log_err(keymap->ctx,
123                 "Too many virtual modifiers defined (maximum %d)\n",
124                 XkbNumVirtualMods);
125         return false;
126     }
127
128     info->defined |= (1 << nextFree);
129     info->newlyDefined |= (1 << nextFree);
130     info->available |= (1 << nextFree);
131
132     keymap->vmod_names[nextFree] = xkb_atom_text(keymap->ctx, stmt->name);
133
134     if (!stmt->value)
135         return true;
136
137     if (!ExprResolveModMask(keymap->ctx, stmt->value, &mask)) {
138         log_err(keymap->ctx, "Declaration of %s ignored\n",
139                 xkb_atom_text(keymap->ctx, stmt->name));
140         return false;
141     }
142
143     keymap->vmods[nextFree] = mask;
144     return true;
145 }
146
147 /**
148  * Returns the index of the given modifier in the keymap->vmod_names array.
149  *
150  * @param keymap Pointer to the xkb data structure.
151  * @param field The Atom of the modifier's name (e.g. Atom for LAlt)
152  * @param type Must be EXPR_TYPE_INT, otherwise return false.
153  * @param val_rtrn Set to the index of the modifier that matches.
154  *
155  * @return true on success, false otherwise. If false is returned, val_rtrn is
156  * undefined.
157  */
158 static bool
159 LookupVModIndex(const struct xkb_keymap *keymap, xkb_atom_t field,
160                 enum expr_value_type type, xkb_mod_index_t *val_rtrn)
161 {
162     xkb_mod_index_t i;
163     const char *name = xkb_atom_text(keymap->ctx, field);
164
165     if (type != EXPR_TYPE_INT)
166         return false;
167
168     /* For each named modifier, get the name and compare it to the one passed
169      * in. If we get a match, return the index of the modifier.
170      * The order of modifiers is the same as in the virtual_modifiers line in
171      * the xkb_types section.
172      */
173     for (i = 0; i < XkbNumVirtualMods; i++) {
174         if (keymap->vmod_names[i] && streq(keymap->vmod_names[i], name)) {
175             *val_rtrn = i;
176             return true;
177         }
178     }
179
180     return false;
181 }
182
183 /**
184  * Get the mask for the given (virtual or core) modifier and set
185  * val_rtrn.uval to the mask value.
186  *
187  * @param priv Pointer to xkb data structure.
188  * @param val_rtrn Member uval is set to the mask returned.
189  *
190  * @return true on success, false otherwise. If false is returned, val_rtrn is
191  * undefined.
192  */
193 bool
194 LookupVModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
195                enum expr_value_type type, xkb_mod_mask_t *val_rtrn)
196 {
197     xkb_mod_index_t ndx;
198
199     if (LookupModMask(ctx, NULL, field, type, val_rtrn)) {
200         return true;
201     }
202     else if (LookupVModIndex(priv, field, type, &ndx)) {
203         *val_rtrn = (1 << (XkbNumModifiers + ndx));
204         return true;
205     }
206
207     return false;
208 }
209
210 xkb_mod_index_t
211 FindKeypadVMod(struct xkb_keymap *keymap)
212 {
213     xkb_atom_t name;
214     xkb_mod_index_t ndx;
215
216     name = xkb_atom_intern(keymap->ctx, "NumLock");
217     if (LookupVModIndex(keymap, name, EXPR_TYPE_INT, &ndx))
218         return ndx;
219
220     return -1;
221 }
222
223 bool
224 ResolveVirtualModifier(ExprDef *def, struct xkb_keymap *keymap,
225                        xkb_mod_index_t *ndx_rtrn, VModInfo *info)
226 {
227     int val;
228
229     if (def->op == EXPR_IDENT) {
230         xkb_mod_index_t i;
231         xkb_mod_mask_t bit;
232         const char *name = xkb_atom_text(keymap->ctx, def->value.str);
233
234         for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
235             if ((info->available & bit) && keymap->vmod_names[i] &&
236                 streq(keymap->vmod_names[i], name)) {
237                 *ndx_rtrn = i;
238                 return true;
239             }
240         }
241     }
242
243     if (!ExprResolveInteger(keymap->ctx, def, &val))
244         return false;
245
246     if (val < 0 || val >= XkbNumVirtualMods) {
247         log_err(keymap->ctx,
248                 "Illegal virtual modifier %d (must be 0..%d inclusive)\n",
249                 val, XkbNumVirtualMods - 1);
250         return false;
251     }
252
253     *ndx_rtrn = (xkb_mod_index_t) val;
254     return true;
255 }