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