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