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