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 ExprResult *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,
154 xkb_atom_t field, enum expr_value_type type,
155 ExprResult *val_rtrn)
157 const LookupEntry *entry;
160 if (!priv || field == XKB_ATOM_NONE || type != EXPR_TYPE_INT)
163 str = xkb_atom_text(ctx, field);
164 for (entry = priv; entry && entry->name; entry++) {
165 if (istreq(str, entry->name)) {
166 val_rtrn->uval = entry->result;
174 static const LookupEntry modIndexNames[] = {
175 { "shift", ShiftMapIndex },
176 { "control", ControlMapIndex },
177 { "lock", LockMapIndex },
178 { "mod1", Mod1MapIndex },
179 { "mod2", Mod2MapIndex },
180 { "mod3", Mod3MapIndex },
181 { "mod4", Mod4MapIndex },
182 { "mod5", Mod5MapIndex },
183 { "none", XkbNoModifier },
188 LookupModIndex(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
189 enum expr_value_type type, ExprResult *val_rtrn)
191 return SimpleLookup(ctx, modIndexNames, field, type, val_rtrn);
195 LookupModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
196 enum expr_value_type type, ExprResult *val_rtrn)
201 if (type != EXPR_TYPE_INT)
203 str = xkb_atom_text(ctx, field);
206 if (istreq(str, "all"))
207 val_rtrn->uval = 0xff;
208 else if (istreq(str, "none"))
210 else if (LookupModIndex(ctx, priv, field, type, val_rtrn))
211 val_rtrn->uval = (1 << val_rtrn->uval);
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 a string is given in value ctx, its first character will be
363 * returned in uval. If an integer is given in value ctx, it will be
364 * returned in ival. If a float is given in value ctx, it will be
365 * returned as millimetres (rather than points) in ival.
367 * If an ident or field reference is given, the lookup function (if given)
368 * will be called. At the moment, only SimpleLookup use this, and they both
369 * return the results in uval. And don't support field references.
374 ExprResolveIntegerLookup(struct xkb_context *ctx, ExprDef *expr,
375 ExprResult *val_rtrn, IdentLookupFunc lookup,
376 const void *lookupPriv)
379 ExprResult leftRtrn, rightRtrn;
380 ExprDef *left, *right;
384 if (expr->value_type == EXPR_TYPE_STRING) {
386 str = xkb_atom_text(ctx, expr->value.str);
388 switch (strlen(str)) {
393 val_rtrn->uval = str[0];
399 if (expr->value_type != EXPR_TYPE_INT) {
401 "Found constant of type %s where an int was expected\n",
402 exprValueTypeText(expr->value_type));
405 val_rtrn->ival = expr->value.ival;
410 ok = lookup(ctx, lookupPriv, expr->value.str,
411 EXPR_TYPE_INT, val_rtrn);
413 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
414 xkb_atom_text(ctx, expr->value.str));
418 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
419 xkb_atom_text(ctx, expr->value.field.element),
420 xkb_atom_text(ctx, expr->value.field.field));
427 left = expr->value.binary.left;
428 right = expr->value.binary.right;
429 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
431 ExprResolveIntegerLookup(ctx, right, &rightRtrn, lookup,
435 val_rtrn->ival = leftRtrn.ival + rightRtrn.ival;
438 val_rtrn->ival = leftRtrn.ival - rightRtrn.ival;
441 val_rtrn->ival = leftRtrn.ival * rightRtrn.ival;
444 if (rightRtrn.ival == 0) {
445 log_err(ctx, "Cannot divide by zero: %d / %d\n",
446 leftRtrn.ival, rightRtrn.ival);
449 val_rtrn->ival = leftRtrn.ival / rightRtrn.ival;
459 log_wsgo(ctx, "Assignment operator not implemented yet\n");
463 log_err(ctx, "The ! operator cannot be applied to an integer\n");
468 left = expr->value.child;
469 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
471 if (expr->op == EXPR_NEGATE)
472 val_rtrn->ival = -leftRtrn.ival;
474 val_rtrn->ival = ~leftRtrn.ival;
479 case EXPR_UNARY_PLUS:
480 left = expr->value.child;
481 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
485 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n", expr->op);
492 ExprResolveInteger(struct xkb_context *ctx, ExprDef *expr,
493 ExprResult *val_rtrn)
495 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
499 ExprResolveGroup(struct xkb_context *ctx, ExprDef *expr,
500 xkb_group_index_t *group_rtrn)
504 static const LookupEntry group_names[] = {
516 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
521 if (result.uval == 0 || result.uval > XkbNumKbdGroups) {
522 log_err(ctx, "Group index %u is out of range (1..%d)\n",
523 result.uval, XkbNumKbdGroups);
527 *group_rtrn = result.uval;
532 ExprResolveLevel(struct xkb_context *ctx, ExprDef *expr,
533 unsigned int *level_rtrn)
537 static const LookupEntry level_names[] = {
549 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
554 if (result.uval < 1 || result.uval > XkbMaxShiftLevel) {
555 log_err(ctx, "Shift level %d is out of range (1..%d)\n",
556 result.uval, XkbMaxShiftLevel);
560 *level_rtrn = result.uval;
565 ExprResolveButton(struct xkb_context *ctx, ExprDef *expr, int *btn_rtrn)
569 static const LookupEntry button_names[] = {
579 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
582 *btn_rtrn = result.ival;
587 ExprResolveString(struct xkb_context *ctx, ExprDef *expr,
588 const char **val_rtrn)
592 if (expr->value_type != EXPR_TYPE_STRING) {
593 log_err(ctx, "Found constant of type %s, expected a string\n",
594 exprValueTypeText(expr->value_type));
598 *val_rtrn = xkb_atom_text(ctx, expr->value.str);
602 log_err(ctx, "Identifier \"%s\" of type string not found\n",
603 xkb_atom_text(ctx, expr->value.str));
607 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
608 xkb_atom_text(ctx, expr->value.field.element),
609 xkb_atom_text(ctx, expr->value.field.field));
620 case EXPR_UNARY_PLUS:
621 log_err(ctx, "%s of strings not permitted\n", exprOpText(expr->op));
625 log_wsgo(ctx, "Unknown operator %d in ResolveString\n", expr->op);
632 ExprResolveKeyName(struct xkb_context *ctx, ExprDef *expr,
633 char name[XkbKeyNameLength])
637 if (expr->value_type != EXPR_TYPE_KEYNAME) {
638 log_err(ctx, "Found constant of type %s, expected a key name\n",
639 exprValueTypeText(expr->value_type));
642 memcpy(name, expr->value.keyName, XkbKeyNameLength);
646 log_err(ctx, "Identifier \"%s\" of type string not found\n",
647 xkb_atom_text(ctx, expr->value.str));
651 log_err(ctx, "Default \"%s.%s\" of type key name not found\n",
652 xkb_atom_text(ctx, expr->value.field.element),
653 xkb_atom_text(ctx, expr->value.field.field));
664 case EXPR_UNARY_PLUS:
665 log_err(ctx, "%s of key name values not permitted\n",
666 exprOpText(expr->op));
670 log_wsgo(ctx, "Unknown operator %d in ResolveKeyName\n", expr->op);
676 /***====================================================================***/
679 ExprResolveEnum(struct xkb_context *ctx, ExprDef *expr,
680 ExprResult *val_rtrn, const LookupEntry *values)
682 if (expr->op != EXPR_IDENT) {
683 log_err(ctx, "Found a %s where an enumerated value was expected\n",
684 exprOpText(expr->op));
687 if (!SimpleLookup(ctx, values, expr->value.str, EXPR_TYPE_INT,
690 log_err(ctx, "Illegal identifier %s (expected one of: ",
691 xkb_atom_text(ctx, expr->value.str));
692 while (values && values->name)
695 log_info(ctx, ", %s", values->name);
697 log_info(ctx, "%s", values->name);
701 log_info(ctx, ")\n");
708 ExprResolveMaskLookup(struct xkb_context *ctx, ExprDef *expr,
709 ExprResult *val_rtrn, IdentLookupFunc lookup,
710 const void *lookupPriv)
713 ExprResult leftRtrn, rightRtrn;
714 ExprDef *left, *right;
715 const char *bogus = NULL;
719 if (expr->value_type != EXPR_TYPE_INT) {
721 "Found constant of type %s where a mask was expected\n",
722 exprValueTypeText(expr->value_type));
725 val_rtrn->ival = expr->value.ival;
729 ok = lookup(ctx, lookupPriv, expr->value.str, EXPR_TYPE_INT, val_rtrn);
731 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
732 xkb_atom_text(ctx, expr->value.str));
736 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
737 xkb_atom_text(ctx, expr->value.field.element),
738 xkb_atom_text(ctx, expr->value.field.field));
742 bogus = "array reference";
744 case EXPR_ACTION_DECL:
746 bogus = "function use";
748 "Unexpected %s in mask expression; Expression Ignored\n",
756 left = expr->value.binary.left;
757 right = expr->value.binary.right;
758 if (ExprResolveMaskLookup(ctx, left, &leftRtrn, lookup,
760 ExprResolveMaskLookup(ctx, right, &rightRtrn, lookup,
764 val_rtrn->ival = leftRtrn.ival | rightRtrn.ival;
767 val_rtrn->ival = leftRtrn.ival & (~rightRtrn.ival);
771 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
772 (expr->op == EXPR_DIVIDE ? "divide" : "multiply"));
782 log_wsgo(ctx, "Assignment operator not implemented yet\n");
786 left = expr->value.child;
787 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
789 val_rtrn->ival = ~leftRtrn.ival;
794 case EXPR_UNARY_PLUS:
797 left = expr->value.child;
798 if (ExprResolveIntegerLookup(ctx, left, &leftRtrn, lookup,
800 log_err(ctx, "The %s operator cannot be used with a mask\n",
801 (expr->op == EXPR_NEGATE ? "-" : "!"));
806 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n", expr->op);
813 ExprResolveMask(struct xkb_context *ctx, ExprDef *expr,
814 ExprResult *val_rtrn, const LookupEntry *values)
816 return ExprResolveMaskLookup(ctx, expr, val_rtrn, SimpleLookup, values);
820 ExprResolveModMask(struct xkb_context *ctx, ExprDef *expr,
821 ExprResult *val_rtrn)
823 return ExprResolveMaskLookup(ctx, expr, val_rtrn, LookupModMask, NULL);
827 ExprResolveVModMask(struct xkb_keymap *keymap, ExprDef *expr,
828 ExprResult *val_rtrn)
830 return ExprResolveMaskLookup(keymap->ctx, expr, val_rtrn, LookupVModMask,
835 ExprResolveKeySym(struct xkb_context *ctx, ExprDef *expr,
836 xkb_keysym_t *sym_rtrn)
841 if (expr->op == EXPR_IDENT) {
843 str = xkb_atom_text(ctx, expr->value.str);
844 *sym_rtrn = xkb_keysym_from_name(str);
845 if (*sym_rtrn != XKB_KEY_NoSymbol)
849 ok = ExprResolveInteger(ctx, expr, &result);
850 if (ok && result.uval < 10)
851 *sym_rtrn = result.uval + '0';