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