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 case EXPR_ACTION_DECL:
186 case EXPR_ACTION_LIST:
187 case EXPR_KEYSYM_LIST:
188 log_err(ctx, "%s of boolean values not permitted\n",
189 expr_op_type_to_string(expr->expr.op));
193 log_wsgo(ctx, "Unknown operator %d in ResolveBoolean\n",
202 ExprResolveKeyCode(struct xkb_context *ctx, const ExprDef *expr,
205 xkb_keycode_t leftRtrn, rightRtrn;
207 switch (expr->expr.op) {
209 if (expr->expr.value_type != EXPR_TYPE_INT) {
211 "Found constant of type %s where an int was expected\n",
212 expr_value_type_to_string(expr->expr.value_type));
216 *kc = (xkb_keycode_t) expr->integer.ival;
223 if (!ExprResolveKeyCode(ctx, expr->binary.left, &leftRtrn) ||
224 !ExprResolveKeyCode(ctx, expr->binary.right, &rightRtrn))
227 switch (expr->expr.op) {
229 *kc = leftRtrn + rightRtrn;
232 *kc = leftRtrn - rightRtrn;
235 *kc = leftRtrn * rightRtrn;
238 if (rightRtrn == 0) {
239 log_err(ctx, "Cannot divide by zero: %d / %d\n",
240 leftRtrn, rightRtrn);
244 *kc = leftRtrn / rightRtrn;
253 if (!ExprResolveKeyCode(ctx, expr->unary.child, &leftRtrn))
259 case EXPR_UNARY_PLUS:
260 return ExprResolveKeyCode(ctx, expr->unary.child, kc);
263 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n",
272 * This function returns ... something. It's a bit of a guess, really.
274 * If an integer is given in value ctx, it will be returned in ival.
275 * If an ident or field reference is given, the lookup function (if given)
276 * will be called. At the moment, only SimpleLookup use this, and they both
277 * return the results in uval. And don't support field references.
282 ExprResolveIntegerLookup(struct xkb_context *ctx, const ExprDef *expr,
283 int *val_rtrn, IdentLookupFunc lookup,
284 const void *lookupPriv)
289 ExprDef *left, *right;
291 switch (expr->expr.op) {
293 if (expr->expr.value_type != EXPR_TYPE_INT) {
295 "Found constant of type %s where an int was expected\n",
296 expr_value_type_to_string(expr->expr.value_type));
300 *val_rtrn = expr->integer.ival;
305 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT, &u);
308 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
309 xkb_atom_text(ctx, expr->ident.ident));
316 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
317 xkb_atom_text(ctx, expr->field_ref.element),
318 xkb_atom_text(ctx, expr->field_ref.field));
325 left = expr->binary.left;
326 right = expr->binary.right;
327 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv) ||
328 !ExprResolveIntegerLookup(ctx, right, &r, lookup, lookupPriv))
331 switch (expr->expr.op) {
343 log_err(ctx, "Cannot divide by zero: %d / %d\n", l, r);
349 log_err(ctx, "%s of integers not permitted\n",
350 expr_op_type_to_string(expr->expr.op));
357 log_wsgo(ctx, "Assignment operator not implemented yet\n");
361 log_err(ctx, "The ! operator cannot be applied to an integer\n");
366 left = expr->unary.child;
367 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv))
370 *val_rtrn = (expr->expr.op == EXPR_NEGATE ? -l : ~l);
373 case EXPR_UNARY_PLUS:
374 left = expr->unary.child;
375 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
379 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n",
388 ExprResolveInteger(struct xkb_context *ctx, const ExprDef *expr,
391 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
395 ExprResolveGroup(struct xkb_context *ctx, const ExprDef *expr,
396 xkb_layout_index_t *group_rtrn)
401 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
406 if (result <= 0 || result > XKB_MAX_GROUPS) {
407 log_err(ctx, "Group index %u is out of range (1..%d)\n",
408 result, XKB_MAX_GROUPS);
412 *group_rtrn = (xkb_layout_index_t) result;
417 ExprResolveLevel(struct xkb_context *ctx, const ExprDef *expr,
418 xkb_level_index_t *level_rtrn)
423 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
429 log_err(ctx, "Shift level %d is out of range\n", result);
433 /* Level is zero-indexed from now on. */
434 *level_rtrn = (unsigned int) (result - 1);
439 ExprResolveButton(struct xkb_context *ctx, const ExprDef *expr, int *btn_rtrn)
441 return ExprResolveIntegerLookup(ctx, expr, btn_rtrn, SimpleLookup,
446 ExprResolveString(struct xkb_context *ctx, const ExprDef *expr,
447 xkb_atom_t *val_rtrn)
449 switch (expr->expr.op) {
451 if (expr->expr.value_type != EXPR_TYPE_STRING) {
452 log_err(ctx, "Found constant of type %s, expected a string\n",
453 expr_value_type_to_string(expr->expr.value_type));
457 *val_rtrn = expr->string.str;
461 log_err(ctx, "Identifier \"%s\" of type string not found\n",
462 xkb_atom_text(ctx, expr->ident.ident));
466 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
467 xkb_atom_text(ctx, expr->field_ref.element),
468 xkb_atom_text(ctx, expr->field_ref.field));
479 case EXPR_UNARY_PLUS:
480 case EXPR_ACTION_DECL:
481 case EXPR_ACTION_LIST:
482 case EXPR_KEYSYM_LIST:
483 log_err(ctx, "%s of strings not permitted\n",
484 expr_op_type_to_string(expr->expr.op));
488 log_wsgo(ctx, "Unknown operator %d in ResolveString\n",
496 ExprResolveEnum(struct xkb_context *ctx, const ExprDef *expr,
497 unsigned int *val_rtrn, const LookupEntry *values)
499 if (expr->expr.op != EXPR_IDENT) {
500 log_err(ctx, "Found a %s where an enumerated value was expected\n",
501 expr_op_type_to_string(expr->expr.op));
505 if (!SimpleLookup(ctx, values, expr->ident.ident, EXPR_TYPE_INT,
507 log_err(ctx, "Illegal identifier %s; expected one of:\n",
508 xkb_atom_text(ctx, expr->ident.ident));
509 while (values && values->name)
511 log_err(ctx, "\t%s\n", values->name);
521 ExprResolveMaskLookup(struct xkb_context *ctx, const ExprDef *expr,
522 unsigned int *val_rtrn, IdentLookupFunc lookup,
523 const void *lookupPriv)
526 unsigned int l = 0, r = 0;
528 ExprDef *left, *right;
529 const char *bogus = NULL;
531 switch (expr->expr.op) {
533 if (expr->expr.value_type != EXPR_TYPE_INT) {
535 "Found constant of type %s where a mask was expected\n",
536 expr_value_type_to_string(expr->expr.value_type));
539 *val_rtrn = (unsigned int) expr->integer.ival;
543 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT,
546 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
547 xkb_atom_text(ctx, expr->ident.ident));
551 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
552 xkb_atom_text(ctx, expr->field_ref.element),
553 xkb_atom_text(ctx, expr->field_ref.field));
557 bogus = "array reference";
559 case EXPR_ACTION_DECL:
561 bogus = "function use";
563 "Unexpected %s in mask expression; Expression Ignored\n",
571 left = expr->binary.left;
572 right = expr->binary.right;
573 if (!ExprResolveMaskLookup(ctx, left, &l, lookup, lookupPriv) ||
574 !ExprResolveMaskLookup(ctx, right, &r, lookup, lookupPriv))
577 switch (expr->expr.op) {
582 *val_rtrn = l & (~r);
586 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
587 (expr->expr.op == EXPR_DIVIDE ? "divide" : "multiply"));
596 log_wsgo(ctx, "Assignment operator not implemented yet\n");
600 left = expr->unary.child;
601 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
607 case EXPR_UNARY_PLUS:
610 left = expr->unary.child;
611 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
612 log_err(ctx, "The %s operator cannot be used with a mask\n",
613 (expr->expr.op == EXPR_NEGATE ? "-" : "!"));
617 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n",
626 ExprResolveMask(struct xkb_context *ctx, const ExprDef *expr,
627 unsigned int *mask_rtrn, const LookupEntry *values)
629 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, SimpleLookup, values);
633 ExprResolveModMask(struct xkb_context *ctx, const ExprDef *expr,
634 enum mod_type mod_type, const struct xkb_mod_set *mods,
635 xkb_mod_mask_t *mask_rtrn)
637 LookupModMaskPriv priv = { .mods = mods, .mod_type = mod_type };
638 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, LookupModMask, &priv);
642 ExprResolveKeySym(struct xkb_context *ctx, const ExprDef *expr,
643 xkb_keysym_t *sym_rtrn)
647 if (expr->expr.op == EXPR_IDENT) {
648 const char *str = xkb_atom_text(ctx, expr->ident.ident);
649 *sym_rtrn = xkb_keysym_from_name(str, 0);
650 if (*sym_rtrn != XKB_KEY_NoSymbol)
654 if (!ExprResolveInteger(ctx, expr, &val))
657 if (val < 0 || val >= 10)
660 *sym_rtrn = XKB_KEY_0 + (xkb_keysym_t) val;
665 ExprResolveMod(struct xkb_context *ctx, const ExprDef *def,
666 enum mod_type mod_type, const struct xkb_mod_set *mods,
667 xkb_mod_index_t *ndx_rtrn)
672 if (def->expr.op != EXPR_IDENT) {
674 "Cannot resolve virtual modifier: "
675 "found %s where a virtual modifier name was expected\n",
676 expr_op_type_to_string(def->expr.op));
680 name = def->ident.ident;
681 ndx = XkbModNameToIndex(mods, name, mod_type);
682 if (ndx == XKB_MOD_INVALID) {
684 "Cannot resolve virtual modifier: "
685 "\"%s\" was not previously declared\n",
686 xkb_atom_text(ctx, name));