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