action: fix missing support for "affect" field
[platform/upstream/libxkbcommon.git] / src / xkbcomp / action.c
1 /************************************************************
2  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of Silicon Graphics not be
10  * used in advertising or publicity pertaining to distribution
11  * of the software without specific prior written permission.
12  * Silicon Graphics makes no representation about the suitability
13  * of this software for any purpose. It is provided "as is"
14  * without any express or implied warranty.
15  *
16  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  *
25  ********************************************************/
26
27 /*
28  * Copyright © 2012 Intel Corporation
29  * Copyright © 2012 Ran Benita <ran234@gmail.com>
30  *
31  * Permission is hereby granted, free of charge, to any person obtaining a
32  * copy of this software and associated documentation files (the "Software"),
33  * to deal in the Software without restriction, including without limitation
34  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35  * and/or sell copies of the Software, and to permit persons to whom the
36  * Software is furnished to do so, subject to the following conditions:
37  *
38  * The above copyright notice and this permission notice (including the next
39  * paragraph) shall be included in all copies or substantial portions of the
40  * Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
45  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48  * DEALINGS IN THE SOFTWARE.
49  *
50  * Author: Daniel Stone <daniel@fooishbar.org>
51  *         Ran Benita <ran234@gmail.com>
52  */
53
54 #include "xkbcomp-priv.h"
55 #include "text.h"
56 #include "expr.h"
57 #include "action.h"
58
59 static const ExprBoolean constTrue = {
60     .expr = {
61         .common = { .type = STMT_EXPR, .next = NULL },
62         .op = EXPR_VALUE,
63         .value_type = EXPR_TYPE_BOOLEAN,
64     },
65     .set = true,
66 };
67
68 static const ExprBoolean constFalse = {
69     .expr = {
70         .common = { .type = STMT_EXPR, .next = NULL },
71         .op = EXPR_VALUE,
72         .value_type = EXPR_TYPE_BOOLEAN,
73     },
74     .set = false,
75 };
76
77 enum action_field {
78     ACTION_FIELD_CLEAR_LOCKS,
79     ACTION_FIELD_LATCH_TO_LOCK,
80     ACTION_FIELD_GEN_KEY_EVENT,
81     ACTION_FIELD_REPORT,
82     ACTION_FIELD_DEFAULT,
83     ACTION_FIELD_AFFECT,
84     ACTION_FIELD_INCREMENT,
85     ACTION_FIELD_MODIFIERS,
86     ACTION_FIELD_GROUP,
87     ACTION_FIELD_X,
88     ACTION_FIELD_Y,
89     ACTION_FIELD_ACCEL,
90     ACTION_FIELD_BUTTON,
91     ACTION_FIELD_VALUE,
92     ACTION_FIELD_CONTROLS,
93     ACTION_FIELD_TYPE,
94     ACTION_FIELD_COUNT,
95     ACTION_FIELD_SCREEN,
96     ACTION_FIELD_SAME,
97     ACTION_FIELD_DATA,
98     ACTION_FIELD_DEVICE,
99     ACTION_FIELD_KEYCODE,
100     ACTION_FIELD_MODS_TO_CLEAR,
101 };
102
103 ActionsInfo *
104 NewActionsInfo(void)
105 {
106     unsigned type;
107     ActionsInfo *info;
108
109     info = calloc(1, sizeof(*info));
110     if (!info)
111         return NULL;
112
113     for (type = 0; type < _ACTION_TYPE_NUM_ENTRIES; type++)
114         info->actions[type].type = type;
115
116     /* Apply some "factory defaults". */
117
118     /* Increment default button. */
119     info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.flags = 0;
120     info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.value = 1;
121
122     return info;
123 }
124
125 void
126 FreeActionsInfo(ActionsInfo *info)
127 {
128     free(info);
129 }
130
131 static const LookupEntry fieldStrings[] = {
132     { "clearLocks",       ACTION_FIELD_CLEAR_LOCKS   },
133     { "latchToLock",      ACTION_FIELD_LATCH_TO_LOCK },
134     { "genKeyEvent",      ACTION_FIELD_GEN_KEY_EVENT },
135     { "generateKeyEvent", ACTION_FIELD_GEN_KEY_EVENT },
136     { "report",           ACTION_FIELD_REPORT        },
137     { "default",          ACTION_FIELD_DEFAULT       },
138     { "affect",           ACTION_FIELD_AFFECT        },
139     { "increment",        ACTION_FIELD_INCREMENT     },
140     { "modifiers",        ACTION_FIELD_MODIFIERS     },
141     { "mods",             ACTION_FIELD_MODIFIERS     },
142     { "group",            ACTION_FIELD_GROUP         },
143     { "x",                ACTION_FIELD_X             },
144     { "y",                ACTION_FIELD_Y             },
145     { "accel",            ACTION_FIELD_ACCEL         },
146     { "accelerate",       ACTION_FIELD_ACCEL         },
147     { "repeat",           ACTION_FIELD_ACCEL         },
148     { "button",           ACTION_FIELD_BUTTON        },
149     { "value",            ACTION_FIELD_VALUE         },
150     { "controls",         ACTION_FIELD_CONTROLS      },
151     { "ctrls",            ACTION_FIELD_CONTROLS      },
152     { "type",             ACTION_FIELD_TYPE          },
153     { "count",            ACTION_FIELD_COUNT         },
154     { "screen",           ACTION_FIELD_SCREEN        },
155     { "same",             ACTION_FIELD_SAME          },
156     { "sameServer",       ACTION_FIELD_SAME          },
157     { "data",             ACTION_FIELD_DATA          },
158     { "device",           ACTION_FIELD_DEVICE        },
159     { "dev",              ACTION_FIELD_DEVICE        },
160     { "key",              ACTION_FIELD_KEYCODE       },
161     { "keycode",          ACTION_FIELD_KEYCODE       },
162     { "kc",               ACTION_FIELD_KEYCODE       },
163     { "clearmods",        ACTION_FIELD_MODS_TO_CLEAR },
164     { "clearmodifiers",   ACTION_FIELD_MODS_TO_CLEAR },
165     { NULL,               0                          }
166 };
167
168 static bool
169 stringToAction(const char *str, unsigned *type_rtrn)
170 {
171     return LookupString(actionTypeNames, str, type_rtrn);
172 }
173
174 static bool
175 stringToField(const char *str, enum action_field *field_rtrn)
176 {
177     return LookupString(fieldStrings, str, field_rtrn);
178 }
179
180 static const char *
181 fieldText(enum action_field field)
182 {
183     return LookupValue(fieldStrings, field);
184 }
185
186 /***====================================================================***/
187
188 static inline bool
189 ReportMismatch(struct xkb_keymap *keymap, enum xkb_action_type action,
190                enum action_field field, const char *type)
191 {
192     log_err(keymap->ctx,
193             "Value of %s field must be of type %s; "
194             "Action %s definition ignored\n",
195             fieldText(field), type, ActionTypeText(action));
196     return false;
197 }
198
199 static inline bool
200 ReportIllegal(struct xkb_keymap *keymap, enum xkb_action_type action,
201               enum action_field field)
202 {
203     log_err(keymap->ctx,
204             "Field %s is not defined for an action of type %s; "
205             "Action definition ignored\n",
206             fieldText(field), ActionTypeText(action));
207     return false;
208 }
209
210 static inline bool
211 ReportActionNotArray(struct xkb_keymap *keymap, enum xkb_action_type action,
212                      enum action_field field)
213 {
214     log_err(keymap->ctx,
215             "The %s field in the %s action is not an array; "
216             "Action definition ignored\n",
217             fieldText(field), ActionTypeText(action));
218     return false;
219 }
220
221 static bool
222 HandleNoAction(struct xkb_keymap *keymap, union xkb_action *action,
223                enum action_field field, const ExprDef *array_ndx,
224                const ExprDef *value)
225
226 {
227     return true;
228 }
229
230 static bool
231 CheckLatchLockFlags(struct xkb_keymap *keymap, enum xkb_action_type action,
232                     enum action_field field, const ExprDef *value,
233                     enum xkb_action_flags *flags_inout)
234 {
235     enum xkb_action_flags tmp;
236     bool result;
237
238     if (field == ACTION_FIELD_CLEAR_LOCKS)
239         tmp = ACTION_LOCK_CLEAR;
240     else if (field == ACTION_FIELD_LATCH_TO_LOCK)
241         tmp = ACTION_LATCH_TO_LOCK;
242     else
243         return false;           /* WSGO! */
244
245     if (!ExprResolveBoolean(keymap->ctx, value, &result))
246         return ReportMismatch(keymap, action, field, "boolean");
247
248     if (result)
249         *flags_inout |= tmp;
250     else
251         *flags_inout &= ~tmp;
252
253     return true;
254 }
255
256 static bool
257 CheckModifierField(struct xkb_keymap *keymap, enum xkb_action_type action,
258                    const ExprDef *value, enum xkb_action_flags *flags_inout,
259                    xkb_mod_mask_t *mods_rtrn)
260 {
261     if (value->expr.op == EXPR_IDENT) {
262         const char *valStr;
263         valStr = xkb_atom_text(keymap->ctx, value->ident.ident);
264         if (valStr && (istreq(valStr, "usemodmapmods") ||
265                        istreq(valStr, "modmapmods"))) {
266
267             *mods_rtrn = 0;
268             *flags_inout |= ACTION_MODS_LOOKUP_MODMAP;
269             return true;
270         }
271     }
272
273     if (!ExprResolveModMask(keymap, value, MOD_BOTH, mods_rtrn))
274         return ReportMismatch(keymap, action,
275                               ACTION_FIELD_MODIFIERS, "modifier mask");
276
277     *flags_inout &= ~ACTION_MODS_LOOKUP_MODMAP;
278     return true;
279 }
280
281 static bool
282 HandleSetLatchMods(struct xkb_keymap *keymap, union xkb_action *action,
283                    enum action_field field, const ExprDef *array_ndx,
284                    const ExprDef *value)
285 {
286     struct xkb_mod_action *act = &action->mods;
287
288     if (array_ndx) {
289         switch (field) {
290         case ACTION_FIELD_CLEAR_LOCKS:
291         case ACTION_FIELD_LATCH_TO_LOCK:
292         case ACTION_FIELD_MODIFIERS:
293             return ReportActionNotArray(keymap, action->type, field);
294         default:
295             break;
296         }
297     }
298
299     switch (field) {
300     case ACTION_FIELD_CLEAR_LOCKS:
301     case ACTION_FIELD_LATCH_TO_LOCK:
302         return CheckLatchLockFlags(keymap, action->type, field, value,
303                                    &act->flags);
304     case ACTION_FIELD_MODIFIERS:
305         return CheckModifierField(keymap, action->type, value,
306                                &act->flags, &act->mods.mods);
307     default:
308         break;
309     }
310
311     return ReportIllegal(keymap, action->type, field);
312 }
313
314 static const LookupEntry lockWhich[] = {
315     { "both", 0 },
316     { "lock", ACTION_LOCK_NO_UNLOCK },
317     { "neither", (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK) },
318     { "unlock", ACTION_LOCK_NO_LOCK },
319     { NULL, 0 }
320 };
321
322 static bool
323 CheckAffectField(struct xkb_keymap *keymap, enum xkb_action_type action,
324                  const ExprDef *value, enum xkb_action_flags *flags_inout)
325 {
326     enum xkb_action_flags flags;
327
328     if (!ExprResolveEnum(keymap->ctx, value, &flags, lockWhich))
329         return ReportMismatch(keymap, action, ACTION_FIELD_AFFECT,
330                               "lock, unlock, both, neither");
331
332     *flags_inout &= ~(ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK);
333     *flags_inout |= flags;
334     return true;
335 }
336
337 static bool
338 HandleLockMods(struct xkb_keymap *keymap, union xkb_action *action,
339                enum action_field field, const ExprDef *array_ndx,
340                const ExprDef *value)
341 {
342     struct xkb_mod_action *act = &action->mods;
343
344     if (array_ndx && (field == ACTION_FIELD_MODIFIERS ||
345                       field == ACTION_FIELD_AFFECT))
346         return ReportActionNotArray(keymap, action->type, field);
347
348     switch (field) {
349     case ACTION_FIELD_MODIFIERS:
350         return CheckModifierField(keymap, action->type, value,
351                                   &act->flags, &act->mods.mods);
352     case ACTION_FIELD_AFFECT:
353         return CheckAffectField(keymap, action->type, value, &act->flags);
354     default:
355         break;
356     }
357
358     return ReportIllegal(keymap, action->type, field);
359 }
360
361 static bool
362 CheckGroupField(struct xkb_keymap *keymap, unsigned action,
363                 const ExprDef *value, enum xkb_action_flags *flags_inout,
364                 int32_t *group_rtrn)
365 {
366     const ExprDef *spec;
367     xkb_layout_index_t idx;
368     enum xkb_action_flags flags = *flags_inout;
369
370     if (value->expr.op == EXPR_NEGATE || value->expr.op == EXPR_UNARY_PLUS) {
371         flags &= ~ACTION_ABSOLUTE_SWITCH;
372         spec = value->unary.child;
373     }
374     else {
375         flags |= ACTION_ABSOLUTE_SWITCH;
376         spec = value;
377     }
378
379     if (!ExprResolveGroup(keymap->ctx, spec, &idx))
380         return ReportMismatch(keymap, action, ACTION_FIELD_GROUP,
381                               "integer (range 1..8)");
382
383     /* +n, -n are relative, n is absolute. */
384     if (value->expr.op == EXPR_NEGATE || value->expr.op == EXPR_UNARY_PLUS) {
385         *group_rtrn = (int32_t) idx;
386         if (value->expr.op == EXPR_NEGATE)
387             *group_rtrn = -*group_rtrn;
388     }
389     else {
390         *group_rtrn = (int32_t) (idx - 1);
391     }
392     *flags_inout = flags;
393     return true;
394 }
395
396 static bool
397 HandleSetLatchGroup(struct xkb_keymap *keymap, union xkb_action *action,
398                     enum action_field field, const ExprDef *array_ndx,
399                     const ExprDef *value)
400 {
401     struct xkb_group_action *act = &action->group;
402
403     if (array_ndx) {
404         switch (field) {
405         case ACTION_FIELD_CLEAR_LOCKS:
406         case ACTION_FIELD_LATCH_TO_LOCK:
407         case ACTION_FIELD_GROUP:
408             return ReportActionNotArray(keymap, action->type, field);
409         default:
410             break;
411         }
412     }
413
414     switch (field) {
415     case ACTION_FIELD_CLEAR_LOCKS:
416     case ACTION_FIELD_LATCH_TO_LOCK:
417         return CheckLatchLockFlags(keymap, action->type, field, value,
418                                    &act->flags);
419     case ACTION_FIELD_GROUP:
420         return CheckGroupField(keymap, action->type, value,
421                                &act->flags, &act->group);
422     default:
423         break;
424     }
425
426     return ReportIllegal(keymap, action->type, field);
427 }
428
429 static bool
430 HandleLockGroup(struct xkb_keymap *keymap, union xkb_action *action,
431                 enum action_field field, const ExprDef *array_ndx,
432                 const ExprDef *value)
433 {
434     struct xkb_group_action *act = &action->group;
435
436     if (array_ndx && field == ACTION_FIELD_GROUP)
437         return ReportActionNotArray(keymap, action->type, field);
438
439     if (field == ACTION_FIELD_GROUP)
440         return CheckGroupField(keymap, action->type, value,
441                                &act->flags, &act->group);
442
443     return ReportIllegal(keymap, action->type, field);
444 }
445
446 static bool
447 HandleMovePtr(struct xkb_keymap *keymap, union xkb_action *action,
448               enum action_field field, const ExprDef *array_ndx,
449               const ExprDef *value)
450 {
451     struct xkb_pointer_action *act = &action->ptr;
452
453     if (array_ndx && (field == ACTION_FIELD_X || field == ACTION_FIELD_Y))
454         return ReportActionNotArray(keymap, action->type, field);
455
456     if (field == ACTION_FIELD_X || field == ACTION_FIELD_Y) {
457         int val;
458         const bool absolute = (value->expr.op != EXPR_NEGATE &&
459                                value->expr.op != EXPR_UNARY_PLUS);
460
461         if (!ExprResolveInteger(keymap->ctx, value, &val))
462             return ReportMismatch(keymap, action->type, field, "integer");
463
464         if (val < INT16_MIN || val > INT16_MAX) {
465             log_err(keymap->ctx,
466                     "The %s field in the %s action must be in range %d..%d; "
467                     "Action definition ignored\n",
468                     fieldText(field), ActionTypeText(action->type),
469                     INT16_MIN, INT16_MAX);
470             return false;
471         }
472
473         if (field == ACTION_FIELD_X) {
474             if (absolute)
475                 act->flags |= ACTION_ABSOLUTE_X;
476             act->x = (int16_t) val;
477         }
478         else {
479             if (absolute)
480                 act->flags |= ACTION_ABSOLUTE_Y;
481             act->y = (int16_t) val;
482         }
483
484         return true;
485     }
486     else if (field == ACTION_FIELD_ACCEL) {
487         bool set;
488
489         if (!ExprResolveBoolean(keymap->ctx, value, &set))
490             return ReportMismatch(keymap, action->type, field, "boolean");
491
492         if (set)
493             act->flags &= ~ACTION_NO_ACCEL;
494         else
495             act->flags |= ACTION_NO_ACCEL;
496     }
497
498     return ReportIllegal(keymap, action->type, field);
499 }
500
501 static bool
502 HandlePtrBtn(struct xkb_keymap *keymap, union xkb_action *action,
503              enum action_field field, const ExprDef *array_ndx,
504              const ExprDef *value)
505 {
506     struct xkb_pointer_button_action *act = &action->btn;
507
508     if (field == ACTION_FIELD_BUTTON) {
509         int btn;
510
511         if (array_ndx)
512             return ReportActionNotArray(keymap, action->type, field);
513
514         if (!ExprResolveButton(keymap->ctx, value, &btn))
515             return ReportMismatch(keymap, action->type, field,
516                                   "integer (range 1..5)");
517
518         if (btn < 0 || btn > 5) {
519             log_err(keymap->ctx,
520                     "Button must specify default or be in the range 1..5; "
521                     "Illegal button value %d ignored\n", btn);
522             return false;
523         }
524
525         act->button = btn;
526         return true;
527     }
528     else if (action->type == ACTION_TYPE_PTR_LOCK &&
529              field == ACTION_FIELD_AFFECT) {
530         if (array_ndx)
531             return ReportActionNotArray(keymap, action->type, field);
532
533         return CheckAffectField(keymap, action->type, value, &act->flags);
534     }
535     else if (field == ACTION_FIELD_COUNT) {
536         int val;
537
538         if (array_ndx)
539             return ReportActionNotArray(keymap, action->type, field);
540
541         if (!ExprResolveInteger(keymap->ctx, value, &val))
542             return ReportMismatch(keymap, action->type, field, "integer");
543
544         if (val < 0 || val > 255) {
545             log_err(keymap->ctx,
546                     "The count field must have a value in the range 0..255; "
547                     "Illegal count %d ignored\n", val);
548             return false;
549         }
550
551         act->count = (uint8_t) val;
552         return true;
553     }
554     return ReportIllegal(keymap, action->type, field);
555 }
556
557 static const LookupEntry ptrDflts[] = {
558     { "dfltbtn", 1 },
559     { "defaultbutton", 1 },
560     { "button", 1 },
561     { NULL, 0 }
562 };
563
564 static bool
565 HandleSetPtrDflt(struct xkb_keymap *keymap, union xkb_action *action,
566                  enum action_field field, const ExprDef *array_ndx,
567                  const ExprDef *value)
568 {
569     struct xkb_pointer_default_action *act = &action->dflt;
570
571     if (field == ACTION_FIELD_AFFECT) {
572         unsigned int val;
573
574         if (array_ndx)
575             return ReportActionNotArray(keymap, action->type, field);
576
577         if (!ExprResolveEnum(keymap->ctx, value, &val, ptrDflts))
578             return ReportMismatch(keymap, action->type, field,
579                                   "pointer component");
580         return true;
581     }
582     else if (field == ACTION_FIELD_BUTTON || field == ACTION_FIELD_VALUE) {
583         const ExprDef *button;
584         int btn;
585
586         if (array_ndx)
587             return ReportActionNotArray(keymap, action->type, field);
588
589         if (value->expr.op == EXPR_NEGATE ||
590             value->expr.op == EXPR_UNARY_PLUS) {
591             act->flags &= ~ACTION_ABSOLUTE_SWITCH;
592             button = value->unary.child;
593         }
594         else {
595             act->flags |= ACTION_ABSOLUTE_SWITCH;
596             button = value;
597         }
598
599         if (!ExprResolveButton(keymap->ctx, button, &btn))
600             return ReportMismatch(keymap, action->type, field,
601                                   "integer (range 1..5)");
602
603         if (btn < 0 || btn > 5) {
604             log_err(keymap->ctx,
605                     "New default button value must be in the range 1..5; "
606                     "Illegal default button value %d ignored\n", btn);
607             return false;
608         }
609         if (btn == 0) {
610             log_err(keymap->ctx,
611                     "Cannot set default pointer button to \"default\"; "
612                     "Illegal default button setting ignored\n");
613             return false;
614         }
615
616         act->value = (value->expr.op == EXPR_NEGATE ? -btn: btn);
617         return true;
618     }
619
620     return ReportIllegal(keymap, action->type, field);
621 }
622
623 static bool
624 HandleSwitchScreen(struct xkb_keymap *keymap, union xkb_action *action,
625                    enum action_field field, const ExprDef *array_ndx,
626                    const ExprDef *value)
627 {
628     struct xkb_switch_screen_action *act = &action->screen;
629
630     if (field == ACTION_FIELD_SCREEN) {
631         const ExprDef *scrn;
632         int val;
633
634         if (array_ndx)
635             return ReportActionNotArray(keymap, action->type, field);
636
637         if (value->expr.op == EXPR_NEGATE ||
638             value->expr.op == EXPR_UNARY_PLUS) {
639             act->flags &= ~ACTION_ABSOLUTE_SWITCH;
640             scrn = value->unary.child;
641         }
642         else {
643             act->flags |= ACTION_ABSOLUTE_SWITCH;
644             scrn = value;
645         }
646
647         if (!ExprResolveInteger(keymap->ctx, scrn, &val))
648             return ReportMismatch(keymap, action->type, field,
649                                   "integer (0..255)");
650
651         if (val < 0 || val > 255) {
652             log_err(keymap->ctx,
653                     "Screen index must be in the range 1..255; "
654                     "Illegal screen value %d ignored\n", val);
655             return false;
656         }
657
658         act->screen = (value->expr.op == EXPR_NEGATE ? -val : val);
659         return true;
660     }
661     else if (field == ACTION_FIELD_SAME) {
662         bool set;
663
664         if (array_ndx)
665             return ReportActionNotArray(keymap, action->type, field);
666
667         if (!ExprResolveBoolean(keymap->ctx, value, &set))
668             return ReportMismatch(keymap, action->type, field, "boolean");
669
670         if (set)
671             act->flags &= ~ACTION_SAME_SCREEN;
672         else
673             act->flags |= ACTION_SAME_SCREEN;
674
675         return true;
676     }
677
678     return ReportIllegal(keymap, action->type, field);
679 }
680
681 static bool
682 HandleSetLockControls(struct xkb_keymap *keymap, union xkb_action *action,
683                       enum action_field field, const ExprDef *array_ndx,
684                       const ExprDef *value)
685 {
686     struct xkb_controls_action *act = &action->ctrls;
687
688     if (field == ACTION_FIELD_CONTROLS) {
689         unsigned int mask;
690
691         if (array_ndx)
692             return ReportActionNotArray(keymap, action->type, field);
693
694         if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlMaskNames))
695             return ReportMismatch(keymap, action->type, field,
696                                   "controls mask");
697
698         act->ctrls = mask;
699         return true;
700     }
701     else if (field == ACTION_FIELD_AFFECT) {
702         if (array_ndx)
703             return ReportActionNotArray(keymap, action->type, field);
704
705         return CheckAffectField(keymap, action->type, value, &act->flags);
706     }
707
708     return ReportIllegal(keymap, action->type, field);
709 }
710
711 static bool
712 HandlePrivate(struct xkb_keymap *keymap, union xkb_action *action,
713               enum action_field field, const ExprDef *array_ndx,
714               const ExprDef *value)
715 {
716     struct xkb_private_action *act = &action->priv;
717
718     if (field == ACTION_FIELD_TYPE) {
719         int type;
720
721         if (!ExprResolveInteger(keymap->ctx, value, &type))
722             return ReportMismatch(keymap, ACTION_TYPE_PRIVATE, field, "integer");
723
724         if (type < 0 || type > 255) {
725             log_err(keymap->ctx,
726                     "Private action type must be in the range 0..255; "
727                     "Illegal type %d ignored\n", type);
728             return false;
729         }
730
731         /*
732          * It's possible for someone to write something like this:
733          *      actions = [ Private(type=3,data[0]=1,data[1]=3,data[2]=3) ]
734          * where the type refers to some existing action type, e.g. LockMods.
735          * This assumes that this action's struct is laid out in memory
736          * exactly as described in the XKB specification and libraries.
737          * We, however, have changed these structs in various ways, so this
738          * assumption is no longer true. Since this is a lousy "feature", we
739          * make actions like these no-ops for now.
740          */
741         if (type < ACTION_TYPE_PRIVATE) {
742             log_info(keymap->ctx,
743                      "Private actions of type %s are not supported; Ignored\n",
744                      ActionTypeText(type));
745             act->type = ACTION_TYPE_NONE;
746         }
747         else {
748             act->type = (enum xkb_action_type) type;
749         }
750
751         return true;
752     }
753     else if (field == ACTION_FIELD_DATA) {
754         if (array_ndx == NULL) {
755             xkb_atom_t val;
756             const char *str;
757             size_t len;
758
759             if (!ExprResolveString(keymap->ctx, value, &val))
760                 return ReportMismatch(keymap, action->type, field, "string");
761
762             str = xkb_atom_text(keymap->ctx, val);
763             len = strlen(str);
764             if (len < 1 || len > 7) {
765                 log_warn(keymap->ctx,
766                          "A private action has 7 data bytes; "
767                          "Illegal data ignored\n");
768                 return false;
769             }
770
771             strncpy((char *) act->data, str, sizeof(act->data));
772             return true;
773         }
774         else {
775             int ndx, datum;
776
777             if (!ExprResolveInteger(keymap->ctx, array_ndx, &ndx)) {
778                 log_err(keymap->ctx,
779                         "Array subscript must be integer; "
780                         "Illegal subscript ignored\n");
781                 return false;
782             }
783
784             if (ndx < 0 || (size_t) ndx >= sizeof(act->data)) {
785                 log_err(keymap->ctx,
786                         "The data for a private action is %lu bytes long; "
787                         "Attempt to use data[%d] ignored\n",
788                         (unsigned long) sizeof(act->data), ndx);
789                 return false;
790             }
791
792             if (!ExprResolveInteger(keymap->ctx, value, &datum))
793                 return ReportMismatch(keymap, act->type, field, "integer");
794
795             if (datum < 0 || datum > 255) {
796                 log_err(keymap->ctx,
797                         "All data for a private action must be 0..255; "
798                         "Illegal datum %d ignored\n", datum);
799                 return false;
800             }
801
802             act->data[ndx] = (uint8_t) datum;
803             return true;
804         }
805     }
806
807     return ReportIllegal(keymap, ACTION_TYPE_NONE, field);
808 }
809
810 typedef bool (*actionHandler)(struct xkb_keymap *keymap,
811                               union xkb_action *action,
812                               enum action_field field,
813                               const ExprDef *array_ndx,
814                               const ExprDef *value);
815
816 static const actionHandler handleAction[_ACTION_TYPE_NUM_ENTRIES] = {
817     [ACTION_TYPE_NONE] = HandleNoAction,
818     [ACTION_TYPE_MOD_SET] = HandleSetLatchMods,
819     [ACTION_TYPE_MOD_LATCH] = HandleSetLatchMods,
820     [ACTION_TYPE_MOD_LOCK] = HandleLockMods,
821     [ACTION_TYPE_GROUP_SET] = HandleSetLatchGroup,
822     [ACTION_TYPE_GROUP_LATCH] = HandleSetLatchGroup,
823     [ACTION_TYPE_GROUP_LOCK] = HandleLockGroup,
824     [ACTION_TYPE_PTR_MOVE] = HandleMovePtr,
825     [ACTION_TYPE_PTR_BUTTON] = HandlePtrBtn,
826     [ACTION_TYPE_PTR_LOCK] = HandlePtrBtn,
827     [ACTION_TYPE_PTR_DEFAULT] = HandleSetPtrDflt,
828     [ACTION_TYPE_TERMINATE] = HandleNoAction,
829     [ACTION_TYPE_SWITCH_VT] = HandleSwitchScreen,
830     [ACTION_TYPE_CTRL_SET] = HandleSetLockControls,
831     [ACTION_TYPE_CTRL_LOCK] = HandleSetLockControls,
832     [ACTION_TYPE_PRIVATE] = HandlePrivate,
833 };
834
835 /***====================================================================***/
836
837 bool
838 HandleActionDef(ExprDef *def, struct xkb_keymap *keymap,
839                 union xkb_action *action, ActionsInfo *info)
840 {
841     ExprDef *arg;
842     const char *str;
843     unsigned handler_type;
844
845     if (def->expr.op != EXPR_ACTION_DECL) {
846         log_err(keymap->ctx, "Expected an action definition, found %s\n",
847                 expr_op_type_to_string(def->expr.op));
848         return false;
849     }
850
851     str = xkb_atom_text(keymap->ctx, def->action.name);
852     if (!stringToAction(str, &handler_type)) {
853         log_err(keymap->ctx, "Unknown action %s\n", str);
854         return false;
855     }
856
857     /*
858      * Get the default values for this action type, as modified by
859      * statements such as:
860      *     latchMods.clearLocks = True;
861      */
862     *action = info->actions[handler_type];
863
864     /*
865      * Now change the action properties as specified for this
866      * particular instance, e.g. "modifiers" and "clearLocks" in:
867      *     SetMods(modifiers=Alt,clearLocks);
868      */
869     for (arg = def->action.args; arg != NULL;
870          arg = (ExprDef *) arg->common.next) {
871         const ExprDef *value;
872         ExprDef *field, *arrayRtrn;
873         const char *elemRtrn, *fieldRtrn;
874         enum action_field fieldNdx;
875
876         if (arg->expr.op == EXPR_ASSIGN) {
877             field = arg->binary.left;
878             value = arg->binary.right;
879         }
880         else if (arg->expr.op == EXPR_NOT || arg->expr.op == EXPR_INVERT) {
881             field = arg->unary.child;
882             value = (const ExprDef *) &constFalse;
883         }
884         else {
885             field = arg;
886             value = (const ExprDef *) &constTrue;
887         }
888
889         if (!ExprResolveLhs(keymap->ctx, field, &elemRtrn, &fieldRtrn,
890                             &arrayRtrn))
891             return false;
892
893         if (elemRtrn) {
894             log_err(keymap->ctx,
895                     "Cannot change defaults in an action definition; "
896                     "Ignoring attempt to change %s.%s\n",
897                     elemRtrn, fieldRtrn);
898             return false;
899         }
900
901         if (!stringToField(fieldRtrn, &fieldNdx)) {
902             log_err(keymap->ctx, "Unknown field name %s\n", fieldRtrn);
903             return false;
904         }
905
906         if (!handleAction[handler_type](keymap, action, fieldNdx, arrayRtrn,
907                                         value))
908             return false;
909     }
910
911     return true;
912 }
913
914
915 bool
916 SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field,
917                ExprDef *array_ndx, ExprDef *value, ActionsInfo *info)
918 {
919     unsigned action;
920     enum action_field action_field;
921
922     if (!stringToAction(elem, &action))
923         return false;
924
925     if (!stringToField(field, &action_field)) {
926         log_err(keymap->ctx, "\"%s\" is not a legal field name\n", field);
927         return false;
928     }
929
930     return handleAction[action](keymap, &info->actions[action],
931                                 action_field, array_ndx, value);
932 }