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