XIHierarchy.c revision b789ec8a
1/************************************************************
2
3Copyright 2007 Peter Hutterer <peter@cs.unisa.edu.au>
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/***********************************************************************
28 *
29 * XIChangeHierarchy - change the device hierarchy, i.e. which slave
30 * device is attached to which master, etc.
31 */
32
33#include <stdint.h>
34#include <X11/extensions/XI2proto.h>
35#include <X11/Xlibint.h>
36#include <X11/extensions/XInput2.h>
37#include <X11/extensions/extutil.h>
38#include "XIint.h"
39
40int
41XIChangeHierarchy(Display* dpy,
42                        XIAnyHierarchyChangeInfo* changes,
43                        int num_changes)
44{
45    XIAnyHierarchyChangeInfo* any;
46    xXIChangeHierarchyReq *req;
47    XExtDisplayInfo *info = XInput_find_display(dpy);
48    char *data = NULL, *dptr;
49    int dlen = 0, i;
50
51    LockDisplay(dpy);
52    if (_XiCheckExtInit(dpy, XInput_2_0, info) == -1)
53	return (NoSuchExtension);
54
55    if (num_changes <= 0)
56        return Success;
57
58    GetReq(XIChangeHierarchy, req);
59    req->reqType = info->codes->major_opcode;
60    req->ReqType = X_XIChangeHierarchy;
61    req->num_changes = num_changes;
62
63    /* alloc required memory */
64    for (i = 0, any = changes; i < num_changes; i++, any++)
65    {
66        switch(any->type)
67        {
68            case XIAddMaster:
69                {
70                    int slen = (strlen(any->add.name));
71                    dlen += sizeof(xXIAddMasterInfo) +
72                        slen + (4 - (slen % 4));
73                }
74                break;
75            case XIRemoveMaster:
76                dlen += sizeof(xXIRemoveMasterInfo);
77                break;
78            case XIAttachSlave:
79                dlen += sizeof(xXIAttachSlaveInfo);
80                break;
81            case XIDetachSlave:
82                dlen += sizeof(xXIDetachSlaveInfo);
83                break;
84            default:
85                return BadValue;
86        }
87    }
88
89    req->length += dlen / 4; /* dlen is 4-byte aligned */
90    data = Xmalloc(dlen);
91    if (!data)
92        return BadAlloc;
93
94    dptr = data;
95    for (i = 0, any = changes; i < num_changes; i++, any++)
96    {
97        switch(any->type)
98        {
99                case XIAddMaster:
100                {
101                    XIAddMasterInfo* C = &any->add;
102                    xXIAddMasterInfo* c = (xXIAddMasterInfo*)dptr;
103                    c->type = C->type;
104                    c->send_core = C->send_core;
105                    c->enable = C->enable;
106                    c->name_len = strlen(C->name);
107                    c->length = (sizeof(xXIAddMasterInfo) + c->name_len + 3)/4;
108                    strncpy((char*)&c[1], C->name, c->name_len);
109                    dptr += c->length;
110                }
111                break;
112            case XIRemoveMaster:
113                {
114                    XIRemoveMasterInfo* R = &any->remove;
115                    xXIRemoveMasterInfo* r = (xXIRemoveMasterInfo*)dptr;
116                    r->type = R->type;
117                    r->return_mode = R->return_mode;
118                    r->deviceid = R->deviceid;
119                    r->length = sizeof(xXIRemoveMasterInfo)/4;
120                    if (r->return_mode == XIAttachToMaster)
121                    {
122                        r->return_pointer = R->return_pointer;
123                        r->return_keyboard = R->return_keyboard;
124                    }
125                    dptr += sizeof(xXIRemoveMasterInfo);
126                }
127                break;
128            case XIAttachSlave:
129                {
130                    XIAttachSlaveInfo* C = &any->attach;
131                    xXIAttachSlaveInfo* c = (xXIAttachSlaveInfo*)dptr;
132
133                    c->type = C->type;
134                    c->deviceid = C->deviceid;
135                    c->length = sizeof(xXIAttachSlaveInfo)/4;
136                    c->new_master = C->new_master;
137
138                    dptr += sizeof(xXIAttachSlaveInfo);
139                }
140                break;
141            case XIDetachSlave:
142                {
143                    XIDetachSlaveInfo *D = &any->detach;
144                    xXIDetachSlaveInfo *d = (xXIDetachSlaveInfo*)dptr;
145
146                    d->type = D->type;
147                    d->deviceid = D->deviceid;
148                    d->length = sizeof(xXIDetachSlaveInfo)/4;
149                    dptr += sizeof(xXIDetachSlaveInfo);
150                }
151        }
152    }
153
154    Data(dpy, data, dlen);
155    Xfree(data);
156    UnlockDisplay(dpy);
157    SyncHandle();
158    return Success;
159}
160