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 /*
28 * Copyright © 2012 Intel Corporation
29 * Copyright © 2012 Ran Benita <ran234@gmail.com>
30 *
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
37 *
38 * The above copyright notice and this permission notice (including the next
39 * paragraph) shall be included in all copies or substantial portions of the
40 * Software.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
49 *
50 * Author: Daniel Stone <daniel@fooishbar.org>
51 * Ran Benita <ran234@gmail.com>
52 */
53
54 #include "config.h"
55
56 #include "xkbcomp-priv.h"
57 #include "text.h"
58 #include "expr.h"
59 #include "action.h"
60 #include "vmod.h"
61 #include "include.h"
62 #include "keysym.h"
63
64 enum key_repeat {
65 KEY_REPEAT_UNDEFINED = 0,
66 KEY_REPEAT_YES = 1,
67 KEY_REPEAT_NO = 2,
68 };
69
70 enum group_field {
71 GROUP_FIELD_SYMS = (1 << 0),
72 GROUP_FIELD_ACTS = (1 << 1),
73 GROUP_FIELD_TYPE = (1 << 2),
74 };
75
76 enum key_field {
77 KEY_FIELD_REPEAT = (1 << 0),
78 KEY_FIELD_DEFAULT_TYPE = (1 << 1),
79 KEY_FIELD_GROUPINFO = (1 << 2),
80 KEY_FIELD_VMODMAP = (1 << 3),
81 };
82
83 typedef struct {
84 enum group_field defined;
85 darray(struct xkb_level) levels;
86 xkb_atom_t type;
87 } GroupInfo;
88
89 typedef struct {
90 enum key_field defined;
91 enum merge_mode merge;
92
93 xkb_atom_t name;
94
95 darray(GroupInfo) groups;
96
97 enum key_repeat repeat;
98 xkb_mod_mask_t vmodmap;
99 xkb_atom_t default_type;
100
101 enum xkb_range_exceed_type out_of_range_group_action;
102 xkb_layout_index_t out_of_range_group_number;
103 } KeyInfo;
104
105 static void
ClearLevelInfo(struct xkb_level * leveli)106 ClearLevelInfo(struct xkb_level *leveli)
107 {
108 if (leveli->num_syms > 1)
109 free(leveli->u.syms);
110 }
111
112 static void
InitGroupInfo(GroupInfo * groupi)113 InitGroupInfo(GroupInfo *groupi)
114 {
115 memset(groupi, 0, sizeof(*groupi));
116 }
117
118 static void
ClearGroupInfo(GroupInfo * groupi)119 ClearGroupInfo(GroupInfo *groupi)
120 {
121 struct xkb_level *leveli;
122 darray_foreach(leveli, groupi->levels)
123 ClearLevelInfo(leveli);
124 darray_free(groupi->levels);
125 }
126
127 static void
CopyGroupInfo(GroupInfo * to,const GroupInfo * from)128 CopyGroupInfo(GroupInfo *to, const GroupInfo *from)
129 {
130 to->defined = from->defined;
131 to->type = from->type;
132 darray_init(to->levels);
133 darray_copy(to->levels, from->levels);
134 for (xkb_level_index_t j = 0; j < darray_size(to->levels); j++)
135 if (darray_item(from->levels, j).num_syms > 1)
136 darray_item(to->levels, j).u.syms =
137 memdup(darray_item(from->levels, j).u.syms,
138 darray_item(from->levels, j).num_syms,
139 sizeof(xkb_keysym_t));
140 }
141
142 static void
InitKeyInfo(struct xkb_context * ctx,KeyInfo * keyi)143 InitKeyInfo(struct xkb_context *ctx, KeyInfo *keyi)
144 {
145 memset(keyi, 0, sizeof(*keyi));
146 keyi->merge = MERGE_OVERRIDE;
147 keyi->name = xkb_atom_intern_literal(ctx, "*");
148 keyi->out_of_range_group_action = RANGE_WRAP;
149 }
150
151 static void
ClearKeyInfo(KeyInfo * keyi)152 ClearKeyInfo(KeyInfo *keyi)
153 {
154 GroupInfo *groupi;
155 darray_foreach(groupi, keyi->groups)
156 ClearGroupInfo(groupi);
157 darray_free(keyi->groups);
158 }
159
160 /***====================================================================***/
161
162 typedef struct {
163 enum merge_mode merge;
164 bool haveSymbol;
165 xkb_mod_index_t modifier;
166 union {
167 xkb_atom_t keyName;
168 xkb_keysym_t keySym;
169 } u;
170 } ModMapEntry;
171
172 typedef struct {
173 char *name; /* e.g. pc+us+inet(evdev) */
174 int errorCount;
175 enum merge_mode merge;
176 xkb_layout_index_t explicit_group;
177 darray(KeyInfo) keys;
178 KeyInfo default_key;
179 ActionsInfo *actions;
180 darray(xkb_atom_t) group_names;
181 darray(ModMapEntry) modmaps;
182 struct xkb_mod_set mods;
183
184 struct xkb_context *ctx;
185 /* Needed for AddKeySymbols. */
186 const struct xkb_keymap *keymap;
187 } SymbolsInfo;
188
189 static void
InitSymbolsInfo(SymbolsInfo * info,const struct xkb_keymap * keymap,ActionsInfo * actions,const struct xkb_mod_set * mods)190 InitSymbolsInfo(SymbolsInfo *info, const struct xkb_keymap *keymap,
191 ActionsInfo *actions, const struct xkb_mod_set *mods)
192 {
193 memset(info, 0, sizeof(*info));
194 info->ctx = keymap->ctx;
195 info->keymap = keymap;
196 info->merge = MERGE_OVERRIDE;
197 InitKeyInfo(keymap->ctx, &info->default_key);
198 info->actions = actions;
199 info->mods = *mods;
200 info->explicit_group = XKB_LAYOUT_INVALID;
201 }
202
203 static void
ClearSymbolsInfo(SymbolsInfo * info)204 ClearSymbolsInfo(SymbolsInfo *info)
205 {
206 KeyInfo *keyi;
207 free(info->name);
208 darray_foreach(keyi, info->keys)
209 ClearKeyInfo(keyi);
210 darray_free(info->keys);
211 darray_free(info->group_names);
212 darray_free(info->modmaps);
213 ClearKeyInfo(&info->default_key);
214 }
215
216 static const char *
KeyInfoText(SymbolsInfo * info,KeyInfo * keyi)217 KeyInfoText(SymbolsInfo *info, KeyInfo *keyi)
218 {
219 return KeyNameText(info->ctx, keyi->name);
220 }
221
222 static bool
MergeGroups(SymbolsInfo * info,GroupInfo * into,GroupInfo * from,bool clobber,bool report,xkb_layout_index_t group,xkb_atom_t key_name)223 MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
224 bool report, xkb_layout_index_t group, xkb_atom_t key_name)
225 {
226 xkb_level_index_t i, levels_in_both;
227 struct xkb_level *level;
228
229 /* First find the type of the merged group. */
230 if (into->type != from->type) {
231 if (from->type == XKB_ATOM_NONE) {
232 /* it's empty for consistency with other comparisons */
233 }
234 else if (into->type == XKB_ATOM_NONE) {
235 into->type = from->type;
236 }
237 else {
238 xkb_atom_t use = (clobber ? from->type : into->type);
239 xkb_atom_t ignore = (clobber ? into->type : from->type);
240
241 if (report)
242 log_warn(info->ctx,
243 "Multiple definitions for group %d type of key %s; "
244 "Using %s, ignoring %s\n",
245 group + 1, KeyNameText(info->ctx, key_name),
246 xkb_atom_text(info->ctx, use),
247 xkb_atom_text(info->ctx, ignore));
248
249 into->type = use;
250 }
251 }
252 into->defined |= (from->defined & GROUP_FIELD_TYPE);
253
254 /* Now look at the levels. */
255
256 if (darray_empty(from->levels)) {
257 InitGroupInfo(from);
258 return true;
259 }
260
261 if (darray_empty(into->levels)) {
262 from->type = into->type;
263 *into = *from;
264 InitGroupInfo(from);
265 return true;
266 }
267
268 /* Merge the actions and syms. */
269 levels_in_both = MIN(darray_size(into->levels), darray_size(from->levels));
270 for (i = 0; i < levels_in_both; i++) {
271 struct xkb_level *intoLevel = &darray_item(into->levels, i);
272 struct xkb_level *fromLevel = &darray_item(from->levels, i);
273
274 if (fromLevel->action.type == ACTION_TYPE_NONE) {
275 /* it's empty for consistency with other comparisons */
276 }
277 else if (intoLevel->action.type == ACTION_TYPE_NONE) {
278 intoLevel->action = fromLevel->action;
279 }
280 else {
281 union xkb_action *use, *ignore;
282 use = (clobber ? &fromLevel->action : &intoLevel->action);
283 ignore = (clobber ? &intoLevel->action : &fromLevel->action);
284
285 if (report)
286 log_warn(info->ctx,
287 "Multiple actions for level %d/group %u on key %s; "
288 "Using %s, ignoring %s\n",
289 i + 1, group + 1, KeyNameText(info->ctx, key_name),
290 ActionTypeText(use->type),
291 ActionTypeText(ignore->type));
292
293 intoLevel->action = *use;
294 }
295
296 if (fromLevel->num_syms == 0) {
297 /* it's empty for consistency with other comparisons */
298 }
299 else if (intoLevel->num_syms == 0) {
300 intoLevel->num_syms = fromLevel->num_syms;
301 if (fromLevel->num_syms > 1)
302 intoLevel->u.syms = fromLevel->u.syms;
303 else
304 intoLevel->u.sym = fromLevel->u.sym;
305 fromLevel->num_syms = 0;
306 }
307 else if (!XkbLevelsSameSyms(fromLevel, intoLevel)) {
308 if (report)
309 log_warn(info->ctx,
310 "Multiple symbols for level %d/group %u on key %s; "
311 "Using %s, ignoring %s\n",
312 i + 1, group + 1, KeyNameText(info->ctx, key_name),
313 (clobber ? "from" : "to"),
314 (clobber ? "to" : "from"));
315
316 if (clobber) {
317 ClearLevelInfo(intoLevel);
318 intoLevel->num_syms = fromLevel->num_syms;
319 if (fromLevel->num_syms > 1)
320 intoLevel->u.syms = fromLevel->u.syms;
321 else
322 intoLevel->u.sym = fromLevel->u.sym;
323 fromLevel->num_syms = 0;
324 }
325 }
326 }
327 /* If @from has extra levels, get them as well. */
328 darray_foreach_from(level, from->levels, levels_in_both) {
329 darray_append(into->levels, *level);
330 level->num_syms = 0;
331 }
332 into->defined |= (from->defined & GROUP_FIELD_ACTS);
333 into->defined |= (from->defined & GROUP_FIELD_SYMS);
334
335 return true;
336 }
337
338 static bool
UseNewKeyField(enum key_field field,enum key_field old,enum key_field new,bool clobber,bool report,enum key_field * collide)339 UseNewKeyField(enum key_field field, enum key_field old, enum key_field new,
340 bool clobber, bool report, enum key_field *collide)
341 {
342 if (!(old & field))
343 return (new & field);
344
345 if (new & field) {
346 if (report)
347 *collide |= field;
348
349 if (clobber)
350 return true;
351 }
352
353 return false;
354 }
355
356 static bool
MergeKeys(SymbolsInfo * info,KeyInfo * into,KeyInfo * from,bool same_file)357 MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from, bool same_file)
358 {
359 xkb_layout_index_t i;
360 xkb_layout_index_t groups_in_both;
361 enum key_field collide = 0;
362 const int verbosity = xkb_context_get_log_verbosity(info->ctx);
363 const bool clobber = (from->merge != MERGE_AUGMENT);
364 const bool report = (same_file && verbosity > 0) || verbosity > 9;
365
366 if (from->merge == MERGE_REPLACE) {
367 ClearKeyInfo(into);
368 *into = *from;
369 InitKeyInfo(info->ctx, from);
370 return true;
371 }
372
373 groups_in_both = MIN(darray_size(into->groups), darray_size(from->groups));
374 for (i = 0; i < groups_in_both; i++)
375 MergeGroups(info,
376 &darray_item(into->groups, i),
377 &darray_item(from->groups, i),
378 clobber, report, i, into->name);
379 /* If @from has extra groups, just move them to @into. */
380 for (i = groups_in_both; i < darray_size(from->groups); i++) {
381 darray_append(into->groups, darray_item(from->groups, i));
382 InitGroupInfo(&darray_item(from->groups, i));
383 }
384
385 if (UseNewKeyField(KEY_FIELD_VMODMAP, into->defined, from->defined,
386 clobber, report, &collide)) {
387 into->vmodmap = from->vmodmap;
388 into->defined |= KEY_FIELD_VMODMAP;
389 }
390 if (UseNewKeyField(KEY_FIELD_REPEAT, into->defined, from->defined,
391 clobber, report, &collide)) {
392 into->repeat = from->repeat;
393 into->defined |= KEY_FIELD_REPEAT;
394 }
395 if (UseNewKeyField(KEY_FIELD_DEFAULT_TYPE, into->defined, from->defined,
396 clobber, report, &collide)) {
397 into->default_type = from->default_type;
398 into->defined |= KEY_FIELD_DEFAULT_TYPE;
399 }
400 if (UseNewKeyField(KEY_FIELD_GROUPINFO, into->defined, from->defined,
401 clobber, report, &collide)) {
402 into->out_of_range_group_action = from->out_of_range_group_action;
403 into->out_of_range_group_number = from->out_of_range_group_number;
404 into->defined |= KEY_FIELD_GROUPINFO;
405 }
406
407 if (collide)
408 log_warn(info->ctx,
409 "Symbol map for key %s redefined; "
410 "Using %s definition for conflicting fields\n",
411 KeyNameText(info->ctx, into->name),
412 (clobber ? "first" : "last"));
413
414 ClearKeyInfo(from);
415 InitKeyInfo(info->ctx, from);
416 return true;
417 }
418
419 /* TODO: Make it so this function doesn't need the entire keymap. */
420 static bool
AddKeySymbols(SymbolsInfo * info,KeyInfo * keyi,bool same_file)421 AddKeySymbols(SymbolsInfo *info, KeyInfo *keyi, bool same_file)
422 {
423 xkb_atom_t real_name;
424 KeyInfo *iter;
425
426 /*
427 * Don't keep aliases in the keys array; this guarantees that
428 * searching for keys to merge with by straight comparison (see the
429 * following loop) is enough, and we won't get multiple KeyInfo's
430 * for the same key because of aliases.
431 */
432 real_name = XkbResolveKeyAlias(info->keymap, keyi->name);
433 if (real_name != XKB_ATOM_NONE)
434 keyi->name = real_name;
435
436 darray_foreach(iter, info->keys)
437 if (iter->name == keyi->name)
438 return MergeKeys(info, iter, keyi, same_file);
439
440 darray_append(info->keys, *keyi);
441 InitKeyInfo(info->ctx, keyi);
442 return true;
443 }
444
445 static bool
AddModMapEntry(SymbolsInfo * info,ModMapEntry * new)446 AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
447 {
448 ModMapEntry *old;
449 bool clobber = (new->merge != MERGE_AUGMENT);
450
451 darray_foreach(old, info->modmaps) {
452 xkb_mod_index_t use, ignore;
453
454 if ((new->haveSymbol != old->haveSymbol) ||
455 (new->haveSymbol && new->u.keySym != old->u.keySym) ||
456 (!new->haveSymbol && new->u.keyName != old->u.keyName))
457 continue;
458
459 if (new->modifier == old->modifier)
460 return true;
461
462 use = (clobber ? new->modifier : old->modifier);
463 ignore = (clobber ? old->modifier : new->modifier);
464
465 if (new->haveSymbol)
466 log_warn(info->ctx,
467 "Symbol \"%s\" added to modifier map for multiple modifiers; "
468 "Using %s, ignoring %s\n",
469 KeysymText(info->ctx, new->u.keySym),
470 ModIndexText(info->ctx, &info->mods, use),
471 ModIndexText(info->ctx, &info->mods, ignore));
472 else
473 log_warn(info->ctx,
474 "Key \"%s\" added to modifier map for multiple modifiers; "
475 "Using %s, ignoring %s\n",
476 KeyNameText(info->ctx, new->u.keyName),
477 ModIndexText(info->ctx, &info->mods, use),
478 ModIndexText(info->ctx, &info->mods, ignore));
479
480 old->modifier = use;
481 return true;
482 }
483
484 darray_append(info->modmaps, *new);
485 return true;
486 }
487
488 /***====================================================================***/
489
490 static void
MergeIncludedSymbols(SymbolsInfo * into,SymbolsInfo * from,enum merge_mode merge)491 MergeIncludedSymbols(SymbolsInfo *into, SymbolsInfo *from,
492 enum merge_mode merge)
493 {
494 xkb_atom_t *group_name;
495 xkb_layout_index_t group_names_in_both;
496
497 if (from->errorCount > 0) {
498 into->errorCount += from->errorCount;
499 return;
500 }
501
502 into->mods = from->mods;
503
504 if (into->name == NULL) {
505 into->name = from->name;
506 from->name = NULL;
507 }
508
509 group_names_in_both = MIN(darray_size(into->group_names),
510 darray_size(from->group_names));
511 for (xkb_layout_index_t i = 0; i < group_names_in_both; i++) {
512 if (!darray_item(from->group_names, i))
513 continue;
514
515 if (merge == MERGE_AUGMENT && darray_item(into->group_names, i))
516 continue;
517
518 darray_item(into->group_names, i) = darray_item(from->group_names, i);
519 }
520 /* If @from has more, get them as well. */
521 darray_foreach_from(group_name, from->group_names, group_names_in_both)
522 darray_append(into->group_names, *group_name);
523
524 if (darray_empty(into->keys)) {
525 into->keys = from->keys;
526 darray_init(from->keys);
527 }
528 else {
529 KeyInfo *keyi;
530 darray_foreach(keyi, from->keys) {
531 keyi->merge = (merge == MERGE_DEFAULT ? keyi->merge : merge);
532 if (!AddKeySymbols(into, keyi, false))
533 into->errorCount++;
534 }
535 }
536
537 if (darray_empty(into->modmaps)) {
538 into->modmaps = from->modmaps;
539 darray_init(from->modmaps);
540 }
541 else {
542 ModMapEntry *mm;
543 darray_foreach(mm, from->modmaps) {
544 mm->merge = (merge == MERGE_DEFAULT ? mm->merge : merge);
545 if (!AddModMapEntry(into, mm))
546 into->errorCount++;
547 }
548 }
549 }
550
551 static void
552 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge);
553
554 static bool
HandleIncludeSymbols(SymbolsInfo * info,IncludeStmt * include)555 HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
556 {
557 SymbolsInfo included;
558
559 InitSymbolsInfo(&included, info->keymap, info->actions, &info->mods);
560 included.name = include->stmt;
561 include->stmt = NULL;
562
563 for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) {
564 SymbolsInfo next_incl;
565 XkbFile *file;
566
567 file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_SYMBOLS);
568 if (!file) {
569 info->errorCount += 10;
570 ClearSymbolsInfo(&included);
571 return false;
572 }
573
574 InitSymbolsInfo(&next_incl, info->keymap, info->actions,
575 &included.mods);
576 if (stmt->modifier) {
577 next_incl.explicit_group = atoi(stmt->modifier) - 1;
578 if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
579 log_err(info->ctx,
580 "Cannot set explicit group to %d - must be between 1..%d; "
581 "Ignoring group number\n",
582 next_incl.explicit_group + 1, XKB_MAX_GROUPS);
583 next_incl.explicit_group = info->explicit_group;
584 }
585 }
586 else {
587 next_incl.explicit_group = info->explicit_group;
588 }
589
590 HandleSymbolsFile(&next_incl, file, MERGE_OVERRIDE);
591
592 MergeIncludedSymbols(&included, &next_incl, stmt->merge);
593
594 ClearSymbolsInfo(&next_incl);
595 FreeXkbFile(file);
596 }
597
598 MergeIncludedSymbols(info, &included, include->merge);
599 ClearSymbolsInfo(&included);
600
601 return (info->errorCount == 0);
602 }
603
604 #define SYMBOLS 1
605 #define ACTIONS 2
606
607 static bool
GetGroupIndex(SymbolsInfo * info,KeyInfo * keyi,ExprDef * arrayNdx,unsigned what,xkb_layout_index_t * ndx_rtrn)608 GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
609 unsigned what, xkb_layout_index_t *ndx_rtrn)
610 {
611 const char *name = (what == SYMBOLS ? "symbols" : "actions");
612
613 if (arrayNdx == NULL) {
614 xkb_layout_index_t i;
615 GroupInfo *groupi;
616 enum group_field field = (what == SYMBOLS ?
617 GROUP_FIELD_SYMS : GROUP_FIELD_ACTS);
618
619 darray_enumerate(i, groupi, keyi->groups) {
620 if (!(groupi->defined & field)) {
621 *ndx_rtrn = i;
622 return true;
623 }
624 }
625
626 if (i >= XKB_MAX_GROUPS) {
627 log_err(info->ctx,
628 "Too many groups of %s for key %s (max %u); "
629 "Ignoring %s defined for extra groups\n",
630 name, KeyInfoText(info, keyi), XKB_MAX_GROUPS, name);
631 return false;
632 }
633
634 darray_resize0(keyi->groups, darray_size(keyi->groups) + 1);
635 *ndx_rtrn = darray_size(keyi->groups) - 1;
636 return true;
637 }
638
639 if (!ExprResolveGroup(info->ctx, arrayNdx, ndx_rtrn)) {
640 log_err(info->ctx,
641 "Illegal group index for %s of key %s\n"
642 "Definition with non-integer array index ignored\n",
643 name, KeyInfoText(info, keyi));
644 return false;
645 }
646
647 (*ndx_rtrn)--;
648 if (*ndx_rtrn >= darray_size(keyi->groups))
649 darray_resize0(keyi->groups, *ndx_rtrn + 1);
650
651 return true;
652 }
653
654 static bool
AddSymbolsToKey(SymbolsInfo * info,KeyInfo * keyi,ExprDef * arrayNdx,ExprDef * value)655 AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
656 ExprDef *value)
657 {
658 xkb_layout_index_t ndx;
659 GroupInfo *groupi;
660 xkb_level_index_t nLevels;
661
662 if (!GetGroupIndex(info, keyi, arrayNdx, SYMBOLS, &ndx))
663 return false;
664
665 groupi = &darray_item(keyi->groups, ndx);
666
667 if (value == NULL) {
668 groupi->defined |= GROUP_FIELD_SYMS;
669 return true;
670 }
671
672 if (value->expr.op != EXPR_KEYSYM_LIST) {
673 log_err(info->ctx,
674 "Expected a list of symbols, found %s; "
675 "Ignoring symbols for group %u of %s\n",
676 expr_op_type_to_string(value->expr.op), ndx + 1,
677 KeyInfoText(info, keyi));
678 return false;
679 }
680
681 if (groupi->defined & GROUP_FIELD_SYMS) {
682 log_err(info->ctx,
683 "Symbols for key %s, group %u already defined; "
684 "Ignoring duplicate definition\n",
685 KeyInfoText(info, keyi), ndx + 1);
686 return false;
687 }
688
689 nLevels = darray_size(value->keysym_list.symsMapIndex);
690 if (darray_size(groupi->levels) < nLevels)
691 darray_resize0(groupi->levels, nLevels);
692
693 groupi->defined |= GROUP_FIELD_SYMS;
694
695 for (xkb_level_index_t i = 0; i < nLevels; i++) {
696 unsigned int sym_index;
697 struct xkb_level *leveli = &darray_item(groupi->levels, i);
698
699 sym_index = darray_item(value->keysym_list.symsMapIndex, i);
700 leveli->num_syms = darray_item(value->keysym_list.symsNumEntries, i);
701 if (leveli->num_syms > 1)
702 leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
703
704 for (unsigned j = 0; j < leveli->num_syms; j++) {
705 xkb_keysym_t keysym = darray_item(value->keysym_list.syms,
706 sym_index + j);
707
708 if (leveli->num_syms == 1) {
709 if (keysym == XKB_KEY_NoSymbol)
710 leveli->num_syms = 0;
711 else
712 leveli->u.sym = keysym;
713 }
714 else if (leveli->num_syms > 1) {
715 leveli->u.syms[j] = keysym;
716 }
717 }
718 }
719
720 return true;
721 }
722
723 static bool
AddActionsToKey(SymbolsInfo * info,KeyInfo * keyi,ExprDef * arrayNdx,ExprDef * value)724 AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
725 ExprDef *value)
726 {
727 xkb_layout_index_t ndx;
728 GroupInfo *groupi;
729 unsigned int nActs;
730 ExprDef *act;
731
732 if (!GetGroupIndex(info, keyi, arrayNdx, ACTIONS, &ndx))
733 return false;
734
735 groupi = &darray_item(keyi->groups, ndx);
736
737 if (value == NULL) {
738 groupi->defined |= GROUP_FIELD_ACTS;
739 return true;
740 }
741
742 if (value->expr.op != EXPR_ACTION_LIST) {
743 log_wsgo(info->ctx,
744 "Bad expression type (%d) for action list value; "
745 "Ignoring actions for group %u of %s\n",
746 value->expr.op, ndx, KeyInfoText(info, keyi));
747 return false;
748 }
749
750 if (groupi->defined & GROUP_FIELD_ACTS) {
751 log_wsgo(info->ctx,
752 "Actions for key %s, group %u already defined\n",
753 KeyInfoText(info, keyi), ndx);
754 return false;
755 }
756
757 nActs = 0;
758 for (act = value->actions.actions; act; act = (ExprDef *) act->common.next)
759 nActs++;
760
761 if (darray_size(groupi->levels) < nActs)
762 darray_resize0(groupi->levels, nActs);
763
764 groupi->defined |= GROUP_FIELD_ACTS;
765
766 act = value->actions.actions;
767 for (unsigned i = 0; i < nActs; i++) {
768 union xkb_action *toAct = &darray_item(groupi->levels, i).action;
769
770 if (!HandleActionDef(info->ctx, info->actions, &info->mods, act, toAct))
771 log_err(info->ctx,
772 "Illegal action definition for %s; "
773 "Action for group %u/level %u ignored\n",
774 KeyInfoText(info, keyi), ndx + 1, i + 1);
775
776 act = (ExprDef *) act->common.next;
777 }
778
779 return true;
780 }
781
782 static const LookupEntry repeatEntries[] = {
783 { "true", KEY_REPEAT_YES },
784 { "yes", KEY_REPEAT_YES },
785 { "on", KEY_REPEAT_YES },
786 { "false", KEY_REPEAT_NO },
787 { "no", KEY_REPEAT_NO },
788 { "off", KEY_REPEAT_NO },
789 { "default", KEY_REPEAT_UNDEFINED },
790 { NULL, 0 }
791 };
792
793 static bool
SetSymbolsField(SymbolsInfo * info,KeyInfo * keyi,const char * field,ExprDef * arrayNdx,ExprDef * value)794 SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
795 ExprDef *arrayNdx, ExprDef *value)
796 {
797 if (istreq(field, "type")) {
798 xkb_layout_index_t ndx;
799 xkb_atom_t val;
800
801 if (!ExprResolveString(info->ctx, value, &val)) {
802 log_err(info->ctx,
803 "The type field of a key symbol map must be a string; "
804 "Ignoring illegal type definition\n");
805 return false;
806 }
807
808 if (!arrayNdx) {
809 keyi->default_type = val;
810 keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
811 }
812 else if (!ExprResolveGroup(info->ctx, arrayNdx, &ndx)) {
813 log_err(info->ctx,
814 "Illegal group index for type of key %s; "
815 "Definition with non-integer array index ignored\n",
816 KeyInfoText(info, keyi));
817 return false;
818 }
819 else {
820 ndx--;
821 if (ndx >= darray_size(keyi->groups))
822 darray_resize0(keyi->groups, ndx + 1);
823 darray_item(keyi->groups, ndx).type = val;
824 darray_item(keyi->groups, ndx).defined |= GROUP_FIELD_TYPE;
825 }
826 }
827 else if (istreq(field, "symbols")) {
828 return AddSymbolsToKey(info, keyi, arrayNdx, value);
829 }
830 else if (istreq(field, "actions")) {
831 return AddActionsToKey(info, keyi, arrayNdx, value);
832 }
833 else if (istreq(field, "vmods") ||
834 istreq(field, "virtualmods") ||
835 istreq(field, "virtualmodifiers")) {
836 xkb_mod_mask_t mask;
837
838 if (!ExprResolveModMask(info->ctx, value, MOD_VIRT, &info->mods,
839 &mask)) {
840 log_err(info->ctx,
841 "Expected a virtual modifier mask, found %s; "
842 "Ignoring virtual modifiers definition for key %s\n",
843 expr_op_type_to_string(value->expr.op),
844 KeyInfoText(info, keyi));
845 return false;
846 }
847
848 keyi->vmodmap = mask;
849 keyi->defined |= KEY_FIELD_VMODMAP;
850 }
851 else if (istreq(field, "locking") ||
852 istreq(field, "lock") ||
853 istreq(field, "locks")) {
854 log_vrb(info->ctx, 1,
855 "Key behaviors not supported; "
856 "Ignoring locking specification for key %s\n",
857 KeyInfoText(info, keyi));
858 }
859 else if (istreq(field, "radiogroup") ||
860 istreq(field, "permanentradiogroup") ||
861 istreq(field, "allownone")) {
862 log_vrb(info->ctx, 1,
863 "Radio groups not supported; "
864 "Ignoring radio group specification for key %s\n",
865 KeyInfoText(info, keyi));
866 }
867 else if (istreq_prefix("overlay", field) ||
868 istreq_prefix("permanentoverlay", field)) {
869 log_vrb(info->ctx, 1,
870 "Overlays not supported; "
871 "Ignoring overlay specification for key %s\n",
872 KeyInfoText(info, keyi));
873 }
874 else if (istreq(field, "repeating") ||
875 istreq(field, "repeats") ||
876 istreq(field, "repeat")) {
877 unsigned int val;
878
879 if (!ExprResolveEnum(info->ctx, value, &val, repeatEntries)) {
880 log_err(info->ctx,
881 "Illegal repeat setting for %s; "
882 "Non-boolean repeat setting ignored\n",
883 KeyInfoText(info, keyi));
884 return false;
885 }
886
887 keyi->repeat = val;
888 keyi->defined |= KEY_FIELD_REPEAT;
889 }
890 else if (istreq(field, "groupswrap") ||
891 istreq(field, "wrapgroups")) {
892 bool set;
893
894 if (!ExprResolveBoolean(info->ctx, value, &set)) {
895 log_err(info->ctx,
896 "Illegal groupsWrap setting for %s; "
897 "Non-boolean value ignored\n",
898 KeyInfoText(info, keyi));
899 return false;
900 }
901
902 keyi->out_of_range_group_action = (set ? RANGE_WRAP : RANGE_SATURATE);
903 keyi->defined |= KEY_FIELD_GROUPINFO;
904 }
905 else if (istreq(field, "groupsclamp") ||
906 istreq(field, "clampgroups")) {
907 bool set;
908
909 if (!ExprResolveBoolean(info->ctx, value, &set)) {
910 log_err(info->ctx,
911 "Illegal groupsClamp setting for %s; "
912 "Non-boolean value ignored\n",
913 KeyInfoText(info, keyi));
914 return false;
915 }
916
917 keyi->out_of_range_group_action = (set ? RANGE_SATURATE : RANGE_WRAP);
918 keyi->defined |= KEY_FIELD_GROUPINFO;
919 }
920 else if (istreq(field, "groupsredirect") ||
921 istreq(field, "redirectgroups")) {
922 xkb_layout_index_t grp;
923
924 if (!ExprResolveGroup(info->ctx, value, &grp)) {
925 log_err(info->ctx,
926 "Illegal group index for redirect of key %s; "
927 "Definition with non-integer group ignored\n",
928 KeyInfoText(info, keyi));
929 return false;
930 }
931
932 keyi->out_of_range_group_action = RANGE_REDIRECT;
933 keyi->out_of_range_group_number = grp - 1;
934 keyi->defined |= KEY_FIELD_GROUPINFO;
935 }
936 else {
937 log_err(info->ctx,
938 "Unknown field %s in a symbol interpretation; "
939 "Definition ignored\n",
940 field);
941 return false;
942 }
943
944 return true;
945 }
946
947 static bool
SetGroupName(SymbolsInfo * info,ExprDef * arrayNdx,ExprDef * value)948 SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
949 {
950 xkb_layout_index_t group, group_to_use;
951 xkb_atom_t name;
952
953 if (!arrayNdx) {
954 log_vrb(info->ctx, 1,
955 "You must specify an index when specifying a group name; "
956 "Group name definition without array subscript ignored\n");
957 return false;
958 }
959
960 if (!ExprResolveGroup(info->ctx, arrayNdx, &group)) {
961 log_err(info->ctx,
962 "Illegal index in group name definition; "
963 "Definition with non-integer array index ignored\n");
964 return false;
965 }
966
967 if (!ExprResolveString(info->ctx, value, &name)) {
968 log_err(info->ctx,
969 "Group name must be a string; "
970 "Illegal name for group %d ignored\n", group);
971 return false;
972 }
973
974 if (info->explicit_group == XKB_LAYOUT_INVALID) {
975 group_to_use = group - 1;
976 }
977 else if (group - 1 == 0) {
978 group_to_use = info->explicit_group;
979 }
980 else {
981 log_warn(info->ctx,
982 "An explicit group was specified for the '%s' map, "
983 "but it provides a name for a group other than Group1 (%d); "
984 "Ignoring group name '%s'\n",
985 info->name, group,
986 xkb_atom_text(info->ctx, name));
987 return false;
988 }
989
990 if (group_to_use >= darray_size(info->group_names))
991 darray_resize0(info->group_names, group_to_use + 1);
992 darray_item(info->group_names, group_to_use) = name;
993
994 return true;
995 }
996
997 static bool
HandleGlobalVar(SymbolsInfo * info,VarDef * stmt)998 HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
999 {
1000 const char *elem, *field;
1001 ExprDef *arrayNdx;
1002 bool ret;
1003
1004 if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx))
1005 return false;
1006
1007 if (elem && istreq(elem, "key")) {
1008 ret = SetSymbolsField(info, &info->default_key, field, arrayNdx,
1009 stmt->value);
1010 }
1011 else if (!elem && (istreq(field, "name") ||
1012 istreq(field, "groupname"))) {
1013 ret = SetGroupName(info, arrayNdx, stmt->value);
1014 }
1015 else if (!elem && (istreq(field, "groupswrap") ||
1016 istreq(field, "wrapgroups"))) {
1017 log_err(info->ctx,
1018 "Global \"groupswrap\" not supported; Ignored\n");
1019 ret = true;
1020 }
1021 else if (!elem && (istreq(field, "groupsclamp") ||
1022 istreq(field, "clampgroups"))) {
1023 log_err(info->ctx,
1024 "Global \"groupsclamp\" not supported; Ignored\n");
1025 ret = true;
1026 }
1027 else if (!elem && (istreq(field, "groupsredirect") ||
1028 istreq(field, "redirectgroups"))) {
1029 log_err(info->ctx,
1030 "Global \"groupsredirect\" not supported; Ignored\n");
1031 ret = true;
1032 }
1033 else if (!elem && istreq(field, "allownone")) {
1034 log_err(info->ctx,
1035 "Radio groups not supported; "
1036 "Ignoring \"allownone\" specification\n");
1037 ret = true;
1038 }
1039 else {
1040 ret = SetActionField(info->ctx, info->actions, &info->mods,
1041 elem, field, arrayNdx, stmt->value);
1042 }
1043
1044 return ret;
1045 }
1046
1047 static bool
HandleSymbolsBody(SymbolsInfo * info,VarDef * def,KeyInfo * keyi)1048 HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
1049 {
1050 bool ok = true;
1051 const char *elem, *field;
1052 ExprDef *arrayNdx;
1053
1054 for (; def; def = (VarDef *) def->common.next) {
1055 if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
1056 log_err(info->ctx,
1057 "Cannot set a global default value from within a key statement; "
1058 "Move statements to the global file scope\n");
1059 continue;
1060 }
1061
1062 if (!def->name) {
1063 if (!def->value || def->value->expr.op == EXPR_KEYSYM_LIST)
1064 field = "symbols";
1065 else
1066 field = "actions";
1067 arrayNdx = NULL;
1068 }
1069 else {
1070 ok = ExprResolveLhs(info->ctx, def->name, &elem, &field,
1071 &arrayNdx);
1072 }
1073
1074 if (ok)
1075 ok = SetSymbolsField(info, keyi, field, arrayNdx, def->value);
1076 }
1077
1078 return ok;
1079 }
1080
1081 static bool
SetExplicitGroup(SymbolsInfo * info,KeyInfo * keyi)1082 SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
1083 {
1084 xkb_layout_index_t i;
1085 GroupInfo *groupi;
1086 bool warn = false;
1087
1088 if (info->explicit_group == XKB_LAYOUT_INVALID)
1089 return true;
1090
1091 darray_enumerate_from(i, groupi, keyi->groups, 1) {
1092 if (groupi->defined) {
1093 warn = true;
1094 ClearGroupInfo(groupi);
1095 InitGroupInfo(groupi);
1096 }
1097 }
1098
1099 if (warn)
1100 log_warn(info->ctx,
1101 "For the map %s an explicit group specified, "
1102 "but key %s has more than one group defined; "
1103 "All groups except first one will be ignored\n",
1104 info->name, KeyInfoText(info, keyi));
1105
1106 darray_resize0(keyi->groups, info->explicit_group + 1);
1107 if (info->explicit_group > 0) {
1108 darray_item(keyi->groups, info->explicit_group) =
1109 darray_item(keyi->groups, 0);
1110 InitGroupInfo(&darray_item(keyi->groups, 0));
1111 }
1112
1113 return true;
1114 }
1115
1116 static bool
HandleSymbolsDef(SymbolsInfo * info,SymbolsDef * stmt)1117 HandleSymbolsDef(SymbolsInfo *info, SymbolsDef *stmt)
1118 {
1119 KeyInfo keyi;
1120
1121 keyi = info->default_key;
1122 darray_init(keyi.groups);
1123 darray_copy(keyi.groups, info->default_key.groups);
1124 for (xkb_layout_index_t i = 0; i < darray_size(keyi.groups); i++)
1125 CopyGroupInfo(&darray_item(keyi.groups, i),
1126 &darray_item(info->default_key.groups, i));
1127 keyi.merge = stmt->merge;
1128 keyi.name = stmt->keyName;
1129
1130 if (!HandleSymbolsBody(info, stmt->symbols, &keyi)) {
1131 info->errorCount++;
1132 return false;
1133 }
1134
1135 if (!SetExplicitGroup(info, &keyi)) {
1136 info->errorCount++;
1137 return false;
1138 }
1139
1140 if (!AddKeySymbols(info, &keyi, true)) {
1141 info->errorCount++;
1142 return false;
1143 }
1144
1145 return true;
1146 }
1147
1148 static bool
HandleModMapDef(SymbolsInfo * info,ModMapDef * def)1149 HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
1150 {
1151 ModMapEntry tmp;
1152 xkb_mod_index_t ndx;
1153 bool ok;
1154 struct xkb_context *ctx = info->ctx;
1155
1156 ndx = XkbModNameToIndex(&info->mods, def->modifier, MOD_REAL);
1157 if (ndx == XKB_MOD_INVALID) {
1158 log_err(info->ctx,
1159 "Illegal modifier map definition; "
1160 "Ignoring map for non-modifier \"%s\"\n",
1161 xkb_atom_text(ctx, def->modifier));
1162 return false;
1163 }
1164
1165 ok = true;
1166 tmp.modifier = ndx;
1167 tmp.merge = def->merge;
1168
1169 for (ExprDef *key = def->keys; key; key = (ExprDef *) key->common.next) {
1170 xkb_keysym_t sym;
1171
1172 if (key->expr.op == EXPR_VALUE &&
1173 key->expr.value_type == EXPR_TYPE_KEYNAME) {
1174 tmp.haveSymbol = false;
1175 tmp.u.keyName = key->key_name.key_name;
1176 }
1177 else if (ExprResolveKeySym(ctx, key, &sym)) {
1178 tmp.haveSymbol = true;
1179 tmp.u.keySym = sym;
1180 }
1181 else {
1182 log_err(info->ctx,
1183 "Modmap entries may contain only key names or keysyms; "
1184 "Illegal definition for %s modifier ignored\n",
1185 ModIndexText(info->ctx, &info->mods, tmp.modifier));
1186 continue;
1187 }
1188
1189 ok = AddModMapEntry(info, &tmp) && ok;
1190 }
1191 return ok;
1192 }
1193
1194 static void
HandleSymbolsFile(SymbolsInfo * info,XkbFile * file,enum merge_mode merge)1195 HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
1196 {
1197 bool ok;
1198
1199 free(info->name);
1200 info->name = strdup_safe(file->name);
1201
1202 for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) {
1203 switch (stmt->type) {
1204 case STMT_INCLUDE:
1205 ok = HandleIncludeSymbols(info, (IncludeStmt *) stmt);
1206 break;
1207 case STMT_SYMBOLS:
1208 ok = HandleSymbolsDef(info, (SymbolsDef *) stmt);
1209 break;
1210 case STMT_VAR:
1211 ok = HandleGlobalVar(info, (VarDef *) stmt);
1212 break;
1213 case STMT_VMOD:
1214 ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
1215 break;
1216 case STMT_MODMAP:
1217 ok = HandleModMapDef(info, (ModMapDef *) stmt);
1218 break;
1219 default:
1220 log_err(info->ctx,
1221 "Symbols files may not include other types; "
1222 "Ignoring %s\n", stmt_type_to_string(stmt->type));
1223 ok = false;
1224 break;
1225 }
1226
1227 if (!ok)
1228 info->errorCount++;
1229
1230 if (info->errorCount > 10) {
1231 log_err(info->ctx, "Abandoning symbols file \"%s\"\n",
1232 file->name);
1233 break;
1234 }
1235 }
1236 }
1237
1238 /**
1239 * Given a keysym @sym, return a key which generates it, or NULL.
1240 * This is used for example in a modifier map definition, such as:
1241 * modifier_map Lock { Caps_Lock };
1242 * where we want to add the Lock modifier to the modmap of the key
1243 * which matches the keysym Caps_Lock.
1244 * Since there can be many keys which generates the keysym, the key
1245 * is chosen first by lowest group in which the keysym appears, than
1246 * by lowest level and than by lowest key code.
1247 */
1248 static struct xkb_key *
FindKeyForSymbol(struct xkb_keymap * keymap,xkb_keysym_t sym)1249 FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
1250 {
1251 struct xkb_key *key;
1252 xkb_layout_index_t group;
1253 bool got_one_group, got_one_level;
1254
1255 group = 0;
1256 do {
1257 xkb_level_index_t level = 0;
1258 got_one_group = false;
1259 do {
1260 got_one_level = false;
1261 xkb_keys_foreach(key, keymap) {
1262 if (group < key->num_groups &&
1263 level < XkbKeyNumLevels(key, group)) {
1264 got_one_group = got_one_level = true;
1265 if (key->groups[group].levels[level].num_syms == 1 &&
1266 key->groups[group].levels[level].u.sym == sym)
1267 return key;
1268 }
1269 }
1270 level++;
1271 } while (got_one_level);
1272 group++;
1273 } while (got_one_group);
1274
1275 return NULL;
1276 }
1277
1278 /*
1279 * Find an appropriate type for a group and return its name.
1280 *
1281 * Simple recipe:
1282 * - ONE_LEVEL for width 0/1
1283 * - ALPHABETIC for 2 shift levels, with lower/upercase keysyms
1284 * - KEYPAD for keypad keys.
1285 * - TWO_LEVEL for other 2 shift level keys.
1286 * and the same for four level keys.
1287 *
1288 * FIXME: Decide how to handle multiple-syms-per-level, and do it.
1289 */
1290 static xkb_atom_t
FindAutomaticType(struct xkb_context * ctx,GroupInfo * groupi)1291 FindAutomaticType(struct xkb_context *ctx, GroupInfo *groupi)
1292 {
1293 xkb_keysym_t sym0, sym1;
1294 const xkb_level_index_t width = darray_size(groupi->levels);
1295
1296 #define GET_SYM(level) \
1297 (darray_item(groupi->levels, level).num_syms == 0 ? \
1298 XKB_KEY_NoSymbol : \
1299 darray_item(groupi->levels, level).num_syms == 1 ? \
1300 darray_item(groupi->levels, level).u.sym : \
1301 /* num_syms > 1 */ \
1302 darray_item(groupi->levels, level).u.syms[0])
1303
1304 if (width == 1 || width <= 0)
1305 return xkb_atom_intern_literal(ctx, "ONE_LEVEL");
1306
1307 sym0 = GET_SYM(0);
1308 sym1 = GET_SYM(1);
1309
1310 if (width == 2) {
1311 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1))
1312 return xkb_atom_intern_literal(ctx, "ALPHABETIC");
1313
1314 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1315 return xkb_atom_intern_literal(ctx, "KEYPAD");
1316
1317 return xkb_atom_intern_literal(ctx, "TWO_LEVEL");
1318 }
1319
1320 if (width <= 4) {
1321 if (xkb_keysym_is_lower(sym0) && xkb_keysym_is_upper(sym1)) {
1322 xkb_keysym_t sym2, sym3;
1323 sym2 = GET_SYM(2);
1324 sym3 = (width == 4 ? GET_SYM(3) : XKB_KEY_NoSymbol);
1325
1326 if (xkb_keysym_is_lower(sym2) && xkb_keysym_is_upper(sym3))
1327 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_ALPHABETIC");
1328
1329 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_SEMIALPHABETIC");
1330 }
1331
1332 if (xkb_keysym_is_keypad(sym0) || xkb_keysym_is_keypad(sym1))
1333 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL_KEYPAD");
1334
1335 return xkb_atom_intern_literal(ctx, "FOUR_LEVEL");
1336 }
1337
1338 return XKB_ATOM_NONE;
1339
1340 #undef GET_SYM
1341 }
1342
1343 static const struct xkb_key_type *
FindTypeForGroup(struct xkb_keymap * keymap,KeyInfo * keyi,xkb_layout_index_t group,bool * explicit_type)1344 FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
1345 xkb_layout_index_t group, bool *explicit_type)
1346 {
1347 unsigned int i;
1348 GroupInfo *groupi = &darray_item(keyi->groups, group);
1349 xkb_atom_t type_name = groupi->type;
1350
1351 *explicit_type = true;
1352
1353 if (type_name == XKB_ATOM_NONE) {
1354 if (keyi->default_type != XKB_ATOM_NONE) {
1355 type_name = keyi->default_type;
1356 }
1357 else {
1358 type_name = FindAutomaticType(keymap->ctx, groupi);
1359 if (type_name != XKB_ATOM_NONE)
1360 *explicit_type = false;
1361 }
1362 }
1363
1364 if (type_name == XKB_ATOM_NONE) {
1365 log_warn(keymap->ctx,
1366 "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
1367 "Using the default type\n",
1368 KeyNameText(keymap->ctx, keyi->name), group + 1,
1369 (unsigned long) darray_size(groupi->levels));
1370 goto use_default;
1371 }
1372
1373 for (i = 0; i < keymap->num_types; i++)
1374 if (keymap->types[i].name == type_name)
1375 break;
1376
1377 if (i >= keymap->num_types) {
1378 log_warn(keymap->ctx,
1379 "The type \"%s\" for key '%s' group %d was not previously defined; "
1380 "Using the default type\n",
1381 xkb_atom_text(keymap->ctx, type_name),
1382 KeyNameText(keymap->ctx, keyi->name), group + 1);
1383 goto use_default;
1384 }
1385
1386 return &keymap->types[i];
1387
1388 use_default:
1389 /*
1390 * Index 0 is guaranteed to contain something, usually
1391 * ONE_LEVEL or at least some default one-level type.
1392 */
1393 return &keymap->types[0];
1394 }
1395
1396 static bool
CopySymbolsDefToKeymap(struct xkb_keymap * keymap,SymbolsInfo * info,KeyInfo * keyi)1397 CopySymbolsDefToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info,
1398 KeyInfo *keyi)
1399 {
1400 struct xkb_key *key;
1401 GroupInfo *groupi;
1402 const GroupInfo *group0;
1403 xkb_layout_index_t i;
1404
1405 /*
1406 * The name is guaranteed to be real and not an alias (see
1407 * AddKeySymbols), so 'false' is safe here.
1408 */
1409 key = XkbKeyByName(keymap, keyi->name, false);
1410 if (!key) {
1411 log_vrb(info->ctx, 5,
1412 "Key %s not found in keycodes; Symbols ignored\n",
1413 KeyInfoText(info, keyi));
1414 return false;
1415 }
1416
1417 /* Find the range of groups we need. */
1418 key->num_groups = 0;
1419 darray_enumerate(i, groupi, keyi->groups)
1420 if (groupi->defined)
1421 key->num_groups = i + 1;
1422
1423 if (key->num_groups <= 0)
1424 return false; /* WSGO */
1425
1426 darray_resize(keyi->groups, key->num_groups);
1427
1428 /*
1429 * If there are empty groups between non-empty ones, fill them with data
1430 * from the first group.
1431 * We can make a wrong assumption here. But leaving gaps is worse.
1432 */
1433 group0 = &darray_item(keyi->groups, 0);
1434 darray_foreach_from(groupi, keyi->groups, 1) {
1435 if (groupi->defined)
1436 continue;
1437
1438 CopyGroupInfo(groupi, group0);
1439 }
1440
1441 key->groups = calloc(key->num_groups, sizeof(*key->groups));
1442
1443 /* Find and assign the groups' types in the keymap. */
1444 darray_enumerate(i, groupi, keyi->groups) {
1445 const struct xkb_key_type *type;
1446 bool explicit_type;
1447
1448 type = FindTypeForGroup(keymap, keyi, i, &explicit_type);
1449
1450 /* Always have as many levels as the type specifies. */
1451 if (type->num_levels < darray_size(groupi->levels)) {
1452 struct xkb_level *leveli;
1453
1454 log_vrb(info->ctx, 1,
1455 "Type \"%s\" has %d levels, but %s has %d levels; "
1456 "Ignoring extra symbols\n",
1457 xkb_atom_text(keymap->ctx, type->name), type->num_levels,
1458 KeyInfoText(info, keyi),
1459 (int) darray_size(groupi->levels));
1460
1461 darray_foreach_from(leveli, groupi->levels, type->num_levels)
1462 ClearLevelInfo(leveli);
1463 }
1464 darray_resize0(groupi->levels, type->num_levels);
1465
1466 key->groups[i].explicit_type = explicit_type;
1467 key->groups[i].type = type;
1468 }
1469
1470 /* Copy levels. */
1471 darray_enumerate(i, groupi, keyi->groups)
1472 darray_steal(groupi->levels, &key->groups[i].levels, NULL);
1473
1474 key->out_of_range_group_number = keyi->out_of_range_group_number;
1475 key->out_of_range_group_action = keyi->out_of_range_group_action;
1476
1477 if (keyi->defined & KEY_FIELD_VMODMAP) {
1478 key->vmodmap = keyi->vmodmap;
1479 key->explicit |= EXPLICIT_VMODMAP;
1480 }
1481
1482 if (keyi->repeat != KEY_REPEAT_UNDEFINED) {
1483 key->repeats = (keyi->repeat == KEY_REPEAT_YES);
1484 key->explicit |= EXPLICIT_REPEAT;
1485 }
1486
1487 darray_foreach(groupi, keyi->groups) {
1488 if (groupi->defined & GROUP_FIELD_ACTS) {
1489 key->explicit |= EXPLICIT_INTERP;
1490 break;
1491 }
1492 }
1493
1494 return true;
1495 }
1496
1497 static bool
CopyModMapDefToKeymap(struct xkb_keymap * keymap,SymbolsInfo * info,ModMapEntry * entry)1498 CopyModMapDefToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info,
1499 ModMapEntry *entry)
1500 {
1501 struct xkb_key *key;
1502
1503 if (!entry->haveSymbol) {
1504 key = XkbKeyByName(keymap, entry->u.keyName, true);
1505 if (!key) {
1506 log_vrb(info->ctx, 5,
1507 "Key %s not found in keycodes; "
1508 "Modifier map entry for %s not updated\n",
1509 KeyNameText(info->ctx, entry->u.keyName),
1510 ModIndexText(info->ctx, &info->mods, entry->modifier));
1511 return false;
1512 }
1513 }
1514 else {
1515 key = FindKeyForSymbol(keymap, entry->u.keySym);
1516 if (!key) {
1517 log_vrb(info->ctx, 5,
1518 "Key \"%s\" not found in symbol map; "
1519 "Modifier map entry for %s not updated\n",
1520 KeysymText(info->ctx, entry->u.keySym),
1521 ModIndexText(info->ctx, &info->mods, entry->modifier));
1522 return false;
1523 }
1524 }
1525
1526 key->modmap |= (1u << entry->modifier);
1527 return true;
1528 }
1529
1530 static bool
CopySymbolsToKeymap(struct xkb_keymap * keymap,SymbolsInfo * info)1531 CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
1532 {
1533 KeyInfo *keyi;
1534 ModMapEntry *mm;
1535
1536 keymap->symbols_section_name = strdup_safe(info->name);
1537 XkbEscapeMapName(keymap->symbols_section_name);
1538
1539 keymap->mods = info->mods;
1540
1541 darray_steal(info->group_names,
1542 &keymap->group_names, &keymap->num_group_names);
1543
1544 darray_foreach(keyi, info->keys)
1545 if (!CopySymbolsDefToKeymap(keymap, info, keyi))
1546 info->errorCount++;
1547
1548 if (xkb_context_get_log_verbosity(keymap->ctx) > 3) {
1549 struct xkb_key *key;
1550
1551 xkb_keys_foreach(key, keymap) {
1552 if (key->name == XKB_ATOM_NONE)
1553 continue;
1554
1555 if (key->num_groups < 1)
1556 log_info(info->ctx,
1557 "No symbols defined for %s\n",
1558 KeyNameText(info->ctx, key->name));
1559 }
1560 }
1561
1562 darray_foreach(mm, info->modmaps)
1563 if (!CopyModMapDefToKeymap(keymap, info, mm))
1564 info->errorCount++;
1565
1566 /* XXX: If we don't ignore errorCount, things break. */
1567 return true;
1568 }
1569
1570 bool
CompileSymbols(XkbFile * file,struct xkb_keymap * keymap,enum merge_mode merge)1571 CompileSymbols(XkbFile *file, struct xkb_keymap *keymap,
1572 enum merge_mode merge)
1573 {
1574 SymbolsInfo info;
1575 ActionsInfo *actions;
1576
1577 actions = NewActionsInfo();
1578 if (!actions)
1579 return false;
1580
1581 InitSymbolsInfo(&info, keymap, actions, &keymap->mods);
1582 info.default_key.merge = merge;
1583
1584 HandleSymbolsFile(&info, file, merge);
1585
1586 if (info.errorCount != 0)
1587 goto err_info;
1588
1589 if (!CopySymbolsToKeymap(keymap, &info))
1590 goto err_info;
1591
1592 ClearSymbolsInfo(&info);
1593 FreeActionsInfo(actions);
1594 return true;
1595
1596 err_info:
1597 FreeActionsInfo(actions);
1598 ClearSymbolsInfo(&info);
1599 return false;
1600 }
1601