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