Updated gamepad, joystick, sensor APIs, removing device indices

Instead of indexing into an internal list of devices which requires locking, we return a list of device IDs which can then be queried individually.

Reference: https://github.com/libsdl-org/SDL/issues/6889
This commit is contained in:
Sam Lantinga
2022-12-27 18:10:06 -08:00
parent e40a96155f
commit 16092f58bb
27 changed files with 917 additions and 797 deletions

View File

@@ -649,13 +649,6 @@ SDL3_0.0.0 {
SDL_GetErrorMsg;
SDL_ClearError;
SDL_Error;
SDL_LockSensors;
SDL_UnlockSensors;
SDL_GetNumSensors;
SDL_GetSensorDeviceName;
SDL_GetSensorDeviceType;
SDL_GetSensorDeviceNonPortableType;
SDL_GetSensorDeviceInstanceID;
SDL_OpenSensor;
SDL_GetSensorFromInstanceID;
SDL_GetSensorName;
@@ -696,16 +689,6 @@ SDL3_0.0.0 {
SDL_ComposeCustomBlendMode;
SDL_LockJoysticks;
SDL_UnlockJoysticks;
SDL_GetNumJoysticks;
SDL_GetJoystickNameForIndex;
SDL_GetJoystickPathForIndex;
SDL_GetJoystickDevicePlayerIndex;
SDL_GetJoystickDeviceGUID;
SDL_GetJoystickDeviceVendor;
SDL_GetJoystickDeviceProduct;
SDL_GetJoystickDeviceProductVersion;
SDL_GetJoystickDeviceType;
SDL_GetJoystickDeviceInstanceID;
SDL_OpenJoystick;
SDL_GetJoystickFromInstanceID;
SDL_GetJoystickFromPlayerIndex;
@@ -761,10 +744,6 @@ SDL3_0.0.0 {
SDL_GetGamepadMappingForGUID;
SDL_GetGamepadMapping;
SDL_IsGamepad;
SDL_GetGamepadNameForIndex;
SDL_GetGamepadPathForIndex;
SDL_GetGamepadTypeForIndex;
SDL_GetGamepadMappingForDeviceIndex;
SDL_OpenGamepad;
SDL_GetGamepadFromInstanceID;
SDL_GetGamepadFromPlayerIndex;
@@ -854,6 +833,32 @@ SDL3_0.0.0 {
SDL_GetEventState;
SDL_GetRenderDriver;
SDL_RunApp;
SDL_GetSensors;
SDL_GetSensorInstanceName;
SDL_GetSensorInstanceType;
SDL_GetSensorInstanceNonPortableType;
SDL_GetJoysticks;
SDL_GetJoystickInstanceName;
SDL_GetJoystickInstancePath;
SDL_GetJoystickInstancePlayerIndex;
SDL_GetJoystickInstanceGUID;
SDL_GetJoystickInstanceVendor;
SDL_GetJoystickInstanceProduct;
SDL_GetJoystickInstanceProductVersion;
SDL_GetJoystickInstanceType;
SDL_GetGamepadInstanceName;
SDL_GetGamepadInstancePath;
SDL_GetGamepadInstanceType;
SDL_GetGamepadInstanceMapping;
SDL_HasJoysticks;
SDL_HasSensors;
SDL_HasGamepads;
SDL_GetGamepads;
SDL_GetGamepadInstancePlayerIndex;
SDL_GetGamepadInstanceGUID;
SDL_GetGamepadInstanceVendor;
SDL_GetGamepadInstanceProduct;
SDL_GetGamepadInstanceProductVersion;
# extra symbols go here (don't modify this line)
local: *;
};

View File

@@ -130,7 +130,6 @@
#define SDL_GetGamepadMappingForGUID SDL_GetGamepadMappingForGUID_REAL
#define SDL_GetGamepadMapping SDL_GetGamepadMapping_REAL
#define SDL_IsGamepad SDL_IsGamepad_REAL
#define SDL_GetGamepadNameForIndex SDL_GetGamepadNameForIndex_REAL
#define SDL_OpenGamepad SDL_OpenGamepad_REAL
#define SDL_GetGamepadName SDL_GetGamepadName_REAL
#define SDL_IsGamepadConnected SDL_IsGamepadConnected_REAL
@@ -182,11 +181,8 @@
#define SDL_AddHintCallback SDL_AddHintCallback_REAL
#define SDL_DelHintCallback SDL_DelHintCallback_REAL
#define SDL_ClearHints SDL_ClearHints_REAL
#define SDL_GetNumJoysticks SDL_GetNumJoysticks_REAL
#define SDL_GetJoystickNameForIndex SDL_GetJoystickNameForIndex_REAL
#define SDL_OpenJoystick SDL_OpenJoystick_REAL
#define SDL_GetJoystickName SDL_GetJoystickName_REAL
#define SDL_GetJoystickDeviceGUID SDL_GetJoystickDeviceGUID_REAL
#define SDL_GetJoystickGUID SDL_GetJoystickGUID_REAL
#define SDL_GetJoystickGUIDString SDL_GetJoystickGUIDString_REAL
#define SDL_GetJoystickGUIDFromString SDL_GetJoystickGUIDFromString_REAL
@@ -578,9 +574,6 @@
#define SDL_CreateSurface SDL_CreateSurface_REAL
#define SDL_CreateSurfaceFrom SDL_CreateSurfaceFrom_REAL
#define SDL_GetHintBoolean SDL_GetHintBoolean_REAL
#define SDL_GetJoystickDeviceVendor SDL_GetJoystickDeviceVendor_REAL
#define SDL_GetJoystickDeviceProduct SDL_GetJoystickDeviceProduct_REAL
#define SDL_GetJoystickDeviceProductVersion SDL_GetJoystickDeviceProductVersion_REAL
#define SDL_GetJoystickVendor SDL_GetJoystickVendor_REAL
#define SDL_GetJoystickProduct SDL_GetJoystickProduct_REAL
#define SDL_GetJoystickProductVersion SDL_GetJoystickProductVersion_REAL
@@ -591,11 +584,9 @@
#define SDL_GetNumGamepadMappings SDL_GetNumGamepadMappings_REAL
#define SDL_GetGamepadMappingForIndex SDL_GetGamepadMappingForIndex_REAL
#define SDL_GetJoystickAxisInitialState SDL_GetJoystickAxisInitialState_REAL
#define SDL_GetJoystickDeviceType SDL_GetJoystickDeviceType_REAL
#define SDL_GetJoystickType SDL_GetJoystickType_REAL
#define SDL_MemoryBarrierReleaseFunction SDL_MemoryBarrierReleaseFunction_REAL
#define SDL_MemoryBarrierAcquireFunction SDL_MemoryBarrierAcquireFunction_REAL
#define SDL_GetJoystickDeviceInstanceID SDL_GetJoystickDeviceInstanceID_REAL
#define SDL_utf8strlen SDL_utf8strlen_REAL
#define SDL_LoadFile_RW SDL_LoadFile_RW_REAL
#define SDL_wcscmp SDL_wcscmp_REAL
@@ -641,7 +632,6 @@
#define SDL_WinRTGetDeviceFamily SDL_WinRTGetDeviceFamily_REAL
#define SDL_log10 SDL_log10_REAL
#define SDL_log10f SDL_log10f_REAL
#define SDL_GetGamepadMappingForDeviceIndex SDL_GetGamepadMappingForDeviceIndex_REAL
#define SDL_LinuxSetThreadPriority SDL_LinuxSetThreadPriority_REAL
#define SDL_HasAVX512F SDL_HasAVX512F_REAL
#define SDL_IsChromebook SDL_IsChromebook_REAL
@@ -652,11 +642,6 @@
#define SDL_wcsdup SDL_wcsdup_REAL
#define SDL_RumbleGamepad SDL_RumbleGamepad_REAL
#define SDL_RumbleJoystick SDL_RumbleJoystick_REAL
#define SDL_GetNumSensors SDL_GetNumSensors_REAL
#define SDL_GetSensorDeviceName SDL_GetSensorDeviceName_REAL
#define SDL_GetSensorDeviceType SDL_GetSensorDeviceType_REAL
#define SDL_GetSensorDeviceNonPortableType SDL_GetSensorDeviceNonPortableType_REAL
#define SDL_GetSensorDeviceInstanceID SDL_GetSensorDeviceInstanceID_REAL
#define SDL_OpenSensor SDL_OpenSensor_REAL
#define SDL_GetSensorFromInstanceID SDL_GetSensorFromInstanceID_REAL
#define SDL_GetSensorName SDL_GetSensorName_REAL
@@ -670,7 +655,6 @@
#define SDL_GetDisplayOrientation SDL_GetDisplayOrientation_REAL
#define SDL_SurfaceHasColorKey SDL_SurfaceHasColorKey_REAL
#define SDL_CreateThreadWithStackSize SDL_CreateThreadWithStackSize_REAL
#define SDL_GetJoystickDevicePlayerIndex SDL_GetJoystickDevicePlayerIndex_REAL
#define SDL_GetJoystickPlayerIndex SDL_GetJoystickPlayerIndex_REAL
#define SDL_GetGamepadPlayerIndex SDL_GetGamepadPlayerIndex_REAL
#define SDL_RenderFlush SDL_RenderFlush_REAL
@@ -702,7 +686,6 @@
#define SDL_strtokr SDL_strtokr_REAL
#define SDL_wcsstr SDL_wcsstr_REAL
#define SDL_wcsncmp SDL_wcsncmp_REAL
#define SDL_GetGamepadTypeForIndex SDL_GetGamepadTypeForIndex_REAL
#define SDL_GetGamepadType SDL_GetGamepadType_REAL
#define SDL_GetGamepadFromPlayerIndex SDL_GetGamepadFromPlayerIndex_REAL
#define SDL_SetGamepadPlayerIndex SDL_SetGamepadPlayerIndex_REAL
@@ -727,8 +710,6 @@
#define SDL_SetJoystickVirtualButton SDL_SetJoystickVirtualButton_REAL
#define SDL_SetJoystickVirtualHat SDL_SetJoystickVirtualHat_REAL
#define SDL_GetErrorMsg SDL_GetErrorMsg_REAL
#define SDL_LockSensors SDL_LockSensors_REAL
#define SDL_UnlockSensors SDL_UnlockSensors_REAL
#define SDL_Metal_GetLayer SDL_Metal_GetLayer_REAL
#define SDL_Metal_GetDrawableSize SDL_Metal_GetDrawableSize_REAL
#define SDL_trunc SDL_trunc_REAL
@@ -835,9 +816,7 @@
#define SDL_GetRectAndLineIntersectionF SDL_GetRectAndLineIntersectionF_REAL
#define SDL_GetRenderWindow SDL_GetRenderWindow_REAL
#define SDL_bsearch SDL_bsearch_REAL
#define SDL_GetGamepadPathForIndex SDL_GetGamepadPathForIndex_REAL
#define SDL_GetGamepadPath SDL_GetGamepadPath_REAL
#define SDL_GetJoystickPathForIndex SDL_GetJoystickPathForIndex_REAL
#define SDL_GetJoystickPath SDL_GetJoystickPath_REAL
#define SDL_AttachVirtualJoystickEx SDL_AttachVirtualJoystickEx_REAL
#define SDL_GetGamepadFirmwareVersion SDL_GetGamepadFirmwareVersion_REAL
@@ -876,3 +855,29 @@
#define SDL_GetEventState SDL_GetEventState_REAL
#define SDL_GetRenderDriver SDL_GetRenderDriver_REAL
#define SDL_RunApp SDL_RunApp_REAL
#define SDL_GetSensors SDL_GetSensors_REAL
#define SDL_GetSensorInstanceName SDL_GetSensorInstanceName_REAL
#define SDL_GetSensorInstanceType SDL_GetSensorInstanceType_REAL
#define SDL_GetSensorInstanceNonPortableType SDL_GetSensorInstanceNonPortableType_REAL
#define SDL_GetJoysticks SDL_GetJoysticks_REAL
#define SDL_GetJoystickInstanceName SDL_GetJoystickInstanceName_REAL
#define SDL_GetJoystickInstancePath SDL_GetJoystickInstancePath_REAL
#define SDL_GetJoystickInstancePlayerIndex SDL_GetJoystickInstancePlayerIndex_REAL
#define SDL_GetJoystickInstanceGUID SDL_GetJoystickInstanceGUID_REAL
#define SDL_GetJoystickInstanceVendor SDL_GetJoystickInstanceVendor_REAL
#define SDL_GetJoystickInstanceProduct SDL_GetJoystickInstanceProduct_REAL
#define SDL_GetJoystickInstanceProductVersion SDL_GetJoystickInstanceProductVersion_REAL
#define SDL_GetJoystickInstanceType SDL_GetJoystickInstanceType_REAL
#define SDL_GetGamepadInstanceName SDL_GetGamepadInstanceName_REAL
#define SDL_GetGamepadInstancePath SDL_GetGamepadInstancePath_REAL
#define SDL_GetGamepadInstanceType SDL_GetGamepadInstanceType_REAL
#define SDL_GetGamepadInstanceMapping SDL_GetGamepadInstanceMapping_REAL
#define SDL_HasJoysticks SDL_HasJoysticks_REAL
#define SDL_HasSensors SDL_HasSensors_REAL
#define SDL_HasGamepads SDL_HasGamepads_REAL
#define SDL_GetGamepads SDL_GetGamepads_REAL
#define SDL_GetGamepadInstancePlayerIndex SDL_GetGamepadInstancePlayerIndex_REAL
#define SDL_GetGamepadInstanceGUID SDL_GetGamepadInstanceGUID_REAL
#define SDL_GetGamepadInstanceVendor SDL_GetGamepadInstanceVendor_REAL
#define SDL_GetGamepadInstanceProduct SDL_GetGamepadInstanceProduct_REAL
#define SDL_GetGamepadInstanceProductVersion SDL_GetGamepadInstanceProductVersion_REAL

View File

@@ -156,9 +156,8 @@ SDL_DYNAPI_PROC(char*,SDL_GetPrefPath,(const char *a, const char *b),(a,b),retur
SDL_DYNAPI_PROC(int,SDL_AddGamepadMapping,(const char *a),(a),return)
SDL_DYNAPI_PROC(char*,SDL_GetGamepadMappingForGUID,(SDL_JoystickGUID a),(a),return)
SDL_DYNAPI_PROC(char*,SDL_GetGamepadMapping,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_IsGamepad,(int a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetGamepadNameForIndex,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_Gamepad*,SDL_OpenGamepad,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_IsGamepad,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_Gamepad*,SDL_OpenGamepad,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetGamepadName,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_IsGamepadConnected,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(SDL_Joystick*,SDL_GetGamepadJoystick,(SDL_Gamepad *a),(a),return)
@@ -209,11 +208,8 @@ SDL_DYNAPI_PROC(const char*,SDL_GetHint,(const char *a),(a),return)
SDL_DYNAPI_PROC(void,SDL_AddHintCallback,(const char *a, SDL_HintCallback b, void *c),(a,b,c),)
SDL_DYNAPI_PROC(void,SDL_DelHintCallback,(const char *a, SDL_HintCallback b, void *c),(a,b,c),)
SDL_DYNAPI_PROC(void,SDL_ClearHints,(void),(),)
SDL_DYNAPI_PROC(int,SDL_GetNumJoysticks,(void),(),return)
SDL_DYNAPI_PROC(const char*,SDL_GetJoystickNameForIndex,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_Joystick*,SDL_OpenJoystick,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_Joystick*,SDL_OpenJoystick,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetJoystickName,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickGUID,SDL_GetJoystickDeviceGUID,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickGUID,SDL_GetJoystickGUID,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(void,SDL_GetJoystickGUIDString,(SDL_JoystickGUID a, char *b, int c),(a,b,c),)
SDL_DYNAPI_PROC(SDL_JoystickGUID,SDL_GetJoystickGUIDFromString,(const char *a),(a),return)
@@ -611,9 +607,6 @@ SDL_DYNAPI_PROC(void,SDL_SetWindowResizable,(SDL_Window *a, SDL_bool b),(a,b),)
SDL_DYNAPI_PROC(SDL_Surface*,SDL_CreateSurface,(int a, int b, Uint32 c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_Surface*,SDL_CreateSurfaceFrom,(void *a, int b, int c, int d, Uint32 e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_GetHintBoolean,(const char *a, SDL_bool b),(a,b),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickDeviceVendor,(int a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickDeviceProduct,(int a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickDeviceProductVersion,(int a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickVendor,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickProduct,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickProductVersion,(SDL_Joystick *a),(a),return)
@@ -624,11 +617,9 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_HasNEON,(void),(),return)
SDL_DYNAPI_PROC(int,SDL_GetNumGamepadMappings,(void),(),return)
SDL_DYNAPI_PROC(char*,SDL_GetGamepadMappingForIndex,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_GetJoystickAxisInitialState,(SDL_Joystick *a, int b, Sint16 *c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_JoystickType,SDL_GetJoystickDeviceType,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickType,SDL_GetJoystickType,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(void,SDL_MemoryBarrierReleaseFunction,(void),(),)
SDL_DYNAPI_PROC(void,SDL_MemoryBarrierAcquireFunction,(void),(),)
SDL_DYNAPI_PROC(SDL_JoystickID,SDL_GetJoystickDeviceInstanceID,(int a),(a),return)
SDL_DYNAPI_PROC(size_t,SDL_utf8strlen,(const char *a),(a),return)
SDL_DYNAPI_PROC(void*,SDL_LoadFile_RW,(SDL_RWops *a, size_t *b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_wcscmp,(const wchar_t *a, const wchar_t *b),(a,b),return)
@@ -678,7 +669,6 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_IsAndroidTV,(void),(),return)
#endif
SDL_DYNAPI_PROC(double,SDL_log10,(double a),(a),return)
SDL_DYNAPI_PROC(float,SDL_log10f,(float a),(a),return)
SDL_DYNAPI_PROC(char*,SDL_GetGamepadMappingForDeviceIndex,(int a),(a),return)
#ifdef __LINUX__
SDL_DYNAPI_PROC(int,SDL_LinuxSetThreadPriority,(Sint64 a, int b),(a,b),return)
#endif
@@ -693,12 +683,7 @@ SDL_DYNAPI_PROC(float,SDL_expf,(float a),(a),return)
SDL_DYNAPI_PROC(wchar_t*,SDL_wcsdup,(const wchar_t *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_RumbleGamepad,(SDL_Gamepad *a, Uint16 b, Uint16 c, Uint32 d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_RumbleJoystick,(SDL_Joystick *a, Uint16 b, Uint16 c, Uint32 d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_GetNumSensors,(void),(),return)
SDL_DYNAPI_PROC(const char*,SDL_GetSensorDeviceName,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_SensorType,SDL_GetSensorDeviceType,(int a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetSensorDeviceNonPortableType,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_SensorID,SDL_GetSensorDeviceInstanceID,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_Sensor*,SDL_OpenSensor,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_Sensor*,SDL_OpenSensor,(SDL_SensorID a),(a),return)
SDL_DYNAPI_PROC(SDL_Sensor*,SDL_GetSensorFromInstanceID,(SDL_SensorID a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetSensorName,(SDL_Sensor *a),(a),return)
SDL_DYNAPI_PROC(SDL_SensorType,SDL_GetSensorType,(SDL_Sensor *a),(a),return)
@@ -721,7 +706,6 @@ SDL_DYNAPI_PROC(SDL_Thread*,SDL_CreateThreadWithStackSize,(SDL_ThreadFunction a,
SDL_DYNAPI_PROC(SDL_Thread*,SDL_CreateThreadWithStackSize,(SDL_ThreadFunction a, const char *b, const size_t c, void *d),(a,b,c,d),return)
#endif
SDL_DYNAPI_PROC(int,SDL_GetJoystickDevicePlayerIndex,(int a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetJoystickPlayerIndex,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetGamepadPlayerIndex,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_RenderFlush,(SDL_Renderer *a),(a),return)
@@ -753,7 +737,6 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_HasARMSIMD,(void),(),return)
SDL_DYNAPI_PROC(char*,SDL_strtokr,(char *a, const char *b, char **c),(a,b,c),return)
SDL_DYNAPI_PROC(wchar_t*,SDL_wcsstr,(const wchar_t *a, const wchar_t *b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_wcsncmp,(const wchar_t *a, const wchar_t *b, size_t c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetGamepadTypeForIndex,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetGamepadType,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(SDL_Gamepad*,SDL_GetGamepadFromPlayerIndex,(int a),(a),return)
SDL_DYNAPI_PROC(void,SDL_SetGamepadPlayerIndex,(SDL_Gamepad *a, int b),(a,b),)
@@ -775,15 +758,13 @@ SDL_DYNAPI_PROC(int,SDL_GetAndroidSDKVersion,(void),(),return)
#endif
SDL_DYNAPI_PROC(int,SDL_isupper,(int a),(a),return)
SDL_DYNAPI_PROC(int,SDL_islower,(int a),(a),return)
SDL_DYNAPI_PROC(int,SDL_AttachVirtualJoystick,(SDL_JoystickType a, int b, int c, int d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_DetachVirtualJoystick,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_IsJoystickVirtual,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickID,SDL_AttachVirtualJoystick,(SDL_JoystickType a, int b, int c, int d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_DetachVirtualJoystick,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_IsJoystickVirtual,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(int,SDL_SetJoystickVirtualAxis,(SDL_Joystick *a, int b, Sint16 c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_SetJoystickVirtualButton,(SDL_Joystick *a, int b, Uint8 c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_SetJoystickVirtualHat,(SDL_Joystick *a, int b, Uint8 c),(a,b,c),return)
SDL_DYNAPI_PROC(char*,SDL_GetErrorMsg,(char *a, int b),(a,b),return)
SDL_DYNAPI_PROC(void,SDL_LockSensors,(void),(),)
SDL_DYNAPI_PROC(void,SDL_UnlockSensors,(void),(),)
SDL_DYNAPI_PROC(void*,SDL_Metal_GetLayer,(SDL_MetalView a),(a),return)
SDL_DYNAPI_PROC(void,SDL_Metal_GetDrawableSize,(SDL_Window *a, int *b, int *c),(a,b,c),)
SDL_DYNAPI_PROC(double,SDL_trunc,(double a),(a),return)
@@ -902,11 +883,9 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_GetRectEnclosingPointsF,(const SDL_FPoint *a, int b
SDL_DYNAPI_PROC(SDL_bool,SDL_GetRectAndLineIntersectionF,(const SDL_FRect *a, float *b, float *c, float *d, float *e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(SDL_Window*,SDL_GetRenderWindow,(SDL_Renderer *a),(a),return)
SDL_DYNAPI_PROC(void*,SDL_bsearch,(const void *a, const void *b, size_t c, size_t d, int (SDLCALL *e)(const void *, const void *)),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(const char*,SDL_GetGamepadPathForIndex,(int a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetGamepadPath,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetJoystickPathForIndex,(int a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetJoystickPath,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_AttachVirtualJoystickEx,(const SDL_VirtualJoystickDesc *a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickID,SDL_AttachVirtualJoystickEx,(const SDL_VirtualJoystickDesc *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetGamepadFirmwareVersion,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickFirmwareVersion,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(void,SDL_GUIDToString,(SDL_GUID a, char *b, int c),(a,b,c),)
@@ -950,3 +929,29 @@ SDL_DYNAPI_PROC(void,SDL_DelayNS,(Uint64 a),(a),)
SDL_DYNAPI_PROC(Uint8,SDL_GetEventState,(Uint32 a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetRenderDriver,(int a),(a),return)
SDL_DYNAPI_PROC(int,SDL_RunApp,(int a, char* b[], SDL_main_func c, void *d),(a,b,c,d),return)
SDL_DYNAPI_PROC(SDL_SensorID*,SDL_GetSensors,(int *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetSensorInstanceName,(SDL_SensorID a),(a),return)
SDL_DYNAPI_PROC(SDL_SensorType,SDL_GetSensorInstanceType,(SDL_SensorID a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetSensorInstanceNonPortableType,(SDL_SensorID a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickID*,SDL_GetJoysticks,(int *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetJoystickInstanceName,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetJoystickInstancePath,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetJoystickInstancePlayerIndex,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickGUID,SDL_GetJoystickInstanceGUID,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickInstanceVendor,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickInstanceProduct,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickInstanceProductVersion,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickType,SDL_GetJoystickInstanceType,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetGamepadInstanceName,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetGamepadInstancePath,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetGamepadInstanceType,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(char*,SDL_GetGamepadInstanceMapping,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_HasJoysticks,(void),(),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_HasSensors,(void),(),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_HasGamepads,(void),(),return)
SDL_DYNAPI_PROC(SDL_JoystickID*,SDL_GetGamepads,(int *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetGamepadInstancePlayerIndex,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickGUID,SDL_GetGamepadInstanceGUID,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetGamepadInstanceVendor,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetGamepadInstanceProduct,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetGamepadInstanceProductVersion,(SDL_JoystickID a),(a),return)

View File

@@ -380,17 +380,17 @@ static void SDL_LogEvent(const SDL_Event *event)
break;
#undef PRINT_CBUTTON_EVENT
#define PRINT_CONTROLLERDEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d)", (uint)event->cdevice.timestamp, (int)event->cdevice.which)
#define PRINT_GAMEPADDEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d)", (uint)event->cdevice.timestamp, (int)event->cdevice.which)
SDL_EVENT_CASE(SDL_GAMEPADADDED)
PRINT_CONTROLLERDEV_EVENT(event);
PRINT_GAMEPADDEV_EVENT(event);
break;
SDL_EVENT_CASE(SDL_GAMEPADREMOVED)
PRINT_CONTROLLERDEV_EVENT(event);
PRINT_GAMEPADDEV_EVENT(event);
break;
SDL_EVENT_CASE(SDL_GAMEPADDEVICEREMAPPED)
PRINT_CONTROLLERDEV_EVENT(event);
PRINT_GAMEPADDEV_EVENT(event);
break;
#undef PRINT_CONTROLLERDEV_EVENT
#undef PRINT_GAMEPADDEV_EVENT
#define PRINT_CTOUCHPAD_EVENT(event) \
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d touchpad=%d finger=%d x=%f y=%f pressure=%f)", \
@@ -998,15 +998,12 @@ static SDL_bool SDL_events_need_polling()
SDL_bool need_polling = SDL_FALSE;
#if !SDL_JOYSTICK_DISABLED
need_polling =
SDL_WasInit(SDL_INIT_JOYSTICK) &&
SDL_update_joysticks &&
(SDL_GetNumJoysticks() > 0);
need_polling = SDL_WasInit(SDL_INIT_JOYSTICK) && SDL_update_joysticks && SDL_HasJoysticks();
#endif
#if !SDL_SENSOR_DISABLED
need_polling = need_polling ||
(SDL_WasInit(SDL_INIT_SENSOR) && SDL_update_sensors && (SDL_GetNumSensors() > 0));
(SDL_WasInit(SDL_INIT_SENSOR) && SDL_update_sensors && SDL_HasSensors());
#endif
return need_polling;

View File

@@ -1443,7 +1443,7 @@ static GamepadMapping_t *SDL_PrivateGenerateAutomaticGamepadMapping(const char *
return SDL_PrivateAddMappingForGUID(guid, mapping, &existing, SDL_GAMEPAD_MAPPING_PRIORITY_DEFAULT);
}
static GamepadMapping_t *SDL_PrivateGetGamepadMapping(int device_index)
static GamepadMapping_t *SDL_PrivateGetGamepadMapping(SDL_JoystickID instance_id)
{
const char *name;
SDL_JoystickGUID guid;
@@ -1451,19 +1451,14 @@ static GamepadMapping_t *SDL_PrivateGetGamepadMapping(int device_index)
SDL_AssertJoysticksLocked();
if ((device_index < 0) || (device_index >= SDL_GetNumJoysticks())) {
SDL_SetError("There are %d joysticks available", SDL_GetNumJoysticks());
return NULL;
}
name = SDL_GetJoystickNameForIndex(device_index);
guid = SDL_GetJoystickDeviceGUID(device_index);
name = SDL_GetJoystickInstanceName(instance_id);
guid = SDL_GetJoystickInstanceGUID(instance_id);
mapping = SDL_PrivateGetGamepadMappingForNameAndGUID(name, guid);
if (mapping == NULL) {
SDL_GamepadMapping raw_map;
SDL_zero(raw_map);
if (SDL_PrivateJoystickGetAutoGamepadMapping(device_index, &raw_map)) {
if (SDL_PrivateJoystickGetAutoGamepadMapping(instance_id, &raw_map)) {
mapping = SDL_PrivateGenerateAutomaticGamepadMapping(name, guid, &raw_map);
}
}
@@ -1896,37 +1891,78 @@ int SDL_InitGamepadMappings(void)
int SDL_InitGamepads(void)
{
int i;
SDL_JoystickID *joysticks;
/* watch for joy events and fire gamepad ones if needed */
/* Watch for joystick events and fire gamepad ones if needed */
SDL_AddEventWatch(SDL_GamepadEventWatcher, NULL);
/* Send added events for gamepads currently attached */
for (i = 0; i < SDL_GetNumJoysticks(); ++i) {
if (SDL_IsGamepad(i)) {
SDL_Event deviceevent;
deviceevent.type = SDL_GAMEPADADDED;
deviceevent.common.timestamp = 0;
deviceevent.cdevice.which = i;
SDL_PushEvent(&deviceevent);
joysticks = SDL_GetJoysticks(NULL);
if (joysticks) {
for (i = 0; joysticks[i]; ++i) {
if (SDL_IsGamepad(joysticks[i])) {
SDL_Event deviceevent;
deviceevent.type = SDL_GAMEPADADDED;
deviceevent.common.timestamp = 0;
deviceevent.cdevice.which = joysticks[i];
SDL_PushEvent(&deviceevent);
}
}
SDL_free(joysticks);
}
return 0;
}
/*
* Get the implementation dependent name of a gamepad
*/
const char *SDL_GetGamepadNameForIndex(int joystick_index)
SDL_bool SDL_HasGamepads(void)
{
SDL_bool retval = SDL_FALSE;
SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL);
if (joysticks) {
int i;
for (i = 0; joysticks[i]; ++i) {
if (SDL_IsGamepad(joysticks[i])) {
retval = SDL_TRUE;
break;
}
}
SDL_free(joysticks);
}
return retval;
}
SDL_JoystickID *SDL_GetGamepads(int *count)
{
int num_joysticks = 0;
int num_gamepads = 0;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
if (joysticks) {
int i;
for (i = num_joysticks - 1; i >= 0; --i) {
if (SDL_IsGamepad(joysticks[i])) {
++num_gamepads;
} else {
SDL_memmove(&joysticks[i], &joysticks[i+1], (num_gamepads + 1) * sizeof(joysticks[i]));
}
}
SDL_free(joysticks);
}
if (count) {
*count = num_gamepads;
}
return joysticks;
}
const char *SDL_GetGamepadInstanceName(SDL_JoystickID instance_id)
{
const char *retval = NULL;
SDL_LockJoysticks();
{
GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(joystick_index);
GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(instance_id);
if (mapping != NULL) {
if (SDL_strcmp(mapping->name, "*") == 0) {
retval = SDL_GetJoystickNameForIndex(joystick_index);
retval = SDL_GetJoystickInstanceName(instance_id);
} else {
retval = mapping->name;
}
@@ -1937,50 +1973,53 @@ const char *SDL_GetGamepadNameForIndex(int joystick_index)
return retval;
}
/*
* Get the implementation dependent path of a gamepad
*/
const char *SDL_GetGamepadPathForIndex(int joystick_index)
const char *SDL_GetGamepadInstancePath(SDL_JoystickID instance_id)
{
const char *retval = NULL;
SDL_LockJoysticks();
{
GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(joystick_index);
if (mapping != NULL) {
retval = SDL_GetJoystickPathForIndex(joystick_index);
}
}
SDL_UnlockJoysticks();
return retval;
return SDL_GetJoystickInstancePath(instance_id);
}
/**
* Get the type of a gamepad.
*/
SDL_GamepadType SDL_GetGamepadTypeForIndex(int joystick_index)
int SDL_GetGamepadInstancePlayerIndex(SDL_JoystickID instance_id)
{
return SDL_GetGamepadTypeFromGUID(SDL_GetJoystickDeviceGUID(joystick_index), SDL_GetJoystickNameForIndex(joystick_index));
return SDL_GetJoystickInstancePlayerIndex(instance_id);
}
/**
* Get the mapping of a gamepad.
* This can be called before any gamepads are opened.
* If no mapping can be found, this function returns NULL.
*/
char *SDL_GetGamepadMappingForDeviceIndex(int joystick_index)
SDL_JoystickGUID SDL_GetGamepadInstanceGUID(SDL_JoystickID instance_id)
{
return SDL_GetJoystickInstanceGUID(instance_id);
}
Uint16 SDL_GetGamepadInstanceVendor(SDL_JoystickID instance_id)
{
return SDL_GetJoystickInstanceVendor(instance_id);
}
Uint16 SDL_GetGamepadInstanceProduct(SDL_JoystickID instance_id)
{
return SDL_GetJoystickInstanceProduct(instance_id);
}
Uint16 SDL_GetGamepadInstanceProductVersion(SDL_JoystickID instance_id)
{
return SDL_GetJoystickInstanceProductVersion(instance_id);
}
SDL_GamepadType SDL_GetGamepadInstanceType(SDL_JoystickID instance_id)
{
return SDL_GetGamepadTypeFromGUID(SDL_GetJoystickInstanceGUID(instance_id), SDL_GetJoystickInstanceName(instance_id));
}
char *SDL_GetGamepadInstanceMapping(SDL_JoystickID instance_id)
{
char *retval = NULL;
SDL_LockJoysticks();
{
GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(joystick_index);
GamepadMapping_t *mapping = SDL_PrivateGetGamepadMapping(instance_id);
if (mapping != NULL) {
SDL_JoystickGUID guid;
char pchGUID[33];
size_t needed;
guid = SDL_GetJoystickDeviceGUID(joystick_index);
guid = SDL_GetJoystickInstanceGUID(instance_id);
SDL_GetJoystickGUIDString(guid, pchGUID, sizeof(pchGUID));
/* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
@@ -2019,13 +2058,13 @@ SDL_bool SDL_IsGamepadNameAndGUID(const char *name, SDL_JoystickGUID guid)
/*
* Return 1 if the joystick at this device index is a supported gamepad
*/
SDL_bool SDL_IsGamepad(int joystick_index)
SDL_bool SDL_IsGamepad(SDL_JoystickID instance_id)
{
SDL_bool retval;
SDL_LockJoysticks();
{
if (SDL_PrivateGetGamepadMapping(joystick_index) != NULL) {
if (SDL_PrivateGetGamepadMapping(instance_id) != NULL) {
retval = SDL_TRUE;
} else {
retval = SDL_FALSE;
@@ -2129,15 +2168,12 @@ SDL_bool SDL_ShouldIgnoreGamepad(const char *name, SDL_JoystickGUID guid)
}
/*
* Open a gamepad for use - the index passed as an argument refers to
* the N'th gamepad on the system. This index is the value which will
* identify this gamepad in future gamepad events.
* Open a gamepad for use
*
* This function returns a gamepad identifier, or NULL if an error occurred.
*/
SDL_Gamepad *SDL_OpenGamepad(int joystick_index)
SDL_Gamepad *SDL_OpenGamepad(SDL_JoystickID instance_id)
{
SDL_JoystickID instance_id;
SDL_Gamepad *gamepad;
SDL_Gamepad *gamepadlist;
GamepadMapping_t *pSupportedGamepad = NULL;
@@ -2146,7 +2182,6 @@ SDL_Gamepad *SDL_OpenGamepad(int joystick_index)
gamepadlist = SDL_gamepads;
/* If the gamepad is already open, return it */
instance_id = SDL_GetJoystickDeviceInstanceID(joystick_index);
while (gamepadlist != NULL) {
if (instance_id == gamepadlist->joystick->instance_id) {
gamepad = gamepadlist;
@@ -2158,9 +2193,9 @@ SDL_Gamepad *SDL_OpenGamepad(int joystick_index)
}
/* Find a gamepad mapping */
pSupportedGamepad = SDL_PrivateGetGamepadMapping(joystick_index);
pSupportedGamepad = SDL_PrivateGetGamepadMapping(instance_id);
if (pSupportedGamepad == NULL) {
SDL_SetError("Couldn't find mapping for device (%d)", joystick_index);
SDL_SetError("Couldn't find mapping for device (%" SDL_PRIu32 ")", instance_id);
SDL_UnlockJoysticks();
return NULL;
}
@@ -2174,7 +2209,7 @@ SDL_Gamepad *SDL_OpenGamepad(int joystick_index)
}
gamepad->magic = &gamepad_magic;
gamepad->joystick = SDL_OpenJoystick(joystick_index);
gamepad->joystick = SDL_OpenJoystick(instance_id);
if (gamepad->joystick == NULL) {
SDL_free(gamepad);
SDL_UnlockJoysticks();

View File

@@ -113,7 +113,7 @@ static int SDL_joysticks_locked;
static SDL_bool SDL_joysticks_initialized;
static SDL_bool SDL_joysticks_quitting = SDL_FALSE;
static SDL_Joystick *SDL_joysticks SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
static SDL_atomic_t SDL_next_joystick_instance_id SDL_GUARDED_BY(SDL_joystick_lock);
static SDL_atomic_t SDL_last_joystick_instance_id SDL_GUARDED_BY(SDL_joystick_lock);
static int SDL_joystick_player_count SDL_GUARDED_BY(SDL_joystick_lock) = 0;
static SDL_JoystickID *SDL_joystick_players SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE;
@@ -169,29 +169,30 @@ void SDL_AssertJoysticksLocked(void)
}
/*
* Get the driver and device index for an API device index
* Get the driver and device index for a joystick instance ID
* This should be called while the joystick lock is held, to prevent another thread from updating the list
*/
static SDL_bool SDL_GetDriverAndJoystickIndex(int device_index, SDL_JoystickDriver **driver, int *driver_index)
static SDL_bool SDL_GetDriverAndJoystickIndex(SDL_JoystickID instance_id, SDL_JoystickDriver **driver, int *driver_index)
{
int i, num_joysticks, total_joysticks = 0;
int i, num_joysticks, device_index;
SDL_AssertJoysticksLocked();
if (device_index >= 0) {
if (instance_id > 0) {
for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
num_joysticks = SDL_joystick_drivers[i]->GetCount();
if (device_index < num_joysticks) {
*driver = SDL_joystick_drivers[i];
*driver_index = device_index;
return SDL_TRUE;
for (device_index = 0; device_index < num_joysticks; ++device_index) {
SDL_JoystickID joystick_id = SDL_joystick_drivers[i]->GetDeviceInstanceID(device_index);
if (joystick_id == instance_id) {
*driver = SDL_joystick_drivers[i];
*driver_index = device_index;
return SDL_TRUE;
}
}
device_index -= num_joysticks;
total_joysticks += num_joysticks;
}
}
SDL_SetError("There are %d joysticks available", total_joysticks);
SDL_SetError("Joystick %" SDL_PRIu32 " not found", instance_id);
return SDL_FALSE;
}
@@ -271,8 +272,7 @@ static SDL_bool SDL_SetJoystickIDForPlayerIndex(int player_index, SDL_JoystickID
}
/* Update the driver with the new index */
device_index = SDL_JoystickGetDeviceIndexFromInstanceID(instance_id);
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
driver->SetDevicePlayerIndex(device_index, player_index);
}
@@ -332,18 +332,57 @@ int SDL_InitJoysticks(void)
return status;
}
/*
* Count the number of joysticks attached to the system
*/
int SDL_GetNumJoysticks(void)
SDL_bool SDL_HasJoysticks(void)
{
int i, total_joysticks = 0;
int i;
SDL_bool retval = SDL_FALSE;
SDL_LockJoysticks();
for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
total_joysticks += SDL_joystick_drivers[i]->GetCount();
if (SDL_joystick_drivers[i]->GetCount() > 0) {
retval = SDL_TRUE;
break;
}
}
SDL_UnlockJoysticks();
return total_joysticks;
return retval;
}
SDL_JoystickID *SDL_GetJoysticks(int *count)
{
int i, num_joysticks, device_index;
int joystick_index = 0, total_joysticks = 0;
SDL_JoystickID *joysticks;
SDL_LockJoysticks();
{
for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
total_joysticks += SDL_joystick_drivers[i]->GetCount();
}
if (count) {
*count = total_joysticks;
}
joysticks = (SDL_JoystickID *)SDL_malloc((total_joysticks + 1) * sizeof(*joysticks));
if (joysticks) {
for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
num_joysticks = SDL_joystick_drivers[i]->GetCount();
for (device_index = 0; device_index < num_joysticks; ++device_index) {
SDL_assert(joystick_index < total_joysticks);
joysticks[joystick_index] = SDL_joystick_drivers[i]->GetDeviceInstanceID(device_index);
SDL_assert(joysticks[joystick_index] > 0);
++joystick_index;
}
}
SDL_assert(joystick_index == total_joysticks);
joysticks[joystick_index] = 0;
} else {
SDL_OutOfMemory();
}
}
SDL_UnlockJoysticks();
return joysticks;
}
/*
@@ -352,19 +391,20 @@ int SDL_GetNumJoysticks(void)
*/
SDL_JoystickID SDL_GetNextJoystickInstanceID()
{
return SDL_AtomicIncRef(&SDL_next_joystick_instance_id);
return SDL_AtomicIncRef(&SDL_last_joystick_instance_id) + 1;
}
/*
* Get the implementation dependent name of a joystick
*/
const char *SDL_GetJoystickNameForIndex(int device_index)
const char *SDL_GetJoystickInstanceName(SDL_JoystickID instance_id)
{
SDL_JoystickDriver *driver;
int device_index;
const char *name = NULL;
SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
name = driver->GetDeviceName(device_index);
}
SDL_UnlockJoysticks();
@@ -376,13 +416,14 @@ const char *SDL_GetJoystickNameForIndex(int device_index)
/*
* Get the implementation dependent path of a joystick
*/
const char *SDL_GetJoystickPathForIndex(int device_index)
const char *SDL_GetJoystickInstancePath(SDL_JoystickID instance_id)
{
SDL_JoystickDriver *driver;
int device_index;
const char *path = NULL;
SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
path = driver->GetDevicePath(device_index);
}
SDL_UnlockJoysticks();
@@ -397,12 +438,12 @@ const char *SDL_GetJoystickPathForIndex(int device_index)
/*
* Get the player index of a joystick, or -1 if it's not available
*/
int SDL_GetJoystickDevicePlayerIndex(int device_index)
int SDL_GetJoystickInstancePlayerIndex(SDL_JoystickID instance_id)
{
int player_index;
SDL_LockJoysticks();
player_index = SDL_GetPlayerIndexForJoystickID(SDL_GetJoystickDeviceInstanceID(device_index));
player_index = SDL_GetPlayerIndexForJoystickID(instance_id);
SDL_UnlockJoysticks();
return player_index;
@@ -457,10 +498,10 @@ static SDL_bool SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
*
* This function returns a joystick identifier, or NULL if an error occurred.
*/
SDL_Joystick *SDL_OpenJoystick(int device_index)
SDL_Joystick *SDL_OpenJoystick(SDL_JoystickID instance_id)
{
SDL_JoystickDriver *driver;
SDL_JoystickID instance_id;
int device_index;
SDL_Joystick *joystick;
SDL_Joystick *joysticklist;
const char *joystickname = NULL;
@@ -469,16 +510,15 @@ SDL_Joystick *SDL_OpenJoystick(int device_index)
SDL_LockJoysticks();
if (!SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
if (!SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
SDL_UnlockJoysticks();
return NULL;
}
joysticklist = SDL_joysticks;
/* If the joystick is already open, return it
* it is important that we have a single joystick * for each instance id
* it is important that we have a single joystick for each instance id
*/
instance_id = driver->GetDeviceInstanceID(device_index);
while (joysticklist) {
if (instance_id == joysticklist->instance_id) {
joystick = joysticklist;
@@ -550,7 +590,7 @@ SDL_Joystick *SDL_OpenJoystick(int device_index)
}
}
joystick->is_game_controller = SDL_IsGamepad(device_index);
joystick->is_gamepad = SDL_IsGamepad(instance_id);
/* Add joystick to list */
++joystick->ref_count;
@@ -570,7 +610,7 @@ SDL_Joystick *SDL_OpenJoystick(int device_index)
return joystick;
}
int SDL_AttachVirtualJoystick(SDL_JoystickType type, int naxes, int nbuttons, int nhats)
SDL_JoystickID SDL_AttachVirtualJoystick(SDL_JoystickType type, int naxes, int nbuttons, int nhats)
{
SDL_VirtualJoystickDesc desc;
@@ -583,10 +623,10 @@ int SDL_AttachVirtualJoystick(SDL_JoystickType type, int naxes, int nbuttons, in
return SDL_AttachVirtualJoystickEx(&desc);
}
int SDL_AttachVirtualJoystickEx(const SDL_VirtualJoystickDesc *desc)
SDL_JoystickID SDL_AttachVirtualJoystickEx(const SDL_VirtualJoystickDesc *desc)
{
#if SDL_JOYSTICK_VIRTUAL
int retval;
SDL_JoystickID retval;
SDL_LockJoysticks();
retval = SDL_JoystickAttachVirtualInner(desc);
@@ -597,36 +637,29 @@ int SDL_AttachVirtualJoystickEx(const SDL_VirtualJoystickDesc *desc)
#endif
}
int SDL_DetachVirtualJoystick(int device_index)
int SDL_DetachVirtualJoystick(SDL_JoystickID instance_id)
{
#if SDL_JOYSTICK_VIRTUAL
SDL_JoystickDriver *driver;
int retval;
SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
if (driver == &SDL_VIRTUAL_JoystickDriver) {
const int retval = SDL_JoystickDetachVirtualInner(device_index);
SDL_UnlockJoysticks();
return retval;
}
}
retval = SDL_JoystickDetachVirtualInner(instance_id);
SDL_UnlockJoysticks();
return SDL_SetError("Virtual joystick not found at provided index");
return retval;
#else
return SDL_SetError("SDL not built with virtual-joystick support");
#endif
}
SDL_bool SDL_IsJoystickVirtual(int device_index)
SDL_bool SDL_IsJoystickVirtual(SDL_JoystickID instance_id)
{
#if SDL_JOYSTICK_VIRTUAL
SDL_JoystickDriver *driver;
int driver_device_index;
int device_index;
SDL_bool is_virtual = SDL_FALSE;
SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &driver_device_index)) {
if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
if (driver == &SDL_VIRTUAL_JoystickDriver) {
is_virtual = SDL_TRUE;
}
@@ -705,13 +738,14 @@ SDL_bool SDL_IsJoystickValid(SDL_Joystick *joystick)
return (joystick && joystick->magic == &joystick_magic);
}
SDL_bool SDL_PrivateJoystickGetAutoGamepadMapping(int device_index, SDL_GamepadMapping *out)
SDL_bool SDL_PrivateJoystickGetAutoGamepadMapping(SDL_JoystickID instance_id, SDL_GamepadMapping *out)
{
SDL_JoystickDriver *driver;
int device_index;
SDL_bool is_ok = SDL_FALSE;
SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
is_ok = driver->GetGamepadMapping(device_index, out);
}
SDL_UnlockJoysticks();
@@ -1341,15 +1375,11 @@ void SDL_PrivateJoystickAddSensor(SDL_Joystick *joystick, SDL_SensorType type, f
}
}
void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance)
void SDL_PrivateJoystickAdded(SDL_JoystickID instance_id)
{
SDL_JoystickDriver *driver;
int driver_device_index;
int device_index;
int player_index = -1;
int device_index = SDL_JoystickGetDeviceIndexFromInstanceID(device_instance);
if (device_index < 0) {
return;
}
SDL_AssertJoysticksLocked();
@@ -1357,14 +1387,14 @@ void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance)
return;
}
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &driver_device_index)) {
player_index = driver->GetDevicePlayerIndex(driver_device_index);
if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
player_index = driver->GetDevicePlayerIndex(device_index);
}
if (player_index < 0 && SDL_IsGamepad(device_index)) {
if (player_index < 0 && SDL_IsGamepad(instance_id)) {
player_index = SDL_FindFreePlayerIndex();
}
if (player_index >= 0) {
SDL_SetJoystickIDForPlayerIndex(player_index, device_instance);
SDL_SetJoystickIDForPlayerIndex(player_index, instance_id);
}
#if !SDL_EVENTS_DISABLED
@@ -1375,73 +1405,13 @@ void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance)
event.common.timestamp = 0;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
event.jdevice.which = instance_id;
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
}
/*
* If there is an existing add event in the queue, it needs to be modified
* to have the right value for which, because the number of controllers in
* the system is now one less.
*/
static void UpdateEventsForDeviceRemoval(int device_index, Uint32 type)
{
int i, num_events;
SDL_Event *events;
SDL_bool isstack;
num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type);
if (num_events <= 0) {
return;
}
events = SDL_small_alloc(SDL_Event, num_events, &isstack);
if (events == NULL) {
return;
}
num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, type, type);
for (i = 0; i < num_events; ++i) {
Sint32 which = -1;
switch (type) {
case SDL_JOYDEVICEADDED:
which = events[i].jdevice.which;
break;
case SDL_GAMEPADADDED:
which = events[i].cdevice.which;
break;
default:
break;
}
if (which < device_index) {
/* No change for index values lower than the removed device */
} else if (which == device_index) {
/* Drop this event entirely */
SDL_memmove(&events[i], &events[i + 1], sizeof(*events) * (num_events - (i + 1)));
--num_events;
--i;
} else {
/* Fix up the device index if greater than the removed device */
switch (type) {
case SDL_JOYDEVICEADDED:
--events[i].jdevice.which;
break;
case SDL_GAMEPADADDED:
--events[i].cdevice.which;
break;
default:
break;
}
}
}
SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
SDL_small_free(events, isstack);
}
void SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)
{
int i, j;
@@ -1473,11 +1443,10 @@ void SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)
}
}
void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
void SDL_PrivateJoystickRemoved(SDL_JoystickID instance_id)
{
SDL_Joystick *joystick = NULL;
int player_index;
int device_index;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif
@@ -1485,15 +1454,12 @@ void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
SDL_AssertJoysticksLocked();
/* Find this joystick... */
device_index = 0;
for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
if (joystick->instance_id == device_instance) {
if (joystick->instance_id == instance_id) {
SDL_PrivateJoystickForceRecentering(joystick);
joystick->attached = SDL_FALSE;
break;
}
++device_index;
}
#if !SDL_EVENTS_DISABLED
@@ -1501,15 +1467,12 @@ void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
event.common.timestamp = 0;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_instance;
event.jdevice.which = instance_id;
SDL_PushEvent(&event);
}
UpdateEventsForDeviceRemoval(device_index, SDL_JOYDEVICEADDED);
UpdateEventsForDeviceRemoval(device_index, SDL_GAMEPADADDED);
#endif /* !SDL_EVENTS_DISABLED */
player_index = SDL_GetPlayerIndexForJoystickID(device_instance);
player_index = SDL_GetPlayerIndexForJoystickID(instance_id);
if (player_index >= 0) {
SDL_joystick_players[player_index] = -1;
}
@@ -2684,13 +2647,14 @@ SDL_bool SDL_ShouldIgnoreJoystick(const char *name, SDL_JoystickGUID guid)
}
/* return the guid for this index */
SDL_JoystickGUID SDL_GetJoystickDeviceGUID(int device_index)
SDL_JoystickGUID SDL_GetJoystickInstanceGUID(SDL_JoystickID instance_id)
{
SDL_JoystickDriver *driver;
int device_index;
SDL_JoystickGUID guid;
SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
guid = driver->GetDeviceGUID(device_index);
} else {
SDL_zero(guid);
@@ -2700,78 +2664,47 @@ SDL_JoystickGUID SDL_GetJoystickDeviceGUID(int device_index)
return guid;
}
Uint16 SDL_GetJoystickDeviceVendor(int device_index)
Uint16 SDL_GetJoystickInstanceVendor(SDL_JoystickID instance_id)
{
Uint16 vendor;
SDL_JoystickGUID guid = SDL_GetJoystickDeviceGUID(device_index);
SDL_JoystickGUID guid = SDL_GetJoystickInstanceGUID(instance_id);
SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL, NULL);
return vendor;
}
Uint16 SDL_GetJoystickDeviceProduct(int device_index)
Uint16 SDL_GetJoystickInstanceProduct(SDL_JoystickID instance_id)
{
Uint16 product;
SDL_JoystickGUID guid = SDL_GetJoystickDeviceGUID(device_index);
SDL_JoystickGUID guid = SDL_GetJoystickInstanceGUID(instance_id);
SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL, NULL);
return product;
}
Uint16 SDL_GetJoystickDeviceProductVersion(int device_index)
Uint16 SDL_GetJoystickInstanceProductVersion(SDL_JoystickID instance_id)
{
Uint16 version;
SDL_JoystickGUID guid = SDL_GetJoystickDeviceGUID(device_index);
SDL_JoystickGUID guid = SDL_GetJoystickInstanceGUID(instance_id);
SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version, NULL);
return version;
}
SDL_JoystickType SDL_GetJoystickDeviceType(int device_index)
SDL_JoystickType SDL_GetJoystickInstanceType(SDL_JoystickID instance_id)
{
SDL_JoystickType type;
SDL_JoystickGUID guid = SDL_GetJoystickDeviceGUID(device_index);
SDL_JoystickGUID guid = SDL_GetJoystickInstanceGUID(instance_id);
type = SDL_GetJoystickGUIDType(guid);
if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
if (SDL_IsGamepad(device_index)) {
if (SDL_IsGamepad(instance_id)) {
type = SDL_JOYSTICK_TYPE_GAMEPAD;
}
}
return type;
}
SDL_JoystickID SDL_GetJoystickDeviceInstanceID(int device_index)
{
SDL_JoystickDriver *driver;
SDL_JoystickID instance_id = -1;
SDL_LockJoysticks();
if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
instance_id = driver->GetDeviceInstanceID(device_index);
}
SDL_UnlockJoysticks();
return instance_id;
}
int SDL_JoystickGetDeviceIndexFromInstanceID(SDL_JoystickID instance_id)
{
int i, num_joysticks, device_index = -1;
SDL_LockJoysticks();
num_joysticks = SDL_GetNumJoysticks();
for (i = 0; i < num_joysticks; ++i) {
if (SDL_GetJoystickDeviceInstanceID(i) == instance_id) {
device_index = i;
break;
}
}
SDL_UnlockJoysticks();
return device_index;
}
SDL_JoystickGUID SDL_GetJoystickGUID(SDL_Joystick *joystick)
{
SDL_JoystickGUID retval;
@@ -2857,7 +2790,7 @@ SDL_JoystickType SDL_GetJoystickType(SDL_Joystick *joystick)
{
CHECK_JOYSTICK_MAGIC(joystick, SDL_JOYSTICK_TYPE_UNKNOWN);
if (joystick->is_game_controller) {
if (joystick->is_gamepad) {
type = SDL_JOYSTICK_TYPE_GAMEPAD;
}
}

View File

@@ -52,9 +52,6 @@ extern void SDL_AssertJoysticksLocked(void) SDL_ASSERT_CAPABILITY(SDL_joystick_l
/* Function to get the next available joystick instance ID */
extern SDL_JoystickID SDL_GetNextJoystickInstanceID(void);
/* Function to return the device index for a joystick ID, or -1 if not found */
extern int SDL_JoystickGetDeviceIndexFromInstanceID(SDL_JoystickID instance_id);
/* Function to standardize the name for a controller
This should be freed with SDL_free() when no longer needed
*/
@@ -134,8 +131,8 @@ extern SDL_bool SDL_ShouldIgnoreJoystick(const char *name, SDL_JoystickGUID guid
/* Internal event queueing functions */
extern void SDL_PrivateJoystickAddTouchpad(SDL_Joystick *joystick, int nfingers);
extern void SDL_PrivateJoystickAddSensor(SDL_Joystick *joystick, SDL_SensorType type, float rate);
extern void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance);
extern void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance);
extern void SDL_PrivateJoystickAdded(SDL_JoystickID instance_id);
extern void SDL_PrivateJoystickRemoved(SDL_JoystickID instance_id);
extern void SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick);
extern int SDL_SendJoystickAxis(Uint64 timestamp, SDL_Joystick *joystick,
Uint8 axis, Sint16 value);
@@ -198,7 +195,7 @@ typedef struct SDL_GamepadMapping
} SDL_GamepadMapping;
/* Function to get autodetected gamepad controller mapping from the driver */
extern SDL_bool SDL_PrivateJoystickGetAutoGamepadMapping(int device_index,
extern SDL_bool SDL_PrivateJoystickGetAutoGamepadMapping(SDL_JoystickID instance_id,
SDL_GamepadMapping *out);
/* Ends C function definitions when using C++ */

View File

@@ -109,7 +109,7 @@ struct SDL_Joystick
Uint64 led_expiration _guarded;
SDL_bool attached _guarded;
SDL_bool is_game_controller _guarded;
SDL_bool is_gamepad _guarded;
SDL_bool delayed_guide_button _guarded; /* SDL_TRUE if this device has the guide button event delayed */
SDL_JoystickPowerLevel epowerlevel _guarded; /* power level of this joystick, SDL_JOYSTICK_POWER_UNKNOWN if not supported */

View File

@@ -141,10 +141,10 @@ static SDL_bool HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
}
device->context = ctx;
ctx->joysticks[0] = -1;
ctx->joysticks[1] = -1;
ctx->joysticks[2] = -1;
ctx->joysticks[3] = -1;
ctx->joysticks[0] = 0;
ctx->joysticks[1] = 0;
ctx->joysticks[2] = 0;
ctx->joysticks[3] = 0;
ctx->rumble[0] = rumbleMagic;
ctx->useRumbleBrake = SDL_FALSE;
@@ -186,14 +186,14 @@ static SDL_bool HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
if (ctx->joysticks[i] == -1) {
if (ctx->joysticks[i] == 0) {
ResetAxisRange(ctx, i);
HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
}
} else {
if (ctx->joysticks[i] != -1) {
if (ctx->joysticks[i] != 0) {
HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
ctx->joysticks[i] = -1;
ctx->joysticks[i] = 0;
}
continue;
}
@@ -315,7 +315,7 @@ static void HIDAPI_DriverGameCube_HandleNintendoPacket(SDL_HIDAPI_Device *device
ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
if (ctx->joysticks[i] == -1) {
if (ctx->joysticks[i] == 0) {
ResetAxisRange(ctx, i);
HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
}
@@ -326,9 +326,9 @@ static void HIDAPI_DriverGameCube_HandleNintendoPacket(SDL_HIDAPI_Device *device
continue;
}
} else {
if (ctx->joysticks[i] != -1) {
if (ctx->joysticks[i] != 0) {
HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
ctx->joysticks[i] = -1;
ctx->joysticks[i] = 0;
}
continue;
}

View File

@@ -30,6 +30,20 @@
static joystick_hwdata *g_VJoys SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
static joystick_hwdata *VIRTUAL_HWDataForInstance(SDL_JoystickID instance_id)
{
joystick_hwdata *vjoy;
SDL_AssertJoysticksLocked();
for (vjoy = g_VJoys; vjoy; vjoy = vjoy->next) {
if (instance_id == vjoy->instance_id) {
return vjoy;
}
}
return NULL;
}
static joystick_hwdata *VIRTUAL_HWDataForIndex(int device_index)
{
joystick_hwdata *vjoy;
@@ -91,10 +105,9 @@ static void VIRTUAL_FreeHWData(joystick_hwdata *hwdata)
SDL_free(hwdata);
}
int SDL_JoystickAttachVirtualInner(const SDL_VirtualJoystickDesc *desc)
SDL_JoystickID SDL_JoystickAttachVirtualInner(const SDL_VirtualJoystickDesc *desc)
{
joystick_hwdata *hwdata = NULL;
int device_index = -1;
const char *name = NULL;
int axis_triggerleft = -1;
int axis_triggerright = -1;
@@ -238,19 +251,15 @@ int SDL_JoystickAttachVirtualInner(const SDL_VirtualJoystickDesc *desc)
}
SDL_PrivateJoystickAdded(hwdata->instance_id);
/* Return the new virtual-device's index */
device_index = SDL_JoystickGetDeviceIndexFromInstanceID(hwdata->instance_id);
return device_index;
return hwdata->instance_id;
}
int SDL_JoystickDetachVirtualInner(int device_index)
int SDL_JoystickDetachVirtualInner(SDL_JoystickID instance_id)
{
SDL_JoystickID instance_id;
joystick_hwdata *hwdata = VIRTUAL_HWDataForIndex(device_index);
joystick_hwdata *hwdata = VIRTUAL_HWDataForInstance(instance_id);
if (hwdata == NULL) {
return SDL_SetError("Virtual joystick data not found");
}
instance_id = hwdata->instance_id;
VIRTUAL_FreeHWData(hwdata);
SDL_PrivateJoystickRemoved(instance_id);
return 0;
@@ -260,7 +269,7 @@ int SDL_SetJoystickVirtualAxisInner(SDL_Joystick *joystick, int axis, Sint16 val
{
joystick_hwdata *hwdata;
SDL_LockJoysticks();
SDL_AssertJoysticksLocked();
if (joystick == NULL || !joystick->hwdata) {
SDL_UnlockJoysticks();
@@ -275,7 +284,6 @@ int SDL_SetJoystickVirtualAxisInner(SDL_Joystick *joystick, int axis, Sint16 val
hwdata->axes[axis] = value;
SDL_UnlockJoysticks();
return 0;
}
@@ -283,7 +291,7 @@ int SDL_SetJoystickVirtualButtonInner(SDL_Joystick *joystick, int button, Uint8
{
joystick_hwdata *hwdata;
SDL_LockJoysticks();
SDL_AssertJoysticksLocked();
if (joystick == NULL || !joystick->hwdata) {
SDL_UnlockJoysticks();
@@ -298,7 +306,6 @@ int SDL_SetJoystickVirtualButtonInner(SDL_Joystick *joystick, int button, Uint8
hwdata->buttons[button] = value;
SDL_UnlockJoysticks();
return 0;
}
@@ -306,7 +313,7 @@ int SDL_SetJoystickVirtualHatInner(SDL_Joystick *joystick, int hat, Uint8 value)
{
joystick_hwdata *hwdata;
SDL_LockJoysticks();
SDL_AssertJoysticksLocked();
if (joystick == NULL || !joystick->hwdata) {
SDL_UnlockJoysticks();
@@ -321,7 +328,6 @@ int SDL_SetJoystickVirtualHatInner(SDL_Joystick *joystick, int hat, Uint8 value)
hwdata->hats[hat] = value;
SDL_UnlockJoysticks();
return 0;
}

View File

@@ -44,8 +44,8 @@ typedef struct joystick_hwdata
struct joystick_hwdata *next;
} joystick_hwdata;
int SDL_JoystickAttachVirtualInner(const SDL_VirtualJoystickDesc *desc);
int SDL_JoystickDetachVirtualInner(int device_index);
SDL_JoystickID SDL_JoystickAttachVirtualInner(const SDL_VirtualJoystickDesc *desc);
int SDL_JoystickDetachVirtualInner(SDL_JoystickID instance_id);
int SDL_SetJoystickVirtualAxisInner(SDL_Joystick *joystick, int axis, Sint16 value);
int SDL_SetJoystickVirtualButtonInner(SDL_Joystick *joystick, int button, Uint8 value);

View File

@@ -50,7 +50,7 @@ static SDL_SensorDriver *SDL_sensor_drivers[] = {
};
static SDL_mutex *SDL_sensor_lock = NULL; /* This needs to support recursive locks */
static SDL_Sensor *SDL_sensors SDL_GUARDED_BY(SDL_sensor_lock) = NULL;
static SDL_atomic_t SDL_next_sensor_instance_id SDL_GUARDED_BY(SDL_sensor_lock);
static SDL_atomic_t SDL_last_sensor_instance_id SDL_GUARDED_BY(SDL_sensor_lock);
static SDL_bool SDL_updating_sensor SDL_GUARDED_BY(SDL_sensor_lock) = SDL_FALSE;
void SDL_LockSensors(void) SDL_ACQUIRE(SDL_sensor_lock)
@@ -87,18 +87,57 @@ int SDL_InitSensors(void)
return status;
}
/*
* Count the number of sensors attached to the system
*/
int SDL_GetNumSensors(void)
SDL_bool SDL_HasSensors(void)
{
int i, total_sensors = 0;
int i;
SDL_bool retval = SDL_FALSE;
SDL_LockSensors();
for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
total_sensors += SDL_sensor_drivers[i]->GetCount();
if (SDL_sensor_drivers[i]->GetCount() > 0) {
retval = SDL_TRUE;
break;
}
}
SDL_UnlockSensors();
return total_sensors;
return retval;
}
SDL_SensorID *SDL_GetSensors(int *count)
{
int i, num_sensors, device_index;
int sensor_index = 0, total_sensors = 0;
SDL_SensorID *sensors;
SDL_LockSensors();
{
for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
total_sensors += SDL_sensor_drivers[i]->GetCount();
}
if (count) {
*count = total_sensors;
}
sensors = (SDL_SensorID *)SDL_malloc((total_sensors + 1) * sizeof(*sensors));
if (sensors) {
for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
num_sensors = SDL_sensor_drivers[i]->GetCount();
for (device_index = 0; device_index < num_sensors; ++device_index) {
SDL_assert(sensor_index < total_sensors);
sensors[sensor_index] = SDL_sensor_drivers[i]->GetDeviceInstanceID(device_index);
SDL_assert(sensors[sensor_index] > 0);
++sensor_index;
}
}
SDL_assert(sensor_index == total_sensors);
sensors[sensor_index] = 0;
} else {
SDL_OutOfMemory();
}
}
SDL_UnlockSensors();
return sensors;
}
/*
@@ -107,44 +146,45 @@ int SDL_GetNumSensors(void)
*/
SDL_SensorID SDL_GetNextSensorInstanceID()
{
return SDL_AtomicIncRef(&SDL_next_sensor_instance_id);
return SDL_AtomicIncRef(&SDL_last_sensor_instance_id) + 1;
}
/*
* Get the driver and device index for an API device index
* Get the driver and device index for a sensor instance ID
* This should be called while the sensor lock is held, to prevent another thread from updating the list
*/
static SDL_bool SDL_GetDriverAndSensorIndex(int device_index, SDL_SensorDriver **driver, int *driver_index)
static SDL_bool SDL_GetDriverAndSensorIndex(SDL_SensorID instance_id, SDL_SensorDriver **driver, int *driver_index)
{
int i, num_sensors, total_sensors = 0;
int i, num_sensors, device_index;
if (device_index >= 0) {
if (instance_id > 0) {
for (i = 0; i < SDL_arraysize(SDL_sensor_drivers); ++i) {
num_sensors = SDL_sensor_drivers[i]->GetCount();
if (device_index < num_sensors) {
*driver = SDL_sensor_drivers[i];
*driver_index = device_index;
return SDL_TRUE;
for (device_index = 0; device_index < num_sensors; ++device_index) {
SDL_SensorID sensor_id = SDL_sensor_drivers[i]->GetDeviceInstanceID(device_index);
if (sensor_id == instance_id) {
*driver = SDL_sensor_drivers[i];
*driver_index = device_index;
return SDL_TRUE;
}
}
device_index -= num_sensors;
total_sensors += num_sensors;
}
}
SDL_SetError("There are %d sensors available", total_sensors);
SDL_SetError("Sensor %" SDL_PRIs32 " not found", instance_id);
return SDL_FALSE;
}
/*
* Get the implementation dependent name of a sensor
*/
const char *SDL_GetSensorDeviceName(int device_index)
const char *SDL_GetSensorInstanceName(SDL_SensorID instance_id)
{
SDL_SensorDriver *driver;
int device_index;
const char *name = NULL;
SDL_LockSensors();
if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
name = driver->GetDeviceName(device_index);
}
SDL_UnlockSensors();
@@ -153,13 +193,14 @@ const char *SDL_GetSensorDeviceName(int device_index)
return name;
}
SDL_SensorType SDL_GetSensorDeviceType(int device_index)
SDL_SensorType SDL_GetSensorInstanceType(SDL_SensorID instance_id)
{
SDL_SensorDriver *driver;
int device_index;
SDL_SensorType type = SDL_SENSOR_INVALID;
SDL_LockSensors();
if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
type = driver->GetDeviceType(device_index);
}
SDL_UnlockSensors();
@@ -167,13 +208,14 @@ SDL_SensorType SDL_GetSensorDeviceType(int device_index)
return type;
}
int SDL_GetSensorDeviceNonPortableType(int device_index)
int SDL_GetSensorInstanceNonPortableType(SDL_SensorID instance_id)
{
SDL_SensorDriver *driver;
int device_index;
int type = -1;
SDL_LockSensors();
if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
if (SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
type = driver->GetDeviceNonPortableType(device_index);
}
SDL_UnlockSensors();
@@ -181,20 +223,6 @@ int SDL_GetSensorDeviceNonPortableType(int device_index)
return type;
}
SDL_SensorID SDL_GetSensorDeviceInstanceID(int device_index)
{
SDL_SensorDriver *driver;
SDL_SensorID instance_id = -1;
SDL_LockSensors();
if (SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
instance_id = driver->GetDeviceInstanceID(device_index);
}
SDL_UnlockSensors();
return instance_id;
}
/*
* Open a sensor for use - the index passed as an argument refers to
* the N'th sensor on the system. This index is the value which will
@@ -202,17 +230,17 @@ SDL_SensorID SDL_GetSensorDeviceInstanceID(int device_index)
*
* This function returns a sensor identifier, or NULL if an error occurred.
*/
SDL_Sensor *SDL_OpenSensor(int device_index)
SDL_Sensor *SDL_OpenSensor(SDL_SensorID instance_id)
{
SDL_SensorDriver *driver;
SDL_SensorID instance_id;
int device_index;
SDL_Sensor *sensor;
SDL_Sensor *sensorlist;
const char *sensorname = NULL;
SDL_LockSensors();
if (!SDL_GetDriverAndSensorIndex(device_index, &driver, &device_index)) {
if (!SDL_GetDriverAndSensorIndex(instance_id, &driver, &device_index)) {
SDL_UnlockSensors();
return NULL;
}
@@ -221,7 +249,6 @@ SDL_Sensor *SDL_OpenSensor(int device_index)
/* If the sensor is already open, return it
* it is important that we have a single sensor * for each instance id
*/
instance_id = driver->GetDeviceInstanceID(device_index);
while (sensorlist) {
if (instance_id == sensorlist->instance_id) {
sensor = sensorlist;
@@ -263,10 +290,10 @@ SDL_Sensor *SDL_OpenSensor(int device_index)
sensor->next = SDL_sensors;
SDL_sensors = sensor;
SDL_UnlockSensors();
driver->Update(sensor);
SDL_UnlockSensors();
return sensor;
}

View File

@@ -34,6 +34,9 @@ extern SDL_SensorID SDL_GetNextSensorInstanceID(void);
extern int SDL_InitSensors(void);
extern void SDL_QuitSensors(void);
extern void SDL_LockSensors(void);
extern void SDL_UnlockSensors(void);
/* Internal event queueing functions */
extern int SDL_SendSensorUpdate(Uint64 timestamp, SDL_Sensor *sensor, Uint64 sensor_timestamp, float *data, int num_values);

View File

@@ -1376,7 +1376,7 @@ static const char *DisplayOrientationName(int orientation)
}
}
static const char *ControllerAxisName(const SDL_GamepadAxis axis)
static const char *GamepadAxisName(const SDL_GamepadAxis axis)
{
switch (axis) {
#define AXIS_CASE(ax) \
@@ -1395,7 +1395,7 @@ static const char *ControllerAxisName(const SDL_GamepadAxis axis)
}
}
static const char *ControllerButtonName(const SDL_GamepadButton button)
static const char *GamepadButtonName(const SDL_GamepadButton button)
{
switch (button) {
#define BUTTON_CASE(btn) \
@@ -1548,11 +1548,11 @@ static void SDLTest_PrintEvent(SDL_Event *event)
event->wheel.x, event->wheel.y, event->wheel.direction, event->wheel.windowID);
break;
case SDL_JOYDEVICEADDED:
SDL_Log("SDL EVENT: Joystick index %" SDL_PRIs32 " attached",
SDL_Log("SDL EVENT: Joystick index %" SDL_PRIu32 " attached",
event->jdevice.which);
break;
case SDL_JOYDEVICEREMOVED:
SDL_Log("SDL EVENT: Joystick %" SDL_PRIs32 " removed",
SDL_Log("SDL EVENT: Joystick %" SDL_PRIu32 " removed",
event->jdevice.which);
break;
case SDL_JOYHATMOTION:
@@ -1587,41 +1587,41 @@ static void SDLTest_PrintEvent(SDL_Event *event)
position = "LEFTUP";
break;
}
SDL_Log("SDL EVENT: Joystick %" SDL_PRIs32 ": hat %d moved to %s",
SDL_Log("SDL EVENT: Joystick %" SDL_PRIu32 ": hat %d moved to %s",
event->jhat.which, event->jhat.hat, position);
} break;
case SDL_JOYBUTTONDOWN:
SDL_Log("SDL EVENT: Joystick %" SDL_PRIs32 ": button %d pressed",
SDL_Log("SDL EVENT: Joystick %" SDL_PRIu32 ": button %d pressed",
event->jbutton.which, event->jbutton.button);
break;
case SDL_JOYBUTTONUP:
SDL_Log("SDL EVENT: Joystick %" SDL_PRIs32 ": button %d released",
SDL_Log("SDL EVENT: Joystick %" SDL_PRIu32 ": button %d released",
event->jbutton.which, event->jbutton.button);
break;
case SDL_GAMEPADADDED:
SDL_Log("SDL EVENT: Controller index %" SDL_PRIs32 " attached",
SDL_Log("SDL EVENT: Gamepad index %" SDL_PRIu32 " attached",
event->cdevice.which);
break;
case SDL_GAMEPADREMOVED:
SDL_Log("SDL EVENT: Controller %" SDL_PRIs32 " removed",
SDL_Log("SDL EVENT: Gamepad %" SDL_PRIu32 " removed",
event->cdevice.which);
break;
case SDL_GAMEPADAXISMOTION:
SDL_Log("SDL EVENT: Controller %" SDL_PRIs32 " axis %d ('%s') value: %d",
SDL_Log("SDL EVENT: Gamepad %" SDL_PRIu32 " axis %d ('%s') value: %d",
event->caxis.which,
event->caxis.axis,
ControllerAxisName((SDL_GamepadAxis)event->caxis.axis),
GamepadAxisName((SDL_GamepadAxis)event->caxis.axis),
event->caxis.value);
break;
case SDL_GAMEPADBUTTONDOWN:
SDL_Log("SDL EVENT: Controller %" SDL_PRIs32 "button %d ('%s') down",
SDL_Log("SDL EVENT: Gamepad %" SDL_PRIu32 "button %d ('%s') down",
event->cbutton.which, event->cbutton.button,
ControllerButtonName((SDL_GamepadButton)event->cbutton.button));
GamepadButtonName((SDL_GamepadButton)event->cbutton.button));
break;
case SDL_GAMEPADBUTTONUP:
SDL_Log("SDL EVENT: Controller %" SDL_PRIs32 " button %d ('%s') up",
SDL_Log("SDL EVENT: Gamepad %" SDL_PRIu32 " button %d ('%s') up",
event->cbutton.which, event->cbutton.button,
ControllerButtonName((SDL_GamepadButton)event->cbutton.button));
GamepadButtonName((SDL_GamepadButton)event->cbutton.button));
break;
case SDL_CLIPBOARDUPDATE:
SDL_Log("SDL EVENT: Clipboard updated");