GetProp.c revision 57f47464
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
321ab64890Smrgint
331ab64890SmrgXGetWindowProperty(
341ab64890Smrg    register Display *dpy,
351ab64890Smrg    Window w,
361ab64890Smrg    Atom property,
371ab64890Smrg    long offset,
381ab64890Smrg    long length,
391ab64890Smrg    Bool delete,
401ab64890Smrg    Atom req_type,
411ab64890Smrg    Atom *actual_type,		/* RETURN */
421ab64890Smrg    int *actual_format,  	/* RETURN  8, 16, or 32 */
431ab64890Smrg    unsigned long *nitems, 	/* RETURN  # of 8-, 16-, or 32-bit entities */
441ab64890Smrg    unsigned long *bytesafter,	/* RETURN */
451ab64890Smrg    unsigned char **prop)	/* RETURN */
461ab64890Smrg{
471ab64890Smrg    xGetPropertyReply reply;
481ab64890Smrg    register xGetPropertyReq *req;
4957f47464Smrg    xError error = {0};
501ab64890Smrg
511ab64890Smrg    LockDisplay(dpy);
521ab64890Smrg    GetReq (GetProperty, req);
531ab64890Smrg    req->window = w;
541ab64890Smrg    req->property = property;
551ab64890Smrg    req->type = req_type;
561ab64890Smrg    req->delete = delete;
571ab64890Smrg    req->longOffset = offset;
581ab64890Smrg    req->longLength = length;
591ab64890Smrg    error.sequenceNumber = dpy->request;
6061b2299dSmrg
611ab64890Smrg    if (!_XReply (dpy, (xReply *) &reply, 0, xFalse)) {
621ab64890Smrg	UnlockDisplay(dpy);
631ab64890Smrg	SyncHandle();
641ab64890Smrg	return (1);	/* not Success */
6561b2299dSmrg	}
661ab64890Smrg
671ab64890Smrg    *prop = (unsigned char *) NULL;
681ab64890Smrg    if (reply.propertyType != None) {
691ab64890Smrg	long nbytes, netbytes;
701ab64890Smrg	switch (reply.format) {
7161b2299dSmrg      /*
721ab64890Smrg       * One extra byte is malloced than is needed to contain the property
7361b2299dSmrg       * data, but this last byte is null terminated and convenient for
7461b2299dSmrg       * returning string properties, so the client doesn't then have to
7561b2299dSmrg       * recopy the string to make it null terminated.
761ab64890Smrg       */
771ab64890Smrg	  case 8:
781ab64890Smrg	    nbytes = netbytes = reply.nItems;
791ab64890Smrg	    if (nbytes + 1 > 0 &&
801ab64890Smrg		(*prop = (unsigned char *) Xmalloc ((unsigned)nbytes + 1)))
811ab64890Smrg		_XReadPad (dpy, (char *) *prop, netbytes);
821ab64890Smrg	    break;
831ab64890Smrg
841ab64890Smrg	  case 16:
851ab64890Smrg	    nbytes = reply.nItems * sizeof (short);
861ab64890Smrg	    netbytes = reply.nItems << 1;
871ab64890Smrg	    if (nbytes + 1 > 0 &&
881ab64890Smrg		(*prop = (unsigned char *) Xmalloc ((unsigned)nbytes + 1)))
891ab64890Smrg		_XRead16Pad (dpy, (short *) *prop, netbytes);
901ab64890Smrg	    break;
911ab64890Smrg
921ab64890Smrg	  case 32:
931ab64890Smrg	    nbytes = reply.nItems * sizeof (long);
941ab64890Smrg	    netbytes = reply.nItems << 2;
951ab64890Smrg	    if (nbytes + 1 > 0 &&
961ab64890Smrg		(*prop = (unsigned char *) Xmalloc ((unsigned)nbytes + 1)))
971ab64890Smrg		_XRead32 (dpy, (long *) *prop, netbytes);
981ab64890Smrg	    break;
991ab64890Smrg
1001ab64890Smrg	  default:
1011ab64890Smrg	    /*
1021ab64890Smrg	     * This part of the code should never be reached.  If it is,
1031ab64890Smrg	     * the server sent back a property with an invalid format.
10461b2299dSmrg	     * This is a BadImplementation error.
1051ab64890Smrg	     */
1061ab64890Smrg	    {
1071ab64890Smrg		/* sequence number stored above */
1081ab64890Smrg		error.type = X_Error;
1091ab64890Smrg		error.majorCode = X_GetProperty;
1101ab64890Smrg		error.minorCode = 0;
1111ab64890Smrg		error.errorCode = BadImplementation;
1121ab64890Smrg		_XError(dpy, &error);
1131ab64890Smrg	    }
1141ab64890Smrg	    nbytes = netbytes = 0L;
1151ab64890Smrg	    break;
1161ab64890Smrg	}
1171ab64890Smrg	if (! *prop) {
1181ab64890Smrg	    _XEatData(dpy, (unsigned long) netbytes);
1191ab64890Smrg	    UnlockDisplay(dpy);
1201ab64890Smrg	    SyncHandle();
1211ab64890Smrg	    return(BadAlloc);	/* not Success */
1221ab64890Smrg	}
1231ab64890Smrg	(*prop)[nbytes] = '\0';
1241ab64890Smrg    }
1251ab64890Smrg    *actual_type = reply.propertyType;
1261ab64890Smrg    *actual_format = reply.format;
1271ab64890Smrg    *nitems = reply.nItems;
1281ab64890Smrg    *bytesafter = reply.bytesAfter;
1291ab64890Smrg    UnlockDisplay(dpy);
1301ab64890Smrg    SyncHandle();
1311ab64890Smrg    return(Success);
1321ab64890Smrg}
1331ab64890Smrg
134