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