action: add a common CheckBooleanFlag function
[platform/upstream/libxkbcommon.git] / src / xkbcomp / action.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
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.
15  *
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.
24  *
25  ********************************************************/
26
27 /*
28  * Copyright © 2012 Intel Corporation
29  * Copyright © 2012 Ran Benita <ran234@gmail.com>
30  *
31  * Permission is hereby granted, free of charge, to any person obtaining a
32  * copy of this software and associated documentation files (the "Software"),
33  * to deal in the Software without restriction, including without limitation
34  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35  * and/or sell copies of the Software, and to permit persons to whom the
36  * Software is furnished to do so, subject to the following conditions:
37  *
38  * The above copyright notice and this permission notice (including the next
39  * paragraph) shall be included in all copies or substantial portions of the
40  * Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
45  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48  * DEALINGS IN THE SOFTWARE.
49  *
50  * Author: Daniel Stone <daniel@fooishbar.org>
51  *         Ran Benita <ran234@gmail.com>
52  */
53
54 #include "xkbcomp-priv.h"
55 #include "text.h"
56 #include "expr.h"
57 #include "action.h"
58
59 static const ExprBoolean constTrue = {
60     .expr = {
61         .common = { .type = STMT_EXPR, .next = NULL },
62         .op = EXPR_VALUE,
63         .value_type = EXPR_TYPE_BOOLEAN,
64     },
65     .set = true,
66 };
67
68 static const ExprBoolean constFalse = {
69     .expr = {
70         .common = { .type = STMT_EXPR, .next = NULL },
71         .op = EXPR_VALUE,
72         .value_type = EXPR_TYPE_BOOLEAN,
73     },
74     .set = false,
75 };
76
77 enum action_field {
78     ACTION_FIELD_CLEAR_LOCKS,
79     ACTION_FIELD_LATCH_TO_LOCK,
80     ACTION_FIELD_GEN_KEY_EVENT,
81     ACTION_FIELD_REPORT,
82     ACTION_FIELD_DEFAULT,
83     ACTION_FIELD_AFFECT,
84     ACTION_FIELD_INCREMENT,
85     ACTION_FIELD_MODIFIERS,
86     ACTION_FIELD_GROUP,
87     ACTION_FIELD_X,
88     ACTION_FIELD_Y,
89     ACTION_FIELD_ACCEL,
90     ACTION_FIELD_BUTTON,
91     ACTION_FIELD_VALUE,
92     ACTION_FIELD_CONTROLS,
93     ACTION_FIELD_TYPE,
94     ACTION_FIELD_COUNT,
95     ACTION_FIELD_SCREEN,
96     ACTION_FIELD_SAME,
97     ACTION_FIELD_DATA,
98     ACTION_FIELD_DEVICE,
99     ACTION_FIELD_KEYCODE,
100     ACTION_FIELD_MODS_TO_CLEAR,
101 };
102
103 ActionsInfo *
104 NewActionsInfo(void)
105 {
106     unsigned type;
107     ActionsInfo *info;
108
109     info = calloc(1, sizeof(*info));
110     if (!info)
111         return NULL;
112
113     for (type = 0; type < _ACTION_TYPE_NUM_ENTRIES; type++)
114         info->actions[type].type = type;
115
116     /* Apply some "factory defaults". */
117
118     /* Increment default button. */
119     info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.flags = 0;
120     info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.value = 1;
121     info->actions[ACTION_TYPE_PTR_MOVE].ptr.flags = ACTION_ACCEL;
122     info->actions[ACTION_TYPE_SWITCH_VT].screen.flags = ACTION_SAME_SCREEN;
123
124     return info;
125 }
126
127 void
128 FreeActionsInfo(ActionsInfo *info)
129 {
130     free(info);
131 }
132
133 static const LookupEntry fieldStrings[] = {
134     { "clearLocks",       ACTION_FIELD_CLEAR_LOCKS   },
135     { "latchToLock",      ACTION_FIELD_LATCH_TO_LOCK },
136     { "genKeyEvent",      ACTION_FIELD_GEN_KEY_EVENT },
137     { "generateKeyEvent", ACTION_FIELD_GEN_KEY_EVENT },
138     { "report",           ACTION_FIELD_REPORT        },
139     { "default",          ACTION_FIELD_DEFAULT       },
140     { "affect",           ACTION_FIELD_AFFECT        },
141     { "increment",        ACTION_FIELD_INCREMENT     },
142     { "modifiers",        ACTION_FIELD_MODIFIERS     },
143     { "mods",             ACTION_FIELD_MODIFIERS     },
144     { "group",            ACTION_FIELD_GROUP         },
145     { "x",                ACTION_FIELD_X             },
146     { "y",                ACTION_FIELD_Y             },
147     { "accel",            ACTION_FIELD_ACCEL         },
148     { "accelerate",       ACTION_FIELD_ACCEL         },
149     { "repeat",           ACTION_FIELD_ACCEL         },
150     { "button",           ACTION_FIELD_BUTTON        },
151     { "value",            ACTION_FIELD_VALUE         },
152     { "controls",         ACTION_FIELD_CONTROLS      },
153     { "ctrls",            ACTION_FIELD_CONTROLS      },
154     { "type",             ACTION_FIELD_TYPE          },
155     { "count",            ACTION_FIELD_COUNT         },
156     { "screen",           ACTION_FIELD_SCREEN        },
157     { "same",             ACTION_FIELD_SAME          },
158     { "sameServer",       ACTION_FIELD_SAME          },
159     { "data",             ACTION_FIELD_DATA          },
160     { "device",           ACTION_FIELD_DEVICE        },
161     { "dev",              ACTION_FIELD_DEVICE        },
162     { "key",              ACTION_FIELD_KEYCODE       },
163     { "keycode",          ACTION_FIELD_KEYCODE       },
164     { "kc",               ACTION_FIELD_KEYCODE       },
165     { "clearmods",        ACTION_FIELD_MODS_TO_CLEAR },
166     { "clearmodifiers",   ACTION_FIELD_MODS_TO_CLEAR },
167     { NULL,               0                          }
168 };
169
170 static bool
171 stringToAction(const char *str, unsigned *type_rtrn)
172 {
173     return LookupString(actionTypeNames, str, type_rtrn);
174 }
175
176 static bool
177 stringToField(const char *str, enum action_field *field_rtrn)
178 {
179     return LookupString(fieldStrings, str, field_rtrn);
180 }
181
182 static const char *
183 fieldText(enum action_field field)
184 {
185     return LookupValue(fieldStrings, field);
186 }
187
188 /***====================================================================***/
189
190 static inline bool
191 ReportMismatch(struct xkb_context *ctx, enum xkb_action_type action,
192                enum action_field field, const char *type)
193 {
194     log_err(ctx,
195             "Value of %s field must be of type %s; "
196             "Action %s definition ignored\n",
197             fieldText(field), type, ActionTypeText(action));
198     return false;
199 }
200
201 static inline bool
202 ReportIllegal(struct xkb_context *ctx, enum xkb_action_type action,
203               enum action_field field)
204 {
205     log_err(ctx,
206             "Field %s is not defined for an action of type %s; "
207             "Action definition ignored\n",
208             fieldText(field), ActionTypeText(action));
209     return false;
210 }
211
212 static inline bool
213 ReportActionNotArray(struct xkb_context *ctx, enum xkb_action_type action,
214                      enum action_field field)
215 {
216     log_err(ctx,
217             "The %s field in the %s action is not an array; "
218             "Action definition ignored\n",
219             fieldText(field), ActionTypeText(action));
220     return false;
221 }
222
223 static bool
224 HandleNoAction(struct xkb_keymap *keymap, union xkb_action *action,
225                enum action_field field, const ExprDef *array_ndx,
226                const ExprDef *value)
227
228 {
229     return true;
230 }
231
232 static bool
233 CheckBooleanFlag(struct xkb_context *ctx, enum xkb_action_type action,
234                  enum action_field field, enum xkb_action_flags flag,
235                  const ExprDef *array_ndx, const ExprDef *value,
236                  enum xkb_action_flags *flags_inout)
237 {
238     bool set;
239
240     if (array_ndx)
241         return ReportActionNotArray(ctx, action, field);
242
243     if (!ExprResolveBoolean(ctx, value, &set))
244         return ReportMismatch(ctx, action, field, "boolean");
245
246     if (set)
247         *flags_inout |= flag;
248     else
249         *flags_inout &= ~flag;
250
251     return true;
252 }
253
254 static bool
255 CheckModifierField(struct xkb_keymap *keymap, enum xkb_action_type action,
256                    const ExprDef *array_ndx, const ExprDef *value,
257                    enum xkb_action_flags *flags_inout, xkb_mod_mask_t *mods_rtrn)
258 {
259     if (array_ndx)
260         return ReportActionNotArray(keymap->ctx, action, ACTION_FIELD_MODIFIERS);
261
262     if (value->expr.op == EXPR_IDENT) {
263         const char *valStr;
264         valStr = xkb_atom_text(keymap->ctx, value->ident.ident);
265         if (valStr && (istreq(valStr, "usemodmapmods") ||
266                        istreq(valStr, "modmapmods"))) {
267             *mods_rtrn = 0;
268             *flags_inout |= ACTION_MODS_LOOKUP_MODMAP;
269             return true;
270         }
271     }
272
273     if (!ExprResolveModMask(keymap, value, MOD_BOTH, mods_rtrn))
274         return ReportMismatch(keymap->ctx, action,
275                               ACTION_FIELD_MODIFIERS, "modifier mask");
276
277     *flags_inout &= ~ACTION_MODS_LOOKUP_MODMAP;
278     return true;
279 }
280
281 static bool
282 HandleSetLatchMods(struct xkb_keymap *keymap, union xkb_action *action,
283                    enum action_field field, const ExprDef *array_ndx,
284                    const ExprDef *value)
285 {
286     struct xkb_mod_action *act = &action->mods;
287
288     if (field == ACTION_FIELD_CLEAR_LOCKS)
289         return CheckBooleanFlag(keymap->ctx, action->type, field,
290                                 ACTION_LOCK_CLEAR, array_ndx, value,
291                                 &act->flags);
292     if (field == ACTION_FIELD_LATCH_TO_LOCK)
293         return CheckBooleanFlag(keymap->ctx, action->type, field,
294                                 ACTION_LATCH_TO_LOCK, array_ndx, value,
295                                 &act->flags);
296     if (field == ACTION_FIELD_MODIFIERS)
297         return CheckModifierField(keymap, action->type, array_ndx, value,
298                                   &act->flags, &act->mods.mods);
299
300     return ReportIllegal(keymap->ctx, action->type, field);
301 }
302
303 static const LookupEntry lockWhich[] = {
304     { "both", 0 },
305     { "lock", ACTION_LOCK_NO_UNLOCK },
306     { "neither", (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK) },
307     { "unlock", ACTION_LOCK_NO_LOCK },
308     { NULL, 0 }
309 };
310
311 static bool
312 CheckAffectField(struct xkb_context *ctx, enum xkb_action_type action,
313                  const ExprDef *array_ndx, const ExprDef *value,
314                  enum xkb_action_flags *flags_inout)
315 {
316     enum xkb_action_flags flags;
317
318     if (array_ndx)
319         return ReportActionNotArray(ctx, action, ACTION_FIELD_AFFECT);
320
321     if (!ExprResolveEnum(ctx, value, &flags, lockWhich))
322         return ReportMismatch(ctx, action, ACTION_FIELD_AFFECT,
323                               "lock, unlock, both, neither");
324
325     *flags_inout &= ~(ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK);
326     *flags_inout |= flags;
327     return true;
328 }
329
330 static bool
331 HandleLockMods(struct xkb_keymap *keymap, union xkb_action *action,
332                enum action_field field, const ExprDef *array_ndx,
333                const ExprDef *value)
334 {
335     struct xkb_mod_action *act = &action->mods;
336
337     if (field == ACTION_FIELD_MODIFIERS)
338         return CheckModifierField(keymap, action->type, array_ndx, value,
339                                   &act->flags, &act->mods.mods);
340     else if (field == ACTION_FIELD_AFFECT)
341         return CheckAffectField(keymap->ctx, action->type, array_ndx, value,
342                                 &act->flags);
343
344     return ReportIllegal(keymap->ctx, action->type, field);
345 }
346
347 static bool
348 CheckGroupField(struct xkb_context *ctx, unsigned action,
349                 const ExprDef *array_ndx, const ExprDef *value,
350                 enum xkb_action_flags *flags_inout, int32_t *group_rtrn)
351 {
352     const ExprDef *spec;
353     xkb_layout_index_t idx;
354     enum xkb_action_flags flags = *flags_inout;
355
356     if (array_ndx)
357         return ReportActionNotArray(ctx, action, ACTION_FIELD_GROUP);
358
359     if (value->expr.op == EXPR_NEGATE || value->expr.op == EXPR_UNARY_PLUS) {
360         flags &= ~ACTION_ABSOLUTE_SWITCH;
361         spec = value->unary.child;
362     }
363     else {
364         flags |= ACTION_ABSOLUTE_SWITCH;
365         spec = value;
366     }
367
368     if (!ExprResolveGroup(ctx, spec, &idx))
369         return ReportMismatch(ctx, action, ACTION_FIELD_GROUP,
370                               "integer (range 1..8)");
371
372     /* +n, -n are relative, n is absolute. */
373     if (value->expr.op == EXPR_NEGATE || value->expr.op == EXPR_UNARY_PLUS) {
374         *group_rtrn = (int32_t) idx;
375         if (value->expr.op == EXPR_NEGATE)
376             *group_rtrn = -*group_rtrn;
377     }
378     else {
379         *group_rtrn = (int32_t) (idx - 1);
380     }
381     *flags_inout = flags;
382     return true;
383 }
384
385 static bool
386 HandleSetLatchGroup(struct xkb_keymap *keymap, union xkb_action *action,
387                     enum action_field field, const ExprDef *array_ndx,
388                     const ExprDef *value)
389 {
390     struct xkb_group_action *act = &action->group;
391
392     if (field == ACTION_FIELD_CLEAR_LOCKS)
393         return CheckBooleanFlag(keymap->ctx, action->type, field,
394                                 ACTION_LOCK_CLEAR, array_ndx, value,
395                                 &act->flags);
396     if (field == ACTION_FIELD_LATCH_TO_LOCK)
397         return CheckBooleanFlag(keymap->ctx, action->type, field,
398                                 ACTION_LATCH_TO_LOCK, array_ndx, value,
399                                 &act->flags);
400     if (field == ACTION_FIELD_GROUP)
401         return CheckGroupField(keymap->ctx, action->type, array_ndx, value,
402                                &act->flags, &act->group);
403
404     return ReportIllegal(keymap->ctx, action->type, field);
405 }
406
407 static bool
408 HandleLockGroup(struct xkb_keymap *keymap, union xkb_action *action,
409                 enum action_field field, const ExprDef *array_ndx,
410                 const ExprDef *value)
411 {
412     struct xkb_group_action *act = &action->group;
413
414     if (field == ACTION_FIELD_GROUP)
415         return CheckGroupField(keymap->ctx, action->type, array_ndx, value,
416                                &act->flags, &act->group);
417
418     return ReportIllegal(keymap->ctx, action->type, field);
419 }
420
421 static bool
422 HandleMovePtr(struct xkb_keymap *keymap, union xkb_action *action,
423               enum action_field field, const ExprDef *array_ndx,
424               const ExprDef *value)
425 {
426     struct xkb_pointer_action *act = &action->ptr;
427
428     if (field == ACTION_FIELD_X || field == ACTION_FIELD_Y) {
429         int val;
430         const bool absolute = (value->expr.op != EXPR_NEGATE &&
431                                value->expr.op != EXPR_UNARY_PLUS);
432
433         if (array_ndx)
434             return ReportActionNotArray(keymap->ctx, action->type, field);
435
436         if (!ExprResolveInteger(keymap->ctx, value, &val))
437             return ReportMismatch(keymap->ctx, action->type, field, "integer");
438
439         if (val < INT16_MIN || val > INT16_MAX) {
440             log_err(keymap->ctx,
441                     "The %s field in the %s action must be in range %d..%d; "
442                     "Action definition ignored\n",
443                     fieldText(field), ActionTypeText(action->type),
444                     INT16_MIN, INT16_MAX);
445             return false;
446         }
447
448         if (field == ACTION_FIELD_X) {
449             if (absolute)
450                 act->flags |= ACTION_ABSOLUTE_X;
451             act->x = (int16_t) val;
452         }
453         else {
454             if (absolute)
455                 act->flags |= ACTION_ABSOLUTE_Y;
456             act->y = (int16_t) val;
457         }
458
459         return true;
460     }
461     else if (field == ACTION_FIELD_ACCEL) {
462         return CheckBooleanFlag(keymap->ctx, action->type, field,
463                                 ACTION_ACCEL, array_ndx, value, &act->flags);
464     }
465
466     return ReportIllegal(keymap->ctx, action->type, field);
467 }
468
469 static bool
470 HandlePtrBtn(struct xkb_keymap *keymap, union xkb_action *action,
471              enum action_field field, const ExprDef *array_ndx,
472              const ExprDef *value)
473 {
474     struct xkb_pointer_button_action *act = &action->btn;
475
476     if (field == ACTION_FIELD_BUTTON) {
477         int btn;
478
479         if (array_ndx)
480             return ReportActionNotArray(keymap->ctx, action->type, field);
481
482         if (!ExprResolveButton(keymap->ctx, value, &btn))
483             return ReportMismatch(keymap->ctx, action->type, field,
484                                   "integer (range 1..5)");
485
486         if (btn < 0 || btn > 5) {
487             log_err(keymap->ctx,
488                     "Button must specify default or be in the range 1..5; "
489                     "Illegal button value %d ignored\n", btn);
490             return false;
491         }
492
493         act->button = btn;
494         return true;
495     }
496     else if (action->type == ACTION_TYPE_PTR_LOCK &&
497              field == ACTION_FIELD_AFFECT) {
498         return CheckAffectField(keymap->ctx, action->type, array_ndx, value,
499                                 &act->flags);
500     }
501     else if (field == ACTION_FIELD_COUNT) {
502         int val;
503
504         if (array_ndx)
505             return ReportActionNotArray(keymap->ctx, action->type, field);
506
507         if (!ExprResolveInteger(keymap->ctx, value, &val))
508             return ReportMismatch(keymap->ctx, action->type, field, "integer");
509
510         if (val < 0 || val > 255) {
511             log_err(keymap->ctx,
512                     "The count field must have a value in the range 0..255; "
513                     "Illegal count %d ignored\n", val);
514             return false;
515         }
516
517         act->count = (uint8_t) val;
518         return true;
519     }
520
521     return ReportIllegal(keymap->ctx, action->type, field);
522 }
523
524 static const LookupEntry ptrDflts[] = {
525     { "dfltbtn", 1 },
526     { "defaultbutton", 1 },
527     { "button", 1 },
528     { NULL, 0 }
529 };
530
531 static bool
532 HandleSetPtrDflt(struct xkb_keymap *keymap, union xkb_action *action,
533                  enum action_field field, const ExprDef *array_ndx,
534                  const ExprDef *value)
535 {
536     struct xkb_pointer_default_action *act = &action->dflt;
537
538     if (field == ACTION_FIELD_AFFECT) {
539         unsigned int val;
540
541         if (array_ndx)
542             return ReportActionNotArray(keymap->ctx, action->type, field);
543
544         if (!ExprResolveEnum(keymap->ctx, value, &val, ptrDflts))
545             return ReportMismatch(keymap->ctx, action->type, field,
546                                   "pointer component");
547         return true;
548     }
549     else if (field == ACTION_FIELD_BUTTON || field == ACTION_FIELD_VALUE) {
550         const ExprDef *button;
551         int btn;
552
553         if (array_ndx)
554             return ReportActionNotArray(keymap->ctx, action->type, field);
555
556         if (value->expr.op == EXPR_NEGATE ||
557             value->expr.op == EXPR_UNARY_PLUS) {
558             act->flags &= ~ACTION_ABSOLUTE_SWITCH;
559             button = value->unary.child;
560         }
561         else {
562             act->flags |= ACTION_ABSOLUTE_SWITCH;
563             button = value;
564         }
565
566         if (!ExprResolveButton(keymap->ctx, button, &btn))
567             return ReportMismatch(keymap->ctx, action->type, field,
568                                   "integer (range 1..5)");
569
570         if (btn < 0 || btn > 5) {
571             log_err(keymap->ctx,
572                     "New default button value must be in the range 1..5; "
573                     "Illegal default button value %d ignored\n", btn);
574             return false;
575         }
576         if (btn == 0) {
577             log_err(keymap->ctx,
578                     "Cannot set default pointer button to \"default\"; "
579                     "Illegal default button setting ignored\n");
580             return false;
581         }
582
583         act->value = (value->expr.op == EXPR_NEGATE ? -btn: btn);
584         return true;
585     }
586
587     return ReportIllegal(keymap->ctx, action->type, field);
588 }
589
590 static bool
591 HandleSwitchScreen(struct xkb_keymap *keymap, union xkb_action *action,
592                    enum action_field field, const ExprDef *array_ndx,
593                    const ExprDef *value)
594 {
595     struct xkb_switch_screen_action *act = &action->screen;
596
597     if (field == ACTION_FIELD_SCREEN) {
598         const ExprDef *scrn;
599         int val;
600
601         if (array_ndx)
602             return ReportActionNotArray(keymap->ctx, action->type, field);
603
604         if (value->expr.op == EXPR_NEGATE ||
605             value->expr.op == EXPR_UNARY_PLUS) {
606             act->flags &= ~ACTION_ABSOLUTE_SWITCH;
607             scrn = value->unary.child;
608         }
609         else {
610             act->flags |= ACTION_ABSOLUTE_SWITCH;
611             scrn = value;
612         }
613
614         if (!ExprResolveInteger(keymap->ctx, scrn, &val))
615             return ReportMismatch(keymap->ctx, action->type, field,
616                                   "integer (0..255)");
617
618         if (val < 0 || val > 255) {
619             log_err(keymap->ctx,
620                     "Screen index must be in the range 1..255; "
621                     "Illegal screen value %d ignored\n", val);
622             return false;
623         }
624
625         act->screen = (value->expr.op == EXPR_NEGATE ? -val : val);
626         return true;
627     }
628     else if (field == ACTION_FIELD_SAME) {
629         return CheckBooleanFlag(keymap->ctx, action->type, field,
630                                 ACTION_SAME_SCREEN, array_ndx, value,
631                                 &act->flags);
632     }
633
634     return ReportIllegal(keymap->ctx, action->type, field);
635 }
636
637 static bool
638 HandleSetLockControls(struct xkb_keymap *keymap, union xkb_action *action,
639                       enum action_field field, const ExprDef *array_ndx,
640                       const ExprDef *value)
641 {
642     struct xkb_controls_action *act = &action->ctrls;
643
644     if (field == ACTION_FIELD_CONTROLS) {
645         unsigned int mask;
646
647         if (array_ndx)
648             return ReportActionNotArray(keymap->ctx, action->type, field);
649
650         if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlMaskNames))
651             return ReportMismatch(keymap->ctx, action->type, field,
652                                   "controls mask");
653
654         act->ctrls = mask;
655         return true;
656     }
657     else if (field == ACTION_FIELD_AFFECT) {
658         return CheckAffectField(keymap->ctx, action->type, array_ndx, value,
659                                 &act->flags);
660     }
661
662     return ReportIllegal(keymap->ctx, action->type, field);
663 }
664
665 static bool
666 HandlePrivate(struct xkb_keymap *keymap, union xkb_action *action,
667               enum action_field field, const ExprDef *array_ndx,
668               const ExprDef *value)
669 {
670     struct xkb_private_action *act = &action->priv;
671
672     if (field == ACTION_FIELD_TYPE) {
673         int type;
674
675         if (array_ndx)
676             return ReportActionNotArray(keymap->ctx, action->type, field);
677
678         if (!ExprResolveInteger(keymap->ctx, value, &type))
679             return ReportMismatch(keymap->ctx, ACTION_TYPE_PRIVATE, field, "integer");
680
681         if (type < 0 || type > 255) {
682             log_err(keymap->ctx,
683                     "Private action type must be in the range 0..255; "
684                     "Illegal type %d ignored\n", type);
685             return false;
686         }
687
688         /*
689          * It's possible for someone to write something like this:
690          *      actions = [ Private(type=3,data[0]=1,data[1]=3,data[2]=3) ]
691          * where the type refers to some existing action type, e.g. LockMods.
692          * This assumes that this action's struct is laid out in memory
693          * exactly as described in the XKB specification and libraries.
694          * We, however, have changed these structs in various ways, so this
695          * assumption is no longer true. Since this is a lousy "feature", we
696          * make actions like these no-ops for now.
697          */
698         if (type < ACTION_TYPE_PRIVATE) {
699             log_info(keymap->ctx,
700                      "Private actions of type %s are not supported; Ignored\n",
701                      ActionTypeText(type));
702             act->type = ACTION_TYPE_NONE;
703         }
704         else {
705             act->type = (enum xkb_action_type) type;
706         }
707
708         return true;
709     }
710     else if (field == ACTION_FIELD_DATA) {
711         if (array_ndx == NULL) {
712             xkb_atom_t val;
713             const char *str;
714             size_t len;
715
716             if (!ExprResolveString(keymap->ctx, value, &val))
717                 return ReportMismatch(keymap->ctx, action->type, field, "string");
718
719             str = xkb_atom_text(keymap->ctx, val);
720             len = strlen(str);
721             if (len < 1 || len > 7) {
722                 log_warn(keymap->ctx,
723                          "A private action has 7 data bytes; "
724                          "Illegal data ignored\n");
725                 return false;
726             }
727
728             strncpy((char *) act->data, str, sizeof(act->data));
729             return true;
730         }
731         else {
732             int ndx, datum;
733
734             if (!ExprResolveInteger(keymap->ctx, array_ndx, &ndx)) {
735                 log_err(keymap->ctx,
736                         "Array subscript must be integer; "
737                         "Illegal subscript ignored\n");
738                 return false;
739             }
740
741             if (ndx < 0 || (size_t) ndx >= sizeof(act->data)) {
742                 log_err(keymap->ctx,
743                         "The data for a private action is %lu bytes long; "
744                         "Attempt to use data[%d] ignored\n",
745                         (unsigned long) sizeof(act->data), ndx);
746                 return false;
747             }
748
749             if (!ExprResolveInteger(keymap->ctx, value, &datum))
750                 return ReportMismatch(keymap->ctx, act->type, field, "integer");
751
752             if (datum < 0 || datum > 255) {
753                 log_err(keymap->ctx,
754                         "All data for a private action must be 0..255; "
755                         "Illegal datum %d ignored\n", datum);
756                 return false;
757             }
758
759             act->data[ndx] = (uint8_t) datum;
760             return true;
761         }
762     }
763
764     return ReportIllegal(keymap->ctx, ACTION_TYPE_NONE, field);
765 }
766
767 typedef bool (*actionHandler)(struct xkb_keymap *keymap,
768                               union xkb_action *action,
769                               enum action_field field,
770                               const ExprDef *array_ndx,
771                               const ExprDef *value);
772
773 static const actionHandler handleAction[_ACTION_TYPE_NUM_ENTRIES] = {
774     [ACTION_TYPE_NONE] = HandleNoAction,
775     [ACTION_TYPE_MOD_SET] = HandleSetLatchMods,
776     [ACTION_TYPE_MOD_LATCH] = HandleSetLatchMods,
777     [ACTION_TYPE_MOD_LOCK] = HandleLockMods,
778     [ACTION_TYPE_GROUP_SET] = HandleSetLatchGroup,
779     [ACTION_TYPE_GROUP_LATCH] = HandleSetLatchGroup,
780     [ACTION_TYPE_GROUP_LOCK] = HandleLockGroup,
781     [ACTION_TYPE_PTR_MOVE] = HandleMovePtr,
782     [ACTION_TYPE_PTR_BUTTON] = HandlePtrBtn,
783     [ACTION_TYPE_PTR_LOCK] = HandlePtrBtn,
784     [ACTION_TYPE_PTR_DEFAULT] = HandleSetPtrDflt,
785     [ACTION_TYPE_TERMINATE] = HandleNoAction,
786     [ACTION_TYPE_SWITCH_VT] = HandleSwitchScreen,
787     [ACTION_TYPE_CTRL_SET] = HandleSetLockControls,
788     [ACTION_TYPE_CTRL_LOCK] = HandleSetLockControls,
789     [ACTION_TYPE_PRIVATE] = HandlePrivate,
790 };
791
792 /***====================================================================***/
793
794 bool
795 HandleActionDef(ExprDef *def, struct xkb_keymap *keymap,
796                 union xkb_action *action, ActionsInfo *info)
797 {
798     ExprDef *arg;
799     const char *str;
800     unsigned handler_type;
801
802     if (def->expr.op != EXPR_ACTION_DECL) {
803         log_err(keymap->ctx, "Expected an action definition, found %s\n",
804                 expr_op_type_to_string(def->expr.op));
805         return false;
806     }
807
808     str = xkb_atom_text(keymap->ctx, def->action.name);
809     if (!stringToAction(str, &handler_type)) {
810         log_err(keymap->ctx, "Unknown action %s\n", str);
811         return false;
812     }
813
814     /*
815      * Get the default values for this action type, as modified by
816      * statements such as:
817      *     latchMods.clearLocks = True;
818      */
819     *action = info->actions[handler_type];
820
821     /*
822      * Now change the action properties as specified for this
823      * particular instance, e.g. "modifiers" and "clearLocks" in:
824      *     SetMods(modifiers=Alt,clearLocks);
825      */
826     for (arg = def->action.args; arg != NULL;
827          arg = (ExprDef *) arg->common.next) {
828         const ExprDef *value;
829         ExprDef *field, *arrayRtrn;
830         const char *elemRtrn, *fieldRtrn;
831         enum action_field fieldNdx;
832
833         if (arg->expr.op == EXPR_ASSIGN) {
834             field = arg->binary.left;
835             value = arg->binary.right;
836         }
837         else if (arg->expr.op == EXPR_NOT || arg->expr.op == EXPR_INVERT) {
838             field = arg->unary.child;
839             value = (const ExprDef *) &constFalse;
840         }
841         else {
842             field = arg;
843             value = (const ExprDef *) &constTrue;
844         }
845
846         if (!ExprResolveLhs(keymap->ctx, field, &elemRtrn, &fieldRtrn,
847                             &arrayRtrn))
848             return false;
849
850         if (elemRtrn) {
851             log_err(keymap->ctx,
852                     "Cannot change defaults in an action definition; "
853                     "Ignoring attempt to change %s.%s\n",
854                     elemRtrn, fieldRtrn);
855             return false;
856         }
857
858         if (!stringToField(fieldRtrn, &fieldNdx)) {
859             log_err(keymap->ctx, "Unknown field name %s\n", fieldRtrn);
860             return false;
861         }
862
863         if (!handleAction[handler_type](keymap, action, fieldNdx, arrayRtrn,
864                                         value))
865             return false;
866     }
867
868     return true;
869 }
870
871 bool
872 SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field,
873                ExprDef *array_ndx, ExprDef *value, ActionsInfo *info)
874 {
875     unsigned action;
876     enum action_field action_field;
877
878     if (!stringToAction(elem, &action))
879         return false;
880
881     if (!stringToField(field, &action_field)) {
882         log_err(keymap->ctx, "\"%s\" is not a legal field name\n", field);
883         return false;
884     }
885
886     return handleAction[action](keymap, &info->actions[action],
887                                 action_field, array_ndx, value);
888 }