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