expr: drop ExprResult from ResolveString
[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     const char *str;
753     struct xkb_message_action *act;
754
755     act = (struct xkb_message_action *) action;
756     switch (field) {
757     case F_Report:
758         if (array_ndx != NULL)
759             return ReportActionNotArray(keymap, action->type, field);
760         if (!ExprResolveMask(keymap->ctx, value, &rtrn, evNames))
761             return ReportMismatch(keymap, action->type, field,
762                                   "key event mask");
763         act->flags &= ~(XkbSA_MessageOnPress | XkbSA_MessageOnRelease);
764         act->flags =
765             rtrn.uval & (XkbSA_MessageOnPress | XkbSA_MessageOnRelease);
766         return true;
767
768     case F_GenKeyEvent:
769         if (array_ndx != NULL)
770             return ReportActionNotArray(keymap, action->type, field);
771         if (!ExprResolveBoolean(keymap->ctx, value, &rtrn))
772             return ReportMismatch(keymap, action->type, field, "boolean");
773         if (rtrn.uval)
774             act->flags |= XkbSA_MessageGenKeyEvent;
775         else
776             act->flags &= ~XkbSA_MessageGenKeyEvent;
777         return true;
778
779     case F_Data:
780         if (array_ndx == NULL) {
781             int len;
782
783             if (!ExprResolveString(keymap->ctx, value, &str))
784                 return ReportMismatch(keymap, action->type, field, "string");
785
786             len = strlen(str);
787             if (len < 1 || len > 6) {
788                 log_warn(keymap->ctx,
789                          "An action message can hold only 6 bytes; "
790                          "Extra %d bytes ignored\n", len - 6);
791             }
792
793             strncpy((char *) act->message, str, 6);
794             return true;
795         }
796         else {
797             unsigned ndx;
798             if (!ExprResolveInteger(keymap->ctx, array_ndx, &rtrn)) {
799                 log_err(keymap->ctx,
800                         "Array subscript must be integer; "
801                         "Illegal subscript ignored\n");
802                 return false;
803             }
804             ndx = rtrn.uval;
805             if (ndx > 5) {
806                 log_err(keymap->ctx,
807                         "An action message is at most 6 bytes long; "
808                         "Attempt to use data[%d] ignored\n", ndx);
809                 return false;
810             }
811             if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
812                 return ReportMismatch(keymap, action->type, field, "integer");
813             if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
814                 log_err(keymap->ctx,
815                         "Message data must be in the range 0..255; "
816                         "Illegal datum %d ignored\n", rtrn.ival);
817                 return false;
818             }
819             act->message[ndx] = rtrn.uval;
820         }
821         return true;
822     }
823     return ReportIllegal(keymap, action->type, field);
824 }
825
826 static bool
827 HandleRedirectKey(struct xkb_keymap *keymap, struct xkb_any_action *action,
828                   unsigned field, ExprDef *array_ndx, ExprDef *value)
829 {
830     struct xkb_key *key;
831     struct xkb_redirect_key_action *act;
832     unsigned t1;
833     xkb_mod_mask_t t2;
834     unsigned long tmp;
835     char key_name[XkbKeyNameLength];
836
837     if (array_ndx != NULL)
838         return ReportActionNotArray(keymap, action->type, field);
839
840     act = (struct xkb_redirect_key_action *) action;
841     switch (field) {
842     case F_Keycode:
843         if (!ExprResolveKeyName(keymap->ctx, value, key_name))
844             return ReportMismatch(keymap, action->type, field, "key name");
845
846         tmp = KeyNameToLong(key_name);
847         key = FindNamedKey(keymap, tmp, true, CreateKeyNames(keymap), 0);
848         if (!key)
849             return ReportNotFound(keymap, action->type, field, "Key",
850                                   KeyNameText(key_name));
851         act->new_kc = XkbKeyGetKeycode(keymap, key);
852         return true;
853
854     case F_ModsToClear:
855     case F_Modifiers:
856         t1 = 0;
857         if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
858             act->mods_mask |= (t2 & 0xff);
859             if (field == F_Modifiers)
860                 act->mods |= (t2 & 0xff);
861             else
862                 act->mods &= ~(t2 & 0xff);
863
864             t2 = (t2 >> 8) & 0xffff;
865             act->vmods_mask |= t2;
866             if (field == F_Modifiers)
867                 act->vmods |= t2;
868             else
869                 act->vmods &= ~t2;
870             return true;
871         }
872         return true;
873     }
874     return ReportIllegal(keymap, action->type, field);
875 }
876
877 static bool
878 HandleDeviceBtn(struct xkb_keymap *keymap, struct xkb_any_action *action,
879                 unsigned field, ExprDef *array_ndx, ExprDef *value)
880 {
881     ExprResult rtrn;
882     struct xkb_device_button_action *act;
883
884     act = (struct xkb_device_button_action *) action;
885     if (field == F_Button) {
886         if (array_ndx != NULL)
887             return ReportActionNotArray(keymap, action->type, field);
888         if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
889             return ReportMismatch(keymap, action->type, field,
890                                   "integer (range 1..255)");
891         if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
892             log_err(keymap->ctx,
893                     "Button must specify default or be in the range 1..255; "
894                     "Illegal button value %d ignored\n", rtrn.ival);
895             return false;
896         }
897         act->button = rtrn.ival;
898         return true;
899     }
900     else if ((action->type == XkbSA_LockDeviceBtn) && (field == F_Affect)) {
901         if (array_ndx != NULL)
902             return ReportActionNotArray(keymap, action->type, field);
903         if (!ExprResolveEnum(keymap->ctx, value, &rtrn, lockWhich))
904             return ReportMismatch(keymap, action->type, field,
905                                   "lock or unlock");
906         act->flags &= ~(XkbSA_LockNoLock | XkbSA_LockNoUnlock);
907         act->flags |= rtrn.ival;
908         return true;
909     }
910     else if (field == F_Count) {
911         if (array_ndx != NULL)
912             return ReportActionNotArray(keymap, action->type, field);
913         if (!ExprResolveButton(keymap->ctx, value, &rtrn))
914             return ReportMismatch(keymap, action->type, field, "integer");
915         if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
916             log_err(keymap->ctx,
917                     "The count field must have a value in the range 0..255; "
918                     "Illegal count %d ignored\n", rtrn.ival);
919             return false;
920         }
921         act->count = rtrn.ival;
922         return true;
923     }
924     else if (field == F_Device) {
925         if (array_ndx != NULL)
926             return ReportActionNotArray(keymap, action->type, field);
927         if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
928             return ReportMismatch(keymap, action->type, field,
929                                   "integer (range 1..255)");
930         if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
931             log_err(keymap->ctx,
932                     "Device must specify default or be in the range 1..255; "
933                     "Illegal device value %d ignored\n", rtrn.ival);
934             return false;
935         }
936         act->device = rtrn.ival;
937         return true;
938     }
939     return ReportIllegal(keymap, action->type, field);
940 }
941
942 static bool
943 HandleDeviceValuator(struct xkb_keymap *keymap, struct xkb_any_action *action,
944                      unsigned field, ExprDef *array_ndx, ExprDef *value)
945 {
946 #if 0
947     ExprResult rtrn;
948     struct xkb_device_valuator_action *act;
949
950     act = (struct xkb_device_valuator_action *) action;
951     /*  XXX - Not yet implemented */
952 #endif
953     return false;
954 }
955
956 static bool
957 HandlePrivate(struct xkb_keymap *keymap, struct xkb_any_action *action,
958               unsigned field, ExprDef *array_ndx, ExprDef *value)
959 {
960     ExprResult rtrn;
961
962     switch (field) {
963     case F_Type:
964         if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
965             return ReportMismatch(keymap, PrivateAction, field, "integer");
966         if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
967             log_err(keymap->ctx,
968                     "Private action type must be in the range 0..255; "
969                     "Illegal type %d ignored\n", rtrn.ival);
970             return false;
971         }
972         action->type = rtrn.uval;
973         return true;
974
975     case F_Data:
976         if (array_ndx == NULL) {
977             const char *str;
978             int len;
979
980             if (!ExprResolveString(keymap->ctx, value, &str))
981                 return ReportMismatch(keymap, action->type, field, "string");
982
983             len = strlen(str);
984             if (len < 1 || len > 7) {
985                 log_warn(keymap->ctx,
986                          "A private action has 7 data bytes; "
987                          "Extra %d bytes ignored\n", len - 6);
988                 return false;
989             }
990
991             strncpy((char *) action->data, str, sizeof(action->data));
992             return true;
993         }
994         else {
995             unsigned ndx;
996             if (!ExprResolveInteger(keymap->ctx, array_ndx, &rtrn)) {
997                 log_err(keymap->ctx,
998                         "Array subscript must be integer; "
999                         "Illegal subscript ignored\n");
1000                 return false;
1001             }
1002             ndx = rtrn.uval;
1003             if (ndx >= sizeof action->data) {
1004                 log_err(keymap->ctx,
1005                         "The data for a private action is 18 bytes long; "
1006                         "Attempt to use data[%d] ignored\n", ndx);
1007                 return false;
1008             }
1009             if (!ExprResolveInteger(keymap->ctx, value, &rtrn))
1010                 return ReportMismatch(keymap, action->type, field, "integer");
1011             if ((rtrn.ival < 0) || (rtrn.ival > 255)) {
1012                 log_err(keymap->ctx,
1013                         "All data for a private action must be 0..255; "
1014                         "Illegal datum %d ignored\n", rtrn.ival);
1015                 return false;
1016             }
1017             action->data[ndx] = rtrn.uval;
1018             return true;
1019         }
1020     }
1021     return ReportIllegal(keymap, PrivateAction, field);
1022 }
1023
1024 typedef bool (*actionHandler)(struct xkb_keymap *keymap,
1025                               struct xkb_any_action *action, unsigned field,
1026                               ExprDef *array_ndx, ExprDef *value);
1027
1028 static const actionHandler handleAction[XkbSA_NumActions + 1] = {
1029     [XkbSA_NoAction] = HandleNoAction,
1030     [XkbSA_SetMods] = HandleSetLatchMods,
1031     [XkbSA_LatchMods] = HandleSetLatchMods,
1032     [XkbSA_LockMods] = HandleLockMods,
1033     [XkbSA_SetGroup] = HandleSetLatchGroup,
1034     [XkbSA_LatchGroup] = HandleSetLatchGroup,
1035     [XkbSA_LockGroup] = HandleLockGroup,
1036     [XkbSA_MovePtr] = HandleMovePtr,
1037     [XkbSA_PtrBtn] = HandlePtrBtn,
1038     [XkbSA_LockPtrBtn] = HandlePtrBtn,
1039     [XkbSA_SetPtrDflt] = HandleSetPtrDflt,
1040     [XkbSA_ISOLock] = HandleISOLock,
1041     [XkbSA_Terminate] = HandleNoAction,
1042     [XkbSA_SwitchScreen] = HandleSwitchScreen,
1043     [XkbSA_SetControls] = HandleSetLockControls,
1044     [XkbSA_LockControls] = HandleSetLockControls,
1045     [XkbSA_ActionMessage] = HandleActionMessage,
1046     [XkbSA_RedirectKey] = HandleRedirectKey,
1047     [XkbSA_DeviceBtn] = HandleDeviceBtn,
1048     [XkbSA_LockDeviceBtn] = HandleDeviceBtn,
1049     [XkbSA_DeviceValuator] = HandleDeviceValuator,
1050     [PrivateAction] = HandlePrivate,
1051 };
1052
1053 /***====================================================================***/
1054
1055 static void
1056 ApplyActionFactoryDefaults(union xkb_action * action)
1057 {
1058     if (action->type == XkbSA_SetPtrDflt) { /* increment default button */
1059         action->dflt.affect = XkbSA_AffectDfltBtn;
1060         action->dflt.flags = 0;
1061         action->dflt.value = 1;
1062     }
1063     else if (action->type == XkbSA_ISOLock) {
1064         action->iso.real_mods = LockMask;
1065     }
1066 }
1067
1068 static void
1069 ActionsInit(struct xkb_context *ctx);
1070
1071 int
1072 HandleActionDef(ExprDef * def,
1073                 struct xkb_keymap *keymap,
1074                 struct xkb_any_action *action, ActionInfo *info)
1075 {
1076     ExprDef *arg;
1077     const char *str;
1078     unsigned tmp, hndlrType;
1079
1080     if (!actionsInitialized)
1081         ActionsInit(keymap->ctx);
1082
1083     if (def->op != EXPR_ACTION_DECL) {
1084         log_err(keymap->ctx, "Expected an action definition, found %s\n",
1085                 exprOpText(def->op));
1086         return false;
1087     }
1088     str = xkb_atom_text(keymap->ctx, def->value.action.name);
1089     if (!str) {
1090         log_wsgo(keymap->ctx, "Missing name in action definition!!\n");
1091         return false;
1092     }
1093     if (!stringToAction(str, &tmp)) {
1094         log_err(keymap->ctx, "Unknown action %s\n", str);
1095         return false;
1096     }
1097     action->type = hndlrType = tmp;
1098     if (action->type != XkbSA_NoAction) {
1099         ApplyActionFactoryDefaults((union xkb_action *) action);
1100         while (info)
1101         {
1102             if ((info->action == XkbSA_NoAction)
1103                 || (info->action == hndlrType)) {
1104                 if (!(*handleAction[hndlrType])(keymap, action,
1105                                                 info->field,
1106                                                 info->array_ndx,
1107                                                 info->value)) {
1108                     return false;
1109                 }
1110             }
1111             info = info->next;
1112         }
1113     }
1114     for (arg = def->value.action.args; arg != NULL;
1115          arg = (ExprDef *) arg->common.next) {
1116         ExprDef *field, *value, *arrayRtrn;
1117         const char *elemRtrn, *fieldRtrn;
1118         unsigned fieldNdx;
1119
1120         if (arg->op == EXPR_ASSIGN) {
1121             field = arg->value.binary.left;
1122             value = arg->value.binary.right;
1123         }
1124         else {
1125             if (arg->op == EXPR_NOT || arg->op == EXPR_INVERT) {
1126                 field = arg->value.child;
1127                 constFalse.value.str = xkb_atom_intern(keymap->ctx, "false");
1128                 value = &constFalse;
1129             }
1130             else {
1131                 field = arg;
1132                 constTrue.value.str = xkb_atom_intern(keymap->ctx, "true");
1133                 value = &constTrue;
1134             }
1135         }
1136         if (!ExprResolveLhs(keymap->ctx, field, &elemRtrn, &fieldRtrn,
1137                             &arrayRtrn))
1138             return false;       /* internal error -- already reported */
1139
1140         if (elemRtrn != NULL) {
1141             log_err(keymap->ctx,
1142                     "Cannot change defaults in an action definition; "
1143                     "Ignoring attempt to change %s.%s\n",
1144                     elemRtrn, fieldRtrn);
1145             return false;
1146         }
1147         if (!stringToField(fieldRtrn, &fieldNdx)) {
1148             log_err(keymap->ctx, "Unknown field name %s\n", fieldRtrn);
1149             return false;
1150         }
1151         if (!handleAction[hndlrType](keymap, action, fieldNdx, arrayRtrn,
1152                                      value))
1153             return false;
1154     }
1155     return true;
1156 }
1157
1158 /***====================================================================***/
1159
1160 int
1161 SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field,
1162                ExprDef *array_ndx, ExprDef *value, ActionInfo **info_rtrn)
1163 {
1164     ActionInfo *new, *old;
1165
1166     if (!actionsInitialized)
1167         ActionsInit(keymap->ctx);
1168
1169     new = malloc(sizeof(*new));
1170     if (!new) {
1171         log_wsgo(keymap->ctx, "Couldn't allocate space for action default\n");
1172         return false;
1173     }
1174
1175     if (istreq(elem, "action"))
1176         new->action = XkbSA_NoAction;
1177     else {
1178         if (!stringToAction(elem, &new->action)) {
1179             free(new);
1180             return false;
1181         }
1182         if (new->action == XkbSA_NoAction) {
1183             log_err(keymap->ctx,
1184                     "\"%s\" is not a valid field in a NoAction action\n",
1185                     field);
1186             free(new);
1187             return false;
1188         }
1189     }
1190     if (!stringToField(field, &new->field)) {
1191         log_err(keymap->ctx, "\"%s\" is not a legal field name\n", field);
1192         free(new);
1193         return false;
1194     }
1195     new->array_ndx = array_ndx;
1196     new->value = value;
1197     new->next = NULL;
1198     old = *info_rtrn;
1199     while ((old) && (old->next))
1200         old = old->next;
1201     if (old == NULL)
1202         *info_rtrn = new;
1203     else
1204         old->next = new;
1205     return true;
1206 }
1207
1208 /***====================================================================***/
1209
1210 static void
1211 ActionsInit(struct xkb_context *ctx)
1212 {
1213     if (!actionsInitialized) {
1214         memset(&constTrue, 0, sizeof(constTrue));
1215         memset(&constFalse, 0, sizeof(constFalse));
1216         constTrue.common.type = STMT_EXPR;
1217         constTrue.common.next = NULL;
1218         constTrue.op = EXPR_IDENT;
1219         constTrue.value_type = EXPR_TYPE_BOOLEAN;
1220         constTrue.value.str = xkb_atom_intern(ctx, "true");
1221         constFalse.common.type = STMT_EXPR;
1222         constFalse.common.next = NULL;
1223         constFalse.op = EXPR_IDENT;
1224         constFalse.value_type = EXPR_TYPE_BOOLEAN;
1225         constFalse.value.str = xkb_atom_intern(ctx, "false");
1226         actionsInitialized = 1;
1227     }
1228 }
1229
1230 union xkb_action *
1231 ResizeKeyActions(struct xkb_keymap *keymap, struct xkb_key *key,
1232                  uint32_t needed)
1233 {
1234     size_t old_ndx, old_num_acts, new_ndx;
1235
1236     if (needed == 0) {
1237         key->acts_index = 0;
1238         return NULL;
1239     }
1240
1241     if (XkbKeyHasActions(key) && key->width >= needed)
1242         return XkbKeyActionsPtr(keymap, key);
1243
1244     /*
1245      * The key may already be in the array, but without enough space.
1246      * This should not happen often, so in order to avoid moving and
1247      * copying stuff from acts and key_acts, we just allocate new
1248      * space for the key at the end, and leave the old space alone.
1249      */
1250
1251     old_ndx = key->acts_index;
1252     old_num_acts = XkbKeyNumActions(key);
1253     new_ndx = darray_size(keymap->acts);
1254
1255     darray_resize0(keymap->acts, new_ndx + needed);
1256     key->acts_index = new_ndx;
1257
1258     /*
1259      * The key was already in the array, copy the old actions to the
1260      * new space.
1261      */
1262     if (old_ndx != 0)
1263         memcpy(darray_mem(keymap->acts, new_ndx),
1264                darray_mem(keymap->acts, old_ndx),
1265                old_num_acts * sizeof(union xkb_action));
1266
1267     return XkbKeyActionsPtr(keymap, key);
1268 }