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 typedef bool (*IdentLookupFunc)(struct xkb_context *ctx, const void *priv,
30 xkb_atom_t field, enum expr_value_type type,
31 unsigned int *val_rtrn);
34 exprOpText(enum expr_op_type op)
40 strcpy(buf, "literal");
43 strcpy(buf, "identifier");
45 case EXPR_ACTION_DECL:
46 strcpy(buf, "action declaration");
49 strcpy(buf, "field reference");
52 strcpy(buf, "array reference");
54 case EXPR_KEYSYM_LIST:
55 strcpy(buf, "list of keysyms");
57 case EXPR_ACTION_LIST:
58 strcpy(buf, "list of actions");
61 strcpy(buf, "addition");
64 strcpy(buf, "subtraction");
67 strcpy(buf, "multiplication");
70 strcpy(buf, "division");
73 strcpy(buf, "assignment");
76 strcpy(buf, "logical not");
79 strcpy(buf, "arithmetic negation");
82 strcpy(buf, "bitwise inversion");
85 strcpy(buf, "unary plus");
88 snprintf(buf, sizeof(buf), "illegal(%d)", op);
95 exprValueTypeText(enum expr_value_type type)
100 case EXPR_TYPE_UNKNOWN:
101 strcpy(buf, "unknown");
103 case EXPR_TYPE_BOOLEAN:
104 strcpy(buf, "boolean");
109 case EXPR_TYPE_STRING:
110 strcpy(buf, "string");
112 case EXPR_TYPE_ACTION:
113 strcpy(buf, "action");
115 case EXPR_TYPE_KEYNAME:
116 strcpy(buf, "keyname");
119 snprintf(buf, sizeof(buf), "illegal(%d)", type);
126 ExprResolveLhs(struct xkb_context *ctx, ExprDef *expr, const char **elem_rtrn,
127 const char **field_rtrn, ExprDef **index_rtrn)
132 *field_rtrn = xkb_atom_text(ctx, expr->value.str);
136 *elem_rtrn = xkb_atom_text(ctx, expr->value.field.element);
137 *field_rtrn = xkb_atom_text(ctx, expr->value.field.field);
141 *elem_rtrn = xkb_atom_text(ctx, expr->value.array.element);
142 *field_rtrn = xkb_atom_text(ctx, expr->value.array.field);
143 *index_rtrn = expr->value.array.entry;
148 log_wsgo(ctx, "Unexpected operator %d in ResolveLhs\n", expr->op);
153 SimpleLookup(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
154 enum expr_value_type type, unsigned int *val_rtrn)
156 const LookupEntry *entry;
159 if (!priv || field == XKB_ATOM_NONE || type != EXPR_TYPE_INT)
162 str = xkb_atom_text(ctx, field);
163 for (entry = priv; entry && entry->name; entry++) {
164 if (istreq(str, entry->name)) {
165 *val_rtrn = entry->value;
173 static const LookupEntry modIndexNames[] = {
174 { "shift", ShiftMapIndex },
175 { "control", ControlMapIndex },
176 { "lock", LockMapIndex },
177 { "mod1", Mod1MapIndex },
178 { "mod2", Mod2MapIndex },
179 { "mod3", Mod3MapIndex },
180 { "mod4", Mod4MapIndex },
181 { "mod5", Mod5MapIndex },
182 { "none", XkbNoModifier },
187 LookupModIndex(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
188 enum expr_value_type type, xkb_mod_index_t *val_rtrn)
190 return SimpleLookup(ctx, modIndexNames, field, type, val_rtrn);
194 LookupModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
195 enum expr_value_type type, xkb_mod_mask_t *val_rtrn)
200 if (type != EXPR_TYPE_INT)
203 str = xkb_atom_text(ctx, field);
205 if (istreq(str, "all"))
207 else if (istreq(str, "none"))
209 else if (LookupModIndex(ctx, priv, field, type, &ndx))
210 *val_rtrn = (1 << ndx);
218 ExprResolveBoolean(struct xkb_context *ctx, ExprDef *expr, bool *set_rtrn)
225 if (expr->value_type != EXPR_TYPE_BOOLEAN) {
227 "Found constant of type %s where boolean was expected\n",
228 exprValueTypeText(expr->value_type));
231 *set_rtrn = !!expr->value.ival;
235 ident = xkb_atom_text(ctx, expr->value.str);
237 if (istreq(ident, "true") ||
238 istreq(ident, "yes") ||
239 istreq(ident, "on")) {
243 else if (istreq(ident, "false") ||
244 istreq(ident, "no") ||
245 istreq(ident, "off")) {
250 log_err(ctx, "Identifier \"%s\" of type boolean is unknown\n",
251 xkb_atom_text(ctx, expr->value.str));
255 log_err(ctx, "Default \"%s.%s\" of type boolean is unknown\n",
256 xkb_atom_text(ctx, expr->value.field.element),
257 xkb_atom_text(ctx, expr->value.field.field));
262 ok = ExprResolveBoolean(ctx, expr, set_rtrn);
264 *set_rtrn = !*set_rtrn;
272 case EXPR_UNARY_PLUS:
273 log_err(ctx, "%s of boolean values not permitted\n",
274 exprOpText(expr->op));
278 log_wsgo(ctx, "Unknown operator %d in ResolveBoolean\n", expr->op);
286 ExprResolveKeyCode(struct xkb_context *ctx, ExprDef *expr, xkb_keycode_t *kc)
288 xkb_keycode_t leftRtrn, rightRtrn;
289 ExprDef *left, *right;
293 if (expr->value_type != EXPR_TYPE_INT) {
295 "Found constant of type %s where an int was expected\n",
296 exprValueTypeText(expr->value_type));
300 *kc = expr->value.uval;
307 left = expr->value.binary.left;
308 right = expr->value.binary.right;
310 if (!ExprResolveKeyCode(ctx, left, &leftRtrn) ||
311 !ExprResolveKeyCode(ctx, right, &rightRtrn))
316 *kc = leftRtrn + rightRtrn;
319 *kc = leftRtrn - rightRtrn;
322 *kc = leftRtrn * rightRtrn;
325 if (rightRtrn == 0) {
326 log_err(ctx, "Cannot divide by zero: %d / %d\n",
327 leftRtrn, rightRtrn);
331 *kc = leftRtrn / rightRtrn;
340 left = expr->value.child;
341 if (!ExprResolveKeyCode(ctx, left, &leftRtrn))
347 case EXPR_UNARY_PLUS:
348 left = expr->value.child;
349 return ExprResolveKeyCode(ctx, left, kc);
352 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n", expr->op);
360 * This function returns ... something. It's a bit of a guess, really.
362 * If an integer is given in value ctx, it will be returned in ival.
363 * If an ident or field reference is given, the lookup function (if given)
364 * will be called. At the moment, only SimpleLookup use this, and they both
365 * return the results in uval. And don't support field references.
370 ExprResolveIntegerLookup(struct xkb_context *ctx, ExprDef *expr,
371 int *val_rtrn, IdentLookupFunc lookup,
372 const void *lookupPriv)
377 ExprDef *left, *right;
381 if (expr->value_type != EXPR_TYPE_INT) {
383 "Found constant of type %s where an int was expected\n",
384 exprValueTypeText(expr->value_type));
388 *val_rtrn = expr->value.ival;
393 ok = lookup(ctx, lookupPriv, expr->value.str, EXPR_TYPE_INT, &u);
396 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
397 xkb_atom_text(ctx, expr->value.str));
404 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
405 xkb_atom_text(ctx, expr->value.field.element),
406 xkb_atom_text(ctx, expr->value.field.field));
413 left = expr->value.binary.left;
414 right = expr->value.binary.right;
415 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv) ||
416 !ExprResolveIntegerLookup(ctx, right, &r, lookup, lookupPriv))
431 log_err(ctx, "Cannot divide by zero: %d / %d\n", l, r);
443 log_wsgo(ctx, "Assignment operator not implemented yet\n");
447 log_err(ctx, "The ! operator cannot be applied to an integer\n");
452 left = expr->value.child;
453 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv))
456 *val_rtrn = (expr->op == EXPR_NEGATE ? -l : ~l);
459 case EXPR_UNARY_PLUS:
460 left = expr->value.child;
461 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
465 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n", expr->op);
473 ExprResolveInteger(struct xkb_context *ctx, ExprDef *expr, int *val_rtrn)
475 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
479 ExprResolveGroup(struct xkb_context *ctx, ExprDef *expr,
480 xkb_group_index_t *group_rtrn)
484 static const LookupEntry group_names[] = {
496 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
501 if (result <= 0 || result > XkbNumKbdGroups) {
502 log_err(ctx, "Group index %u is out of range (1..%d)\n",
503 result, XkbNumKbdGroups);
507 *group_rtrn = (xkb_group_index_t) result;
512 ExprResolveLevel(struct xkb_context *ctx, ExprDef *expr,
513 unsigned int *level_rtrn)
517 static const LookupEntry level_names[] = {
529 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
534 if (result < 1 || result > XkbMaxShiftLevel) {
535 log_err(ctx, "Shift level %d is out of range (1..%d)\n",
536 result, XkbMaxShiftLevel);
540 *level_rtrn = (unsigned int) result;
545 ExprResolveButton(struct xkb_context *ctx, ExprDef *expr, int *btn_rtrn)
548 static const LookupEntry button_names[] = {
558 if (!ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
567 ExprResolveString(struct xkb_context *ctx, ExprDef *expr,
568 const char **val_rtrn)
572 if (expr->value_type != EXPR_TYPE_STRING) {
573 log_err(ctx, "Found constant of type %s, expected a string\n",
574 exprValueTypeText(expr->value_type));
578 *val_rtrn = xkb_atom_text(ctx, expr->value.str);
582 log_err(ctx, "Identifier \"%s\" of type string not found\n",
583 xkb_atom_text(ctx, expr->value.str));
587 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
588 xkb_atom_text(ctx, expr->value.field.element),
589 xkb_atom_text(ctx, expr->value.field.field));
600 case EXPR_UNARY_PLUS:
601 log_err(ctx, "%s of strings not permitted\n", exprOpText(expr->op));
605 log_wsgo(ctx, "Unknown operator %d in ResolveString\n", expr->op);
612 ExprResolveKeyName(struct xkb_context *ctx, ExprDef *expr,
613 char name[XkbKeyNameLength])
617 if (expr->value_type != EXPR_TYPE_KEYNAME) {
618 log_err(ctx, "Found constant of type %s, expected a key name\n",
619 exprValueTypeText(expr->value_type));
622 memcpy(name, expr->value.keyName, XkbKeyNameLength);
626 log_err(ctx, "Identifier \"%s\" of type string not found\n",
627 xkb_atom_text(ctx, expr->value.str));
631 log_err(ctx, "Default \"%s.%s\" of type key name not found\n",
632 xkb_atom_text(ctx, expr->value.field.element),
633 xkb_atom_text(ctx, expr->value.field.field));
644 case EXPR_UNARY_PLUS:
645 log_err(ctx, "%s of key name values not permitted\n",
646 exprOpText(expr->op));
650 log_wsgo(ctx, "Unknown operator %d in ResolveKeyName\n", expr->op);
657 ExprResolveEnum(struct xkb_context *ctx, ExprDef *expr,
658 unsigned int *val_rtrn, const LookupEntry *values)
660 if (expr->op != EXPR_IDENT) {
661 log_err(ctx, "Found a %s where an enumerated value was expected\n",
662 exprOpText(expr->op));
666 if (!SimpleLookup(ctx, values, expr->value.str, EXPR_TYPE_INT,
668 log_err(ctx, "Illegal identifier %s; expected one of:\n",
669 xkb_atom_text(ctx, expr->value.str));
670 while (values && values->name)
672 log_err(ctx, "\t%s\n", values->name);
682 ExprResolveMaskLookup(struct xkb_context *ctx, ExprDef *expr,
683 unsigned int *val_rtrn, IdentLookupFunc lookup,
684 const void *lookupPriv)
689 ExprDef *left, *right;
690 const char *bogus = NULL;
694 if (expr->value_type != EXPR_TYPE_INT) {
696 "Found constant of type %s where a mask was expected\n",
697 exprValueTypeText(expr->value_type));
700 *val_rtrn = (unsigned int) expr->value.ival;
704 ok = lookup(ctx, lookupPriv, expr->value.str, EXPR_TYPE_INT,
707 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
708 xkb_atom_text(ctx, expr->value.str));
712 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
713 xkb_atom_text(ctx, expr->value.field.element),
714 xkb_atom_text(ctx, expr->value.field.field));
718 bogus = "array reference";
720 case EXPR_ACTION_DECL:
722 bogus = "function use";
724 "Unexpected %s in mask expression; Expression Ignored\n",
732 left = expr->value.binary.left;
733 right = expr->value.binary.right;
734 if (!ExprResolveMaskLookup(ctx, left, &l, lookup, lookupPriv) ||
735 !ExprResolveMaskLookup(ctx, right, &r, lookup, lookupPriv))
743 *val_rtrn = l & (~r);
747 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
748 (expr->op == EXPR_DIVIDE ? "divide" : "multiply"));
757 log_wsgo(ctx, "Assignment operator not implemented yet\n");
761 left = expr->value.child;
762 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
768 case EXPR_UNARY_PLUS:
771 left = expr->value.child;
772 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
773 log_err(ctx, "The %s operator cannot be used with a mask\n",
774 (expr->op == EXPR_NEGATE ? "-" : "!"));
778 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n", expr->op);
786 ExprResolveMask(struct xkb_context *ctx, ExprDef *expr,
787 unsigned int *mask_rtrn, const LookupEntry *values)
789 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, SimpleLookup, values);
793 ExprResolveModMask(struct xkb_context *ctx, ExprDef *expr,
794 xkb_mod_mask_t *mask_rtrn)
796 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, LookupModMask, NULL);
800 ExprResolveVModMask(struct xkb_keymap *keymap, ExprDef *expr,
801 xkb_mod_mask_t *mask_rtrn)
803 return ExprResolveMaskLookup(keymap->ctx, expr, mask_rtrn, LookupVModMask,
808 ExprResolveKeySym(struct xkb_context *ctx, ExprDef *expr,
809 xkb_keysym_t *sym_rtrn)
813 if (expr->op == EXPR_IDENT) {
815 str = xkb_atom_text(ctx, expr->value.str);
816 *sym_rtrn = xkb_keysym_from_name(str);
817 if (*sym_rtrn != XKB_KEY_NoSymbol)
821 if (!ExprResolveInteger(ctx, expr, &val))
824 if (val < 0 || val >= 10)
827 *sym_rtrn = ((xkb_keysym_t) val) + '0';