Use our own keysyms
[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 #include "xkballoc.h"
28 #include "xkbmisc.h"
29 #include "xkbcommon/xkbcommon.h"
30 #include "XKBcommonint.h"
31
32 #define mapSize(m) (sizeof(m) / sizeof(struct xkb_kt_map_entry))
33 static struct xkb_kt_map_entry map2Level[]= {
34     { true, ShiftMask, {1, ShiftMask, 0} }
35 };
36
37 static struct xkb_kt_map_entry mapAlpha[]= {
38     { true, ShiftMask, { 1, ShiftMask, 0 } },
39     { true, LockMask,  { 0, LockMask,  0 } }
40 };
41
42 static struct xkb_mods preAlpha[]= {
43     { 0,        0,        0 },
44     { LockMask, LockMask, 0 }
45 };
46
47 #define NL_VMOD_MASK 0
48 static  struct xkb_kt_map_entry mapKeypad[]= {
49     { true,  ShiftMask, { 1, ShiftMask, 0 } },
50     { false, 0,         { 1, 0, NL_VMOD_MASK } }
51 };
52
53 static struct xkb_key_type canonicalTypes[XkbNumRequiredTypes] = {
54     { { 0, 0, 0 },
55       1,        /* num_levels */
56       0,        /* map_count */
57       NULL, NULL,
58       NULL, NULL
59     },
60     { { ShiftMask, ShiftMask, 0 },
61       2,        /* num_levels */
62       mapSize(map2Level),   /* map_count */
63       map2Level, NULL,
64       NULL,      NULL
65     },
66     { { ShiftMask|LockMask, ShiftMask|LockMask, 0 },
67       2,        /* num_levels */
68       mapSize(mapAlpha),    /* map_count */
69       mapAlpha, preAlpha,
70       NULL,     NULL
71     },
72     { { ShiftMask, ShiftMask, NL_VMOD_MASK },
73       2,        /* num_levels */
74       mapSize(mapKeypad),   /* map_count */
75       mapKeypad, NULL,
76       NULL,      NULL
77     }
78 };
79
80 int
81 XkbcInitCanonicalKeyTypes(struct xkb_keymap * xkb, unsigned which, int keypadVMod)
82 {
83     struct xkb_client_map * map;
84     struct xkb_key_type *from, *to;
85     int rtrn;
86
87     if (!xkb)
88         return BadMatch;
89
90     rtrn= XkbcAllocClientMap(xkb, XkbKeyTypesMask, XkbNumRequiredTypes);
91     if (rtrn != Success)
92         return rtrn;
93
94     map= xkb->map;
95     if ((which & XkbAllRequiredTypes) == 0)
96         return Success;
97
98     rtrn = Success;
99     from = canonicalTypes;
100     to = map->types;
101
102     if (which & XkbOneLevelMask)
103         rtrn = XkbcCopyKeyType(&from[XkbOneLevelIndex], &to[XkbOneLevelIndex]);
104
105     if ((which & XkbTwoLevelMask) && (rtrn == Success))
106         rtrn = XkbcCopyKeyType(&from[XkbTwoLevelIndex], &to[XkbTwoLevelIndex]);
107
108     if ((which & XkbAlphabeticMask) && (rtrn == Success))
109         rtrn = XkbcCopyKeyType(&from[XkbAlphabeticIndex],
110                                &to[XkbAlphabeticIndex]);
111
112     if ((which & XkbKeypadMask) && (rtrn == Success)) {
113         struct xkb_key_type * type;
114
115         rtrn = XkbcCopyKeyType(&from[XkbKeypadIndex], &to[XkbKeypadIndex]);
116         type = &to[XkbKeypadIndex];
117
118         if ((keypadVMod >= 0) && (keypadVMod < XkbNumVirtualMods) &&
119             (rtrn == Success)) {
120             type->mods.vmods = (1 << keypadVMod);
121             type->map[0].active = true;
122             type->map[0].mods.mask = ShiftMask;
123             type->map[0].mods.real_mods = ShiftMask;
124             type->map[0].mods.vmods = 0;
125             type->map[0].level = 1;
126             type->map[1].active = false;
127             type->map[1].mods.mask = 0;
128             type->map[1].mods.real_mods = 0;
129             type->map[1].mods.vmods = (1 << keypadVMod);
130             type->map[1].level = 1;
131         }
132     }
133
134     return Success;
135 }
136
137 bool
138 XkbcVirtualModsToReal(struct xkb_keymap * xkb, unsigned virtual_mask,
139                       unsigned *mask_rtrn)
140 {
141     int i, bit;
142     unsigned mask;
143
144     if (!xkb)
145         return false;
146     if (virtual_mask == 0) {
147         *mask_rtrn = 0;
148         return true;
149     }
150     if (!xkb->server)
151         return false;
152
153     for (i = mask = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
154         if (virtual_mask & bit)
155             mask |= xkb->server->vmods[i];
156     }
157
158     *mask_rtrn = mask;
159     return true;
160 }
161
162 /*
163  * All latin-1 alphanumerics, plus parens, slash, minus, underscore and
164  * wildcards.
165  */
166 static const unsigned char componentSpecLegal[] = {
167     0x00, 0x00, 0x00, 0x00, 0x00, 0xa7, 0xff, 0x83,
168     0xfe, 0xff, 0xff, 0x87, 0xfe, 0xff, 0xff, 0x07,
169     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
170     0xff, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xff
171 };
172
173 void
174 XkbcEnsureSafeMapName(char *name)
175 {
176     if (!name)
177         return;
178
179     while (*name!='\0') {
180         if ((componentSpecLegal[(*name) / 8] & (1 << ((*name) % 8))) == 0)
181             *name= '_';
182         name++;
183     }
184 }
185
186 unsigned
187 _XkbcKSCheckCase(xkb_keysym_t ks)
188 {
189     unsigned set = (ks & (~0xff)) >> 8;
190     unsigned rtrn = 0;
191
192     switch (set) {
193     case 0: /* latin 1 */
194         if ((ks >= XKB_KEY_A && ks <= XKB_KEY_Z) ||
195             (ks >= XKB_KEY_Agrave && ks <= XKB_KEY_THORN && ks != XKB_KEY_multiply))
196             rtrn |= _XkbKSUpper;
197         if ((ks >= XKB_KEY_a && ks <= XKB_KEY_z) ||
198             (ks >= XKB_KEY_agrave && ks <= XKB_KEY_ydiaeresis))
199             rtrn |= _XkbKSLower;
200         break;
201     case 1: /* latin 2 */
202         if ((ks >= XKB_KEY_Aogonek && ks <= XKB_KEY_Zabovedot && ks != XKB_KEY_breve) ||
203             (ks >= XKB_KEY_Racute && ks<=XKB_KEY_Tcedilla))
204             rtrn |= _XkbKSUpper;
205         if ((ks >= XKB_KEY_aogonek && ks <= XKB_KEY_zabovedot && ks != XKB_KEY_caron) ||
206             (ks >= XKB_KEY_racute && ks <= XKB_KEY_tcedilla))
207             rtrn |= _XkbKSLower;
208         break;
209     case 2: /* latin 3 */
210         if ((ks >= XKB_KEY_Hstroke && ks <= XKB_KEY_Jcircumflex) ||
211             (ks >= XKB_KEY_Cabovedot && ks <= XKB_KEY_Scircumflex))
212             rtrn |= _XkbKSUpper;
213         if ((ks >= XKB_KEY_hstroke && ks <= XKB_KEY_jcircumflex) ||
214             (ks >= XKB_KEY_cabovedot && ks <= XKB_KEY_scircumflex))
215             rtrn |= _XkbKSLower;
216         break;
217     case 3: /* latin 4 */
218         if ((ks >= XKB_KEY_Rcedilla && ks <= XKB_KEY_Tslash) ||
219             (ks == XKB_KEY_ENG) ||
220             (ks >= XKB_KEY_Amacron && ks <= XKB_KEY_Umacron))
221             rtrn |= _XkbKSUpper;
222         if ((ks >= XKB_KEY_rcedilla && ks <= XKB_KEY_tslash) ||
223             (ks == XKB_KEY_eng) ||
224             (ks >= XKB_KEY_amacron && ks <= XKB_KEY_umacron))
225             rtrn |= _XkbKSLower;
226         break;
227     case 18: /* latin 8 */
228         if ((ks == XKB_KEY_Wcircumflex) ||
229             (ks == XKB_KEY_Ycircumflex) ||
230             (ks == XKB_KEY_Babovedot) ||
231             (ks == XKB_KEY_Dabovedot) ||
232             (ks == XKB_KEY_Fabovedot) ||
233             (ks == XKB_KEY_Mabovedot) ||
234             (ks == XKB_KEY_Pabovedot) ||
235             (ks == XKB_KEY_Sabovedot) ||
236             (ks == XKB_KEY_Tabovedot) ||
237             (ks == XKB_KEY_Wdiaeresis) ||
238             (ks == XKB_KEY_Ygrave))
239             rtrn |= _XkbKSUpper;
240         if ((ks == XKB_KEY_wcircumflex) ||
241             (ks == XKB_KEY_ycircumflex) ||
242             (ks == XKB_KEY_babovedot) ||
243             (ks == XKB_KEY_dabovedot) ||
244             (ks == XKB_KEY_fabovedot) ||
245             (ks == XKB_KEY_mabovedot) ||
246             (ks == XKB_KEY_pabovedot) ||
247             (ks == XKB_KEY_sabovedot) ||
248             (ks == XKB_KEY_tabovedot) ||
249             (ks == XKB_KEY_wdiaeresis) ||
250             (ks == XKB_KEY_ygrave))
251             rtrn |= _XkbKSLower;
252         break;
253     case 19: /* latin 9 */
254         if (ks == XKB_KEY_OE || ks == XKB_KEY_Ydiaeresis)
255             rtrn |= _XkbKSUpper;
256         if (ks == XKB_KEY_oe)
257             rtrn |= _XkbKSLower;
258         break;
259     }
260
261     return rtrn;
262 }