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