expr: split expression types and allocate them separately
[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 inline bool
222 ReportNotFound(struct xkb_keymap *keymap, enum xkb_action_type action,
223                enum action_field field, const char *what, const char *bad)
224 {
225     log_err(keymap->ctx,
226             "%s named %s not found; "
227             "Ignoring the %s field of an %s action\n",
228             what, bad, fieldText(field), ActionTypeText(action));
229     return false;
230 }
231
232 static bool
233 HandleNoAction(struct xkb_keymap *keymap, union xkb_action *action,
234                enum action_field field, const ExprDef *array_ndx,
235                const ExprDef *value)
236
237 {
238     return true;
239 }
240
241 static bool
242 CheckLatchLockFlags(struct xkb_keymap *keymap, enum xkb_action_type action,
243                     enum action_field field, const ExprDef *value,
244                     enum xkb_action_flags *flags_inout)
245 {
246     enum xkb_action_flags tmp;
247     bool result;
248
249     if (field == ACTION_FIELD_CLEAR_LOCKS)
250         tmp = ACTION_LOCK_CLEAR;
251     else if (field == ACTION_FIELD_LATCH_TO_LOCK)
252         tmp = ACTION_LATCH_TO_LOCK;
253     else
254         return false;           /* WSGO! */
255
256     if (!ExprResolveBoolean(keymap->ctx, value, &result))
257         return ReportMismatch(keymap, action, field, "boolean");
258
259     if (result)
260         *flags_inout |= tmp;
261     else
262         *flags_inout &= ~tmp;
263
264     return true;
265 }
266
267 static bool
268 CheckModifierField(struct xkb_keymap *keymap, enum xkb_action_type action,
269                    const ExprDef *value, enum xkb_action_flags *flags_inout,
270                    xkb_mod_mask_t *mods_rtrn)
271 {
272     if (value->expr.op == EXPR_IDENT) {
273         const char *valStr;
274         valStr = xkb_atom_text(keymap->ctx, value->ident.ident);
275         if (valStr && (istreq(valStr, "usemodmapmods") ||
276                        istreq(valStr, "modmapmods"))) {
277
278             *mods_rtrn = 0;
279             *flags_inout |= ACTION_MODS_LOOKUP_MODMAP;
280             return true;
281         }
282     }
283
284     if (!ExprResolveModMask(keymap, value, MOD_BOTH, mods_rtrn))
285         return ReportMismatch(keymap, action,
286                               ACTION_FIELD_MODIFIERS, "modifier mask");
287
288     *flags_inout &= ~ACTION_MODS_LOOKUP_MODMAP;
289     return true;
290 }
291
292 static bool
293 HandleSetLatchMods(struct xkb_keymap *keymap, union xkb_action *action,
294                    enum action_field field, const ExprDef *array_ndx,
295                    const ExprDef *value)
296 {
297     struct xkb_mod_action *act = &action->mods;
298     enum xkb_action_flags rtrn, t1;
299     xkb_mod_mask_t t2;
300
301     if (array_ndx != NULL) {
302         switch (field) {
303         case ACTION_FIELD_CLEAR_LOCKS:
304         case ACTION_FIELD_LATCH_TO_LOCK:
305         case ACTION_FIELD_MODIFIERS:
306             return ReportActionNotArray(keymap, action->type, field);
307         default:
308             break;
309         }
310     }
311
312     switch (field) {
313     case ACTION_FIELD_CLEAR_LOCKS:
314     case ACTION_FIELD_LATCH_TO_LOCK:
315         rtrn = act->flags;
316         if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
317             act->flags = rtrn;
318             return true;
319         }
320         return false;
321
322     case ACTION_FIELD_MODIFIERS:
323         t1 = act->flags;
324         if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
325             act->flags = t1;
326             act->mods.mods = t2;
327             return true;
328         }
329         return false;
330
331     default:
332         break;
333     }
334
335     return ReportIllegal(keymap, action->type, field);
336 }
337
338 static bool
339 HandleLockMods(struct xkb_keymap *keymap, union xkb_action *action,
340                enum action_field field, const ExprDef *array_ndx,
341                const ExprDef *value)
342 {
343     struct xkb_mod_action *act = &action->mods;
344     enum xkb_action_flags t1;
345     xkb_mod_mask_t t2;
346
347     if (array_ndx && field == ACTION_FIELD_MODIFIERS)
348         return ReportActionNotArray(keymap, action->type, field);
349
350     switch (field) {
351     case ACTION_FIELD_MODIFIERS:
352         t1 = act->flags;
353         if (CheckModifierField(keymap, action->type, value, &t1, &t2)) {
354             act->flags = t1;
355             act->mods.mods = t2;
356             return true;
357         }
358         return false;
359
360     default:
361         break;
362     }
363
364     return ReportIllegal(keymap, action->type, field);
365 }
366
367 static bool
368 CheckGroupField(struct xkb_keymap *keymap, unsigned action,
369                 const ExprDef *value, enum xkb_action_flags *flags_inout,
370                 xkb_layout_index_t *grp_rtrn)
371 {
372     const ExprDef *spec;
373
374     if (value->expr.op == EXPR_NEGATE || value->expr.op == EXPR_UNARY_PLUS) {
375         *flags_inout &= ~ACTION_ABSOLUTE_SWITCH;
376         spec = value->unary.child;
377     }
378     else {
379         *flags_inout |= ACTION_ABSOLUTE_SWITCH;
380         spec = value;
381     }
382
383     if (!ExprResolveGroup(keymap->ctx, spec, grp_rtrn))
384         return ReportMismatch(keymap, action, ACTION_FIELD_GROUP,
385                               "integer (range 1..8)");
386
387     if (value->expr.op == EXPR_NEGATE)
388         *grp_rtrn = -*grp_rtrn;
389     else if (value->expr.op != EXPR_UNARY_PLUS)
390         (*grp_rtrn)--;
391
392     return true;
393 }
394
395 static bool
396 HandleSetLatchGroup(struct xkb_keymap *keymap, union xkb_action *action,
397                     enum action_field field, const ExprDef *array_ndx,
398                     const ExprDef *value)
399 {
400     struct xkb_group_action *act = &action->group;
401     enum xkb_action_flags rtrn, t1;
402     xkb_layout_index_t t2;
403
404     if (array_ndx != NULL) {
405         switch (field) {
406         case ACTION_FIELD_CLEAR_LOCKS:
407         case ACTION_FIELD_LATCH_TO_LOCK:
408         case ACTION_FIELD_GROUP:
409             return ReportActionNotArray(keymap, action->type, field);
410
411         default:
412             break;
413         }
414     }
415
416     switch (field) {
417     case ACTION_FIELD_CLEAR_LOCKS:
418     case ACTION_FIELD_LATCH_TO_LOCK:
419         rtrn = act->flags;
420         if (CheckLatchLockFlags(keymap, action->type, field, value, &rtrn)) {
421             act->flags = rtrn;
422             return true;
423         }
424         return false;
425
426     case ACTION_FIELD_GROUP:
427         t1 = act->flags;
428         if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
429             act->flags = t1;
430             act->group = t2;
431             return true;
432         }
433         return false;
434
435     default:
436         break;
437     }
438
439     return ReportIllegal(keymap, action->type, field);
440 }
441
442 static bool
443 HandleLockGroup(struct xkb_keymap *keymap, union xkb_action *action,
444                 enum action_field field, const ExprDef *array_ndx,
445                 const ExprDef *value)
446 {
447     struct xkb_group_action *act = &action->group;
448     enum xkb_action_flags t1;
449     xkb_layout_index_t t2;
450
451     if ((array_ndx != NULL) && (field == ACTION_FIELD_GROUP))
452         return ReportActionNotArray(keymap, action->type, field);
453     if (field == ACTION_FIELD_GROUP) {
454         t1 = act->flags;
455         if (CheckGroupField(keymap, action->type, value, &t1, &t2)) {
456             act->flags = t1;
457             act->group = t2;
458             return true;
459         }
460         return false;
461     }
462     return ReportIllegal(keymap, action->type, field);
463 }
464
465 static bool
466 HandleMovePtr(struct xkb_keymap *keymap, union xkb_action *action,
467               enum action_field field, const ExprDef *array_ndx,
468               const ExprDef *value)
469 {
470     struct xkb_pointer_action *act = &action->ptr;
471     bool absolute;
472
473     if (array_ndx && (field == ACTION_FIELD_X || field == ACTION_FIELD_Y))
474         return ReportActionNotArray(keymap, action->type, field);
475
476     if (field == ACTION_FIELD_X || field == ACTION_FIELD_Y) {
477         int val;
478
479         if (value->expr.op == EXPR_NEGATE || value->expr.op == EXPR_UNARY_PLUS)
480             absolute = false;
481         else
482             absolute = true;
483
484         if (!ExprResolveInteger(keymap->ctx, value, &val))
485             return ReportMismatch(keymap, action->type, field, "integer");
486
487         if (field == ACTION_FIELD_X) {
488             if (absolute)
489                 act->flags |= ACTION_ABSOLUTE_X;
490             act->x = val;
491         }
492         else {
493             if (absolute)
494                 act->flags |= ACTION_ABSOLUTE_Y;
495             act->y = val;
496         }
497
498         return true;
499     }
500     else if (field == ACTION_FIELD_ACCEL) {
501         bool set;
502
503         if (!ExprResolveBoolean(keymap->ctx, value, &set))
504             return ReportMismatch(keymap, action->type, field, "boolean");
505
506         if (set)
507             act->flags &= ~ACTION_NO_ACCEL;
508         else
509             act->flags |= ACTION_NO_ACCEL;
510     }
511
512     return ReportIllegal(keymap, action->type, field);
513 }
514
515 static const LookupEntry lockWhich[] = {
516     { "both", 0 },
517     { "lock", ACTION_LOCK_NO_UNLOCK },
518     { "neither", (ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK) },
519     { "unlock", ACTION_LOCK_NO_LOCK },
520     { NULL, 0 }
521 };
522
523 static bool
524 HandlePtrBtn(struct xkb_keymap *keymap, union xkb_action *action,
525              enum action_field field, const ExprDef *array_ndx,
526              const ExprDef *value)
527 {
528     struct xkb_pointer_button_action *act = &action->btn;
529
530     if (field == ACTION_FIELD_BUTTON) {
531         int btn;
532
533         if (array_ndx)
534             return ReportActionNotArray(keymap, action->type, field);
535
536         if (!ExprResolveButton(keymap->ctx, value, &btn))
537             return ReportMismatch(keymap, action->type, field,
538                                   "integer (range 1..5)");
539
540         if (btn < 0 || btn > 5) {
541             log_err(keymap->ctx,
542                     "Button must specify default or be in the range 1..5; "
543                     "Illegal button value %d ignored\n", btn);
544             return false;
545         }
546
547         act->button = btn;
548         return true;
549     }
550     else if (action->type == ACTION_TYPE_PTR_LOCK &&
551              field == ACTION_FIELD_AFFECT) {
552         enum xkb_action_flags val;
553
554         if (array_ndx)
555             return ReportActionNotArray(keymap, action->type, field);
556
557         if (!ExprResolveEnum(keymap->ctx, value, &val, lockWhich))
558             return ReportMismatch(keymap, action->type, field,
559                                   "lock or unlock");
560
561         act->flags &= ~(ACTION_LOCK_NO_LOCK | ACTION_LOCK_NO_UNLOCK);
562         act->flags |= val;
563         return true;
564     }
565     else if (field == ACTION_FIELD_COUNT) {
566         int btn;
567
568         if (array_ndx)
569             return ReportActionNotArray(keymap, action->type, field);
570
571         /* XXX: Should this actually be ResolveButton? */
572         if (!ExprResolveButton(keymap->ctx, value, &btn))
573             return ReportMismatch(keymap, action->type, field, "integer");
574
575         if (btn < 0 || btn > 255) {
576             log_err(keymap->ctx,
577                     "The count field must have a value in the range 0..255; "
578                     "Illegal count %d ignored\n", btn);
579             return false;
580         }
581
582         act->count = btn;
583         return true;
584     }
585     return ReportIllegal(keymap, action->type, field);
586 }
587
588 static const LookupEntry ptrDflts[] = {
589     { "dfltbtn", 1 },
590     { "defaultbutton", 1 },
591     { "button", 1 },
592     { NULL, 0 }
593 };
594
595 static bool
596 HandleSetPtrDflt(struct xkb_keymap *keymap, union xkb_action *action,
597                  enum action_field field, const ExprDef *array_ndx,
598                  const ExprDef *value)
599 {
600     struct xkb_pointer_default_action *act = &action->dflt;
601
602     if (field == ACTION_FIELD_AFFECT) {
603         unsigned int val;
604
605         if (array_ndx)
606             return ReportActionNotArray(keymap, action->type, field);
607
608         if (!ExprResolveEnum(keymap->ctx, value, &val, ptrDflts))
609             return ReportMismatch(keymap, action->type, field,
610                                   "pointer component");
611         return true;
612     }
613     else if (field == ACTION_FIELD_BUTTON || field == ACTION_FIELD_VALUE) {
614         const ExprDef *button;
615         int btn;
616
617         if (array_ndx)
618             return ReportActionNotArray(keymap, action->type, field);
619
620         if (value->expr.op == EXPR_NEGATE ||
621             value->expr.op == EXPR_UNARY_PLUS) {
622             act->flags &= ~ACTION_ABSOLUTE_SWITCH;
623             button = value->unary.child;
624         }
625         else {
626             act->flags |= ACTION_ABSOLUTE_SWITCH;
627             button = value;
628         }
629
630         if (!ExprResolveButton(keymap->ctx, button, &btn))
631             return ReportMismatch(keymap, action->type, field,
632                                   "integer (range 1..5)");
633
634         if (btn < 0 || btn > 5) {
635             log_err(keymap->ctx,
636                     "New default button value must be in the range 1..5; "
637                     "Illegal default button value %d ignored\n", btn);
638             return false;
639         }
640         if (btn == 0) {
641             log_err(keymap->ctx,
642                     "Cannot set default pointer button to \"default\"; "
643                     "Illegal default button setting ignored\n");
644             return false;
645         }
646
647         act->value = (value->expr.op == EXPR_NEGATE ? -btn: btn);
648         return true;
649     }
650
651     return ReportIllegal(keymap, action->type, field);
652 }
653
654 static bool
655 HandleSwitchScreen(struct xkb_keymap *keymap, union xkb_action *action,
656                    enum action_field field, const ExprDef *array_ndx,
657                    const ExprDef *value)
658 {
659     struct xkb_switch_screen_action *act = &action->screen;
660
661     if (field == ACTION_FIELD_SCREEN) {
662         const ExprDef *scrn;
663         int val;
664
665         if (array_ndx)
666             return ReportActionNotArray(keymap, action->type, field);
667
668         if (value->expr.op == EXPR_NEGATE ||
669             value->expr.op == EXPR_UNARY_PLUS) {
670             act->flags &= ~ACTION_ABSOLUTE_SWITCH;
671             scrn = value->unary.child;
672         }
673         else {
674             act->flags |= ACTION_ABSOLUTE_SWITCH;
675             scrn = value;
676         }
677
678         if (!ExprResolveInteger(keymap->ctx, scrn, &val))
679             return ReportMismatch(keymap, action->type, field,
680                                   "integer (0..255)");
681
682         if (val < 0 || val > 255) {
683             log_err(keymap->ctx,
684                     "Screen index must be in the range 1..255; "
685                     "Illegal screen value %d ignored\n", val);
686             return false;
687         }
688
689         act->screen = (value->expr.op == EXPR_NEGATE ? -val : val);
690         return true;
691     }
692     else if (field == ACTION_FIELD_SAME) {
693         bool set;
694
695         if (array_ndx)
696             return ReportActionNotArray(keymap, action->type, field);
697
698         if (!ExprResolveBoolean(keymap->ctx, value, &set))
699             return ReportMismatch(keymap, action->type, field, "boolean");
700
701         if (set)
702             act->flags &= ~ACTION_SAME_SCREEN;
703         else
704             act->flags |= ACTION_SAME_SCREEN;
705
706         return true;
707     }
708
709     return ReportIllegal(keymap, action->type, field);
710 }
711
712 static bool
713 HandleSetLockControls(struct xkb_keymap *keymap, union xkb_action *action,
714                       enum action_field field, const ExprDef *array_ndx,
715                       const ExprDef *value)
716 {
717     struct xkb_controls_action *act = &action->ctrls;
718
719     if (field == ACTION_FIELD_CONTROLS) {
720         unsigned int mask;
721
722         if (array_ndx)
723             return ReportActionNotArray(keymap, action->type, field);
724
725         if (!ExprResolveMask(keymap->ctx, value, &mask, ctrlMaskNames))
726             return ReportMismatch(keymap, action->type, field,
727                                   "controls mask");
728
729         act->ctrls = mask;
730         return true;
731     }
732
733     return ReportIllegal(keymap, action->type, field);
734 }
735
736 static bool
737 HandlePrivate(struct xkb_keymap *keymap, union xkb_action *action,
738               enum action_field field, const ExprDef *array_ndx,
739               const ExprDef *value)
740 {
741     struct xkb_private_action *act = &action->priv;
742
743     if (field == ACTION_FIELD_TYPE) {
744         int type;
745
746         if (!ExprResolveInteger(keymap->ctx, value, &type))
747             return ReportMismatch(keymap, ACTION_TYPE_PRIVATE, field, "integer");
748
749         if (type < 0 || type > 255) {
750             log_err(keymap->ctx,
751                     "Private action type must be in the range 0..255; "
752                     "Illegal type %d ignored\n", type);
753             return false;
754         }
755
756         /*
757          * It's possible for someone to write something like this:
758          *      actions = [ Private(type=3,data[0]=1,data[1]=3,data[2]=3) ]
759          * where the type refers to some existing action type, e.g. LockMods.
760          * This assumes that this action's struct is laid out in memory
761          * exactly as described in the XKB specification and libraries.
762          * We, however, have changed these structs in various ways, so this
763          * assumption is no longer true. Since this is a lousy "feature", we
764          * make actions like these no-ops for now.
765          */
766         if (type < ACTION_TYPE_PRIVATE) {
767             log_info(keymap->ctx,
768                      "Private actions of type %s are not supported; Ignored\n",
769                      ActionTypeText(type));
770             act->type = ACTION_TYPE_NONE;
771         }
772         else {
773             act->type = (enum xkb_action_type) type;
774         }
775
776         return true;
777     }
778     else if (field == ACTION_FIELD_DATA) {
779         if (array_ndx == NULL) {
780             xkb_atom_t val;
781             const char *str;
782             int len;
783
784             if (!ExprResolveString(keymap->ctx, value, &val))
785                 return ReportMismatch(keymap, action->type, field, "string");
786
787             str = xkb_atom_text(keymap->ctx, val);
788             len = strlen(str);
789             if (len < 1 || len > 7) {
790                 log_warn(keymap->ctx,
791                          "A private action has 7 data bytes; "
792                          "Extra %d bytes ignored\n", len - 6);
793                 return false;
794             }
795
796             strncpy((char *) act->data, str, sizeof(act->data));
797             return true;
798         }
799         else {
800             int ndx, datum;
801
802             if (!ExprResolveInteger(keymap->ctx, array_ndx, &ndx)) {
803                 log_err(keymap->ctx,
804                         "Array subscript must be integer; "
805                         "Illegal subscript ignored\n");
806                 return false;
807             }
808
809             if (ndx < 0 || ndx >= sizeof(act->data)) {
810                 log_err(keymap->ctx,
811                         "The data for a private action is %lu bytes long; "
812                         "Attempt to use data[%d] ignored\n",
813                         (unsigned long) sizeof(act->data), ndx);
814                 return false;
815             }
816
817             if (!ExprResolveInteger(keymap->ctx, value, &datum))
818                 return ReportMismatch(keymap, act->type, field, "integer");
819
820             if (datum < 0 || datum > 255) {
821                 log_err(keymap->ctx,
822                         "All data for a private action must be 0..255; "
823                         "Illegal datum %d ignored\n", datum);
824                 return false;
825             }
826
827             act->data[ndx] = (uint8_t) datum;
828             return true;
829         }
830     }
831
832     return ReportIllegal(keymap, ACTION_TYPE_NONE, field);
833 }
834
835 typedef bool (*actionHandler)(struct xkb_keymap *keymap,
836                               union xkb_action *action,
837                               enum action_field field,
838                               const ExprDef *array_ndx,
839                               const ExprDef *value);
840
841 static const actionHandler handleAction[_ACTION_TYPE_NUM_ENTRIES] = {
842     [ACTION_TYPE_NONE] = HandleNoAction,
843     [ACTION_TYPE_MOD_SET] = HandleSetLatchMods,
844     [ACTION_TYPE_MOD_LATCH] = HandleSetLatchMods,
845     [ACTION_TYPE_MOD_LOCK] = HandleLockMods,
846     [ACTION_TYPE_GROUP_SET] = HandleSetLatchGroup,
847     [ACTION_TYPE_GROUP_LATCH] = HandleSetLatchGroup,
848     [ACTION_TYPE_GROUP_LOCK] = HandleLockGroup,
849     [ACTION_TYPE_PTR_MOVE] = HandleMovePtr,
850     [ACTION_TYPE_PTR_BUTTON] = HandlePtrBtn,
851     [ACTION_TYPE_PTR_LOCK] = HandlePtrBtn,
852     [ACTION_TYPE_PTR_DEFAULT] = HandleSetPtrDflt,
853     [ACTION_TYPE_TERMINATE] = HandleNoAction,
854     [ACTION_TYPE_SWITCH_VT] = HandleSwitchScreen,
855     [ACTION_TYPE_CTRL_SET] = HandleSetLockControls,
856     [ACTION_TYPE_CTRL_LOCK] = HandleSetLockControls,
857     [ACTION_TYPE_PRIVATE] = HandlePrivate,
858 };
859
860 /***====================================================================***/
861
862 bool
863 HandleActionDef(ExprDef *def, struct xkb_keymap *keymap,
864                 union xkb_action *action, ActionsInfo *info)
865 {
866     ExprDef *arg;
867     const char *str;
868     unsigned handler_type;
869
870     if (def->expr.op != EXPR_ACTION_DECL) {
871         log_err(keymap->ctx, "Expected an action definition, found %s\n",
872                 expr_op_type_to_string(def->expr.op));
873         return false;
874     }
875
876     str = xkb_atom_text(keymap->ctx, def->action.name);
877     if (!stringToAction(str, &handler_type)) {
878         log_err(keymap->ctx, "Unknown action %s\n", str);
879         return false;
880     }
881
882     /*
883      * Get the default values for this action type, as modified by
884      * statements such as:
885      *     latchMods.clearLocks = True;
886      */
887     *action = info->actions[handler_type];
888
889     /*
890      * Now change the action properties as specified for this
891      * particular instance, e.g. "modifiers" and "clearLocks" in:
892      *     SetMods(modifiers=Alt,clearLocks);
893      */
894     for (arg = def->action.args; arg != NULL;
895          arg = (ExprDef *) arg->common.next) {
896         const ExprDef *value;
897         ExprDef *field, *arrayRtrn;
898         const char *elemRtrn, *fieldRtrn;
899         enum action_field fieldNdx;
900
901         if (arg->expr.op == EXPR_ASSIGN) {
902             field = arg->binary.left;
903             value = arg->binary.right;
904         }
905         else if (arg->expr.op == EXPR_NOT || arg->expr.op == EXPR_INVERT) {
906             field = arg->unary.child;
907             value = (const ExprDef *) &constFalse;
908         }
909         else {
910             field = arg;
911             value = (const ExprDef *) &constTrue;
912         }
913
914         if (!ExprResolveLhs(keymap->ctx, field, &elemRtrn, &fieldRtrn,
915                             &arrayRtrn))
916             return false;
917
918         if (elemRtrn) {
919             log_err(keymap->ctx,
920                     "Cannot change defaults in an action definition; "
921                     "Ignoring attempt to change %s.%s\n",
922                     elemRtrn, fieldRtrn);
923             return false;
924         }
925
926         if (!stringToField(fieldRtrn, &fieldNdx)) {
927             log_err(keymap->ctx, "Unknown field name %s\n", fieldRtrn);
928             return false;
929         }
930
931         if (!handleAction[handler_type](keymap, action, fieldNdx, arrayRtrn,
932                                         value))
933             return false;
934     }
935
936     return true;
937 }
938
939
940 bool
941 SetActionField(struct xkb_keymap *keymap, const char *elem, const char *field,
942                ExprDef *array_ndx, ExprDef *value, ActionsInfo *info)
943 {
944     unsigned action;
945     enum action_field action_field;
946
947     if (!stringToAction(elem, &action))
948         return false;
949
950     if (!stringToField(field, &action_field)) {
951         log_err(keymap->ctx, "\"%s\" is not a legal field name\n", field);
952         return false;
953     }
954
955     return handleAction[action](keymap, &info->actions[action],
956                                 action_field, array_ndx, value);
957 }