expr: drop ExprResult from ResolveKeyName
[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 #include "action.h"
28
29 static bool actionsInitialized;
30 static ExprDef constTrue;
31 static ExprDef constFalse;
32
33 /***====================================================================***/
34
35 static const LookupEntry actionStrings[] = {
36     { "noaction",          XkbSA_NoAction       },
37     { "setmods",           XkbSA_SetMods        },
38     { "latchmods",         XkbSA_LatchMods      },
39     { "lockmods",          XkbSA_LockMods       },
40     { "setgroup",          XkbSA_SetGroup       },
41     { "latchgroup",        XkbSA_LatchGroup     },
42     { "lockgroup",         XkbSA_LockGroup      },
43     { "moveptr",           XkbSA_MovePtr        },
44     { "movepointer",       XkbSA_MovePtr        },
45     { "ptrbtn",            XkbSA_PtrBtn         },
46     { "pointerbutton",     XkbSA_PtrBtn         },
47     { "lockptrbtn",        XkbSA_LockPtrBtn     },
48     { "lockpointerbutton", XkbSA_LockPtrBtn     },
49     { "lockptrbutton",     XkbSA_LockPtrBtn     },
50     { "lockpointerbtn",    XkbSA_LockPtrBtn     },
51     { "setptrdflt",        XkbSA_SetPtrDflt     },
52     { "setpointerdefault", XkbSA_SetPtrDflt     },
53     { "isolock",           XkbSA_ISOLock        },
54     { "terminate",         XkbSA_Terminate      },
55     { "terminateserver",   XkbSA_Terminate      },
56     { "switchscreen",      XkbSA_SwitchScreen   },
57     { "setcontrols",       XkbSA_SetControls    },
58     { "lockcontrols",      XkbSA_LockControls   },
59     { "actionmessage",     XkbSA_ActionMessage  },
60     { "messageaction",     XkbSA_ActionMessage  },
61     { "message",           XkbSA_ActionMessage  },
62     { "redirect",          XkbSA_RedirectKey    },
63     { "redirectkey",       XkbSA_RedirectKey    },
64     { "devbtn",            XkbSA_DeviceBtn      },
65     { "devicebtn",         XkbSA_DeviceBtn      },
66     { "devbutton",         XkbSA_DeviceBtn      },
67     { "devicebutton",      XkbSA_DeviceBtn      },
68     { "lockdevbtn",        XkbSA_DeviceBtn      },
69     { "lockdevicebtn",     XkbSA_LockDeviceBtn  },
70     { "lockdevbutton",     XkbSA_LockDeviceBtn  },
71     { "lockdevicebutton",  XkbSA_LockDeviceBtn  },
72     { "devval",            XkbSA_DeviceValuator },
73     { "deviceval",         XkbSA_DeviceValuator },
74     { "devvaluator",       XkbSA_DeviceValuator },
75     { "devicevaluator",    XkbSA_DeviceValuator },
76     { "private",           PrivateAction        },
77     { NULL,                0                    }
78 };
79
80 static const LookupEntry fieldStrings[] = {
81     { "clearLocks",       F_ClearLocks  },
82     { "latchToLock",      F_LatchToLock },
83     { "genKeyEvent",      F_GenKeyEvent },
84     { "generateKeyEvent", F_GenKeyEvent },
85     { "report",           F_Report      },
86     { "default",          F_Default     },
87     { "affect",           F_Affect      },
88     { "increment",        F_Increment   },
89     { "modifiers",        F_Modifiers   },
90     { "mods",             F_Modifiers   },
91     { "group",            F_Group       },
92     { "x",                F_X           },
93     { "y",                F_Y           },
94     { "accel",            F_Accel       },
95     { "accelerate",       F_Accel       },
96     { "repeat",           F_Accel       },
97     { "button",           F_Button      },
98     { "value",            F_Value       },
99     { "controls",         F_Controls    },
100     { "ctrls",            F_Controls    },
101     { "type",             F_Type        },
102     { "count",            F_Count       },
103     { "screen",           F_Screen      },
104     { "same",             F_Same        },
105     { "sameServer",       F_Same        },
106     { "data",             F_Data        },
107     { "device",           F_Device      },
108     { "dev",              F_Device      },
109     { "key",              F_Keycode     },
110     { "keycode",          F_Keycode     },
111     { "kc",               F_Keycode     },
112     { "clearmods",        F_ModsToClear },
113     { "clearmodifiers",   F_ModsToClear },
114     { NULL,               0             }
115 };
116
117 static bool
118 stringToValue(const LookupEntry tab[], const char *string,
119               unsigned *value_rtrn)
120 {
121     const LookupEntry *entry;
122
123     if (!string)
124         return false;
125
126     for (entry = tab; entry->name != NULL; entry++) {
127         if (istreq(entry->name, string)) {
128             *value_rtrn = entry->result;
129             return true;
130         }
131     }
132
133     return false;
134 }
135
136 static const char *
137 valueToString(const LookupEntry tab[], unsigned value)
138 {
139     const LookupEntry *entry;
140
141     for (entry = tab; entry->name != NULL; entry++)
142         if (entry->result == value)
143             return entry->name;
144
145     return "unknown";
146 }
147
148 static bool
149 stringToAction(const char *str, unsigned *type_rtrn)
150 {
151     return stringToValue(actionStrings, str, type_rtrn);
152 }
153
154 static bool
155 stringToField(const char *str, unsigned *field_rtrn)
156 {
157     return stringToValue(fieldStrings, str, field_rtrn);
158 }
159
160 static const char *
161 fieldText(unsigned field)
162 {
163     return valueToString(fieldStrings, field);
164 }
165
166 /***====================================================================***/
167
168 static inline bool
169 ReportMismatch(struct xkb_keymap *keymap, unsigned action, unsigned field,
170                const char *type)
171 {
172     log_err(keymap->ctx,
173             "Value of %s field must be of type %s; "
174             "Action %s definition ignored\n",
175             fieldText(field), type, ActionTypeText(action));
176     return false;
177 }
178
179 static inline bool
180 ReportIllegal(struct xkb_keymap *keymap, unsigned action, unsigned field)
181 {
182     log_err(keymap->ctx,
183             "Field %s is not defined for an action of type %s; "
184             "Action definition ignored\n",
185             fieldText(field), ActionTypeText(action));
186     return false;
187 }
188
189 static inline bool
190 ReportActionNotArray(struct xkb_keymap *keymap, unsigned action,
191                      unsigned field)
192 {
193     log_err(keymap->ctx,
194             "The %s field in the %s action is not an array; "
195             "Action definition ignored\n",
196             fieldText(field), ActionTypeText(action));
197     return false;
198 }
199
200 static inline bool
201 ReportNotFound(struct xkb_keymap *keymap, unsigned action, unsigned field,
202                const char *what, const char *bad)
203 {
204     log_err(keymap->ctx,
205             "%s named %s not found; "
206             "Ignoring the %s field of an %s action\n",
207             what, bad, fieldText(field), ActionTypeText(action));
208     return false;
209 }
210
211 static bool
212 HandleNoAction(struct xkb_keymap *keymap, struct xkb_any_action *action,
213                unsigned field, ExprDef *array_ndx, ExprDef *value)
214
215 {
216     return ReportIllegal(keymap, action->type, field);
217 }
218
219 static bool
220 CheckLatchLockFlags(struct xkb_keymap *keymap, unsigned action,
221                     unsigned field, ExprDef * value, unsigned *flags_inout)
222 {
223     unsigned tmp;
224     ExprResult result;
225
226     if (field == F_ClearLocks)
227         tmp = XkbSA_ClearLocks;
228     else if (field == F_LatchToLock)
229         tmp = XkbSA_LatchToLock;
230     else
231         return false;           /* WSGO! */
232     if (!ExprResolveBoolean(keymap->ctx, value, &result))
233         return ReportMismatch(keymap, action, field, "boolean");
234     if (result.uval)
235         *flags_inout |= tmp;
236     else
237         *flags_inout &= ~tmp;
238     return true;
239 }
240
241 static bool
242 CheckModifierField(struct xkb_keymap *keymap, unsigned action, ExprDef *value,
243                    unsigned *flags_inout, xkb_mod_mask_t *mods_rtrn)
244 {
245     ExprResult rtrn;
246     if (value->op == EXPR_IDENT) {
247         const char *valStr;
248         valStr = xkb_atom_text(keymap->ctx, value->value.str);
249         if (valStr && (istreq(valStr, "usemodmapmods") ||
250                        istreq(valStr, "modmapmods"))) {
251
252             *mods_rtrn = 0;
253             *flags_inout |= XkbSA_UseModMapMods;
254             return true;
255         }
256     }
257
258     if (!ExprResolveVModMask(keymap, value, &rtrn))
259         return ReportMismatch(keymap, action, F_Modifiers, "modifier mask");
260
261     *mods_rtrn = (xkb_mod_mask_t) rtrn.ival;
262     *flags_inout &= ~XkbSA_UseModMapMods;
263     return true;
264 }
265
266 static bool
267 HandleSetLatchMods(struct xkb_keymap *keymap, struct xkb_any_action *action,
268                    unsigned field, ExprDef *array_ndx, ExprDef *value)
269 {
270     struct xkb_mod_action *act;
271     unsigned rtrn;
272     unsigned t1;
273     xkb_mod_mask_t t2;
274
275     act = (struct xkb_mod_action *) action;
276     if (array_ndx != NULL) {
277         switch (field) {
278         case F_ClearLocks:
279         case F_LatchToLock:
280         case F_Modifiers:
281             return ReportActionNotArray(keymap, action->type, field);
282         }
283     }
284     switch (field) {
285     case F_ClearLocks:
286     case F_LatchToLock:
287         rtrn = act->flags;
288         if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
289             act->flags = rtrn;
290             return true;
291         }
292         return false;
293
294     case F_Modifiers:
295         t1 = act->flags;
296         if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
297             act->flags = t1;
298             act->real_mods = act->mask = (t2 & 0xff);
299             act->vmods = (t2 >> 8) & 0xffff;
300             return true;
301         }
302         return false;
303     }
304     return ReportIllegal(keymap, action->type, field);
305 }
306
307 static bool
308 HandleLockMods(struct xkb_keymap *keymap, struct xkb_any_action *action,
309                unsigned field, ExprDef *array_ndx, ExprDef *value)
310 {
311     struct xkb_mod_action *act;
312     unsigned t1;
313     xkb_mod_mask_t t2;
314
315     act = (struct xkb_mod_action *) action;
316     if ((array_ndx != NULL) && (field == F_Modifiers))
317         return ReportActionNotArray(keymap, action->type, field);
318     switch (field) {
319     case F_Modifiers:
320         t1 = act->flags;
321         if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
322             act->flags = t1;
323             act->real_mods = act->mask = (t2 & 0xff);
324             act->vmods = (t2 >> 8) & 0xffff;
325             return true;
326         }
327         return false;
328     }
329     return ReportIllegal(keymap, action->type, field);
330 }
331
332 static bool
333 CheckGroupField(struct xkb_keymap *keymap, unsigned action,
334                 ExprDef * value, unsigned *flags_inout,
335                 xkb_group_index_t *grp_rtrn)
336 {
337     ExprDef *spec;
338     ExprResult rtrn;
339
340     if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
341         *flags_inout &= ~XkbSA_GroupAbsolute;
342         spec = value->value.child;
343     }
344     else {
345         *flags_inout |= XkbSA_GroupAbsolute;
346         spec = value;
347     }
348
349     if (!ExprResolveGroup(keymap->ctx, spec, &rtrn))
350         return ReportMismatch(keymap, action, F_Group,
351                               "integer (range 1..8)");
352     if (value->op == EXPR_NEGATE)
353         *grp_rtrn = -rtrn.ival;
354     else if (value->op == EXPR_UNARY_PLUS)
355         *grp_rtrn = rtrn.ival;
356     else
357         *grp_rtrn = rtrn.ival - 1;
358     return true;
359 }
360
361 static bool
362 HandleSetLatchGroup(struct xkb_keymap *keymap, struct xkb_any_action *action,
363                     unsigned field, ExprDef *array_ndx, ExprDef *value)
364 {
365     struct xkb_group_action *act;
366     unsigned rtrn;
367     unsigned t1;
368     xkb_group_index_t t2;
369
370     act = (struct xkb_group_action *) action;
371     if (array_ndx != NULL) {
372         switch (field) {
373         case F_ClearLocks:
374         case F_LatchToLock:
375         case F_Group:
376             return ReportActionNotArray(keymap, action->type, field);
377         }
378     }
379     switch (field) {
380     case F_ClearLocks:
381     case F_LatchToLock:
382         rtrn = act->flags;
383         if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
384             act->flags = rtrn;
385             return true;
386         }
387         return false;
388
389     case F_Group:
390         t1 = act->flags;
391         if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
392             act->flags = t1;
393             act->group = t2;
394             return true;
395         }
396         return false;
397     }
398     return ReportIllegal(keymap, action->type, field);
399 }
400
401 static bool
402 HandleLockGroup(struct xkb_keymap *keymap, struct xkb_any_action *action,
403                 unsigned field, ExprDef *array_ndx, ExprDef *value)
404 {
405     struct xkb_group_action *act;
406     unsigned t1;
407     xkb_group_index_t t2;
408
409     act = (struct xkb_group_action *) action;
410     if ((array_ndx != NULL) && (field == F_Group))
411         return ReportActionNotArray(keymap, action->type, field);
412     if (field == F_Group) {
413         t1 = act->flags;
414         if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
415             act->flags = t1;
416             act->group = t2;
417             return true;
418         }
419         return false;
420     }
421     return ReportIllegal(keymap, action->type, field);
422 }
423
424 static bool
425 HandleMovePtr(struct xkb_keymap *keymap, struct xkb_any_action *action,
426               unsigned field, ExprDef *array_ndx, ExprDef *value)
427 {
428     ExprResult rtrn;
429     struct xkb_pointer_action *act;
430     bool absolute;
431
432     act = (struct xkb_pointer_action *) action;
433     if ((array_ndx != NULL) && ((field == F_X) || (field == F_Y)))
434         return ReportActionNotArray(keymap, action->type, field);
435
436     if ((field == F_X) || (field == F_Y)) {
437         if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS)
438             absolute = false;
439         else
440             absolute = true;
441         if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
442             return ReportMismatch(keymap, action->type, field, "integer");
443         if (field == F_X) {
444             if (absolute)
445                 act->flags |= XkbSA_MoveAbsoluteX;
446             act->x = rtrn.ival;
447         }
448         else {
449             if (absolute)
450                 act->flags |= XkbSA_MoveAbsoluteY;
451             act->y = rtrn.ival;
452         }
453         return true;
454     }
455     else if (field == F_Accel) {
456         if (!ExprResolveBoolean(keymap->ctx, value, &rtrn))
457             return ReportMismatch(keymap, action->type, field, "boolean");
458         if (rtrn.uval)
459             act->flags &= ~XkbSA_NoAcceleration;
460         else
461             act->flags |= XkbSA_NoAcceleration;
462     }
463     return ReportIllegal(keymap, action->type, field);
464 }
465
466 static const LookupEntry lockWhich[] = {
467     { "both", 0 },
468     { "lock", XkbSA_LockNoUnlock },
469     { "neither", (XkbSA_LockNoLock | XkbSA_LockNoUnlock) },
470     { "unlock", XkbSA_LockNoLock },
471     { NULL, 0 }
472 };
473
474 static bool
475 HandlePtrBtn(struct xkb_keymap *keymap, struct xkb_any_action *action,
476              unsigned field, ExprDef *array_ndx, ExprDef *value)
477 {
478     ExprResult rtrn;
479     struct xkb_pointer_button_action *act;
480
481     act = (struct xkb_pointer_button_action *) action;
482     if (field == F_Button) {
483         if (array_ndx != NULL)
484             return ReportActionNotArray(keymap, action->type, field);
485         if (!ExprResolveButton(keymap->ctx, value, &rtrn))
486             return ReportMismatch(keymap, action->type, field,
487                                   "integer (range 1..5)");
488         if ((rtrn.ival < 0) || (rtrn.ival > 5)) {
489             log_err(keymap->ctx,
490                     "Button must specify default or be in the range 1..5; "
491                     "Illegal button value %d ignored\n", rtrn.ival);
492             return false;
493         }
494         act->button = rtrn.ival;
495         return true;
496     }
497     else if ((action->type == XkbSA_LockPtrBtn) && (field == F_Affect)) {
498         if (array_ndx != NULL)
499             return ReportActionNotArray(keymap, action->type, field);
500         if (!ExprResolveEnum(keymap->ctx, value, &rtrn, lockWhich))
501             return ReportMismatch(keymap, action->type, field,
502                                   "lock or unlock");
503         act->flags &= ~(XkbSA_LockNoLock | XkbSA_LockNoUnlock);
504         act->flags |= rtrn.ival;
505         return true;
506     }
507     else if (field == F_Count) {
508         if (array_ndx != NULL)
509             return ReportActionNotArray(keymap, action->type, field);
510         if (!ExprResolveButton(keymap->ctx, value, &rtrn))
511             return ReportMismatch(keymap, action->type, field, "integer");
512         if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
513             log_err(keymap->ctx,
514                     "The count field must have a value in the range 0..255; "
515                     "Illegal count %d ignored\n", rtrn.ival);
516             return false;
517         }
518         act->count = rtrn.ival;
519         return true;
520     }
521     return ReportIllegal(keymap, action->type, field);
522 }
523
524 static const LookupEntry ptrDflts[] = {
525     { "dfltbtn", XkbSA_AffectDfltBtn },
526     { "defaultbutton", XkbSA_AffectDfltBtn },
527     { "button", XkbSA_AffectDfltBtn },
528     { NULL, 0 }
529 };
530
531 static bool
532 HandleSetPtrDflt(struct xkb_keymap *keymap, struct xkb_any_action *action,
533                  unsigned field, ExprDef *array_ndx, ExprDef *value)
534 {
535     ExprResult rtrn;
536     struct xkb_pointer_default_action *act;
537
538     act = (struct xkb_pointer_default_action *) action;
539     if (field == F_Affect) {
540         if (array_ndx != NULL)
541             return ReportActionNotArray(keymap, action->type, field);
542         if (!ExprResolveEnum(keymap->ctx, value, &rtrn, ptrDflts))
543             return ReportMismatch(keymap, action->type, field,
544                                   "pointer component");
545         act->affect = rtrn.uval;
546         return true;
547     }
548     else if ((field == F_Button) || (field == F_Value)) {
549         ExprDef *btn;
550         if (array_ndx != NULL)
551             return ReportActionNotArray(keymap, action->type, field);
552         if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
553             act->flags &= ~XkbSA_DfltBtnAbsolute;
554             btn = value->value.child;
555         }
556         else {
557             act->flags |= XkbSA_DfltBtnAbsolute;
558             btn = value;
559         }
560
561         if (!ExprResolveButton(keymap->ctx, btn, &rtrn))
562             return ReportMismatch(keymap, action->type, field,
563                                   "integer (range 1..5)");
564         if ((rtrn.ival < 0) || (rtrn.ival > 5)) {
565             log_err(keymap->ctx,
566                     "New default button value must be in the range 1..5; "
567                     "Illegal default button value %d ignored\n", rtrn.ival);
568             return false;
569         }
570         if (rtrn.ival == 0) {
571             log_err(keymap->ctx,
572                     "Cannot set default pointer button to \"default\"; "
573                     "Illegal default button setting ignored\n");
574             return false;
575         }
576
577         act->value = (value->op == EXPR_NEGATE ? -rtrn.ival : rtrn.ival);
578         return true;
579     }
580     return ReportIllegal(keymap, action->type, field);
581 }
582
583 static const LookupEntry isoNames[] = {
584     { "mods", XkbSA_ISONoAffectMods },
585     { "modifiers", XkbSA_ISONoAffectMods },
586     { "group", XkbSA_ISONoAffectGroup },
587     { "groups", XkbSA_ISONoAffectGroup },
588     { "ptr", XkbSA_ISONoAffectPtr },
589     { "pointer", XkbSA_ISONoAffectPtr },
590     { "ctrls", XkbSA_ISONoAffectCtrls },
591     { "controls", XkbSA_ISONoAffectCtrls },
592     { "all", ~((unsigned) 0) },
593     { "none", 0 },
594     { NULL, 0 },
595 };
596
597 static bool
598 HandleISOLock(struct xkb_keymap *keymap, struct xkb_any_action *action,
599               unsigned field, ExprDef *array_ndx, ExprDef *value)
600 {
601     ExprResult rtrn;
602     struct xkb_iso_action *act;
603     unsigned flags;
604     xkb_mod_mask_t mods;
605     xkb_group_index_t group;
606
607     act = (struct xkb_iso_action *) action;
608     switch (field) {
609     case F_Modifiers:
610         if (array_ndx != NULL)
611             return ReportActionNotArray(keymap, action->type, field);
612         flags = act->flags;
613         if (CheckModifierField(keymap, action->type, value, &flags, &mods)) {
614             act->flags = flags & (~XkbSA_ISODfltIsGroup);
615             act->real_mods = mods & 0xff;
616             act->vmods = (mods >> 8) & 0xff;
617             return true;
618         }
619         return false;
620
621     case F_Group:
622         if (array_ndx != NULL)
623             return ReportActionNotArray(keymap, action->type, field);
624         flags = act->flags;
625         if (CheckGroupField(keymap, action->type, value, &flags, &group)) {
626             act->flags = flags | XkbSA_ISODfltIsGroup;
627             act->group = group;
628             return true;
629         }
630         return false;
631
632     case F_Affect:
633         if (array_ndx != NULL)
634             return ReportActionNotArray(keymap, action->type, field);
635         if (!ExprResolveMask(keymap->ctx, value, &rtrn, isoNames))
636             return ReportMismatch(keymap, action->type, field,
637                                   "keyboard component");
638         act->affect = (~rtrn.uval) & XkbSA_ISOAffectMask;
639         return true;
640     }
641     return ReportIllegal(keymap, action->type, field);
642 }
643
644 static bool
645 HandleSwitchScreen(struct xkb_keymap *keymap, struct xkb_any_action *action,
646                    unsigned field, ExprDef *array_ndx, ExprDef *value)
647 {
648     ExprResult rtrn;
649     struct xkb_switch_screen_action *act;
650
651     act = (struct xkb_switch_screen_action *) action;
652     if (field == F_Screen) {
653         ExprDef *scrn;
654         if (array_ndx != NULL)
655             return ReportActionNotArray(keymap, action->type, field);
656         if (value->op == EXPR_NEGATE || value->op == EXPR_UNARY_PLUS) {
657             act->flags &= ~XkbSA_SwitchAbsolute;
658             scrn = value->value.child;
659         }
660         else {
661             act->flags |= XkbSA_SwitchAbsolute;
662             scrn = value;
663         }
664
665         if (!ExprResolveInteger(keymap->ctx, scrn, &rtrn))
666             return ReportMismatch(keymap, action->type, field,
667                                   "integer (0..255)");
668         if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
669             log_err(keymap->ctx,
670                     "Screen index must be in the range 1..255; "
671                     "Illegal screen value %d ignored\n", rtrn.ival);
672             return false;
673         }
674         if (value->op == EXPR_NEGATE)
675             act->screen = -rtrn.ival;
676         else
677             act->screen = rtrn.ival;
678         return true;
679     }
680     else if (field == F_Same) {
681         if (array_ndx != NULL)
682             return ReportActionNotArray(keymap, action->type, field);
683         if (!ExprResolveBoolean(keymap->ctx, value, &rtrn))
684             return ReportMismatch(keymap, action->type, field, "boolean");
685         if (rtrn.uval)
686             act->flags &= ~XkbSA_SwitchApplication;
687         else
688             act->flags |= XkbSA_SwitchApplication;
689         return true;
690     }
691     return ReportIllegal(keymap, action->type, field);
692 }
693
694 const LookupEntry ctrlNames[] = {
695     { "repeatkeys", XkbRepeatKeysMask },
696     { "repeat", XkbRepeatKeysMask },
697     { "autorepeat", XkbRepeatKeysMask },
698     { "slowkeys", XkbSlowKeysMask },
699     { "bouncekeys", XkbBounceKeysMask },
700     { "stickykeys", XkbStickyKeysMask },
701     { "mousekeys", XkbMouseKeysMask },
702     { "mousekeysaccel", XkbMouseKeysAccelMask },
703     { "accessxkeys", XkbAccessXKeysMask },
704     { "accessxtimeout", XkbAccessXTimeoutMask },
705     { "accessxfeedback", XkbAccessXFeedbackMask },
706     { "audiblebell", XkbAudibleBellMask },
707     { "ignoregrouplock", XkbIgnoreGroupLockMask },
708     { "all", XkbAllBooleanCtrlsMask },
709     { "overlay1", 0 },
710     { "overlay2", 0 },
711     { "none", 0 },
712     { NULL, 0 }
713 };
714
715 static bool
716 HandleSetLockControls(struct xkb_keymap *keymap,
717                       struct xkb_any_action *action,
718                       unsigned field, ExprDef *array_ndx,
719                       ExprDef *value)
720 {
721     ExprResult rtrn;
722     struct xkb_controls_action *act;
723
724     act = (struct xkb_controls_action *) action;
725     if (field == F_Controls) {
726         if (array_ndx != NULL)
727             return ReportActionNotArray(keymap, action->type, field);
728         if (!ExprResolveMask(keymap->ctx, value, &rtrn, ctrlNames))
729             return ReportMismatch(keymap, action->type, field,
730                                   "controls mask");
731         act->ctrls = rtrn.uval;
732         return true;
733     }
734     return ReportIllegal(keymap, action->type, field);
735 }
736
737 static const LookupEntry evNames[] = {
738     { "press", XkbSA_MessageOnPress },
739     { "keypress", XkbSA_MessageOnPress },
740     { "release", XkbSA_MessageOnRelease },
741     { "keyrelease", XkbSA_MessageOnRelease },
742     { "all", XkbSA_MessageOnPress | XkbSA_MessageOnRelease },
743     { "none", 0 },
744     { NULL, 0 }
745 };
746
747 static bool
748 HandleActionMessage(struct xkb_keymap *keymap, struct xkb_any_action *action,
749                     unsigned field, ExprDef *array_ndx, ExprDef *value)
750 {
751     ExprResult rtrn;
752     struct xkb_message_action *act;
753
754     act = (struct xkb_message_action *) action;
755     switch (field) {
756     case F_Report:
757         if (array_ndx != NULL)
758             return ReportActionNotArray(keymap, action->type, field);
759         if (!ExprResolveMask(keymap->ctx, value, &rtrn, evNames))
760             return ReportMismatch(keymap, action->type, field,
761                                   "key event mask");
762         act->flags &= ~(XkbSA_MessageOnPress | XkbSA_MessageOnRelease);
763         act->flags =
764             rtrn.uval & (XkbSA_MessageOnPress | XkbSA_MessageOnRelease);
765         return true;
766
767     case F_GenKeyEvent:
768         if (array_ndx != NULL)
769             return ReportActionNotArray(keymap, action->type, field);
770         if (!ExprResolveBoolean(keymap->ctx, value, &rtrn))
771             return ReportMismatch(keymap, action->type, field, "boolean");
772         if (rtrn.uval)
773             act->flags |= XkbSA_MessageGenKeyEvent;
774         else
775             act->flags &= ~XkbSA_MessageGenKeyEvent;
776         return true;
777
778     case F_Data:
779         if (array_ndx == NULL) {
780             if (!ExprResolveString(keymap->ctx, value, &rtrn))
781                 return ReportMismatch(keymap, action->type, field, "string");
782             else {
783                 int len = strlen(rtrn.str);
784                 if ((len < 1) || (len > 6)) {
785                     log_warn(keymap->ctx,
786                              "An action message can hold only 6 bytes; "
787                              "Extra %d bytes ignored\n", len - 6);
788                 }
789                 strncpy((char *) act->message, rtrn.str, 6);
790             }
791             return true;
792         }
793         else {
794             unsigned ndx;
795             if (!ExprResolveInteger(keymap->ctx, array_ndx, &rtrn)) {
796                 log_err(keymap->ctx,
797                         "Array subscript must be integer; "
798                         "Illegal subscript ignored\n");
799                 return false;
800             }
801             ndx = rtrn.uval;
802             if (ndx > 5) {
803                 log_err(keymap->ctx,
804                         "An action message is at most 6 bytes long; "
805                         "Attempt to use data[%d] ignored\n", ndx);
806                 return false;
807             }
808             if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
809                 return ReportMismatch(keymap, action->type, field, "integer");
810             if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
811                 log_err(keymap->ctx,
812                         "Message data must be in the range 0..255; "
813                         "Illegal datum %d ignored\n", rtrn.ival);
814                 return false;
815             }
816             act->message[ndx] = rtrn.uval;
817         }
818         return true;
819     }
820     return ReportIllegal(keymap, action->type, field);
821 }
822
823 static bool
824 HandleRedirectKey(struct xkb_keymap *keymap, struct xkb_any_action *action,
825                   unsigned field, ExprDef *array_ndx, ExprDef *value)
826 {
827     struct xkb_key *key;
828     struct xkb_redirect_key_action *act;
829     unsigned t1;
830     xkb_mod_mask_t t2;
831     unsigned long tmp;
832     char key_name[XkbKeyNameLength];
833
834     if (array_ndx != NULL)
835         return ReportActionNotArray(keymap, action->type, field);
836
837     act = (struct xkb_redirect_key_action *) action;
838     switch (field) {
839     case F_Keycode:
840         if (!ExprResolveKeyName(keymap->ctx, value, key_name))
841             return ReportMismatch(keymap, action->type, field, "key name");
842
843         tmp = KeyNameToLong(key_name);
844         key = FindNamedKey(keymap, tmp, true, CreateKeyNames(keymap), 0);
845         if (!key)
846             return ReportNotFound(keymap, action->type, field, "Key",
847                                   KeyNameText(key_name));
848         act->new_kc = XkbKeyGetKeycode(keymap, key);
849         return true;
850
851     case F_ModsToClear:
852     case F_Modifiers:
853         t1 = 0;
854         if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
855             act->mods_mask |= (t2 & 0xff);
856             if (field == F_Modifiers)
857                 act->mods |= (t2 & 0xff);
858             else
859                 act->mods &= ~(t2 & 0xff);
860
861             t2 = (t2 >> 8) & 0xffff;
862             act->vmods_mask |= t2;
863             if (field == F_Modifiers)
864                 act->vmods |= t2;
865             else
866                 act->vmods &= ~t2;
867             return true;
868         }
869         return true;
870     }
871     return ReportIllegal(keymap, action->type, field);
872 }
873
874 static bool
875 HandleDeviceBtn(struct xkb_keymap *keymap, struct xkb_any_action *action,
876                 unsigned field, ExprDef *array_ndx, ExprDef *value)
877 {
878     ExprResult rtrn;
879     struct xkb_device_button_action *act;
880
881     act = (struct xkb_device_button_action *) action;
882     if (field == F_Button) {
883         if (array_ndx != NULL)
884             return ReportActionNotArray(keymap, action->type, field);
885         if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
886             return ReportMismatch(keymap, action->type, field,
887                                   "integer (range 1..255)");
888         if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
889             log_err(keymap->ctx,
890                     "Button must specify default or be in the range 1..255; "
891                     "Illegal button value %d ignored\n", rtrn.ival);
892             return false;
893         }
894         act->button = rtrn.ival;
895         return true;
896     }
897     else if ((action->type == XkbSA_LockDeviceBtn) && (field == F_Affect)) {
898         if (array_ndx != NULL)
899             return ReportActionNotArray(keymap, action->type, field);
900         if (!ExprResolveEnum(keymap->ctx, value, &rtrn, lockWhich))
901             return ReportMismatch(keymap, action->type, field,
902                                   "lock or unlock");
903         act->flags &= ~(XkbSA_LockNoLock | XkbSA_LockNoUnlock);
904         act->flags |= rtrn.ival;
905         return true;
906     }
907     else if (field == F_Count) {
908         if (array_ndx != NULL)
909             return ReportActionNotArray(keymap, action->type, field);
910         if (!ExprResolveButton(keymap->ctx, value, &rtrn))
911             return ReportMismatch(keymap, action->type, field, "integer");
912         if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
913             log_err(keymap->ctx,
914                     "The count field must have a value in the range 0..255; "
915                     "Illegal count %d ignored\n", rtrn.ival);
916             return false;
917         }
918         act->count = rtrn.ival;
919         return true;
920     }
921     else if (field == F_Device) {
922         if (array_ndx != NULL)
923             return ReportActionNotArray(keymap, action->type, field);
924         if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
925             return ReportMismatch(keymap, action->type, field,
926                                   "integer (range 1..255)");
927         if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
928             log_err(keymap->ctx,
929                     "Device must specify default or be in the range 1..255; "
930                     "Illegal device value %d ignored\n", rtrn.ival);
931             return false;
932         }
933         act->device = rtrn.ival;
934         return true;
935     }
936     return ReportIllegal(keymap, action->type, field);
937 }
938
939 static bool
940 HandleDeviceValuator(struct xkb_keymap *keymap, struct xkb_any_action *action,
941                      unsigned field, ExprDef *array_ndx, ExprDef *value)
942 {
943 #if 0
944     ExprResult rtrn;
945     struct xkb_device_valuator_action *act;
946
947     act = (struct xkb_device_valuator_action *) action;
948     /*  XXX - Not yet implemented */
949 #endif
950     return false;
951 }
952
953 static bool
954 HandlePrivate(struct xkb_keymap *keymap, struct xkb_any_action *action,
955               unsigned field, ExprDef *array_ndx, ExprDef *value)
956 {
957     ExprResult rtrn;
958
959     switch (field) {
960     case F_Type:
961         if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
962             return ReportMismatch(keymap, PrivateAction, field, "integer");
963         if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
964             log_err(keymap->ctx,
965                     "Private action type must be in the range 0..255; "
966                     "Illegal type %d ignored\n", rtrn.ival);
967             return false;
968         }
969         action->type = rtrn.uval;
970         return true;
971
972     case F_Data:
973         if (array_ndx == NULL) {
974             if (!ExprResolveString(keymap->ctx, value, &rtrn))
975                 return ReportMismatch(keymap, action->type, field, "string");
976             else {
977                 int len = strlen(rtrn.str);
978                 if ((len < 1) || (len > 7)) {
979                     log_warn(keymap->ctx,
980                              "A private action has 7 data bytes; "
981                              "Extra %d bytes ignored\n", len - 6);
982                     return false;
983                 }
984                 strncpy((char *) action->data, rtrn.str, sizeof action->data);
985             }
986             return true;
987         }
988         else {
989             unsigned ndx;
990             if (!ExprResolveInteger(keymap->ctx, array_ndx, &rtrn)) {
991                 log_err(keymap->ctx,
992                         "Array subscript must be integer; "
993                         "Illegal subscript ignored\n");
994                 return false;
995             }
996             ndx = rtrn.uval;
997             if (ndx >= sizeof action->data) {
998                 log_err(keymap->ctx,
999                         "The data for a private action is 18 bytes long; "
1000                         "Attempt to use data[%d] ignored\n", ndx);
1001                 return false;
1002             }
1003             if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
1004                 return ReportMismatch(keymap, action->type, field, "integer");
1005             if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
1006                 log_err(keymap->ctx,
1007                         "All data for a private action must be 0..255; "
1008                         "Illegal datum %d ignored\n", rtrn.ival);
1009                 return false;
1010             }
1011             action->data[ndx] = rtrn.uval;
1012             return true;
1013         }
1014     }
1015     return ReportIllegal(keymap, PrivateAction, field);
1016 }
1017
1018 typedef bool (*actionHandler)(struct xkb_keymap *keymap,
1019                               struct xkb_any_action *action, unsigned field,
1020                               ExprDef *array_ndx, ExprDef *value);
1021
1022 static const actionHandler handleAction[XkbSA_NumActions + 1] = {
1023     [XkbSA_NoAction] = HandleNoAction,
1024     [XkbSA_SetMods] = HandleSetLatchMods,
1025     [XkbSA_LatchMods] = HandleSetLatchMods,
1026     [XkbSA_LockMods] = HandleLockMods,
1027     [XkbSA_SetGroup] = HandleSetLatchGroup,
1028     [XkbSA_LatchGroup] = HandleSetLatchGroup,
1029     [XkbSA_LockGroup] = HandleLockGroup,
1030     [XkbSA_MovePtr] = HandleMovePtr,
1031     [XkbSA_PtrBtn] = HandlePtrBtn,
1032     [XkbSA_LockPtrBtn] = HandlePtrBtn,
1033     [XkbSA_SetPtrDflt] = HandleSetPtrDflt,
1034     [XkbSA_ISOLock] = HandleISOLock,
1035     [XkbSA_Terminate] = HandleNoAction,
1036     [XkbSA_SwitchScreen] = HandleSwitchScreen,
1037     [XkbSA_SetControls] = HandleSetLockControls,
1038     [XkbSA_LockControls] = HandleSetLockControls,
1039     [XkbSA_ActionMessage] = HandleActionMessage,
1040     [XkbSA_RedirectKey] = HandleRedirectKey,
1041     [XkbSA_DeviceBtn] = HandleDeviceBtn,
1042     [XkbSA_LockDeviceBtn] = HandleDeviceBtn,
1043     [XkbSA_DeviceValuator] = HandleDeviceValuator,
1044     [PrivateAction] = HandlePrivate,
1045 };
1046
1047 /***====================================================================***/
1048
1049 static void
1050 ApplyActionFactoryDefaults(union xkb_action * action)
1051 {
1052     if (action->type == XkbSA_SetPtrDflt) { /* increment default button */
1053         action->dflt.affect = XkbSA_AffectDfltBtn;
1054         action->dflt.flags = 0;
1055         action->dflt.value = 1;
1056     }
1057     else if (action->type == XkbSA_ISOLock) {
1058         action->iso.real_mods = LockMask;
1059     }
1060 }
1061
1062 static void
1063 ActionsInit(struct xkb_context *ctx);
1064
1065 int
1066 HandleActionDef(ExprDef * def,
1067                 struct xkb_keymap *keymap,
1068                 struct xkb_any_action *action, ActionInfo *info)
1069 {
1070     ExprDef *arg;
1071     const char *str;
1072     unsigned tmp, hndlrType;
1073
1074     if (!actionsInitialized)
1075         ActionsInit(keymap->ctx);
1076
1077     if (def->op != EXPR_ACTION_DECL) {
1078         log_err(keymap->ctx, "Expected an action definition, found %s\n",
1079                 exprOpText(def->op));
1080         return false;
1081     }
1082     str = xkb_atom_text(keymap->ctx, def->value.action.name);
1083     if (!str) {
1084         log_wsgo(keymap->ctx, "Missing name in action definition!!\n");
1085         return false;
1086     }
1087     if (!stringToAction(str, &tmp)) {
1088         log_err(keymap->ctx, "Unknown action %s\n", str);
1089         return false;
1090     }
1091     action->type = hndlrType = tmp;
1092     if (action->type != XkbSA_NoAction) {
1093         ApplyActionFactoryDefaults((union xkb_action *) action);
1094         while (info)
1095         {
1096             if ((info->action == XkbSA_NoAction)
1097                 || (info->action == hndlrType)) {
1098                 if (!(*handleAction[hndlrType])(keymap, action,
1099                                                 info->field,
1100                                                 info->array_ndx,
1101                                                 info->value)) {
1102                     return false;
1103                 }
1104             }
1105             info = info->next;
1106         }
1107     }
1108     for (arg = def->value.action.args; arg != NULL;
1109          arg = (ExprDef *) arg->common.next) {
1110         ExprDef *field, *value, *arrayRtrn;
1111         ExprResult elemRtrn, fieldRtrn;
1112         unsigned fieldNdx;
1113
1114         if (arg->op == EXPR_ASSIGN) {
1115             field = arg->value.binary.left;
1116             value = arg->value.binary.right;
1117         }
1118         else {
1119             if (arg->op == EXPR_NOT || arg->op == EXPR_INVERT) {
1120                 field = arg->value.child;
1121                 constFalse.value.str = xkb_atom_intern(keymap->ctx, "false");
1122                 value = &constFalse;
1123             }
1124             else {
1125                 field = arg;
1126                 constTrue.value.str = xkb_atom_intern(keymap->ctx, "true");
1127                 value = &constTrue;
1128             }
1129         }
1130         if (!ExprResolveLhs(keymap, field, &elemRtrn, &fieldRtrn, &arrayRtrn))
1131             return false;       /* internal error -- already reported */
1132
1133         if (elemRtrn.str != NULL) {
1134             log_err(keymap->ctx,
1135                     "Cannot change defaults in an action definition; "
1136                     "Ignoring attempt to change %s.%s\n",
1137                     elemRtrn.str, fieldRtrn.str);
1138             return false;
1139         }
1140         if (!stringToField(fieldRtrn.str, &fieldNdx)) {
1141             log_err(keymap->ctx, "Unknown field name %s\n", fieldRtrn.str);
1142             return false;
1143         }
1144         if (!(*handleAction[hndlrType])(keymap, action, fieldNdx, arrayRtrn,
1145                                         value))
1146             return false;
1147     }
1148     return true;
1149 }
1150
1151 /***====================================================================***/
1152
1153 int
1154 SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field,
1155                ExprDef *array_ndx, ExprDef *value, ActionInfo **info_rtrn)
1156 {
1157     ActionInfo *new, *old;
1158
1159     if (!actionsInitialized)
1160         ActionsInit(keymap->ctx);
1161
1162     new = malloc(sizeof(*new));
1163     if (!new) {
1164         log_wsgo(keymap->ctx, "Couldn't allocate space for action default\n");
1165         return false;
1166     }
1167
1168     if (istreq(elem, "action"))
1169         new->action = XkbSA_NoAction;
1170     else {
1171         if (!stringToAction(elem, &new->action)) {
1172             free(new);
1173             return false;
1174         }
1175         if (new->action == XkbSA_NoAction) {
1176             log_err(keymap->ctx,
1177                     "\"%s\" is not a valid field in a NoAction action\n",
1178                     field);
1179             free(new);
1180             return false;
1181         }
1182     }
1183     if (!stringToField(field, &new->field)) {
1184         log_err(keymap->ctx, "\"%s\" is not a legal field name\n", field);
1185         free(new);
1186         return false;
1187     }
1188     new->array_ndx = array_ndx;
1189     new->value = value;
1190     new->next = NULL;
1191     old = *info_rtrn;
1192     while ((old) && (old->next))
1193         old = old->next;
1194     if (old == NULL)
1195         *info_rtrn = new;
1196     else
1197         old->next = new;
1198     return true;
1199 }
1200
1201 /***====================================================================***/
1202
1203 static void
1204 ActionsInit(struct xkb_context *ctx)
1205 {
1206     if (!actionsInitialized) {
1207         memset(&constTrue, 0, sizeof(constTrue));
1208         memset(&constFalse, 0, sizeof(constFalse));
1209         constTrue.common.type = STMT_EXPR;
1210         constTrue.common.next = NULL;
1211         constTrue.op = EXPR_IDENT;
1212         constTrue.value_type = EXPR_TYPE_BOOLEAN;
1213         constTrue.value.str = xkb_atom_intern(ctx, "true");
1214         constFalse.common.type = STMT_EXPR;
1215         constFalse.common.next = NULL;
1216         constFalse.op = EXPR_IDENT;
1217         constFalse.value_type = EXPR_TYPE_BOOLEAN;
1218         constFalse.value.str = xkb_atom_intern(ctx, "false");
1219         actionsInitialized = 1;
1220     }
1221 }
1222
1223 union xkb_action *
1224 ResizeKeyActions(struct xkb_keymap *keymap, struct xkb_key *key,
1225                  uint32_t needed)
1226 {
1227     size_t old_ndx, old_num_acts, new_ndx;
1228
1229     if (needed == 0) {
1230         key->acts_index = 0;
1231         return NULL;
1232     }
1233
1234     if (XkbKeyHasActions(key) && key->width >= needed)
1235         return XkbKeyActionsPtr(keymap, key);
1236
1237     /*
1238      * The key may already be in the array, but without enough space.
1239      * This should not happen often, so in order to avoid moving and
1240      * copying stuff from acts and key_acts, we just allocate new
1241      * space for the key at the end, and leave the old space alone.
1242      */
1243
1244     old_ndx = key->acts_index;
1245     old_num_acts = XkbKeyNumActions(key);
1246     new_ndx = darray_size(keymap->acts);
1247
1248     darray_resize0(keymap->acts, new_ndx + needed);
1249     key->acts_index = new_ndx;
1250
1251     /*
1252      * The key was already in the array, copy the old actions to the
1253      * new space.
1254      */
1255     if (old_ndx != 0)
1256         memcpy(darray_mem(keymap->acts, new_ndx),
1257                darray_mem(keymap->acts, old_ndx),
1258                old_num_acts * sizeof(union xkb_action));
1259
1260     return XkbKeyActionsPtr(keymap, key);
1261 }