XIQueryDevice.c revision 44584a44
1c27c18e8Smrg/*
2c27c18e8Smrg * Copyright © 2009 Red Hat, Inc.
3c27c18e8Smrg *
4c27c18e8Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5c27c18e8Smrg * copy of this software and associated documentation files (the "Software"),
6c27c18e8Smrg * to deal in the Software without restriction, including without limitation
7c27c18e8Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c27c18e8Smrg * and/or sell copies of the Software, and to permit persons to whom the
9c27c18e8Smrg * Software is furnished to do so, subject to the following conditions:
10c27c18e8Smrg *
11c27c18e8Smrg * The above copyright notice and this permission notice (including the next
12c27c18e8Smrg * paragraph) shall be included in all copies or substantial portions of the
13c27c18e8Smrg * Software.
14c27c18e8Smrg *
15c27c18e8Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16c27c18e8Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17c27c18e8Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18c27c18e8Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19c27c18e8Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20c27c18e8Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21c27c18e8Smrg * DEALINGS IN THE SOFTWARE.
22c27c18e8Smrg *
23c27c18e8Smrg */
24c27c18e8Smrg
25f1ee322dSmrg#if HAVE_CONFIG_H
26f1ee322dSmrg#include <config.h>
27f1ee322dSmrg#endif
28f1ee322dSmrg
29c27c18e8Smrg#include <stdint.h>
30c27c18e8Smrg#include <X11/Xlibint.h>
31c27c18e8Smrg#include <X11/extensions/XI2proto.h>
32c27c18e8Smrg#include <X11/extensions/XInput2.h>
33c27c18e8Smrg#include <X11/extensions/extutil.h>
34c27c18e8Smrg#include "XIint.h"
35c27c18e8Smrg
36f1ee322dSmrgextern int copy_classes(XIDeviceInfo* to, xXIAnyInfo* from, int *nclasses);
37c27c18e8Smrgextern int size_classes(xXIAnyInfo* from, int nclasses);
38c27c18e8Smrg
39c27c18e8SmrgXIDeviceInfo*
40c27c18e8SmrgXIQueryDevice(Display *dpy, int deviceid, int *ndevices_return)
41c27c18e8Smrg{
42c27c18e8Smrg    XIDeviceInfo        *info = NULL;
43c27c18e8Smrg    xXIQueryDeviceReq   *req;
44c27c18e8Smrg    xXIQueryDeviceReply reply;
45c27c18e8Smrg    char                *ptr;
46c27c18e8Smrg    int                 i;
47c27c18e8Smrg    char                *buf;
48c27c18e8Smrg
49c27c18e8Smrg    XExtDisplayInfo *extinfo = XInput_find_display(dpy);
50c27c18e8Smrg
51c27c18e8Smrg    LockDisplay(dpy);
52b789ec8aSmrg    if (_XiCheckExtInit(dpy, XInput_2_0, extinfo) == -1)
5344584a44Smrg        goto error_unlocked;
54c27c18e8Smrg
55c27c18e8Smrg    GetReq(XIQueryDevice, req);
56c27c18e8Smrg    req->reqType  = extinfo->codes->major_opcode;
57c27c18e8Smrg    req->ReqType  = X_XIQueryDevice;
58c27c18e8Smrg    req->deviceid = deviceid;
59c27c18e8Smrg
60c27c18e8Smrg    if (!_XReply(dpy, (xReply*) &reply, 0, xFalse))
61c27c18e8Smrg        goto error;
62c27c18e8Smrg
63c27c18e8Smrg    *ndevices_return = reply.num_devices;
64c27c18e8Smrg    info = Xmalloc((reply.num_devices + 1) * sizeof(XIDeviceInfo));
65c27c18e8Smrg    if (!info)
66c27c18e8Smrg        goto error;
67c27c18e8Smrg
68c27c18e8Smrg    buf = Xmalloc(reply.length * 4);
69c27c18e8Smrg    _XRead(dpy, buf, reply.length * 4);
70c27c18e8Smrg    ptr = buf;
71c27c18e8Smrg
72c27c18e8Smrg    /* info is a null-terminated array */
73c27c18e8Smrg    info[reply.num_devices].name = NULL;
74c27c18e8Smrg
75c27c18e8Smrg    for (i = 0; i < reply.num_devices; i++)
76c27c18e8Smrg    {
77f1ee322dSmrg        int             nclasses;
78f1ee322dSmrg        size_t          sz;
79c27c18e8Smrg        XIDeviceInfo    *lib = &info[i];
80c27c18e8Smrg        xXIDeviceInfo   *wire = (xXIDeviceInfo*)ptr;
81c27c18e8Smrg
82c27c18e8Smrg        lib->deviceid    = wire->deviceid;
83c27c18e8Smrg        lib->use         = wire->use;
84c27c18e8Smrg        lib->attachment  = wire->attachment;
85c27c18e8Smrg        lib->enabled     = wire->enabled;
86f1ee322dSmrg        nclasses         = wire->num_classes;
87c27c18e8Smrg
88c27c18e8Smrg        ptr += sizeof(xXIDeviceInfo);
89c27c18e8Smrg
90c27c18e8Smrg        lib->name = Xcalloc(wire->name_len + 1, 1);
91c27c18e8Smrg        strncpy(lib->name, ptr, wire->name_len);
92c27c18e8Smrg        ptr += ((wire->name_len + 3)/4) * 4;
93c27c18e8Smrg
94f1ee322dSmrg        sz = size_classes((xXIAnyInfo*)ptr, nclasses);
95f1ee322dSmrg        lib->classes = Xmalloc(sz);
96f1ee322dSmrg        ptr += copy_classes(lib, (xXIAnyInfo*)ptr, &nclasses);
97f1ee322dSmrg        /* We skip over unused classes */
98f1ee322dSmrg        lib->num_classes = nclasses;
99c27c18e8Smrg    }
100c27c18e8Smrg
101c27c18e8Smrg    Xfree(buf);
102c27c18e8Smrg    UnlockDisplay(dpy);
103c27c18e8Smrg    SyncHandle();
104c27c18e8Smrg    return info;
105c27c18e8Smrg
106c27c18e8Smrgerror:
107c27c18e8Smrg    UnlockDisplay(dpy);
10844584a44Smrgerror_unlocked:
109c27c18e8Smrg    SyncHandle();
110c27c18e8Smrg    *ndevices_return = -1;
111c27c18e8Smrg    return NULL;
112c27c18e8Smrg}
113c27c18e8Smrg
114c27c18e8Smrgvoid
115c27c18e8SmrgXIFreeDeviceInfo(XIDeviceInfo* info)
116c27c18e8Smrg{
117c27c18e8Smrg    XIDeviceInfo *ptr = info;
118c27c18e8Smrg    while(ptr->name)
119c27c18e8Smrg    {
120c27c18e8Smrg        Xfree(ptr->classes);
121c27c18e8Smrg        Xfree(ptr->name);
122c27c18e8Smrg        ptr++;
123c27c18e8Smrg    }
124c27c18e8Smrg    Xfree(info);
125c27c18e8Smrg}
126