GetProp.c revision 61b2299d
1/* $Xorg: GetProp.c,v 1.5 2001/02/09 02:03:33 xorgcvs Exp $ */
2/*
3
4Copyright 1986, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26*/
27/* $XFree86: xc/lib/X11/GetProp.c,v 1.5 2001/10/28 03:32:30 tsi Exp $ */
28
29#define NEED_REPLIES
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include "Xlibint.h"
34
35int
36XGetWindowProperty(
37    register Display *dpy,
38    Window w,
39    Atom property,
40    long offset,
41    long length,
42    Bool delete,
43    Atom req_type,
44    Atom *actual_type,		/* RETURN */
45    int *actual_format,  	/* RETURN  8, 16, or 32 */
46    unsigned long *nitems, 	/* RETURN  # of 8-, 16-, or 32-bit entities */
47    unsigned long *bytesafter,	/* RETURN */
48    unsigned char **prop)	/* RETURN */
49{
50    xGetPropertyReply reply;
51    register xGetPropertyReq *req;
52    xError error;
53
54    LockDisplay(dpy);
55    GetReq (GetProperty, req);
56    req->window = w;
57    req->property = property;
58    req->type = req_type;
59    req->delete = delete;
60    req->longOffset = offset;
61    req->longLength = length;
62    error.sequenceNumber = dpy->request;
63
64    if (!_XReply (dpy, (xReply *) &reply, 0, xFalse)) {
65	UnlockDisplay(dpy);
66	SyncHandle();
67	return (1);	/* not Success */
68	}
69
70    *prop = (unsigned char *) NULL;
71    if (reply.propertyType != None) {
72	long nbytes, netbytes;
73	switch (reply.format) {
74      /*
75       * One extra byte is malloced than is needed to contain the property
76       * data, but this last byte is null terminated and convenient for
77       * returning string properties, so the client doesn't then have to
78       * recopy the string to make it null terminated.
79       */
80	  case 8:
81	    nbytes = netbytes = reply.nItems;
82	    if (nbytes + 1 > 0 &&
83		(*prop = (unsigned char *) Xmalloc ((unsigned)nbytes + 1)))
84		_XReadPad (dpy, (char *) *prop, netbytes);
85	    break;
86
87	  case 16:
88	    nbytes = reply.nItems * sizeof (short);
89	    netbytes = reply.nItems << 1;
90	    if (nbytes + 1 > 0 &&
91		(*prop = (unsigned char *) Xmalloc ((unsigned)nbytes + 1)))
92		_XRead16Pad (dpy, (short *) *prop, netbytes);
93	    break;
94
95	  case 32:
96	    nbytes = reply.nItems * sizeof (long);
97	    netbytes = reply.nItems << 2;
98	    if (nbytes + 1 > 0 &&
99		(*prop = (unsigned char *) Xmalloc ((unsigned)nbytes + 1)))
100		_XRead32 (dpy, (long *) *prop, netbytes);
101	    break;
102
103	  default:
104	    /*
105	     * This part of the code should never be reached.  If it is,
106	     * the server sent back a property with an invalid format.
107	     * This is a BadImplementation error.
108	     */
109	    {
110		/* sequence number stored above */
111		error.type = X_Error;
112		error.majorCode = X_GetProperty;
113		error.minorCode = 0;
114		error.errorCode = BadImplementation;
115		_XError(dpy, &error);
116	    }
117	    nbytes = netbytes = 0L;
118	    break;
119	}
120	if (! *prop) {
121	    _XEatData(dpy, (unsigned long) netbytes);
122	    UnlockDisplay(dpy);
123	    SyncHandle();
124	    return(BadAlloc);	/* not Success */
125	}
126	(*prop)[nbytes] = '\0';
127    }
128    *actual_type = reply.propertyType;
129    *actual_format = reply.format;
130    *nitems = reply.nItems;
131    *bytesafter = reply.bytesAfter;
132    UnlockDisplay(dpy);
133    SyncHandle();
134    return(Success);
135}
136
137