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