Drop keysym.h pollution from XKBcommon.h
[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 #include <X11/keysym.h>
34
35 #define mapSize(m) (sizeof(m) / sizeof(XkbKTMapEntryRec))
36 static XkbKTMapEntryRec map2Level[]= {
37     { True, ShiftMask, {1, ShiftMask, 0} }
38 };
39
40 static XkbKTMapEntryRec mapAlpha[]= {
41     { True, ShiftMask, { 1, ShiftMask, 0 } },
42     { True, LockMask,  { 0, LockMask,  0 } }
43 };
44
45 static XkbModsRec preAlpha[]= {
46     { 0,        0,        0 },
47     { LockMask, LockMask, 0 }
48 };
49
50 #define NL_VMOD_MASK 0
51 static  XkbKTMapEntryRec mapKeypad[]= {
52     { True,  ShiftMask, { 1, ShiftMask, 0 } },
53     { False, 0,         { 1, 0, NL_VMOD_MASK } }
54 };
55
56 static XkbKeyTypeRec canonicalTypes[XkbNumRequiredTypes] = {
57     { { 0, 0, 0 },
58       1,        /* num_levels */
59       0,        /* map_count */
60       NULL, NULL,
61       None, NULL
62     },
63     { { ShiftMask, ShiftMask, 0 },
64       2,        /* num_levels */
65       mapSize(map2Level),   /* map_count */
66       map2Level, NULL,
67       None,      NULL
68     },
69     { { ShiftMask|LockMask, ShiftMask|LockMask, 0 },
70       2,        /* num_levels */
71       mapSize(mapAlpha),    /* map_count */
72       mapAlpha, preAlpha,
73       None,     NULL
74     },
75     { { ShiftMask, ShiftMask, NL_VMOD_MASK },
76       2,        /* num_levels */
77       mapSize(mapKeypad),   /* map_count */
78       mapKeypad, NULL,
79       None,      NULL
80     }
81 };
82
83 int
84 XkbcInitCanonicalKeyTypes(XkbcDescPtr xkb, unsigned which, int keypadVMod)
85 {
86     XkbClientMapPtr map;
87     XkbKeyTypePtr from,to;
88     int rtrn;
89
90     if (!xkb)
91         return BadMatch;
92
93     rtrn= XkbcAllocClientMap(xkb, XkbKeyTypesMask, XkbNumRequiredTypes);
94     if (rtrn != Success)
95         return rtrn;
96
97     map= xkb->map;
98     if ((which & XkbAllRequiredTypes) == 0)
99         return Success;
100
101     rtrn = Success;
102     from = canonicalTypes;
103     to = map->types;
104
105     if (which & XkbOneLevelMask)
106         rtrn = XkbcCopyKeyType(&from[XkbOneLevelIndex], &to[XkbOneLevelIndex]);
107
108     if ((which & XkbTwoLevelMask) && (rtrn == Success))
109         rtrn = XkbcCopyKeyType(&from[XkbTwoLevelIndex], &to[XkbTwoLevelIndex]);
110
111     if ((which & XkbAlphabeticMask) && (rtrn == Success))
112         rtrn = XkbcCopyKeyType(&from[XkbAlphabeticIndex],
113                                &to[XkbAlphabeticIndex]);
114
115     if ((which & XkbKeypadMask) && (rtrn == Success)) {
116         XkbKeyTypePtr type;
117
118         rtrn = XkbcCopyKeyType(&from[XkbKeypadIndex], &to[XkbKeypadIndex]);
119         type = &to[XkbKeypadIndex];
120
121         if ((keypadVMod >= 0) && (keypadVMod < XkbNumVirtualMods) &&
122             (rtrn == Success)) {
123             type->mods.vmods = (1 << keypadVMod);
124             type->map[0].active = True;
125             type->map[0].mods.mask = ShiftMask;
126             type->map[0].mods.real_mods = ShiftMask;
127             type->map[0].mods.vmods = 0;
128             type->map[0].level = 1;
129             type->map[1].active = False;
130             type->map[1].mods.mask = 0;
131             type->map[1].mods.real_mods = 0;
132             type->map[1].mods.vmods = (1 << keypadVMod);
133             type->map[1].level = 1;
134         }
135     }
136
137     return Success;
138 }
139
140 Bool
141 XkbcVirtualModsToReal(XkbcDescPtr xkb, unsigned virtual_mask,
142                       unsigned *mask_rtrn)
143 {
144     int i, bit;
145     unsigned mask;
146
147     if (!xkb)
148         return False;
149     if (virtual_mask == 0) {
150         *mask_rtrn = 0;
151         return True;
152     }
153     if (!xkb->server)
154         return False;
155
156     for (i = mask = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
157         if (virtual_mask & bit)
158             mask |= xkb->server->vmods[i];
159     }
160
161     *mask_rtrn = mask;
162     return True;
163 }
164
165 /*
166  * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
167  * wildcards.
168  */
169 static unsigned char componentSpecLegal[] = {
170     0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83,
171     0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
172     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
173     0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff
174 };
175
176 void
177 XkbcEnsureSafeMapName(char *name)
178 {
179     if (!name)
180         return;
181
182     while (*name!='\0') {
183         if ((componentSpecLegal[(*name) / 8] & (1 << ((*name) % 8))) == 0)
184             *name= '_';
185         name++;
186     }
187 }
188
189 unsigned
190 _XkbcKSCheckCase(KeySym ks)
191 {
192 unsigned        set,rtrn;
193
194     set= (ks & (~0xff)) >> 8;
195     rtrn= 0;
196     switch (set) {
197         case 0:         /* latin 1 */
198             if (((ks>=XK_A)&&(ks<=XK_Z))||
199                 ((ks>=XK_Agrave)&&(ks<=XK_THORN)&&(ks!=XK_multiply))) {
200                 rtrn|= _XkbcKSUpper;
201             }
202             if (((ks>=XK_a)&&(ks<=XK_z))||
203                 ((ks>=XK_agrave)&&(ks<=XK_ydiaeresis))) {
204                 rtrn|= _XkbcKSLower;
205             }
206             break;
207         case 1:         /* latin 2 */
208             if (((ks>=XK_Aogonek)&&(ks<=XK_Zabovedot)&&(ks!=XK_breve))||
209                 ((ks>=XK_Racute)&&(ks<=XK_Tcedilla))) {
210                 rtrn|= _XkbcKSUpper;
211             }
212             if (((ks>=XK_aogonek)&&(ks<=XK_zabovedot)&&(ks!=XK_caron))||
213                 ((ks>=XK_racute)&&(ks<=XK_tcedilla))) {
214                 rtrn|= _XkbcKSLower;
215             }
216             break;
217         case 2:         /* latin 3 */
218             if (((ks>=XK_Hstroke)&&(ks<=XK_Jcircumflex))||
219                 ((ks>=XK_Cabovedot)&&(ks<=XK_Scircumflex))) {
220                 rtrn|= _XkbcKSUpper;
221             }
222             if (((ks>=XK_hstroke)&&(ks<=XK_jcircumflex))||
223                 ((ks>=XK_cabovedot)&&(ks<=XK_scircumflex))) {
224                 rtrn|= _XkbcKSLower;
225             }
226             break;
227         case 3:         /* latin 4 */
228             if (((ks>=XK_Rcedilla)&&(ks<=XK_Tslash))||
229                 (ks==XK_ENG)||
230                 ((ks>=XK_Amacron)&&(ks<=XK_Umacron))) {
231                 rtrn|= _XkbcKSUpper;
232             }
233             if (((ks>=XK_rcedilla)&&(ks<=XK_tslash))||
234                 (ks==XK_eng)||
235                 ((ks>=XK_amacron)&&(ks<=XK_umacron))) {
236                 rtrn|= _XkbcKSLower;
237             }
238             break;
239         case 18:                /* latin 8 */
240             if ((ks==XK_Babovedot)||
241                 ((ks>=XK_Dabovedot)&&(ks<=XK_Wacute))||
242                 ((ks>=XK_Ygrave)&&(ks<=XK_Fabovedot))||
243                 (ks==XK_Mabovedot)||
244                 (ks==XK_Pabovedot)||
245                 (ks==XK_Sabovedot)||
246                 (ks==XK_Wdiaeresis)||
247                 ((ks>=XK_Wcircumflex)&&(ks<=XK_Ycircumflex))) {
248                 rtrn|= _XkbcKSUpper;
249             }
250             if ((ks==XK_babovedot)||
251                 (ks==XK_dabovedot)||
252                 (ks==XK_fabovedot)||
253                 (ks==XK_mabovedot)||
254                 ((ks>=XK_wgrave)&&(ks<=XK_wacute))||
255                 (ks==XK_ygrave)||
256                 ((ks>=XK_wdiaeresis)&&(ks<=XK_ycircumflex))) {
257                 rtrn|= _XkbcKSLower;
258             }
259             break;
260         case 19:                /* latin 9 */
261             if ((ks==XK_OE)||(ks==XK_Ydiaeresis)) {
262                 rtrn|= _XkbcKSUpper;
263             }
264             if (ks==XK_oe) {
265                 rtrn|= _XkbcKSLower;
266             }
267             break;
268     }
269     return rtrn;
270 }