# HG changeset patch # User Jonathan Newman # Date 1570231955 -3600 # Sat Oct 05 00:32:35 2019 +0100 # Node ID 8e438499a64375e30027f0f267c51e301ba07bef # Parent e896a6e84cf81e410fd52935f73ccf23be346b0c joystick: Add accelerometer and gyroscope APIs Not yet implemented by any drivers. diff -r e896a6e84cf8 -r 8e438499a643 include/SDL_joystick.h --- a/include/SDL_joystick.h Wed Oct 02 14:55:02 2019 -0400 +++ b/include/SDL_joystick.h Sat Oct 05 00:32:35 2019 +0100 @@ -387,6 +387,37 @@ */ extern DECLSPEC int SDLCALL SDL_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms); + +/** +* Get the current state of the joystick's accelerometer, including the force of gravity. +* If the joystick supplies built-in calibration data, it will be applied to the values retrieved by this function. +* +* \param joystick The joystick from which to retrieve accelerometer data +* \param x The joystick's acceleration in its x axis, in g +* \param y The joystick's acceleration in its y axis, in g +* \param z The joystick's acceleration in its z axis (right-handed coordinate system), in g +* +* \return 0, or -1 if this joystick does not provide an accelerometer, or -2 if the accelerometer is not ready but may be available after some time passes +* +*/ +extern DECLSPEC int SDLCALL SDL_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z); + + +/** +* Get the current state of the joystick's gyroscope. +* If the joystick supplies built-in calibration data, it will be applied to the values retrieved by this function. +* +* \param joystick The joystick from which to retrieve gyroscope data +* \param dx The joystick's angular velocity around its x axis, in degrees per second +* \param dy The joystick's angular velocity around its y axis, in degrees per second +* \param dz The joystick's angular velocity around its z axis, in degrees per second +* +* \return 0, or -1 if this joystick does not provide an gyroscope, or -2 if the gyroscope is not ready but may be available after some time passes +* +*/ +extern DECLSPEC int SDLCALL SDL_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz); + + /** * Close a joystick previously opened with SDL_JoystickOpen(). */ diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/SDL_joystick.c --- a/src/joystick/SDL_joystick.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/SDL_joystick.c Sat Oct 05 00:32:35 2019 +0100 @@ -636,6 +636,24 @@ return joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble, duration_ms); } +int +SDL_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) +{ + if (!SDL_PrivateJoystickValid(joystick)) { + return -1; + } + return joystick->driver->GetAccelerometer(joystick, x, y, z); +} + +int +SDL_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) +{ + if (!SDL_PrivateJoystickValid(joystick)) { + return -1; + } + return joystick->driver->GetGyroscope(joystick, dx, dy, dz); +} + /* * Close a joystick previously opened with SDL_JoystickOpen() */ diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/SDL_sysjoystick.h --- a/src/joystick/SDL_sysjoystick.h Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/SDL_sysjoystick.h Sat Oct 05 00:32:35 2019 +0100 @@ -125,6 +125,10 @@ /* Rumble functionality */ int (*Rumble)(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms); + /* IMU */ + int (*GetAccelerometer)(SDL_Joystick * joystick, float *x, float *y, float *z); + int (*GetGyroscope)(SDL_Joystick * joystick, float *dx, float *dy, float *dz); + /* Function to update the state of a joystick - called as a device poll. * This function shouldn't update the joystick structure directly, * but instead should call SDL_PrivateJoystick*() to deliver events diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/android/SDL_sysjoystick.c --- a/src/joystick/android/SDL_sysjoystick.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/android/SDL_sysjoystick.c Sat Oct 05 00:32:35 2019 +0100 @@ -629,6 +629,18 @@ return SDL_Unsupported(); } +static int +ANDROID_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +ANDROID_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + static void ANDROID_JoystickUpdate(SDL_Joystick * joystick) { @@ -700,6 +712,8 @@ ANDROID_JoystickGetDeviceInstanceID, ANDROID_JoystickOpen, ANDROID_JoystickRumble, + ANDROID_JoystickGetAccelerometer, + ANDROID_JoystickGetGyroscope, ANDROID_JoystickUpdate, ANDROID_JoystickClose, ANDROID_JoystickQuit, diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/bsd/SDL_sysjoystick.c --- a/src/joystick/bsd/SDL_sysjoystick.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/bsd/SDL_sysjoystick.c Sat Oct 05 00:32:35 2019 +0100 @@ -757,6 +757,18 @@ return SDL_Unsupported(); } +static int +BSD_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +BSD_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + SDL_JoystickDriver SDL_BSD_JoystickDriver = { BSD_JoystickInit, @@ -768,6 +780,8 @@ BSD_JoystickGetDeviceInstanceID, BSD_JoystickOpen, BSD_JoystickRumble, + BSD_JoystickGetAccelerometer, + BSD_JoystickGetGyroscope, BSD_JoystickUpdate, BSD_JoystickClose, BSD_JoystickQuit, diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/darwin/SDL_sysjoystick.c --- a/src/joystick/darwin/SDL_sysjoystick.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/darwin/SDL_sysjoystick.c Sat Oct 05 00:32:35 2019 +0100 @@ -872,6 +872,18 @@ return 0; } +static int +DARWIN_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +DARWIN_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + static void DARWIN_JoystickUpdate(SDL_Joystick * joystick) { @@ -1009,6 +1021,8 @@ DARWIN_JoystickGetDeviceInstanceID, DARWIN_JoystickOpen, DARWIN_JoystickRumble, + DARWIN_JoystickGetAccelerometer, + DARWIN_JoystickGetGyroscope, DARWIN_JoystickUpdate, DARWIN_JoystickClose, DARWIN_JoystickQuit, diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/dummy/SDL_sysjoystick.c --- a/src/joystick/dummy/SDL_sysjoystick.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/dummy/SDL_sysjoystick.c Sat Oct 05 00:32:35 2019 +0100 @@ -84,6 +84,18 @@ return SDL_Unsupported(); } +static int +DUMMY_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +DUMMY_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + static void DUMMY_JoystickUpdate(SDL_Joystick * joystick) { @@ -110,6 +122,8 @@ DUMMY_JoystickGetDeviceInstanceID, DUMMY_JoystickOpen, DUMMY_JoystickRumble, + DUMMY_JoystickGetAccelerometer, + DUMMY_JoystickGetGyroscope, DUMMY_JoystickUpdate, DUMMY_JoystickClose, DUMMY_JoystickQuit, diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/emscripten/SDL_sysjoystick.c --- a/src/joystick/emscripten/SDL_sysjoystick.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/emscripten/SDL_sysjoystick.c Sat Oct 05 00:32:35 2019 +0100 @@ -399,6 +399,18 @@ return SDL_Unsupported(); } +static int +EMSCRIPTEN_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +EMSCRIPTEN_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver = { EMSCRIPTEN_JoystickInit, @@ -410,6 +422,8 @@ EMSCRIPTEN_JoystickGetDeviceInstanceID, EMSCRIPTEN_JoystickOpen, EMSCRIPTEN_JoystickRumble, + EMSCRIPTEN_JoystickGetAccelerometer, + EMSCRIPTEN_JoystickGetGyroscope, EMSCRIPTEN_JoystickUpdate, EMSCRIPTEN_JoystickClose, EMSCRIPTEN_JoystickQuit, diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/haiku/SDL_haikujoystick.cc --- a/src/joystick/haiku/SDL_haikujoystick.cc Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/haiku/SDL_haikujoystick.cc Sat Oct 05 00:32:35 2019 +0100 @@ -255,6 +255,18 @@ return SDL_Unsupported(); } + static int + HAIKU_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) + { + return SDL_Unsupported(); + } + + static int + HAIKU_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) + { + return SDL_Unsupported(); + } + SDL_JoystickDriver SDL_HAIKU_JoystickDriver = { HAIKU_JoystickInit, @@ -266,6 +278,8 @@ HAIKU_JoystickGetDeviceInstanceID, HAIKU_JoystickOpen, HAIKU_JoystickRumble, + HAIKU_JoystickGetAccelerometer, + HAIKU_JoystickGetGyroscope, HAIKU_JoystickUpdate, HAIKU_JoystickClose, HAIKU_JoystickQuit, diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/hidapi/SDL_hidapi_ps4.c --- a/src/joystick/hidapi/SDL_hidapi_ps4.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/hidapi/SDL_hidapi_ps4.c Sat Oct 05 00:32:35 2019 +0100 @@ -305,6 +305,18 @@ return 0; } +static int +HIDAPI_DriverPS4_GetAccelerometer(SDL_Joystick *joystick, hid_device *dev, void *context, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +HIDAPI_DriverPS4_GetGyroscope(SDL_Joystick *joystick, hid_device *dev, void *context, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + static void HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS4_Context *ctx, PS4StatePacket_t *packet) { @@ -463,6 +475,8 @@ HIDAPI_DriverPS4_GetDeviceName, HIDAPI_DriverPS4_Init, HIDAPI_DriverPS4_Rumble, + HIDAPI_DriverPS4_GetAccelerometer, + HIDAPI_DriverPS4_GetGyroscope, HIDAPI_DriverPS4_Update, HIDAPI_DriverPS4_Quit }; diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/hidapi/SDL_hidapi_switch.c --- a/src/joystick/hidapi/SDL_hidapi_switch.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/hidapi/SDL_hidapi_switch.c Sat Oct 05 00:32:35 2019 +0100 @@ -680,6 +680,18 @@ return 0; } +static int +HIDAPI_DriverSwitch_GetAccelerometer(SDL_Joystick *joystick, hid_device *dev, void *context, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +HIDAPI_DriverSwitch_GetGyroscope(SDL_Joystick *joystick, hid_device *dev, void *context, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + static void HandleSimpleControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchSimpleStatePacket_t *packet) { /* 0x8000 is the neutral value for all joystick axes */ @@ -894,6 +906,8 @@ HIDAPI_DriverSwitch_GetDeviceName, HIDAPI_DriverSwitch_Init, HIDAPI_DriverSwitch_Rumble, + HIDAPI_DriverSwitch_GetAccelerometer, + HIDAPI_DriverSwitch_GetGyroscope, HIDAPI_DriverSwitch_Update, HIDAPI_DriverSwitch_Quit }; diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/hidapi/SDL_hidapi_xbox360.c --- a/src/joystick/hidapi/SDL_hidapi_xbox360.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c Sat Oct 05 00:32:35 2019 +0100 @@ -382,6 +382,18 @@ return 0; } +static int +HIDAPI_DriverXbox360_GetAccelerometer(SDL_Joystick *joystick, hid_device *dev, void *context, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +HIDAPI_DriverXbox360_GetGyroscope(SDL_Joystick *joystick, hid_device *dev, void *context, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + #ifdef __WIN32__ /* This is the packet format for Xbox 360 and Xbox One controllers on Windows, however with this interface there is no rumble support, no guide button, @@ -780,6 +792,8 @@ HIDAPI_DriverXbox360_GetDeviceName, HIDAPI_DriverXbox360_Init, HIDAPI_DriverXbox360_Rumble, + HIDAPI_DriverXbox360_GetAccelerometer, + HIDAPI_DriverXbox360_GetGyroscope, HIDAPI_DriverXbox360_Update, HIDAPI_DriverXbox360_Quit }; diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/hidapi/SDL_hidapi_xboxone.c --- a/src/joystick/hidapi/SDL_hidapi_xboxone.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c Sat Oct 05 00:32:35 2019 +0100 @@ -206,6 +206,18 @@ return 0; } +static int +HIDAPI_DriverXboxOne_GetAccelerometer(SDL_Joystick *joystick, hid_device *dev, void *context, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +HIDAPI_DriverXboxOne_GetGyroscope(SDL_Joystick *joystick, hid_device *dev, void *context, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + static void HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size) { @@ -313,6 +325,8 @@ HIDAPI_DriverXboxOne_GetDeviceName, HIDAPI_DriverXboxOne_Init, HIDAPI_DriverXboxOne_Rumble, + HIDAPI_DriverXboxOne_GetAccelerometer, + HIDAPI_DriverXboxOne_GetGyroscope, HIDAPI_DriverXboxOne_Update, HIDAPI_DriverXboxOne_Quit }; diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/hidapi/SDL_hidapijoystick.c --- a/src/joystick/hidapi/SDL_hidapijoystick.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/hidapi/SDL_hidapijoystick.c Sat Oct 05 00:32:35 2019 +0100 @@ -1018,6 +1018,32 @@ return result; } +static int +HIDAPI_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) +{ + struct joystick_hwdata *hwdata = joystick->hwdata; + SDL_HIDAPI_DeviceDriver *driver = hwdata->driver; + int result; + + SDL_LockMutex(hwdata->mutex); + result = driver->GetAccelerometer(joystick, hwdata->dev, hwdata->context, x, y, z); + SDL_UnlockMutex(hwdata->mutex); + return result; +} + +static int +HIDAPI_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) +{ + struct joystick_hwdata *hwdata = joystick->hwdata; + SDL_HIDAPI_DeviceDriver *driver = hwdata->driver; + int result; + + SDL_LockMutex(hwdata->mutex); + result = driver->GetGyroscope(joystick, hwdata->dev, hwdata->context, dx, dy, dz); + SDL_UnlockMutex(hwdata->mutex); + return result; +} + static void HIDAPI_JoystickUpdate(SDL_Joystick * joystick) { @@ -1087,6 +1113,8 @@ HIDAPI_JoystickGetDeviceInstanceID, HIDAPI_JoystickOpen, HIDAPI_JoystickRumble, + HIDAPI_JoystickGetAccelerometer, + HIDAPI_JoystickGetGyroscope, HIDAPI_JoystickUpdate, HIDAPI_JoystickClose, HIDAPI_JoystickQuit, diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/hidapi/SDL_hidapijoystick_c.h --- a/src/joystick/hidapi/SDL_hidapijoystick_c.h Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h Sat Oct 05 00:32:35 2019 +0100 @@ -51,6 +51,8 @@ const char *(*GetDeviceName)(Uint16 vendor_id, Uint16 product_id); SDL_bool (*Init)(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context); int (*Rumble)(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms); + int (*GetAccelerometer)(SDL_Joystick *joystick, hid_device *dev, void *context, float *x, float *y, float *z); + int (*GetGyroscope)(SDL_Joystick *joystick, hid_device *dev, void *context, float *dx, float *dy, float *dz); SDL_bool (*Update)(SDL_Joystick *joystick, hid_device *dev, void *context); void (*Quit)(SDL_Joystick *joystick, hid_device *dev, void *context); diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/iphoneos/SDL_sysjoystick.m --- a/src/joystick/iphoneos/SDL_sysjoystick.m Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/iphoneos/SDL_sysjoystick.m Sat Oct 05 00:32:35 2019 +0100 @@ -785,6 +785,18 @@ return SDL_Unsupported(); } +static int +IOS_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +IOS_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + static void IOS_JoystickUpdate(SDL_Joystick * joystick) { @@ -876,6 +888,8 @@ IOS_JoystickGetDeviceInstanceID, IOS_JoystickOpen, IOS_JoystickRumble, + IOS_JoystickGetAccelerometer, + IOS_JoystickGetGyroscope, IOS_JoystickUpdate, IOS_JoystickClose, IOS_JoystickQuit, diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/linux/SDL_sysjoystick.c --- a/src/joystick/linux/SDL_sysjoystick.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/linux/SDL_sysjoystick.c Sat Oct 05 00:32:35 2019 +0100 @@ -794,6 +794,18 @@ return 0; } +static int +LINUX_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +LINUX_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + static SDL_INLINE void HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value) { @@ -1048,6 +1060,8 @@ LINUX_JoystickGetDeviceInstanceID, LINUX_JoystickOpen, LINUX_JoystickRumble, + LINUX_JoystickGetAccelerometer, + LINUX_JoystickGetGyroscope, LINUX_JoystickUpdate, LINUX_JoystickClose, LINUX_JoystickQuit, diff -r e896a6e84cf8 -r 8e438499a643 src/joystick/windows/SDL_windowsjoystick.c --- a/src/joystick/windows/SDL_windowsjoystick.c Wed Oct 02 14:55:02 2019 -0400 +++ b/src/joystick/windows/SDL_windowsjoystick.c Sat Oct 05 00:32:35 2019 +0100 @@ -485,6 +485,18 @@ } } +static int +WINDOWS_JoystickGetAccelerometer(SDL_Joystick * joystick, float *x, float *y, float *z) +{ + return SDL_Unsupported(); +} + +static int +WINDOWS_JoystickGetGyroscope(SDL_Joystick * joystick, float *dx, float *dy, float *dz) +{ + return SDL_Unsupported(); +} + static void WINDOWS_JoystickUpdate(SDL_Joystick * joystick) { @@ -561,6 +573,8 @@ WINDOWS_JoystickGetDeviceInstanceID, WINDOWS_JoystickOpen, WINDOWS_JoystickRumble, + WINDOWS_JoystickGetAccelerometer, + WINDOWS_JoystickGetGyroscope, WINDOWS_JoystickUpdate, WINDOWS_JoystickClose, WINDOWS_JoystickQuit,