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