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