xserver-multidpi/Xi/xichangehierarchy.c

511 lines
14 KiB
C
Raw Normal View History

/*
* 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 "xiquerydevice.h" /* for GetDeviceUse */
#include "xkbsrv.h"
#include "xichangehierarchy.h"
#include "xibarriers.h"
/**
* Send the current state of the device hierarchy to all clients.
*/
void
XISendDeviceHierarchyEvent(int flags[MAXDEVICES])
{
2009-05-12 13:33:54 +02:00
xXIHierarchyEvent *ev;
xXIHierarchyInfo *info;
DeviceIntRec dummyDev;
DeviceIntPtr dev;
int i;
if (!flags)
return;
ev = calloc(1, sizeof(xXIHierarchyEvent) +
MAXDEVICES * sizeof(xXIHierarchyInfo));
if (!ev)
return;
ev->type = GenericEvent;
ev->extension = IReqCode;
ev->evtype = XI_HierarchyChanged;
ev->time = GetTimeInMillis();
ev->flags = 0;
ev->num_info = 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_info++;
info++;
}
}
ev->length = bytes_to_int32(ev->num_info * sizeof(xXIHierarchyInfo));
Xi: silence valgrind warning. (#36120) Conditional jump or move depends on uninitialised value(s) at 0x4357A1: GetEventMask (events.c:454) by 0x43B9E8: DeliverEventsToWindow (events.c:2029) by 0x4E0C59: SendEventToAllWindows (exevents.c:2125) by 0x4E8301: XISendDeviceHierarchyEvent (xichangehierarchy.c:118) by 0x426F99: DisableDevice (devices.c:507) by 0x46BF72: xf86Wakeup (xf86Events.c:457) by 0x432ABA: WakeupHandler (dixutils.c:419) by 0x45B708: WaitForSomething (WaitFor.c:235) by 0x42E8D9: Dispatch (dispatch.c:367) by 0x422DC9: main (main.c:287) Uninitialised value was created by a stack allocation at 0x4E8190: XISendDeviceHierarchyEvent (xichangehierarchy.c:61) Conditional jump or move depends on uninitialised value(s) at 0x43BB78: DeliverEventsToWindow (events.c:2010) by 0x4DDEEA: FindInterestedChildren (exevents.c:2103) by 0x4DDEFF: FindInterestedChildren (exevents.c:2104) by 0x4DDEFF: FindInterestedChildren (exevents.c:2104) by 0x4DDEFF: FindInterestedChildren (exevents.c:2104) by 0x4DDEFF: FindInterestedChildren (exevents.c:2104) by 0x4E0C6F: SendEventToAllWindows (exevents.c:2127) by 0x4E8301: XISendDeviceHierarchyEvent (xichangehierarchy.c:118) by 0x426F99: DisableDevice (devices.c:507) by 0x46BF72: xf86Wakeup (xf86Events.c:457) by 0x432ABA: WakeupHandler (dixutils.c:419) by 0x45B708: WaitForSomething (WaitFor.c:235) Uninitialised value was created by a stack allocation at 0x4E8190: XISendDeviceHierarchyEvent (xichangehierarchy.c:61) Set the type of dummyDev to SLAVE. The jump listed above comes from a check to IsMaster() in GetEventMask() that would then set the XIAllMasterDevices mask. Hierarchy events can only be set for XIAllDevices so the above IsMaster() check had no effect and the device type doesn't really matter anyway beyond shuting up valgrind. Also initialize dummyDev to 0 to ease future debugging. X.Org Bug 36120 <http://bugs.freedesktop.org/show_bug.cgi?id=36120> Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2011-04-11 03:24:29 +02:00
memset(&dummyDev, 0, sizeof(dummyDev));
dummyDev.id = XIAllDevices;
Xi: silence valgrind warning. (#36120) Conditional jump or move depends on uninitialised value(s) at 0x4357A1: GetEventMask (events.c:454) by 0x43B9E8: DeliverEventsToWindow (events.c:2029) by 0x4E0C59: SendEventToAllWindows (exevents.c:2125) by 0x4E8301: XISendDeviceHierarchyEvent (xichangehierarchy.c:118) by 0x426F99: DisableDevice (devices.c:507) by 0x46BF72: xf86Wakeup (xf86Events.c:457) by 0x432ABA: WakeupHandler (dixutils.c:419) by 0x45B708: WaitForSomething (WaitFor.c:235) by 0x42E8D9: Dispatch (dispatch.c:367) by 0x422DC9: main (main.c:287) Uninitialised value was created by a stack allocation at 0x4E8190: XISendDeviceHierarchyEvent (xichangehierarchy.c:61) Conditional jump or move depends on uninitialised value(s) at 0x43BB78: DeliverEventsToWindow (events.c:2010) by 0x4DDEEA: FindInterestedChildren (exevents.c:2103) by 0x4DDEFF: FindInterestedChildren (exevents.c:2104) by 0x4DDEFF: FindInterestedChildren (exevents.c:2104) by 0x4DDEFF: FindInterestedChildren (exevents.c:2104) by 0x4DDEFF: FindInterestedChildren (exevents.c:2104) by 0x4E0C6F: SendEventToAllWindows (exevents.c:2127) by 0x4E8301: XISendDeviceHierarchyEvent (xichangehierarchy.c:118) by 0x426F99: DisableDevice (devices.c:507) by 0x46BF72: xf86Wakeup (xf86Events.c:457) by 0x432ABA: WakeupHandler (dixutils.c:419) by 0x45B708: WaitForSomething (WaitFor.c:235) Uninitialised value was created by a stack allocation at 0x4E8190: XISendDeviceHierarchyEvent (xichangehierarchy.c:61) Set the type of dummyDev to SLAVE. The jump listed above comes from a check to IsMaster() in GetEventMask() that would then set the XIAllMasterDevices mask. Hierarchy events can only be set for XIAllDevices so the above IsMaster() check had no effect and the device type doesn't really matter anyway beyond shuting up valgrind. Also initialize dummyDev to 0 to ease future debugging. X.Org Bug 36120 <http://bugs.freedesktop.org/show_bug.cgi?id=36120> Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2011-04-11 03:24:29 +02:00
dummyDev.type = SLAVE;
SendEventToAllWindows(&dummyDev, (XI_HierarchyChangedMask >> 8),
(xEvent *) ev, 1);
free(ev);
}
/***********************************************************************
*
* This procedure allows a client to change the device hierarchy through
* adding new master devices, removing them, etc.
*
*/
int _X_COLD
SProcXIChangeHierarchy(ClientPtr client)
{
2009-05-12 13:33:54 +02:00
REQUEST(xXIChangeHierarchyReq);
swaps(&stuff->length);
2009-05-12 13:33:54 +02:00
return (ProcXIChangeHierarchy(client));
}
static int
add_master(ClientPtr client, xXIAddMasterInfo * c, int flags[MAXDEVICES])
{
DeviceIntPtr ptr, keybd, XTestptr, XTestkeybd;
char *name;
int rc;
name = calloc(c->name_len + 1, sizeof(char));
if (name == NULL) {
rc = BadAlloc;
goto unwind;
}
strncpy(name, (char *) &c[1], c->name_len);
rc = AllocDevicePair(client, name, &ptr, &keybd,
CorePointerProc, CoreKeyboardProc, TRUE);
if (rc != Success)
goto unwind;
if (!c->send_core)
ptr->coreEvents = keybd->coreEvents = FALSE;
/* Allocate virtual slave devices for xtest events */
rc = AllocXTestDevice(client, name, &XTestptr, &XTestkeybd, ptr, keybd);
if (rc != Success) {
DeleteInputDeviceRequest(ptr);
DeleteInputDeviceRequest(keybd);
goto unwind;
}
ActivateDevice(ptr, FALSE);
ActivateDevice(keybd, FALSE);
flags[ptr->id] |= XIMasterAdded;
flags[keybd->id] |= XIMasterAdded;
ActivateDevice(XTestptr, FALSE);
ActivateDevice(XTestkeybd, FALSE);
flags[XTestptr->id] |= XISlaveAdded;
flags[XTestkeybd->id] |= XISlaveAdded;
if (c->enable) {
EnableDevice(ptr, FALSE);
EnableDevice(keybd, FALSE);
flags[ptr->id] |= XIDeviceEnabled;
flags[keybd->id] |= XIDeviceEnabled;
EnableDevice(XTestptr, FALSE);
EnableDevice(XTestkeybd, FALSE);
flags[XTestptr->id] |= XIDeviceEnabled;
flags[XTestkeybd->id] |= XIDeviceEnabled;
}
/* Attach the XTest virtual devices to the newly
created master device */
AttachDevice(NULL, XTestptr, ptr);
AttachDevice(NULL, XTestkeybd, keybd);
flags[XTestptr->id] |= XISlaveAttached;
flags[XTestkeybd->id] |= XISlaveAttached;
for (int i = 0; i < currentMaxClients; i++)
XIBarrierNewMasterDevice(clients[i], ptr->id);
unwind:
free(name);
return rc;
}
static void
disable_clientpointer(DeviceIntPtr dev)
{
int i;
for (i = 0; i < currentMaxClients; i++) {
ClientPtr client = clients[i];
if (client && client->clientPtr == dev)
client->clientPtr = NULL;
}
}
static int
remove_master(ClientPtr client, xXIRemoveMasterInfo * r, int flags[MAXDEVICES])
{
DeviceIntPtr ptr, keybd, XTestptr, XTestkeybd;
int rc = Success;
if (r->return_mode != XIAttachToMaster && r->return_mode != XIFloating)
return BadValue;
rc = dixLookupDevice(&ptr, r->deviceid, client, DixDestroyAccess);
if (rc != Success)
goto unwind;
if (!IsMaster(ptr)) {
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;
}
ptr = GetMaster(ptr, MASTER_POINTER);
rc = dixLookupDevice(&ptr, ptr->id, client, DixDestroyAccess);
if (rc != Success)
goto unwind;
keybd = GetMaster(ptr, MASTER_KEYBOARD);
rc = dixLookupDevice(&keybd, keybd->id, client, DixDestroyAccess);
if (rc != Success)
goto unwind;
XTestptr = GetXTestDevice(ptr);
rc = dixLookupDevice(&XTestptr, XTestptr->id, client, DixDestroyAccess);
if (rc != Success)
goto unwind;
XTestkeybd = GetXTestDevice(keybd);
rc = dixLookupDevice(&XTestkeybd, XTestkeybd->id, client, DixDestroyAccess);
if (rc != Success)
goto unwind;
disable_clientpointer(ptr);
/* 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, DixAddAccess);
if (rc != Success)
goto unwind;
if (!IsMaster(newptr)) {
client->errorValue = r->return_pointer;
rc = BadDevice;
goto unwind;
}
rc = dixLookupDevice(&newkeybd, r->return_keyboard,
client, DixAddAccess);
if (rc != Success)
goto unwind;
if (!IsMaster(newkeybd)) {
client->errorValue = r->return_keyboard;
rc = BadDevice;
goto unwind;
}
for (attached = inputInfo.devices; attached; attached = attached->next) {
if (!IsMaster(attached)) {
if (GetMaster(attached, MASTER_ATTACHED) == ptr) {
AttachDevice(client, attached, newptr);
flags[attached->id] |= XISlaveAttached;
}
if (GetMaster(attached, MASTER_ATTACHED) == keybd) {
AttachDevice(client, attached, newkeybd);
flags[attached->id] |= XISlaveAttached;
}
}
}
}
for (int i = 0; i < currentMaxClients; i++)
XIBarrierRemoveMasterDevice(clients[i], ptr->id);
/* disable the remove the devices, XTest devices must be done first
else the sprites they rely on will be destroyed */
DisableDevice(XTestptr, FALSE);
DisableDevice(XTestkeybd, FALSE);
DisableDevice(keybd, FALSE);
DisableDevice(ptr, FALSE);
flags[XTestptr->id] |= XIDeviceDisabled | XISlaveDetached;
flags[XTestkeybd->id] |= XIDeviceDisabled | XISlaveDetached;
flags[keybd->id] |= XIDeviceDisabled;
flags[ptr->id] |= XIDeviceDisabled;
flags[XTestptr->id] |= XISlaveRemoved;
flags[XTestkeybd->id] |= XISlaveRemoved;
flags[keybd->id] |= XIMasterRemoved;
flags[ptr->id] |= XIMasterRemoved;
RemoveDevice(XTestptr, FALSE);
RemoveDevice(XTestkeybd, FALSE);
RemoveDevice(keybd, FALSE);
RemoveDevice(ptr, FALSE);
unwind:
return rc;
}
static int
detach_slave(ClientPtr client, xXIDetachSlaveInfo * c, int flags[MAXDEVICES])
{
DeviceIntPtr dev;
int rc;
rc = dixLookupDevice(&dev, c->deviceid, client, DixManageAccess);
if (rc != Success)
goto unwind;
if (IsMaster(dev)) {
client->errorValue = c->deviceid;
rc = BadDevice;
goto unwind;
}
/* Don't allow changes to XTest Devices, these are fixed */
if (IsXTestDevice(dev, NULL)) {
client->errorValue = c->deviceid;
rc = BadDevice;
goto unwind;
}
ReleaseButtonsAndKeys(dev);
AttachDevice(client, dev, NULL);
flags[dev->id] |= XISlaveDetached;
unwind:
return rc;
}
static int
attach_slave(ClientPtr client, xXIAttachSlaveInfo * c, int flags[MAXDEVICES])
{
DeviceIntPtr dev;
DeviceIntPtr newmaster;
int rc;
rc = dixLookupDevice(&dev, c->deviceid, client, DixManageAccess);
if (rc != Success)
goto unwind;
if (IsMaster(dev)) {
client->errorValue = c->deviceid;
rc = BadDevice;
goto unwind;
}
/* Don't allow changes to XTest Devices, these are fixed */
if (IsXTestDevice(dev, NULL)) {
client->errorValue = c->deviceid;
rc = BadDevice;
goto unwind;
}
rc = dixLookupDevice(&newmaster, c->new_master, client, DixAddAccess);
if (rc != Success)
goto unwind;
if (!IsMaster(newmaster)) {
client->errorValue = c->new_master;
rc = BadDevice;
goto unwind;
}
if (!((IsPointerDevice(newmaster) && IsPointerDevice(dev)) ||
(IsKeyboardDevice(newmaster) && IsKeyboardDevice(dev)))) {
rc = BadDevice;
goto unwind;
}
ReleaseButtonsAndKeys(dev);
AttachDevice(client, dev, newmaster);
flags[dev->id] |= XISlaveAttached;
unwind:
return rc;
}
#define SWAPIF(cmd) if (client->swapped) { cmd; }
int
2009-05-12 13:33:54 +02:00
ProcXIChangeHierarchy(ClientPtr client)
{
xXIAnyHierarchyChangeInfo *any;
size_t len; /* length of data remaining in request */
int rc = Success;
int flags[MAXDEVICES] = { 0 };
2009-05-12 13:33:54 +02:00
REQUEST(xXIChangeHierarchyReq);
REQUEST_AT_LEAST_SIZE(xXIChangeHierarchyReq);
if (!stuff->num_changes)
return rc;
len = ((size_t)client->req_len << 2) - sizeof(xXIChangeHierarchyReq);
any = (xXIAnyHierarchyChangeInfo *) &stuff[1];
while (stuff->num_changes--) {
if (len < sizeof(xXIAnyHierarchyChangeInfo)) {
rc = BadLength;
goto unwind;
}
SWAPIF(swaps(&any->type));
SWAPIF(swaps(&any->length));
if (len < ((size_t)any->length << 2))
return BadLength;
#define CHANGE_SIZE_MATCH(type) \
do { \
if ((len < sizeof(type)) || (any->length != (sizeof(type) >> 2))) { \
rc = BadLength; \
goto unwind; \
} \
} while(0)
switch (any->type) {
case XIAddMaster:
{
xXIAddMasterInfo *c = (xXIAddMasterInfo *) any;
/* Variable length, due to appended name string */
if (len < sizeof(xXIAddMasterInfo)) {
rc = BadLength;
goto unwind;
}
SWAPIF(swaps(&c->name_len));
if (c->name_len > (len - sizeof(xXIAddMasterInfo))) {
rc = BadLength;
goto unwind;
}
rc = add_master(client, c, flags);
if (rc != Success)
goto unwind;
}
break;
case XIRemoveMaster:
{
xXIRemoveMasterInfo *r = (xXIRemoveMasterInfo *) any;
CHANGE_SIZE_MATCH(xXIRemoveMasterInfo);
rc = remove_master(client, r, flags);
if (rc != Success)
goto unwind;
}
break;
case XIDetachSlave:
{
xXIDetachSlaveInfo *c = (xXIDetachSlaveInfo *) any;
CHANGE_SIZE_MATCH(xXIDetachSlaveInfo);
rc = detach_slave(client, c, flags);
if (rc != Success)
goto unwind;
}
break;
case XIAttachSlave:
{
xXIAttachSlaveInfo *c = (xXIAttachSlaveInfo *) any;
CHANGE_SIZE_MATCH(xXIAttachSlaveInfo);
rc = attach_slave(client, c, flags);
if (rc != Success)
goto unwind;
}
break;
}
len -= any->length * 4;
any = (xXIAnyHierarchyChangeInfo *) ((char *) any + any->length * 4);
}
unwind:
XISendDeviceHierarchyEvent(flags);
return rc;
}