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"
34 typedef bool (*IdentLookupFunc)(struct xkb_context *ctx, const void *priv,
35 xkb_atom_t field, enum expr_value_type type,
36 unsigned int *val_rtrn);
39 ExprResolveLhs(struct xkb_context *ctx, const ExprDef *expr,
40 const char **elem_rtrn, const char **field_rtrn,
43 switch (expr->expr.op) {
46 *field_rtrn = xkb_atom_text(ctx, expr->ident.ident);
48 return (*field_rtrn != NULL);
50 *elem_rtrn = xkb_atom_text(ctx, expr->field_ref.element);
51 *field_rtrn = xkb_atom_text(ctx, expr->field_ref.field);
53 return (*elem_rtrn != NULL && *field_rtrn != NULL);
55 *elem_rtrn = xkb_atom_text(ctx, expr->array_ref.element);
56 *field_rtrn = xkb_atom_text(ctx, expr->array_ref.field);
57 *index_rtrn = expr->array_ref.entry;
58 if (expr->array_ref.element != XKB_ATOM_NONE && *elem_rtrn == NULL)
60 if (*field_rtrn == NULL)
66 log_wsgo(ctx, "Unexpected operator %d in ResolveLhs\n", expr->expr.op);
71 SimpleLookup(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
72 enum expr_value_type type, unsigned int *val_rtrn)
74 const LookupEntry *entry;
77 if (!priv || field == XKB_ATOM_NONE || type != EXPR_TYPE_INT)
80 str = xkb_atom_text(ctx, field);
81 for (entry = priv; entry && entry->name; entry++) {
82 if (istreq(str, entry->name)) {
83 *val_rtrn = entry->value;
91 /* Data passed in the *priv argument for LookupModMask. */
93 const struct xkb_mod_set *mods;
94 enum mod_type mod_type;
98 LookupModMask(struct xkb_context *ctx, const void *priv, xkb_atom_t field,
99 enum expr_value_type type, xkb_mod_mask_t *val_rtrn)
103 const LookupModMaskPriv *arg = priv;
104 const struct xkb_mod_set *mods = arg->mods;
105 enum mod_type mod_type = arg->mod_type;
107 if (type != EXPR_TYPE_INT)
110 str = xkb_atom_text(ctx, field);
114 if (istreq(str, "all")) {
115 *val_rtrn = MOD_REAL_MASK_ALL;
119 if (istreq(str, "none")) {
124 ndx = XkbModNameToIndex(mods, field, mod_type);
125 if (ndx == XKB_MOD_INVALID)
128 *val_rtrn = (1u << ndx);
133 ExprResolveBoolean(struct xkb_context *ctx, const ExprDef *expr,
139 switch (expr->expr.op) {
141 if (expr->expr.value_type != EXPR_TYPE_BOOLEAN) {
143 "Found constant of type %s where boolean was expected\n",
144 expr_value_type_to_string(expr->expr.value_type));
147 *set_rtrn = expr->boolean.set;
151 ident = xkb_atom_text(ctx, expr->ident.ident);
153 if (istreq(ident, "true") ||
154 istreq(ident, "yes") ||
155 istreq(ident, "on")) {
159 else if (istreq(ident, "false") ||
160 istreq(ident, "no") ||
161 istreq(ident, "off")) {
166 log_err(ctx, "Identifier \"%s\" of type boolean is unknown\n", ident);
170 log_err(ctx, "Default \"%s.%s\" of type boolean is unknown\n",
171 xkb_atom_text(ctx, expr->field_ref.element),
172 xkb_atom_text(ctx, expr->field_ref.field));
177 ok = ExprResolveBoolean(ctx, expr->unary.child, set_rtrn);
179 *set_rtrn = !*set_rtrn;
187 case EXPR_UNARY_PLUS:
188 case EXPR_ACTION_DECL:
189 case EXPR_ACTION_LIST:
190 case EXPR_KEYSYM_LIST:
191 log_err(ctx, "%s of boolean values not permitted\n",
192 expr_op_type_to_string(expr->expr.op));
196 log_wsgo(ctx, "Unknown operator %d in ResolveBoolean\n",
205 ExprResolveKeyCode(struct xkb_context *ctx, const ExprDef *expr,
208 xkb_keycode_t leftRtrn, rightRtrn;
210 switch (expr->expr.op) {
212 if (expr->expr.value_type != EXPR_TYPE_INT) {
214 "Found constant of type %s where an int was expected\n",
215 expr_value_type_to_string(expr->expr.value_type));
219 *kc = (xkb_keycode_t) expr->integer.ival;
226 if (!ExprResolveKeyCode(ctx, expr->binary.left, &leftRtrn) ||
227 !ExprResolveKeyCode(ctx, expr->binary.right, &rightRtrn))
230 switch (expr->expr.op) {
232 *kc = leftRtrn + rightRtrn;
235 *kc = leftRtrn - rightRtrn;
238 *kc = leftRtrn * rightRtrn;
241 if (rightRtrn == 0) {
242 log_err(ctx, "Cannot divide by zero: %d / %d\n",
243 leftRtrn, rightRtrn);
247 *kc = leftRtrn / rightRtrn;
256 if (!ExprResolveKeyCode(ctx, expr->unary.child, &leftRtrn))
262 case EXPR_UNARY_PLUS:
263 return ExprResolveKeyCode(ctx, expr->unary.child, kc);
266 log_wsgo(ctx, "Unknown operator %d in ResolveKeyCode\n",
275 * This function returns ... something. It's a bit of a guess, really.
277 * If an integer is given in value ctx, it will be returned in ival.
278 * If an ident or field reference is given, the lookup function (if given)
279 * will be called. At the moment, only SimpleLookup use this, and they both
280 * return the results in uval. And don't support field references.
285 ExprResolveIntegerLookup(struct xkb_context *ctx, const ExprDef *expr,
286 int *val_rtrn, IdentLookupFunc lookup,
287 const void *lookupPriv)
292 ExprDef *left, *right;
294 switch (expr->expr.op) {
296 if (expr->expr.value_type != EXPR_TYPE_INT) {
298 "Found constant of type %s where an int was expected\n",
299 expr_value_type_to_string(expr->expr.value_type));
303 *val_rtrn = expr->integer.ival;
308 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT, &u);
311 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
312 xkb_atom_text(ctx, expr->ident.ident));
319 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
320 xkb_atom_text(ctx, expr->field_ref.element),
321 xkb_atom_text(ctx, expr->field_ref.field));
328 left = expr->binary.left;
329 right = expr->binary.right;
330 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv) ||
331 !ExprResolveIntegerLookup(ctx, right, &r, lookup, lookupPriv))
334 switch (expr->expr.op) {
346 log_err(ctx, "Cannot divide by zero: %d / %d\n", l, r);
352 log_err(ctx, "%s of integers not permitted\n",
353 expr_op_type_to_string(expr->expr.op));
360 log_wsgo(ctx, "Assignment operator not implemented yet\n");
364 log_err(ctx, "The ! operator cannot be applied to an integer\n");
369 left = expr->unary.child;
370 if (!ExprResolveIntegerLookup(ctx, left, &l, lookup, lookupPriv))
373 *val_rtrn = (expr->expr.op == EXPR_NEGATE ? -l : ~l);
376 case EXPR_UNARY_PLUS:
377 left = expr->unary.child;
378 return ExprResolveIntegerLookup(ctx, left, val_rtrn, lookup,
382 log_wsgo(ctx, "Unknown operator %d in ResolveInteger\n",
391 ExprResolveInteger(struct xkb_context *ctx, const ExprDef *expr,
394 return ExprResolveIntegerLookup(ctx, expr, val_rtrn, NULL, NULL);
398 ExprResolveGroup(struct xkb_context *ctx, const ExprDef *expr,
399 xkb_layout_index_t *group_rtrn)
404 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
409 if (result <= 0 || result > XKB_MAX_GROUPS) {
410 log_err_with_code(ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
411 "Group index %u is out of range (1..%d)\n",
412 result, XKB_MAX_GROUPS);
416 *group_rtrn = (xkb_layout_index_t) result;
421 ExprResolveLevel(struct xkb_context *ctx, const ExprDef *expr,
422 xkb_level_index_t *level_rtrn)
427 ok = ExprResolveIntegerLookup(ctx, expr, &result, SimpleLookup,
433 log_err_with_code(ctx, XKB_ERROR_UNSUPPORTED_SHIFT_LEVEL,
434 "Shift level %d is out of range\n", result);
438 /* Level is zero-indexed from now on. */
439 *level_rtrn = (unsigned int) (result - 1);
444 ExprResolveButton(struct xkb_context *ctx, const ExprDef *expr, int *btn_rtrn)
446 return ExprResolveIntegerLookup(ctx, expr, btn_rtrn, SimpleLookup,
451 ExprResolveString(struct xkb_context *ctx, const ExprDef *expr,
452 xkb_atom_t *val_rtrn)
454 switch (expr->expr.op) {
456 if (expr->expr.value_type != EXPR_TYPE_STRING) {
457 log_err(ctx, "Found constant of type %s, expected a string\n",
458 expr_value_type_to_string(expr->expr.value_type));
462 *val_rtrn = expr->string.str;
466 log_err(ctx, "Identifier \"%s\" of type string not found\n",
467 xkb_atom_text(ctx, expr->ident.ident));
471 log_err(ctx, "Default \"%s.%s\" of type string not found\n",
472 xkb_atom_text(ctx, expr->field_ref.element),
473 xkb_atom_text(ctx, expr->field_ref.field));
484 case EXPR_UNARY_PLUS:
485 case EXPR_ACTION_DECL:
486 case EXPR_ACTION_LIST:
487 case EXPR_KEYSYM_LIST:
488 log_err(ctx, "%s of strings not permitted\n",
489 expr_op_type_to_string(expr->expr.op));
493 log_wsgo(ctx, "Unknown operator %d in ResolveString\n",
501 ExprResolveEnum(struct xkb_context *ctx, const ExprDef *expr,
502 unsigned int *val_rtrn, const LookupEntry *values)
504 if (expr->expr.op != EXPR_IDENT) {
505 log_err(ctx, "Found a %s where an enumerated value was expected\n",
506 expr_op_type_to_string(expr->expr.op));
510 if (!SimpleLookup(ctx, values, expr->ident.ident, EXPR_TYPE_INT,
512 log_err(ctx, "Illegal identifier %s; expected one of:\n",
513 xkb_atom_text(ctx, expr->ident.ident));
514 while (values && values->name)
516 log_err(ctx, "\t%s\n", values->name);
526 ExprResolveMaskLookup(struct xkb_context *ctx, const ExprDef *expr,
527 unsigned int *val_rtrn, IdentLookupFunc lookup,
528 const void *lookupPriv)
531 unsigned int l = 0, r = 0;
533 ExprDef *left, *right;
534 const char *bogus = NULL;
536 switch (expr->expr.op) {
538 if (expr->expr.value_type != EXPR_TYPE_INT) {
540 "Found constant of type %s where a mask was expected\n",
541 expr_value_type_to_string(expr->expr.value_type));
544 *val_rtrn = (unsigned int) expr->integer.ival;
548 ok = lookup(ctx, lookupPriv, expr->ident.ident, EXPR_TYPE_INT,
551 log_err(ctx, "Identifier \"%s\" of type int is unknown\n",
552 xkb_atom_text(ctx, expr->ident.ident));
556 log_err(ctx, "Default \"%s.%s\" of type int is unknown\n",
557 xkb_atom_text(ctx, expr->field_ref.element),
558 xkb_atom_text(ctx, expr->field_ref.field));
562 bogus = "array reference";
564 case EXPR_ACTION_DECL:
566 bogus = "function use";
568 "Unexpected %s in mask expression; Expression Ignored\n",
576 left = expr->binary.left;
577 right = expr->binary.right;
578 if (!ExprResolveMaskLookup(ctx, left, &l, lookup, lookupPriv) ||
579 !ExprResolveMaskLookup(ctx, right, &r, lookup, lookupPriv))
582 switch (expr->expr.op) {
587 *val_rtrn = l & (~r);
591 log_err(ctx, "Cannot %s masks; Illegal operation ignored\n",
592 (expr->expr.op == EXPR_DIVIDE ? "divide" : "multiply"));
601 log_wsgo(ctx, "Assignment operator not implemented yet\n");
605 left = expr->unary.child;
606 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
612 case EXPR_UNARY_PLUS:
615 left = expr->unary.child;
616 if (!ExprResolveIntegerLookup(ctx, left, &v, lookup, lookupPriv))
617 log_err(ctx, "The %s operator cannot be used with a mask\n",
618 (expr->expr.op == EXPR_NEGATE ? "-" : "!"));
622 log_wsgo(ctx, "Unknown operator %d in ResolveMask\n",
631 ExprResolveMask(struct xkb_context *ctx, const ExprDef *expr,
632 unsigned int *mask_rtrn, const LookupEntry *values)
634 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, SimpleLookup, values);
638 ExprResolveModMask(struct xkb_context *ctx, const ExprDef *expr,
639 enum mod_type mod_type, const struct xkb_mod_set *mods,
640 xkb_mod_mask_t *mask_rtrn)
642 LookupModMaskPriv priv = { .mods = mods, .mod_type = mod_type };
643 return ExprResolveMaskLookup(ctx, expr, mask_rtrn, LookupModMask, &priv);
647 ExprResolveKeySym(struct xkb_context *ctx, const ExprDef *expr,
648 xkb_keysym_t *sym_rtrn)
652 if (expr->expr.op == EXPR_IDENT) {
653 const char *str = xkb_atom_text(ctx, expr->ident.ident);
654 *sym_rtrn = xkb_keysym_from_name(str, 0);
655 if (*sym_rtrn != XKB_KEY_NoSymbol)
659 if (!ExprResolveInteger(ctx, expr, &val))
662 if (val < XKB_KEYSYM_MIN) {
663 log_warn_with_code(ctx, XKB_WARNING_UNRECOGNIZED_KEYSYM,
664 "unrecognized keysym \"-0x%x\" (%d)\n",
665 (unsigned int) -val, val);
669 /* Special case for digits 0..9 */
671 *sym_rtrn = XKB_KEY_0 + (xkb_keysym_t) val;
675 if (val <= XKB_KEYSYM_MAX) {
676 log_warn_with_code(ctx, XKB_WARNING_NUMERIC_KEYSYM,
677 "numeric keysym \"0x%x\" (%d)",
678 (unsigned int) val, val);
679 *sym_rtrn = (xkb_keysym_t) val;
683 log_warn_with_code(ctx, XKB_WARNING_UNRECOGNIZED_KEYSYM,
684 "unrecognized keysym \"0x%x\" (%d)\n",
685 (unsigned int) val, val);
691 ExprResolveMod(struct xkb_context *ctx, const ExprDef *def,
692 enum mod_type mod_type, const struct xkb_mod_set *mods,
693 xkb_mod_index_t *ndx_rtrn)
698 if (def->expr.op != EXPR_IDENT) {
700 "Cannot resolve virtual modifier: "
701 "found %s where a virtual modifier name was expected\n",
702 expr_op_type_to_string(def->expr.op));
706 name = def->ident.ident;
707 ndx = XkbModNameToIndex(mods, name, mod_type);
708 if (ndx == XKB_MOD_INVALID) {
710 "Cannot resolve virtual modifier: "
711 "\"%s\" was not previously declared\n",
712 xkb_atom_text(ctx, name));