1 /************************************************************
2 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
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.
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.
25 ********************************************************/
27 #include "xkbcomp-priv.h"
31 typedef bool (*IdentLookupFunc)(struct xkb_context *ctx, const void *priv,
32 xkb_atom_t field, enum expr_value_type type,
33 unsigned int *val_rtrn);
36 ExprResolveLhs(struct xkb_context *ctx, const ExprDef *expr,
37 const char **elem_rtrn, const char **field_rtrn,
43 *field_rtrn = xkb_atom_text(ctx, expr->value.str);
47 *elem_rtrn = xkb_atom_text(ctx, expr->value.field.element);
48 *field_rtrn = xkb_atom_text(ctx, expr->value.field.field);
52 *elem_rtrn = xkb_atom_text(ctx, expr->value.array.element);
53 *field_rtrn = xkb_atom_text(ctx, expr->value.array.field);
54 *index_rtrn = expr->value.array.entry;
59 log_wsgo(ctx, "Unexpected operator %d in ResolveLhs\n", expr->op);
64 SimpleLookup(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
65 enum expr_value_type type, unsigned int *val_rtrn)
67 const LookupEntry *entry;
70 if (!priv || field == XKB_ATOM_NONE || type != EXPR_TYPE_INT)
73 str = xkb_atom_text(ctx, field);
74 for (entry = priv; entry && entry->name; entry++) {
75 if (istreq(str, entry->name)) {
76 *val_rtrn = entry->value;
85 LookupModIndex(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
86 enum expr_value_type type, xkb_mod_index_t *val_rtrn)
88 const struct xkb_keymap *keymap = priv;
89 *val_rtrn = ModNameToIndex(keymap, field);
90 return (*val_rtrn != XKB_MOD_INVALID);
94 LookupModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
95 enum expr_value_type type, xkb_mod_mask_t *val_rtrn)
99 const struct xkb_keymap *keymap = priv;
101 if (type != EXPR_TYPE_INT)
104 str = xkb_atom_text(ctx, field);
106 if (istreq(str, "all"))
108 else if (istreq(str, "none"))
110 else if (LookupModIndex(ctx, keymap, field, type, &ndx))
111 *val_rtrn = (1 << ndx);
119 ExprResolveBoolean(struct xkb_context *ctx, const ExprDef *expr,
127 if (expr->value_type != EXPR_TYPE_BOOLEAN) {
129 "Found constant of type %s where boolean was expected\n",
130 expr_value_type_to_string(expr->value_type));
133 *set_rtrn = !!expr->value.ival;
137 ident = xkb_atom_text(ctx, expr->value.str);
139 if (istreq(ident, "true") ||
140 istreq(ident, "yes") ||
141 istreq(ident, "on")) {
145 else if (istreq(ident, "false") ||
146 istreq(ident, "no") ||
147 istreq(ident, "off")) {
152 log_err(ctx, "Identifier \"%s\" of type boolean is unknown\n",
153 xkb_atom_text(ctx, expr->value.str));
157 log_err(ctx, "Default \"%s.%s\" of type boolean is unknown\n",
158 xkb_atom_text(ctx, expr->value.field.element),
159 xkb_atom_text(ctx, expr->value.field.field));
164 ok = ExprResolveBoolean(ctx, expr, set_rtrn);
166 *set_rtrn = !*set_rtrn;
174 case EXPR_UNARY_PLUS:
175 log_err(ctx, "%s of boolean values not permitted\n",
176 expr_op_type_to_string(expr->op));
180 log_wsgo(ctx, "Unknown operator %d in ResolveBoolean\n", expr->op);
188 ExprResolveKeyCode(struct xkb_context *ctx, const ExprDef *expr,
191 xkb_keycode_t leftRtrn, rightRtrn;
192 ExprDef *left, *right;
196 if (expr->value_type != EXPR_TYPE_INT) {
198 "Found constant of type %s where an int was expected\n",
199 expr_value_type_to_string(expr->value_type));
203 *kc = expr->value.uval;
210 left = expr->value.binary.left;
211 right = expr->value.binary.right;
213 if (!ExprResolveKeyCode(ctx, left, &leftRtrn) ||
214 !ExprResolveKeyCode(ctx, right, &rightRtrn))
219 *kc = leftRtrn + rightRtrn;
222 *kc = leftRtrn - rightRtrn;
225 *kc = leftRtrn * rightRtrn;
228 if (rightRtrn == 0) {
229 log_err(ctx, "Cannot divide by zero: %d / %d\n",
230 leftRtrn, rightRtrn);
234 *kc = leftRtrn / rightRtrn;
243 left = expr->value.child;
244 if (!ExprResolveKeyCode(ctx, left, &leftRtrn))
250 case EXPR_UNARY_PLUS:
251 left = expr->value.child;
252 return ExprResolveKeyCode(ctx, left, kc);
255 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n", expr->op);
263 * This function returns ... something. It's a bit of a guess, really.
265 * If an integer is given in value ctx, it will be returned in ival.
266 * If an ident or field reference is given, the lookup function (if given)
267 * will be called. At the moment, only SimpleLookup use this, and they both
268 * return the results in uval. And don't support field references.
273 ExprResolveIntegerLookup(struct xkb_context *ctx, const ExprDef *expr,
274 int *val_rtrn, IdentLookupFunc lookup,
275 const void *lookupPriv)
280 ExprDef *left, *right;
284 if (expr->value_type != EXPR_TYPE_INT) {
286 "Found constant of type %s where an int was expected\n",
287 expr_value_type_to_string(expr->value_type));
291 *val_rtrn = expr->value.ival;
296 ok = lookup(ctx, lookupPriv, expr->value.str, EXPR_TYPE_INT, &u);
299 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
300 xkb_atom_text(ctx, expr->value.str));
307 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
308 xkb_atom_text(ctx, expr->value.field.element),
309 xkb_atom_text(ctx, expr->value.field.field));
316 left = expr->value.binary.left;
317 right = expr->value.binary.right;
318 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv) ||
319 !ExprResolveIntegerLookup(ctx, right, &r, lookup, lookupPriv))
334 log_err(ctx, "Cannot divide by zero: %d / %d\n", l, r);
346 log_wsgo(ctx, "Assignment operator not implemented yet\n");
350 log_err(ctx, "The ! operator cannot be applied to an integer\n");
355 left = expr->value.child;
356 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv))
359 *val_rtrn = (expr->op == EXPR_NEGATE ? -l : ~l);
362 case EXPR_UNARY_PLUS:
363 left = expr->value.child;
364 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
368 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n", expr->op);
376 ExprResolveInteger(struct xkb_context *ctx, const ExprDef *expr,
379 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
383 ExprResolveGroup(struct xkb_context *ctx, const ExprDef *expr,
384 xkb_layout_index_t *group_rtrn)
389 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
394 if (result <= 0 || result > XKB_NUM_GROUPS) {
395 log_err(ctx, "Group index %u is out of range (1..%d)\n",
396 result, XKB_NUM_GROUPS);
400 *group_rtrn = (xkb_layout_index_t) result;
405 ExprResolveLevel(struct xkb_context *ctx, const ExprDef *expr,
406 xkb_level_index_t *level_rtrn)
411 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
417 log_err(ctx, "Shift level %d is out of range\n", result);
421 /* Level is zero-indexed from now on. */
422 *level_rtrn = (unsigned int) (result - 1);
427 ExprResolveButton(struct xkb_context *ctx, const ExprDef *expr, int *btn_rtrn)
431 if (!ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
440 ExprResolveString(struct xkb_context *ctx, const ExprDef *expr,
441 xkb_atom_t *val_rtrn)
445 if (expr->value_type != EXPR_TYPE_STRING) {
446 log_err(ctx, "Found constant of type %s, expected a string\n",
447 expr_value_type_to_string(expr->value_type));
451 *val_rtrn = expr->value.str;
455 log_err(ctx, "Identifier \"%s\" of type string not found\n",
456 xkb_atom_text(ctx, expr->value.str));
460 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
461 xkb_atom_text(ctx, expr->value.field.element),
462 xkb_atom_text(ctx, expr->value.field.field));
473 case EXPR_UNARY_PLUS:
474 log_err(ctx, "%s of strings not permitted\n",
475 expr_op_type_to_string(expr->op));
479 log_wsgo(ctx, "Unknown operator %d in ResolveString\n", expr->op);
486 ExprResolveEnum(struct xkb_context *ctx, const ExprDef *expr,
487 unsigned int *val_rtrn, const LookupEntry *values)
489 if (expr->op != EXPR_IDENT) {
490 log_err(ctx, "Found a %s where an enumerated value was expected\n",
491 expr_op_type_to_string(expr->op));
495 if (!SimpleLookup(ctx, values, expr->value.str, EXPR_TYPE_INT,
497 log_err(ctx, "Illegal identifier %s; expected one of:\n",
498 xkb_atom_text(ctx, expr->value.str));
499 while (values && values->name)
501 log_err(ctx, "\t%s\n", values->name);
511 ExprResolveMaskLookup(struct xkb_context *ctx, const ExprDef *expr,
512 unsigned int *val_rtrn, IdentLookupFunc lookup,
513 const void *lookupPriv)
518 ExprDef *left, *right;
519 const char *bogus = NULL;
523 if (expr->value_type != EXPR_TYPE_INT) {
525 "Found constant of type %s where a mask was expected\n",
526 expr_value_type_to_string(expr->value_type));
529 *val_rtrn = (unsigned int) expr->value.ival;
533 ok = lookup(ctx, lookupPriv, expr->value.str, EXPR_TYPE_INT,
536 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
537 xkb_atom_text(ctx, expr->value.str));
541 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
542 xkb_atom_text(ctx, expr->value.field.element),
543 xkb_atom_text(ctx, expr->value.field.field));
547 bogus = "array reference";
549 case EXPR_ACTION_DECL:
551 bogus = "function use";
553 "Unexpected %s in mask expression; Expression Ignored\n",
561 left = expr->value.binary.left;
562 right = expr->value.binary.right;
563 if (!ExprResolveMaskLookup(ctx, left, &l, lookup, lookupPriv) ||
564 !ExprResolveMaskLookup(ctx, right, &r, lookup, lookupPriv))
572 *val_rtrn = l & (~r);
576 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
577 (expr->op == EXPR_DIVIDE ? "divide" : "multiply"));
586 log_wsgo(ctx, "Assignment operator not implemented yet\n");
590 left = expr->value.child;
591 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
597 case EXPR_UNARY_PLUS:
600 left = expr->value.child;
601 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
602 log_err(ctx, "The %s operator cannot be used with a mask\n",
603 (expr->op == EXPR_NEGATE ? "-" : "!"));
607 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n", expr->op);
615 ExprResolveMask(struct xkb_context *ctx, const ExprDef *expr,
616 unsigned int *mask_rtrn, const LookupEntry *values)
618 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, SimpleLookup, values);
622 ExprResolveModMask(struct xkb_keymap *keymap, const ExprDef *expr,
623 xkb_mod_mask_t *mask_rtrn)
625 return ExprResolveMaskLookup(keymap->ctx, expr, mask_rtrn, LookupModMask,
630 LookupVModIndex(struct xkb_context *ctx, const void *priv,
631 xkb_atom_t field, enum expr_value_type type,
632 xkb_mod_index_t *val_rtrn)
634 const struct xkb_mod *mod;
636 const struct xkb_keymap *keymap = priv;
638 if (type != EXPR_TYPE_INT)
641 darray_enumerate(i, mod, keymap->mods) {
642 if (mod->type == MOD_VIRT && mod->name == field) {
652 LookupVModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
653 enum expr_value_type type, xkb_mod_mask_t *val_rtrn)
656 const struct xkb_keymap *keymap = priv;
658 if (LookupModMask(ctx, keymap, field, type, val_rtrn)) {
661 else if (LookupVModIndex(ctx, keymap, field, type, &ndx)) {
662 *val_rtrn = (1 << ndx);
670 ExprResolveVModMask(struct xkb_keymap *keymap, const ExprDef *expr,
671 xkb_mod_mask_t *mask_rtrn)
673 return ExprResolveMaskLookup(keymap->ctx, expr, mask_rtrn, LookupVModMask,
678 ExprResolveKeySym(struct xkb_context *ctx, const ExprDef *expr,
679 xkb_keysym_t *sym_rtrn)
683 if (expr->op == EXPR_IDENT) {
685 str = xkb_atom_text(ctx, expr->value.str);
686 *sym_rtrn = xkb_keysym_from_name(str);
687 if (*sym_rtrn != XKB_KEY_NoSymbol)
691 if (!ExprResolveInteger(ctx, expr, &val))
694 if (val < 0 || val >= 10)
697 *sym_rtrn = ((xkb_keysym_t) val) + '0';
702 ExprResolveVMod(struct xkb_keymap *keymap, const ExprDef *def,
703 xkb_mod_index_t *ndx_rtrn)
705 const struct xkb_mod *mod;
707 xkb_atom_t name = def->value.str;
709 if (def->op != EXPR_IDENT) {
711 "Cannot resolve virtual modifier: "
712 "found %s where a virtual modifier name was expected\n",
713 expr_op_type_to_string(def->op));
717 darray_enumerate(i, mod, keymap->mods) {
718 if (mod->type == MOD_VIRT && mod->name == name) {
725 "Cannot resolve virtual modifier: "
726 "\"%s\" was not previously declared\n",
727 xkb_atom_text(keymap->ctx, name));