KeysymStr.c revision 61b2299d
1/* $Xorg: KeysymStr.c,v 1.5 2001/02/09 02:03:34 xorgcvs Exp $ */
2
3/*
4
5Copyright 1990, 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/X11/KeysymStr.c,v 3.9 2003/04/13 19:22:16 dawes Exp $ */
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include "Xlibint.h"
34#include <X11/Xresource.h>
35#include <X11/keysymdef.h>
36
37#include <stdio.h> /* sprintf */
38
39typedef unsigned long Signature;
40
41#define NEEDVTABLE
42#include "ks_tables.h"
43#include "Key.h"
44
45
46typedef struct _GRNData {
47    char *name;
48    XrmRepresentation type;
49    XrmValuePtr value;
50} GRNData;
51
52/*ARGSUSED*/
53static Bool
54SameValue(
55    XrmDatabase*	db,
56    XrmBindingList      bindings,
57    XrmQuarkList	quarks,
58    XrmRepresentation*  type,
59    XrmValuePtr		value,
60    XPointer		data
61)
62{
63    GRNData *gd = (GRNData *)data;
64
65    if ((*type == gd->type) && (value->size == gd->value->size) &&
66	!strncmp((char *)value->addr, (char *)gd->value->addr, value->size))
67    {
68	gd->name = XrmQuarkToString(*quarks); /* XXX */
69	return True;
70    }
71    return False;
72}
73
74char *XKeysymToString(KeySym ks)
75{
76    register int i, n;
77    int h;
78    register int idx;
79    const unsigned char *entry;
80    unsigned char val1, val2, val3, val4;
81    XrmDatabase keysymdb;
82
83    if (!ks || (ks & ((unsigned long) ~0x1fffffff)) != 0)
84	return ((char *)NULL);
85    if (ks == XK_VoidSymbol)
86	ks = 0;
87    if (ks <= 0x1fffffff)
88    {
89	val1 = ks >> 24;
90	val2 = (ks >> 16) & 0xff;
91	val3 = (ks >> 8) & 0xff;
92	val4 = ks & 0xff;
93	i = ks % VTABLESIZE;
94	h = i + 1;
95	n = VMAXHASH;
96	while ((idx = hashKeysym[i]))
97	{
98	    entry = &_XkeyTable[idx];
99	    if ((entry[0] == val1) && (entry[1] == val2) &&
100                (entry[2] == val3) && (entry[3] == val4))
101		return ((char *)entry + 4);
102	    if (!--n)
103		break;
104	    i += h;
105	    if (i >= VTABLESIZE)
106		i -= VTABLESIZE;
107	}
108    }
109
110    if ((keysymdb = _XInitKeysymDB()))
111    {
112	char buf[9];
113	XrmValue resval;
114	XrmQuark empty = NULLQUARK;
115	GRNData data;
116
117	sprintf(buf, "%lX", ks);
118	resval.addr = (XPointer)buf;
119	resval.size = strlen(buf) + 1;
120	data.name = (char *)NULL;
121	data.type = XrmPermStringToQuark("String");
122	data.value = &resval;
123	(void)XrmEnumerateDatabase(keysymdb, &empty, &empty, XrmEnumAllLevels,
124				   SameValue, (XPointer)&data);
125        if (data.name)
126	    return data.name;
127    }
128    if (ks >= 0x01000100 && ks <= 0x0110ffff) {
129        KeySym val = ks & 0xffffff;
130        char *s;
131        int i;
132        if (val & 0xff0000)
133            i = 10;
134        else
135            i = 6;
136        s = Xmalloc(i);
137        if (s == NULL)
138            return s;
139        i--;
140        s[i--] = '\0';
141        for (; i; i--){
142            val1 = val & 0xf;
143            val >>= 4;
144            if (val1 < 10)
145                s[i] = '0'+ val1;
146            else
147                s[i] = 'A'+ val1 - 10;
148        }
149        s[i] = 'U';
150        return s;
151    }
152    return ((char *) NULL);
153}
154