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