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