xref: /aosp_15_r20/external/libxkbcommon/src/xkbcomp/vmod.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /************************************************************
2*2b949d04SAndroid Build Coastguard Worker  * Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3*2b949d04SAndroid Build Coastguard Worker  *
4*2b949d04SAndroid Build Coastguard Worker  * Permission to use, copy, modify, and distribute this
5*2b949d04SAndroid Build Coastguard Worker  * software and its documentation for any purpose and without
6*2b949d04SAndroid Build Coastguard Worker  * fee is hereby granted, provided that the above copyright
7*2b949d04SAndroid Build Coastguard Worker  * notice appear in all copies and that both that copyright
8*2b949d04SAndroid Build Coastguard Worker  * notice and this permission notice appear in supporting
9*2b949d04SAndroid Build Coastguard Worker  * documentation, and that the name of Silicon Graphics not be
10*2b949d04SAndroid Build Coastguard Worker  * used in advertising or publicity pertaining to distribution
11*2b949d04SAndroid Build Coastguard Worker  * of the software without specific prior written permission.
12*2b949d04SAndroid Build Coastguard Worker  * Silicon Graphics makes no representation about the suitability
13*2b949d04SAndroid Build Coastguard Worker  * of this software for any purpose. It is provided "as is"
14*2b949d04SAndroid Build Coastguard Worker  * without any express or implied warranty.
15*2b949d04SAndroid Build Coastguard Worker  *
16*2b949d04SAndroid Build Coastguard Worker  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17*2b949d04SAndroid Build Coastguard Worker  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18*2b949d04SAndroid Build Coastguard Worker  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19*2b949d04SAndroid Build Coastguard Worker  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20*2b949d04SAndroid Build Coastguard Worker  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21*2b949d04SAndroid Build Coastguard Worker  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22*2b949d04SAndroid Build Coastguard Worker  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23*2b949d04SAndroid Build Coastguard Worker  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
24*2b949d04SAndroid Build Coastguard Worker  *
25*2b949d04SAndroid Build Coastguard Worker  ********************************************************/
26*2b949d04SAndroid Build Coastguard Worker 
27*2b949d04SAndroid Build Coastguard Worker #include "config.h"
28*2b949d04SAndroid Build Coastguard Worker 
29*2b949d04SAndroid Build Coastguard Worker #include "xkbcomp-priv.h"
30*2b949d04SAndroid Build Coastguard Worker #include "text.h"
31*2b949d04SAndroid Build Coastguard Worker #include "expr.h"
32*2b949d04SAndroid Build Coastguard Worker #include "vmod.h"
33*2b949d04SAndroid Build Coastguard Worker 
34*2b949d04SAndroid Build Coastguard Worker bool
HandleVModDef(struct xkb_context * ctx,struct xkb_mod_set * mods,VModDef * stmt,enum merge_mode merge)35*2b949d04SAndroid Build Coastguard Worker HandleVModDef(struct xkb_context *ctx, struct xkb_mod_set *mods,
36*2b949d04SAndroid Build Coastguard Worker               VModDef *stmt, enum merge_mode merge)
37*2b949d04SAndroid Build Coastguard Worker {
38*2b949d04SAndroid Build Coastguard Worker     xkb_mod_index_t i;
39*2b949d04SAndroid Build Coastguard Worker     struct xkb_mod *mod;
40*2b949d04SAndroid Build Coastguard Worker     xkb_mod_mask_t mapping;
41*2b949d04SAndroid Build Coastguard Worker 
42*2b949d04SAndroid Build Coastguard Worker     merge = (merge == MERGE_DEFAULT ? stmt->merge : merge);
43*2b949d04SAndroid Build Coastguard Worker 
44*2b949d04SAndroid Build Coastguard Worker     if (stmt->value) {
45*2b949d04SAndroid Build Coastguard Worker         /*
46*2b949d04SAndroid Build Coastguard Worker          * This is a statement such as 'virtualModifiers NumLock = Mod1';
47*2b949d04SAndroid Build Coastguard Worker          * it sets the vmod-to-real-mod[s] mapping directly instead of going
48*2b949d04SAndroid Build Coastguard Worker          * through modifier_map or some such.
49*2b949d04SAndroid Build Coastguard Worker          */
50*2b949d04SAndroid Build Coastguard Worker         if (!ExprResolveModMask(ctx, stmt->value, MOD_REAL, mods, &mapping)) {
51*2b949d04SAndroid Build Coastguard Worker             log_err(ctx,
52*2b949d04SAndroid Build Coastguard Worker                     "Declaration of %s ignored\n",
53*2b949d04SAndroid Build Coastguard Worker                     xkb_atom_text(ctx, stmt->name));
54*2b949d04SAndroid Build Coastguard Worker             return false;
55*2b949d04SAndroid Build Coastguard Worker         }
56*2b949d04SAndroid Build Coastguard Worker     }
57*2b949d04SAndroid Build Coastguard Worker     else {
58*2b949d04SAndroid Build Coastguard Worker         mapping = 0;
59*2b949d04SAndroid Build Coastguard Worker     }
60*2b949d04SAndroid Build Coastguard Worker 
61*2b949d04SAndroid Build Coastguard Worker     xkb_mods_enumerate(i, mod, mods) {
62*2b949d04SAndroid Build Coastguard Worker         if (mod->name == stmt->name) {
63*2b949d04SAndroid Build Coastguard Worker             if (mod->type != MOD_VIRT) {
64*2b949d04SAndroid Build Coastguard Worker                 log_err(ctx,
65*2b949d04SAndroid Build Coastguard Worker                         "Can't add a virtual modifier named \"%s\"; "
66*2b949d04SAndroid Build Coastguard Worker                         "there is already a non-virtual modifier with this name! Ignored\n",
67*2b949d04SAndroid Build Coastguard Worker                         xkb_atom_text(ctx, mod->name));
68*2b949d04SAndroid Build Coastguard Worker                 return false;
69*2b949d04SAndroid Build Coastguard Worker             }
70*2b949d04SAndroid Build Coastguard Worker 
71*2b949d04SAndroid Build Coastguard Worker             if (mod->mapping == mapping)
72*2b949d04SAndroid Build Coastguard Worker                 return true;
73*2b949d04SAndroid Build Coastguard Worker 
74*2b949d04SAndroid Build Coastguard Worker             if (mod->mapping != 0) {
75*2b949d04SAndroid Build Coastguard Worker                 xkb_mod_mask_t use, ignore;
76*2b949d04SAndroid Build Coastguard Worker 
77*2b949d04SAndroid Build Coastguard Worker                 use = (merge == MERGE_OVERRIDE ? mapping : mod->mapping);
78*2b949d04SAndroid Build Coastguard Worker                 ignore = (merge == MERGE_OVERRIDE ? mod->mapping : mapping);
79*2b949d04SAndroid Build Coastguard Worker 
80*2b949d04SAndroid Build Coastguard Worker                 log_warn(ctx,
81*2b949d04SAndroid Build Coastguard Worker                          "Virtual modifier %s defined multiple times; "
82*2b949d04SAndroid Build Coastguard Worker                          "Using %s, ignoring %s\n",
83*2b949d04SAndroid Build Coastguard Worker                          xkb_atom_text(ctx, stmt->name),
84*2b949d04SAndroid Build Coastguard Worker                          ModMaskText(ctx, mods, use),
85*2b949d04SAndroid Build Coastguard Worker                          ModMaskText(ctx, mods, ignore));
86*2b949d04SAndroid Build Coastguard Worker 
87*2b949d04SAndroid Build Coastguard Worker                 mapping = use;
88*2b949d04SAndroid Build Coastguard Worker             }
89*2b949d04SAndroid Build Coastguard Worker 
90*2b949d04SAndroid Build Coastguard Worker             mod->mapping = mapping;
91*2b949d04SAndroid Build Coastguard Worker             return true;
92*2b949d04SAndroid Build Coastguard Worker         }
93*2b949d04SAndroid Build Coastguard Worker     }
94*2b949d04SAndroid Build Coastguard Worker 
95*2b949d04SAndroid Build Coastguard Worker     if (mods->num_mods >= XKB_MAX_MODS) {
96*2b949d04SAndroid Build Coastguard Worker         log_err(ctx,
97*2b949d04SAndroid Build Coastguard Worker                 "Too many modifiers defined (maximum %d)\n",
98*2b949d04SAndroid Build Coastguard Worker                 XKB_MAX_MODS);
99*2b949d04SAndroid Build Coastguard Worker         return false;
100*2b949d04SAndroid Build Coastguard Worker     }
101*2b949d04SAndroid Build Coastguard Worker 
102*2b949d04SAndroid Build Coastguard Worker     mods->mods[mods->num_mods].name = stmt->name;
103*2b949d04SAndroid Build Coastguard Worker     mods->mods[mods->num_mods].type = MOD_VIRT;
104*2b949d04SAndroid Build Coastguard Worker     mods->mods[mods->num_mods].mapping = mapping;
105*2b949d04SAndroid Build Coastguard Worker     mods->num_mods++;
106*2b949d04SAndroid Build Coastguard Worker     return true;
107*2b949d04SAndroid Build Coastguard Worker }
108