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_keymap *keymap;
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_keymap *keymap = arg->keymap;
98 enum mod_type mod_type = arg->mod_type;
100 if (type != EXPR_TYPE_INT)
103 str = xkb_atom_text(ctx, field);
105 if (istreq(str, "all")) {
106 *val_rtrn = MOD_REAL_MASK_ALL;
110 if (istreq(str, "none")) {
115 ndx = ModNameToIndex(keymap, field, mod_type);
116 if (ndx == XKB_MOD_INVALID)
119 *val_rtrn = (1u << ndx);
124 ExprResolveBoolean(struct xkb_context *ctx, const ExprDef *expr,
130 switch (expr->expr.op) {
132 if (expr->expr.value_type != EXPR_TYPE_BOOLEAN) {
134 "Found constant of type %s where boolean was expected\n",
135 expr_value_type_to_string(expr->expr.value_type));
138 *set_rtrn = expr->boolean.set;
142 ident = xkb_atom_text(ctx, expr->ident.ident);
144 if (istreq(ident, "true") ||
145 istreq(ident, "yes") ||
146 istreq(ident, "on")) {
150 else if (istreq(ident, "false") ||
151 istreq(ident, "no") ||
152 istreq(ident, "off")) {
157 log_err(ctx, "Identifier \"%s\" of type boolean is unknown\n", ident);
161 log_err(ctx, "Default \"%s.%s\" of type boolean is unknown\n",
162 xkb_atom_text(ctx, expr->field_ref.element),
163 xkb_atom_text(ctx, expr->field_ref.field));
168 ok = ExprResolveBoolean(ctx, expr, set_rtrn);
170 *set_rtrn = !*set_rtrn;
178 case EXPR_UNARY_PLUS:
179 log_err(ctx, "%s of boolean values not permitted\n",
180 expr_op_type_to_string(expr->expr.op));
184 log_wsgo(ctx, "Unknown operator %d in ResolveBoolean\n",
193 ExprResolveKeyCode(struct xkb_context *ctx, const ExprDef *expr,
196 xkb_keycode_t leftRtrn, rightRtrn;
198 switch (expr->expr.op) {
200 if (expr->expr.value_type != EXPR_TYPE_INT) {
202 "Found constant of type %s where an int was expected\n",
203 expr_value_type_to_string(expr->expr.value_type));
207 *kc = (xkb_keycode_t) expr->integer.ival;
214 if (!ExprResolveKeyCode(ctx, expr->binary.left, &leftRtrn) ||
215 !ExprResolveKeyCode(ctx, expr->binary.right, &rightRtrn))
218 switch (expr->expr.op) {
220 *kc = leftRtrn + rightRtrn;
223 *kc = leftRtrn - rightRtrn;
226 *kc = leftRtrn * rightRtrn;
229 if (rightRtrn == 0) {
230 log_err(ctx, "Cannot divide by zero: %d / %d\n",
231 leftRtrn, rightRtrn);
235 *kc = leftRtrn / rightRtrn;
244 if (!ExprResolveKeyCode(ctx, expr->unary.child, &leftRtrn))
250 case EXPR_UNARY_PLUS:
251 return ExprResolveKeyCode(ctx, expr->unary.child, kc);
254 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n",
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;
282 switch (expr->expr.op) {
284 if (expr->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->expr.value_type));
291 *val_rtrn = expr->integer.ival;
296 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT, &u);
299 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
300 xkb_atom_text(ctx, expr->ident.ident));
307 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
308 xkb_atom_text(ctx, expr->field_ref.element),
309 xkb_atom_text(ctx, expr->field_ref.field));
316 left = expr->binary.left;
317 right = expr->binary.right;
318 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv) ||
319 !ExprResolveIntegerLookup(ctx, right, &r, lookup, lookupPriv))
322 switch (expr->expr.op) {
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->unary.child;
356 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv))
359 *val_rtrn = (expr->expr.op == EXPR_NEGATE ? -l : ~l);
362 case EXPR_UNARY_PLUS:
363 left = expr->unary.child;
364 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
368 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n",
377 ExprResolveInteger(struct xkb_context *ctx, const ExprDef *expr,
380 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
384 ExprResolveGroup(struct xkb_context *ctx, const ExprDef *expr,
385 xkb_layout_index_t *group_rtrn)
390 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
395 if (result <= 0 || result > XKB_MAX_GROUPS) {
396 log_err(ctx, "Group index %u is out of range (1..%d)\n",
397 result, XKB_MAX_GROUPS);
401 *group_rtrn = (xkb_layout_index_t) result;
406 ExprResolveLevel(struct xkb_context *ctx, const ExprDef *expr,
407 xkb_level_index_t *level_rtrn)
412 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
418 log_err(ctx, "Shift level %d is out of range\n", result);
422 /* Level is zero-indexed from now on. */
423 *level_rtrn = (unsigned int) (result - 1);
428 ExprResolveButton(struct xkb_context *ctx, const ExprDef *expr, int *btn_rtrn)
430 return ExprResolveIntegerLookup(ctx, expr, btn_rtrn, SimpleLookup,
435 ExprResolveString(struct xkb_context *ctx, const ExprDef *expr,
436 xkb_atom_t *val_rtrn)
438 switch (expr->expr.op) {
440 if (expr->expr.value_type != EXPR_TYPE_STRING) {
441 log_err(ctx, "Found constant of type %s, expected a string\n",
442 expr_value_type_to_string(expr->expr.value_type));
446 *val_rtrn = expr->string.str;
450 log_err(ctx, "Identifier \"%s\" of type string not found\n",
451 xkb_atom_text(ctx, expr->ident.ident));
455 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
456 xkb_atom_text(ctx, expr->field_ref.element),
457 xkb_atom_text(ctx, expr->field_ref.field));
468 case EXPR_UNARY_PLUS:
469 log_err(ctx, "%s of strings not permitted\n",
470 expr_op_type_to_string(expr->expr.op));
474 log_wsgo(ctx, "Unknown operator %d in ResolveString\n",
482 ExprResolveEnum(struct xkb_context *ctx, const ExprDef *expr,
483 unsigned int *val_rtrn, const LookupEntry *values)
485 if (expr->expr.op != EXPR_IDENT) {
486 log_err(ctx, "Found a %s where an enumerated value was expected\n",
487 expr_op_type_to_string(expr->expr.op));
491 if (!SimpleLookup(ctx, values, expr->ident.ident, EXPR_TYPE_INT,
493 log_err(ctx, "Illegal identifier %s; expected one of:\n",
494 xkb_atom_text(ctx, expr->ident.ident));
495 while (values && values->name)
497 log_err(ctx, "\t%s\n", values->name);
507 ExprResolveMaskLookup(struct xkb_context *ctx, const ExprDef *expr,
508 unsigned int *val_rtrn, IdentLookupFunc lookup,
509 const void *lookupPriv)
514 ExprDef *left, *right;
515 const char *bogus = NULL;
517 switch (expr->expr.op) {
519 if (expr->expr.value_type != EXPR_TYPE_INT) {
521 "Found constant of type %s where a mask was expected\n",
522 expr_value_type_to_string(expr->expr.value_type));
525 *val_rtrn = (unsigned int) expr->integer.ival;
529 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT,
532 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
533 xkb_atom_text(ctx, expr->ident.ident));
537 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
538 xkb_atom_text(ctx, expr->field_ref.element),
539 xkb_atom_text(ctx, expr->field_ref.field));
543 bogus = "array reference";
545 case EXPR_ACTION_DECL:
547 bogus = "function use";
549 "Unexpected %s in mask expression; Expression Ignored\n",
557 left = expr->binary.left;
558 right = expr->binary.right;
559 if (!ExprResolveMaskLookup(ctx, left, &l, lookup, lookupPriv) ||
560 !ExprResolveMaskLookup(ctx, right, &r, lookup, lookupPriv))
563 switch (expr->expr.op) {
568 *val_rtrn = l & (~r);
572 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
573 (expr->expr.op == EXPR_DIVIDE ? "divide" : "multiply"));
582 log_wsgo(ctx, "Assignment operator not implemented yet\n");
586 left = expr->unary.child;
587 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
593 case EXPR_UNARY_PLUS:
596 left = expr->unary.child;
597 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
598 log_err(ctx, "The %s operator cannot be used with a mask\n",
599 (expr->expr.op == EXPR_NEGATE ? "-" : "!"));
603 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n",
612 ExprResolveMask(struct xkb_context *ctx, const ExprDef *expr,
613 unsigned int *mask_rtrn, const LookupEntry *values)
615 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, SimpleLookup, values);
619 ExprResolveModMask(struct xkb_keymap *keymap, const ExprDef *expr,
620 enum mod_type mod_type, xkb_mod_mask_t *mask_rtrn)
622 LookupModMaskPriv priv = { .keymap = keymap, .mod_type = mod_type };
623 return ExprResolveMaskLookup(keymap->ctx, expr, mask_rtrn, LookupModMask,
628 ExprResolveKeySym(struct xkb_context *ctx, const ExprDef *expr,
629 xkb_keysym_t *sym_rtrn)
633 if (expr->expr.op == EXPR_IDENT) {
634 const char *str = xkb_atom_text(ctx, expr->ident.ident);
635 *sym_rtrn = xkb_keysym_from_name(str, 0);
636 if (*sym_rtrn != XKB_KEY_NoSymbol)
640 if (!ExprResolveInteger(ctx, expr, &val))
643 if (val < 0 || val >= 10)
646 *sym_rtrn = XKB_KEY_0 + (xkb_keysym_t) val;
651 ExprResolveMod(struct xkb_keymap *keymap, const ExprDef *def,
652 enum mod_type mod_type, xkb_mod_index_t *ndx_rtrn)
657 if (def->expr.op != EXPR_IDENT) {
659 "Cannot resolve virtual modifier: "
660 "found %s where a virtual modifier name was expected\n",
661 expr_op_type_to_string(def->expr.op));
665 name = def->ident.ident;
666 ndx = ModNameToIndex(keymap, name, mod_type);
667 if (ndx == XKB_MOD_INVALID) {
669 "Cannot resolve virtual modifier: "
670 "\"%s\" was not previously declared\n",
671 xkb_atom_text(keymap->ctx, name));