keysym: Coding style cleanup
[platform/upstream/libxkbcommon.git] / src / keysym.c
1 /*
2 Copyright 1985, 1987, 1990, 1998  The Open Group
3 Copyright 2008  Dan Nicholson
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the names of the authors or their
23 institutions shall not be used in advertising or otherwise to promote the
24 sale, use or other dealings in this Software without prior written
25 authorization from the authors.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include <X11/X.h>
32 #include <X11/keysymdef.h>
33 #include "X11/XkbCommon.h"
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "ks_tables.h"
38
39 char *
40 XkbcKeysymToString(KeySym ks)
41 {
42     int i, n, h, idx;
43     const unsigned char *entry;
44     unsigned char val1, val2, val3, val4;
45
46     if (!ks || (ks & ((unsigned long) ~0x1fffffff)) != 0)
47         return NULL;
48
49     if (ks == XK_VoidSymbol)
50         ks = 0;
51
52     if (ks <= 0x1fffffff) {
53         val1 = ks >> 24;
54         val2 = (ks >> 16) & 0xff;
55         val3 = (ks >> 8) & 0xff;
56         val4 = ks & 0xff;
57         i = ks % VTABLESIZE;
58         h = i + 1;
59         n = VMAXHASH;
60
61         while ((idx = hashKeysym[i])) {
62             entry = &_XkeyTable[idx];
63
64             if ((entry[0] == val1) && (entry[1] == val2) &&
65                 (entry[2] == val3) && (entry[3] == val4))
66                 return ((char *)entry + 4);
67
68             if (!--n)
69                 break;
70
71             i += h;
72             if (i >= VTABLESIZE)
73                 i -= VTABLESIZE;
74         }
75     }
76
77     if (ks >= 0x01000100 && ks <= 0x0110ffff) {
78         KeySym val = ks & 0xffffff;
79         char *s;
80         int i;
81
82         if (val & 0xff0000)
83             i = 10;
84         else
85             i = 6;
86
87         s = malloc(i);
88         if (!s)
89             return s;
90
91         i--;
92         s[i--] = '\0';
93         for (; i; i--) {
94             val1 = val & 0xf;
95             val >>= 4;
96             if (val1 < 10)
97                 s[i] = '0' + val1;
98             else
99                 s[i] = 'A' + val1 - 10;
100         }
101         s[i] = 'U';
102         return s;
103     }
104
105     return NULL;
106 }
107
108 KeySym
109 XkbcStringToKeysym(const char *s)
110 {
111     int i, n, h, c, idx;
112     unsigned long sig = 0;
113     const char *p = s;
114     const unsigned char *entry;
115     unsigned char sig1, sig2;
116     KeySym val;
117
118     while ((c = *p++))
119         sig = (sig << 1) + c;
120
121     i = sig % KTABLESIZE;
122     h = i + 1;
123     sig1 = (sig >> 8) & 0xff;
124     sig2 = sig & 0xff;
125     n = KMAXHASH;
126
127     while ((idx = hashString[i])) {
128         entry = &_XkeyTable[idx];
129
130         if ((entry[0] == sig1) && (entry[1] == sig2) &&
131             !strcmp(s, (char *)entry + 6))
132         {
133             val = (entry[2] << 24) | (entry[3] << 16) |
134                   (entry[4] << 8)  | entry[5];
135             if (!val)
136                 val = XK_VoidSymbol;
137             return val;
138         }
139
140         if (!--n)
141             break;
142
143         i += h;
144         if (i >= KTABLESIZE)
145             i -= KTABLESIZE;
146     }
147
148     if (*s == 'U') {
149         val = 0;
150
151         for (p = &s[1]; *p; p++) {
152             c = *p;
153
154             if ('0' <= c && c <= '9')
155                 val = (val << 4) + c - '0';
156             else if ('a' <= c && c <= 'f')
157                 val = (val << 4) + c - 'a' + 10;
158             else if ('A' <= c && c <= 'F')
159                 val = (val << 4) + c - 'A' + 10;
160             else
161                 return NoSymbol;
162
163             if (val > 0x10ffff)
164                 return NoSymbol;
165         }
166
167         if (val < 0x20 || (val > 0x7e && val < 0xa0))
168             return NoSymbol;
169         if (val < 0x100)
170             return val;
171         return val | 0x01000000;
172     }
173
174     return NoSymbol;
175 }