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