Date: Wed, 25 Feb 2004 06:41:17 -0500
authorSam Lantinga <slouken@libsdl.org>
Thu, 26 Feb 2004 13:45:22 +0000
changeset 8585db50aa5bf08
parent 857 ce05e92d909b
child 859 6b28c91bf3d2
Date: Wed, 25 Feb 2004 06:41:17 -0500
From: "Ryan C. Gordon"
Subject: Re: MacOS X bugs...

This isn't an ideal patch (trying to open a joystick that has previously
been unplugged will report success, but it'll just never give any input,
etc), but it handles the worst case of deadlock in the event subsystem.
src/joystick/darwin/SDL_sysjoystick.c
     1.1 --- a/src/joystick/darwin/SDL_sysjoystick.c	Thu Feb 26 08:57:32 2004 +0000
     1.2 +++ b/src/joystick/darwin/SDL_sysjoystick.c	Thu Feb 26 13:45:22 2004 +0000
     1.3 @@ -92,6 +92,9 @@
     1.4  	recElement* firstButton;
     1.5  	recElement* firstHat;
     1.6  
     1.7 +	int removed;
     1.8 +	int uncentered;
     1.9 +
    1.10  	struct joystick_hwdata* pNext;			// next device
    1.11  };
    1.12  typedef struct joystick_hwdata recDevice;
    1.13 @@ -161,6 +164,19 @@
    1.14  		return ((value - pElement->minReport) * deviceScale / readScale) + min;
    1.15  }
    1.16  
    1.17 +
    1.18 +static void HIDRemovalCallback(void * target,
    1.19 +                               IOReturn result,
    1.20 +                               void * refcon,
    1.21 +                               void * sender)
    1.22 +{
    1.23 +	recDevice *device = (recDevice *) refcon;
    1.24 +	device->removed = 1;
    1.25 +	device->uncentered = 1;
    1.26 +}
    1.27 +
    1.28 +
    1.29 +
    1.30  /* Create and open an interface to device, required prior to extracting values or building queues.
    1.31   * Note: appliction now owns the device and must close and release it prior to exiting
    1.32   */
    1.33 @@ -193,6 +209,9 @@
    1.34  		result = (*(pDevice->interface))->open (pDevice->interface, 0);
    1.35  		if (kIOReturnSuccess != result)
    1.36  			HIDReportErrorNum ("Failed to open pDevice->interface via open.", result);
    1.37 +		else
    1.38 +			(*(pDevice->interface))->setRemovalCallback (pDevice->interface, HIDRemovalCallback, pDevice, pDevice);
    1.39 +
    1.40  	}
    1.41  	return result;
    1.42  }
    1.43 @@ -722,7 +741,27 @@
    1.44  	recElement *element;
    1.45  	SInt32 value;
    1.46  	int i;
    1.47 -	
    1.48 +
    1.49 +	if (device->removed)  /* device was unplugged; ignore it. */
    1.50 +	{
    1.51 +		if (device->uncentered)
    1.52 +		{
    1.53 +			device->uncentered = 0;
    1.54 +
    1.55 +			/* Tell the app that everything is centered/unpressed... */
    1.56 +			for (i = 0; i < device->axes; i++)
    1.57 +				SDL_PrivateJoystickAxis(joystick, i, 0);
    1.58 +
    1.59 +			for (i = 0; i < device->buttons; i++)
    1.60 +				SDL_PrivateJoystickButton(joystick, i, 0);
    1.61 +
    1.62 +			for (i = 0; i < device->hats; i++)
    1.63 +				SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
    1.64 +		}
    1.65 +
    1.66 +		return;
    1.67 +	}
    1.68 +
    1.69  	element = device->firstAxis;
    1.70  	i = 0;
    1.71  	while (element)