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"
33 static const ExprDef constTrue = {
34 .common = { .type = STMT_EXPR, .next = NULL },
36 .value_type = EXPR_TYPE_BOOLEAN,
37 .value = { .ival = 1 },
40 static const ExprDef constFalse = {
41 .common = { .type = STMT_EXPR, .next = NULL },
43 .value_type = EXPR_TYPE_BOOLEAN,
44 .value = { .ival = 0 },
48 ACTION_FIELD_CLEAR_LOCKS,
49 ACTION_FIELD_LATCH_TO_LOCK,
50 ACTION_FIELD_GEN_KEY_EVENT,
54 ACTION_FIELD_INCREMENT,
55 ACTION_FIELD_MODIFIERS,
62 ACTION_FIELD_CONTROLS,
70 ACTION_FIELD_MODS_TO_CLEAR,
79 info = calloc(1, sizeof(*info));
83 /* This includes PrivateAction. */
84 for (type = 0; type < ACTION_TYPE_LAST; type++)
85 info->actions[type].type = type;
87 /* Apply some "factory defaults". */
89 /* Increment default button. */
90 info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.flags = 0;
91 info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.value = 1;
97 FreeActionsInfo(ActionsInfo *info)
102 static const LookupEntry fieldStrings[] = {
103 { "clearLocks", ACTION_FIELD_CLEAR_LOCKS },
104 { "latchToLock", ACTION_FIELD_LATCH_TO_LOCK },
105 { "genKeyEvent", ACTION_FIELD_GEN_KEY_EVENT },
106 { "generateKeyEvent", ACTION_FIELD_GEN_KEY_EVENT },
107 { "report", ACTION_FIELD_REPORT },
108 { "default", ACTION_FIELD_DEFAULT },
109 { "affect", ACTION_FIELD_AFFECT },
110 { "increment", ACTION_FIELD_INCREMENT },
111 { "modifiers", ACTION_FIELD_MODIFIERS },
112 { "mods", ACTION_FIELD_MODIFIERS },
113 { "group", ACTION_FIELD_GROUP },
114 { "x", ACTION_FIELD_X },
115 { "y", ACTION_FIELD_Y },
116 { "accel", ACTION_FIELD_ACCEL },
117 { "accelerate", ACTION_FIELD_ACCEL },
118 { "repeat", ACTION_FIELD_ACCEL },
119 { "button", ACTION_FIELD_BUTTON },
120 { "value", ACTION_FIELD_VALUE },
121 { "controls", ACTION_FIELD_CONTROLS },
122 { "ctrls", ACTION_FIELD_CONTROLS },
123 { "type", ACTION_FIELD_TYPE },
124 { "count", ACTION_FIELD_COUNT },
125 { "screen", ACTION_FIELD_SCREEN },
126 { "same", ACTION_FIELD_SAME },
127 { "sameServer", ACTION_FIELD_SAME },
128 { "data", ACTION_FIELD_DATA },
129 { "device", ACTION_FIELD_DEVICE },
130 { "dev", ACTION_FIELD_DEVICE },
131 { "key", ACTION_FIELD_KEYCODE },
132 { "keycode", ACTION_FIELD_KEYCODE },
133 { "kc", ACTION_FIELD_KEYCODE },
134 { "clearmods", ACTION_FIELD_MODS_TO_CLEAR },
135 { "clearmodifiers", ACTION_FIELD_MODS_TO_CLEAR },
140 stringToAction(const char *str, unsigned *type_rtrn)
142 return LookupString(actionTypeNames, str, type_rtrn);
146 stringToField(const char *str, enum action_field *field_rtrn)
148 return LookupString(fieldStrings, str, field_rtrn);
152 fieldText(enum action_field field)
154 return LookupValue(fieldStrings, field);
157 /***====================================================================***/
160 ReportMismatch(struct xkb_keymap *keymap, enum xkb_action_type action,
161 enum action_field field, const char *type)
164 "Value of %s field must be of type %s; "
165 "Action %s definition ignored\n",
166 fieldText(field), type, ActionTypeText(action));
171 ReportIllegal(struct xkb_keymap *keymap, enum xkb_action_type action,
172 enum action_field field)
175 "Field %s is not defined for an action of type %s; "
176 "Action definition ignored\n",
177 fieldText(field), ActionTypeText(action));
182 ReportActionNotArray(struct xkb_keymap *keymap, enum xkb_action_type action,
183 enum action_field field)
186 "The %s field in the %s action is not an array; "
187 "Action definition ignored\n",
188 fieldText(field), ActionTypeText(action));
193 ReportNotFound(struct xkb_keymap *keymap, enum xkb_action_type action,
194 enum action_field field, const char *what, const char *bad)
197 "%s named %s not found; "
198 "Ignoring the %s field of an %s action\n",
199 what, bad, fieldText(field), ActionTypeText(action));
204 HandleNoAction(struct xkb_keymap *keymap, union xkb_action *action,
205 enum action_field field, const ExprDef *array_ndx,
206 const ExprDef *value)
213 CheckLatchLockFlags(struct xkb_keymap *keymap, enum xkb_action_type action,
214 enum action_field field, const ExprDef * value,
215 enum xkb_action_flags *flags_inout)
217 enum xkb_action_flags tmp;
220 if (field == ACTION_FIELD_CLEAR_LOCKS)
221 tmp = ACTION_LOCK_CLEAR;
222 else if (field == ACTION_FIELD_LATCH_TO_LOCK)
223 tmp = ACTION_LATCH_TO_LOCK;
225 return false; /* WSGO! */
227 if (!ExprResolveBoolean(keymap->ctx, value, &result))
228 return ReportMismatch(keymap, action, field, "boolean");
233 *flags_inout &= ~tmp;
239 CheckModifierField(struct xkb_keymap *keymap, enum xkb_action_type action,
240 const ExprDef *value, enum xkb_action_flags *flags_inout,
241 xkb_mod_mask_t *mods_rtrn)
243 if (value->op == EXPR_IDENT) {
245 valStr = xkb_atom_text(keymap->ctx, value->value.str);
246 if (valStr && (istreq(valStr, "usemodmapmods") ||
247 istreq(valStr, "modmapmods"))) {
250 *flags_inout |= ACTION_MODS_LOOKUP_MODMAP;
255 if (!ExprResolveVModMask(keymap, value, mods_rtrn))
256 return ReportMismatch(keymap, action,
257 ACTION_FIELD_MODIFIERS, "modifier mask");
259 *flags_inout &= ~ACTION_MODS_LOOKUP_MODMAP;
264 HandleSetLatchMods(struct xkb_keymap *keymap, union xkb_action *action,
265 enum action_field field, const ExprDef *array_ndx,
266 const ExprDef *value)
268 struct xkb_mod_action *act = &action->mods;
269 enum xkb_action_flags rtrn, t1;
272 if (array_ndx != NULL) {
274 case ACTION_FIELD_CLEAR_LOCKS:
275 case ACTION_FIELD_LATCH_TO_LOCK:
276 case ACTION_FIELD_MODIFIERS:
277 return ReportActionNotArray(keymap, action->type, field);
284 case ACTION_FIELD_CLEAR_LOCKS:
285 case ACTION_FIELD_LATCH_TO_LOCK:
287 if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
293 case ACTION_FIELD_MODIFIERS:
295 if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
306 return ReportIllegal(keymap, action->type, field);
310 HandleLockMods(struct xkb_keymap *keymap, union xkb_action *action,
311 enum action_field field, const ExprDef *array_ndx,
312 const ExprDef *value)
314 struct xkb_mod_action *act = &action->mods;
315 enum xkb_action_flags t1;
318 if (array_ndx && field == ACTION_FIELD_MODIFIERS)
319 return ReportActionNotArray(keymap, action->type, field);
322 case ACTION_FIELD_MODIFIERS:
324 if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
335 return ReportIllegal(keymap, action->type, field);
339 CheckGroupField(struct xkb_keymap *keymap, unsigned action,
340 const ExprDef *value, enum xkb_action_flags *flags_inout,
341 xkb_group_index_t *grp_rtrn)
345 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
346 *flags_inout &= ~ACTION_ABSOLUTE_SWITCH;
347 spec = value->value.child;
350 *flags_inout |= ACTION_ABSOLUTE_SWITCH;
354 if (!ExprResolveGroup(keymap->ctx, spec, grp_rtrn))
355 return ReportMismatch(keymap, action, ACTION_FIELD_GROUP,
356 "integer (range 1..8)");
358 if (value->op == EXPR_NEGATE)
359 *grp_rtrn = -*grp_rtrn;
360 else if (value->op != EXPR_UNARY_PLUS)
367 HandleSetLatchGroup(struct xkb_keymap *keymap, union xkb_action *action,
368 enum action_field field, const ExprDef *array_ndx,
369 const ExprDef *value)
371 struct xkb_group_action *act = &action->group;
372 enum xkb_action_flags rtrn, t1;
373 xkb_group_index_t t2;
375 if (array_ndx != NULL) {
377 case ACTION_FIELD_CLEAR_LOCKS:
378 case ACTION_FIELD_LATCH_TO_LOCK:
379 case ACTION_FIELD_GROUP:
380 return ReportActionNotArray(keymap, action->type, field);
388 case ACTION_FIELD_CLEAR_LOCKS:
389 case ACTION_FIELD_LATCH_TO_LOCK:
391 if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
397 case ACTION_FIELD_GROUP:
399 if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
410 return ReportIllegal(keymap, action->type, field);
414 HandleLockGroup(struct xkb_keymap *keymap, union xkb_action *action,
415 enum action_field field, const ExprDef *array_ndx,
416 const ExprDef *value)
418 struct xkb_group_action *act = &action->group;
419 enum xkb_action_flags t1;
420 xkb_group_index_t t2;
422 if ((array_ndx != NULL) && (field == ACTION_FIELD_GROUP))
423 return ReportActionNotArray(keymap, action->type, field);
424 if (field == ACTION_FIELD_GROUP) {
426 if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
433 return ReportIllegal(keymap, action->type, field);
437 HandleMovePtr(struct xkb_keymap *keymap, union xkb_action *action,
438 enum action_field field, const ExprDef *array_ndx,
439 const ExprDef *value)
441 struct xkb_pointer_action *act = &action->ptr;
444 if (array_ndx && (field == ACTION_FIELD_X || field == ACTION_FIELD_Y))
445 return ReportActionNotArray(keymap, action->type, field);
447 if (field == ACTION_FIELD_X || field == ACTION_FIELD_Y) {
450 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS)
455 if (!ExprResolveInteger(keymap->ctx, value, &val))
456 return ReportMismatch(keymap, action->type, field, "integer");
458 if (field == ACTION_FIELD_X) {
460 act->flags |= ACTION_ABSOLUTE_X;
465 act->flags |= ACTION_ABSOLUTE_Y;
471 else if (field == ACTION_FIELD_ACCEL) {
474 if (!ExprResolveBoolean(keymap->ctx, value, &set))
475 return ReportMismatch(keymap, action->type, field, "boolean");
478 act->flags &= ~ACTION_NO_ACCEL;
480 act->flags |= ACTION_NO_ACCEL;
483 return ReportIllegal(keymap, action->type, field);
486 static const LookupEntry lockWhich[] = {
488 { "lock", ACTION_LOCK_NO_UNLOCK },
489 { "neither", (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK) },
490 { "unlock", ACTION_LOCK_NO_LOCK },
495 HandlePtrBtn(struct xkb_keymap *keymap, union xkb_action *action,
496 enum action_field field, const ExprDef *array_ndx,
497 const ExprDef *value)
499 struct xkb_pointer_button_action *act = &action->btn;
501 if (field == ACTION_FIELD_BUTTON) {
505 return ReportActionNotArray(keymap, action->type, field);
507 if (!ExprResolveButton(keymap->ctx, value, &btn))
508 return ReportMismatch(keymap, action->type, field,
509 "integer (range 1..5)");
511 if (btn < 0 || btn > 5) {
513 "Button must specify default or be in the range 1..5; "
514 "Illegal button value %d ignored\n", btn);
521 else if (action->type == ACTION_TYPE_PTR_LOCK &&
522 field == ACTION_FIELD_AFFECT) {
523 enum xkb_action_flags val;
526 return ReportActionNotArray(keymap, action->type, field);
528 if (!ExprResolveEnum(keymap->ctx, value, &val, lockWhich))
529 return ReportMismatch(keymap, action->type, field,
532 act->flags &= ~(ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK);
536 else if (field == ACTION_FIELD_COUNT) {
540 return ReportActionNotArray(keymap, action->type, field);
542 /* XXX: Should this actually be ResolveButton? */
543 if (!ExprResolveButton(keymap->ctx, value, &btn))
544 return ReportMismatch(keymap, action->type, field, "integer");
546 if (btn < 0 || btn > 255) {
548 "The count field must have a value in the range 0..255; "
549 "Illegal count %d ignored\n", btn);
556 return ReportIllegal(keymap, action->type, field);
559 static const LookupEntry ptrDflts[] = {
561 { "defaultbutton", 1 },
567 HandleSetPtrDflt(struct xkb_keymap *keymap, union xkb_action *action,
568 enum action_field field, const ExprDef *array_ndx,
569 const ExprDef *value)
571 struct xkb_pointer_default_action *act = &action->dflt;
573 if (field == ACTION_FIELD_AFFECT) {
577 return ReportActionNotArray(keymap, action->type, field);
579 if (!ExprResolveEnum(keymap->ctx, value, &val, ptrDflts))
580 return ReportMismatch(keymap, action->type, field,
581 "pointer component");
584 else if (field == ACTION_FIELD_BUTTON || field == ACTION_FIELD_VALUE) {
585 const ExprDef *button;
589 return ReportActionNotArray(keymap, action->type, field);
591 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
592 act->flags &= ~ACTION_ABSOLUTE_SWITCH;
593 button = value->value.child;
596 act->flags |= ACTION_ABSOLUTE_SWITCH;
600 if (!ExprResolveButton(keymap->ctx, button, &btn))
601 return ReportMismatch(keymap, action->type, field,
602 "integer (range 1..5)");
604 if (btn < 0 || btn > 5) {
606 "New default button value must be in the range 1..5; "
607 "Illegal default button value %d ignored\n", btn);
612 "Cannot set default pointer button to \"default\"; "
613 "Illegal default button setting ignored\n");
617 act->value = (value->op == EXPR_NEGATE ? -btn: btn);
621 return ReportIllegal(keymap, action->type, field);
625 HandleSwitchScreen(struct xkb_keymap *keymap, union xkb_action *action,
626 enum action_field field, const ExprDef *array_ndx,
627 const ExprDef *value)
629 struct xkb_switch_screen_action *act = &action->screen;
631 if (field == ACTION_FIELD_SCREEN) {
636 return ReportActionNotArray(keymap, action->type, field);
638 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
639 act->flags &= ~ACTION_ABSOLUTE_SWITCH;
640 scrn = value->value.child;
643 act->flags |= ACTION_ABSOLUTE_SWITCH;
647 if (!ExprResolveInteger(keymap->ctx, scrn, &val))
648 return ReportMismatch(keymap, action->type, field,
651 if (val < 0 || val > 255) {
653 "Screen index must be in the range 1..255; "
654 "Illegal screen value %d ignored\n", val);
658 act->screen = (value->op == EXPR_NEGATE ? -val : val);
661 else if (field == ACTION_FIELD_SAME) {
665 return ReportActionNotArray(keymap, action->type, field);
667 if (!ExprResolveBoolean(keymap->ctx, value, &set))
668 return ReportMismatch(keymap, action->type, field, "boolean");
671 act->flags &= ~ACTION_SAME_SCREEN;
673 act->flags |= ACTION_SAME_SCREEN;
678 return ReportIllegal(keymap, action->type, field);
682 HandleSetLockControls(struct xkb_keymap *keymap, union xkb_action *action,
683 enum action_field field, const ExprDef *array_ndx,
684 const ExprDef *value)
686 struct xkb_controls_action *act = &action->ctrls;
688 if (field == ACTION_FIELD_CONTROLS) {
692 return ReportActionNotArray(keymap, action->type, field);
694 if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlMaskNames))
695 return ReportMismatch(keymap, action->type, field,
702 return ReportIllegal(keymap, action->type, field);
706 HandlePrivate(struct xkb_keymap *keymap, union xkb_action *action,
707 enum action_field field, const ExprDef *array_ndx,
708 const ExprDef *value)
710 struct xkb_private_action *act = &action->priv;
712 if (field == ACTION_FIELD_TYPE) {
715 if (!ExprResolveInteger(keymap->ctx, value, &type))
716 return ReportMismatch(keymap, ACTION_TYPE_PRIVATE, field, "integer");
718 if (type < 0 || type > 255) {
720 "Private action type must be in the range 0..255; "
721 "Illegal type %d ignored\n", type);
725 act->type = (uint8_t) type;
728 else if (field == ACTION_FIELD_DATA) {
729 if (array_ndx == NULL) {
734 if (!ExprResolveString(keymap->ctx, value, &val))
735 return ReportMismatch(keymap, action->type, field, "string");
737 str = xkb_atom_text(keymap->ctx, val);
739 if (len < 1 || len > 7) {
740 log_warn(keymap->ctx,
741 "A private action has 7 data bytes; "
742 "Extra %d bytes ignored\n", len - 6);
746 strncpy((char *) act->data, str, sizeof(act->data));
752 if (!ExprResolveInteger(keymap->ctx, array_ndx, &ndx)) {
754 "Array subscript must be integer; "
755 "Illegal subscript ignored\n");
759 if (ndx < 0 || ndx >= sizeof(act->data)) {
761 "The data for a private action is %zu bytes long; "
762 "Attempt to use data[%d] ignored\n",
763 sizeof(act->data), ndx);
767 if (!ExprResolveInteger(keymap->ctx, value, &datum))
768 return ReportMismatch(keymap, act->type, field, "integer");
770 if (datum < 0 || datum > 255) {
772 "All data for a private action must be 0..255; "
773 "Illegal datum %d ignored\n", datum);
777 act->data[ndx] = (uint8_t) datum;
782 return ReportIllegal(keymap, ACTION_TYPE_NONE, field);
785 typedef bool (*actionHandler)(struct xkb_keymap *keymap,
786 union xkb_action *action,
787 enum action_field field,
788 const ExprDef *array_ndx,
789 const ExprDef *value);
791 static const actionHandler handleAction[ACTION_TYPE_LAST] = {
792 [ACTION_TYPE_NONE] = HandleNoAction,
793 [ACTION_TYPE_MOD_SET] = HandleSetLatchMods,
794 [ACTION_TYPE_MOD_LATCH] = HandleSetLatchMods,
795 [ACTION_TYPE_MOD_LOCK] = HandleLockMods,
796 [ACTION_TYPE_GROUP_SET] = HandleSetLatchGroup,
797 [ACTION_TYPE_GROUP_LATCH] = HandleSetLatchGroup,
798 [ACTION_TYPE_GROUP_LOCK] = HandleLockGroup,
799 [ACTION_TYPE_PTR_MOVE] = HandleMovePtr,
800 [ACTION_TYPE_PTR_BUTTON] = HandlePtrBtn,
801 [ACTION_TYPE_PTR_LOCK] = HandlePtrBtn,
802 [ACTION_TYPE_PTR_DEFAULT] = HandleSetPtrDflt,
803 [ACTION_TYPE_TERMINATE] = HandleNoAction,
804 [ACTION_TYPE_SWITCH_VT] = HandleSwitchScreen,
805 [ACTION_TYPE_CTRL_SET] = HandleSetLockControls,
806 [ACTION_TYPE_CTRL_LOCK] = HandleSetLockControls,
807 [ACTION_TYPE_PRIVATE] = HandlePrivate,
810 /***====================================================================***/
813 HandleActionDef(ExprDef *def, struct xkb_keymap *keymap,
814 union xkb_action *action, ActionsInfo *info)
820 if (def->op != EXPR_ACTION_DECL) {
821 log_err(keymap->ctx, "Expected an action definition, found %s\n",
822 expr_op_type_to_string(def->op));
826 str = xkb_atom_text(keymap->ctx, def->value.action.name);
827 if (!stringToAction(str, &hndlrType)) {
828 log_err(keymap->ctx, "Unknown action %s\n", str);
833 * Get the default values for this action type, as modified by
834 * statements such as:
835 * latchMods.clearLocks = True;
837 *action = info->actions[hndlrType];
840 * Now change the action properties as specified for this
841 * particular instance, e.g. "modifiers" and "clearLocks" in:
842 * SetMods(modifiers=Alt,clearLocks);
844 for (arg = def->value.action.args; arg != NULL;
845 arg = (ExprDef *) arg->common.next) {
846 const ExprDef *value;
847 ExprDef *field, *arrayRtrn;
848 const char *elemRtrn, *fieldRtrn;
849 enum action_field fieldNdx;
851 if (arg->op == EXPR_ASSIGN) {
852 field = arg->value.binary.left;
853 value = arg->value.binary.right;
855 else if (arg->op == EXPR_NOT || arg->op == EXPR_INVERT) {
856 field = arg->value.child;
864 if (!ExprResolveLhs(keymap->ctx, field, &elemRtrn, &fieldRtrn,
870 "Cannot change defaults in an action definition; "
871 "Ignoring attempt to change %s.%s\n",
872 elemRtrn, fieldRtrn);
876 if (!stringToField(fieldRtrn, &fieldNdx)) {
877 log_err(keymap->ctx, "Unknown field name %s\n", fieldRtrn);
881 if (!handleAction[hndlrType](keymap, action, fieldNdx, arrayRtrn,
891 SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field,
892 ExprDef *array_ndx, ExprDef *value, ActionsInfo *info)
895 enum action_field action_field;
897 if (!stringToAction(elem, &action))
900 if (!stringToField(field, &action_field)) {
901 log_err(keymap->ctx, "\"%s\" is not a legal field name\n", field);
905 return handleAction[action](keymap, &info->actions[action],
906 action_field, array_ndx, value);