Copy XkbEnsureSafeMapName from xkbfile
[profile/ivi/libxkbcommon.git] / src / misc.c
1 /************************************************************
2 Copyright (c) 1993 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 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include "X11/extensions/XKBcommon.h"
32 #include "XKBcommonint.h"
33
34 #define mapSize(m) (sizeof(m) / sizeof(XkbKTMapEntryRec))
35 static XkbKTMapEntryRec map2Level[]= {
36     { True, ShiftMask, {1, ShiftMask, 0} }
37 };
38
39 static XkbKTMapEntryRec mapAlpha[]= {
40     { True, ShiftMask, { 1, ShiftMask, 0 } },
41     { True, LockMask,  { 0, LockMask,  0 } }
42 };
43
44 static XkbModsRec preAlpha[]= {
45     { 0,        0,        0 },
46     { LockMask, LockMask, 0 }
47 };
48
49 #define NL_VMOD_MASK 0
50 static  XkbKTMapEntryRec mapKeypad[]= {
51     { True,  ShiftMask, { 1, ShiftMask, 0 } },
52     { False, 0,         { 1, 0, NL_VMOD_MASK } }
53 };
54
55 static XkbKeyTypeRec canonicalTypes[XkbNumRequiredTypes] = {
56     { { 0, 0, 0 },
57       1,        /* num_levels */
58       0,        /* map_count */
59       NULL, NULL,
60       None, NULL
61     },
62     { { ShiftMask, ShiftMask, 0 },
63       2,        /* num_levels */
64       mapSize(map2Level),   /* map_count */
65       map2Level, NULL,
66       None,      NULL
67     },
68     { { ShiftMask|LockMask, ShiftMask|LockMask, 0 },
69       2,        /* num_levels */
70       mapSize(mapAlpha),    /* map_count */
71       mapAlpha, preAlpha,
72       None,     NULL
73     },
74     { { ShiftMask, ShiftMask, NL_VMOD_MASK },
75       2,        /* num_levels */
76       mapSize(mapKeypad),   /* map_count */
77       mapKeypad, NULL,
78       None,      NULL
79     }
80 };
81
82 int
83 XkbcInitCanonicalKeyTypes(XkbcDescPtr xkb, unsigned which, int keypadVMod)
84 {
85     XkbClientMapPtr map;
86     XkbKeyTypePtr from,to;
87     int rtrn;
88
89     if (!xkb)
90         return BadMatch;
91
92     rtrn= XkbcAllocClientMap(xkb, XkbKeyTypesMask, XkbNumRequiredTypes);
93     if (rtrn != Success)
94         return rtrn;
95
96     map= xkb->map;
97     if ((which & XkbAllRequiredTypes) == 0)
98         return Success;
99
100     rtrn = Success;
101     from = canonicalTypes;
102     to = map->types;
103
104     if (which & XkbOneLevelMask)
105         rtrn = XkbcCopyKeyType(&from[XkbOneLevelIndex], &to[XkbOneLevelIndex]);
106
107     if ((which & XkbTwoLevelMask) && (rtrn == Success))
108         rtrn = XkbcCopyKeyType(&from[XkbTwoLevelIndex], &to[XkbTwoLevelIndex]);
109
110     if ((which & XkbAlphabeticMask) && (rtrn == Success))
111         rtrn = XkbcCopyKeyType(&from[XkbAlphabeticIndex],
112                                &to[XkbAlphabeticIndex]);
113
114     if ((which & XkbKeypadMask) && (rtrn == Success)) {
115         XkbKeyTypePtr type;
116
117         rtrn = XkbcCopyKeyType(&from[XkbKeypadIndex], &to[XkbKeypadIndex]);
118         type = &to[XkbKeypadIndex];
119
120         if ((keypadVMod >= 0) && (keypadVMod < XkbNumVirtualMods) &&
121             (rtrn == Success)) {
122             type->mods.vmods = (1 << keypadVMod);
123             type->map[0].active = True;
124             type->map[0].mods.mask = ShiftMask;
125             type->map[0].mods.real_mods = ShiftMask;
126             type->map[0].mods.vmods = 0;
127             type->map[0].level = 1;
128             type->map[1].active = False;
129             type->map[1].mods.mask = 0;
130             type->map[1].mods.real_mods = 0;
131             type->map[1].mods.vmods = (1 << keypadVMod);
132             type->map[1].level = 1;
133         }
134     }
135
136     return Success;
137 }
138
139 Bool
140 XkbcVirtualModsToReal(XkbcDescPtr xkb, unsigned virtual_mask,
141                       unsigned *mask_rtrn)
142 {
143     int i, bit;
144     unsigned mask;
145
146     if (!xkb)
147         return False;
148     if (virtual_mask == 0) {
149         *mask_rtrn = 0;
150         return True;
151     }
152     if (!xkb->server)
153         return False;
154
155     for (i = mask = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
156         if (virtual_mask & bit)
157             mask |= xkb->server->vmods[i];
158     }
159
160     *mask_rtrn = mask;
161     return True;
162 }
163
164 /*
165  * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
166  * wildcards.
167  */
168 static unsigned char componentSpecLegal[] = {
169     0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83,
170     0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
171     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
172     0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff
173 };
174
175 void
176 XkbcEnsureSafeMapName(char *name)
177 {
178     if (!name)
179         return;
180
181     while (*name!='\0') {
182         if ((componentSpecLegal[(*name) / 8] & (1 << ((*name) % 8))) == 0)
183             *name= '_';
184         name++;
185     }
186 }