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