GetProp.c revision 61b2299d
11ab64890Smrg/* $Xorg: GetProp.c,v 1.5 2001/02/09 02:03:33 xorgcvs Exp $ */
21ab64890Smrg/*
31ab64890Smrg
41ab64890SmrgCopyright 1986, 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 in
131ab64890Smrgall copies or substantial portions of the Software.
141ab64890Smrg
151ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
181ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
191ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
201ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
211ab64890Smrg
221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
231ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
241ab64890Smrgin this Software without prior written authorization from The Open Group.
251ab64890Smrg
261ab64890Smrg*/
271ab64890Smrg/* $XFree86: xc/lib/X11/GetProp.c,v 1.5 2001/10/28 03:32:30 tsi Exp $ */
281ab64890Smrg
291ab64890Smrg#define NEED_REPLIES
301ab64890Smrg#ifdef HAVE_CONFIG_H
311ab64890Smrg#include <config.h>
321ab64890Smrg#endif
331ab64890Smrg#include "Xlibint.h"
341ab64890Smrg
351ab64890Smrgint
361ab64890SmrgXGetWindowProperty(
371ab64890Smrg    register Display *dpy,
381ab64890Smrg    Window w,
391ab64890Smrg    Atom property,
401ab64890Smrg    long offset,
411ab64890Smrg    long length,
421ab64890Smrg    Bool delete,
431ab64890Smrg    Atom req_type,
441ab64890Smrg    Atom *actual_type,		/* RETURN */
451ab64890Smrg    int *actual_format,  	/* RETURN  8, 16, or 32 */
461ab64890Smrg    unsigned long *nitems, 	/* RETURN  # of 8-, 16-, or 32-bit entities */
471ab64890Smrg    unsigned long *bytesafter,	/* RETURN */
481ab64890Smrg    unsigned char **prop)	/* RETURN */
491ab64890Smrg{
501ab64890Smrg    xGetPropertyReply reply;
511ab64890Smrg    register xGetPropertyReq *req;
521ab64890Smrg    xError error;
531ab64890Smrg
541ab64890Smrg    LockDisplay(dpy);
551ab64890Smrg    GetReq (GetProperty, req);
561ab64890Smrg    req->window = w;
571ab64890Smrg    req->property = property;
581ab64890Smrg    req->type = req_type;
591ab64890Smrg    req->delete = delete;
601ab64890Smrg    req->longOffset = offset;
611ab64890Smrg    req->longLength = length;
621ab64890Smrg    error.sequenceNumber = dpy->request;
6361b2299dSmrg
641ab64890Smrg    if (!_XReply (dpy, (xReply *) &reply, 0, xFalse)) {
651ab64890Smrg	UnlockDisplay(dpy);
661ab64890Smrg	SyncHandle();
671ab64890Smrg	return (1);	/* not Success */
6861b2299dSmrg	}
691ab64890Smrg
701ab64890Smrg    *prop = (unsigned char *) NULL;
711ab64890Smrg    if (reply.propertyType != None) {
721ab64890Smrg	long nbytes, netbytes;
731ab64890Smrg	switch (reply.format) {
7461b2299dSmrg      /*
751ab64890Smrg       * One extra byte is malloced than is needed to contain the property
7661b2299dSmrg       * data, but this last byte is null terminated and convenient for
7761b2299dSmrg       * returning string properties, so the client doesn't then have to
7861b2299dSmrg       * recopy the string to make it null terminated.
791ab64890Smrg       */
801ab64890Smrg	  case 8:
811ab64890Smrg	    nbytes = netbytes = reply.nItems;
821ab64890Smrg	    if (nbytes + 1 > 0 &&
831ab64890Smrg		(*prop = (unsigned char *) Xmalloc ((unsigned)nbytes + 1)))
841ab64890Smrg		_XReadPad (dpy, (char *) *prop, netbytes);
851ab64890Smrg	    break;
861ab64890Smrg
871ab64890Smrg	  case 16:
881ab64890Smrg	    nbytes = reply.nItems * sizeof (short);
891ab64890Smrg	    netbytes = reply.nItems << 1;
901ab64890Smrg	    if (nbytes + 1 > 0 &&
911ab64890Smrg		(*prop = (unsigned char *) Xmalloc ((unsigned)nbytes + 1)))
921ab64890Smrg		_XRead16Pad (dpy, (short *) *prop, netbytes);
931ab64890Smrg	    break;
941ab64890Smrg
951ab64890Smrg	  case 32:
961ab64890Smrg	    nbytes = reply.nItems * sizeof (long);
971ab64890Smrg	    netbytes = reply.nItems << 2;
981ab64890Smrg	    if (nbytes + 1 > 0 &&
991ab64890Smrg		(*prop = (unsigned char *) Xmalloc ((unsigned)nbytes + 1)))
1001ab64890Smrg		_XRead32 (dpy, (long *) *prop, netbytes);
1011ab64890Smrg	    break;
1021ab64890Smrg
1031ab64890Smrg	  default:
1041ab64890Smrg	    /*
1051ab64890Smrg	     * This part of the code should never be reached.  If it is,
1061ab64890Smrg	     * the server sent back a property with an invalid format.
10761b2299dSmrg	     * This is a BadImplementation error.
1081ab64890Smrg	     */
1091ab64890Smrg	    {
1101ab64890Smrg		/* sequence number stored above */
1111ab64890Smrg		error.type = X_Error;
1121ab64890Smrg		error.majorCode = X_GetProperty;
1131ab64890Smrg		error.minorCode = 0;
1141ab64890Smrg		error.errorCode = BadImplementation;
1151ab64890Smrg		_XError(dpy, &error);
1161ab64890Smrg	    }
1171ab64890Smrg	    nbytes = netbytes = 0L;
1181ab64890Smrg	    break;
1191ab64890Smrg	}
1201ab64890Smrg	if (! *prop) {
1211ab64890Smrg	    _XEatData(dpy, (unsigned long) netbytes);
1221ab64890Smrg	    UnlockDisplay(dpy);
1231ab64890Smrg	    SyncHandle();
1241ab64890Smrg	    return(BadAlloc);	/* not Success */
1251ab64890Smrg	}
1261ab64890Smrg	(*prop)[nbytes] = '\0';
1271ab64890Smrg    }
1281ab64890Smrg    *actual_type = reply.propertyType;
1291ab64890Smrg    *actual_format = reply.format;
1301ab64890Smrg    *nitems = reply.nItems;
1311ab64890Smrg    *bytesafter = reply.bytesAfter;
1321ab64890Smrg    UnlockDisplay(dpy);
1331ab64890Smrg    SyncHandle();
1341ab64890Smrg    return(Success);
1351ab64890Smrg}
1361ab64890Smrg
137