Fix sign-compare warnings
[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 /*
28  * Copyright © 2012 Intel Corporation
29  * Copyright © 2012 Ran Benita <ran234@gmail.com>
30  *
31  * Permission is hereby granted, free of charge, to any person obtaining a
32  * copy of this software and associated documentation files (the "Software"),
33  * to deal in the Software without restriction, including without limitation
34  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35  * and/or sell copies of the Software, and to permit persons to whom the
36  * Software is furnished to do so, subject to the following conditions:
37  *
38  * The above copyright notice and this permission notice (including the next
39  * paragraph) shall be included in all copies or substantial portions of the
40  * Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
45  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48  * DEALINGS IN THE SOFTWARE.
49  *
50  * Author: Daniel Stone <daniel@fooishbar.org>
51  *         Ran Benita <ran234@gmail.com>
52  */
53
54 #include "xkbcomp-priv.h"
55 #include "text.h"
56 #include "expr.h"
57 #include "action.h"
58
59 static const ExprBoolean constTrue = {
60     .expr = {
61         .common = { .type = STMT_EXPR, .next = NULL },
62         .op = EXPR_VALUE,
63         .value_type = EXPR_TYPE_BOOLEAN,
64     },
65     .set = true,
66 };
67
68 static const ExprBoolean constFalse = {
69     .expr = {
70         .common = { .type = STMT_EXPR, .next = NULL },
71         .op = EXPR_VALUE,
72         .value_type = EXPR_TYPE_BOOLEAN,
73     },
74     .set = false,
75 };
76
77 enum action_field {
78     ACTION_FIELD_CLEAR_LOCKS,
79     ACTION_FIELD_LATCH_TO_LOCK,
80     ACTION_FIELD_GEN_KEY_EVENT,
81     ACTION_FIELD_REPORT,
82     ACTION_FIELD_DEFAULT,
83     ACTION_FIELD_AFFECT,
84     ACTION_FIELD_INCREMENT,
85     ACTION_FIELD_MODIFIERS,
86     ACTION_FIELD_GROUP,
87     ACTION_FIELD_X,
88     ACTION_FIELD_Y,
89     ACTION_FIELD_ACCEL,
90     ACTION_FIELD_BUTTON,
91     ACTION_FIELD_VALUE,
92     ACTION_FIELD_CONTROLS,
93     ACTION_FIELD_TYPE,
94     ACTION_FIELD_COUNT,
95     ACTION_FIELD_SCREEN,
96     ACTION_FIELD_SAME,
97     ACTION_FIELD_DATA,
98     ACTION_FIELD_DEVICE,
99     ACTION_FIELD_KEYCODE,
100     ACTION_FIELD_MODS_TO_CLEAR,
101 };
102
103 ActionsInfo *
104 NewActionsInfo(void)
105 {
106     unsigned type;
107     ActionsInfo *info;
108
109     info = calloc(1, sizeof(*info));
110     if (!info)
111         return NULL;
112
113     for (type = 0; type < _ACTION_TYPE_NUM_ENTRIES; type++)
114         info->actions[type].type = type;
115
116     /* Apply some "factory defaults". */
117
118     /* Increment default button. */
119     info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.flags = 0;
120     info->actions[ACTION_TYPE_PTR_DEFAULT].dflt.value = 1;
121
122     return info;
123 }
124
125 void
126 FreeActionsInfo(ActionsInfo *info)
127 {
128     free(info);
129 }
130
131 static const LookupEntry fieldStrings[] = {
132     { "clearLocks",       ACTION_FIELD_CLEAR_LOCKS   },
133     { "latchToLock",      ACTION_FIELD_LATCH_TO_LOCK },
134     { "genKeyEvent",      ACTION_FIELD_GEN_KEY_EVENT },
135     { "generateKeyEvent", ACTION_FIELD_GEN_KEY_EVENT },
136     { "report",           ACTION_FIELD_REPORT        },
137     { "default",          ACTION_FIELD_DEFAULT       },
138     { "affect",           ACTION_FIELD_AFFECT        },
139     { "increment",        ACTION_FIELD_INCREMENT     },
140     { "modifiers",        ACTION_FIELD_MODIFIERS     },
141     { "mods",             ACTION_FIELD_MODIFIERS     },
142     { "group",            ACTION_FIELD_GROUP         },
143     { "x",                ACTION_FIELD_X             },
144     { "y",                ACTION_FIELD_Y             },
145     { "accel",            ACTION_FIELD_ACCEL         },
146     { "accelerate",       ACTION_FIELD_ACCEL         },
147     { "repeat",           ACTION_FIELD_ACCEL         },
148     { "button",           ACTION_FIELD_BUTTON        },
149     { "value",            ACTION_FIELD_VALUE         },
150     { "controls",         ACTION_FIELD_CONTROLS      },
151     { "ctrls",            ACTION_FIELD_CONTROLS      },
152     { "type",             ACTION_FIELD_TYPE          },
153     { "count",            ACTION_FIELD_COUNT         },
154     { "screen",           ACTION_FIELD_SCREEN        },
155     { "same",             ACTION_FIELD_SAME          },
156     { "sameServer",       ACTION_FIELD_SAME          },
157     { "data",             ACTION_FIELD_DATA          },
158     { "device",           ACTION_FIELD_DEVICE        },
159     { "dev",              ACTION_FIELD_DEVICE        },
160     { "key",              ACTION_FIELD_KEYCODE       },
161     { "keycode",          ACTION_FIELD_KEYCODE       },
162     { "kc",               ACTION_FIELD_KEYCODE       },
163     { "clearmods",        ACTION_FIELD_MODS_TO_CLEAR },
164     { "clearmodifiers",   ACTION_FIELD_MODS_TO_CLEAR },
165     { NULL,               0                          }
166 };
167
168 static bool
169 stringToAction(const char *str, unsigned *type_rtrn)
170 {
171     return LookupString(actionTypeNames, str, type_rtrn);
172 }
173
174 static bool
175 stringToField(const char *str, enum action_field *field_rtrn)
176 {
177     return LookupString(fieldStrings, str, field_rtrn);
178 }
179
180 static const char *
181 fieldText(enum action_field field)
182 {
183     return LookupValue(fieldStrings, field);
184 }
185
186 /***====================================================================***/
187
188 static inline bool
189 ReportMismatch(struct xkb_keymap *keymap, enum xkb_action_type action,
190                enum action_field field, const char *type)
191 {
192     log_err(keymap->ctx,
193             "Value of %s field must be of type %s; "
194             "Action %s definition ignored\n",
195             fieldText(field), type, ActionTypeText(action));
196     return false;
197 }
198
199 static inline bool
200 ReportIllegal(struct xkb_keymap *keymap, enum xkb_action_type action,
201               enum action_field field)
202 {
203     log_err(keymap->ctx,
204             "Field %s is not defined for an action of type %s; "
205             "Action definition ignored\n",
206             fieldText(field), ActionTypeText(action));
207     return false;
208 }
209
210 static inline bool
211 ReportActionNotArray(struct xkb_keymap *keymap, enum xkb_action_type action,
212                      enum action_field field)
213 {
214     log_err(keymap->ctx,
215             "The %s field in the %s action is not an array; "
216             "Action definition ignored\n",
217             fieldText(field), ActionTypeText(action));
218     return false;
219 }
220
221 static bool
222 HandleNoAction(struct xkb_keymap *keymap, union xkb_action *action,
223                enum action_field field, const ExprDef *array_ndx,
224                const ExprDef *value)
225
226 {
227     return true;
228 }
229
230 static bool
231 CheckLatchLockFlags(struct xkb_keymap *keymap, enum xkb_action_type action,
232                     enum action_field field, const ExprDef *value,
233                     enum xkb_action_flags *flags_inout)
234 {
235     enum xkb_action_flags tmp;
236     bool result;
237
238     if (field == ACTION_FIELD_CLEAR_LOCKS)
239         tmp = ACTION_LOCK_CLEAR;
240     else if (field == ACTION_FIELD_LATCH_TO_LOCK)
241         tmp = ACTION_LATCH_TO_LOCK;
242     else
243         return false;           /* WSGO! */
244
245     if (!ExprResolveBoolean(keymap->ctx, value, &result))
246         return ReportMismatch(keymap, action, field, "boolean");
247
248     if (result)
249         *flags_inout |= tmp;
250     else
251         *flags_inout &= ~tmp;
252
253     return true;
254 }
255
256 static bool
257 CheckModifierField(struct xkb_keymap *keymap, enum xkb_action_type action,
258                    const ExprDef *value, enum xkb_action_flags *flags_inout,
259                    xkb_mod_mask_t *mods_rtrn)
260 {
261     if (value->expr.op == EXPR_IDENT) {
262         const char *valStr;
263         valStr = xkb_atom_text(keymap->ctx, value->ident.ident);
264         if (valStr && (istreq(valStr, "usemodmapmods") ||
265                        istreq(valStr, "modmapmods"))) {
266
267             *mods_rtrn = 0;
268             *flags_inout |= ACTION_MODS_LOOKUP_MODMAP;
269             return true;
270         }
271     }
272
273     if (!ExprResolveModMask(keymap, value, MOD_BOTH, mods_rtrn))
274         return ReportMismatch(keymap, action,
275                               ACTION_FIELD_MODIFIERS, "modifier mask");
276
277     *flags_inout &= ~ACTION_MODS_LOOKUP_MODMAP;
278     return true;
279 }
280
281 static bool
282 HandleSetLatchMods(struct xkb_keymap *keymap, union xkb_action *action,
283                    enum action_field field, const ExprDef *array_ndx,
284                    const ExprDef *value)
285 {
286     struct xkb_mod_action *act = &action->mods;
287     enum xkb_action_flags rtrn, t1;
288     xkb_mod_mask_t t2;
289
290     if (array_ndx != NULL) {
291         switch (field) {
292         case ACTION_FIELD_CLEAR_LOCKS:
293         case ACTION_FIELD_LATCH_TO_LOCK:
294         case ACTION_FIELD_MODIFIERS:
295             return ReportActionNotArray(keymap, action->type, field);
296         default:
297             break;
298         }
299     }
300
301     switch (field) {
302     case ACTION_FIELD_CLEAR_LOCKS:
303     case ACTION_FIELD_LATCH_TO_LOCK:
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 ACTION_FIELD_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     default:
321         break;
322     }
323
324     return ReportIllegal(keymap, action->type, field);
325 }
326
327 static bool
328 HandleLockMods(struct xkb_keymap *keymap, union xkb_action *action,
329                enum action_field field, const ExprDef *array_ndx,
330                const ExprDef *value)
331 {
332     struct xkb_mod_action *act = &action->mods;
333     enum xkb_action_flags t1;
334     xkb_mod_mask_t t2;
335
336     if (array_ndx && field == ACTION_FIELD_MODIFIERS)
337         return ReportActionNotArray(keymap, action->type, field);
338
339     switch (field) {
340     case ACTION_FIELD_MODIFIERS:
341         t1 = act->flags;
342         if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
343             act->flags = t1;
344             act->mods.mods = t2;
345             return true;
346         }
347         return false;
348
349     default:
350         break;
351     }
352
353     return ReportIllegal(keymap, action->type, field);
354 }
355
356 static bool
357 CheckGroupField(struct xkb_keymap *keymap, unsigned action,
358                 const ExprDef *value, enum xkb_action_flags *flags_inout,
359                 xkb_layout_index_t *grp_rtrn)
360 {
361     const ExprDef *spec;
362
363     if (value->expr.op == EXPR_NEGATE || value->expr.op == EXPR_UNARY_PLUS) {
364         *flags_inout &= ~ACTION_ABSOLUTE_SWITCH;
365         spec = value->unary.child;
366     }
367     else {
368         *flags_inout |= ACTION_ABSOLUTE_SWITCH;
369         spec = value;
370     }
371
372     if (!ExprResolveGroup(keymap->ctx, spec, grp_rtrn))
373         return ReportMismatch(keymap, action, ACTION_FIELD_GROUP,
374                               "integer (range 1..8)");
375
376     if (value->expr.op == EXPR_NEGATE)
377         *grp_rtrn = -*grp_rtrn;
378     else if (value->expr.op != EXPR_UNARY_PLUS)
379         (*grp_rtrn)--;
380
381     return true;
382 }
383
384 static bool
385 HandleSetLatchGroup(struct xkb_keymap *keymap, union xkb_action *action,
386                     enum action_field field, const ExprDef *array_ndx,
387                     const ExprDef *value)
388 {
389     struct xkb_group_action *act = &action->group;
390     enum xkb_action_flags rtrn, t1;
391     xkb_layout_index_t t2;
392
393     if (array_ndx != NULL) {
394         switch (field) {
395         case ACTION_FIELD_CLEAR_LOCKS:
396         case ACTION_FIELD_LATCH_TO_LOCK:
397         case ACTION_FIELD_GROUP:
398             return ReportActionNotArray(keymap, action->type, field);
399
400         default:
401             break;
402         }
403     }
404
405     switch (field) {
406     case ACTION_FIELD_CLEAR_LOCKS:
407     case ACTION_FIELD_LATCH_TO_LOCK:
408         rtrn = act->flags;
409         if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
410             act->flags = rtrn;
411             return true;
412         }
413         return false;
414
415     case ACTION_FIELD_GROUP:
416         t1 = act->flags;
417         if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
418             act->flags = t1;
419             act->group = t2;
420             return true;
421         }
422         return false;
423
424     default:
425         break;
426     }
427
428     return ReportIllegal(keymap, action->type, field);
429 }
430
431 static bool
432 HandleLockGroup(struct xkb_keymap *keymap, union xkb_action *action,
433                 enum action_field field, const ExprDef *array_ndx,
434                 const ExprDef *value)
435 {
436     struct xkb_group_action *act = &action->group;
437     enum xkb_action_flags t1;
438     xkb_layout_index_t t2;
439
440     if ((array_ndx != NULL) && (field == ACTION_FIELD_GROUP))
441         return ReportActionNotArray(keymap, action->type, field);
442     if (field == ACTION_FIELD_GROUP) {
443         t1 = act->flags;
444         if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
445             act->flags = t1;
446             act->group = t2;
447             return true;
448         }
449         return false;
450     }
451     return ReportIllegal(keymap, action->type, field);
452 }
453
454 static bool
455 HandleMovePtr(struct xkb_keymap *keymap, union xkb_action *action,
456               enum action_field field, const ExprDef *array_ndx,
457               const ExprDef *value)
458 {
459     struct xkb_pointer_action *act = &action->ptr;
460
461     if (array_ndx && (field == ACTION_FIELD_X || field == ACTION_FIELD_Y))
462         return ReportActionNotArray(keymap, action->type, field);
463
464     if (field == ACTION_FIELD_X || field == ACTION_FIELD_Y) {
465         int val;
466         const bool absolute = (value->expr.op != EXPR_NEGATE &&
467                                value->expr.op != EXPR_UNARY_PLUS);
468
469         if (!ExprResolveInteger(keymap->ctx, value, &val))
470             return ReportMismatch(keymap, action->type, field, "integer");
471
472         if (val < INT16_MIN || val > INT16_MAX) {
473             log_err(keymap->ctx,
474                     "The %s field in the %s action must be in range %d..%d; "
475                     "Action definition ignored\n",
476                     fieldText(field), ActionTypeText(action->type),
477                     INT16_MIN, INT16_MAX);
478             return false;
479         }
480
481         if (field == ACTION_FIELD_X) {
482             if (absolute)
483                 act->flags |= ACTION_ABSOLUTE_X;
484             act->x = (int16_t) val;
485         }
486         else {
487             if (absolute)
488                 act->flags |= ACTION_ABSOLUTE_Y;
489             act->y = (int16_t) val;
490         }
491
492         return true;
493     }
494     else if (field == ACTION_FIELD_ACCEL) {
495         bool set;
496
497         if (!ExprResolveBoolean(keymap->ctx, value, &set))
498             return ReportMismatch(keymap, action->type, field, "boolean");
499
500         if (set)
501             act->flags &= ~ACTION_NO_ACCEL;
502         else
503             act->flags |= ACTION_NO_ACCEL;
504     }
505
506     return ReportIllegal(keymap, action->type, field);
507 }
508
509 static const LookupEntry lockWhich[] = {
510     { "both", 0 },
511     { "lock", ACTION_LOCK_NO_UNLOCK },
512     { "neither", (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK) },
513     { "unlock", ACTION_LOCK_NO_LOCK },
514     { NULL, 0 }
515 };
516
517 static bool
518 HandlePtrBtn(struct xkb_keymap *keymap, union xkb_action *action,
519              enum action_field field, const ExprDef *array_ndx,
520              const ExprDef *value)
521 {
522     struct xkb_pointer_button_action *act = &action->btn;
523
524     if (field == ACTION_FIELD_BUTTON) {
525         int btn;
526
527         if (array_ndx)
528             return ReportActionNotArray(keymap, action->type, field);
529
530         if (!ExprResolveButton(keymap->ctx, value, &btn))
531             return ReportMismatch(keymap, action->type, field,
532                                   "integer (range 1..5)");
533
534         if (btn < 0 || btn > 5) {
535             log_err(keymap->ctx,
536                     "Button must specify default or be in the range 1..5; "
537                     "Illegal button value %d ignored\n", btn);
538             return false;
539         }
540
541         act->button = btn;
542         return true;
543     }
544     else if (action->type == ACTION_TYPE_PTR_LOCK &&
545              field == ACTION_FIELD_AFFECT) {
546         enum xkb_action_flags val;
547
548         if (array_ndx)
549             return ReportActionNotArray(keymap, action->type, field);
550
551         if (!ExprResolveEnum(keymap->ctx, value, &val, lockWhich))
552             return ReportMismatch(keymap, action->type, field,
553                                   "lock or unlock");
554
555         act->flags &= ~(ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK);
556         act->flags |= val;
557         return true;
558     }
559     else if (field == ACTION_FIELD_COUNT) {
560         int val;
561
562         if (array_ndx)
563             return ReportActionNotArray(keymap, action->type, field);
564
565         if (!ExprResolveInteger(keymap->ctx, value, &val))
566             return ReportMismatch(keymap, action->type, field, "integer");
567
568         if (val < 0 || val > 255) {
569             log_err(keymap->ctx,
570                     "The count field must have a value in the range 0..255; "
571                     "Illegal count %d ignored\n", val);
572             return false;
573         }
574
575         act->count = (uint16_t) val;
576         return true;
577     }
578     return ReportIllegal(keymap, action->type, field);
579 }
580
581 static const LookupEntry ptrDflts[] = {
582     { "dfltbtn", 1 },
583     { "defaultbutton", 1 },
584     { "button", 1 },
585     { NULL, 0 }
586 };
587
588 static bool
589 HandleSetPtrDflt(struct xkb_keymap *keymap, union xkb_action *action,
590                  enum action_field field, const ExprDef *array_ndx,
591                  const ExprDef *value)
592 {
593     struct xkb_pointer_default_action *act = &action->dflt;
594
595     if (field == ACTION_FIELD_AFFECT) {
596         unsigned int val;
597
598         if (array_ndx)
599             return ReportActionNotArray(keymap, action->type, field);
600
601         if (!ExprResolveEnum(keymap->ctx, value, &val, ptrDflts))
602             return ReportMismatch(keymap, action->type, field,
603                                   "pointer component");
604         return true;
605     }
606     else if (field == ACTION_FIELD_BUTTON || field == ACTION_FIELD_VALUE) {
607         const ExprDef *button;
608         int btn;
609
610         if (array_ndx)
611             return ReportActionNotArray(keymap, action->type, field);
612
613         if (value->expr.op == EXPR_NEGATE ||
614             value->expr.op == EXPR_UNARY_PLUS) {
615             act->flags &= ~ACTION_ABSOLUTE_SWITCH;
616             button = value->unary.child;
617         }
618         else {
619             act->flags |= ACTION_ABSOLUTE_SWITCH;
620             button = value;
621         }
622
623         if (!ExprResolveButton(keymap->ctx, button, &btn))
624             return ReportMismatch(keymap, action->type, field,
625                                   "integer (range 1..5)");
626
627         if (btn < 0 || btn > 5) {
628             log_err(keymap->ctx,
629                     "New default button value must be in the range 1..5; "
630                     "Illegal default button value %d ignored\n", btn);
631             return false;
632         }
633         if (btn == 0) {
634             log_err(keymap->ctx,
635                     "Cannot set default pointer button to \"default\"; "
636                     "Illegal default button setting ignored\n");
637             return false;
638         }
639
640         act->value = (value->expr.op == EXPR_NEGATE ? -btn: btn);
641         return true;
642     }
643
644     return ReportIllegal(keymap, action->type, field);
645 }
646
647 static bool
648 HandleSwitchScreen(struct xkb_keymap *keymap, union xkb_action *action,
649                    enum action_field field, const ExprDef *array_ndx,
650                    const ExprDef *value)
651 {
652     struct xkb_switch_screen_action *act = &action->screen;
653
654     if (field == ACTION_FIELD_SCREEN) {
655         const ExprDef *scrn;
656         int val;
657
658         if (array_ndx)
659             return ReportActionNotArray(keymap, action->type, field);
660
661         if (value->expr.op == EXPR_NEGATE ||
662             value->expr.op == EXPR_UNARY_PLUS) {
663             act->flags &= ~ACTION_ABSOLUTE_SWITCH;
664             scrn = value->unary.child;
665         }
666         else {
667             act->flags |= ACTION_ABSOLUTE_SWITCH;
668             scrn = value;
669         }
670
671         if (!ExprResolveInteger(keymap->ctx, scrn, &val))
672             return ReportMismatch(keymap, action->type, field,
673                                   "integer (0..255)");
674
675         if (val < 0 || val > 255) {
676             log_err(keymap->ctx,
677                     "Screen index must be in the range 1..255; "
678                     "Illegal screen value %d ignored\n", val);
679             return false;
680         }
681
682         act->screen = (value->expr.op == EXPR_NEGATE ? -val : val);
683         return true;
684     }
685     else if (field == ACTION_FIELD_SAME) {
686         bool set;
687
688         if (array_ndx)
689             return ReportActionNotArray(keymap, action->type, field);
690
691         if (!ExprResolveBoolean(keymap->ctx, value, &set))
692             return ReportMismatch(keymap, action->type, field, "boolean");
693
694         if (set)
695             act->flags &= ~ACTION_SAME_SCREEN;
696         else
697             act->flags |= ACTION_SAME_SCREEN;
698
699         return true;
700     }
701
702     return ReportIllegal(keymap, action->type, field);
703 }
704
705 static bool
706 HandleSetLockControls(struct xkb_keymap *keymap, union xkb_action *action,
707                       enum action_field field, const ExprDef *array_ndx,
708                       const ExprDef *value)
709 {
710     struct xkb_controls_action *act = &action->ctrls;
711
712     if (field == ACTION_FIELD_CONTROLS) {
713         unsigned int mask;
714
715         if (array_ndx)
716             return ReportActionNotArray(keymap, action->type, field);
717
718         if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlMaskNames))
719             return ReportMismatch(keymap, action->type, field,
720                                   "controls mask");
721
722         act->ctrls = mask;
723         return true;
724     }
725
726     return ReportIllegal(keymap, action->type, field);
727 }
728
729 static bool
730 HandlePrivate(struct xkb_keymap *keymap, union xkb_action *action,
731               enum action_field field, const ExprDef *array_ndx,
732               const ExprDef *value)
733 {
734     struct xkb_private_action *act = &action->priv;
735
736     if (field == ACTION_FIELD_TYPE) {
737         int type;
738
739         if (!ExprResolveInteger(keymap->ctx, value, &type))
740             return ReportMismatch(keymap, ACTION_TYPE_PRIVATE, field, "integer");
741
742         if (type < 0 || type > 255) {
743             log_err(keymap->ctx,
744                     "Private action type must be in the range 0..255; "
745                     "Illegal type %d ignored\n", type);
746             return false;
747         }
748
749         /*
750          * It's possible for someone to write something like this:
751          *      actions = [ Private(type=3,data[0]=1,data[1]=3,data[2]=3) ]
752          * where the type refers to some existing action type, e.g. LockMods.
753          * This assumes that this action's struct is laid out in memory
754          * exactly as described in the XKB specification and libraries.
755          * We, however, have changed these structs in various ways, so this
756          * assumption is no longer true. Since this is a lousy "feature", we
757          * make actions like these no-ops for now.
758          */
759         if (type < ACTION_TYPE_PRIVATE) {
760             log_info(keymap->ctx,
761                      "Private actions of type %s are not supported; Ignored\n",
762                      ActionTypeText(type));
763             act->type = ACTION_TYPE_NONE;
764         }
765         else {
766             act->type = (enum xkb_action_type) type;
767         }
768
769         return true;
770     }
771     else if (field == ACTION_FIELD_DATA) {
772         if (array_ndx == NULL) {
773             xkb_atom_t val;
774             const char *str;
775             int len;
776
777             if (!ExprResolveString(keymap->ctx, value, &val))
778                 return ReportMismatch(keymap, action->type, field, "string");
779
780             str = xkb_atom_text(keymap->ctx, val);
781             len = strlen(str);
782             if (len < 1 || len > 7) {
783                 log_warn(keymap->ctx,
784                          "A private action has 7 data bytes; "
785                          "Extra %d bytes ignored\n", len - 6);
786                 return false;
787             }
788
789             strncpy((char *) act->data, str, sizeof(act->data));
790             return true;
791         }
792         else {
793             int ndx, datum;
794
795             if (!ExprResolveInteger(keymap->ctx, array_ndx, &ndx)) {
796                 log_err(keymap->ctx,
797                         "Array subscript must be integer; "
798                         "Illegal subscript ignored\n");
799                 return false;
800             }
801
802             if (ndx < 0 || (size_t) ndx >= sizeof(act->data)) {
803                 log_err(keymap->ctx,
804                         "The data for a private action is %lu bytes long; "
805                         "Attempt to use data[%d] ignored\n",
806                         (unsigned long) sizeof(act->data), ndx);
807                 return false;
808             }
809
810             if (!ExprResolveInteger(keymap->ctx, value, &datum))
811                 return ReportMismatch(keymap, act->type, field, "integer");
812
813             if (datum < 0 || datum > 255) {
814                 log_err(keymap->ctx,
815                         "All data for a private action must be 0..255; "
816                         "Illegal datum %d ignored\n", datum);
817                 return false;
818             }
819
820             act->data[ndx] = (uint8_t) datum;
821             return true;
822         }
823     }
824
825     return ReportIllegal(keymap, ACTION_TYPE_NONE, field);
826 }
827
828 typedef bool (*actionHandler)(struct xkb_keymap *keymap,
829                               union xkb_action *action,
830                               enum action_field field,
831                               const ExprDef *array_ndx,
832                               const ExprDef *value);
833
834 static const actionHandler handleAction[_ACTION_TYPE_NUM_ENTRIES] = {
835     [ACTION_TYPE_NONE] = HandleNoAction,
836     [ACTION_TYPE_MOD_SET] = HandleSetLatchMods,
837     [ACTION_TYPE_MOD_LATCH] = HandleSetLatchMods,
838     [ACTION_TYPE_MOD_LOCK] = HandleLockMods,
839     [ACTION_TYPE_GROUP_SET] = HandleSetLatchGroup,
840     [ACTION_TYPE_GROUP_LATCH] = HandleSetLatchGroup,
841     [ACTION_TYPE_GROUP_LOCK] = HandleLockGroup,
842     [ACTION_TYPE_PTR_MOVE] = HandleMovePtr,
843     [ACTION_TYPE_PTR_BUTTON] = HandlePtrBtn,
844     [ACTION_TYPE_PTR_LOCK] = HandlePtrBtn,
845     [ACTION_TYPE_PTR_DEFAULT] = HandleSetPtrDflt,
846     [ACTION_TYPE_TERMINATE] = HandleNoAction,
847     [ACTION_TYPE_SWITCH_VT] = HandleSwitchScreen,
848     [ACTION_TYPE_CTRL_SET] = HandleSetLockControls,
849     [ACTION_TYPE_CTRL_LOCK] = HandleSetLockControls,
850     [ACTION_TYPE_PRIVATE] = HandlePrivate,
851 };
852
853 /***====================================================================***/
854
855 bool
856 HandleActionDef(ExprDef *def, struct xkb_keymap *keymap,
857                 union xkb_action *action, ActionsInfo *info)
858 {
859     ExprDef *arg;
860     const char *str;
861     unsigned handler_type;
862
863     if (def->expr.op != EXPR_ACTION_DECL) {
864         log_err(keymap->ctx, "Expected an action definition, found %s\n",
865                 expr_op_type_to_string(def->expr.op));
866         return false;
867     }
868
869     str = xkb_atom_text(keymap->ctx, def->action.name);
870     if (!stringToAction(str, &handler_type)) {
871         log_err(keymap->ctx, "Unknown action %s\n", str);
872         return false;
873     }
874
875     /*
876      * Get the default values for this action type, as modified by
877      * statements such as:
878      *     latchMods.clearLocks = True;
879      */
880     *action = info->actions[handler_type];
881
882     /*
883      * Now change the action properties as specified for this
884      * particular instance, e.g. "modifiers" and "clearLocks" in:
885      *     SetMods(modifiers=Alt,clearLocks);
886      */
887     for (arg = def->action.args; arg != NULL;
888          arg = (ExprDef *) arg->common.next) {
889         const ExprDef *value;
890         ExprDef *field, *arrayRtrn;
891         const char *elemRtrn, *fieldRtrn;
892         enum action_field fieldNdx;
893
894         if (arg->expr.op == EXPR_ASSIGN) {
895             field = arg->binary.left;
896             value = arg->binary.right;
897         }
898         else if (arg->expr.op == EXPR_NOT || arg->expr.op == EXPR_INVERT) {
899             field = arg->unary.child;
900             value = (const ExprDef *) &constFalse;
901         }
902         else {
903             field = arg;
904             value = (const ExprDef *) &constTrue;
905         }
906
907         if (!ExprResolveLhs(keymap->ctx, field, &elemRtrn, &fieldRtrn,
908                             &arrayRtrn))
909             return false;
910
911         if (elemRtrn) {
912             log_err(keymap->ctx,
913                     "Cannot change defaults in an action definition; "
914                     "Ignoring attempt to change %s.%s\n",
915                     elemRtrn, fieldRtrn);
916             return false;
917         }
918
919         if (!stringToField(fieldRtrn, &fieldNdx)) {
920             log_err(keymap->ctx, "Unknown field name %s\n", fieldRtrn);
921             return false;
922         }
923
924         if (!handleAction[handler_type](keymap, action, fieldNdx, arrayRtrn,
925                                         value))
926             return false;
927     }
928
929     return true;
930 }
931
932
933 bool
934 SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field,
935                ExprDef *array_ndx, ExprDef *value, ActionsInfo *info)
936 {
937     unsigned action;
938     enum action_field action_field;
939
940     if (!stringToAction(elem, &action))
941         return false;
942
943     if (!stringToField(field, &action_field)) {
944         log_err(keymap->ctx, "\"%s\" is not a legal field name\n", field);
945         return false;
946     }
947
948     return handleAction[action](keymap, &info->actions[action],
949                                 action_field, array_ndx, value);
950 }