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 ********************************************************/
29 #include "xkbcomp-priv.h"
33 typedef bool (*IdentLookupFunc)(struct xkb_context *ctx, const void *priv,
34 xkb_atom_t field, enum expr_value_type type,
35 unsigned int *val_rtrn);
38 ExprResolveLhs(struct xkb_context *ctx, const ExprDef *expr,
39 const char **elem_rtrn, const char **field_rtrn,
42 switch (expr->expr.op) {
45 *field_rtrn = xkb_atom_text(ctx, expr->ident.ident);
47 return (*field_rtrn != NULL);
49 *elem_rtrn = xkb_atom_text(ctx, expr->field_ref.element);
50 *field_rtrn = xkb_atom_text(ctx, expr->field_ref.field);
52 return (*elem_rtrn != NULL && *field_rtrn != NULL);
54 *elem_rtrn = xkb_atom_text(ctx, expr->array_ref.element);
55 *field_rtrn = xkb_atom_text(ctx, expr->array_ref.field);
56 *index_rtrn = expr->array_ref.entry;
57 if (expr->array_ref.element != XKB_ATOM_NONE && *elem_rtrn == NULL)
59 if (*field_rtrn == NULL)
65 log_wsgo(ctx, "Unexpected operator %d in ResolveLhs\n", expr->expr.op);
70 SimpleLookup(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
71 enum expr_value_type type, unsigned int *val_rtrn)
73 const LookupEntry *entry;
76 if (!priv || field == XKB_ATOM_NONE || type != EXPR_TYPE_INT)
79 str = xkb_atom_text(ctx, field);
80 for (entry = priv; entry && entry->name; entry++) {
81 if (istreq(str, entry->name)) {
82 *val_rtrn = entry->value;
90 /* Data passed in the *priv argument for LookupModMask. */
92 const struct xkb_mod_set *mods;
93 enum mod_type mod_type;
97 LookupModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
98 enum expr_value_type type, xkb_mod_mask_t *val_rtrn)
102 const LookupModMaskPriv *arg = priv;
103 const struct xkb_mod_set *mods = arg->mods;
104 enum mod_type mod_type = arg->mod_type;
106 if (type != EXPR_TYPE_INT)
109 str = xkb_atom_text(ctx, field);
113 if (istreq(str, "all")) {
114 *val_rtrn = MOD_REAL_MASK_ALL;
118 if (istreq(str, "none")) {
123 ndx = XkbModNameToIndex(mods, field, mod_type);
124 if (ndx == XKB_MOD_INVALID)
127 *val_rtrn = (1u << ndx);
132 ExprResolveBoolean(struct xkb_context *ctx, const ExprDef *expr,
138 switch (expr->expr.op) {
140 if (expr->expr.value_type != EXPR_TYPE_BOOLEAN) {
142 "Found constant of type %s where boolean was expected\n",
143 expr_value_type_to_string(expr->expr.value_type));
146 *set_rtrn = expr->boolean.set;
150 ident = xkb_atom_text(ctx, expr->ident.ident);
152 if (istreq(ident, "true") ||
153 istreq(ident, "yes") ||
154 istreq(ident, "on")) {
158 else if (istreq(ident, "false") ||
159 istreq(ident, "no") ||
160 istreq(ident, "off")) {
165 log_err(ctx, "Identifier \"%s\" of type boolean is unknown\n", ident);
169 log_err(ctx, "Default \"%s.%s\" of type boolean is unknown\n",
170 xkb_atom_text(ctx, expr->field_ref.element),
171 xkb_atom_text(ctx, expr->field_ref.field));
176 ok = ExprResolveBoolean(ctx, expr->unary.child, set_rtrn);
178 *set_rtrn = !*set_rtrn;
186 case EXPR_UNARY_PLUS:
187 case EXPR_ACTION_DECL:
188 case EXPR_ACTION_LIST:
189 case EXPR_KEYSYM_LIST:
190 log_err(ctx, "%s of boolean values not permitted\n",
191 expr_op_type_to_string(expr->expr.op));
195 log_wsgo(ctx, "Unknown operator %d in ResolveBoolean\n",
204 ExprResolveKeyCode(struct xkb_context *ctx, const ExprDef *expr,
207 xkb_keycode_t leftRtrn, rightRtrn;
209 switch (expr->expr.op) {
211 if (expr->expr.value_type != EXPR_TYPE_INT) {
213 "Found constant of type %s where an int was expected\n",
214 expr_value_type_to_string(expr->expr.value_type));
218 *kc = (xkb_keycode_t) expr->integer.ival;
225 if (!ExprResolveKeyCode(ctx, expr->binary.left, &leftRtrn) ||
226 !ExprResolveKeyCode(ctx, expr->binary.right, &rightRtrn))
229 switch (expr->expr.op) {
231 *kc = leftRtrn + rightRtrn;
234 *kc = leftRtrn - rightRtrn;
237 *kc = leftRtrn * rightRtrn;
240 if (rightRtrn == 0) {
241 log_err(ctx, "Cannot divide by zero: %d / %d\n",
242 leftRtrn, rightRtrn);
246 *kc = leftRtrn / rightRtrn;
255 if (!ExprResolveKeyCode(ctx, expr->unary.child, &leftRtrn))
261 case EXPR_UNARY_PLUS:
262 return ExprResolveKeyCode(ctx, expr->unary.child, kc);
265 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n",
274 * This function returns ... something. It's a bit of a guess, really.
276 * If an integer is given in value ctx, it will be returned in ival.
277 * If an ident or field reference is given, the lookup function (if given)
278 * will be called. At the moment, only SimpleLookup use this, and they both
279 * return the results in uval. And don't support field references.
284 ExprResolveIntegerLookup(struct xkb_context *ctx, const ExprDef *expr,
285 int *val_rtrn, IdentLookupFunc lookup,
286 const void *lookupPriv)
291 ExprDef *left, *right;
293 switch (expr->expr.op) {
295 if (expr->expr.value_type != EXPR_TYPE_INT) {
297 "Found constant of type %s where an int was expected\n",
298 expr_value_type_to_string(expr->expr.value_type));
302 *val_rtrn = expr->integer.ival;
307 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT, &u);
310 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
311 xkb_atom_text(ctx, expr->ident.ident));
318 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
319 xkb_atom_text(ctx, expr->field_ref.element),
320 xkb_atom_text(ctx, expr->field_ref.field));
327 left = expr->binary.left;
328 right = expr->binary.right;
329 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv) ||
330 !ExprResolveIntegerLookup(ctx, right, &r, lookup, lookupPriv))
333 switch (expr->expr.op) {
345 log_err(ctx, "Cannot divide by zero: %d / %d\n", l, r);
351 log_err(ctx, "%s of integers not permitted\n",
352 expr_op_type_to_string(expr->expr.op));
359 log_wsgo(ctx, "Assignment operator not implemented yet\n");
363 log_err(ctx, "The ! operator cannot be applied to an integer\n");
368 left = expr->unary.child;
369 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv))
372 *val_rtrn = (expr->expr.op == EXPR_NEGATE ? -l : ~l);
375 case EXPR_UNARY_PLUS:
376 left = expr->unary.child;
377 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
381 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n",
390 ExprResolveInteger(struct xkb_context *ctx, const ExprDef *expr,
393 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
397 ExprResolveGroup(struct xkb_context *ctx, const ExprDef *expr,
398 xkb_layout_index_t *group_rtrn)
403 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
408 if (result <= 0 || result > XKB_MAX_GROUPS) {
409 log_err(ctx, "Group index %u is out of range (1..%d)\n",
410 result, XKB_MAX_GROUPS);
414 *group_rtrn = (xkb_layout_index_t) result;
419 ExprResolveLevel(struct xkb_context *ctx, const ExprDef *expr,
420 xkb_level_index_t *level_rtrn)
425 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
431 log_err(ctx, "Shift level %d is out of range\n", result);
435 /* Level is zero-indexed from now on. */
436 *level_rtrn = (unsigned int) (result - 1);
441 ExprResolveButton(struct xkb_context *ctx, const ExprDef *expr, int *btn_rtrn)
443 return ExprResolveIntegerLookup(ctx, expr, btn_rtrn, SimpleLookup,
448 ExprResolveString(struct xkb_context *ctx, const ExprDef *expr,
449 xkb_atom_t *val_rtrn)
451 switch (expr->expr.op) {
453 if (expr->expr.value_type != EXPR_TYPE_STRING) {
454 log_err(ctx, "Found constant of type %s, expected a string\n",
455 expr_value_type_to_string(expr->expr.value_type));
459 *val_rtrn = expr->string.str;
463 log_err(ctx, "Identifier \"%s\" of type string not found\n",
464 xkb_atom_text(ctx, expr->ident.ident));
468 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
469 xkb_atom_text(ctx, expr->field_ref.element),
470 xkb_atom_text(ctx, expr->field_ref.field));
481 case EXPR_UNARY_PLUS:
482 case EXPR_ACTION_DECL:
483 case EXPR_ACTION_LIST:
484 case EXPR_KEYSYM_LIST:
485 log_err(ctx, "%s of strings not permitted\n",
486 expr_op_type_to_string(expr->expr.op));
490 log_wsgo(ctx, "Unknown operator %d in ResolveString\n",
498 ExprResolveEnum(struct xkb_context *ctx, const ExprDef *expr,
499 unsigned int *val_rtrn, const LookupEntry *values)
501 if (expr->expr.op != EXPR_IDENT) {
502 log_err(ctx, "Found a %s where an enumerated value was expected\n",
503 expr_op_type_to_string(expr->expr.op));
507 if (!SimpleLookup(ctx, values, expr->ident.ident, EXPR_TYPE_INT,
509 log_err(ctx, "Illegal identifier %s; expected one of:\n",
510 xkb_atom_text(ctx, expr->ident.ident));
511 while (values && values->name)
513 log_err(ctx, "\t%s\n", values->name);
523 ExprResolveMaskLookup(struct xkb_context *ctx, const ExprDef *expr,
524 unsigned int *val_rtrn, IdentLookupFunc lookup,
525 const void *lookupPriv)
528 unsigned int l = 0, r = 0;
530 ExprDef *left, *right;
531 const char *bogus = NULL;
533 switch (expr->expr.op) {
535 if (expr->expr.value_type != EXPR_TYPE_INT) {
537 "Found constant of type %s where a mask was expected\n",
538 expr_value_type_to_string(expr->expr.value_type));
541 *val_rtrn = (unsigned int) expr->integer.ival;
545 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT,
548 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
549 xkb_atom_text(ctx, expr->ident.ident));
553 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
554 xkb_atom_text(ctx, expr->field_ref.element),
555 xkb_atom_text(ctx, expr->field_ref.field));
559 bogus = "array reference";
561 case EXPR_ACTION_DECL:
563 bogus = "function use";
565 "Unexpected %s in mask expression; Expression Ignored\n",
573 left = expr->binary.left;
574 right = expr->binary.right;
575 if (!ExprResolveMaskLookup(ctx, left, &l, lookup, lookupPriv) ||
576 !ExprResolveMaskLookup(ctx, right, &r, lookup, lookupPriv))
579 switch (expr->expr.op) {
584 *val_rtrn = l & (~r);
588 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
589 (expr->expr.op == EXPR_DIVIDE ? "divide" : "multiply"));
598 log_wsgo(ctx, "Assignment operator not implemented yet\n");
602 left = expr->unary.child;
603 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
609 case EXPR_UNARY_PLUS:
612 left = expr->unary.child;
613 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
614 log_err(ctx, "The %s operator cannot be used with a mask\n",
615 (expr->expr.op == EXPR_NEGATE ? "-" : "!"));
619 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n",
628 ExprResolveMask(struct xkb_context *ctx, const ExprDef *expr,
629 unsigned int *mask_rtrn, const LookupEntry *values)
631 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, SimpleLookup, values);
635 ExprResolveModMask(struct xkb_context *ctx, const ExprDef *expr,
636 enum mod_type mod_type, const struct xkb_mod_set *mods,
637 xkb_mod_mask_t *mask_rtrn)
639 LookupModMaskPriv priv = { .mods = mods, .mod_type = mod_type };
640 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, LookupModMask, &priv);
644 ExprResolveKeySym(struct xkb_context *ctx, const ExprDef *expr,
645 xkb_keysym_t *sym_rtrn)
649 if (expr->expr.op == EXPR_IDENT) {
650 const char *str = xkb_atom_text(ctx, expr->ident.ident);
651 *sym_rtrn = xkb_keysym_from_name(str, 0);
652 if (*sym_rtrn != XKB_KEY_NoSymbol)
656 if (!ExprResolveInteger(ctx, expr, &val))
659 if (val < 0 || val >= 10)
662 *sym_rtrn = XKB_KEY_0 + (xkb_keysym_t) val;
667 ExprResolveMod(struct xkb_context *ctx, const ExprDef *def,
668 enum mod_type mod_type, const struct xkb_mod_set *mods,
669 xkb_mod_index_t *ndx_rtrn)
674 if (def->expr.op != EXPR_IDENT) {
676 "Cannot resolve virtual modifier: "
677 "found %s where a virtual modifier name was expected\n",
678 expr_op_type_to_string(def->expr.op));
682 name = def->ident.ident;
683 ndx = XkbModNameToIndex(mods, name, mod_type);
684 if (ndx == XKB_MOD_INVALID) {
686 "Cannot resolve virtual modifier: "
687 "\"%s\" was not previously declared\n",
688 xkb_atom_text(ctx, name));