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