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 enum xkb_action_flags *flags_inout)
218 enum xkb_action_flags tmp;
221 if (field == ACTION_FIELD_CLEAR_LOCKS)
222 tmp = ACTION_LOCK_CLEAR;
223 else if (field == ACTION_FIELD_LATCH_TO_LOCK)
224 tmp = ACTION_LATCH_TO_LOCK;
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, enum xkb_action_flags *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 |= ACTION_MODS_LOOKUP_MODMAP;
256 if (!ExprResolveVModMask(keymap, value, mods_rtrn))
257 return ReportMismatch(keymap, action,
258 ACTION_FIELD_MODIFIERS, "modifier mask");
260 *flags_inout &= ~ACTION_MODS_LOOKUP_MODMAP;
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;
270 enum xkb_action_flags rtrn, t1;
273 if (array_ndx != NULL) {
275 case ACTION_FIELD_CLEAR_LOCKS:
276 case ACTION_FIELD_LATCH_TO_LOCK:
277 case ACTION_FIELD_MODIFIERS:
278 return ReportActionNotArray(keymap, action->type, field);
285 case ACTION_FIELD_CLEAR_LOCKS:
286 case ACTION_FIELD_LATCH_TO_LOCK:
288 if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
294 case ACTION_FIELD_MODIFIERS:
296 if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
307 return ReportIllegal(keymap, action->type, field);
311 HandleLockMods(struct xkb_keymap *keymap, union xkb_action *action,
312 enum action_field field, const ExprDef *array_ndx,
313 const ExprDef *value)
315 struct xkb_mod_action *act = &action->mods;
316 enum xkb_action_flags t1;
319 if (array_ndx && field == ACTION_FIELD_MODIFIERS)
320 return ReportActionNotArray(keymap, action->type, field);
323 case ACTION_FIELD_MODIFIERS:
325 if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
336 return ReportIllegal(keymap, action->type, field);
340 CheckGroupField(struct xkb_keymap *keymap, unsigned action,
341 const ExprDef *value, enum xkb_action_flags *flags_inout,
342 xkb_group_index_t *grp_rtrn)
346 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
347 *flags_inout &= ~ACTION_ABSOLUTE_SWITCH;
348 spec = value->value.child;
351 *flags_inout |= ACTION_ABSOLUTE_SWITCH;
355 if (!ExprResolveGroup(keymap->ctx, spec, grp_rtrn))
356 return ReportMismatch(keymap, action, ACTION_FIELD_GROUP,
357 "integer (range 1..8)");
359 if (value->op == EXPR_NEGATE)
360 *grp_rtrn = -*grp_rtrn;
361 else if (value->op != EXPR_UNARY_PLUS)
368 HandleSetLatchGroup(struct xkb_keymap *keymap, union xkb_action *action,
369 enum action_field field, const ExprDef *array_ndx,
370 const ExprDef *value)
372 struct xkb_group_action *act = &action->group;
373 enum xkb_action_flags rtrn, t1;
374 xkb_group_index_t t2;
376 if (array_ndx != NULL) {
378 case ACTION_FIELD_CLEAR_LOCKS:
379 case ACTION_FIELD_LATCH_TO_LOCK:
380 case ACTION_FIELD_GROUP:
381 return ReportActionNotArray(keymap, action->type, field);
389 case ACTION_FIELD_CLEAR_LOCKS:
390 case ACTION_FIELD_LATCH_TO_LOCK:
392 if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
398 case ACTION_FIELD_GROUP:
400 if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
411 return ReportIllegal(keymap, action->type, field);
415 HandleLockGroup(struct xkb_keymap *keymap, union xkb_action *action,
416 enum action_field field, const ExprDef *array_ndx,
417 const ExprDef *value)
419 struct xkb_group_action *act = &action->group;
420 enum xkb_action_flags t1;
421 xkb_group_index_t t2;
423 if ((array_ndx != NULL) && (field == ACTION_FIELD_GROUP))
424 return ReportActionNotArray(keymap, action->type, field);
425 if (field == ACTION_FIELD_GROUP) {
427 if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
434 return ReportIllegal(keymap, action->type, field);
438 HandleMovePtr(struct xkb_keymap *keymap, union xkb_action *action,
439 enum action_field field, const ExprDef *array_ndx,
440 const ExprDef *value)
442 struct xkb_pointer_action *act = &action->ptr;
445 if (array_ndx && (field == ACTION_FIELD_X || field == ACTION_FIELD_Y))
446 return ReportActionNotArray(keymap, action->type, field);
448 if (field == ACTION_FIELD_X || field == ACTION_FIELD_Y) {
451 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS)
456 if (!ExprResolveInteger(keymap->ctx, value, &val))
457 return ReportMismatch(keymap, action->type, field, "integer");
459 if (field == ACTION_FIELD_X) {
461 act->flags |= ACTION_ABSOLUTE_X;
466 act->flags |= ACTION_ABSOLUTE_Y;
472 else if (field == ACTION_FIELD_ACCEL) {
475 if (!ExprResolveBoolean(keymap->ctx, value, &set))
476 return ReportMismatch(keymap, action->type, field, "boolean");
479 act->flags &= ~ACTION_NO_ACCEL;
481 act->flags |= ACTION_NO_ACCEL;
484 return ReportIllegal(keymap, action->type, field);
487 static const LookupEntry lockWhich[] = {
489 { "lock", ACTION_LOCK_NO_UNLOCK },
490 { "neither", (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK) },
491 { "unlock", ACTION_LOCK_NO_LOCK },
496 HandlePtrBtn(struct xkb_keymap *keymap, union xkb_action *action,
497 enum action_field field, const ExprDef *array_ndx,
498 const ExprDef *value)
500 struct xkb_pointer_button_action *act = &action->btn;
502 if (field == ACTION_FIELD_BUTTON) {
506 return ReportActionNotArray(keymap, action->type, field);
508 if (!ExprResolveButton(keymap->ctx, value, &btn))
509 return ReportMismatch(keymap, action->type, field,
510 "integer (range 1..5)");
512 if (btn < 0 || btn > 5) {
514 "Button must specify default or be in the range 1..5; "
515 "Illegal button value %d ignored\n", btn);
522 else if (action->type == ACTION_TYPE_PTR_LOCK &&
523 field == ACTION_FIELD_AFFECT) {
524 enum xkb_action_flags val;
527 return ReportActionNotArray(keymap, action->type, field);
529 if (!ExprResolveEnum(keymap->ctx, value, &val, lockWhich))
530 return ReportMismatch(keymap, action->type, field,
533 act->flags &= ~(ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK);
537 else if (field == ACTION_FIELD_COUNT) {
541 return ReportActionNotArray(keymap, action->type, field);
543 /* XXX: Should this actually be ResolveButton? */
544 if (!ExprResolveButton(keymap->ctx, value, &btn))
545 return ReportMismatch(keymap, action->type, field, "integer");
547 if (btn < 0 || btn > 255) {
549 "The count field must have a value in the range 0..255; "
550 "Illegal count %d ignored\n", btn);
557 return ReportIllegal(keymap, action->type, field);
560 static const LookupEntry ptrDflts[] = {
561 { "dfltbtn", XkbSA_AffectDfltBtn },
562 { "defaultbutton", XkbSA_AffectDfltBtn },
563 { "button", XkbSA_AffectDfltBtn },
568 HandleSetPtrDflt(struct xkb_keymap *keymap, union xkb_action *action,
569 enum action_field field, const ExprDef *array_ndx,
570 const ExprDef *value)
572 struct xkb_pointer_default_action *act = &action->dflt;
574 if (field == ACTION_FIELD_AFFECT) {
578 return ReportActionNotArray(keymap, action->type, field);
580 if (!ExprResolveEnum(keymap->ctx, value, &val, ptrDflts))
581 return ReportMismatch(keymap, action->type, field,
582 "pointer component");
586 else if (field == ACTION_FIELD_BUTTON || field == ACTION_FIELD_VALUE) {
587 const ExprDef *button;
591 return ReportActionNotArray(keymap, action->type, field);
593 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
594 act->flags &= ~ACTION_ABSOLUTE_SWITCH;
595 button = value->value.child;
598 act->flags |= ACTION_ABSOLUTE_SWITCH;
602 if (!ExprResolveButton(keymap->ctx, button, &btn))
603 return ReportMismatch(keymap, action->type, field,
604 "integer (range 1..5)");
606 if (btn < 0 || btn > 5) {
608 "New default button value must be in the range 1..5; "
609 "Illegal default button value %d ignored\n", btn);
614 "Cannot set default pointer button to \"default\"; "
615 "Illegal default button setting ignored\n");
619 act->value = (value->op == EXPR_NEGATE ? -btn: btn);
623 return ReportIllegal(keymap, action->type, field);
627 HandleSwitchScreen(struct xkb_keymap *keymap, union xkb_action *action,
628 enum action_field field, const ExprDef *array_ndx,
629 const ExprDef *value)
631 struct xkb_switch_screen_action *act = &action->screen;
633 if (field == ACTION_FIELD_SCREEN) {
638 return ReportActionNotArray(keymap, action->type, field);
640 if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
641 act->flags &= ~ACTION_ABSOLUTE_SWITCH;
642 scrn = value->value.child;
645 act->flags |= ACTION_ABSOLUTE_SWITCH;
649 if (!ExprResolveInteger(keymap->ctx, scrn, &val))
650 return ReportMismatch(keymap, action->type, field,
653 if (val < 0 || val > 255) {
655 "Screen index must be in the range 1..255; "
656 "Illegal screen value %d ignored\n", val);
660 act->screen = (value->op == EXPR_NEGATE ? -val : val);
663 else if (field == ACTION_FIELD_SAME) {
667 return ReportActionNotArray(keymap, action->type, field);
669 if (!ExprResolveBoolean(keymap->ctx, value, &set))
670 return ReportMismatch(keymap, action->type, field, "boolean");
673 act->flags &= ~ACTION_SAME_SCREEN;
675 act->flags |= ACTION_SAME_SCREEN;
680 return ReportIllegal(keymap, action->type, field);
684 HandleSetLockControls(struct xkb_keymap *keymap, union xkb_action *action,
685 enum action_field field, const ExprDef *array_ndx,
686 const ExprDef *value)
688 struct xkb_controls_action *act = &action->ctrls;
690 if (field == ACTION_FIELD_CONTROLS) {
694 return ReportActionNotArray(keymap, action->type, field);
696 if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlMaskNames))
697 return ReportMismatch(keymap, action->type, field,
704 return ReportIllegal(keymap, action->type, field);
708 HandlePrivate(struct xkb_keymap *keymap, union xkb_action *action,
709 enum action_field field, const ExprDef *array_ndx,
710 const ExprDef *value)
712 struct xkb_private_action *act = &action->priv;
714 if (field == ACTION_FIELD_TYPE) {
717 if (!ExprResolveInteger(keymap->ctx, value, &type))
718 return ReportMismatch(keymap, ACTION_TYPE_PRIVATE, field, "integer");
720 if (type < 0 || type > 255) {
722 "Private action type must be in the range 0..255; "
723 "Illegal type %d ignored\n", type);
727 act->type = (uint8_t) type;
730 else if (field == ACTION_FIELD_DATA) {
731 if (array_ndx == NULL) {
736 if (!ExprResolveString(keymap->ctx, value, &val))
737 return ReportMismatch(keymap, action->type, field, "string");
739 str = xkb_atom_text(keymap->ctx, val);
741 if (len < 1 || len > 7) {
742 log_warn(keymap->ctx,
743 "A private action has 7 data bytes; "
744 "Extra %d bytes ignored\n", len - 6);
748 strncpy((char *) act->data, str, sizeof(act->data));
754 if (!ExprResolveInteger(keymap->ctx, array_ndx, &ndx)) {
756 "Array subscript must be integer; "
757 "Illegal subscript ignored\n");
761 if (ndx < 0 || ndx >= sizeof(act->data)) {
763 "The data for a private action is %zu bytes long; "
764 "Attempt to use data[%d] ignored\n",
765 sizeof(act->data), ndx);
769 if (!ExprResolveInteger(keymap->ctx, value, &datum))
770 return ReportMismatch(keymap, act->type, field, "integer");
772 if (datum < 0 || datum > 255) {
774 "All data for a private action must be 0..255; "
775 "Illegal datum %d ignored\n", datum);
779 act->data[ndx] = (uint8_t) datum;
784 return ReportIllegal(keymap, ACTION_TYPE_NONE, field);
787 typedef bool (*actionHandler)(struct xkb_keymap *keymap,
788 union xkb_action *action,
789 enum action_field field,
790 const ExprDef *array_ndx,
791 const ExprDef *value);
793 static const actionHandler handleAction[ACTION_TYPE_LAST] = {
794 [ACTION_TYPE_NONE] = HandleNoAction,
795 [ACTION_TYPE_MOD_SET] = HandleSetLatchMods,
796 [ACTION_TYPE_MOD_LATCH] = HandleSetLatchMods,
797 [ACTION_TYPE_MOD_LOCK] = HandleLockMods,
798 [ACTION_TYPE_GROUP_SET] = HandleSetLatchGroup,
799 [ACTION_TYPE_GROUP_LATCH] = HandleSetLatchGroup,
800 [ACTION_TYPE_GROUP_LOCK] = HandleLockGroup,
801 [ACTION_TYPE_PTR_MOVE] = HandleMovePtr,
802 [ACTION_TYPE_PTR_BUTTON] = HandlePtrBtn,
803 [ACTION_TYPE_PTR_LOCK] = HandlePtrBtn,
804 [ACTION_TYPE_PTR_DEFAULT] = HandleSetPtrDflt,
805 [ACTION_TYPE_TERMINATE] = HandleNoAction,
806 [ACTION_TYPE_SWITCH_VT] = HandleSwitchScreen,
807 [ACTION_TYPE_CTRL_SET] = HandleSetLockControls,
808 [ACTION_TYPE_CTRL_LOCK] = HandleSetLockControls,
809 [ACTION_TYPE_PRIVATE] = HandlePrivate,
812 /***====================================================================***/
815 HandleActionDef(ExprDef *def, struct xkb_keymap *keymap,
816 union xkb_action *action, ActionsInfo *info)
822 if (def->op != EXPR_ACTION_DECL) {
823 log_err(keymap->ctx, "Expected an action definition, found %s\n",
824 expr_op_type_to_string(def->op));
828 str = xkb_atom_text(keymap->ctx, def->value.action.name);
829 if (!stringToAction(str, &hndlrType)) {
830 log_err(keymap->ctx, "Unknown action %s\n", str);
835 * Get the default values for this action type, as modified by
836 * statements such as:
837 * latchMods.clearLocks = True;
839 *action = info->actions[hndlrType];
842 * Now change the action properties as specified for this
843 * particular instance, e.g. "modifiers" and "clearLocks" in:
844 * SetMods(modifiers=Alt,clearLocks);
846 for (arg = def->value.action.args; arg != NULL;
847 arg = (ExprDef *) arg->common.next) {
848 const ExprDef *value;
849 ExprDef *field, *arrayRtrn;
850 const char *elemRtrn, *fieldRtrn;
851 enum action_field fieldNdx;
853 if (arg->op == EXPR_ASSIGN) {
854 field = arg->value.binary.left;
855 value = arg->value.binary.right;
857 else if (arg->op == EXPR_NOT || arg->op == EXPR_INVERT) {
858 field = arg->value.child;
866 if (!ExprResolveLhs(keymap->ctx, field, &elemRtrn, &fieldRtrn,
872 "Cannot change defaults in an action definition; "
873 "Ignoring attempt to change %s.%s\n",
874 elemRtrn, fieldRtrn);
878 if (!stringToField(fieldRtrn, &fieldNdx)) {
879 log_err(keymap->ctx, "Unknown field name %s\n", fieldRtrn);
883 if (!handleAction[hndlrType](keymap, action, fieldNdx, arrayRtrn,
893 SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field,
894 ExprDef *array_ndx, ExprDef *value, ActionsInfo *info)
897 enum action_field action_field;
899 if (!stringToAction(elem, &action))
902 if (!stringToField(field, &action_field)) {
903 log_err(keymap->ctx, "\"%s\" is not a legal field name\n", field);
907 return handleAction[action](keymap, &info->actions[action],
908 action_field, array_ndx, value);