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