Replace some input devPrivates with regular struct fields

In the process, fixes a memory leak in CloseDevice, and an unchecked
memory allocation in InitializePredictableAccelerationProperties.

Signed-off-by: Keith Packard <keithp@keithp.com>
Reviewed-by: Jamey Sharp <jamey@minilop.net>
This commit is contained in:
Keith Packard 2010-04-29 12:24:49 -07:00
parent 998e982b77
commit 65e961fcc1
6 changed files with 47 additions and 92 deletions

View File

@ -94,9 +94,6 @@ Bool ShouldFreeInputMasks(WindowPtr /* pWin */ ,
static Bool MakeInputMasks(WindowPtr /* pWin */ static Bool MakeInputMasks(WindowPtr /* pWin */
); );
/* Used to sture classes currently not in use by an MD */
extern DevPrivateKey UnusedClassesPrivateKey;
/* /*
* Only let the given client know of core events which will affect its * Only let the given client know of core events which will affect its
* interpretation of input events, if the client's ClientPointer (or the * interpretation of input events, if the client's ClientPointer (or the
@ -228,8 +225,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
if (!to->intfeed) if (!to->intfeed)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->intfeed = classes->intfeed; to->intfeed = classes->intfeed;
} }
@ -253,7 +249,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->intfeed && !from->intfeed) } else if (to->intfeed && !from->intfeed)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->intfeed = to->intfeed; classes->intfeed = to->intfeed;
to->intfeed = NULL; to->intfeed = NULL;
} }
@ -264,8 +260,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
if (!to->stringfeed) if (!to->stringfeed)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->stringfeed = classes->stringfeed; to->stringfeed = classes->stringfeed;
} }
@ -289,7 +284,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->stringfeed && !from->stringfeed) } else if (to->stringfeed && !from->stringfeed)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->stringfeed = to->stringfeed; classes->stringfeed = to->stringfeed;
to->stringfeed = NULL; to->stringfeed = NULL;
} }
@ -300,8 +295,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
if (!to->bell) if (!to->bell)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->bell = classes->bell; to->bell = classes->bell;
} }
@ -326,7 +320,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->bell && !from->bell) } else if (to->bell && !from->bell)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->bell = to->bell; classes->bell = to->bell;
to->bell = NULL; to->bell = NULL;
} }
@ -337,8 +331,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
if (!to->leds) if (!to->leds)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->leds = classes->leds; to->leds = classes->leds;
} }
@ -365,7 +358,7 @@ DeepCopyFeedbackClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->leds && !from->leds) } else if (to->leds && !from->leds)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->leds = to->leds; classes->leds = to->leds;
to->leds = NULL; to->leds = NULL;
} }
@ -385,8 +378,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
if (!to->kbdfeed) if (!to->kbdfeed)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->kbdfeed = classes->kbdfeed; to->kbdfeed = classes->kbdfeed;
if (!to->kbdfeed) if (!to->kbdfeed)
@ -417,7 +409,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->kbdfeed && !from->kbdfeed) } else if (to->kbdfeed && !from->kbdfeed)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->kbdfeed = to->kbdfeed; classes->kbdfeed = to->kbdfeed;
to->kbdfeed = NULL; to->kbdfeed = NULL;
} }
@ -426,8 +418,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
{ {
if (!to->key) if (!to->key)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->key = classes->key; to->key = classes->key;
if (!to->key) if (!to->key)
InitKeyboardDeviceStruct(to, NULL, NULL, NULL); InitKeyboardDeviceStruct(to, NULL, NULL, NULL);
@ -439,7 +430,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->key && !from->key) } else if (to->key && !from->key)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->key = to->key; classes->key = to->key;
to->key = NULL; to->key = NULL;
} }
@ -476,8 +467,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
{ {
WindowPtr *oldTrace; WindowPtr *oldTrace;
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->focus = classes->focus; to->focus = classes->focus;
if (!to->focus) if (!to->focus)
{ {
@ -500,7 +490,7 @@ DeepCopyKeyboardClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->focus) } else if (to->focus)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->focus = to->focus; classes->focus = to->focus;
to->focus = NULL; to->focus = NULL;
} }
@ -518,8 +508,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
PtrFeedbackPtr *p, it; PtrFeedbackPtr *p, it;
if (!to->ptrfeed) if (!to->ptrfeed)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->ptrfeed = classes->ptrfeed; to->ptrfeed = classes->ptrfeed;
} }
@ -543,7 +532,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->ptrfeed && !from->ptrfeed) } else if (to->ptrfeed && !from->ptrfeed)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->ptrfeed = to->ptrfeed; classes->ptrfeed = to->ptrfeed;
to->ptrfeed = NULL; to->ptrfeed = NULL;
} }
@ -553,8 +542,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
ValuatorClassPtr v; ValuatorClassPtr v;
if (!to->valuator) if (!to->valuator)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->valuator = classes->valuator; to->valuator = classes->valuator;
if (to->valuator) if (to->valuator)
classes->valuator = NULL; classes->valuator = NULL;
@ -577,7 +565,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->valuator && !from->valuator) } else if (to->valuator && !from->valuator)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->valuator = to->valuator; classes->valuator = to->valuator;
to->valuator = NULL; to->valuator = NULL;
} }
@ -586,8 +574,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
{ {
if (!to->button) if (!to->button)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->button = classes->button; to->button = classes->button;
if (!to->button) if (!to->button)
{ {
@ -617,7 +604,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->button && !from->button) } else if (to->button && !from->button)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->button = to->button; classes->button = to->button;
to->button = NULL; to->button = NULL;
} }
@ -626,8 +613,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
{ {
if (!to->proximity) if (!to->proximity)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->proximity = classes->proximity; to->proximity = classes->proximity;
if (!to->proximity) if (!to->proximity)
{ {
@ -642,7 +628,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->proximity) } else if (to->proximity)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->proximity = to->proximity; classes->proximity = to->proximity;
to->proximity = NULL; to->proximity = NULL;
} }
@ -651,8 +637,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
{ {
if (!to->absolute) if (!to->absolute)
{ {
classes = dixLookupPrivate(&to->devPrivates, classes = to->unused_classes;
UnusedClassesPrivateKey);
to->absolute = classes->absolute; to->absolute = classes->absolute;
if (!to->absolute) if (!to->absolute)
{ {
@ -667,7 +652,7 @@ DeepCopyPointerClasses(DeviceIntPtr from, DeviceIntPtr to)
} else if (to->absolute) } else if (to->absolute)
{ {
ClassesPtr classes; ClassesPtr classes;
classes = dixLookupPrivate(&to->devPrivates, UnusedClassesPrivateKey); classes = to->unused_classes;
classes->absolute = to->absolute; classes->absolute = to->absolute;
to->absolute = NULL; to->absolute = NULL;
} }

View File

@ -89,13 +89,6 @@ SOFTWARE.
* This file handles input device-related stuff. * This file handles input device-related stuff.
*/ */
static int CoreDevicePrivateKeyIndex;
DevPrivateKey CoreDevicePrivateKey = &CoreDevicePrivateKeyIndex;
/* Used to store classes currently not in use by an MD */
static int UnusedClassesPrivateKeyIndex;
DevPrivateKey UnusedClassesPrivateKey = &UnusedClassesPrivateKeyIndex;
static void RecalculateMasterButtons(DeviceIntPtr slave); static void RecalculateMasterButtons(DeviceIntPtr slave);
/** /**
@ -852,8 +845,9 @@ CloseDevice(DeviceIntPtr dev)
if (IsMaster(dev)) if (IsMaster(dev))
{ {
classes = dixLookupPrivate(&dev->devPrivates, UnusedClassesPrivateKey); classes = dev->unused_classes;
FreeAllDeviceClasses(classes); FreeAllDeviceClasses(classes);
xfree(classes);
} }
if (DevHasCursor(dev) && dev->spriteInfo->sprite) { if (DevHasCursor(dev) && dev->spriteInfo->sprite) {
@ -2465,7 +2459,6 @@ AllocDevicePair (ClientPtr client, char* name,
{ {
DeviceIntPtr pointer; DeviceIntPtr pointer;
DeviceIntPtr keyboard; DeviceIntPtr keyboard;
ClassesPtr classes;
*ptr = *keybd = NULL; *ptr = *keybd = NULL;
pointer = AddInputDevice(client, ptr_proc, TRUE); pointer = AddInputDevice(client, ptr_proc, TRUE);
@ -2511,12 +2504,9 @@ AllocDevicePair (ClientPtr client, char* name,
keyboard->last.slave = NULL; keyboard->last.slave = NULL;
keyboard->type = (master) ? MASTER_KEYBOARD : SLAVE; keyboard->type = (master) ? MASTER_KEYBOARD : SLAVE;
/* The ClassesRec stores the device classes currently not used. */ /* The ClassesRec stores the device classes currently not used. */
classes = xcalloc(1, sizeof(ClassesRec)); pointer->unused_classes = xcalloc(1, sizeof(ClassesRec));
dixSetPrivate(&pointer->devPrivates, UnusedClassesPrivateKey, classes); keyboard->unused_classes = xcalloc(1, sizeof(ClassesRec));
classes = xcalloc(1, sizeof(ClassesRec));
dixSetPrivate(&keyboard->devPrivates, UnusedClassesPrivateKey, classes);
*ptr = pointer; *ptr = pointer;
*keybd = keyboard; *keybd = keyboard;

View File

@ -1408,12 +1408,8 @@ CheckGrabForSyncs(DeviceIntPtr thisDev, Bool thisMode, Bool otherMode)
ComputeFreezes(); ComputeFreezes();
} }
/* Only ever used if a grab is called on an attached slave device. */
static int GrabPrivateKeyIndex;
static DevPrivateKey GrabPrivateKey = &GrabPrivateKeyIndex;
/** /**
* Save the device's master device in the devPrivates. This needs to be done * Save the device's master device id. This needs to be done
* if a client directly grabs a slave device that is attached to a master. For * if a client directly grabs a slave device that is attached to a master. For
* the duration of the grab, the device is detached, ungrabbing re-attaches it * the duration of the grab, the device is detached, ungrabbing re-attaches it
* though. * though.
@ -1424,35 +1420,28 @@ static DevPrivateKey GrabPrivateKey = &GrabPrivateKeyIndex;
static void static void
DetachFromMaster(DeviceIntPtr dev) DetachFromMaster(DeviceIntPtr dev)
{ {
intptr_t id;
if (!dev->u.master) if (!dev->u.master)
return; return;
id = dev->u.master->id; dev->saved_master_id = dev->u.master->id;
dixSetPrivate(&dev->devPrivates, GrabPrivateKey, (void *)id);
AttachDevice(NULL, dev, NULL); AttachDevice(NULL, dev, NULL);
} }
static void static void
ReattachToOldMaster(DeviceIntPtr dev) ReattachToOldMaster(DeviceIntPtr dev)
{ {
int id;
void *p;
DeviceIntPtr master = NULL; DeviceIntPtr master = NULL;
if (IsMaster(dev)) if (IsMaster(dev))
return; return;
dixLookupDevice(&master, dev->saved_master_id, serverClient, DixUseAccess);
p = dixLookupPrivate(&dev->devPrivates, GrabPrivateKey);
id = (intptr_t) p; /* silence gcc warnings */
dixLookupDevice(&master, id, serverClient, DixUseAccess);
if (master) if (master)
{ {
AttachDevice(serverClient, dev, master); AttachDevice(serverClient, dev, master);
dixSetPrivate(&dev->devPrivates, GrabPrivateKey, NULL); dev->saved_master_id = 0;
} }
} }

View File

@ -83,8 +83,6 @@ GetAccelerationProfile(DeviceVelocityPtr vel, int profile_num);
/* some int which is not a profile number */ /* some int which is not a profile number */
#define PROFILE_UNINITIALIZE (-100) #define PROFILE_UNINITIALIZE (-100)
/* number of properties for predictable acceleration */
#define NPROPS_PREDICTABLE_ACCEL 4
/** /**
* Init struct so it should match the average case * Init struct so it should match the average case
@ -323,26 +321,18 @@ AccelInitScaleProperty(DeviceIntPtr dev, DeviceVelocityPtr vel)
return XIRegisterPropertyHandler(dev, AccelSetScaleProperty, NULL, NULL); return XIRegisterPropertyHandler(dev, AccelSetScaleProperty, NULL, NULL);
} }
static int AccelPropHandlerPrivateKeyIndex;
DevPrivateKey AccelPropHandlerPrivateKey = &AccelPropHandlerPrivateKeyIndex;
BOOL BOOL
InitializePredictableAccelerationProperties(DeviceIntPtr dev) InitializePredictableAccelerationProperties(DeviceIntPtr dev)
{ {
DeviceVelocityPtr vel = GetDevicePredictableAccelData(dev); DeviceVelocityPtr vel = GetDevicePredictableAccelData(dev);
long *prop_handlers;
if(!vel) if(!vel)
return FALSE; return FALSE;
prop_handlers = xalloc(NPROPS_PREDICTABLE_ACCEL * sizeof(long));
prop_handlers[0] = AccelInitProfileProperty(dev, vel); vel->prop_handlers[0] = AccelInitProfileProperty(dev, vel);
prop_handlers[1] = AccelInitDecelProperty(dev, vel); vel->prop_handlers[1] = AccelInitDecelProperty(dev, vel);
prop_handlers[2] = AccelInitAdaptDecelProperty(dev, vel); vel->prop_handlers[2] = AccelInitAdaptDecelProperty(dev, vel);
prop_handlers[3] = AccelInitScaleProperty(dev, vel); vel->prop_handlers[3] = AccelInitScaleProperty(dev, vel);
dixSetPrivate(&dev->devPrivates, AccelPropHandlerPrivateKey,
prop_handlers);
return TRUE; return TRUE;
} }
@ -350,8 +340,8 @@ InitializePredictableAccelerationProperties(DeviceIntPtr dev)
BOOL BOOL
DeletePredictableAccelerationProperties(DeviceIntPtr dev) DeletePredictableAccelerationProperties(DeviceIntPtr dev)
{ {
DeviceVelocityPtr vel;
Atom prop; Atom prop;
long *prop_handlers;
int i; int i;
prop = XIGetKnownProperty(ACCEL_PROP_VELOCITY_SCALING); prop = XIGetKnownProperty(ACCEL_PROP_VELOCITY_SCALING);
@ -363,13 +353,10 @@ DeletePredictableAccelerationProperties(DeviceIntPtr dev)
prop = XIGetKnownProperty(ACCEL_PROP_PROFILE_NUMBER); prop = XIGetKnownProperty(ACCEL_PROP_PROFILE_NUMBER);
XIDeleteDeviceProperty(dev, prop, FALSE); XIDeleteDeviceProperty(dev, prop, FALSE);
prop_handlers = dixLookupPrivate(&dev->devPrivates, vel = GetDevicePredictableAccelData(dev);
AccelPropHandlerPrivateKey); for (i = 0; vel && i < NPROPS_PREDICTABLE_ACCEL; i++)
dixSetPrivate(&dev->devPrivates, AccelPropHandlerPrivateKey, NULL); if (vel->prop_handlers[i])
XIUnregisterPropertyHandler(dev, vel->prop_handlers[i]);
for (i = 0; prop_handlers && i < NPROPS_PREDICTABLE_ACCEL; i++)
XIUnregisterPropertyHandler(dev, prop_handlers[i]);
xfree(prop_handlers);
return TRUE; return TRUE;
} }

View File

@ -514,8 +514,9 @@ typedef struct _DeviceIntRec {
LedFeedbackPtr leds; LedFeedbackPtr leds;
struct _XkbInterest *xkb_interest; struct _XkbInterest *xkb_interest;
char *config_info; /* used by the hotplug layer */ char *config_info; /* used by the hotplug layer */
ClassesPtr unused_classes; /* for master devices */
int saved_master_id; /* for slaves while grabbed */
PrivateRec *devPrivates; PrivateRec *devPrivates;
int nPrivates;
DeviceUnwrapProc unwrapProc; DeviceUnwrapProc unwrapProc;
SpriteInfoPtr spriteInfo; SpriteInfoPtr spriteInfo;
union { union {

View File

@ -62,6 +62,9 @@ typedef struct _MotionTracker {
int dir; /* initial direction bitfield */ int dir; /* initial direction bitfield */
} MotionTracker, *MotionTrackerPtr; } MotionTracker, *MotionTrackerPtr;
/* number of properties for predictable acceleration */
#define NPROPS_PREDICTABLE_ACCEL 4
/** /**
* Contains all data needed to implement mouse ballistics * Contains all data needed to implement mouse ballistics
*/ */
@ -88,9 +91,9 @@ typedef struct _DeviceVelocityRec {
struct { /* to be able to query this information */ struct { /* to be able to query this information */
int profile_number; int profile_number;
} statistics; } statistics;
long prop_handlers[NPROPS_PREDICTABLE_ACCEL];
} DeviceVelocityRec, *DeviceVelocityPtr; } DeviceVelocityRec, *DeviceVelocityPtr;
extern _X_EXPORT void extern _X_EXPORT void
InitVelocityData(DeviceVelocityPtr vel); InitVelocityData(DeviceVelocityPtr vel);