StrKeysym.c revision 1ab64890
11ab64890Smrg/* $Xorg: StrKeysym.c,v 1.5 2001/02/09 02:03:37 xorgcvs Exp $ */
21ab64890Smrg/*
31ab64890Smrg
41ab64890SmrgCopyright 1985, 1987, 1990, 1998  The Open Group
51ab64890Smrg
61ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
71ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
81ab64890Smrgthe above copyright notice appear in all copies and that both that
91ab64890Smrgcopyright notice and this permission notice appear in supporting
101ab64890Smrgdocumentation.
111ab64890Smrg
121ab64890SmrgThe above copyright notice and this permission notice shall be included in
131ab64890Smrgall copies or substantial portions of the Software.
141ab64890Smrg
151ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
181ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
191ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
201ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
211ab64890Smrg
221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
231ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
241ab64890Smrgin this Software without prior written authorization from The Open Group.
251ab64890Smrg
261ab64890Smrg*/
271ab64890Smrg/* $XFree86: xc/lib/X11/StrKeysym.c,v 3.7 2003/04/13 19:22:18 dawes Exp $ */
281ab64890Smrg
291ab64890Smrg#ifdef HAVE_CONFIG_H
301ab64890Smrg#include <config.h>
311ab64890Smrg#endif
321ab64890Smrg#include "Xlibint.h"
331ab64890Smrg#include <X11/Xresource.h>
341ab64890Smrg#include <X11/keysymdef.h>
351ab64890Smrg#include "Xresinternal.h"
361ab64890Smrg
371ab64890Smrg#define NEEDKTABLE
381ab64890Smrg#include "ks_tables.h"
391ab64890Smrg#include "Key.h"
401ab64890Smrg
411ab64890Smrg#ifndef KEYSYMDB
421ab64890Smrg#ifndef XKEYSYMDB
431ab64890Smrg#define KEYSYMDB "/usr/lib/X11/XKeysymDB"
441ab64890Smrg#else
451ab64890Smrg#define KEYSYMDB XKEYSYMDB
461ab64890Smrg#endif
471ab64890Smrg#endif
481ab64890Smrg
491ab64890Smrgstatic Bool initialized;
501ab64890Smrgstatic XrmDatabase keysymdb;
511ab64890Smrgstatic XrmQuark Qkeysym[2];
521ab64890Smrg
531ab64890SmrgXrmDatabase
541ab64890Smrg_XInitKeysymDB(void)
551ab64890Smrg{
561ab64890Smrg    if (!initialized)
571ab64890Smrg    {
581ab64890Smrg	const char *dbname;
591ab64890Smrg
601ab64890Smrg	XrmInitialize();
611ab64890Smrg	/* use and name of this env var is not part of the standard */
621ab64890Smrg	/* implementation-dependent feature */
631ab64890Smrg	dbname = getenv("XKEYSYMDB");
641ab64890Smrg	if (!dbname)
651ab64890Smrg	    dbname = KEYSYMDB;
661ab64890Smrg	keysymdb = XrmGetFileDatabase(dbname);
671ab64890Smrg	if (keysymdb)
681ab64890Smrg	    Qkeysym[0] = XrmStringToQuark("Keysym");
691ab64890Smrg	initialized = True;
701ab64890Smrg    }
711ab64890Smrg    return keysymdb;
721ab64890Smrg}
731ab64890Smrg
741ab64890SmrgKeySym
751ab64890SmrgXStringToKeysym(_Xconst char *s)
761ab64890Smrg{
771ab64890Smrg    register int i, n;
781ab64890Smrg    int h;
791ab64890Smrg    register Signature sig = 0;
801ab64890Smrg    register const char *p = s;
811ab64890Smrg    register int c;
821ab64890Smrg    register int idx;
831ab64890Smrg    const unsigned char *entry;
841ab64890Smrg    unsigned char sig1, sig2;
851ab64890Smrg    KeySym val;
861ab64890Smrg
871ab64890Smrg    while ((c = *p++))
881ab64890Smrg	sig = (sig << 1) + c;
891ab64890Smrg    i = sig % KTABLESIZE;
901ab64890Smrg    h = i + 1;
911ab64890Smrg    sig1 = (sig >> 8) & 0xff;
921ab64890Smrg    sig2 = sig & 0xff;
931ab64890Smrg    n = KMAXHASH;
941ab64890Smrg    while ((idx = hashString[i]))
951ab64890Smrg    {
961ab64890Smrg	entry = &_XkeyTable[idx];
971ab64890Smrg	if ((entry[0] == sig1) && (entry[1] == sig2) &&
981ab64890Smrg	    !strcmp(s, (char *)entry + 6))
991ab64890Smrg	{
1001ab64890Smrg	    val = (entry[2] << 24) | (entry[3] << 16) |
1011ab64890Smrg	          (entry[4] << 8)  | entry[5];
1021ab64890Smrg	    if (!val)
1031ab64890Smrg		val = XK_VoidSymbol;
1041ab64890Smrg	    return val;
1051ab64890Smrg	}
1061ab64890Smrg	if (!--n)
1071ab64890Smrg	    break;
1081ab64890Smrg	i += h;
1091ab64890Smrg	if (i >= KTABLESIZE)
1101ab64890Smrg	    i -= KTABLESIZE;
1111ab64890Smrg    }
1121ab64890Smrg
1131ab64890Smrg    if (!initialized)
1141ab64890Smrg	(void)_XInitKeysymDB();
1151ab64890Smrg    if (keysymdb)
1161ab64890Smrg    {
1171ab64890Smrg	XrmValue result;
1181ab64890Smrg	XrmRepresentation from_type;
1191ab64890Smrg	char c;
1201ab64890Smrg	XrmQuark names[2];
1211ab64890Smrg
1221ab64890Smrg	names[0] = _XrmInternalStringToQuark(s, p - s - 1, sig, False);
1231ab64890Smrg	names[1] = NULLQUARK;
1241ab64890Smrg	(void)XrmQGetResource(keysymdb, names, Qkeysym, &from_type, &result);
1251ab64890Smrg	if (result.addr && (result.size > 1))
1261ab64890Smrg	{
1271ab64890Smrg	    val = 0;
1281ab64890Smrg	    for (i = 0; i < result.size - 1; i++)
1291ab64890Smrg	    {
1301ab64890Smrg		c = ((char *)result.addr)[i];
1311ab64890Smrg		if ('0' <= c && c <= '9') val = (val<<4)+c-'0';
1321ab64890Smrg		else if ('a' <= c && c <= 'f') val = (val<<4)+c-'a'+10;
1331ab64890Smrg		else if ('A' <= c && c <= 'F') val = (val<<4)+c-'A'+10;
1341ab64890Smrg		else return NoSymbol;
1351ab64890Smrg	    }
1361ab64890Smrg	    return val;
1371ab64890Smrg	}
1381ab64890Smrg    }
1391ab64890Smrg
1401ab64890Smrg    if (*s == 'U') {
1411ab64890Smrg    	val = 0;
1421ab64890Smrg        for (p = &s[1]; *p; p++) {
1431ab64890Smrg            c = *p;
1441ab64890Smrg	    if ('0' <= c && c <= '9') val = (val<<4)+c-'0';
1451ab64890Smrg	    else if ('a' <= c && c <= 'f') val = (val<<4)+c-'a'+10;
1461ab64890Smrg	    else if ('A' <= c && c <= 'F') val = (val<<4)+c-'A'+10;
1471ab64890Smrg	    else return NoSymbol;
1481ab64890Smrg	    if (val > 0x10ffff)
1491ab64890Smrg		return NoSymbol;
1501ab64890Smrg	}
1511ab64890Smrg	if (val < 0x20 || (val > 0x7e && val < 0xa0))
1521ab64890Smrg	    return NoSymbol;
1531ab64890Smrg	if (val < 0x100)
1541ab64890Smrg	    return val;
1551ab64890Smrg        return val | 0x01000000;
1561ab64890Smrg    }
1571ab64890Smrg    return NoSymbol;
1581ab64890Smrg}
159