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