11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1986, 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 in
121ab64890Smrgall copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
171ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201ab64890Smrg
211ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
221ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
231ab64890Smrgin this Software without prior written authorization from The Open Group.
241ab64890Smrg
251ab64890Smrg*/
261ab64890Smrg
271ab64890Smrg#ifdef HAVE_CONFIG_H
281ab64890Smrg#include <config.h>
291ab64890Smrg#endif
301ab64890Smrg#include "Xlibint.h"
311ab64890Smrg#include "Xintatom.h"
321ab64890Smrg
331ab64890Smrgstatic
341ab64890Smrgchar *_XGetAtomName(
351ab64890Smrg    Display *dpy,
361ab64890Smrg    Atom atom)
371ab64890Smrg{
381ab64890Smrg    xResourceReq *req;
391ab64890Smrg    register Entry *table;
401ab64890Smrg    register int idx;
411ab64890Smrg    register Entry e;
421ab64890Smrg
431ab64890Smrg    if (dpy->atoms) {
441ab64890Smrg	table = dpy->atoms->table;
451ab64890Smrg	for (idx = TABLESIZE; --idx >= 0; ) {
461ab64890Smrg	    if ((e = *table++) && (e->atom == atom)) {
47258a0ebeSmrg		return strdup(EntryName(e));
4861b2299dSmrg	    }
491ab64890Smrg	}
501ab64890Smrg    }
511ab64890Smrg    GetResReq(GetAtomName, atom, req);
521ab64890Smrg    return (char *)NULL;
531ab64890Smrg}
541ab64890Smrg
551ab64890Smrgchar *XGetAtomName(
561ab64890Smrg    register Display *dpy,
571ab64890Smrg    Atom atom)
581ab64890Smrg{
591ab64890Smrg    xGetAtomNameReply rep;
601ab64890Smrg    char *name;
611ab64890Smrg
621ab64890Smrg    LockDisplay(dpy);
631ab64890Smrg    if ((name = _XGetAtomName(dpy, atom))) {
641ab64890Smrg	UnlockDisplay(dpy);
651ab64890Smrg	return name;
6661b2299dSmrg    }
671ab64890Smrg    if (_XReply(dpy, (xReply *)&rep, 0, xFalse) == 0) {
681ab64890Smrg	UnlockDisplay(dpy);
691ab64890Smrg	SyncHandle();
701ab64890Smrg	return(NULL);
711ab64890Smrg    }
72eb411b4bSmrg    if ((name = Xmalloc(rep.nameLength + 1))) {
731ab64890Smrg	_XReadPad(dpy, name, (long)rep.nameLength);
741ab64890Smrg	name[rep.nameLength] = '\0';
751ab64890Smrg	_XUpdateAtomCache(dpy, name, atom, 0, -1, 0);
761ab64890Smrg    } else {
77eb411b4bSmrg	_XEatDataWords(dpy, rep.length);
781ab64890Smrg	name = (char *) NULL;
791ab64890Smrg    }
801ab64890Smrg    UnlockDisplay(dpy);
811ab64890Smrg    SyncHandle();
821ab64890Smrg    return(name);
831ab64890Smrg}
841ab64890Smrg
851ab64890Smrgtypedef struct {
862d67cb4fSmrg    uint64_t start_seq;
872d67cb4fSmrg    uint64_t stop_seq;
881ab64890Smrg    Atom *atoms;
891ab64890Smrg    char **names;
901ab64890Smrg    int idx;
911ab64890Smrg    int count;
921ab64890Smrg    Status status;
931ab64890Smrg} _XGetAtomNameState;
941ab64890Smrg
951ab64890Smrgstatic
961ab64890SmrgBool _XGetAtomNameHandler(
971ab64890Smrg    register Display *dpy,
981ab64890Smrg    register xReply *rep,
991ab64890Smrg    char *buf,
1001ab64890Smrg    int len,
1011ab64890Smrg    XPointer data)
1021ab64890Smrg{
1031ab64890Smrg    register _XGetAtomNameState *state;
1041ab64890Smrg    xGetAtomNameReply replbuf;
1051ab64890Smrg    register xGetAtomNameReply *repl;
1062d67cb4fSmrg    uint64_t last_request_read = X_DPY_GET_LAST_REQUEST_READ(dpy);
1071ab64890Smrg
1081ab64890Smrg    state = (_XGetAtomNameState *)data;
1092d67cb4fSmrg    if (last_request_read < state->start_seq ||
1102d67cb4fSmrg	last_request_read > state->stop_seq)
1111ab64890Smrg	return False;
1121ab64890Smrg    while (state->idx < state->count && state->names[state->idx])
1131ab64890Smrg	state->idx++;
1141ab64890Smrg    if (state->idx >= state->count)
1151ab64890Smrg	return False;
1161ab64890Smrg    if (rep->generic.type == X_Error) {
1171ab64890Smrg	state->status = 0;
1181ab64890Smrg	return False;
1191ab64890Smrg    }
1201ab64890Smrg    repl = (xGetAtomNameReply *)
1211ab64890Smrg	_XGetAsyncReply(dpy, (char *)&replbuf, rep, buf, len,
1221ab64890Smrg			(SIZEOF(xGetAtomNameReply) - SIZEOF(xReply)) >> 2,
1231ab64890Smrg			False);
124eb411b4bSmrg    state->names[state->idx] = Xmalloc(repl->nameLength + 1);
1251ab64890Smrg    _XGetAsyncData(dpy, state->names[state->idx], buf, len,
1261ab64890Smrg		   SIZEOF(xGetAtomNameReply), repl->nameLength,
1271ab64890Smrg		   repl->length << 2);
1281ab64890Smrg    if (state->names[state->idx]) {
1291ab64890Smrg	state->names[state->idx][repl->nameLength] = '\0';
1301ab64890Smrg	_XUpdateAtomCache(dpy, state->names[state->idx],
1311ab64890Smrg			  state->atoms[state->idx], 0, -1, 0);
1321ab64890Smrg    } else {
1331ab64890Smrg	state->status = 0;
1341ab64890Smrg    }
1351ab64890Smrg    return True;
1361ab64890Smrg}
1371ab64890Smrg
1381ab64890SmrgStatus
1391ab64890SmrgXGetAtomNames (
1401ab64890Smrg    Display *dpy,
1411ab64890Smrg    Atom *atoms,
1421ab64890Smrg    int count,
1431ab64890Smrg    char **names_return)
1441ab64890Smrg{
1451ab64890Smrg    _XAsyncHandler async;
1461ab64890Smrg    _XGetAtomNameState async_state;
1471ab64890Smrg    xGetAtomNameReply rep;
1481ab64890Smrg    int i;
1491ab64890Smrg    int missed = -1;
1501ab64890Smrg
1511ab64890Smrg    LockDisplay(dpy);
1522d67cb4fSmrg    async_state.start_seq = X_DPY_GET_REQUEST(dpy) + 1;
1531ab64890Smrg    async_state.atoms = atoms;
1541ab64890Smrg    async_state.names = names_return;
1551ab64890Smrg    async_state.idx = 0;
1561ab64890Smrg    async_state.count = count - 1;
1571ab64890Smrg    async_state.status = 1;
1581ab64890Smrg    async.next = dpy->async_handlers;
1591ab64890Smrg    async.handler = _XGetAtomNameHandler;
1601ab64890Smrg    async.data = (XPointer)&async_state;
1611ab64890Smrg    dpy->async_handlers = &async;
1621ab64890Smrg    for (i = 0; i < count; i++) {
1631ab64890Smrg	if (!(names_return[i] = _XGetAtomName(dpy, atoms[i]))) {
1641ab64890Smrg	    missed = i;
1652d67cb4fSmrg	    async_state.stop_seq = X_DPY_GET_REQUEST(dpy);
1661ab64890Smrg	}
1671ab64890Smrg    }
1681ab64890Smrg    if (missed >= 0) {
1691ab64890Smrg	if (_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
170eb411b4bSmrg	    if ((names_return[missed] = Xmalloc(rep.nameLength + 1))) {
1711ab64890Smrg		_XReadPad(dpy, names_return[missed], (long)rep.nameLength);
1721ab64890Smrg		names_return[missed][rep.nameLength] = '\0';
1731ab64890Smrg		_XUpdateAtomCache(dpy, names_return[missed], atoms[missed],
1741ab64890Smrg				  0, -1, 0);
1751ab64890Smrg	    } else {
176eb411b4bSmrg		_XEatDataWords(dpy, rep.length);
1771ab64890Smrg		async_state.status = 0;
1781ab64890Smrg	    }
1791ab64890Smrg	}
1801ab64890Smrg    }
1811ab64890Smrg    DeqAsyncHandler(dpy, &async);
1821ab64890Smrg    UnlockDisplay(dpy);
1831ab64890Smrg    if (missed >= 0)
1841ab64890Smrg	SyncHandle();
1851ab64890Smrg    return async_state.status;
1861ab64890Smrg}
187