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