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))
50818534a1Smrg		Xfree(e);
511ab64890Smrg	}
52818534a1Smrg	Xfree(dpy->atoms);
53d4a3aaf4Smrg	dpy->atoms = NULL;
541ab64890Smrg    }
551ab64890Smrg}
561ab64890Smrg
571ab64890Smrgstatic
581ab64890SmrgAtom _XInternAtom(
591ab64890Smrg    Display *dpy,
601ab64890Smrg    _Xconst char *name,
611ab64890Smrg    Bool onlyIfExists,
621ab64890Smrg    unsigned long *psig,
631ab64890Smrg    int *pidx,
641ab64890Smrg    int *pn)
651ab64890Smrg{
661ab64890Smrg    register AtomTable *atoms;
671ab64890Smrg    register char *s1, c, *s2;
681ab64890Smrg    register unsigned long sig;
691ab64890Smrg    register int idx = 0, i;
701ab64890Smrg    Entry e;
711ab64890Smrg    int n, firstidx, rehash = 0;
721ab64890Smrg    xInternAtomReq *req;
731ab64890Smrg
741ab64890Smrg    /* look in the cache first */
751ab64890Smrg    if (!(atoms = dpy->atoms)) {
76eb411b4bSmrg	dpy->atoms = atoms = Xcalloc(1, sizeof(AtomTable));
771ab64890Smrg	dpy->free_funcs->atoms = _XFreeAtomTable;
781ab64890Smrg    }
791ab64890Smrg    sig = 0;
801ab64890Smrg    for (s1 = (char *)name; (c = *s1++); )
811ab64890Smrg	sig += c;
821ab64890Smrg    n = s1 - (char *)name - 1;
831ab64890Smrg    if (atoms) {
841ab64890Smrg	firstidx = idx = HASH(sig);
851ab64890Smrg	while ((e = atoms->table[idx])) {
861ab64890Smrg	    if (e != RESERVED && e->sig == sig) {
871ab64890Smrg	    	for (i = n, s1 = (char *)name, s2 = EntryName(e); --i >= 0; ) {
881ab64890Smrg		    if (*s1++ != *s2++)
891ab64890Smrg		    	goto nomatch;
901ab64890Smrg	    	}
911ab64890Smrg	    	if (!*s2)
921ab64890Smrg		    return e->atom;
931ab64890Smrg	    }
941ab64890Smrgnomatch:    if (idx == firstidx)
951ab64890Smrg		rehash = REHASHVAL(sig);
961ab64890Smrg	    idx = REHASH(idx, rehash);
971ab64890Smrg	    if (idx == firstidx)
981ab64890Smrg		break;
991ab64890Smrg	}
1001ab64890Smrg    }
1011ab64890Smrg    *psig = sig;
1021ab64890Smrg    *pidx = idx;
1031ab64890Smrg    if (atoms && !atoms->table[idx])
1041ab64890Smrg	atoms->table[idx] = RESERVED; /* reserve slot */
1051ab64890Smrg    *pn = n;
1061ab64890Smrg    /* not found, go to the server */
1071ab64890Smrg    GetReq(InternAtom, req);
1081ab64890Smrg    req->nbytes = n;
1091ab64890Smrg    req->onlyIfExists = onlyIfExists;
1101ab64890Smrg    req->length += (n+3)>>2;
1111ab64890Smrg    Data(dpy, name, n);
1121ab64890Smrg    return None;
1131ab64890Smrg}
1141ab64890Smrg
1151ab64890Smrgvoid
1161ab64890Smrg_XUpdateAtomCache(
1171ab64890Smrg    Display *dpy,
1181ab64890Smrg    const char *name,
1191ab64890Smrg    Atom atom,
1201ab64890Smrg    unsigned long sig,
1211ab64890Smrg    int idx,
1221ab64890Smrg    int n)
1231ab64890Smrg{
1241ab64890Smrg    Entry e, oe;
1251ab64890Smrg    register char *s1;
1261ab64890Smrg    register char c;
1271ab64890Smrg    int firstidx, rehash;
1281ab64890Smrg
1291ab64890Smrg    if (!dpy->atoms) {
1301ab64890Smrg	if (idx < 0) {
131eb411b4bSmrg	    dpy->atoms = Xcalloc(1, sizeof(AtomTable));
1321ab64890Smrg	    dpy->free_funcs->atoms = _XFreeAtomTable;
1331ab64890Smrg	}
1341ab64890Smrg	if (!dpy->atoms)
1351ab64890Smrg	    return;
1361ab64890Smrg    }
1371ab64890Smrg    if (!sig) {
1381ab64890Smrg	for (s1 = (char *)name; (c = *s1++); )
1391ab64890Smrg	    sig += c;
1401ab64890Smrg	n = s1 - (char *)name - 1;
1411ab64890Smrg	if (idx < 0) {
1421ab64890Smrg	    firstidx = idx = HASH(sig);
1431ab64890Smrg	    if (dpy->atoms->table[idx]) {
1441ab64890Smrg		rehash = REHASHVAL(sig);
1451ab64890Smrg		do
1461ab64890Smrg		    idx = REHASH(idx, rehash);
1471ab64890Smrg		while (idx != firstidx && dpy->atoms->table[idx]);
1481ab64890Smrg	    }
1491ab64890Smrg	}
1501ab64890Smrg    }
151eb411b4bSmrg    e = Xmalloc(sizeof(EntryRec) + n + 1);
1521ab64890Smrg    if (e) {
1531ab64890Smrg	e->sig = sig;
1541ab64890Smrg	e->atom = atom;
1551ab64890Smrg	strcpy(EntryName(e), name);
1561ab64890Smrg	if ((oe = dpy->atoms->table[idx]) && (oe != RESERVED))
157818534a1Smrg	    Xfree(oe);
1581ab64890Smrg	dpy->atoms->table[idx] = e;
1591ab64890Smrg    }
1601ab64890Smrg}
1611ab64890Smrg
1621ab64890SmrgAtom
1631ab64890SmrgXInternAtom (
1641ab64890Smrg    Display *dpy,
1651ab64890Smrg    const char *name,
1661ab64890Smrg    Bool onlyIfExists)
1671ab64890Smrg{
1681ab64890Smrg    Atom atom;
1691ab64890Smrg    unsigned long sig;
1701ab64890Smrg    int idx, n;
1711ab64890Smrg    xInternAtomReply rep;
1721ab64890Smrg
1731ab64890Smrg    if (!name)
1741ab64890Smrg	name = "";
1751ab64890Smrg    LockDisplay(dpy);
1761ab64890Smrg    if ((atom = _XInternAtom(dpy, name, onlyIfExists, &sig, &idx, &n))) {
1771ab64890Smrg	UnlockDisplay(dpy);
1781ab64890Smrg	return atom;
1791ab64890Smrg    }
1801ab64890Smrg    if (dpy->atoms && dpy->atoms->table[idx] == RESERVED)
1811ab64890Smrg	dpy->atoms->table[idx] = NULL; /* unreserve slot */
1821ab64890Smrg    if (_XReply (dpy, (xReply *)&rep, 0, xTrue)) {
1831ab64890Smrg	if ((atom = rep.atom))
1841ab64890Smrg	    _XUpdateAtomCache(dpy, name, atom, sig, idx, n);
1851ab64890Smrg    }
1861ab64890Smrg    UnlockDisplay(dpy);
1871ab64890Smrg    SyncHandle();
1881ab64890Smrg    return (rep.atom);
1891ab64890Smrg}
1901ab64890Smrg
1911ab64890Smrgtypedef struct {
1922d67cb4fSmrg    uint64_t start_seq;
1932d67cb4fSmrg    uint64_t stop_seq;
1941ab64890Smrg    char **names;
1951ab64890Smrg    Atom *atoms;
1961ab64890Smrg    int count;
1971ab64890Smrg    Status status;
1981ab64890Smrg} _XIntAtomState;
1991ab64890Smrg
2001ab64890Smrgstatic
2011ab64890SmrgBool _XIntAtomHandler(
2021ab64890Smrg    register Display *dpy,
2031ab64890Smrg    register xReply *rep,
2041ab64890Smrg    char *buf,
2051ab64890Smrg    int len,
2061ab64890Smrg    XPointer data)
2071ab64890Smrg{
2081ab64890Smrg    register _XIntAtomState *state;
2091ab64890Smrg    register int i, idx = 0;
2101ab64890Smrg    xInternAtomReply replbuf;
2111ab64890Smrg    register xInternAtomReply *repl;
2122d67cb4fSmrg    uint64_t last_request_read = X_DPY_GET_LAST_REQUEST_READ(dpy);
2131ab64890Smrg
2141ab64890Smrg    state = (_XIntAtomState *)data;
2152d67cb4fSmrg
2162d67cb4fSmrg    if (last_request_read < state->start_seq ||
2172d67cb4fSmrg	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);
2582d67cb4fSmrg    async_state.start_seq = X_DPY_GET_REQUEST(dpy) + 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);
2722d67cb4fSmrg	    async_state.stop_seq = X_DPY_GET_REQUEST(dpy);
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))
28861b2299dSmrg		_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