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.affect = XkbSA_AffectDfltBtn;
91 info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.flags = 0;
92 info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.value = 1;
98 FreeActionsInfo(ActionsInfo *info)
103 static const LookupEntry fieldStrings[] = {
104 { "clearLocks", ACTION_FIELD_CLEAR_LOCKS },
105 { "latchToLock", ACTION_FIELD_LATCH_TO_LOCK },
106 { "genKeyEvent", ACTION_FIELD_GEN_KEY_EVENT },
107 { "generateKeyEvent", ACTION_FIELD_GEN_KEY_EVENT },
108 { "report", ACTION_FIELD_REPORT },
109 { "default", ACTION_FIELD_DEFAULT },
110 { "affect", ACTION_FIELD_AFFECT },
111 { "increment", ACTION_FIELD_INCREMENT },
112 { "modifiers", ACTION_FIELD_MODIFIERS },
113 { "mods", ACTION_FIELD_MODIFIERS },
114 { "group", ACTION_FIELD_GROUP },
115 { "x", ACTION_FIELD_X },
116 { "y", ACTION_FIELD_Y },
117 { "accel", ACTION_FIELD_ACCEL },
118 { "accelerate", ACTION_FIELD_ACCEL },
119 { "repeat", ACTION_FIELD_ACCEL },
120 { "button", ACTION_FIELD_BUTTON },
121 { "value", ACTION_FIELD_VALUE },
122 { "controls", ACTION_FIELD_CONTROLS },
123 { "ctrls", ACTION_FIELD_CONTROLS },
124 { "type", ACTION_FIELD_TYPE },
125 { "count", ACTION_FIELD_COUNT },
126 { "screen", ACTION_FIELD_SCREEN },
127 { "same", ACTION_FIELD_SAME },
128 { "sameServer", ACTION_FIELD_SAME },
129 { "data", ACTION_FIELD_DATA },
130 { "device", ACTION_FIELD_DEVICE },
131 { "dev", ACTION_FIELD_DEVICE },
132 { "key", ACTION_FIELD_KEYCODE },
133 { "keycode", ACTION_FIELD_KEYCODE },
134 { "kc", ACTION_FIELD_KEYCODE },
135 { "clearmods", ACTION_FIELD_MODS_TO_CLEAR },
136 { "clearmodifiers", ACTION_FIELD_MODS_TO_CLEAR },
141 stringToAction(const char *str, unsigned *type_rtrn)
143 return LookupString(actionTypeNames, str, type_rtrn);
147 stringToField(const char *str, enum action_field *field_rtrn)
149 return LookupString(fieldStrings, str, field_rtrn);
153 fieldText(enum action_field field)
155 return LookupValue(fieldStrings, field);
158 /***====================================================================***/
161 ReportMismatch(struct xkb_keymap *keymap, enum xkb_action_type action,
162 enum action_field field, const char *type)
165 "Value of %s field must be of type %s; "
166 "Action %s definition ignored\n",
167 fieldText(field), type, ActionTypeText(action));
172 ReportIllegal(struct xkb_keymap *keymap, enum xkb_action_type action,
173 enum action_field field)
176 "Field %s is not defined for an action of type %s; "
177 "Action definition ignored\n",
178 fieldText(field), ActionTypeText(action));
183 ReportActionNotArray(struct xkb_keymap *keymap, enum xkb_action_type action,
184 enum action_field field)
187 "The %s field in the %s action is not an array; "
188 "Action definition ignored\n",
189 fieldText(field), ActionTypeText(action));
194 ReportNotFound(struct xkb_keymap *keymap, enum xkb_action_type action,
195 enum action_field field, const char *what, const char *bad)
198 "%s named %s not found; "
199 "Ignoring the %s field of an %s action\n",
200 what, bad, fieldText(field), ActionTypeText(action));
205 HandleNoAction(struct xkb_keymap *keymap, union xkb_action *action,
206 enum action_field field, const ExprDef *array_ndx,
207 const ExprDef *value)
214 CheckLatchLockFlags(struct xkb_keymap *keymap, enum xkb_action_type action,
215 enum action_field field, const ExprDef * value,
216 unsigned *flags_inout)
221 if (field == ACTION_FIELD_CLEAR_LOCKS)
222 tmp = XkbSA_ClearLocks;
223 else if (field == ACTION_FIELD_LATCH_TO_LOCK)
224 tmp = XkbSA_LatchToLock;
226 return false; /* WSGO! */
228 if (!ExprResolveBoolean(keymap->ctx, value, &result))
229 return ReportMismatch(keymap, action, field, "boolean");
234 *flags_inout &= ~tmp;
240 CheckModifierField(struct xkb_keymap *keymap, enum xkb_action_type action,
241 const ExprDef *value, unsigned *flags_inout,
242 xkb_mod_mask_t *mods_rtrn)
244 if (value->op == EXPR_IDENT) {
246 valStr = xkb_atom_text(keymap->ctx, value->value.str);
247 if (valStr && (istreq(valStr, "usemodmapmods") ||
248 istreq(valStr, "modmapmods"))) {
251 *flags_inout |= XkbSA_UseModMapMods;
256 if (!ExprResolveVModMask(keymap, value, mods_rtrn))
257 return ReportMismatch(keymap, action,
258 ACTION_FIELD_MODIFIERS, "modifier mask");
260 *flags_inout &= ~XkbSA_UseModMapMods;
265 HandleSetLatchMods(struct xkb_keymap *keymap, union xkb_action *action,
266 enum action_field field, const ExprDef *array_ndx,
267 const ExprDef *value)
269 struct xkb_mod_action *act = &action->mods;
274 if (array_ndx != NULL) {
276 case ACTION_FIELD_CLEAR_LOCKS:
277 case ACTION_FIELD_LATCH_TO_LOCK:
278 case ACTION_FIELD_MODIFIERS:
279 return ReportActionNotArray(keymap, action->type, field);
286 case ACTION_FIELD_CLEAR_LOCKS:
287 case ACTION_FIELD_LATCH_TO_LOCK:
289 if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
295 case ACTION_FIELD_MODIFIERS:
297 if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
308 return ReportIllegal(keymap, action->type, field);
312 HandleLockMods(struct xkb_keymap *keymap, union xkb_action *action,
313 enum action_field field, const ExprDef *array_ndx,
314 const ExprDef *value)
316 struct xkb_mod_action *act = &action->mods;
320 if (array_ndx && field == ACTION_FIELD_MODIFIERS)
321 return ReportActionNotArray(keymap, action->type, field);
324 case ACTION_FIELD_MODIFIERS:
326 if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
337 return ReportIllegal(keymap, action->type, field);
341 CheckGroupField(struct xkb_keymap *keymap, unsigned action,
342 const ExprDef *value, unsigned *flags_inout,
343 xkb_group_index_t *grp_rtrn)
347 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
348 *flags_inout &= ~XkbSA_GroupAbsolute;
349 spec = value->value.child;
352 *flags_inout |= XkbSA_GroupAbsolute;
356 if (!ExprResolveGroup(keymap->ctx, spec, grp_rtrn))
357 return ReportMismatch(keymap, action, ACTION_FIELD_GROUP,
358 "integer (range 1..8)");
360 if (value->op == EXPR_NEGATE)
361 *grp_rtrn = -*grp_rtrn;
362 else if (value->op != EXPR_UNARY_PLUS)
369 HandleSetLatchGroup(struct xkb_keymap *keymap, union xkb_action *action,
370 enum action_field field, const ExprDef *array_ndx,
371 const ExprDef *value)
373 struct xkb_group_action *act = &action->group;
376 xkb_group_index_t t2;
378 if (array_ndx != NULL) {
380 case ACTION_FIELD_CLEAR_LOCKS:
381 case ACTION_FIELD_LATCH_TO_LOCK:
382 case ACTION_FIELD_GROUP:
383 return ReportActionNotArray(keymap, action->type, field);
391 case ACTION_FIELD_CLEAR_LOCKS:
392 case ACTION_FIELD_LATCH_TO_LOCK:
394 if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
400 case ACTION_FIELD_GROUP:
402 if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
413 return ReportIllegal(keymap, action->type, field);
417 HandleLockGroup(struct xkb_keymap *keymap, union xkb_action *action,
418 enum action_field field, const ExprDef *array_ndx,
419 const ExprDef *value)
421 struct xkb_group_action *act = &action->group;
423 xkb_group_index_t t2;
425 if ((array_ndx != NULL) && (field == ACTION_FIELD_GROUP))
426 return ReportActionNotArray(keymap, action->type, field);
427 if (field == ACTION_FIELD_GROUP) {
429 if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
436 return ReportIllegal(keymap, action->type, field);
440 HandleMovePtr(struct xkb_keymap *keymap, union xkb_action *action,
441 enum action_field field, const ExprDef *array_ndx,
442 const ExprDef *value)
444 struct xkb_pointer_action *act = &action->ptr;
447 if (array_ndx && (field == ACTION_FIELD_X || field == ACTION_FIELD_Y))
448 return ReportActionNotArray(keymap, action->type, field);
450 if (field == ACTION_FIELD_X || field == ACTION_FIELD_Y) {
453 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS)
458 if (!ExprResolveInteger(keymap->ctx, value, &val))
459 return ReportMismatch(keymap, action->type, field, "integer");
461 if (field == ACTION_FIELD_X) {
463 act->flags |= XkbSA_MoveAbsoluteX;
468 act->flags |= XkbSA_MoveAbsoluteY;
474 else if (field == ACTION_FIELD_ACCEL) {
477 if (!ExprResolveBoolean(keymap->ctx, value, &set))
478 return ReportMismatch(keymap, action->type, field, "boolean");
481 act->flags &= ~XkbSA_NoAcceleration;
483 act->flags |= XkbSA_NoAcceleration;
486 return ReportIllegal(keymap, action->type, field);
489 static const LookupEntry lockWhich[] = {
491 { "lock", XkbSA_LockNoUnlock },
492 { "neither", (XkbSA_LockNoLock | XkbSA_LockNoUnlock) },
493 { "unlock", XkbSA_LockNoLock },
498 HandlePtrBtn(struct xkb_keymap *keymap, union xkb_action *action,
499 enum action_field field, const ExprDef *array_ndx,
500 const ExprDef *value)
502 struct xkb_pointer_button_action *act = &action->btn;
504 if (field == ACTION_FIELD_BUTTON) {
508 return ReportActionNotArray(keymap, action->type, field);
510 if (!ExprResolveButton(keymap->ctx, value, &btn))
511 return ReportMismatch(keymap, action->type, field,
512 "integer (range 1..5)");
514 if (btn < 0 || btn > 5) {
516 "Button must specify default or be in the range 1..5; "
517 "Illegal button value %d ignored\n", btn);
524 else if (action->type == ACTION_TYPE_PTR_LOCK &&
525 field == ACTION_FIELD_AFFECT) {
529 return ReportActionNotArray(keymap, action->type, field);
531 if (!ExprResolveEnum(keymap->ctx, value, &val, lockWhich))
532 return ReportMismatch(keymap, action->type, field,
535 act->flags &= ~(XkbSA_LockNoLock | XkbSA_LockNoUnlock);
539 else if (field == ACTION_FIELD_COUNT) {
543 return ReportActionNotArray(keymap, action->type, field);
545 /* XXX: Should this actually be ResolveButton? */
546 if (!ExprResolveButton(keymap->ctx, value, &btn))
547 return ReportMismatch(keymap, action->type, field, "integer");
549 if (btn < 0 || btn > 255) {
551 "The count field must have a value in the range 0..255; "
552 "Illegal count %d ignored\n", btn);
559 return ReportIllegal(keymap, action->type, field);
562 static const LookupEntry ptrDflts[] = {
563 { "dfltbtn", XkbSA_AffectDfltBtn },
564 { "defaultbutton", XkbSA_AffectDfltBtn },
565 { "button", XkbSA_AffectDfltBtn },
570 HandleSetPtrDflt(struct xkb_keymap *keymap, union xkb_action *action,
571 enum action_field field, const ExprDef *array_ndx,
572 const ExprDef *value)
574 struct xkb_pointer_default_action *act = &action->dflt;
576 if (field == ACTION_FIELD_AFFECT) {
580 return ReportActionNotArray(keymap, action->type, field);
582 if (!ExprResolveEnum(keymap->ctx, value, &val, ptrDflts))
583 return ReportMismatch(keymap, action->type, field,
584 "pointer component");
588 else if (field == ACTION_FIELD_BUTTON || field == ACTION_FIELD_VALUE) {
589 const ExprDef *button;
593 return ReportActionNotArray(keymap, action->type, field);
595 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
596 act->flags &= ~XkbSA_DfltBtnAbsolute;
597 button = value->value.child;
600 act->flags |= XkbSA_DfltBtnAbsolute;
604 if (!ExprResolveButton(keymap->ctx, button, &btn))
605 return ReportMismatch(keymap, action->type, field,
606 "integer (range 1..5)");
608 if (btn < 0 || btn > 5) {
610 "New default button value must be in the range 1..5; "
611 "Illegal default button value %d ignored\n", btn);
616 "Cannot set default pointer button to \"default\"; "
617 "Illegal default button setting ignored\n");
621 act->value = (value->op == EXPR_NEGATE ? -btn: btn);
625 return ReportIllegal(keymap, action->type, field);
629 HandleSwitchScreen(struct xkb_keymap *keymap, union xkb_action *action,
630 enum action_field field, const ExprDef *array_ndx,
631 const ExprDef *value)
633 struct xkb_switch_screen_action *act = &action->screen;
635 if (field == ACTION_FIELD_SCREEN) {
640 return ReportActionNotArray(keymap, action->type, field);
642 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
643 act->flags &= ~XkbSA_SwitchAbsolute;
644 scrn = value->value.child;
647 act->flags |= XkbSA_SwitchAbsolute;
651 if (!ExprResolveInteger(keymap->ctx, scrn, &val))
652 return ReportMismatch(keymap, action->type, field,
655 if (val < 0 || val > 255) {
657 "Screen index must be in the range 1..255; "
658 "Illegal screen value %d ignored\n", val);
662 act->screen = (value->op == EXPR_NEGATE ? -val : val);
665 else if (field == ACTION_FIELD_SAME) {
669 return ReportActionNotArray(keymap, action->type, field);
671 if (!ExprResolveBoolean(keymap->ctx, value, &set))
672 return ReportMismatch(keymap, action->type, field, "boolean");
675 act->flags &= ~XkbSA_SwitchApplication;
677 act->flags |= XkbSA_SwitchApplication;
682 return ReportIllegal(keymap, action->type, field);
686 HandleSetLockControls(struct xkb_keymap *keymap, union xkb_action *action,
687 enum action_field field, const ExprDef *array_ndx,
688 const ExprDef *value)
690 struct xkb_controls_action *act = &action->ctrls;
692 if (field == ACTION_FIELD_CONTROLS) {
696 return ReportActionNotArray(keymap, action->type, field);
698 if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlMaskNames))
699 return ReportMismatch(keymap, action->type, field,
706 return ReportIllegal(keymap, action->type, field);
710 HandlePrivate(struct xkb_keymap *keymap, union xkb_action *action,
711 enum action_field field, const ExprDef *array_ndx,
712 const ExprDef *value)
714 struct xkb_private_action *act = &action->priv;
716 if (field == ACTION_FIELD_TYPE) {
719 if (!ExprResolveInteger(keymap->ctx, value, &type))
720 return ReportMismatch(keymap, ACTION_TYPE_PRIVATE, field, "integer");
722 if (type < 0 || type > 255) {
724 "Private action type must be in the range 0..255; "
725 "Illegal type %d ignored\n", type);
729 act->type = (uint8_t) type;
732 else if (field == ACTION_FIELD_DATA) {
733 if (array_ndx == NULL) {
738 if (!ExprResolveString(keymap->ctx, value, &val))
739 return ReportMismatch(keymap, action->type, field, "string");
741 str = xkb_atom_text(keymap->ctx, val);
743 if (len < 1 || len > 7) {
744 log_warn(keymap->ctx,
745 "A private action has 7 data bytes; "
746 "Extra %d bytes ignored\n", len - 6);
750 strncpy((char *) act->data, str, sizeof(act->data));
756 if (!ExprResolveInteger(keymap->ctx, array_ndx, &ndx)) {
758 "Array subscript must be integer; "
759 "Illegal subscript ignored\n");
763 if (ndx < 0 || ndx >= sizeof(act->data)) {
765 "The data for a private action is %zu bytes long; "
766 "Attempt to use data[%d] ignored\n",
767 sizeof(act->data), ndx);
771 if (!ExprResolveInteger(keymap->ctx, value, &datum))
772 return ReportMismatch(keymap, act->type, field, "integer");
774 if (datum < 0 || datum > 255) {
776 "All data for a private action must be 0..255; "
777 "Illegal datum %d ignored\n", datum);
781 act->data[ndx] = (uint8_t) datum;
786 return ReportIllegal(keymap, ACTION_TYPE_NONE, field);
789 typedef bool (*actionHandler)(struct xkb_keymap *keymap,
790 union xkb_action *action,
791 enum action_field field,
792 const ExprDef *array_ndx,
793 const ExprDef *value);
795 static const actionHandler handleAction[ACTION_TYPE_LAST] = {
796 [ACTION_TYPE_NONE] = HandleNoAction,
797 [ACTION_TYPE_MOD_SET] = HandleSetLatchMods,
798 [ACTION_TYPE_MOD_LATCH] = HandleSetLatchMods,
799 [ACTION_TYPE_MOD_LOCK] = HandleLockMods,
800 [ACTION_TYPE_GROUP_SET] = HandleSetLatchGroup,
801 [ACTION_TYPE_GROUP_LATCH] = HandleSetLatchGroup,
802 [ACTION_TYPE_GROUP_LOCK] = HandleLockGroup,
803 [ACTION_TYPE_PTR_MOVE] = HandleMovePtr,
804 [ACTION_TYPE_PTR_BUTTON] = HandlePtrBtn,
805 [ACTION_TYPE_PTR_LOCK] = HandlePtrBtn,
806 [ACTION_TYPE_PTR_DEFAULT] = HandleSetPtrDflt,
807 [ACTION_TYPE_TERMINATE] = HandleNoAction,
808 [ACTION_TYPE_SWITCH_VT] = HandleSwitchScreen,
809 [ACTION_TYPE_CTRL_SET] = HandleSetLockControls,
810 [ACTION_TYPE_CTRL_LOCK] = HandleSetLockControls,
811 [ACTION_TYPE_PRIVATE] = HandlePrivate,
814 /***====================================================================***/
817 HandleActionDef(ExprDef *def, struct xkb_keymap *keymap,
818 union xkb_action *action, ActionsInfo *info)
824 if (def->op != EXPR_ACTION_DECL) {
825 log_err(keymap->ctx, "Expected an action definition, found %s\n",
826 expr_op_type_to_string(def->op));
830 str = xkb_atom_text(keymap->ctx, def->value.action.name);
831 if (!stringToAction(str, &hndlrType)) {
832 log_err(keymap->ctx, "Unknown action %s\n", str);
837 * Get the default values for this action type, as modified by
838 * statements such as:
839 * latchMods.clearLocks = True;
841 *action = info->actions[hndlrType];
844 * Now change the action properties as specified for this
845 * particular instance, e.g. "modifiers" and "clearLocks" in:
846 * SetMods(modifiers=Alt,clearLocks);
848 for (arg = def->value.action.args; arg != NULL;
849 arg = (ExprDef *) arg->common.next) {
850 const ExprDef *value;
851 ExprDef *field, *arrayRtrn;
852 const char *elemRtrn, *fieldRtrn;
853 enum action_field fieldNdx;
855 if (arg->op == EXPR_ASSIGN) {
856 field = arg->value.binary.left;
857 value = arg->value.binary.right;
859 else if (arg->op == EXPR_NOT || arg->op == EXPR_INVERT) {
860 field = arg->value.child;
868 if (!ExprResolveLhs(keymap->ctx, field, &elemRtrn, &fieldRtrn,
874 "Cannot change defaults in an action definition; "
875 "Ignoring attempt to change %s.%s\n",
876 elemRtrn, fieldRtrn);
880 if (!stringToField(fieldRtrn, &fieldNdx)) {
881 log_err(keymap->ctx, "Unknown field name %s\n", fieldRtrn);
885 if (!handleAction[hndlrType](keymap, action, fieldNdx, arrayRtrn,
895 SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field,
896 ExprDef *array_ndx, ExprDef *value, ActionsInfo *info)
899 enum action_field action_field;
901 if (!stringToAction(elem, &action))
904 if (!stringToField(field, &action_field)) {
905 log_err(keymap->ctx, "\"%s\" is not a legal field name\n", field);
909 return handleAction[action](keymap, &info->actions[action],
910 action_field, array_ndx, value);