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);
47 *elem_rtrn = xkb_atom_text(ctx, expr->field_ref.element);
48 *field_rtrn = xkb_atom_text(ctx, expr->field_ref.field);
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;
59 log_wsgo(ctx, "Unexpected operator %d in ResolveLhs\n", expr->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;
84 /* Data passed in the *priv argument for LookupModMask. */
86 const struct xkb_mod_set *mods;
87 enum mod_type mod_type;
91 LookupModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
92 enum expr_value_type type, xkb_mod_mask_t *val_rtrn)
96 const LookupModMaskPriv *arg = priv;
97 const struct xkb_mod_set *mods = arg->mods;
98 enum mod_type mod_type = arg->mod_type;
100 if (type != EXPR_TYPE_INT)
103 str = xkb_atom_text(ctx, field);
107 if (istreq(str, "all")) {
108 *val_rtrn = MOD_REAL_MASK_ALL;
112 if (istreq(str, "none")) {
117 ndx = XkbModNameToIndex(mods, field, mod_type);
118 if (ndx == XKB_MOD_INVALID)
121 *val_rtrn = (1u << ndx);
126 ExprResolveBoolean(struct xkb_context *ctx, const ExprDef *expr,
132 switch (expr->expr.op) {
134 if (expr->expr.value_type != EXPR_TYPE_BOOLEAN) {
136 "Found constant of type %s where boolean was expected\n",
137 expr_value_type_to_string(expr->expr.value_type));
140 *set_rtrn = expr->boolean.set;
144 ident = xkb_atom_text(ctx, expr->ident.ident);
146 if (istreq(ident, "true") ||
147 istreq(ident, "yes") ||
148 istreq(ident, "on")) {
152 else if (istreq(ident, "false") ||
153 istreq(ident, "no") ||
154 istreq(ident, "off")) {
159 log_err(ctx, "Identifier \"%s\" of type boolean is unknown\n", ident);
163 log_err(ctx, "Default \"%s.%s\" of type boolean is unknown\n",
164 xkb_atom_text(ctx, expr->field_ref.element),
165 xkb_atom_text(ctx, expr->field_ref.field));
170 ok = ExprResolveBoolean(ctx, expr->unary.child, set_rtrn);
172 *set_rtrn = !*set_rtrn;
180 case EXPR_UNARY_PLUS:
181 log_err(ctx, "%s of boolean values not permitted\n",
182 expr_op_type_to_string(expr->expr.op));
186 log_wsgo(ctx, "Unknown operator %d in ResolveBoolean\n",
195 ExprResolveKeyCode(struct xkb_context *ctx, const ExprDef *expr,
198 xkb_keycode_t leftRtrn, rightRtrn;
200 switch (expr->expr.op) {
202 if (expr->expr.value_type != EXPR_TYPE_INT) {
204 "Found constant of type %s where an int was expected\n",
205 expr_value_type_to_string(expr->expr.value_type));
209 *kc = (xkb_keycode_t) expr->integer.ival;
216 if (!ExprResolveKeyCode(ctx, expr->binary.left, &leftRtrn) ||
217 !ExprResolveKeyCode(ctx, expr->binary.right, &rightRtrn))
220 switch (expr->expr.op) {
222 *kc = leftRtrn + rightRtrn;
225 *kc = leftRtrn - rightRtrn;
228 *kc = leftRtrn * rightRtrn;
231 if (rightRtrn == 0) {
232 log_err(ctx, "Cannot divide by zero: %d / %d\n",
233 leftRtrn, rightRtrn);
237 *kc = leftRtrn / rightRtrn;
246 if (!ExprResolveKeyCode(ctx, expr->unary.child, &leftRtrn))
252 case EXPR_UNARY_PLUS:
253 return ExprResolveKeyCode(ctx, expr->unary.child, kc);
256 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n",
265 * This function returns ... something. It's a bit of a guess, really.
267 * If an integer is given in value ctx, it will be returned in ival.
268 * If an ident or field reference is given, the lookup function (if given)
269 * will be called. At the moment, only SimpleLookup use this, and they both
270 * return the results in uval. And don't support field references.
275 ExprResolveIntegerLookup(struct xkb_context *ctx, const ExprDef *expr,
276 int *val_rtrn, IdentLookupFunc lookup,
277 const void *lookupPriv)
282 ExprDef *left, *right;
284 switch (expr->expr.op) {
286 if (expr->expr.value_type != EXPR_TYPE_INT) {
288 "Found constant of type %s where an int was expected\n",
289 expr_value_type_to_string(expr->expr.value_type));
293 *val_rtrn = expr->integer.ival;
298 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT, &u);
301 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
302 xkb_atom_text(ctx, expr->ident.ident));
309 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
310 xkb_atom_text(ctx, expr->field_ref.element),
311 xkb_atom_text(ctx, expr->field_ref.field));
318 left = expr->binary.left;
319 right = expr->binary.right;
320 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv) ||
321 !ExprResolveIntegerLookup(ctx, right, &r, lookup, lookupPriv))
324 switch (expr->expr.op) {
336 log_err(ctx, "Cannot divide by zero: %d / %d\n", l, r);
342 log_err(ctx, "%s of integers not permitted\n",
343 expr_op_type_to_string(expr->expr.op));
350 log_wsgo(ctx, "Assignment operator not implemented yet\n");
354 log_err(ctx, "The ! operator cannot be applied to an integer\n");
359 left = expr->unary.child;
360 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv))
363 *val_rtrn = (expr->expr.op == EXPR_NEGATE ? -l : ~l);
366 case EXPR_UNARY_PLUS:
367 left = expr->unary.child;
368 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
372 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n",
381 ExprResolveInteger(struct xkb_context *ctx, const ExprDef *expr,
384 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
388 ExprResolveGroup(struct xkb_context *ctx, const ExprDef *expr,
389 xkb_layout_index_t *group_rtrn)
394 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
399 if (result <= 0 || result > XKB_MAX_GROUPS) {
400 log_err(ctx, "Group index %u is out of range (1..%d)\n",
401 result, XKB_MAX_GROUPS);
405 *group_rtrn = (xkb_layout_index_t) result;
410 ExprResolveLevel(struct xkb_context *ctx, const ExprDef *expr,
411 xkb_level_index_t *level_rtrn)
416 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
422 log_err(ctx, "Shift level %d is out of range\n", result);
426 /* Level is zero-indexed from now on. */
427 *level_rtrn = (unsigned int) (result - 1);
432 ExprResolveButton(struct xkb_context *ctx, const ExprDef *expr, int *btn_rtrn)
434 return ExprResolveIntegerLookup(ctx, expr, btn_rtrn, SimpleLookup,
439 ExprResolveString(struct xkb_context *ctx, const ExprDef *expr,
440 xkb_atom_t *val_rtrn)
442 switch (expr->expr.op) {
444 if (expr->expr.value_type != EXPR_TYPE_STRING) {
445 log_err(ctx, "Found constant of type %s, expected a string\n",
446 expr_value_type_to_string(expr->expr.value_type));
450 *val_rtrn = expr->string.str;
454 log_err(ctx, "Identifier \"%s\" of type string not found\n",
455 xkb_atom_text(ctx, expr->ident.ident));
459 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
460 xkb_atom_text(ctx, expr->field_ref.element),
461 xkb_atom_text(ctx, expr->field_ref.field));
472 case EXPR_UNARY_PLUS:
473 log_err(ctx, "%s of strings not permitted\n",
474 expr_op_type_to_string(expr->expr.op));
478 log_wsgo(ctx, "Unknown operator %d in ResolveString\n",
486 ExprResolveEnum(struct xkb_context *ctx, const ExprDef *expr,
487 unsigned int *val_rtrn, const LookupEntry *values)
489 if (expr->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->expr.op));
495 if (!SimpleLookup(ctx, values, expr->ident.ident, EXPR_TYPE_INT,
497 log_err(ctx, "Illegal identifier %s; expected one of:\n",
498 xkb_atom_text(ctx, expr->ident.ident));
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)
516 unsigned int l = 0, r = 0;
518 ExprDef *left, *right;
519 const char *bogus = NULL;
521 switch (expr->expr.op) {
523 if (expr->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->expr.value_type));
529 *val_rtrn = (unsigned int) expr->integer.ival;
533 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT,
536 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
537 xkb_atom_text(ctx, expr->ident.ident));
541 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
542 xkb_atom_text(ctx, expr->field_ref.element),
543 xkb_atom_text(ctx, expr->field_ref.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->binary.left;
562 right = expr->binary.right;
563 if (!ExprResolveMaskLookup(ctx, left, &l, lookup, lookupPriv) ||
564 !ExprResolveMaskLookup(ctx, right, &r, lookup, lookupPriv))
567 switch (expr->expr.op) {
572 *val_rtrn = l & (~r);
576 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
577 (expr->expr.op == EXPR_DIVIDE ? "divide" : "multiply"));
586 log_wsgo(ctx, "Assignment operator not implemented yet\n");
590 left = expr->unary.child;
591 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
597 case EXPR_UNARY_PLUS:
600 left = expr->unary.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->expr.op == EXPR_NEGATE ? "-" : "!"));
607 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n",
616 ExprResolveMask(struct xkb_context *ctx, const ExprDef *expr,
617 unsigned int *mask_rtrn, const LookupEntry *values)
619 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, SimpleLookup, values);
623 ExprResolveModMask(struct xkb_context *ctx, const ExprDef *expr,
624 enum mod_type mod_type, const struct xkb_mod_set *mods,
625 xkb_mod_mask_t *mask_rtrn)
627 LookupModMaskPriv priv = { .mods = mods, .mod_type = mod_type };
628 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, LookupModMask, &priv);
632 ExprResolveKeySym(struct xkb_context *ctx, const ExprDef *expr,
633 xkb_keysym_t *sym_rtrn)
637 if (expr->expr.op == EXPR_IDENT) {
638 const char *str = xkb_atom_text(ctx, expr->ident.ident);
639 *sym_rtrn = xkb_keysym_from_name(str, 0);
640 if (*sym_rtrn != XKB_KEY_NoSymbol)
644 if (!ExprResolveInteger(ctx, expr, &val))
647 if (val < 0 || val >= 10)
650 *sym_rtrn = XKB_KEY_0 + (xkb_keysym_t) val;
655 ExprResolveMod(struct xkb_context *ctx, const ExprDef *def,
656 enum mod_type mod_type, const struct xkb_mod_set *mods,
657 xkb_mod_index_t *ndx_rtrn)
662 if (def->expr.op != EXPR_IDENT) {
664 "Cannot resolve virtual modifier: "
665 "found %s where a virtual modifier name was expected\n",
666 expr_op_type_to_string(def->expr.op));
670 name = def->ident.ident;
671 ndx = XkbModNameToIndex(mods, name, mod_type);
672 if (ndx == XKB_MOD_INVALID) {
674 "Cannot resolve virtual modifier: "
675 "\"%s\" was not previously declared\n",
676 xkb_atom_text(ctx, name));