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