Initial implementation of keysym handlers
[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 *XkbcKeysymToString(KeySym ks)
40 {
41     register int i, n;
42     int h;
43     register int idx;
44     const unsigned char *entry;
45     unsigned char val1, val2, val3, val4;
46
47     if (!ks || (ks & ((unsigned long) ~0x1fffffff)) != 0)
48         return ((char *)NULL);
49     if (ks == XK_VoidSymbol)
50         ks = 0;
51     if (ks <= 0x1fffffff)
52     {
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         while ((idx = hashKeysym[i]))
61         {
62             entry = &_XkeyTable[idx];
63             if ((entry[0] == val1) && (entry[1] == val2) &&
64                 (entry[2] == val3) && (entry[3] == val4))
65                 return ((char *)entry + 4);
66             if (!--n)
67                 break;
68             i += h;
69             if (i >= VTABLESIZE)
70                 i -= VTABLESIZE;
71         }
72     }
73
74     if (ks >= 0x01000100 && ks <= 0x0110ffff) {
75         KeySym val = ks & 0xffffff;
76         char *s;
77         int i;
78         if (val & 0xff0000)
79             i = 10;
80         else
81             i = 6;
82         s = malloc(i);
83         if (s == NULL)
84             return s;
85         i--;
86         s[i--] = '\0';
87         for (; i; i--){
88             val1 = val & 0xf;
89             val >>= 4;
90             if (val1 < 10)
91                 s[i] = '0'+ val1;
92             else
93                 s[i] = 'A'+ val1 - 10;
94         }
95         s[i] = 'U';
96         return s;
97     }
98     return ((char *) NULL);
99 }
100
101 KeySym
102 XkbcStringToKeysym(const char *s)
103 {
104     register int i, n;
105     int h;
106     register unsigned long sig = 0;
107     register const char *p = s;
108     register int c;
109     register int idx;
110     const unsigned char *entry;
111     unsigned char sig1, sig2;
112     KeySym val;
113
114     while ((c = *p++))
115         sig = (sig << 1) + c;
116     i = sig % KTABLESIZE;
117     h = i + 1;
118     sig1 = (sig >> 8) & 0xff;
119     sig2 = sig & 0xff;
120     n = KMAXHASH;
121     while ((idx = hashString[i]))
122     {
123         entry = &_XkeyTable[idx];
124         if ((entry[0] == sig1) && (entry[1] == sig2) &&
125             !strcmp(s, (char *)entry + 6))
126         {
127             val = (entry[2] << 24) | (entry[3] << 16) |
128                   (entry[4] << 8)  | entry[5];
129             if (!val)
130                 val = XK_VoidSymbol;
131             return val;
132         }
133         if (!--n)
134             break;
135         i += h;
136         if (i >= KTABLESIZE)
137             i -= KTABLESIZE;
138     }
139
140     if (*s == 'U') {
141         val = 0;
142         for (p = &s[1]; *p; p++) {
143             c = *p;
144             if ('0' <= c && c <= '9') val = (val<<4)+c-'0';
145             else if ('a' <= c && c <= 'f') val = (val<<4)+c-'a'+10;
146             else if ('A' <= c && c <= 'F') val = (val<<4)+c-'A'+10;
147             else return NoSymbol;
148             if (val > 0x10ffff)
149                 return NoSymbol;
150         }
151         if (val < 0x20 || (val > 0x7e && val < 0xa0))
152             return NoSymbol;
153         if (val < 0x100)
154             return val;
155         return val | 0x01000000;
156     }
157     return NoSymbol;
158 }