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