11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1985, 1987, 1990, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included in
121ab64890Smrgall copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
171ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201ab64890Smrg
211ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
221ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
231ab64890Smrgin this Software without prior written authorization from The Open Group.
241ab64890Smrg
251ab64890Smrg*/
261ab64890Smrg
271ab64890Smrg#ifdef HAVE_CONFIG_H
281ab64890Smrg#include <config.h>
291ab64890Smrg#endif
3047f86ac1Smrg#include <limits.h>
311ab64890Smrg#include "Xlibint.h"
321ab64890Smrg#include <X11/Xresource.h>
331ab64890Smrg#include <X11/keysymdef.h>
341ab64890Smrg#include "Xresinternal.h"
351ab64890Smrg
361ab64890Smrg#define NEEDKTABLE
371ab64890Smrg#include "ks_tables.h"
381ab64890Smrg#include "Key.h"
391ab64890Smrg
401ab64890Smrg#ifndef KEYSYMDB
411ab64890Smrg#ifndef XKEYSYMDB
421ab64890Smrg#define KEYSYMDB "/usr/lib/X11/XKeysymDB"
431ab64890Smrg#else
441ab64890Smrg#define KEYSYMDB XKEYSYMDB
451ab64890Smrg#endif
461ab64890Smrg#endif
471ab64890Smrg
481ab64890Smrgstatic Bool initialized;
491ab64890Smrgstatic XrmDatabase keysymdb;
501ab64890Smrgstatic XrmQuark Qkeysym[2];
511ab64890Smrg
521ab64890SmrgXrmDatabase
531ab64890Smrg_XInitKeysymDB(void)
541ab64890Smrg{
551ab64890Smrg    if (!initialized)
561ab64890Smrg    {
571ab64890Smrg	const char *dbname;
581ab64890Smrg
591ab64890Smrg	XrmInitialize();
601ab64890Smrg	/* use and name of this env var is not part of the standard */
611ab64890Smrg	/* implementation-dependent feature */
621ab64890Smrg	dbname = getenv("XKEYSYMDB");
631ab64890Smrg	if (!dbname)
641ab64890Smrg	    dbname = KEYSYMDB;
651ab64890Smrg	keysymdb = XrmGetFileDatabase(dbname);
661ab64890Smrg	if (keysymdb)
671ab64890Smrg	    Qkeysym[0] = XrmStringToQuark("Keysym");
681ab64890Smrg	initialized = True;
691ab64890Smrg    }
701ab64890Smrg    return keysymdb;
711ab64890Smrg}
721ab64890Smrg
731ab64890SmrgKeySym
741ab64890SmrgXStringToKeysym(_Xconst char *s)
751ab64890Smrg{
761ab64890Smrg    register int i, n;
771ab64890Smrg    int h;
781ab64890Smrg    register Signature sig = 0;
791ab64890Smrg    register const char *p = s;
801ab64890Smrg    register int c;
811ab64890Smrg    register int idx;
821ab64890Smrg    const unsigned char *entry;
831ab64890Smrg    unsigned char sig1, sig2;
841ab64890Smrg    KeySym val;
851ab64890Smrg
861ab64890Smrg    while ((c = *p++))
871ab64890Smrg	sig = (sig << 1) + c;
881ab64890Smrg    i = sig % KTABLESIZE;
891ab64890Smrg    h = i + 1;
901ab64890Smrg    sig1 = (sig >> 8) & 0xff;
911ab64890Smrg    sig2 = sig & 0xff;
921ab64890Smrg    n = KMAXHASH;
931ab64890Smrg    while ((idx = hashString[i]))
941ab64890Smrg    {
951ab64890Smrg	entry = &_XkeyTable[idx];
961ab64890Smrg	if ((entry[0] == sig1) && (entry[1] == sig2) &&
97eb411b4bSmrg	    !strcmp(s, (const char *)entry + 6))
981ab64890Smrg	{
991ab64890Smrg	    val = (entry[2] << 24) | (entry[3] << 16) |
1001ab64890Smrg	          (entry[4] << 8)  | entry[5];
1011ab64890Smrg	    if (!val)
1021ab64890Smrg		val = XK_VoidSymbol;
1031ab64890Smrg	    return val;
1041ab64890Smrg	}
1051ab64890Smrg	if (!--n)
1061ab64890Smrg	    break;
1071ab64890Smrg	i += h;
1081ab64890Smrg	if (i >= KTABLESIZE)
1091ab64890Smrg	    i -= KTABLESIZE;
1101ab64890Smrg    }
1111ab64890Smrg
1121ab64890Smrg    if (!initialized)
1131ab64890Smrg	(void)_XInitKeysymDB();
1141ab64890Smrg    if (keysymdb)
1151ab64890Smrg    {
1161ab64890Smrg	XrmValue result;
1171ab64890Smrg	XrmRepresentation from_type;
1183233502eSmrg	char d;
1191ab64890Smrg	XrmQuark names[2];
1201ab64890Smrg
1211ab64890Smrg	names[0] = _XrmInternalStringToQuark(s, p - s - 1, sig, False);
1221ab64890Smrg	names[1] = NULLQUARK;
1231ab64890Smrg	(void)XrmQGetResource(keysymdb, names, Qkeysym, &from_type, &result);
1241ab64890Smrg	if (result.addr && (result.size > 1))
1251ab64890Smrg	{
1261ab64890Smrg	    val = 0;
1271ab64890Smrg	    for (i = 0; i < result.size - 1; i++)
1281ab64890Smrg	    {
1293233502eSmrg		d = ((char *)result.addr)[i];
1303233502eSmrg		if ('0' <= d && d <= '9') val = (val<<4)+d-'0';
1313233502eSmrg		else if ('a' <= d && d <= 'f') val = (val<<4)+d-'a'+10;
1323233502eSmrg		else if ('A' <= d && d <= 'F') val = (val<<4)+d-'A'+10;
1331ab64890Smrg		else return NoSymbol;
1341ab64890Smrg	    }
1351ab64890Smrg	    return val;
1361ab64890Smrg	}
1371ab64890Smrg    }
1381ab64890Smrg
1391ab64890Smrg    if (*s == 'U') {
1401ab64890Smrg    	val = 0;
1411ab64890Smrg        for (p = &s[1]; *p; p++) {
1421ab64890Smrg            c = *p;
1431ab64890Smrg	    if ('0' <= c && c <= '9') val = (val<<4)+c-'0';
1441ab64890Smrg	    else if ('a' <= c && c <= 'f') val = (val<<4)+c-'a'+10;
1451ab64890Smrg	    else if ('A' <= c && c <= 'F') val = (val<<4)+c-'A'+10;
1461ab64890Smrg	    else return NoSymbol;
1471ab64890Smrg	    if (val > 0x10ffff)
1481ab64890Smrg		return NoSymbol;
1491ab64890Smrg	}
1501ab64890Smrg	if (val < 0x20 || (val > 0x7e && val < 0xa0))
1511ab64890Smrg	    return NoSymbol;
1521ab64890Smrg	if (val < 0x100)
1531ab64890Smrg	    return val;
1541ab64890Smrg        return val | 0x01000000;
1551ab64890Smrg    }
15647f86ac1Smrg
15747f86ac1Smrg    if (strlen(s) > 2 && s[0] == '0' && s[1] == 'x') {
15847f86ac1Smrg        char *tmp = NULL;
15947f86ac1Smrg        val = strtoul(s, &tmp, 16);
16047f86ac1Smrg        if (val == ULONG_MAX || (tmp && *tmp != '\0'))
16147f86ac1Smrg            return NoSymbol;
16247f86ac1Smrg        else
16347f86ac1Smrg            return val;
16447f86ac1Smrg    }
16547f86ac1Smrg
16647f86ac1Smrg    /* Stupid inconsistency between the headers and XKeysymDB: the former has
16747f86ac1Smrg     * no separating underscore, while some XF86* syms in the latter did.
16847f86ac1Smrg     * As a last ditch effort, try without. */
16947f86ac1Smrg    if (strncmp(s, "XF86_", 5) == 0) {
17047f86ac1Smrg        KeySym ret;
17147f86ac1Smrg        char *tmp = strdup(s);
17247f86ac1Smrg        if (!tmp)
17347f86ac1Smrg            return NoSymbol;
17447f86ac1Smrg        memmove(&tmp[4], &tmp[5], strlen(s) - 5 + 1);
17547f86ac1Smrg        ret = XStringToKeysym(tmp);
17647f86ac1Smrg        free(tmp);
17747f86ac1Smrg        return ret;
17847f86ac1Smrg    }
17947f86ac1Smrg
1801ab64890Smrg    return NoSymbol;
1811ab64890Smrg}
182