IntAtom.c revision eb411b4b
11ab64890Smrg/* 21ab64890Smrg 31ab64890SmrgCopyright 1986, 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 121ab64890Smrgin all copies or substantial portions of the Software. 131ab64890Smrg 141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151ab64890SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161ab64890SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 171ab64890SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR 181ab64890SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 191ab64890SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 201ab64890SmrgOTHER DEALINGS IN THE SOFTWARE. 211ab64890Smrg 221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall 231ab64890Smrgnot be used in advertising or otherwise to promote the sale, use or 241ab64890Smrgother dealings in this Software without prior written authorization 251ab64890Smrgfrom The Open Group. 261ab64890Smrg 271ab64890Smrg*/ 281ab64890Smrg 291ab64890Smrg#ifdef HAVE_CONFIG_H 301ab64890Smrg#include <config.h> 311ab64890Smrg#endif 321ab64890Smrg#include "Xlibint.h" 331ab64890Smrg#include "Xintatom.h" 341ab64890Smrg 351ab64890Smrg#define HASH(sig) ((sig) & (TABLESIZE-1)) 361ab64890Smrg#define REHASHVAL(sig) ((((sig) % (TABLESIZE-3)) + 2) | 1) 371ab64890Smrg#define REHASH(idx,rehash) ((idx + rehash) & (TABLESIZE-1)) 381ab64890Smrg 391ab64890Smrgvoid 401ab64890Smrg_XFreeAtomTable(Display *dpy) 411ab64890Smrg{ 421ab64890Smrg register Entry *table; 431ab64890Smrg register int i; 441ab64890Smrg register Entry e; 451ab64890Smrg 461ab64890Smrg if (dpy->atoms) { 471ab64890Smrg table = dpy->atoms->table; 481ab64890Smrg for (i = TABLESIZE; --i >= 0; ) { 491ab64890Smrg if ((e = *table++) && (e != RESERVED)) 501ab64890Smrg Xfree((char *)e); 511ab64890Smrg } 521ab64890Smrg Xfree((char *)dpy->atoms); 531ab64890Smrg } 541ab64890Smrg} 551ab64890Smrg 561ab64890Smrgstatic 571ab64890SmrgAtom _XInternAtom( 581ab64890Smrg Display *dpy, 591ab64890Smrg _Xconst char *name, 601ab64890Smrg Bool onlyIfExists, 611ab64890Smrg unsigned long *psig, 621ab64890Smrg int *pidx, 631ab64890Smrg int *pn) 641ab64890Smrg{ 651ab64890Smrg register AtomTable *atoms; 661ab64890Smrg register char *s1, c, *s2; 671ab64890Smrg register unsigned long sig; 681ab64890Smrg register int idx = 0, i; 691ab64890Smrg Entry e; 701ab64890Smrg int n, firstidx, rehash = 0; 711ab64890Smrg xInternAtomReq *req; 721ab64890Smrg 731ab64890Smrg /* look in the cache first */ 741ab64890Smrg if (!(atoms = dpy->atoms)) { 75eb411b4bSmrg dpy->atoms = atoms = Xcalloc(1, sizeof(AtomTable)); 761ab64890Smrg dpy->free_funcs->atoms = _XFreeAtomTable; 771ab64890Smrg } 781ab64890Smrg sig = 0; 791ab64890Smrg for (s1 = (char *)name; (c = *s1++); ) 801ab64890Smrg sig += c; 811ab64890Smrg n = s1 - (char *)name - 1; 821ab64890Smrg if (atoms) { 831ab64890Smrg firstidx = idx = HASH(sig); 841ab64890Smrg while ((e = atoms->table[idx])) { 851ab64890Smrg if (e != RESERVED && e->sig == sig) { 861ab64890Smrg for (i = n, s1 = (char *)name, s2 = EntryName(e); --i >= 0; ) { 871ab64890Smrg if (*s1++ != *s2++) 881ab64890Smrg goto nomatch; 891ab64890Smrg } 901ab64890Smrg if (!*s2) 911ab64890Smrg return e->atom; 921ab64890Smrg } 931ab64890Smrgnomatch: if (idx == firstidx) 941ab64890Smrg rehash = REHASHVAL(sig); 951ab64890Smrg idx = REHASH(idx, rehash); 961ab64890Smrg if (idx == firstidx) 971ab64890Smrg break; 981ab64890Smrg } 991ab64890Smrg } 1001ab64890Smrg *psig = sig; 1011ab64890Smrg *pidx = idx; 1021ab64890Smrg if (atoms && !atoms->table[idx]) 1031ab64890Smrg atoms->table[idx] = RESERVED; /* reserve slot */ 1041ab64890Smrg *pn = n; 1051ab64890Smrg /* not found, go to the server */ 1061ab64890Smrg GetReq(InternAtom, req); 1071ab64890Smrg req->nbytes = n; 1081ab64890Smrg req->onlyIfExists = onlyIfExists; 1091ab64890Smrg req->length += (n+3)>>2; 1101ab64890Smrg Data(dpy, name, n); 1111ab64890Smrg return None; 1121ab64890Smrg} 1131ab64890Smrg 1141ab64890Smrgvoid 1151ab64890Smrg_XUpdateAtomCache( 1161ab64890Smrg Display *dpy, 1171ab64890Smrg const char *name, 1181ab64890Smrg Atom atom, 1191ab64890Smrg unsigned long sig, 1201ab64890Smrg int idx, 1211ab64890Smrg int n) 1221ab64890Smrg{ 1231ab64890Smrg Entry e, oe; 1241ab64890Smrg register char *s1; 1251ab64890Smrg register char c; 1261ab64890Smrg int firstidx, rehash; 1271ab64890Smrg 1281ab64890Smrg if (!dpy->atoms) { 1291ab64890Smrg if (idx < 0) { 130eb411b4bSmrg dpy->atoms = Xcalloc(1, sizeof(AtomTable)); 1311ab64890Smrg dpy->free_funcs->atoms = _XFreeAtomTable; 1321ab64890Smrg } 1331ab64890Smrg if (!dpy->atoms) 1341ab64890Smrg return; 1351ab64890Smrg } 1361ab64890Smrg if (!sig) { 1371ab64890Smrg for (s1 = (char *)name; (c = *s1++); ) 1381ab64890Smrg sig += c; 1391ab64890Smrg n = s1 - (char *)name - 1; 1401ab64890Smrg if (idx < 0) { 1411ab64890Smrg firstidx = idx = HASH(sig); 1421ab64890Smrg if (dpy->atoms->table[idx]) { 1431ab64890Smrg rehash = REHASHVAL(sig); 1441ab64890Smrg do 1451ab64890Smrg idx = REHASH(idx, rehash); 1461ab64890Smrg while (idx != firstidx && dpy->atoms->table[idx]); 1471ab64890Smrg } 1481ab64890Smrg } 1491ab64890Smrg } 150eb411b4bSmrg e = Xmalloc(sizeof(EntryRec) + n + 1); 1511ab64890Smrg if (e) { 1521ab64890Smrg e->sig = sig; 1531ab64890Smrg e->atom = atom; 1541ab64890Smrg strcpy(EntryName(e), name); 1551ab64890Smrg if ((oe = dpy->atoms->table[idx]) && (oe != RESERVED)) 1561ab64890Smrg Xfree((char *)oe); 1571ab64890Smrg dpy->atoms->table[idx] = e; 1581ab64890Smrg } 1591ab64890Smrg} 1601ab64890Smrg 1611ab64890SmrgAtom 1621ab64890SmrgXInternAtom ( 1631ab64890Smrg Display *dpy, 1641ab64890Smrg const char *name, 1651ab64890Smrg Bool onlyIfExists) 1661ab64890Smrg{ 1671ab64890Smrg Atom atom; 1681ab64890Smrg unsigned long sig; 1691ab64890Smrg int idx, n; 1701ab64890Smrg xInternAtomReply rep; 1711ab64890Smrg 1721ab64890Smrg if (!name) 1731ab64890Smrg name = ""; 1741ab64890Smrg LockDisplay(dpy); 1751ab64890Smrg if ((atom = _XInternAtom(dpy, name, onlyIfExists, &sig, &idx, &n))) { 1761ab64890Smrg UnlockDisplay(dpy); 1771ab64890Smrg return atom; 1781ab64890Smrg } 1791ab64890Smrg if (dpy->atoms && dpy->atoms->table[idx] == RESERVED) 1801ab64890Smrg dpy->atoms->table[idx] = NULL; /* unreserve slot */ 1811ab64890Smrg if (_XReply (dpy, (xReply *)&rep, 0, xTrue)) { 1821ab64890Smrg if ((atom = rep.atom)) 1831ab64890Smrg _XUpdateAtomCache(dpy, name, atom, sig, idx, n); 1841ab64890Smrg } 1851ab64890Smrg UnlockDisplay(dpy); 1861ab64890Smrg SyncHandle(); 1871ab64890Smrg return (rep.atom); 1881ab64890Smrg} 1891ab64890Smrg 1901ab64890Smrgtypedef struct { 1911ab64890Smrg unsigned long start_seq; 1921ab64890Smrg unsigned long stop_seq; 1931ab64890Smrg char **names; 1941ab64890Smrg Atom *atoms; 1951ab64890Smrg int count; 1961ab64890Smrg Status status; 1971ab64890Smrg} _XIntAtomState; 1981ab64890Smrg 1991ab64890Smrgstatic 2001ab64890SmrgBool _XIntAtomHandler( 2011ab64890Smrg register Display *dpy, 2021ab64890Smrg register xReply *rep, 2031ab64890Smrg char *buf, 2041ab64890Smrg int len, 2051ab64890Smrg XPointer data) 2061ab64890Smrg{ 2071ab64890Smrg register _XIntAtomState *state; 2081ab64890Smrg register int i, idx = 0; 2091ab64890Smrg xInternAtomReply replbuf; 2101ab64890Smrg register xInternAtomReply *repl; 2111ab64890Smrg 2121ab64890Smrg state = (_XIntAtomState *)data; 2131ab64890Smrg if (dpy->last_request_read < state->start_seq || 2141ab64890Smrg dpy->last_request_read > state->stop_seq) 2151ab64890Smrg return False; 2161ab64890Smrg for (i = 0; i < state->count; i++) { 2171ab64890Smrg if (state->atoms[i] & 0x80000000) { 2181ab64890Smrg idx = ~state->atoms[i]; 2191ab64890Smrg state->atoms[i] = None; 2201ab64890Smrg break; 2211ab64890Smrg } 2221ab64890Smrg } 2231ab64890Smrg if (i >= state->count) 2241ab64890Smrg return False; 2251ab64890Smrg if (rep->generic.type == X_Error) { 2261ab64890Smrg state->status = 0; 2271ab64890Smrg return False; 2281ab64890Smrg } 2291ab64890Smrg repl = (xInternAtomReply *) 2301ab64890Smrg _XGetAsyncReply(dpy, (char *)&replbuf, rep, buf, len, 2311ab64890Smrg (SIZEOF(xInternAtomReply) - SIZEOF(xReply)) >> 2, 2321ab64890Smrg True); 2331ab64890Smrg if ((state->atoms[i] = repl->atom)) 2341ab64890Smrg _XUpdateAtomCache(dpy, state->names[i], (Atom) repl->atom, 2351ab64890Smrg (unsigned long)0, idx, 0); 2361ab64890Smrg return True; 2371ab64890Smrg} 2381ab64890Smrg 2391ab64890SmrgStatus 2401ab64890SmrgXInternAtoms ( 2411ab64890Smrg Display *dpy, 2421ab64890Smrg char **names, 2431ab64890Smrg int count, 2441ab64890Smrg Bool onlyIfExists, 2451ab64890Smrg Atom *atoms_return) 2461ab64890Smrg{ 2471ab64890Smrg int i, idx, n, tidx; 2481ab64890Smrg unsigned long sig; 2491ab64890Smrg _XAsyncHandler async; 2501ab64890Smrg _XIntAtomState async_state; 2511ab64890Smrg int missed = -1; 2521ab64890Smrg xInternAtomReply rep; 2531ab64890Smrg 2541ab64890Smrg LockDisplay(dpy); 2551ab64890Smrg async_state.start_seq = dpy->request + 1; 2561ab64890Smrg async_state.atoms = atoms_return; 2571ab64890Smrg async_state.names = names; 2581ab64890Smrg async_state.count = count - 1; 2591ab64890Smrg async_state.status = 1; 2601ab64890Smrg async.next = dpy->async_handlers; 2611ab64890Smrg async.handler = _XIntAtomHandler; 2621ab64890Smrg async.data = (XPointer)&async_state; 2631ab64890Smrg dpy->async_handlers = &async; 2641ab64890Smrg for (i = 0; i < count; i++) { 2651ab64890Smrg if (!(atoms_return[i] = _XInternAtom(dpy, names[i], onlyIfExists, 2661ab64890Smrg &sig, &idx, &n))) { 2671ab64890Smrg missed = i; 2681ab64890Smrg atoms_return[i] = ~((Atom)idx); 2691ab64890Smrg async_state.stop_seq = dpy->request; 2701ab64890Smrg } 2711ab64890Smrg } 2721ab64890Smrg if (missed >= 0) { 2731ab64890Smrg if (dpy->atoms) { 2741ab64890Smrg /* unreserve anything we just reserved */ 2751ab64890Smrg for (i = 0; i < count; i++) { 2761ab64890Smrg if (atoms_return[i] & 0x80000000) { 2771ab64890Smrg tidx = ~atoms_return[i]; 2781ab64890Smrg if (dpy->atoms->table[tidx] == RESERVED) 2791ab64890Smrg dpy->atoms->table[tidx] = NULL; 2801ab64890Smrg } 2811ab64890Smrg } 2821ab64890Smrg } 2831ab64890Smrg if (_XReply (dpy, (xReply *)&rep, 0, xTrue)) { 2841ab64890Smrg if ((atoms_return[missed] = rep.atom)) 28561b2299dSmrg _XUpdateAtomCache(dpy, names[missed], (Atom) rep.atom, 2861ab64890Smrg sig, idx, n); 2871ab64890Smrg } else { 2881ab64890Smrg atoms_return[missed] = None; 2891ab64890Smrg async_state.status = 0; 2901ab64890Smrg } 2911ab64890Smrg } 2921ab64890Smrg DeqAsyncHandler(dpy, &async); 2931ab64890Smrg UnlockDisplay(dpy); 2941ab64890Smrg if (missed >= 0) 2951ab64890Smrg SyncHandle(); 2961ab64890Smrg return async_state.status; 2971ab64890Smrg} 298