StrKeysym.c revision 1ab64890
1/* $Xorg: StrKeysym.c,v 1.5 2001/02/09 02:03:37 xorgcvs Exp $ */
2/*
3
4Copyright 1985, 1987, 1990, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26*/
27/* $XFree86: xc/lib/X11/StrKeysym.c,v 3.7 2003/04/13 19:22:18 dawes Exp $ */
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include "Xlibint.h"
33#include <X11/Xresource.h>
34#include <X11/keysymdef.h>
35#include "Xresinternal.h"
36
37#define NEEDKTABLE
38#include "ks_tables.h"
39#include "Key.h"
40
41#ifndef KEYSYMDB
42#ifndef XKEYSYMDB
43#define KEYSYMDB "/usr/lib/X11/XKeysymDB"
44#else
45#define KEYSYMDB XKEYSYMDB
46#endif
47#endif
48
49static Bool initialized;
50static XrmDatabase keysymdb;
51static XrmQuark Qkeysym[2];
52
53XrmDatabase
54_XInitKeysymDB(void)
55{
56    if (!initialized)
57    {
58	const char *dbname;
59
60	XrmInitialize();
61	/* use and name of this env var is not part of the standard */
62	/* implementation-dependent feature */
63	dbname = getenv("XKEYSYMDB");
64	if (!dbname)
65	    dbname = KEYSYMDB;
66	keysymdb = XrmGetFileDatabase(dbname);
67	if (keysymdb)
68	    Qkeysym[0] = XrmStringToQuark("Keysym");
69	initialized = True;
70    }
71    return keysymdb;
72}
73
74KeySym
75XStringToKeysym(_Xconst char *s)
76{
77    register int i, n;
78    int h;
79    register Signature sig = 0;
80    register const char *p = s;
81    register int c;
82    register int idx;
83    const unsigned char *entry;
84    unsigned char sig1, sig2;
85    KeySym val;
86
87    while ((c = *p++))
88	sig = (sig << 1) + c;
89    i = sig % KTABLESIZE;
90    h = i + 1;
91    sig1 = (sig >> 8) & 0xff;
92    sig2 = sig & 0xff;
93    n = KMAXHASH;
94    while ((idx = hashString[i]))
95    {
96	entry = &_XkeyTable[idx];
97	if ((entry[0] == sig1) && (entry[1] == sig2) &&
98	    !strcmp(s, (char *)entry + 6))
99	{
100	    val = (entry[2] << 24) | (entry[3] << 16) |
101	          (entry[4] << 8)  | entry[5];
102	    if (!val)
103		val = XK_VoidSymbol;
104	    return val;
105	}
106	if (!--n)
107	    break;
108	i += h;
109	if (i >= KTABLESIZE)
110	    i -= KTABLESIZE;
111    }
112
113    if (!initialized)
114	(void)_XInitKeysymDB();
115    if (keysymdb)
116    {
117	XrmValue result;
118	XrmRepresentation from_type;
119	char c;
120	XrmQuark names[2];
121
122	names[0] = _XrmInternalStringToQuark(s, p - s - 1, sig, False);
123	names[1] = NULLQUARK;
124	(void)XrmQGetResource(keysymdb, names, Qkeysym, &from_type, &result);
125	if (result.addr && (result.size > 1))
126	{
127	    val = 0;
128	    for (i = 0; i < result.size - 1; i++)
129	    {
130		c = ((char *)result.addr)[i];
131		if ('0' <= c && c <= '9') val = (val<<4)+c-'0';
132		else if ('a' <= c && c <= 'f') val = (val<<4)+c-'a'+10;
133		else if ('A' <= c && c <= 'F') val = (val<<4)+c-'A'+10;
134		else return NoSymbol;
135	    }
136	    return val;
137	}
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}
159