Lookup.c revision 6c321187
1/* $Xorg: Lookup.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
2
3/*
4
5Copyright 1988, 1989, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27*/
28/* $XFree86: xc/lib/Xmu/Lookup.c,v 3.7 2001/01/17 19:42:56 dawes Exp $ */
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include <X11/Xmu/Lookup.h>
34
35#define XK_LATIN1
36#define XK_PUBLISHING
37#include <X11/keysymdef.h>
38
39/* bit (1<<i) means character is in codeset i */
40static unsigned short _Xconst latin1[128] =
41  {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
42   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
43   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
44   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
45   0x10ee, 0x0000, 0x1000, 0x1084, 0x102e, 0x1000, 0x1080, 0x108e, /* 10 */
46   0x108e, 0x1080, 0x0000, 0x1080, 0x1080, 0x10ee, 0x1000, 0x1008,
47   0x108e, 0x1080, 0x1084, 0x1084, 0x108e, 0x1004, 0x1000, 0x1084, /* 11 */
48   0x100e, 0x1000, 0x0000, 0x1080, 0x1000, 0x1084, 0x1000, 0x0000,
49   0x0004, 0x000e, 0x000e, 0x0008, 0x000e, 0x0008, 0x0008, 0x0006, /* 12 */
50   0x0004, 0x000e, 0x0004, 0x000e, 0x0004, 0x000e, 0x000e, 0x0004,
51   0x0000, 0x0004, 0x0004, 0x0006, 0x000e, 0x0008, 0x000e, 0x000e, /* 13 */
52   0x0008, 0x0004, 0x000e, 0x000c, 0x000e, 0x0002, 0x0000, 0x000e,
53   0x0004, 0x000e, 0x000e, 0x0008, 0x000e, 0x0008, 0x0008, 0x0006, /* 14 */
54   0x0004, 0x000e, 0x0004, 0x000e, 0x0004, 0x000e, 0x000e, 0x0004,
55   0x0000, 0x0004, 0x0004, 0x0006, 0x000e, 0x0008, 0x000e, 0x000e, /* 15 */
56   0x0008, 0x0004, 0x000e, 0x000c, 0x000e, 0x0002, 0x0000, 0x0000};
57
58/* bit (1<<i) means character is in codeset i */
59static unsigned short _Xconst latin2[128] =
60  {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
61   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
62   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
63   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
64   0x0000, 0x0008, 0x0004, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 10 */
65   0x0000, 0x0008, 0x0004, 0x0000, 0x0000, 0x0000, 0x0008, 0x0004,
66   0x0000, 0x0008, 0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0008, /* 11 */
67   0x0000, 0x0008, 0x0004, 0x0000, 0x0000, 0x0000, 0x0008, 0x0004,
68   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 12 */
69   0x0008, 0x0000, 0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
70   0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 13 */
71   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
72   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 14 */
73   0x0008, 0x0000, 0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
74   0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 15 */
75   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x000c};
76
77/* maps Cyrillic keysyms to 8859-5 */
78static unsigned char _Xconst cyrillic[128] =
79   {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83    0x00, 0xf2, 0xf3, 0xf1, 0xf4, 0xf5, 0xf6, 0xf7, /* 10 */
84    0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0x00, 0xfe, 0xff,
85    0xf0, 0xa2, 0xa3, 0xa1, 0xa4, 0xa5, 0xa6, 0xa7, /* 11 */
86    0xa8, 0xa9, 0xaa, 0xab, 0xac, 0x00, 0xae, 0xaf,
87    0xee, 0xd0, 0xd1, 0xe6, 0xd4, 0xd5, 0xe4, 0xd3, /* 12 */
88    0xe5, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
89    0xdf, 0xef, 0xe0, 0xe1, 0xe2, 0xe3, 0xd6, 0xd2, /* 13 */
90    0xec, 0xeb, 0xd7, 0xe8, 0xed, 0xe9, 0xe7, 0xea,
91    0xce, 0xb0, 0xb1, 0xc6, 0xb4, 0xb5, 0xc4, 0xb3, /* 14 */
92    0xc5, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe,
93    0xbf, 0xcf, 0xc0, 0xc1, 0xc2, 0xc3, 0xb6, 0xb2, /* 15 */
94    0xcc, 0xcb, 0xb7, 0xc8, 0xcd, 0xc9, 0xc7, 0xca};
95
96/* maps Greek keysyms to 8859-7 */
97static unsigned char _Xconst greek[128] =
98   {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102    0x00, 0xb6, 0xb8, 0xb9, 0xba, 0xda, 0x00, 0xbc, /* 10 */
103    0xbe, 0xdb, 0x00, 0xbf, 0x00, 0x00, 0xb5, 0xaf,
104    0x00, 0xdc, 0xdd, 0xde, 0xdf, 0xfa, 0xc0, 0xfc, /* 11 */
105    0xfd, 0xfb, 0xe0, 0xfe, 0x00, 0x00, 0x00, 0x00,
106    0x00, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 12 */
107    0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
108    0xd0, 0xd1, 0xd3, 0x00, 0xd4, 0xd5, 0xd6, 0xd7, /* 13 */
109    0xd8, 0xd9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110    0x00, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* 14 */
111    0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
112    0xf0, 0xf1, 0xf3, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7, /* 15 */
113    0xf8, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
114
115#define sLatin1		(unsigned long)0
116#define sLatin2		(unsigned long)1
117#define sLatin3		(unsigned long)2
118#define sLatin4		(unsigned long)3
119#define sKana		(unsigned long)4
120#define sX0201		(unsigned long)0x01000004
121#define sArabic		(unsigned long)5
122#define sCyrillic	(unsigned long)6
123#define sGreek		(unsigned long)7
124#define sAPL		(unsigned long)11
125#define sHebrew		(unsigned long)12
126
127int
128XmuLookupString(register XKeyEvent *event, unsigned char *buffer, int nbytes,
129		KeySym *keysym, XComposeStatus *status,
130		unsigned long keysymSet)
131{
132    int count;
133    KeySym symbol;
134    unsigned long kset;
135
136    kset = keysymSet & 0xffffff;
137    count = XLookupString(event, (char *)buffer, nbytes, &symbol, status);
138    if (keysym) *keysym = symbol;
139    if ((nbytes == 0) || (symbol == NoSymbol)) {
140	/* nothing */
141    } else if ((count == 0) && ((symbol >> 8) == kset)) {
142	count = 1;
143	switch (keysymSet) {
144	case sKana:
145	    buffer[0] = (symbol & 0xff);
146	    if (buffer[0] == 0x7e)
147		count = 0;
148	    break;
149	case sCyrillic:
150	    buffer[0] = cyrillic[symbol & 0x7f];
151	    break;
152	case sGreek:
153	    buffer[0] = greek[symbol & 0x7f];
154	    if (!buffer[0])
155		count = 0;
156	    break;
157	default:
158	    buffer[0] = (symbol & 0xff);
159	    break;
160	}
161    } else if ((keysymSet != 0) && (count == 1) &&
162	       (((unsigned char *)buffer)[0] == symbol) &&
163	       (symbol & 0x80) &&
164	       !(latin1[symbol & 0x7f] & (1 << kset))) {
165	if ((keysymSet == sHebrew) && (symbol == XK_multiply))
166	    buffer[0] = 0xaa;
167	else if ((keysymSet == sHebrew) && (symbol == XK_division))
168	    buffer[0] = 0xba;
169	else if ((keysymSet == sCyrillic) && (symbol == XK_section))
170	    buffer[0] = 0xfd;
171	else if ((keysymSet == sX0201) && (symbol == XK_yen))
172	    buffer[0] = 0x5c;
173	else
174	    count = 0;
175    } else if (count != 0) {
176	if ((keysymSet == sX0201) &&
177	    ((symbol == XK_backslash) || (symbol == XK_asciitilde)))
178	    count = 0;
179    } else if (((symbol >> 8) == sLatin2) &&
180	       (symbol & 0x80) && (latin2[symbol & 0x7f] & (1 << kset))) {
181	buffer[0] = (symbol & 0xff);
182	count = 1;
183    } else if ((keysymSet == sGreek) &&
184	       ((symbol == XK_leftsinglequotemark) ||
185		(symbol == XK_rightsinglequotemark))) {
186	buffer[0] = symbol - (XK_leftsinglequotemark - 0xa1);
187	count = 1;
188    }
189    return count;
190}
191
192/* produces ISO 8859-1 encoding plus ASCII control */
193int
194XmuLookupLatin1(register XKeyEvent *event, unsigned char *buffer, int nbytes,
195		KeySym *keysym, XComposeStatus *status)
196{
197    return XLookupString(event, (char *)buffer, nbytes, keysym, status);
198}
199
200/* produces ISO 8859-2 encoding plus ASCII control */
201int
202XmuLookupLatin2(register XKeyEvent *event, unsigned char *buffer, int nbytes,
203		KeySym *keysym, XComposeStatus *status)
204{
205    return XmuLookupString(event, buffer, nbytes, keysym, status, sLatin2);
206}
207
208/* produces ISO 8859-3 encoding plus ASCII control */
209int
210XmuLookupLatin3(register XKeyEvent *event, unsigned char *buffer, int nbytes,
211		KeySym *keysym, XComposeStatus *status)
212{
213    return XmuLookupString(event, buffer, nbytes, keysym, status, sLatin3);
214}
215
216/* produces ISO 8859-4 encoding plus ASCII control */
217int
218XmuLookupLatin4(register XKeyEvent *event, unsigned char *buffer, int nbytes,
219		KeySym *keysym, XComposeStatus *status)
220{
221    return XmuLookupString(event, buffer, nbytes, keysym, status, sLatin4);
222}
223
224/* produces ISO 8859-1 GL plus Katakana plus ASCII control */
225int
226XmuLookupKana(register XKeyEvent *event, unsigned char *buffer, int nbytes,
227	      KeySym *keysym, XComposeStatus *status)
228{
229    return XmuLookupString(event, buffer, nbytes, keysym, status, sKana);
230}
231
232/* produces JIS X0201-1976 (8-bit) */
233int
234XmuLookupJISX0201(register XKeyEvent *event, unsigned char *buffer, int nbytes,
235		  KeySym *keysym, XComposeStatus *status)
236{
237    return XmuLookupString(event, buffer, nbytes, keysym, status, sX0201);
238}
239
240/* produces ISO 8859-6 encoding plus ASCII control */
241int
242XmuLookupArabic(register XKeyEvent *event, unsigned char *buffer, int nbytes,
243		KeySym *keysym, XComposeStatus *status)
244{
245    return XmuLookupString(event, buffer, nbytes, keysym, status, sArabic);
246}
247
248/* produces ISO/IEC 8859-5 encoding plus ASCII control */
249int
250XmuLookupCyrillic(register XKeyEvent *event, unsigned char *buffer, int nbytes,
251		  KeySym *keysym, XComposeStatus *status)
252{
253    return XmuLookupString(event, buffer, nbytes, keysym, status, sCyrillic);
254}
255
256/* produces ISO 8859-7 encoding plus ASCII control */
257int
258XmuLookupGreek(register XKeyEvent *event, unsigned char *buffer, int nbytes,
259	       KeySym *keysym, XComposeStatus *status)
260{
261    return XmuLookupString(event, buffer, nbytes, keysym, status, sGreek);
262}
263
264/* XXX this character set needs work */
265
266int
267XmuLookupAPL(register XKeyEvent *event, unsigned char *buffer, int nbytes,
268	     KeySym *keysym, XComposeStatus *status)
269{
270    return XmuLookupString(event, buffer, nbytes, keysym, status, sAPL);
271}
272
273/* produces ISO 8859-8 encoding plus ASCII control */
274int
275XmuLookupHebrew(register XKeyEvent *event, unsigned char *buffer, int nbytes,
276		KeySym *keysym, XComposeStatus *status)
277{
278    return XmuLookupString(event, buffer, nbytes, keysym, status, sHebrew);
279}
280