summaryrefslogtreecommitdiff
path: root/Xi/chdevhier.c
blob: 631857d58fb6b8da4e15d72dab7e90534e429fb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright 2007-2008 Peter Hutterer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Author: Peter Hutterer, University of South Australia, NICTA
 */

/***********************************************************************
 *
 * Request change in the device hierarchy.
 *
 */


#define	 NEED_EVENTS
#define	 NEED_REPLIES
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <X11/X.h>	/* for inputstr.h    */
#include <X11/Xproto.h>	/* Request macro     */
#include "inputstr.h"	/* DeviceIntPtr      */
#include "windowstr.h"	/* window structure  */
#include "scrnintstr.h"	/* screen structure  */
#include <X11/extensions/XI.h>
#include <X11/extensions/XIproto.h>
#include <X11/extensions/geproto.h>
#include "extnsionst.h"
#include "exevents.h"
#include "exglobals.h"
#include "geext.h"
#include "xace.h"

#include "chdevhier.h"


/***********************************************************************
 *
 * This procedure allows a client to change the device hierarchy through
 * adding new master devices, removing them, etc.
 *
 */

int SProcXChangeDeviceHierarchy(ClientPtr client)
{
    char n;

    REQUEST(xChangeDeviceHierarchyReq);
    swaps(&stuff->length, n);
    return (ProcXChangeDeviceHierarchy(client));
}

#define SWAPIF(cmd) if (client->swapped) { cmd; }

int
ProcXChangeDeviceHierarchy(ClientPtr client)
{
    DeviceIntPtr ptr, keybd;
    DeviceIntRec dummyDev;
    xAnyHierarchyChangeInfo *any;
    int required_len = sizeof(xChangeDeviceHierarchyReq);
    char n;
    int rc = Success;
    int nchanges = 0;
    deviceHierarchyChangedEvent ev;

    REQUEST(xChangeDeviceHierarchyReq);
    REQUEST_AT_LEAST_SIZE(xChangeDeviceHierarchyReq);

    any = (xAnyHierarchyChangeInfo*)&stuff[1];
    while(stuff->num_changes--)
    {
        SWAPIF(swapl(&any->type, n));
        SWAPIF(swaps(&any->length, n));

        required_len += any->length;
        if ((stuff->length * 4) < required_len)
            return BadLength;

        switch(any->type)
        {
            case CH_CreateMasterDevice:
                {
                    xCreateMasterInfo* c = (xCreateMasterInfo*)any;
                    char* name;

                    SWAPIF(swaps(&c->namelen, n));
                    name = xcalloc(c->namelen + 1, sizeof(char));
                    strncpy(name, (char*)&c[1], c->namelen);


                    rc = AllocMasterDevice(client, name, &ptr, &keybd);
                    if (rc != Success)
                    {
                        xfree(name);
                        goto unwind;
                    }

                    if (!c->sendCore)
                        ptr->coreEvents = keybd->coreEvents =  FALSE;
#ifdef XKB
                    /* supplying NULL for rules simply means we re-use
                       whatever ruleset we used for the previous devices. */
                    XkbSetRulesDflts(NULL, "pc105", "us", NULL, NULL);
#endif
                    ActivateDevice(ptr);
                    ActivateDevice(keybd);

                    if (c->enable)
                    {
                        EnableDevice(ptr);
                        EnableDevice(keybd);
                    }
                    xfree(name);
                    nchanges++;
                }
                break;
            case CH_RemoveMasterDevice:
                {
                    xRemoveMasterInfo* r = (xRemoveMasterInfo*)any;

                    if (r->returnMode != AttachToMaster &&
                            r->returnMode != Floating)
                        return BadValue;

                    rc = dixLookupDevice(&ptr, r->deviceid, client,
                                         DixDestroyAccess);
                    if (rc != Success)
                        goto unwind;

                    if (!ptr->isMaster)
                    {
                        client->errorValue = r->deviceid;
                        rc = BadDevice;
                        goto unwind;
                    }

                    /* XXX: For now, don't allow removal of VCP, VCK */
                    if (ptr == inputInfo.pointer ||
                            ptr == inputInfo.keyboard)
                    {
                        rc = BadDevice;
                        goto unwind;
                    }

                    /* disable keyboards first */
                    if (IsPointerDevice(ptr))
                    {
                        rc = dixLookupDevice(&keybd,
                                             ptr->spriteInfo->paired->id,
                                             client,
                                             DixDestroyAccess);
                        if (rc != Success)
                            goto unwind;
                    }
                    else
                    {
                        keybd = ptr;
                        rc = dixLookupDevice(&ptr,
                                             keybd->spriteInfo->paired->id,
                                             client,
                                             DixDestroyAccess);
                        if (rc != Success)
                            goto unwind;
                    }


                    /* Disabling sends the devices floating, reattach them if
                     * desired. */
                    if (r->returnMode == AttachToMaster)
                    {
                        DeviceIntPtr attached,
                                     newptr,
                                     newkeybd;

                        rc = dixLookupDevice(&newptr, r->returnPointer,
                                             client, DixWriteAccess);
                        if (rc != Success)
                            goto unwind;

                        if (!newptr->isMaster)
                        {
                            client->errorValue = r->returnPointer;
                            rc = BadDevice;
                            goto unwind;
                        }

                        rc = dixLookupDevice(&newkeybd, r->returnKeyboard,
                                             client, DixWriteAccess);
                        if (rc != Success)
                            goto unwind;

                        if (!newkeybd->isMaster)
                        {
                            client->errorValue = r->returnKeyboard;
                            rc = BadDevice;
                            goto unwind;
                        }

                        for (attached = inputInfo.devices;
                                attached;
                                attached = attached->next)
                        {
                            if (!attached->isMaster) {
                                if (attached->u.master == ptr)
                                    AttachDevice(client, attached, newptr);
                                if (attached->u.master == keybd)
                                    AttachDevice(client, attached, newkeybd);
                            }
                        }
                    }

                    /* can't disable until we removed pairing */
                    keybd->spriteInfo->paired = NULL;
                    ptr->spriteInfo->paired = NULL;
                    DisableDevice(keybd);
                    DisableDevice(ptr);

                    RemoveDevice(keybd);
                    RemoveDevice(ptr);
                    nchanges++;
                }
                break;
            case CH_ChangeAttachment:
                {
                    xChangeAttachmentInfo* c = (xChangeAttachmentInfo*)any;

                    rc = dixLookupDevice(&ptr, c->deviceid, client,
                                          DixWriteAccess);
                    if (rc != Success)
                       goto unwind;

                    if (ptr->isMaster)
                    {
                        client->errorValue = c->deviceid;
                        rc = BadDevice;
                        goto unwind;
                    }

                    if (c->changeMode == Floating)
                        AttachDevice(client, ptr, NULL);
                    else
                    {
                        DeviceIntPtr newmaster;
                        rc = dixLookupDevice(&newmaster, c->newMaster,
                                             client, DixWriteAccess);
                        if (rc != Success)
                            goto unwind;
                        if (!newmaster->isMaster)
                        {
                            client->errorValue = c->newMaster;
                            rc = BadDevice;
                            goto unwind;
                        }

                        if (!((IsPointerDevice(newmaster) &&
                                    IsPointerDevice(ptr)) ||
                                (IsKeyboardDevice(newmaster) &&
                                 IsKeyboardDevice(ptr))))
                        {
                            rc = BadDevice;
                            goto unwind;
                        }
                        AttachDevice(client, ptr, newmaster);
                    }
                    nchanges++;
                }
                break;
        }

        any = (xAnyHierarchyChangeInfo*)((char*)any + any->length);
    }

unwind:

    if (nchanges > 0) /* even if an error occured, we need to send an event if
                       we changed anything in the hierarchy. */
    {
        ev.type = GenericEvent;
        ev.extension = IReqCode;
        ev.length = 0;
        ev.evtype = XI_DeviceHierarchyChangedNotify;
        ev.time = GetTimeInMillis();

        SendEventToAllWindows(&dummyDev, XI_DeviceHierarchyChangedMask,
                (xEvent*)&ev, 1);
    }
    return rc;
}