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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
/*
* Copyright 2007-2008 Peter Hutterer
* Copyright 2009 Red Hat, Inc.
*
* 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.
*
*/
#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/XI2proto.h>
#include <X11/extensions/geproto.h>
#include "extnsionst.h"
#include "exevents.h"
#include "exglobals.h"
#include "geext.h"
#include "xace.h"
#include "querydev.h" /* for GetDeviceUse */
#include "xkbsrv.h"
#include "chdevhier.h"
extern DevPrivateKey XTstDevicePrivateKey;
/**
* Send the current state of the device hierarchy to all clients.
*/
void XISendDeviceHierarchyEvent(int flags[MAXDEVICES])
{
xXIDeviceHierarchyEvent *ev;
xXIHierarchyInfo *info;
DeviceIntRec dummyDev;
DeviceIntPtr dev;
int i;
if (!flags)
return;
ev = xcalloc(1, sizeof(xXIDeviceHierarchyEvent) +
MAXDEVICES * sizeof(xXIHierarchyInfo));
ev->type = GenericEvent;
ev->extension = IReqCode;
ev->evtype = XI_HierarchyChanged;
ev->time = GetTimeInMillis();
ev->flags = 0;
ev->num_devices = inputInfo.numDevices;
info = (xXIHierarchyInfo*)&ev[1];
for (dev = inputInfo.devices; dev; dev = dev->next)
{
info->deviceid = dev->id;
info->enabled = dev->enabled;
info->use = GetDeviceUse(dev, &info->attachment);
info->flags = flags[dev->id];
ev->flags |= info->flags;
info++;
}
for (dev = inputInfo.off_devices; dev; dev = dev->next)
{
info->deviceid = dev->id;
info->enabled = dev->enabled;
info->use = GetDeviceUse(dev, &info->attachment);
info->flags = flags[dev->id];
ev->flags |= info->flags;
info++;
}
for (i = 0; i < MAXDEVICES; i++)
{
if (flags[i] & (XIMasterRemoved | XISlaveRemoved))
{
info->deviceid = i;
info->enabled = FALSE;
info->flags = flags[i];
info->use = 0;
ev->flags |= info->flags;
ev->num_devices++;
info++;
}
}
ev->length = (ev->num_devices * sizeof(xXIHierarchyInfo))/4;
dummyDev.id = XIAllDevices;
SendEventToAllWindows(&dummyDev, (XI_HierarchyChangedMask >> 8), (xEvent*)ev, 1);
}
/***********************************************************************
*
* This procedure allows a client to change the device hierarchy through
* adding new master devices, removing them, etc.
*
*/
int SProcXIChangeDeviceHierarchy(ClientPtr client)
{
char n;
REQUEST(xXIChangeDeviceHierarchyReq);
swaps(&stuff->length, n);
return (ProcXIChangeDeviceHierarchy(client));
}
#define SWAPIF(cmd) if (client->swapped) { cmd; }
int
ProcXIChangeDeviceHierarchy(ClientPtr client)
{
DeviceIntPtr ptr, keybd, xtstptr, xtstkeybd;
xXIAnyHierarchyChangeInfo *any;
int required_len = sizeof(xXIChangeDeviceHierarchyReq);
char n;
int rc = Success;
int flags[MAXDEVICES] = {0};
REQUEST(xXIChangeDeviceHierarchyReq);
REQUEST_AT_LEAST_SIZE(xXIChangeDeviceHierarchyReq);
if (!stuff->num_changes)
return rc;
any = (xXIAnyHierarchyChangeInfo*)&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 XICreateMasterDevice:
{
xXICreateMasterInfo* c = (xXICreateMasterInfo*)any;
char* name;
SWAPIF(swaps(&c->name_len, n));
name = xcalloc(c->name_len + 1, sizeof(char));
strncpy(name, (char*)&c[1], c->name_len);
rc = AllocDevicePair(client, name, &ptr, &keybd, TRUE);
if (rc != Success)
{
xfree(name);
goto unwind;
}
if (!c->send_core)
ptr->coreEvents = keybd->coreEvents = FALSE;
/* Allocate virtual slave devices for xtest events */
rc = AllocXtstDevice(client, name, &xtstptr, &xtstkeybd);
if (rc != Success)
{
xfree(name);
goto unwind;
}
ActivateDevice(ptr, FALSE);
ActivateDevice(keybd, FALSE);
flags[ptr->id] |= XIMasterAdded;
flags[keybd->id] |= XIMasterAdded;
ActivateDevice(xtstptr, FALSE);
ActivateDevice(xtstkeybd, FALSE);
flags[xtstptr->id] |= XISlaveAdded;
flags[xtstkeybd->id] |= XISlaveAdded;
if (c->enable)
{
EnableDevice(ptr, FALSE);
EnableDevice(keybd, FALSE);
flags[ptr->id] |= XIDeviceEnabled;
flags[keybd->id] |= XIDeviceEnabled;
EnableDevice(xtstptr, FALSE);
EnableDevice(xtstkeybd, FALSE);
flags[xtstptr->id] |= XIDeviceEnabled;
flags[xtstkeybd->id] |= XIDeviceEnabled;
}
/* Attach the XTest virtual devices to the newly
created master device */
AttachDevice(NULL, xtstptr, ptr);
AttachDevice(NULL, xtstkeybd, keybd);
flags[xtstptr->id] |= XISlaveAttached;
flags[xtstkeybd->id] |= XISlaveAttached;
xfree(name);
}
break;
case XIRemoveMasterDevice:
{
xXIRemoveMasterInfo* r = (xXIRemoveMasterInfo*)any;
DeviceIntPtr xtstdevice;
if (r->return_mode != XIAttachToMaster &&
r->return_mode != XIFloating)
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;
}
for(xtstdevice = inputInfo.devices; xtstdevice ; xtstdevice = xtstdevice->next )
if( !xtstdevice->isMaster && xtstdevice->u.master == ptr &&
dixLookupPrivate(&xtstdevice->devPrivates, XTstDevicePrivateKey ))
break;
rc = dixLookupDevice(&xtstptr, xtstdevice->id, client,
DixDestroyAccess);
if (rc != Success)
goto unwind;
/* find keyboards to destroy */
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;
}
/* handle xtst pointer / keyboard slave devices */
if ( IsPointerDevice(xtstptr))
{
/* Search the matching keyboard */
for(xtstdevice = inputInfo.devices; xtstdevice ; xtstdevice = xtstdevice->next )
if( !xtstdevice->isMaster &&
xtstdevice->u.master == keybd &&
IsKeyboardDevice(xtstdevice) &&
dixLookupPrivate(&xtstdevice->devPrivates, XTstDevicePrivateKey ))
break;
rc = dixLookupDevice(&xtstkeybd,
xtstdevice->id,
client,
DixDestroyAccess);
if (rc != Success)
goto unwind;
}
else
{
xtstkeybd = xtstptr;
/* Search the matching pointer */
for(xtstdevice = inputInfo.devices; xtstdevice ; xtstdevice = xtstdevice->next )
if( !xtstdevice->isMaster &&
xtstdevice->u.master == ptr &&
IsPointerDevice(xtstdevice) &&
dixLookupPrivate(&xtstdevice->devPrivates, XTstDevicePrivateKey )
)
break;
rc = dixLookupDevice(&xtstptr,
xtstdevice->id,
client,
DixDestroyAccess);
if (rc != Success)
goto unwind;
}
/* Disabling sends the devices floating, reattach them if
* desired. */
if (r->return_mode == XIAttachToMaster)
{
DeviceIntPtr attached,
newptr,
newkeybd;
rc = dixLookupDevice(&newptr, r->return_pointer,
client, DixWriteAccess);
if (rc != Success)
goto unwind;
if (!newptr->isMaster)
{
client->errorValue = r->return_pointer;
rc = BadDevice;
goto unwind;
}
rc = dixLookupDevice(&newkeybd, r->return_keyboard,
client, DixWriteAccess);
if (rc != Success)
goto unwind;
if (!newkeybd->isMaster)
{
client->errorValue = r->return_keyboard;
rc = BadDevice;
goto unwind;
}
for (attached = inputInfo.devices;
attached;
attached = attached->next)
{
if (!attached->isMaster) {
if (attached->u.master == ptr)
{
AttachDevice(client, attached, newptr);
flags[attached->id] |= XISlaveAttached;
}
if (attached->u.master == keybd)
{
AttachDevice(client, attached, newkeybd);
flags[attached->id] |= XISlaveAttached;
}
}
}
}
/* can't disable until we removed pairing */
keybd->spriteInfo->paired = NULL;
ptr->spriteInfo->paired = NULL;
xtstptr->spriteInfo->paired = NULL;
xtstkeybd->spriteInfo->paired = NULL;
/* disable the remove the devices, xtst devices must be done first
else the sprites they rely on will be destroyed */
DisableDevice(xtstptr, FALSE);
DisableDevice(xtstkeybd, FALSE);
DisableDevice(keybd, FALSE);
DisableDevice(ptr, FALSE);
flags[xtstptr->id] |= XIDeviceDisabled | XISlaveDetached;
flags[xtstkeybd->id] |= XIDeviceDisabled | XISlaveDetached;
flags[keybd->id] |= XIDeviceDisabled;
flags[ptr->id] |= XIDeviceDisabled;
RemoveDevice(xtstptr, FALSE);
RemoveDevice(xtstkeybd, FALSE);
RemoveDevice(keybd, FALSE);
RemoveDevice(ptr, FALSE);
flags[xtstptr->id] |= XISlaveRemoved;
flags[xtstkeybd->id] |= XISlaveRemoved;
flags[keybd->id] |= XIMasterRemoved;
flags[ptr->id] |= XIMasterRemoved;
}
break;
case XIDetachSlave:
{
xXIDetachSlaveInfo* c = (xXIDetachSlaveInfo*)any;
DeviceIntPtr *xtstdevice;
rc = dixLookupDevice(&ptr, c->deviceid, client,
DixWriteAccess);
if (rc != Success)
goto unwind;
if (ptr->isMaster)
{
client->errorValue = c->deviceid;
rc = BadDevice;
goto unwind;
}
xtstdevice = dixLookupPrivate( &ptr->devPrivates,
XTstDevicePrivateKey );
/* Don't allow changes to Xtst Devices, these are fixed */
if( xtstdevice )
{
client->errorValue = c->deviceid;
rc = BadDevice;
goto unwind;
}
AttachDevice(client, ptr, NULL);
flags[ptr->id] |= XISlaveDetached;
}
break;
case XIAttachSlave:
{
xXIAttachSlaveInfo* c = (xXIAttachSlaveInfo*)any;
DeviceIntPtr newmaster;
DeviceIntPtr *xtstdevice;
rc = dixLookupDevice(&ptr, c->deviceid, client,
DixWriteAccess);
if (rc != Success)
goto unwind;
if (ptr->isMaster)
{
client->errorValue = c->deviceid;
rc = BadDevice;
goto unwind;
}
xtstdevice = dixLookupPrivate( &ptr->devPrivates,
XTstDevicePrivateKey );
/* Don't allow changes to Xtst Devices, these are fixed */
if( xtstdevice )
{
client->errorValue = c->deviceid;
rc = BadDevice;
goto unwind;
}
rc = dixLookupDevice(&newmaster, c->new_master,
client, DixWriteAccess);
if (rc != Success)
goto unwind;
if (!newmaster->isMaster)
{
client->errorValue = c->new_master;
rc = BadDevice;
goto unwind;
}
if (!((IsPointerDevice(newmaster) &&
IsPointerDevice(ptr)) ||
(IsKeyboardDevice(newmaster) &&
IsKeyboardDevice(ptr))))
{
rc = BadDevice;
goto unwind;
}
AttachDevice(client, ptr, newmaster);
flags[ptr->id] |= XISlaveAttached;
}
break;
}
any = (xXIAnyHierarchyChangeInfo*)((char*)any + any->length * 4);
}
unwind:
XISendDeviceHierarchyEvent(flags);
return rc;
}
|