GetAtomNm.c revision eb411b4b
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    char *name;
401ab64890Smrg    register Entry *table;
411ab64890Smrg    register int idx;
421ab64890Smrg    register Entry e;
431ab64890Smrg
441ab64890Smrg    if (dpy->atoms) {
451ab64890Smrg	table = dpy->atoms->table;
461ab64890Smrg	for (idx = TABLESIZE; --idx >= 0; ) {
471ab64890Smrg	    if ((e = *table++) && (e->atom == atom)) {
481ab64890Smrg		idx = strlen(EntryName(e)) + 1;
49eb411b4bSmrg		if ((name = Xmalloc(idx)))
501ab64890Smrg		    strcpy(name, EntryName(e));
511ab64890Smrg		return name;
5261b2299dSmrg	    }
531ab64890Smrg	}
541ab64890Smrg    }
551ab64890Smrg    GetResReq(GetAtomName, atom, req);
561ab64890Smrg    return (char *)NULL;
571ab64890Smrg}
581ab64890Smrg
591ab64890Smrgchar *XGetAtomName(
601ab64890Smrg    register Display *dpy,
611ab64890Smrg    Atom atom)
621ab64890Smrg{
631ab64890Smrg    xGetAtomNameReply rep;
641ab64890Smrg    char *name;
651ab64890Smrg
661ab64890Smrg    LockDisplay(dpy);
671ab64890Smrg    if ((name = _XGetAtomName(dpy, atom))) {
681ab64890Smrg	UnlockDisplay(dpy);
691ab64890Smrg	return name;
7061b2299dSmrg    }
711ab64890Smrg    if (_XReply(dpy, (xReply *)&rep, 0, xFalse) == 0) {
721ab64890Smrg	UnlockDisplay(dpy);
731ab64890Smrg	SyncHandle();
741ab64890Smrg	return(NULL);
751ab64890Smrg    }
76eb411b4bSmrg    if ((name = Xmalloc(rep.nameLength + 1))) {
771ab64890Smrg	_XReadPad(dpy, name, (long)rep.nameLength);
781ab64890Smrg	name[rep.nameLength] = '\0';
791ab64890Smrg	_XUpdateAtomCache(dpy, name, atom, 0, -1, 0);
801ab64890Smrg    } else {
81eb411b4bSmrg	_XEatDataWords(dpy, rep.length);
821ab64890Smrg	name = (char *) NULL;
831ab64890Smrg    }
841ab64890Smrg    UnlockDisplay(dpy);
851ab64890Smrg    SyncHandle();
861ab64890Smrg    return(name);
871ab64890Smrg}
881ab64890Smrg
891ab64890Smrgtypedef struct {
901ab64890Smrg    unsigned long start_seq;
911ab64890Smrg    unsigned long stop_seq;
921ab64890Smrg    Atom *atoms;
931ab64890Smrg    char **names;
941ab64890Smrg    int idx;
951ab64890Smrg    int count;
961ab64890Smrg    Status status;
971ab64890Smrg} _XGetAtomNameState;
981ab64890Smrg
991ab64890Smrgstatic
1001ab64890SmrgBool _XGetAtomNameHandler(
1011ab64890Smrg    register Display *dpy,
1021ab64890Smrg    register xReply *rep,
1031ab64890Smrg    char *buf,
1041ab64890Smrg    int len,
1051ab64890Smrg    XPointer data)
1061ab64890Smrg{
1071ab64890Smrg    register _XGetAtomNameState *state;
1081ab64890Smrg    xGetAtomNameReply replbuf;
1091ab64890Smrg    register xGetAtomNameReply *repl;
1101ab64890Smrg
1111ab64890Smrg    state = (_XGetAtomNameState *)data;
1121ab64890Smrg    if (dpy->last_request_read < state->start_seq ||
1131ab64890Smrg	dpy->last_request_read > state->stop_seq)
1141ab64890Smrg	return False;
1151ab64890Smrg    while (state->idx < state->count && state->names[state->idx])
1161ab64890Smrg	state->idx++;
1171ab64890Smrg    if (state->idx >= state->count)
1181ab64890Smrg	return False;
1191ab64890Smrg    if (rep->generic.type == X_Error) {
1201ab64890Smrg	state->status = 0;
1211ab64890Smrg	return False;
1221ab64890Smrg    }
1231ab64890Smrg    repl = (xGetAtomNameReply *)
1241ab64890Smrg	_XGetAsyncReply(dpy, (char *)&replbuf, rep, buf, len,
1251ab64890Smrg			(SIZEOF(xGetAtomNameReply) - SIZEOF(xReply)) >> 2,
1261ab64890Smrg			False);
127eb411b4bSmrg    state->names[state->idx] = Xmalloc(repl->nameLength + 1);
1281ab64890Smrg    _XGetAsyncData(dpy, state->names[state->idx], buf, len,
1291ab64890Smrg		   SIZEOF(xGetAtomNameReply), repl->nameLength,
1301ab64890Smrg		   repl->length << 2);
1311ab64890Smrg    if (state->names[state->idx]) {
1321ab64890Smrg	state->names[state->idx][repl->nameLength] = '\0';
1331ab64890Smrg	_XUpdateAtomCache(dpy, state->names[state->idx],
1341ab64890Smrg			  state->atoms[state->idx], 0, -1, 0);
1351ab64890Smrg    } else {
1361ab64890Smrg	state->status = 0;
1371ab64890Smrg    }
1381ab64890Smrg    return True;
1391ab64890Smrg}
1401ab64890Smrg
1411ab64890SmrgStatus
1421ab64890SmrgXGetAtomNames (
1431ab64890Smrg    Display *dpy,
1441ab64890Smrg    Atom *atoms,
1451ab64890Smrg    int count,
1461ab64890Smrg    char **names_return)
1471ab64890Smrg{
1481ab64890Smrg    _XAsyncHandler async;
1491ab64890Smrg    _XGetAtomNameState async_state;
1501ab64890Smrg    xGetAtomNameReply rep;
1511ab64890Smrg    int i;
1521ab64890Smrg    int missed = -1;
1531ab64890Smrg
1541ab64890Smrg    LockDisplay(dpy);
1551ab64890Smrg    async_state.start_seq = dpy->request + 1;
1561ab64890Smrg    async_state.atoms = atoms;
1571ab64890Smrg    async_state.names = names_return;
1581ab64890Smrg    async_state.idx = 0;
1591ab64890Smrg    async_state.count = count - 1;
1601ab64890Smrg    async_state.status = 1;
1611ab64890Smrg    async.next = dpy->async_handlers;
1621ab64890Smrg    async.handler = _XGetAtomNameHandler;
1631ab64890Smrg    async.data = (XPointer)&async_state;
1641ab64890Smrg    dpy->async_handlers = &async;
1651ab64890Smrg    for (i = 0; i < count; i++) {
1661ab64890Smrg	if (!(names_return[i] = _XGetAtomName(dpy, atoms[i]))) {
1671ab64890Smrg	    missed = i;
1681ab64890Smrg	    async_state.stop_seq = dpy->request;
1691ab64890Smrg	}
1701ab64890Smrg    }
1711ab64890Smrg    if (missed >= 0) {
1721ab64890Smrg	if (_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
173eb411b4bSmrg	    if ((names_return[missed] = Xmalloc(rep.nameLength + 1))) {
1741ab64890Smrg		_XReadPad(dpy, names_return[missed], (long)rep.nameLength);
1751ab64890Smrg		names_return[missed][rep.nameLength] = '\0';
1761ab64890Smrg		_XUpdateAtomCache(dpy, names_return[missed], atoms[missed],
1771ab64890Smrg				  0, -1, 0);
1781ab64890Smrg	    } else {
179eb411b4bSmrg		_XEatDataWords(dpy, rep.length);
1801ab64890Smrg		async_state.status = 0;
1811ab64890Smrg	    }
1821ab64890Smrg	}
1831ab64890Smrg    }
1841ab64890Smrg    DeqAsyncHandler(dpy, &async);
1851ab64890Smrg    UnlockDisplay(dpy);
1861ab64890Smrg    if (missed >= 0)
1871ab64890Smrg	SyncHandle();
1881ab64890Smrg    return async_state.status;
1891ab64890Smrg}
190