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,
40 switch (expr->expr.op) {
43 *field_rtrn = xkb_atom_text(ctx, expr->ident.ident);
45 return (*field_rtrn != NULL);
47 *elem_rtrn = xkb_atom_text(ctx, expr->field_ref.element);
48 *field_rtrn = xkb_atom_text(ctx, expr->field_ref.field);
50 return (*elem_rtrn != NULL && *field_rtrn != NULL);
52 *elem_rtrn = xkb_atom_text(ctx, expr->array_ref.element);
53 *field_rtrn = xkb_atom_text(ctx, expr->array_ref.field);
54 *index_rtrn = expr->array_ref.entry;
55 if (expr->array_ref.element != XKB_ATOM_NONE && *elem_rtrn == NULL)
57 if (*field_rtrn == NULL)
63 log_wsgo(ctx, "Unexpected operator %d in ResolveLhs\n", expr->expr.op);
68 SimpleLookup(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
69 enum expr_value_type type, unsigned int *val_rtrn)
71 const LookupEntry *entry;
74 if (!priv || field == XKB_ATOM_NONE || type != EXPR_TYPE_INT)
77 str = xkb_atom_text(ctx, field);
78 for (entry = priv; entry && entry->name; entry++) {
79 if (istreq(str, entry->name)) {
80 *val_rtrn = entry->value;
88 /* Data passed in the *priv argument for LookupModMask. */
90 const struct xkb_mod_set *mods;
91 enum mod_type mod_type;
95 LookupModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
96 enum expr_value_type type, xkb_mod_mask_t *val_rtrn)
100 const LookupModMaskPriv *arg = priv;
101 const struct xkb_mod_set *mods = arg->mods;
102 enum mod_type mod_type = arg->mod_type;
104 if (type != EXPR_TYPE_INT)
107 str = xkb_atom_text(ctx, field);
111 if (istreq(str, "all")) {
112 *val_rtrn = MOD_REAL_MASK_ALL;
116 if (istreq(str, "none")) {
121 ndx = XkbModNameToIndex(mods, field, mod_type);
122 if (ndx == XKB_MOD_INVALID)
125 *val_rtrn = (1u << ndx);
130 ExprResolveBoolean(struct xkb_context *ctx, const ExprDef *expr,
136 switch (expr->expr.op) {
138 if (expr->expr.value_type != EXPR_TYPE_BOOLEAN) {
140 "Found constant of type %s where boolean was expected\n",
141 expr_value_type_to_string(expr->expr.value_type));
144 *set_rtrn = expr->boolean.set;
148 ident = xkb_atom_text(ctx, expr->ident.ident);
150 if (istreq(ident, "true") ||
151 istreq(ident, "yes") ||
152 istreq(ident, "on")) {
156 else if (istreq(ident, "false") ||
157 istreq(ident, "no") ||
158 istreq(ident, "off")) {
163 log_err(ctx, "Identifier \"%s\" of type boolean is unknown\n", ident);
167 log_err(ctx, "Default \"%s.%s\" of type boolean is unknown\n",
168 xkb_atom_text(ctx, expr->field_ref.element),
169 xkb_atom_text(ctx, expr->field_ref.field));
174 ok = ExprResolveBoolean(ctx, expr->unary.child, set_rtrn);
176 *set_rtrn = !*set_rtrn;
184 case EXPR_UNARY_PLUS:
185 log_err(ctx, "%s of boolean values not permitted\n",
186 expr_op_type_to_string(expr->expr.op));
190 log_wsgo(ctx, "Unknown operator %d in ResolveBoolean\n",
199 ExprResolveKeyCode(struct xkb_context *ctx, const ExprDef *expr,
202 xkb_keycode_t leftRtrn, rightRtrn;
204 switch (expr->expr.op) {
206 if (expr->expr.value_type != EXPR_TYPE_INT) {
208 "Found constant of type %s where an int was expected\n",
209 expr_value_type_to_string(expr->expr.value_type));
213 *kc = (xkb_keycode_t) expr->integer.ival;
220 if (!ExprResolveKeyCode(ctx, expr->binary.left, &leftRtrn) ||
221 !ExprResolveKeyCode(ctx, expr->binary.right, &rightRtrn))
224 switch (expr->expr.op) {
226 *kc = leftRtrn + rightRtrn;
229 *kc = leftRtrn - rightRtrn;
232 *kc = leftRtrn * rightRtrn;
235 if (rightRtrn == 0) {
236 log_err(ctx, "Cannot divide by zero: %d / %d\n",
237 leftRtrn, rightRtrn);
241 *kc = leftRtrn / rightRtrn;
250 if (!ExprResolveKeyCode(ctx, expr->unary.child, &leftRtrn))
256 case EXPR_UNARY_PLUS:
257 return ExprResolveKeyCode(ctx, expr->unary.child, kc);
260 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n",
269 * This function returns ... something. It's a bit of a guess, really.
271 * If an integer is given in value ctx, it will be returned in ival.
272 * If an ident or field reference is given, the lookup function (if given)
273 * will be called. At the moment, only SimpleLookup use this, and they both
274 * return the results in uval. And don't support field references.
279 ExprResolveIntegerLookup(struct xkb_context *ctx, const ExprDef *expr,
280 int *val_rtrn, IdentLookupFunc lookup,
281 const void *lookupPriv)
286 ExprDef *left, *right;
288 switch (expr->expr.op) {
290 if (expr->expr.value_type != EXPR_TYPE_INT) {
292 "Found constant of type %s where an int was expected\n",
293 expr_value_type_to_string(expr->expr.value_type));
297 *val_rtrn = expr->integer.ival;
302 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT, &u);
305 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
306 xkb_atom_text(ctx, expr->ident.ident));
313 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
314 xkb_atom_text(ctx, expr->field_ref.element),
315 xkb_atom_text(ctx, expr->field_ref.field));
322 left = expr->binary.left;
323 right = expr->binary.right;
324 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv) ||
325 !ExprResolveIntegerLookup(ctx, right, &r, lookup, lookupPriv))
328 switch (expr->expr.op) {
340 log_err(ctx, "Cannot divide by zero: %d / %d\n", l, r);
346 log_err(ctx, "%s of integers not permitted\n",
347 expr_op_type_to_string(expr->expr.op));
354 log_wsgo(ctx, "Assignment operator not implemented yet\n");
358 log_err(ctx, "The ! operator cannot be applied to an integer\n");
363 left = expr->unary.child;
364 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv))
367 *val_rtrn = (expr->expr.op == EXPR_NEGATE ? -l : ~l);
370 case EXPR_UNARY_PLUS:
371 left = expr->unary.child;
372 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
376 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n",
385 ExprResolveInteger(struct xkb_context *ctx, const ExprDef *expr,
388 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
392 ExprResolveGroup(struct xkb_context *ctx, const ExprDef *expr,
393 xkb_layout_index_t *group_rtrn)
398 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
403 if (result <= 0 || result > XKB_MAX_GROUPS) {
404 log_err(ctx, "Group index %u is out of range (1..%d)\n",
405 result, XKB_MAX_GROUPS);
409 *group_rtrn = (xkb_layout_index_t) result;
414 ExprResolveLevel(struct xkb_context *ctx, const ExprDef *expr,
415 xkb_level_index_t *level_rtrn)
420 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
426 log_err(ctx, "Shift level %d is out of range\n", result);
430 /* Level is zero-indexed from now on. */
431 *level_rtrn = (unsigned int) (result - 1);
436 ExprResolveButton(struct xkb_context *ctx, const ExprDef *expr, int *btn_rtrn)
438 return ExprResolveIntegerLookup(ctx, expr, btn_rtrn, SimpleLookup,
443 ExprResolveString(struct xkb_context *ctx, const ExprDef *expr,
444 xkb_atom_t *val_rtrn)
446 switch (expr->expr.op) {
448 if (expr->expr.value_type != EXPR_TYPE_STRING) {
449 log_err(ctx, "Found constant of type %s, expected a string\n",
450 expr_value_type_to_string(expr->expr.value_type));
454 *val_rtrn = expr->string.str;
458 log_err(ctx, "Identifier \"%s\" of type string not found\n",
459 xkb_atom_text(ctx, expr->ident.ident));
463 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
464 xkb_atom_text(ctx, expr->field_ref.element),
465 xkb_atom_text(ctx, expr->field_ref.field));
476 case EXPR_UNARY_PLUS:
477 log_err(ctx, "%s of strings not permitted\n",
478 expr_op_type_to_string(expr->expr.op));
482 log_wsgo(ctx, "Unknown operator %d in ResolveString\n",
490 ExprResolveEnum(struct xkb_context *ctx, const ExprDef *expr,
491 unsigned int *val_rtrn, const LookupEntry *values)
493 if (expr->expr.op != EXPR_IDENT) {
494 log_err(ctx, "Found a %s where an enumerated value was expected\n",
495 expr_op_type_to_string(expr->expr.op));
499 if (!SimpleLookup(ctx, values, expr->ident.ident, EXPR_TYPE_INT,
501 log_err(ctx, "Illegal identifier %s; expected one of:\n",
502 xkb_atom_text(ctx, expr->ident.ident));
503 while (values && values->name)
505 log_err(ctx, "\t%s\n", values->name);
515 ExprResolveMaskLookup(struct xkb_context *ctx, const ExprDef *expr,
516 unsigned int *val_rtrn, IdentLookupFunc lookup,
517 const void *lookupPriv)
520 unsigned int l = 0, r = 0;
522 ExprDef *left, *right;
523 const char *bogus = NULL;
525 switch (expr->expr.op) {
527 if (expr->expr.value_type != EXPR_TYPE_INT) {
529 "Found constant of type %s where a mask was expected\n",
530 expr_value_type_to_string(expr->expr.value_type));
533 *val_rtrn = (unsigned int) expr->integer.ival;
537 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT,
540 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
541 xkb_atom_text(ctx, expr->ident.ident));
545 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
546 xkb_atom_text(ctx, expr->field_ref.element),
547 xkb_atom_text(ctx, expr->field_ref.field));
551 bogus = "array reference";
553 case EXPR_ACTION_DECL:
555 bogus = "function use";
557 "Unexpected %s in mask expression; Expression Ignored\n",
565 left = expr->binary.left;
566 right = expr->binary.right;
567 if (!ExprResolveMaskLookup(ctx, left, &l, lookup, lookupPriv) ||
568 !ExprResolveMaskLookup(ctx, right, &r, lookup, lookupPriv))
571 switch (expr->expr.op) {
576 *val_rtrn = l & (~r);
580 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
581 (expr->expr.op == EXPR_DIVIDE ? "divide" : "multiply"));
590 log_wsgo(ctx, "Assignment operator not implemented yet\n");
594 left = expr->unary.child;
595 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
601 case EXPR_UNARY_PLUS:
604 left = expr->unary.child;
605 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
606 log_err(ctx, "The %s operator cannot be used with a mask\n",
607 (expr->expr.op == EXPR_NEGATE ? "-" : "!"));
611 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n",
620 ExprResolveMask(struct xkb_context *ctx, const ExprDef *expr,
621 unsigned int *mask_rtrn, const LookupEntry *values)
623 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, SimpleLookup, values);
627 ExprResolveModMask(struct xkb_context *ctx, const ExprDef *expr,
628 enum mod_type mod_type, const struct xkb_mod_set *mods,
629 xkb_mod_mask_t *mask_rtrn)
631 LookupModMaskPriv priv = { .mods = mods, .mod_type = mod_type };
632 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, LookupModMask, &priv);
636 ExprResolveKeySym(struct xkb_context *ctx, const ExprDef *expr,
637 xkb_keysym_t *sym_rtrn)
641 if (expr->expr.op == EXPR_IDENT) {
642 const char *str = xkb_atom_text(ctx, expr->ident.ident);
643 *sym_rtrn = xkb_keysym_from_name(str, 0);
644 if (*sym_rtrn != XKB_KEY_NoSymbol)
648 if (!ExprResolveInteger(ctx, expr, &val))
651 if (val < 0 || val >= 10)
654 *sym_rtrn = XKB_KEY_0 + (xkb_keysym_t) val;
659 ExprResolveMod(struct xkb_context *ctx, const ExprDef *def,
660 enum mod_type mod_type, const struct xkb_mod_set *mods,
661 xkb_mod_index_t *ndx_rtrn)
666 if (def->expr.op != EXPR_IDENT) {
668 "Cannot resolve virtual modifier: "
669 "found %s where a virtual modifier name was expected\n",
670 expr_op_type_to_string(def->expr.op));
674 name = def->ident.ident;
675 ndx = XkbModNameToIndex(mods, name, mod_type);
676 if (ndx == XKB_MOD_INVALID) {
678 "Cannot resolve virtual modifier: "
679 "\"%s\" was not previously declared\n",
680 xkb_atom_text(ctx, name));