#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdint.h>
#include <X11/Xlibint.h>
#include <X11/extensions/XI2.h>
#include <X11/extensions/XI2proto.h>
#include <X11/extensions/XInput2.h>
#include <X11/extensions/extutil.h>
#include "XIint.h"
Atom*
XIListProperties(Display* dpy, int deviceid, int *num_props_return)
{
xXIListPropertiesReq *req;
xXIListPropertiesReply rep;
XExtDisplayInfo *info = XInput_find_display(dpy);
Atom *props = NULL;
LockDisplay(dpy);
*num_props_return = 0;
if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1)
goto cleanup;
GetReq(XIListProperties, req);
req->reqType = info->codes->major_opcode;
req->ReqType = X_XIListProperties;
req->deviceid = deviceid;
if (!_XReply(dpy, (xReply*)&rep, 0, xFalse))
goto cleanup;
if (rep.num_properties) {
props = (Atom*)Xmalloc(rep.num_properties * sizeof(Atom));
if (!props)
{
_XEatData(dpy, rep.num_properties << 2);
goto cleanup;
}
_XRead32(dpy, (long*)props, rep.num_properties << 2);
}
*num_props_return = rep.num_properties;
cleanup:
UnlockDisplay(dpy);
SyncHandle();
return props;
}
void
XIDeleteProperty(Display* dpy, int deviceid, Atom property)
{
xXIDeletePropertyReq *req;
XExtDisplayInfo *info = XInput_find_display(dpy);
LockDisplay(dpy);
if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1)
return;
GetReq(XIDeleteProperty, req);
req->reqType = info->codes->major_opcode;
req->ReqType = X_XIDeleteProperty;
req->deviceid = deviceid;
req->property = property;
UnlockDisplay(dpy);
SyncHandle();
}
void
XIChangeProperty(Display* dpy, int deviceid, Atom property, Atom type,
int format, int mode, unsigned char *data, int num_items)
{
xXIChangePropertyReq *req;
int len;
XExtDisplayInfo *info = XInput_find_display(dpy);
LockDisplay(dpy);
if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1)
return;
GetReq(XIChangeProperty, req);
req->reqType = info->codes->major_opcode;
req->ReqType = X_XIChangeProperty;
req->deviceid = deviceid;
req->property = property;
req->type = type;
req->mode = mode;
if (num_items < 0) {
req->num_items = 0;
req->format = 0;
} else {
req->num_items = num_items;
req->format = format;
}
switch (req->format) {
case 8:
len = (num_items + 3)/4;
SetReqLen(req, len, len);
len = num_items;
break;
case 16:
len = (num_items + 1)/2;
SetReqLen(req, len, len);
len = num_items * 2;
break;
case 32:
len = num_items;
SetReqLen(req, len, len);
len = num_items * 4;
break;
default:
;
len = 0;
}
Data(dpy, (const char*)data, len);
UnlockDisplay(dpy);
SyncHandle();
}
Status
XIGetProperty(Display* dpy, int deviceid, Atom property, long offset,
long length, Bool delete_property, Atom type,
Atom *type_return, int *format_return,
unsigned long *num_items_return,unsigned long *bytes_after_return,
unsigned char **data)
{
xXIGetPropertyReq *req;
xXIGetPropertyReply rep;
long nbytes, rbytes;
XExtDisplayInfo *info = XInput_find_display(dpy);
LockDisplay(dpy);
if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1)
return 1;
GetReq(XIGetProperty, req);
req->reqType = info->codes->major_opcode;
req->ReqType = X_XIGetProperty;
req->deviceid = deviceid;
req->property = property;
req->type = type;
req->offset = offset;
req->len = length;
req->delete = delete_property;
if (!_XReply (dpy, (xReply *) &rep, 0, xFalse))
{
UnlockDisplay (dpy);
SyncHandle ();
return 1;
}
*data = NULL;
if (rep.type != None) {
if (rep.format != 8 && rep.format != 16 && rep.format != 32) {
nbytes = rep.length << 2;
_XEatData(dpy, nbytes);
UnlockDisplay(dpy);
SyncHandle();
return(BadImplementation);
}
nbytes = rep.num_items * rep.format/8;
rbytes = nbytes + 1;
*data = Xmalloc(rbytes);
if (!(*data)) {
_XEatData(dpy, nbytes);
UnlockDisplay(dpy);
SyncHandle();
return(BadAlloc);
}
_XReadPad (dpy, (char *)*data, nbytes);
(*data)[rbytes - 1] = '\0';
}
*type_return = rep.type;
*format_return = rep.format;
*num_items_return = rep.num_items;
*bytes_after_return = rep.bytes_after;
UnlockDisplay (dpy);
SyncHandle();
return Success;
}