XGetDProp.c revision 21e67964
121e67964Smrg/************************************************************
221e67964Smrg
321e67964SmrgCopyright 2008 Peter Hutterer
421e67964Smrg
521e67964SmrgPermission to use, copy, modify, distribute, and sell this software and its
621e67964Smrgdocumentation for any purpose is hereby granted without fee, provided that
721e67964Smrgthe above copyright notice appear in all copies and that both that
821e67964Smrgcopyright notice and this permission notice appear in supporting
921e67964Smrgdocumentation.
1021e67964Smrg
1121e67964SmrgThe above copyright notice and this permission notice shall be included in
1221e67964Smrgall copies or substantial portions of the Software.
1321e67964Smrg
1421e67964SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1521e67964SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1621e67964SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1721e67964SmrgAUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1821e67964SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1921e67964SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2021e67964Smrg
2121e67964SmrgExcept as contained in this notice, the name of the author shall not be
2221e67964Smrgused in advertising or otherwise to promote the sale, use or other dealings
2321e67964Smrgin this Software without prior written authorization from the author.
2421e67964Smrg
2521e67964Smrg*/
2621e67964Smrg
2721e67964Smrg/***********************************************************************
2821e67964Smrg * XGetDeviceProperties - get an input device's properties.
2921e67964Smrg */
3021e67964Smrg
3121e67964Smrg#ifdef HAVE_CONFIG_H
3221e67964Smrg#include <config.h>
3321e67964Smrg#endif
3421e67964Smrg
3521e67964Smrg#include <X11/Xlibint.h>
3621e67964Smrg#include <X11/extensions/XI.h>
3721e67964Smrg#include <X11/extensions/XIproto.h>
3821e67964Smrg#include <X11/extensions/XInput.h>
3921e67964Smrg#include <X11/extensions/extutil.h>
4021e67964Smrg#include "XIint.h"
4121e67964Smrg
4221e67964Smrgint
4321e67964SmrgXGetDeviceProperty(Display* dpy, XDevice* dev,
4421e67964Smrg			 Atom property, long offset, long length, Bool delete,
4521e67964Smrg                         Atom req_type, Atom *actual_type, int *actual_format,
4621e67964Smrg                         unsigned long *nitems, unsigned long *bytes_after,
4721e67964Smrg                         unsigned char **prop)
4821e67964Smrg{
4921e67964Smrg    xGetDevicePropertyReq   *req;
5021e67964Smrg    xGetDevicePropertyReply rep;
5121e67964Smrg    long                    nbytes, rbytes;
5221e67964Smrg
5321e67964Smrg    XExtDisplayInfo *info = XInput_find_display(dpy);
5421e67964Smrg
5521e67964Smrg    LockDisplay(dpy);
5621e67964Smrg    if (_XiCheckExtInit(dpy, XInput_Initial_Release, info) == -1)
5721e67964Smrg	return 1;
5821e67964Smrg
5921e67964Smrg    GetReq(GetDeviceProperty, req);
6021e67964Smrg    req->reqType    = info->codes->major_opcode;
6121e67964Smrg    req->ReqType    = X_GetDeviceProperty;
6221e67964Smrg    req->deviceid   = dev->device_id;
6321e67964Smrg    req->property   = property;
6421e67964Smrg    req->type       = req_type;
6521e67964Smrg    req->longOffset = offset;
6621e67964Smrg    req->longLength = length;
6721e67964Smrg    req->delete     = delete;
6821e67964Smrg
6921e67964Smrg    if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
7021e67964Smrg    {
7121e67964Smrg	UnlockDisplay (dpy);
7221e67964Smrg	SyncHandle ();
7321e67964Smrg	return 1;
7421e67964Smrg    }
7521e67964Smrg
7621e67964Smrg    *prop = (unsigned char *) NULL;
7721e67964Smrg
7821e67964Smrg    if (rep.propertyType != None) {
7921e67964Smrg	/*
8021e67964Smrg	 * One extra byte is malloced than is needed to contain the property
8121e67964Smrg	 * data, but this last byte is null terminated and convenient for
8221e67964Smrg	 * returning string properties, so the client doesn't then have to
8321e67964Smrg	 * recopy the string to make it null terminated.
8421e67964Smrg	 */
8521e67964Smrg	switch (rep.format) {
8621e67964Smrg	case 8:
8721e67964Smrg	    nbytes = rep.nItems;
8821e67964Smrg	    rbytes = rep.nItems + 1;
8921e67964Smrg	    if (rbytes > 0 &&
9021e67964Smrg		(*prop = (unsigned char *) Xmalloc ((unsigned)rbytes)))
9121e67964Smrg		_XReadPad (dpy, (char *) *prop, nbytes);
9221e67964Smrg	    break;
9321e67964Smrg
9421e67964Smrg	case 16:
9521e67964Smrg	    nbytes = rep.nItems << 1;
9621e67964Smrg	    rbytes = rep.nItems * sizeof (short) + 1;
9721e67964Smrg	    if (rbytes > 0 &&
9821e67964Smrg		(*prop = (unsigned char *) Xmalloc ((unsigned)rbytes)))
9921e67964Smrg		_XRead16Pad (dpy, (short *) *prop, nbytes);
10021e67964Smrg	    break;
10121e67964Smrg
10221e67964Smrg	case 32:
10321e67964Smrg	    nbytes = rep.nItems << 2;
10421e67964Smrg	    rbytes = rep.nItems * sizeof (long) + 1;
10521e67964Smrg	    if (rbytes > 0 &&
10621e67964Smrg		(*prop = (unsigned char *) Xmalloc ((unsigned)rbytes)))
10721e67964Smrg		_XRead32 (dpy, (long *) *prop, nbytes);
10821e67964Smrg	    break;
10921e67964Smrg
11021e67964Smrg	default:
11121e67964Smrg	    /*
11221e67964Smrg	     * This part of the code should never be reached.  If it is,
11321e67964Smrg	     * the server sent back a property with an invalid format.
11421e67964Smrg	     */
11521e67964Smrg	    nbytes = rep.length << 2;
11621e67964Smrg	    _XEatData(dpy, (unsigned long) nbytes);
11721e67964Smrg	    UnlockDisplay(dpy);
11821e67964Smrg	    SyncHandle();
11921e67964Smrg	    return(BadImplementation);
12021e67964Smrg	}
12121e67964Smrg	if (! *prop) {
12221e67964Smrg	    _XEatData(dpy, (unsigned long) nbytes);
12321e67964Smrg	    UnlockDisplay(dpy);
12421e67964Smrg	    SyncHandle();
12521e67964Smrg	    return(BadAlloc);
12621e67964Smrg	}
12721e67964Smrg	(*prop)[rbytes - 1] = '\0';
12821e67964Smrg    }
12921e67964Smrg
13021e67964Smrg    *actual_type = rep.propertyType;
13121e67964Smrg    *actual_format = rep.format;
13221e67964Smrg    *nitems = rep.nItems;
13321e67964Smrg    *bytes_after = rep.bytesAfter;
13421e67964Smrg    UnlockDisplay (dpy);
13521e67964Smrg    SyncHandle ();
13621e67964Smrg
13721e67964Smrg    return Success;
13821e67964Smrg}
13921e67964Smrg
140