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