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