Debian Codegraph @

src/xkbcomp/action.c

raw
1 /*
2 * For HPND:
3 * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
4 *
5 * For MIT:
6 * Copyright © 2012 Intel Corporation
7 * Copyright © 2012 Ran Benita <[email protected]>
8 *
9 * SPDX-License-Identifier: HPND AND MIT
10 *
11 * Author: Daniel Stone <[email protected]>
12 * Author: Ran Benita <[email protected]>
13 */
14
15 #include "config.h"
16
17 #include <assert.h>
18
19 #include "xkbcommon/xkbcommon.h"
20 #include "context.h"
21 #include "keymap.h"
22 #include "messages-codes.h"
23 #include "xkbcomp-priv.h"
24 #include "text.h"
25 #include "expr.h"
26 #include "xkbcomp/ast.h"
27 #include "action.h"
28
29 static const ExprBoolean constTrue = {
30 .common = { .type = STMT_EXPR_BOOLEAN_LITERAL, .next = NULL },
31 .set = true,
32 };
33
34 static const ExprBoolean constFalse = {
35 .common = { .type = STMT_EXPR_BOOLEAN_LITERAL, .next = NULL },
36 .set = false,
37 };
38
39 enum action_field {
40 ACTION_FIELD_CLEAR_LOCKS,
41 ACTION_FIELD_LATCH_TO_LOCK,
42 ACTION_FIELD_GEN_KEY_EVENT,
43 ACTION_FIELD_REPORT,
44 ACTION_FIELD_DEFAULT,
45 ACTION_FIELD_AFFECT,
46 ACTION_FIELD_INCREMENT,
47 ACTION_FIELD_MODIFIERS,
48 ACTION_FIELD_GROUP,
49 ACTION_FIELD_X,
50 ACTION_FIELD_Y,
51 ACTION_FIELD_ACCEL,
52 ACTION_FIELD_BUTTON,
53 ACTION_FIELD_VALUE,
54 ACTION_FIELD_CONTROLS,
55 ACTION_FIELD_TYPE,
56 ACTION_FIELD_COUNT,
57 ACTION_FIELD_SCREEN,
58 ACTION_FIELD_SAME,
59 ACTION_FIELD_DATA,
60 ACTION_FIELD_DEVICE,
61 ACTION_FIELD_KEYCODE,
62 ACTION_FIELD_MODS_TO_CLEAR,
63 ACTION_FIELD_LOCK_ON_RELEASE,
64 ACTION_FIELD_UNLOCK_ON_PRESS,
65 ACTION_FIELD_LATCH_ON_PRESS,
66 };
67
68 void
69 InitActionsInfo(ActionsInfo *info)
70 {
71 for (enum xkb_action_type type = 0; type < _ACTION_TYPE_NUM_ENTRIES; type++)
72 info->actions[type].type = type;
73
74 /* Apply some "factory defaults". */
75
76 /* Increment default button. */
77 info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.flags = 0;
78 info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.value = 1;
79 info->actions[ACTION_TYPE_PTR_MOVE].ptr.flags = ACTION_ACCEL;
80 info->actions[ACTION_TYPE_SWITCH_VT].screen.flags = ACTION_SAME_SCREEN;
81 }
82
83 static const LookupEntry fieldStrings[] = {
84 { "clearLocks", ACTION_FIELD_CLEAR_LOCKS },
85 { "latchToLock", ACTION_FIELD_LATCH_TO_LOCK },
86 { "genKeyEvent", ACTION_FIELD_GEN_KEY_EVENT },
87 { "generateKeyEvent", ACTION_FIELD_GEN_KEY_EVENT },
88 { "report", ACTION_FIELD_REPORT },
89 { "default", ACTION_FIELD_DEFAULT },
90 { "affect", ACTION_FIELD_AFFECT },
91 { "increment", ACTION_FIELD_INCREMENT },
92 { "modifiers", ACTION_FIELD_MODIFIERS },
93 { "mods", ACTION_FIELD_MODIFIERS },
94 { "group", ACTION_FIELD_GROUP },
95 { "x", ACTION_FIELD_X },
96 { "y", ACTION_FIELD_Y },
97 { "accel", ACTION_FIELD_ACCEL },
98 { "accelerate", ACTION_FIELD_ACCEL },
99 { "repeat", ACTION_FIELD_ACCEL },
100 { "button", ACTION_FIELD_BUTTON },
101 { "value", ACTION_FIELD_VALUE },
102 { "controls", ACTION_FIELD_CONTROLS },
103 { "ctrls", ACTION_FIELD_CONTROLS },
104 { "type", ACTION_FIELD_TYPE },
105 { "count", ACTION_FIELD_COUNT },
106 { "screen", ACTION_FIELD_SCREEN },
107 { "same", ACTION_FIELD_SAME },
108 { "sameServer", ACTION_FIELD_SAME },
109 { "data", ACTION_FIELD_DATA },
110 { "device", ACTION_FIELD_DEVICE },
111 { "dev", ACTION_FIELD_DEVICE },
112 { "key", ACTION_FIELD_KEYCODE },
113 { "keycode", ACTION_FIELD_KEYCODE },
114 { "kc", ACTION_FIELD_KEYCODE },
115 { "clearmods", ACTION_FIELD_MODS_TO_CLEAR },
116 { "clearmodifiers", ACTION_FIELD_MODS_TO_CLEAR },
117 { "lockOnRelease", ACTION_FIELD_LOCK_ON_RELEASE },
118 { "unlockOnPress", ACTION_FIELD_UNLOCK_ON_PRESS },
119 { "latchOnPress", ACTION_FIELD_LATCH_ON_PRESS },
120 { NULL, 0 }
121 };
122
123 static bool
124 stringToActionType(const char *str, enum xkb_action_type *type_rtrn)
125 {
126 unsigned int type = 0;
127 const bool ret = LookupString(actionTypeNames, str, &type);
128 *type_rtrn = (enum xkb_action_type) type;
129 return ret;
130 }
131
132 static bool
133 stringToField(const char *str, enum action_field *field_rtrn)
134 {
135 unsigned int field = 0;
136 const bool ret = LookupString(fieldStrings, str, &field);
137 *field_rtrn = (enum action_field) field;
138 return ret;
139 }
140
141 static const char *
142 fieldText(enum action_field field)
143 {
144 return LookupValue(fieldStrings, field);
145 }
146
147 /***====================================================================***/
148
149 static inline bool
150 ReportMismatch(struct xkb_context *ctx, xkb_message_code_t code,
151 enum xkb_action_type action, enum action_field field,
152 const char *type)
153 {
154 log_err(ctx, code,
155 "Value of %s field must be of type %s; "
156 "Action %s definition ignored\n",
157 fieldText(field), type, ActionTypeText(action));
158 return false;
159 }
160
161 static inline bool
162 ReportFormatVersionMismatch(struct xkb_context *ctx,
163 enum xkb_action_type action,
164 enum action_field field,
165 enum xkb_keymap_format format, const char *versions)
166 {
167 log_err(ctx, XKB_ERROR_INCOMPATIBLE_KEYMAP_TEXT_FORMAT,
168 "Field %s for an action of type %s requires keymap text format %s, "
169 " but got: %d; Action definition ignored\n",
170 fieldText(field), ActionTypeText(action), versions, format);
171 return false;
172 }
173
174 static inline bool
175 ReportIllegal(struct xkb_context *ctx, enum xkb_action_type action,
176 enum action_field field)
177 {
178 log_err(ctx, XKB_ERROR_INVALID_ACTION_FIELD,
179 "Field %s is not defined for an action of type %s; "
180 "Action definition ignored\n",
181 fieldText(field), ActionTypeText(action));
182 return false;
183 }
184
185 static inline bool
186 ReportActionNotArray(struct xkb_context *ctx, enum xkb_action_type action,
187 enum action_field field)
188 {
189 log_err(ctx, XKB_ERROR_WRONG_FIELD_TYPE,
190 "The %s field in the %s action is not an array; "
191 "Action definition ignored\n",
192 fieldText(field), ActionTypeText(action));
193 return false;
194 }
195
196 static bool
197 HandleNoAction(struct xkb_context *ctx, enum xkb_keymap_format format,
198 const struct xkb_mod_set *mods,
199 union xkb_action *action, enum action_field field,
200 const ExprDef *array_ndx, const ExprDef *value)
201
202 {
203 log_err(ctx, XKB_ERROR_INVALID_ACTION_FIELD,
204 "The \"%s\" action takes no argument, but got \"%s\" field; "
205 "Action definition ignored\n",
206 ActionTypeText(action->type), fieldText(field));
207 return false;
208 }
209
210 static bool
211 CheckBooleanFlag(struct xkb_context *ctx, enum xkb_action_type action,
212 enum action_field field, enum xkb_action_flags flag,
213 const ExprDef *array_ndx, const ExprDef *value,
214 enum xkb_action_flags *flags_inout)
215 {
216 bool set = false;
217
218 if (array_ndx)
219 return ReportActionNotArray(ctx, action, field);
220
221 if (!ExprResolveBoolean(ctx, value, &set))
222 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE,
223 action, field, "boolean");
224
225 if (set)
226 *flags_inout |= flag;
227 else
228 *flags_inout &= ~flag;
229
230 return true;
231 }
232
233 static bool
234 CheckModifierField(struct xkb_context *ctx, const struct xkb_mod_set *mods,
235 enum xkb_action_type action, const ExprDef *array_ndx,
236 const ExprDef *value, enum xkb_action_flags *flags_inout,
237 xkb_mod_mask_t *mods_rtrn)
238 {
239 if (array_ndx)
240 return ReportActionNotArray(ctx, action, ACTION_FIELD_MODIFIERS);
241
242 if (value->common.type == STMT_EXPR_IDENT) {
243 const char *valStr;
244 valStr = xkb_atom_text(ctx, value->ident.ident);
245 if (valStr && (istreq(valStr, "usemodmapmods") ||
246 istreq(valStr, "modmapmods"))) {
247 *mods_rtrn = 0;
248 *flags_inout |= ACTION_MODS_LOOKUP_MODMAP;
249 return true;
250 }
251 }
252
253 if (!ExprResolveModMask(ctx, value, MOD_BOTH, mods, mods_rtrn))
254 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE, action,
255 ACTION_FIELD_MODIFIERS, "modifier mask");
256
257 *flags_inout &= ~ACTION_MODS_LOOKUP_MODMAP;
258 return true;
259 }
260
261 static const LookupEntry lockWhich[] = {
262 { "both", 0 },
263 { "lock", ACTION_LOCK_NO_UNLOCK },
264 { "neither", (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK) },
265 { "unlock", ACTION_LOCK_NO_LOCK },
266 { NULL, 0 }
267 };
268
269 static bool
270 CheckAffectField(struct xkb_context *ctx, enum xkb_action_type action,
271 const ExprDef *array_ndx, const ExprDef *value,
272 enum xkb_action_flags *flags_inout)
273 {
274 if (array_ndx)
275 return ReportActionNotArray(ctx, action, ACTION_FIELD_AFFECT);
276
277 uint32_t flags = 0;
278 if (!ExprResolveEnum(ctx, value, &flags, lockWhich))
279 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE,
280 action, ACTION_FIELD_AFFECT,
281 "lock, unlock, both, neither");
282
283 *flags_inout &= ~(ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK);
284 *flags_inout |= (enum xkb_action_flags) flags;
285 return true;
286 }
287
288 static bool
289 HandleSetLatchLockMods(struct xkb_context *ctx, enum xkb_keymap_format format,
290 const struct xkb_mod_set *mods,
291 union xkb_action *action, enum action_field field,
292 const ExprDef *array_ndx, const ExprDef *value)
293 {
294 struct xkb_mod_action *act = &action->mods;
295 const enum xkb_action_type type = action->type;
296
297 if (field == ACTION_FIELD_MODIFIERS)
298 return CheckModifierField(ctx, mods, action->type, array_ndx, value,
299 &act->flags, &act->mods.mods);
300 if (field == ACTION_FIELD_UNLOCK_ON_PRESS) {
301 /* Ensure to update if a new modifier action is introduced. */
302 assert(type == ACTION_TYPE_MOD_SET || type == ACTION_TYPE_MOD_LATCH ||
303 type == ACTION_TYPE_MOD_LOCK);
304 if (isModsUnLockOnPressSupported(format)) {
305 return CheckBooleanFlag(ctx, action->type, field,
306 ACTION_UNLOCK_ON_PRESS, array_ndx,
307 value, &act->flags);
308 } else {
309 return ReportFormatVersionMismatch(ctx, action->type, field,
310 format, ">= 2");
311 }
312 }
313 if ((type == ACTION_TYPE_MOD_SET || type == ACTION_TYPE_MOD_LATCH) &&
314 field == ACTION_FIELD_CLEAR_LOCKS)
315 return CheckBooleanFlag(ctx, action->type, field,
316 ACTION_LOCK_CLEAR, array_ndx, value,
317 &act->flags);
318 if (type == ACTION_TYPE_MOD_LATCH) {
319 if (field == ACTION_FIELD_LATCH_TO_LOCK)
320 return CheckBooleanFlag(ctx, action->type, field,
321 ACTION_LATCH_TO_LOCK, array_ndx, value,
322 &act->flags);
323 if (field == ACTION_FIELD_LATCH_ON_PRESS) {
324 if (isModsLatchOnPressSupported(format)) {
325 return CheckBooleanFlag(ctx, action->type, field,
326 ACTION_LATCH_ON_PRESS, array_ndx, value,
327 &act->flags);
328 } else {
329 return ReportFormatVersionMismatch(ctx, action->type, field,
330 format, ">= 2");
331 }
332 }
333 }
334 if (type == ACTION_TYPE_MOD_LOCK && field == ACTION_FIELD_AFFECT)
335 return CheckAffectField(ctx, action->type, array_ndx, value,
336 &act->flags);
337
338 return ReportIllegal(ctx, action->type, field);
339 }
340
341 static bool
342 CheckGroupField(struct xkb_context *ctx, enum xkb_action_type action,
343 xkb_layout_index_t max_groups, const ExprDef *array_ndx,
344 const ExprDef *value, enum xkb_action_flags *flags_inout,
345 int32_t *group_rtrn)
346 {
347 const ExprDef *spec;
348 xkb_layout_index_t idx = 0;
349 enum xkb_action_flags flags = *flags_inout;
350
351 if (array_ndx)
352 return ReportActionNotArray(ctx, action, ACTION_FIELD_GROUP);
353
354 if (value->common.type == STMT_EXPR_NEGATE || value->common.type == STMT_EXPR_UNARY_PLUS) {
355 flags &= ~ACTION_ABSOLUTE_SWITCH;
356 spec = value->unary.child;
357 }
358 else {
359 flags |= ACTION_ABSOLUTE_SWITCH;
360 spec = value;
361 }
362
363 if (!ExprResolveGroup(ctx, max_groups, spec, &idx))
364 return ReportMismatch(ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX, action,
365 ACTION_FIELD_GROUP, "integer");
366
367 /* `+n`, `-n` are relative, `n` is absolute. */
368 if (value->common.type == STMT_EXPR_NEGATE || value->common.type == STMT_EXPR_UNARY_PLUS) {
369 *group_rtrn = (int32_t) idx;
370 if (value->common.type == STMT_EXPR_NEGATE)
371 *group_rtrn = -*group_rtrn;
372 }
373 else {
374 *group_rtrn = (int32_t) (idx - 1);
375 }
376 *flags_inout = flags;
377 return true;
378 }
379
380 static bool
381 HandleSetLatchLockGroup(struct xkb_context *ctx, enum xkb_keymap_format format,
382 const struct xkb_mod_set *mods,
383 union xkb_action *action, enum action_field field,
384 const ExprDef *array_ndx, const ExprDef *value)
385 {
386 struct xkb_group_action *act = &action->group;
387 const enum xkb_action_type type = action->type;
388
389 if (field == ACTION_FIELD_GROUP) {
390 const xkb_layout_index_t max_groups = format_max_groups(format);
391 return CheckGroupField(ctx, action->type, max_groups, array_ndx, value,
392 &act->flags, &act->group);
393 }
394 if ((type == ACTION_TYPE_GROUP_SET || type == ACTION_TYPE_GROUP_LATCH) &&
395 field == ACTION_FIELD_CLEAR_LOCKS)
396 return CheckBooleanFlag(ctx, action->type, field,
397 ACTION_LOCK_CLEAR, array_ndx, value,
398 &act->flags);
399 if (type == ACTION_TYPE_GROUP_LATCH &&
400 field == ACTION_FIELD_LATCH_TO_LOCK)
401 return CheckBooleanFlag(ctx, action->type, field,
402 ACTION_LATCH_TO_LOCK, array_ndx, value,
403 &act->flags);
404 if (type == ACTION_TYPE_GROUP_LOCK &&
405 field == ACTION_FIELD_LOCK_ON_RELEASE) {
406 /* TODO: support this for `ACTION_TYPE_MOD_LOCK` too? */
407 if (isGroupLockOnReleaseSupported(format)) {
408 return CheckBooleanFlag(ctx, action->type, field,
409 ACTION_LOCK_ON_RELEASE, array_ndx, value,
410 &act->flags);
411 } else {
412 return ReportFormatVersionMismatch(ctx, action->type, field, format,
413 ">= v2");
414 }
415 }
416
417 return ReportIllegal(ctx, action->type, field);
418 }
419
420 static bool
421 HandleMovePtr(struct xkb_context *ctx, enum xkb_keymap_format format,
422 const struct xkb_mod_set *mods,
423 union xkb_action *action, enum action_field field,
424 const ExprDef *array_ndx, const ExprDef *value)
425 {
426 struct xkb_pointer_action *act = &action->ptr;
427
428 if (field == ACTION_FIELD_X || field == ACTION_FIELD_Y) {
429 int64_t val = 0;
430 const bool absolute = (value->common.type != STMT_EXPR_NEGATE &&
431 value->common.type != STMT_EXPR_UNARY_PLUS);
432
433 if (array_ndx)
434 return ReportActionNotArray(ctx, action->type, field);
435
436 if (!ExprResolveInteger(ctx, value, &val))
437 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE, action->type,
438 field, "integer");
439
440 if (val < INT16_MIN || val > INT16_MAX) {
441 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
442 "The %s field in the %s action must be in range %d..%d, "
443 "but got %"PRId64". Action definition ignored\n",
444 fieldText(field), ActionTypeText(action->type),
445 INT16_MIN, INT16_MAX, val);
446 return false;
447 }
448
449 if (field == ACTION_FIELD_X) {
450 if (absolute)
451 act->flags |= ACTION_ABSOLUTE_X;
452 act->x = (int16_t) val;
453 }
454 else {
455 if (absolute)
456 act->flags |= ACTION_ABSOLUTE_Y;
457 act->y = (int16_t) val;
458 }
459
460 return true;
461 }
462 else if (field == ACTION_FIELD_ACCEL) {
463 return CheckBooleanFlag(ctx, action->type, field,
464 ACTION_ACCEL, array_ndx, value, &act->flags);
465 }
466
467 return ReportIllegal(ctx, action->type, field);
468 }
469
470 static bool
471 HandlePtrBtn(struct xkb_context *ctx, enum xkb_keymap_format format,
472 const struct xkb_mod_set *mods,
473 union xkb_action *action, enum action_field field,
474 const ExprDef *array_ndx, const ExprDef *value)
475 {
476 struct xkb_pointer_button_action *act = &action->btn;
477
478 if (field == ACTION_FIELD_BUTTON) {
479 int64_t btn = 0;
480
481 if (array_ndx)
482 return ReportActionNotArray(ctx, action->type, field);
483
484 if (!ExprResolveButton(ctx, value, &btn))
485 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE, action->type,
486 field, "integer (range 1..5)");
487
488 if (btn < 0 || btn > 5) {
489 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
490 "Button must specify default or be in the range 1..5; "
491 "Illegal button value %"PRId64" ignored\n", btn);
492 return false;
493 }
494
495 act->button = (uint8_t) btn;
496 return true;
497 }
498 else if (action->type == ACTION_TYPE_PTR_LOCK &&
499 field == ACTION_FIELD_AFFECT) {
500 return CheckAffectField(ctx, action->type, array_ndx, value,
501 &act->flags);
502 }
503 else if (field == ACTION_FIELD_COUNT) {
504 int64_t val = 0;
505
506 if (array_ndx)
507 return ReportActionNotArray(ctx, action->type, field);
508
509 if (!ExprResolveInteger(ctx, value, &val))
510 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE, action->type,
511 field, "integer");
512
513 if (val < 0 || val > 255) {
514 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
515 "The count field must have a value in the range 0..255; "
516 "Illegal count %"PRId64" ignored\n", val);
517 return false;
518 }
519
520 act->count = (uint8_t) val;
521 return true;
522 }
523
524 return ReportIllegal(ctx, action->type, field);
525 }
526
527 static const LookupEntry ptrDflts[] = {
528 { "dfltbtn", 1 },
529 { "defaultbutton", 1 },
530 { "button", 1 },
531 { NULL, 0 }
532 };
533
534 static bool
535 HandleSetPtrDflt(struct xkb_context *ctx, enum xkb_keymap_format format,
536 const struct xkb_mod_set *mods,
537 union xkb_action *action, enum action_field field,
538 const ExprDef *array_ndx, const ExprDef *value)
539 {
540 struct xkb_pointer_default_action *act = &action->dflt;
541
542 if (field == ACTION_FIELD_AFFECT) {
543 uint32_t val = 0;
544
545 if (array_ndx)
546 return ReportActionNotArray(ctx, action->type, field);
547
548 if (!ExprResolveEnum(ctx, value, &val, ptrDflts))
549 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE, action->type,
550 field, "pointer component");
551 return true;
552 }
553 else if (field == ACTION_FIELD_BUTTON || field == ACTION_FIELD_VALUE) {
554 const ExprDef *button;
555 int64_t btn = 0;
556
557 if (array_ndx)
558 return ReportActionNotArray(ctx, action->type, field);
559
560 if (value->common.type == STMT_EXPR_NEGATE ||
561 value->common.type == STMT_EXPR_UNARY_PLUS) {
562 act->flags &= ~ACTION_ABSOLUTE_SWITCH;
563 button = value->unary.child;
564 }
565 else {
566 act->flags |= ACTION_ABSOLUTE_SWITCH;
567 button = value;
568 }
569
570 if (!ExprResolveButton(ctx, button, &btn))
571 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE, action->type,
572 field, "integer (range 1..5)");
573
574 if (btn < 0 || btn > 5) {
575 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
576 "New default button value must be in the range 1..5; "
577 "Illegal default button value %"PRId64" ignored\n", btn);
578 return false;
579 }
580 if (btn == 0) {
581 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
582 "Cannot set default pointer button to \"default\"; "
583 "Illegal default button setting ignored\n");
584 return false;
585 }
586
587 act->value = (int8_t) (value->common.type == STMT_EXPR_NEGATE ? -btn: btn);
588 return true;
589 }
590
591 return ReportIllegal(ctx, action->type, field);
592 }
593
594 static bool
595 HandleSwitchScreen(struct xkb_context *ctx, enum xkb_keymap_format format,
596 const struct xkb_mod_set *mods,
597 union xkb_action *action, enum action_field field,
598 const ExprDef *array_ndx, const ExprDef *value)
599 {
600 struct xkb_switch_screen_action *act = &action->screen;
601
602 if (field == ACTION_FIELD_SCREEN) {
603 const ExprDef *scrn;
604 int64_t val = 0;
605
606 if (array_ndx)
607 return ReportActionNotArray(ctx, action->type, field);
608
609 if (value->common.type == STMT_EXPR_NEGATE ||
610 value->common.type == STMT_EXPR_UNARY_PLUS) {
611 act->flags &= ~ACTION_ABSOLUTE_SWITCH;
612 scrn = value->unary.child;
613 }
614 else {
615 act->flags |= ACTION_ABSOLUTE_SWITCH;
616 scrn = value;
617 }
618
619 if (!ExprResolveInteger(ctx, scrn, &val))
620 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE, action->type,
621 field, "integer (-128..127)");
622
623 val = (value->common.type == STMT_EXPR_NEGATE ? -val : val);
624 if (val < INT8_MIN || val > INT8_MAX) {
625 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
626 "Screen index must be in the range %d..%d; "
627 "Illegal screen value %"PRId64" ignored\n",
628 INT8_MIN, INT8_MAX, val);
629 return false;
630 }
631
632 act->screen = (int8_t) val;
633 return true;
634 }
635 else if (field == ACTION_FIELD_SAME) {
636 return CheckBooleanFlag(ctx, action->type, field,
637 ACTION_SAME_SCREEN, array_ndx, value,
638 &act->flags);
639 }
640
641 return ReportIllegal(ctx, action->type, field);
642 }
643
644 static bool
645 HandleSetLockControls(struct xkb_context *ctx, enum xkb_keymap_format format,
646 const struct xkb_mod_set *mods,
647 union xkb_action *action, enum action_field field,
648 const ExprDef *array_ndx, const ExprDef *value)
649 {
650 struct xkb_controls_action *act = &action->ctrls;
651
652 if (field == ACTION_FIELD_CONTROLS) {
653 if (array_ndx)
654 return ReportActionNotArray(ctx, action->type, field);
655
656 uint32_t mask = 0;
657 if (!ExprResolveMask(ctx, value, &mask, ctrlMaskNames))
658 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE, action->type,
659 field, "controls mask");
660
661 act->ctrls = (enum xkb_action_controls) mask;
662 return true;
663 }
664 else if (field == ACTION_FIELD_AFFECT &&
665 action->type == ACTION_TYPE_CTRL_LOCK) {
666 return CheckAffectField(ctx, action->type, array_ndx, value,
667 &act->flags);
668 }
669
670 return ReportIllegal(ctx, action->type, field);
671 }
672
673 static bool
674 HandleUnsupportedLegacy(struct xkb_context *ctx, enum xkb_keymap_format format,
675 const struct xkb_mod_set *mods,
676 union xkb_action *action, enum action_field field,
677 const ExprDef *array_ndx, const ExprDef *value)
678
679 {
680 /* Do not check any field */
681 return true;
682 }
683
684 static bool
685 HandlePrivate(struct xkb_context *ctx, enum xkb_keymap_format format,
686 const struct xkb_mod_set *mods,
687 union xkb_action *action, enum action_field field,
688 const ExprDef *array_ndx, const ExprDef *value)
689 {
690 struct xkb_private_action *act = &action->priv;
691
692 if (field == ACTION_FIELD_TYPE) {
693 int64_t type = 0;
694
695 if (array_ndx)
696 return ReportActionNotArray(ctx, action->type, field);
697
698 if (!ExprResolveInteger(ctx, value, &type))
699 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE,
700 ACTION_TYPE_PRIVATE, field, "integer");
701
702 if (type < 0 || type > 255) {
703 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
704 "Private action type must be in the range 0..255; "
705 "Illegal type %"PRId64" ignored\n", type);
706 return false;
707 }
708
709 /*
710 * It's possible for someone to write something like this:
711 * actions = [ Private(type=3,data[0]=1,data[1]=3,data[2]=3) ]
712 * where the type refers to some existing action type, e.g. LockMods.
713 * This assumes that this action's struct is laid out in memory
714 * exactly as described in the XKB specification and libraries.
715 * We, however, have changed these structs in various ways, so this
716 * assumption is no longer true. Since this is a lousy "feature", we
717 * make actions like these no-ops for now.
718 */
719 if (type < ACTION_TYPE_PRIVATE) {
720 log_info(ctx, XKB_LOG_MESSAGE_NO_ID,
721 "Private actions of type %s are not supported; Ignored\n",
722 ActionTypeText((enum xkb_action_type) type));
723 act->type = ACTION_TYPE_NONE;
724 }
725 else {
726 act->type = (enum xkb_action_type) type;
727 }
728
729 return true;
730 }
731 else if (field == ACTION_FIELD_DATA) {
732 if (array_ndx == NULL) {
733 xkb_atom_t val = XKB_ATOM_NONE;
734
735 if (!ExprResolveString(ctx, value, &val))
736 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE,
737 action->type, field, "string");
738
739 const char *str = xkb_atom_text(ctx, val);
740 size_t len = strlen(str);
741 if (len < 1 || len > sizeof(act->data)) {
742 log_warn(ctx, XKB_LOG_MESSAGE_NO_ID,
743 "A private action has %zu data bytes; "
744 "Illegal data ignored\n", sizeof(act->data));
745 return false;
746 }
747
748 /* act->data may not be null-terminated, this is intentional */
749 memset(act->data, 0, sizeof(act->data));
750 memcpy(act->data, str, len);
751 return true;
752 }
753 else {
754 int64_t ndx = 0, datum = 0;
755
756 if (!ExprResolveInteger(ctx, array_ndx, &ndx)) {
757 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
758 "Array subscript must be integer; "
759 "Illegal subscript ignored\n");
760 return false;
761 }
762
763 if (ndx < 0 || (size_t) ndx >= sizeof(act->data)) {
764 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
765 "The data for a private action is %zu bytes long; "
766 "Attempt to use data[%"PRId64"] ignored\n",
767 sizeof(act->data), ndx);
768 return false;
769 }
770
771 if (!ExprResolveInteger(ctx, value, &datum))
772 return ReportMismatch(ctx, XKB_ERROR_WRONG_FIELD_TYPE, act->type,
773 field, "integer");
774
775 if (datum < 0 || datum > 255) {
776 log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
777 "All data for a private action must be 0..255; "
778 "Illegal datum %"PRId64" ignored\n", datum);
779 return false;
780 }
781
782 act->data[ndx] = (uint8_t) datum;
783 return true;
784 }
785 }
786
787 return ReportIllegal(ctx, ACTION_TYPE_NONE, field);
788 }
789
790 typedef bool (*actionHandler)(struct xkb_context *ctx,
791 enum xkb_keymap_format format,
792 const struct xkb_mod_set *mods,
793 union xkb_action *action,
794 enum action_field field,
795 const ExprDef *array_ndx,
796 const ExprDef *value);
797
798 static const actionHandler handleAction[_ACTION_TYPE_NUM_ENTRIES] = {
799 [ACTION_TYPE_NONE] = HandleNoAction,
800 [ACTION_TYPE_VOID] = HandleNoAction,
801 [ACTION_TYPE_MOD_SET] = HandleSetLatchLockMods,
802 [ACTION_TYPE_MOD_LATCH] = HandleSetLatchLockMods,
803 [ACTION_TYPE_MOD_LOCK] = HandleSetLatchLockMods,
804 [ACTION_TYPE_GROUP_SET] = HandleSetLatchLockGroup,
805 [ACTION_TYPE_GROUP_LATCH] = HandleSetLatchLockGroup,
806 [ACTION_TYPE_GROUP_LOCK] = HandleSetLatchLockGroup,
807 [ACTION_TYPE_PTR_MOVE] = HandleMovePtr,
808 [ACTION_TYPE_PTR_BUTTON] = HandlePtrBtn,
809 [ACTION_TYPE_PTR_LOCK] = HandlePtrBtn,
810 [ACTION_TYPE_PTR_DEFAULT] = HandleSetPtrDflt,
811 [ACTION_TYPE_TERMINATE] = HandleNoAction,
812 [ACTION_TYPE_SWITCH_VT] = HandleSwitchScreen,
813 [ACTION_TYPE_CTRL_SET] = HandleSetLockControls,
814 [ACTION_TYPE_CTRL_LOCK] = HandleSetLockControls,
815 [ACTION_TYPE_UNSUPPORTED_LEGACY] = HandleUnsupportedLegacy,
816 [ACTION_TYPE_PRIVATE] = HandlePrivate,
817 };
818
819 /* Ensure to not miss `xkb_action_type` updates */
820 static_assert(ACTION_TYPE_INTERNAL == 18 &&
821 ACTION_TYPE_INTERNAL + 1 == _ACTION_TYPE_NUM_ENTRIES,
822 "Missing action type");
823
824 /***====================================================================***/
825
826 bool
827 HandleActionDef(struct xkb_context *ctx, enum xkb_keymap_format format,
828 ActionsInfo *info, const struct xkb_mod_set *mods, ExprDef *def,
829 union xkb_action *action)
830 {
831 if (def->common.type != STMT_EXPR_ACTION_DECL) {
832 log_err(ctx, XKB_ERROR_WRONG_FIELD_TYPE,
833 "Expected an action definition, found %s\n",
834 stmt_type_to_string(def->common.type));
835 return false;
836 }
837
838 const char *action_name = xkb_atom_text(ctx, def->action.name);
839 enum xkb_action_type handler_type;
840 if (!stringToActionType(action_name, &handler_type)) {
841 log_err(ctx, XKB_ERROR_UNKNOWN_ACTION_TYPE,
842 "Unknown action %s\n", action_name);
843 return false;
844 }
845
846 /*
847 * Get the default values for this action type, as modified by
848 * statements such as:
849 * latchMods.clearLocks = True;
850 */
851 *action = info->actions[handler_type];
852
853 if (handler_type == ACTION_TYPE_UNSUPPORTED_LEGACY) {
854 log_warn(ctx, XKB_WARNING_UNSUPPORTED_LEGACY_ACTION,
855 "Unsupported legacy action type \"%s\".\n", action_name);
856 /*
857 * Degrade to an uneffective supported action type.
858 * Fields will still be processed with the original action type.
859 */
860 action->type = ACTION_TYPE_NONE;
861 /* FIXME: Degrading to NoAction() has the following drawbacks:
862 * - It disables overwriting a previous action.
863 * - It enables interpretations. */
864 }
865
866 /*
867 * Now change the action properties as specified for this
868 * particular instance, e.g. "modifiers" and "clearLocks" in:
869 * SetMods(modifiers=Alt,clearLocks);
870 */
871 for (ExprDef *arg = def->action.args; arg != NULL;
872 arg = (ExprDef *) arg->common.next) {
873 const ExprDef *value;
874 ExprDef *field, *arrayRtrn;
875 const char *elemRtrn, *fieldRtrn;
876
877 if (arg->common.type == STMT_EXPR_ASSIGN) {
878 field = arg->binary.left;
879 value = arg->binary.right;
880 }
881 else if (arg->common.type == STMT_EXPR_NOT || arg->common.type == STMT_EXPR_INVERT) {
882 field = arg->unary.child;
883 value = (const ExprDef *) &constFalse;
884 }
885 else {
886 field = arg;
887 value = (const ExprDef *) &constTrue;
888 }
889
890 if (!ExprResolveLhs(ctx, field, &elemRtrn, &fieldRtrn, &arrayRtrn))
891 return false;
892
893 if (elemRtrn) {
894 log_err(ctx, XKB_ERROR_GLOBAL_DEFAULTS_WRONG_SCOPE,
895 "Cannot change defaults in an action definition; "
896 "Ignoring attempt to change \"%s.%s\".\n",
897 elemRtrn, fieldRtrn);
898 return false;
899 }
900
901 enum action_field fieldNdx;
902 if (!stringToField(fieldRtrn, &fieldNdx)) {
903 log_err(ctx, XKB_ERROR_INVALID_ACTION_FIELD,
904 "Unknown field name %s\n", fieldRtrn);
905 return false;
906 }
907
908 if (!handleAction[handler_type](ctx, format, mods, action, fieldNdx,
909 arrayRtrn, value))
910 return false;
911 }
912
913 return true;
914 }
915
916 bool
917 SetDefaultActionField(struct xkb_context *ctx, enum xkb_keymap_format format,
918 ActionsInfo *info, struct xkb_mod_set *mods,
919 const char *elem, const char *field, ExprDef *array_ndx,
920 ExprDef *value, enum merge_mode merge)
921 {
922 enum xkb_action_type action;
923 if (!stringToActionType(elem, &action)) {
924 log_err(ctx, XKB_ERROR_UNKNOWN_ACTION_TYPE, "Unknown action %s\n", elem);
925 return false;
926 }
927
928 enum action_field action_field;
929 if (!stringToField(field, &action_field)) {
930 log_err(ctx, XKB_ERROR_INVALID_ACTION_FIELD,
931 "\"%s\" is not a legal field name\n", field);
932 return false;
933 }
934
935 union xkb_action* const into = &info->actions[action];
936 /* Initialize with current defaults to enable comparison, see aftwerwards */
937 union xkb_action from = *into;
938
939 /* Parse action */
940 if (!handleAction[action](ctx, format, mods, &from, action_field, array_ndx, value))
941 return false;
942
943 /*
944 * Merge action with its corresponding default
945 *
946 * NOTE: Contrary to other items, actions do not have a “defined” field, so
947 * we fallback to compare all the actions fields. The drawback is that it
948 * overmatches, as even setting an *explicit* default value for the first
949 * time (and different from the “factory” default) would *always* display a
950 * warning. So we guard the logging with a high verbosity, as best effort
951 * mitigation.
952 */
953 if (!action_equal(into, &from)) {
954 const bool replace = (merge != MERGE_AUGMENT);
955 log_vrb(ctx, XKB_LOG_VERBOSITY_VERBOSE, XKB_LOG_MESSAGE_NO_ID,
956 "Conflicting field \"%s\" for default action \"%s\"; "
957 "Using %s, ignore %s\n",
958 fieldText(action_field), ActionTypeText(action),
959 (replace ? "from" : "into"), (replace ? "into" : "from"));
960 if (replace)
961 *into = from;
962 }
963 return true;
964 }