From 5a079a2d114f96d4847d1ee305d5b7c16eeec50e Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 27 Dec 2025 12:03:39 -0800 Subject: Initial commit --- contrib/SDL-3.2.8/src/haptic/SDL_haptic.c | 788 +++++++++++ contrib/SDL-3.2.8/src/haptic/SDL_haptic_c.h | 28 + contrib/SDL-3.2.8/src/haptic/SDL_syshaptic.h | 194 +++ .../SDL-3.2.8/src/haptic/android/SDL_syshaptic.c | 307 +++++ .../SDL-3.2.8/src/haptic/android/SDL_syshaptic_c.h | 28 + .../SDL-3.2.8/src/haptic/darwin/SDL_syshaptic.c | 1373 ++++++++++++++++++++ .../SDL-3.2.8/src/haptic/darwin/SDL_syshaptic_c.h | 29 + contrib/SDL-3.2.8/src/haptic/dummy/SDL_syshaptic.c | 151 +++ contrib/SDL-3.2.8/src/haptic/linux/SDL_syshaptic.c | 1134 ++++++++++++++++ .../src/haptic/windows/SDL_dinputhaptic.c | 1244 ++++++++++++++++++ .../src/haptic/windows/SDL_dinputhaptic_c.h | 53 + .../src/haptic/windows/SDL_windowshaptic.c | 369 ++++++ .../src/haptic/windows/SDL_windowshaptic_c.h | 87 ++ 13 files changed, 5785 insertions(+) create mode 100644 contrib/SDL-3.2.8/src/haptic/SDL_haptic.c create mode 100644 contrib/SDL-3.2.8/src/haptic/SDL_haptic_c.h create mode 100644 contrib/SDL-3.2.8/src/haptic/SDL_syshaptic.h create mode 100644 contrib/SDL-3.2.8/src/haptic/android/SDL_syshaptic.c create mode 100644 contrib/SDL-3.2.8/src/haptic/android/SDL_syshaptic_c.h create mode 100644 contrib/SDL-3.2.8/src/haptic/darwin/SDL_syshaptic.c create mode 100644 contrib/SDL-3.2.8/src/haptic/darwin/SDL_syshaptic_c.h create mode 100644 contrib/SDL-3.2.8/src/haptic/dummy/SDL_syshaptic.c create mode 100644 contrib/SDL-3.2.8/src/haptic/linux/SDL_syshaptic.c create mode 100644 contrib/SDL-3.2.8/src/haptic/windows/SDL_dinputhaptic.c create mode 100644 contrib/SDL-3.2.8/src/haptic/windows/SDL_dinputhaptic_c.h create mode 100644 contrib/SDL-3.2.8/src/haptic/windows/SDL_windowshaptic.c create mode 100644 contrib/SDL-3.2.8/src/haptic/windows/SDL_windowshaptic_c.h (limited to 'contrib/SDL-3.2.8/src/haptic') diff --git a/contrib/SDL-3.2.8/src/haptic/SDL_haptic.c b/contrib/SDL-3.2.8/src/haptic/SDL_haptic.c new file mode 100644 index 0000000..1c11db6 --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/SDL_haptic.c @@ -0,0 +1,788 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#include "SDL_syshaptic.h" +#include "SDL_haptic_c.h" +#include "../joystick/SDL_joystick_c.h" // For SDL_IsJoystickValid +#include "../SDL_hints_c.h" + +typedef struct SDL_Haptic_VIDPID_Naxes { + Uint16 vid; + Uint16 pid; + Uint16 naxes; +} SDL_Haptic_VIDPID_Naxes; + +static void SDL_Haptic_Load_Axes_List(SDL_Haptic_VIDPID_Naxes **entries, int *num_entries) +{ + SDL_Haptic_VIDPID_Naxes entry; + const char *spot; + int length = 0; + + spot = SDL_GetHint(SDL_HINT_JOYSTICK_HAPTIC_AXES); + if (!spot) + return; + + while (SDL_sscanf(spot, "0x%hx/0x%hx/%hu%n", &entry.vid, &entry.pid, &entry.naxes, &length) == 3) { + SDL_assert(length > 0); + spot += length; + length = 0; + + if ((*num_entries % 8) == 0) { + int new_max = *num_entries + 8; + SDL_Haptic_VIDPID_Naxes *new_entries = + (SDL_Haptic_VIDPID_Naxes *)SDL_realloc(*entries, new_max * sizeof(**entries)); + + // Out of memory, go with what we have already + if (!new_entries) + break; + + *entries = new_entries; + } + (*entries)[(*num_entries)++] = entry; + + if (spot[0] == ',') + spot++; + } +} + +// /* Return -1 if not found */ +static int SDL_Haptic_Naxes_List_Index(struct SDL_Haptic_VIDPID_Naxes *entries, int num_entries, Uint16 vid, Uint16 pid) +{ + if (!entries) + return -1; + + int i; + for (i = 0; i < num_entries; ++i) { + if (entries[i].vid == vid && entries[i].pid == pid) + return i; + } + + return -1; +} + +// Check if device needs a custom number of naxes +static int SDL_Haptic_Get_Naxes(Uint16 vid, Uint16 pid) +{ + int num_entries = 0, index = 0, naxes = -1; + SDL_Haptic_VIDPID_Naxes *naxes_list = NULL; + + SDL_Haptic_Load_Axes_List(&naxes_list, &num_entries); + if (!num_entries || !naxes_list) + return -1; + + // Perform "wildcard" pass + index = SDL_Haptic_Naxes_List_Index(naxes_list, num_entries, 0xffff, 0xffff); + if (index >= 0) + naxes = naxes_list[index].naxes; + + index = SDL_Haptic_Naxes_List_Index(naxes_list, num_entries, vid, pid); + if (index >= 0) + naxes = naxes_list[index].naxes; + + SDL_free(naxes_list); + return naxes; +} + +static SDL_Haptic *SDL_haptics = NULL; + +#define CHECK_HAPTIC_MAGIC(haptic, result) \ + if (!SDL_ObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC)) { \ + SDL_InvalidParamError("haptic"); \ + return result; \ + } + +bool SDL_InitHaptics(void) +{ + return SDL_SYS_HapticInit(); +} + +static bool SDL_GetHapticIndex(SDL_HapticID instance_id, int *driver_index) +{ + int num_haptics, device_index; + + if (instance_id > 0) { + num_haptics = SDL_SYS_NumHaptics(); + for (device_index = 0; device_index < num_haptics; ++device_index) { + SDL_HapticID haptic_id = SDL_SYS_HapticInstanceID(device_index); + if (haptic_id == instance_id) { + *driver_index = device_index; + return true; + } + } + } + + SDL_SetError("Haptic device %" SDL_PRIu32 " not found", instance_id); + return false; +} + +SDL_HapticID *SDL_GetHaptics(int *count) +{ + int device_index; + int haptic_index = 0, num_haptics = 0; + SDL_HapticID *haptics; + + num_haptics = SDL_SYS_NumHaptics(); + + haptics = (SDL_HapticID *)SDL_malloc((num_haptics + 1) * sizeof(*haptics)); + if (haptics) { + if (count) { + *count = num_haptics; + } + + for (device_index = 0; device_index < num_haptics; ++device_index) { + haptics[haptic_index] = SDL_SYS_HapticInstanceID(device_index); + SDL_assert(haptics[haptic_index] > 0); + ++haptic_index; + } + haptics[haptic_index] = 0; + } else { + if (count) { + *count = 0; + } + } + + return haptics; +} + +const char *SDL_GetHapticNameForID(SDL_HapticID instance_id) +{ + int device_index; + const char *name = NULL; + + if (SDL_GetHapticIndex(instance_id, &device_index)) { + name = SDL_GetPersistentString(SDL_SYS_HapticName(device_index)); + } + return name; +} + +SDL_Haptic *SDL_OpenHaptic(SDL_HapticID instance_id) +{ + SDL_Haptic *haptic; + SDL_Haptic *hapticlist; + const char *name; + int device_index = 0; + + if (!SDL_GetHapticIndex(instance_id, &device_index)) { + return NULL; + } + + hapticlist = SDL_haptics; + /* If the haptic device is already open, return it + * it is important that we have a single haptic device for each instance id + */ + while (hapticlist) { + if (instance_id == hapticlist->instance_id) { + haptic = hapticlist; + ++haptic->ref_count; + return haptic; + } + hapticlist = hapticlist->next; + } + + // Create the haptic device + haptic = (SDL_Haptic *)SDL_calloc(1, sizeof(*haptic)); + if (!haptic) { + return NULL; + } + + // Initialize the haptic device + SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true); + haptic->instance_id = instance_id; + haptic->rumble_id = -1; + if (!SDL_SYS_HapticOpen(haptic)) { + SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, false); + SDL_free(haptic); + return NULL; + } + + if (!haptic->name) { + name = SDL_SYS_HapticName(device_index); + if (name) { + haptic->name = SDL_strdup(name); + } + } + + // Add haptic to list + ++haptic->ref_count; + // Link the haptic in the list + haptic->next = SDL_haptics; + SDL_haptics = haptic; + + // Disable autocenter and set gain to max. + if (haptic->supported & SDL_HAPTIC_GAIN) { + SDL_SetHapticGain(haptic, 100); + } + if (haptic->supported & SDL_HAPTIC_AUTOCENTER) { + SDL_SetHapticAutocenter(haptic, 0); + } + + return haptic; +} + +SDL_Haptic *SDL_GetHapticFromID(SDL_HapticID instance_id) +{ + SDL_Haptic *haptic; + + for (haptic = SDL_haptics; haptic; haptic = haptic->next) { + if (instance_id == haptic->instance_id) { + break; + } + } + return haptic; +} + +SDL_HapticID SDL_GetHapticID(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, 0); + + return haptic->instance_id; +} + +const char *SDL_GetHapticName(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, NULL); + + return SDL_GetPersistentString(haptic->name); +} + +bool SDL_IsMouseHaptic(void) +{ + if (SDL_SYS_HapticMouse() < 0) { + return false; + } + return true; +} + +SDL_Haptic *SDL_OpenHapticFromMouse(void) +{ + int device_index; + + device_index = SDL_SYS_HapticMouse(); + + if (device_index < 0) { + SDL_SetError("Haptic: Mouse isn't a haptic device."); + return NULL; + } + + return SDL_OpenHaptic(device_index); +} + +bool SDL_IsJoystickHaptic(SDL_Joystick *joystick) +{ + bool result = false; + + SDL_LockJoysticks(); + { + // Must be a valid joystick + if (SDL_IsJoystickValid(joystick) && + !SDL_IsGamepad(SDL_GetJoystickID(joystick))) { + result = SDL_SYS_JoystickIsHaptic(joystick); + } + } + SDL_UnlockJoysticks(); + + return result; +} + +SDL_Haptic *SDL_OpenHapticFromJoystick(SDL_Joystick *joystick) +{ + SDL_Haptic *haptic; + SDL_Haptic *hapticlist; + + SDL_LockJoysticks(); + { + // Must be a valid joystick + if (!SDL_IsJoystickValid(joystick)) { + SDL_SetError("Haptic: Joystick isn't valid."); + SDL_UnlockJoysticks(); + return NULL; + } + + // Joystick must be haptic + if (SDL_IsGamepad(SDL_GetJoystickID(joystick)) || + !SDL_SYS_JoystickIsHaptic(joystick)) { + SDL_SetError("Haptic: Joystick isn't a haptic device."); + SDL_UnlockJoysticks(); + return NULL; + } + + hapticlist = SDL_haptics; + // Check to see if joystick's haptic is already open + while (hapticlist) { + if (SDL_SYS_JoystickSameHaptic(hapticlist, joystick)) { + haptic = hapticlist; + ++haptic->ref_count; + SDL_UnlockJoysticks(); + return haptic; + } + hapticlist = hapticlist->next; + } + + // Create the haptic device + haptic = (SDL_Haptic *)SDL_calloc(1, sizeof(*haptic)); + if (!haptic) { + SDL_UnlockJoysticks(); + return NULL; + } + + /* Initialize the haptic device + * This function should fill in the instance ID and name. + */ + SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, true); + haptic->rumble_id = -1; + if (!SDL_SYS_HapticOpenFromJoystick(haptic, joystick)) { + SDL_SetError("Haptic: SDL_SYS_HapticOpenFromJoystick failed."); + SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, false); + SDL_free(haptic); + SDL_UnlockJoysticks(); + return NULL; + } + SDL_assert(haptic->instance_id != 0); + } + SDL_UnlockJoysticks(); + + // Check if custom number of haptic axes was defined + Uint16 vid = SDL_GetJoystickVendor(joystick); + Uint16 pid = SDL_GetJoystickProduct(joystick); + int general_axes = SDL_GetNumJoystickAxes(joystick); + + int naxes = SDL_Haptic_Get_Naxes(vid, pid); + if (naxes > 0) + haptic->naxes = naxes; + + // Limit to the actual number of axes found on the device + if (general_axes >= 0 && naxes > general_axes) + haptic->naxes = general_axes; + + // Add haptic to list + ++haptic->ref_count; + // Link the haptic in the list + haptic->next = SDL_haptics; + SDL_haptics = haptic; + + return haptic; +} + +void SDL_CloseHaptic(SDL_Haptic *haptic) +{ + int i; + SDL_Haptic *hapticlist; + SDL_Haptic *hapticlistprev; + + CHECK_HAPTIC_MAGIC(haptic,); + + // Check if it's still in use + if (--haptic->ref_count > 0) { + return; + } + + // Close it, properly removing effects if needed + for (i = 0; i < haptic->neffects; i++) { + if (haptic->effects[i].hweffect != NULL) { + SDL_DestroyHapticEffect(haptic, i); + } + } + SDL_SYS_HapticClose(haptic); + SDL_SetObjectValid(haptic, SDL_OBJECT_TYPE_HAPTIC, false); + + // Remove from the list + hapticlist = SDL_haptics; + hapticlistprev = NULL; + while (hapticlist) { + if (haptic == hapticlist) { + if (hapticlistprev) { + // unlink this entry + hapticlistprev->next = hapticlist->next; + } else { + SDL_haptics = haptic->next; + } + + break; + } + hapticlistprev = hapticlist; + hapticlist = hapticlist->next; + } + + // Free the data associated with this device + SDL_free(haptic->name); + SDL_free(haptic); +} + +void SDL_QuitHaptics(void) +{ + while (SDL_haptics) { + SDL_CloseHaptic(SDL_haptics); + } + + SDL_SYS_HapticQuit(); +} + +int SDL_GetMaxHapticEffects(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, -1); + + return haptic->neffects; +} + +int SDL_GetMaxHapticEffectsPlaying(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, -1); + + return haptic->nplaying; +} + +Uint32 SDL_GetHapticFeatures(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, 0); + + return haptic->supported; +} + +int SDL_GetNumHapticAxes(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, -1); + + return haptic->naxes; +} + +bool SDL_HapticEffectSupported(SDL_Haptic *haptic, const SDL_HapticEffect *effect) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + if (!effect) { + return false; + } + + if ((haptic->supported & effect->type) != 0) { + return true; + } + return false; +} + +int SDL_CreateHapticEffect(SDL_Haptic *haptic, const SDL_HapticEffect *effect) +{ + int i; + + CHECK_HAPTIC_MAGIC(haptic, -1); + + if (!effect) { + SDL_InvalidParamError("effect"); + return -1; + } + + // Check to see if effect is supported + if (SDL_HapticEffectSupported(haptic, effect) == false) { + SDL_SetError("Haptic: Effect not supported by haptic device."); + return -1; + } + + // See if there's a free slot + for (i = 0; i < haptic->neffects; i++) { + if (haptic->effects[i].hweffect == NULL) { + + // Now let the backend create the real effect + if (!SDL_SYS_HapticNewEffect(haptic, &haptic->effects[i], effect)) { + return -1; // Backend failed to create effect + } + + SDL_memcpy(&haptic->effects[i].effect, effect, + sizeof(SDL_HapticEffect)); + return i; + } + } + + SDL_SetError("Haptic: Device has no free space left."); + return -1; +} + +static bool ValidEffect(SDL_Haptic *haptic, int effect) +{ + if ((effect < 0) || (effect >= haptic->neffects)) { + SDL_SetError("Haptic: Invalid effect identifier."); + return false; + } + return true; +} + +bool SDL_UpdateHapticEffect(SDL_Haptic *haptic, int effect, const SDL_HapticEffect *data) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + if (!ValidEffect(haptic, effect)) { + return false; + } + + if (!data) { + return SDL_InvalidParamError("data"); + } + + // Can't change type dynamically. + if (data->type != haptic->effects[effect].effect.type) { + return SDL_SetError("Haptic: Updating effect type is illegal."); + } + + // Updates the effect + if (!SDL_SYS_HapticUpdateEffect(haptic, &haptic->effects[effect], data)) { + return false; + } + + SDL_memcpy(&haptic->effects[effect].effect, data, + sizeof(SDL_HapticEffect)); + return true; +} + +bool SDL_RunHapticEffect(SDL_Haptic *haptic, int effect, Uint32 iterations) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + if (!ValidEffect(haptic, effect)) { + return false; + } + + // Run the effect + if (!SDL_SYS_HapticRunEffect(haptic, &haptic->effects[effect], iterations)) { + return false; + } + + return true; +} + +bool SDL_StopHapticEffect(SDL_Haptic *haptic, int effect) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + if (!ValidEffect(haptic, effect)) { + return false; + } + + // Stop the effect + if (!SDL_SYS_HapticStopEffect(haptic, &haptic->effects[effect])) { + return false; + } + + return true; +} + +void SDL_DestroyHapticEffect(SDL_Haptic *haptic, int effect) +{ + CHECK_HAPTIC_MAGIC(haptic,); + + if (!ValidEffect(haptic, effect)) { + return; + } + + // Not allocated + if (haptic->effects[effect].hweffect == NULL) { + return; + } + + SDL_SYS_HapticDestroyEffect(haptic, &haptic->effects[effect]); +} + +bool SDL_GetHapticEffectStatus(SDL_Haptic *haptic, int effect) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + if (!ValidEffect(haptic, effect)) { + return false; + } + + if (!(haptic->supported & SDL_HAPTIC_STATUS)) { + return SDL_SetError("Haptic: Device does not support status queries."); + } + + SDL_ClearError(); + + return (SDL_SYS_HapticGetEffectStatus(haptic, &haptic->effects[effect]) > 0); +} + +bool SDL_SetHapticGain(SDL_Haptic *haptic, int gain) +{ + const char *env; + int real_gain, max_gain; + + CHECK_HAPTIC_MAGIC(haptic, false); + + if (!(haptic->supported & SDL_HAPTIC_GAIN)) { + return SDL_SetError("Haptic: Device does not support setting gain."); + } + + if ((gain < 0) || (gain > 100)) { + return SDL_SetError("Haptic: Gain must be between 0 and 100."); + } + + // The user can use an environment variable to override the max gain. + env = SDL_getenv("SDL_HAPTIC_GAIN_MAX"); + if (env) { + max_gain = SDL_atoi(env); + + // Check for sanity. + if (max_gain < 0) { + max_gain = 0; + } else if (max_gain > 100) { + max_gain = 100; + } + + // We'll scale it linearly with SDL_HAPTIC_GAIN_MAX + real_gain = (gain * max_gain) / 100; + } else { + real_gain = gain; + } + + return SDL_SYS_HapticSetGain(haptic, real_gain); +} + +bool SDL_SetHapticAutocenter(SDL_Haptic *haptic, int autocenter) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + if (!(haptic->supported & SDL_HAPTIC_AUTOCENTER)) { + return SDL_SetError("Haptic: Device does not support setting autocenter."); + } + + if ((autocenter < 0) || (autocenter > 100)) { + return SDL_SetError("Haptic: Autocenter must be between 0 and 100."); + } + + return SDL_SYS_HapticSetAutocenter(haptic, autocenter); +} + +bool SDL_PauseHaptic(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + if (!(haptic->supported & SDL_HAPTIC_PAUSE)) { + return SDL_SetError("Haptic: Device does not support setting pausing."); + } + + return SDL_SYS_HapticPause(haptic); +} + +bool SDL_ResumeHaptic(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + if (!(haptic->supported & SDL_HAPTIC_PAUSE)) { + return true; // Not going to be paused, so we pretend it's unpaused. + } + + return SDL_SYS_HapticResume(haptic); +} + +bool SDL_StopHapticEffects(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + return SDL_SYS_HapticStopAll(haptic); +} + +bool SDL_HapticRumbleSupported(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + // Most things can use SINE, but XInput only has LEFTRIGHT. + return (haptic->supported & (SDL_HAPTIC_SINE | SDL_HAPTIC_LEFTRIGHT)) != 0; +} + +bool SDL_InitHapticRumble(SDL_Haptic *haptic) +{ + SDL_HapticEffect *efx = &haptic->rumble_effect; + + CHECK_HAPTIC_MAGIC(haptic, false); + + // Already allocated. + if (haptic->rumble_id >= 0) { + return true; + } + + SDL_zerop(efx); + if (haptic->supported & SDL_HAPTIC_SINE) { + efx->type = SDL_HAPTIC_SINE; + efx->periodic.direction.type = SDL_HAPTIC_CARTESIAN; + efx->periodic.period = 1000; + efx->periodic.magnitude = 0x4000; + efx->periodic.length = 5000; + efx->periodic.attack_length = 0; + efx->periodic.fade_length = 0; + } else if (haptic->supported & SDL_HAPTIC_LEFTRIGHT) { // XInput? + efx->type = SDL_HAPTIC_LEFTRIGHT; + efx->leftright.length = 5000; + efx->leftright.large_magnitude = 0x4000; + efx->leftright.small_magnitude = 0x4000; + } else { + return SDL_SetError("Device doesn't support rumble"); + } + + haptic->rumble_id = SDL_CreateHapticEffect(haptic, &haptic->rumble_effect); + if (haptic->rumble_id >= 0) { + return true; + } + return false; +} + +bool SDL_PlayHapticRumble(SDL_Haptic *haptic, float strength, Uint32 length) +{ + SDL_HapticEffect *efx; + Sint16 magnitude; + + CHECK_HAPTIC_MAGIC(haptic, false); + + if (haptic->rumble_id < 0) { + return SDL_SetError("Haptic: Rumble effect not initialized on haptic device"); + } + + // Clamp strength. + if (strength > 1.0f) { + strength = 1.0f; + } else if (strength < 0.0f) { + strength = 0.0f; + } + magnitude = (Sint16)(32767.0f * strength); + + efx = &haptic->rumble_effect; + if (efx->type == SDL_HAPTIC_SINE) { + efx->periodic.magnitude = magnitude; + efx->periodic.length = length; + } else if (efx->type == SDL_HAPTIC_LEFTRIGHT) { + efx->leftright.small_magnitude = efx->leftright.large_magnitude = magnitude; + efx->leftright.length = length; + } else { + SDL_assert(!"This should have been caught elsewhere"); + } + + if (!SDL_UpdateHapticEffect(haptic, haptic->rumble_id, &haptic->rumble_effect)) { + return false; + } + + return SDL_RunHapticEffect(haptic, haptic->rumble_id, 1); +} + +bool SDL_StopHapticRumble(SDL_Haptic *haptic) +{ + CHECK_HAPTIC_MAGIC(haptic, false); + + if (haptic->rumble_id < 0) { + return SDL_SetError("Haptic: Rumble effect not initialized on haptic device"); + } + + return SDL_StopHapticEffect(haptic, haptic->rumble_id); +} diff --git a/contrib/SDL-3.2.8/src/haptic/SDL_haptic_c.h b/contrib/SDL-3.2.8/src/haptic/SDL_haptic_c.h new file mode 100644 index 0000000..3f5ce87 --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/SDL_haptic_c.h @@ -0,0 +1,28 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef SDL_haptic_c_h_ +#define SDL_haptic_c_h_ + +extern bool SDL_InitHaptics(void); +extern void SDL_QuitHaptics(void); + +#endif // SDL_haptic_c_h_ diff --git a/contrib/SDL-3.2.8/src/haptic/SDL_syshaptic.h b/contrib/SDL-3.2.8/src/haptic/SDL_syshaptic.h new file mode 100644 index 0000000..ec60a71 --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/SDL_syshaptic.h @@ -0,0 +1,194 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "SDL_internal.h" + +#ifndef SDL_syshaptic_h_ +#define SDL_syshaptic_h_ + +// Set up for C function definitions, even when using C++ +#ifdef __cplusplus +extern "C" { +#endif + +struct haptic_effect +{ + SDL_HapticEffect effect; // The current event + struct haptic_hweffect *hweffect; // The hardware behind the event +}; + +/* + * The real SDL_Haptic struct. + */ +struct SDL_Haptic +{ + SDL_HapticID instance_id; // Device instance, monotonically increasing from 0 + char *name; // Device name - system dependent + + struct haptic_effect *effects; // Allocated effects + int neffects; // Maximum amount of effects + int nplaying; // Maximum amount of effects to play at the same time + Uint32 supported; // Supported effects and features + int naxes; // Number of axes on the device. + + struct haptic_hwdata *hwdata; // Driver dependent + int ref_count; // Count for multiple opens + + int rumble_id; // ID of rumble effect for simple rumble API. + SDL_HapticEffect rumble_effect; // Rumble effect. + struct SDL_Haptic *next; // pointer to next haptic we have allocated +}; + +/* + * Scans the system for haptic devices. + * + * Returns number of devices on success, -1 on error. + */ +extern bool SDL_SYS_HapticInit(void); + +// Function to return the number of haptic devices plugged in right now +extern int SDL_SYS_NumHaptics(void); + +/* + * Gets the instance ID of the haptic device + */ +extern SDL_HapticID SDL_SYS_HapticInstanceID(int index); + +/* + * Gets the device dependent name of the haptic device + */ +extern const char *SDL_SYS_HapticName(int index); + +/* + * Opens the haptic device for usage. The haptic device should have + * the index value set previously. + */ +extern bool SDL_SYS_HapticOpen(SDL_Haptic *haptic); + +/* + * Returns the index of the haptic core pointer or -1 if none is found. + */ +extern int SDL_SYS_HapticMouse(void); + +/* + * Checks to see if the joystick has haptic capabilities. + */ +extern bool SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick); + +/* + * Opens the haptic device for usage using the same device as + * the joystick. + */ +extern bool SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, + SDL_Joystick *joystick); +/* + * Checks to see if haptic device and joystick device are the same. + * + * Returns true if they are the same, false if they aren't. + */ +extern bool SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, + SDL_Joystick *joystick); + +/* + * Closes a haptic device after usage. + */ +extern void SDL_SYS_HapticClose(SDL_Haptic *haptic); + +/* + * Performs a cleanup on the haptic subsystem. + */ +extern void SDL_SYS_HapticQuit(void); + +/* + * Creates a new haptic effect on the haptic device using base + * as a template for the effect. + */ +extern bool SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, + struct haptic_effect *effect, + const SDL_HapticEffect *base); + +/* + * Updates the haptic effect on the haptic device using data + * as a template. + */ +extern bool SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, + struct haptic_effect *effect, + const SDL_HapticEffect *data); + +/* + * Runs the effect on the haptic device. + */ +extern bool SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, + struct haptic_effect *effect, + Uint32 iterations); + +/* + * Stops the effect on the haptic device. + */ +extern bool SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, + struct haptic_effect *effect); + +/* + * Cleanups up the effect on the haptic device. + */ +extern void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, + struct haptic_effect *effect); + +/* + * Queries the device for the status of effect. + * + * Returns 0 if device is stopped, >0 if device is playing and + * -1 on error. + */ +extern int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic, + struct haptic_effect *effect); + +/* + * Sets the global gain of the haptic device. + */ +extern bool SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain); + +/* + * Sets the autocenter feature of the haptic device. + */ +extern bool SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter); + +/* + * Pauses the haptic device. + */ +extern bool SDL_SYS_HapticPause(SDL_Haptic *haptic); + +/* + * Unpauses the haptic device. + */ +extern bool SDL_SYS_HapticResume(SDL_Haptic *haptic); + +/* + * Stops all the currently playing haptic effects on the device. + */ +extern bool SDL_SYS_HapticStopAll(SDL_Haptic *haptic); + +// Ends C function definitions when using C++ +#ifdef __cplusplus +} +#endif + +#endif // SDL_syshaptic_h_ diff --git a/contrib/SDL-3.2.8/src/haptic/android/SDL_syshaptic.c b/contrib/SDL-3.2.8/src/haptic/android/SDL_syshaptic.c new file mode 100644 index 0000000..d155dbc --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/android/SDL_syshaptic.c @@ -0,0 +1,307 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_HAPTIC_ANDROID + +#include "SDL_syshaptic_c.h" +#include "../SDL_syshaptic.h" +#include "../../core/android/SDL_android.h" + +typedef struct SDL_hapticlist_item +{ + SDL_HapticID instance_id; + int device_id; + char *name; + SDL_Haptic *haptic; + struct SDL_hapticlist_item *next; +} SDL_hapticlist_item; + +static SDL_hapticlist_item *SDL_hapticlist = NULL; +static SDL_hapticlist_item *SDL_hapticlist_tail = NULL; +static int numhaptics = 0; + +bool SDL_SYS_HapticInit(void) +{ + Android_JNI_PollHapticDevices(); + + return true; +} + +int SDL_SYS_NumHaptics(void) +{ + return numhaptics; +} + +static SDL_hapticlist_item *HapticByOrder(int index) +{ + SDL_hapticlist_item *item = SDL_hapticlist; + if ((index < 0) || (index >= numhaptics)) { + return NULL; + } + while (index > 0) { + SDL_assert(item != NULL); + --index; + item = item->next; + } + return item; +} + +static SDL_hapticlist_item *HapticByInstanceID(SDL_HapticID instance_id) +{ + SDL_hapticlist_item *item; + for (item = SDL_hapticlist; item; item = item->next) { + if (instance_id == item->instance_id) { + return item; + } + } + return NULL; +} + +SDL_HapticID SDL_SYS_HapticInstanceID(int index) +{ + SDL_hapticlist_item *item = HapticByOrder(index); + if (item) { + return item->instance_id; + } + return 0; +} + +const char *SDL_SYS_HapticName(int index) +{ + SDL_hapticlist_item *item = HapticByOrder(index); + if (!item) { + SDL_SetError("No such device"); + return NULL; + } + return item->name; +} + +static SDL_hapticlist_item *OpenHaptic(SDL_Haptic *haptic, SDL_hapticlist_item *item) +{ + if (!item) { + SDL_SetError("No such device"); + return NULL; + } + if (item->haptic) { + SDL_SetError("Haptic already opened"); + return NULL; + } + + haptic->hwdata = (struct haptic_hwdata *)item; + item->haptic = haptic; + + haptic->instance_id = item->instance_id; + if (item->name) { + haptic->name = SDL_strdup(item->name); + } + haptic->supported = SDL_HAPTIC_LEFTRIGHT; + haptic->neffects = 1; + haptic->nplaying = haptic->neffects; + haptic->effects = (struct haptic_effect *)SDL_calloc(haptic->neffects, sizeof(struct haptic_effect)); + if (!haptic->effects) { + return NULL; + } + return item; +} + +static SDL_hapticlist_item *OpenHapticByInstanceID(SDL_Haptic *haptic, SDL_HapticID instance_id) +{ + return OpenHaptic(haptic, HapticByInstanceID(instance_id)); +} + +bool SDL_SYS_HapticOpen(SDL_Haptic *haptic) +{ + return OpenHapticByInstanceID(haptic, haptic->instance_id) != NULL; +} + +int SDL_SYS_HapticMouse(void) +{ + return -1; +} + +bool SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick) +{ + return false; +} + +bool SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ + return SDL_Unsupported(); +} + +bool SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ + return false; +} + +void SDL_SYS_HapticClose(SDL_Haptic *haptic) +{ + ((SDL_hapticlist_item *)haptic->hwdata)->haptic = NULL; + haptic->hwdata = NULL; +} + +void SDL_SYS_HapticQuit(void) +{ +/* We don't have any way to scan for joysticks (and their vibrators) at init, so don't wipe the list + * of joysticks here in case this is a reinit. + */ +#if 0 + SDL_hapticlist_item *item = NULL; + SDL_hapticlist_item *next = NULL; + + for (item = SDL_hapticlist; item; item = next) { + next = item->next; + SDL_free(item); + } + + SDL_hapticlist = SDL_hapticlist_tail = NULL; + numhaptics = 0; + return; +#endif +} + +bool SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, + struct haptic_effect *effect, const SDL_HapticEffect *base) +{ + return true; +} + +bool SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, + struct haptic_effect *effect, + const SDL_HapticEffect *data) +{ + return true; +} + +bool SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, + Uint32 iterations) +{ + float large = effect->effect.leftright.large_magnitude / 32767.0f; + float small = effect->effect.leftright.small_magnitude / 32767.0f; + + float total = (large * 0.6f) + (small * 0.4f); + + Android_JNI_HapticRun(((SDL_hapticlist_item *)haptic->hwdata)->device_id, total, effect->effect.leftright.length); + return true; +} + +bool SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + Android_JNI_HapticStop(((SDL_hapticlist_item *)haptic->hwdata)->device_id); + return true; +} + +void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ +} + +int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + return 0; +} + +bool SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain) +{ + return true; +} + +bool SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter) +{ + return true; +} + +bool SDL_SYS_HapticPause(SDL_Haptic *haptic) +{ + return true; +} + +bool SDL_SYS_HapticResume(SDL_Haptic *haptic) +{ + return true; +} + +bool SDL_SYS_HapticStopAll(SDL_Haptic *haptic) +{ + return true; +} + +bool Android_AddHaptic(int device_id, const char *name) +{ + SDL_hapticlist_item *item; + item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item)); + if (!item) { + return false; + } + + item->instance_id = SDL_GetNextObjectID(); + item->device_id = device_id; + item->name = SDL_strdup(name); + if (!item->name) { + SDL_free(item); + return false; + } + + if (!SDL_hapticlist_tail) { + SDL_hapticlist = SDL_hapticlist_tail = item; + } else { + SDL_hapticlist_tail->next = item; + SDL_hapticlist_tail = item; + } + + ++numhaptics; + return true; +} + +bool Android_RemoveHaptic(int device_id) +{ + SDL_hapticlist_item *item; + SDL_hapticlist_item *prev = NULL; + + for (item = SDL_hapticlist; item; item = item->next) { + // found it, remove it. + if (device_id == item->device_id) { + const bool result = item->haptic ? true : false; + + if (prev) { + prev->next = item->next; + } else { + SDL_assert(SDL_hapticlist == item); + SDL_hapticlist = item->next; + } + if (item == SDL_hapticlist_tail) { + SDL_hapticlist_tail = prev; + } + + // Need to decrement the haptic count + --numhaptics; + // !!! TODO: Send a haptic remove event? + + SDL_free(item->name); + SDL_free(item); + return result; + } + prev = item; + } + return false; +} + +#endif // SDL_HAPTIC_ANDROID diff --git a/contrib/SDL-3.2.8/src/haptic/android/SDL_syshaptic_c.h b/contrib/SDL-3.2.8/src/haptic/android/SDL_syshaptic_c.h new file mode 100644 index 0000000..4f58f4f --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/android/SDL_syshaptic_c.h @@ -0,0 +1,28 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_HAPTIC_ANDROID + +extern bool Android_AddHaptic(int device_id, const char *name); +extern bool Android_RemoveHaptic(int device_id); + +#endif // SDL_HAPTIC_ANDROID diff --git a/contrib/SDL-3.2.8/src/haptic/darwin/SDL_syshaptic.c b/contrib/SDL-3.2.8/src/haptic/darwin/SDL_syshaptic.c new file mode 100644 index 0000000..b48a3fe --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/darwin/SDL_syshaptic.c @@ -0,0 +1,1373 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_HAPTIC_IOKIT + +#include "../SDL_syshaptic.h" +#include "../../joystick/SDL_sysjoystick.h" // For the real SDL_Joystick +#include "../../joystick/darwin/SDL_iokitjoystick_c.h" // For joystick hwdata +#include "SDL_syshaptic_c.h" + +#include +#include +#include +#include +#include + +#ifndef IO_OBJECT_NULL +#define IO_OBJECT_NULL ((io_service_t)0) +#endif + +/* + * List of available haptic devices. + */ +typedef struct SDL_hapticlist_item +{ + SDL_HapticID instance_id; + char name[256]; // Name of the device. + + io_service_t dev; // Node we use to create the device. + SDL_Haptic *haptic; // Haptic currently associated with it. + + // Usage pages for determining if it's a mouse or not. + long usage; + long usagePage; + + struct SDL_hapticlist_item *next; +} SDL_hapticlist_item; + +/* + * Haptic system hardware data. + */ +struct haptic_hwdata +{ + FFDeviceObjectReference device; // Hardware device. + UInt8 axes[3]; +}; + +/* + * Haptic system effect data. + */ +struct haptic_hweffect +{ + FFEffectObjectReference ref; // Reference. + struct FFEFFECT effect; // Hardware effect. +}; + +/* + * Prototypes. + */ +static void SDL_SYS_HapticFreeFFEFFECT(FFEFFECT *effect, int type); +static bool HIDGetDeviceProduct(io_service_t dev, char *name); + +static SDL_hapticlist_item *SDL_hapticlist = NULL; +static SDL_hapticlist_item *SDL_hapticlist_tail = NULL; +static int numhaptics = -1; + +/* + * Like strerror but for force feedback errors. + */ +static const char *FFStrError(unsigned int err) +{ + switch (err) { + case FFERR_DEVICEFULL: + return "device full"; + // This should be valid, but for some reason isn't defined... + /* case FFERR_DEVICENOTREG: + return "device not registered"; */ + case FFERR_DEVICEPAUSED: + return "device paused"; + case FFERR_DEVICERELEASED: + return "device released"; + case FFERR_EFFECTPLAYING: + return "effect playing"; + case FFERR_EFFECTTYPEMISMATCH: + return "effect type mismatch"; + case FFERR_EFFECTTYPENOTSUPPORTED: + return "effect type not supported"; + case FFERR_GENERIC: + return "undetermined error"; + case FFERR_HASEFFECTS: + return "device has effects"; + case FFERR_INCOMPLETEEFFECT: + return "incomplete effect"; + case FFERR_INTERNAL: + return "internal fault"; + case FFERR_INVALIDDOWNLOADID: + return "invalid download id"; + case FFERR_INVALIDPARAM: + return "invalid parameter"; + case FFERR_MOREDATA: + return "more data"; + case FFERR_NOINTERFACE: + return "interface not supported"; + case FFERR_NOTDOWNLOADED: + return "effect is not downloaded"; + case FFERR_NOTINITIALIZED: + return "object has not been initialized"; + case FFERR_OUTOFMEMORY: + return "out of memory"; + case FFERR_UNPLUGGED: + return "device is unplugged"; + case FFERR_UNSUPPORTED: + return "function call unsupported"; + case FFERR_UNSUPPORTEDAXIS: + return "axis unsupported"; + + default: + return "unknown error"; + } +} + +/* + * Initializes the haptic subsystem. + */ +bool SDL_SYS_HapticInit(void) +{ + IOReturn result; + io_iterator_t iter; + CFDictionaryRef match; + io_service_t device; + + if (numhaptics != -1) { + return SDL_SetError("Haptic subsystem already initialized!"); + } + numhaptics = 0; + + // Get HID devices. + match = IOServiceMatching(kIOHIDDeviceKey); + if (!match) { + return SDL_SetError("Haptic: Failed to get IOServiceMatching."); + } + + // Now search I/O Registry for matching devices. + result = IOServiceGetMatchingServices(kIOMainPortDefault, match, &iter); + if (result != kIOReturnSuccess) { + return SDL_SetError("Haptic: Couldn't create a HID object iterator."); + } + // IOServiceGetMatchingServices consumes dictionary. + + if (!IOIteratorIsValid(iter)) { // No iterator. + return true; + } + + while ((device = IOIteratorNext(iter)) != IO_OBJECT_NULL) { + MacHaptic_MaybeAddDevice(device); + // always release as the AddDevice will retain IF it's a forcefeedback device + IOObjectRelease(device); + } + IOObjectRelease(iter); + + return true; +} + +int SDL_SYS_NumHaptics(void) +{ + return numhaptics; +} + +static SDL_hapticlist_item *HapticByDevIndex(int device_index) +{ + SDL_hapticlist_item *item = SDL_hapticlist; + + if ((device_index < 0) || (device_index >= numhaptics)) { + return NULL; + } + + while (device_index > 0) { + SDL_assert(item != NULL); + --device_index; + item = item->next; + } + + return item; +} + +static SDL_hapticlist_item *HapticByInstanceID(SDL_HapticID instance_id) +{ + SDL_hapticlist_item *item; + for (item = SDL_hapticlist; item; item = item->next) { + if (instance_id == item->instance_id) { + return item; + } + } + return NULL; +} + +bool MacHaptic_MaybeAddDevice(io_object_t device) +{ + IOReturn result; + CFMutableDictionaryRef hidProperties; + CFTypeRef refCF; + SDL_hapticlist_item *item; + + if (numhaptics == -1) { + return false; // not initialized. We'll pick these up on enumeration if we init later. + } + + // Check for force feedback. + if (FFIsForceFeedback(device) != FF_OK) { + return false; + } + + // Make sure we don't already have it + for (item = SDL_hapticlist; item; item = item->next) { + if (IOObjectIsEqualTo((io_object_t)item->dev, device)) { + // Already added + return false; + } + } + + item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item)); + if (!item) { + return SDL_SetError("Could not allocate haptic storage"); + } + item->instance_id = SDL_GetNextObjectID(); + + // retain it as we are going to keep it around a while + IOObjectRetain(device); + + // Set basic device data. + HIDGetDeviceProduct(device, item->name); + item->dev = device; + + // Set usage pages. + hidProperties = 0; + refCF = 0; + result = IORegistryEntryCreateCFProperties(device, + &hidProperties, + kCFAllocatorDefault, + kNilOptions); + if ((result == KERN_SUCCESS) && hidProperties) { + refCF = CFDictionaryGetValue(hidProperties, + CFSTR(kIOHIDPrimaryUsagePageKey)); + if (refCF) { + if (!CFNumberGetValue(refCF, kCFNumberLongType, &item->usagePage)) { + SDL_SetError("Haptic: Receiving device's usage page."); + } + refCF = CFDictionaryGetValue(hidProperties, + CFSTR(kIOHIDPrimaryUsageKey)); + if (refCF) { + if (!CFNumberGetValue(refCF, kCFNumberLongType, &item->usage)) { + SDL_SetError("Haptic: Receiving device's usage."); + } + } + } + CFRelease(hidProperties); + } + + if (!SDL_hapticlist_tail) { + SDL_hapticlist = SDL_hapticlist_tail = item; + } else { + SDL_hapticlist_tail->next = item; + SDL_hapticlist_tail = item; + } + + // Device has been added. + ++numhaptics; + + return true; +} + +bool MacHaptic_MaybeRemoveDevice(io_object_t device) +{ + SDL_hapticlist_item *item; + SDL_hapticlist_item *prev = NULL; + + if (numhaptics == -1) { + return false; // not initialized. ignore this. + } + + for (item = SDL_hapticlist; item; item = item->next) { + // found it, remove it. + if (IOObjectIsEqualTo((io_object_t)item->dev, device)) { + bool result = item->haptic ? true : false; + + if (prev) { + prev->next = item->next; + } else { + SDL_assert(SDL_hapticlist == item); + SDL_hapticlist = item->next; + } + if (item == SDL_hapticlist_tail) { + SDL_hapticlist_tail = prev; + } + + // Need to decrement the haptic count + --numhaptics; + // !!! TODO: Send a haptic remove event? + + IOObjectRelease(item->dev); + SDL_free(item); + return result; + } + prev = item; + } + + return false; +} + +SDL_HapticID SDL_SYS_HapticInstanceID(int index) +{ + SDL_hapticlist_item *item; + item = HapticByDevIndex(index); + if (item) { + return item->instance_id; + } + return 0; +} + +/* + * Return the name of a haptic device, does not need to be opened. + */ +const char *SDL_SYS_HapticName(int index) +{ + SDL_hapticlist_item *item; + item = HapticByDevIndex(index); + if (item) { + return item->name; + } + return NULL; +} + +/* + * Gets the device's product name. + */ +static bool HIDGetDeviceProduct(io_service_t dev, char *name) +{ + CFMutableDictionaryRef hidProperties, usbProperties; + io_registry_entry_t parent1, parent2; + kern_return_t ret; + + hidProperties = usbProperties = 0; + + ret = IORegistryEntryCreateCFProperties(dev, &hidProperties, + kCFAllocatorDefault, kNilOptions); + if ((ret != KERN_SUCCESS) || !hidProperties) { + return SDL_SetError("Haptic: Unable to create CFProperties."); + } + + /* macOS currently is not mirroring all USB properties to HID page so need to look at USB device page also + * get dictionary for USB properties: step up two levels and get CF dictionary for USB properties + */ + if ((KERN_SUCCESS == + IORegistryEntryGetParentEntry(dev, kIOServicePlane, &parent1)) && + (KERN_SUCCESS == + IORegistryEntryGetParentEntry(parent1, kIOServicePlane, &parent2)) && + (KERN_SUCCESS == + IORegistryEntryCreateCFProperties(parent2, &usbProperties, + kCFAllocatorDefault, + kNilOptions))) { + if (usbProperties) { + CFTypeRef refCF = 0; + /* get device info + * try hid dictionary first, if fail then go to USB dictionary + */ + + // Get product name + refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey)); + if (!refCF) { + refCF = CFDictionaryGetValue(usbProperties, + CFSTR("USB Product Name")); + } + if (refCF) { + if (!CFStringGetCString(refCF, name, 256, + CFStringGetSystemEncoding())) { + return SDL_SetError("Haptic: CFStringGetCString error retrieving pDevice->product."); + } + } + + CFRelease(usbProperties); + } else { + return SDL_SetError("Haptic: IORegistryEntryCreateCFProperties failed to create usbProperties."); + } + + // Release stuff. + if (kIOReturnSuccess != IOObjectRelease(parent2)) { + SDL_SetError("Haptic: IOObjectRelease error with parent2."); + } + if (kIOReturnSuccess != IOObjectRelease(parent1)) { + SDL_SetError("Haptic: IOObjectRelease error with parent1."); + } + } else { + return SDL_SetError("Haptic: Error getting registry entries."); + } + + return true; +} + +#define FF_TEST(ff, s) \ + if (features.supportedEffects & (ff)) \ + supported |= (s) +/* + * Gets supported features. + */ +static bool GetSupportedFeatures(SDL_Haptic *haptic) +{ + HRESULT ret; + FFDeviceObjectReference device; + FFCAPABILITIES features; + unsigned int supported; + Uint32 val; + + device = haptic->hwdata->device; + + ret = FFDeviceGetForceFeedbackCapabilities(device, &features); + if (ret != FF_OK) { + return SDL_SetError("Haptic: Unable to get device's supported features."); + } + + supported = 0; + + // Get maximum effects. + haptic->neffects = features.storageCapacity; + haptic->nplaying = features.playbackCapacity; + + // Test for effects. + FF_TEST(FFCAP_ET_CONSTANTFORCE, SDL_HAPTIC_CONSTANT); + FF_TEST(FFCAP_ET_RAMPFORCE, SDL_HAPTIC_RAMP); + FF_TEST(FFCAP_ET_SQUARE, SDL_HAPTIC_SQUARE); + FF_TEST(FFCAP_ET_SINE, SDL_HAPTIC_SINE); + FF_TEST(FFCAP_ET_TRIANGLE, SDL_HAPTIC_TRIANGLE); + FF_TEST(FFCAP_ET_SAWTOOTHUP, SDL_HAPTIC_SAWTOOTHUP); + FF_TEST(FFCAP_ET_SAWTOOTHDOWN, SDL_HAPTIC_SAWTOOTHDOWN); + FF_TEST(FFCAP_ET_SPRING, SDL_HAPTIC_SPRING); + FF_TEST(FFCAP_ET_DAMPER, SDL_HAPTIC_DAMPER); + FF_TEST(FFCAP_ET_INERTIA, SDL_HAPTIC_INERTIA); + FF_TEST(FFCAP_ET_FRICTION, SDL_HAPTIC_FRICTION); + FF_TEST(FFCAP_ET_CUSTOMFORCE, SDL_HAPTIC_CUSTOM); + + // Check if supports gain. + ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_FFGAIN, + &val, sizeof(val)); + if (ret == FF_OK) { + supported |= SDL_HAPTIC_GAIN; + } else if (ret != FFERR_UNSUPPORTED) { + return SDL_SetError("Haptic: Unable to get if device supports gain: %s.", + FFStrError(ret)); + } + + // Checks if supports autocenter. + ret = FFDeviceGetForceFeedbackProperty(device, FFPROP_AUTOCENTER, + &val, sizeof(val)); + if (ret == FF_OK) { + supported |= SDL_HAPTIC_AUTOCENTER; + } else if (ret != FFERR_UNSUPPORTED) { + return SDL_SetError("Haptic: Unable to get if device supports autocenter: %s.", + FFStrError(ret)); + } + + // Check for axes, we have an artificial limit on axes + haptic->naxes = ((features.numFfAxes) > 3) ? 3 : features.numFfAxes; + // Actually store the axes we want to use + SDL_memcpy(haptic->hwdata->axes, features.ffAxes, + haptic->naxes * sizeof(Uint8)); + + // Always supported features. + supported |= SDL_HAPTIC_STATUS | SDL_HAPTIC_PAUSE; + + haptic->supported = supported; + return true; +} + +/* + * Opens the haptic device from the file descriptor. + */ +static bool SDL_SYS_HapticOpenFromService(SDL_Haptic *haptic, io_service_t service) +{ + HRESULT ret; + + // Allocate the hwdata + haptic->hwdata = (struct haptic_hwdata *) SDL_calloc(1, sizeof(*haptic->hwdata)); + if (!haptic->hwdata) { + goto creat_err; + } + + // Open the device + ret = FFCreateDevice(service, &haptic->hwdata->device); + if (ret != FF_OK) { + SDL_SetError("Haptic: Unable to create device from service: %s.", FFStrError(ret)); + goto creat_err; + } + + // Get supported features. + if (!GetSupportedFeatures(haptic)) { + goto open_err; + } + + // Reset and then enable actuators. + ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device, + FFSFFC_RESET); + if (ret != FF_OK) { + SDL_SetError("Haptic: Unable to reset device: %s.", FFStrError(ret)); + goto open_err; + } + ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device, + FFSFFC_SETACTUATORSON); + if (ret != FF_OK) { + SDL_SetError("Haptic: Unable to enable actuators: %s.", + FFStrError(ret)); + goto open_err; + } + + // Allocate effects memory. + haptic->effects = (struct haptic_effect *) + SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects); + if (!haptic->effects) { + goto open_err; + } + // Clear the memory + SDL_memset(haptic->effects, 0, + sizeof(struct haptic_effect) * haptic->neffects); + + return true; + + // Error handling +open_err: + FFReleaseDevice(haptic->hwdata->device); +creat_err: + if (haptic->hwdata) { + SDL_free(haptic->hwdata); + haptic->hwdata = NULL; + } + return false; +} + +/* + * Opens a haptic device for usage. + */ +bool SDL_SYS_HapticOpen(SDL_Haptic *haptic) +{ + SDL_hapticlist_item *item; + item = HapticByInstanceID(haptic->instance_id); + + return SDL_SYS_HapticOpenFromService(haptic, item->dev); +} + +/* + * Opens a haptic device from first mouse it finds for usage. + */ +int SDL_SYS_HapticMouse(void) +{ + int device_index = 0; + SDL_hapticlist_item *item; + + for (item = SDL_hapticlist; item; item = item->next) { + if ((item->usagePage == kHIDPage_GenericDesktop) && + (item->usage == kHIDUsage_GD_Mouse)) { + return device_index; + } + ++device_index; + } + + return 0; +} + +/* + * Checks to see if a joystick has haptic features. + */ +bool SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick) +{ +#ifdef SDL_JOYSTICK_IOKIT + if (joystick->driver != &SDL_DARWIN_JoystickDriver) { + return false; + } + if (joystick->hwdata->ffservice != 0) { + return true; + } +#endif + return false; +} + +/* + * Checks to see if the haptic device and joystick are in reality the same. + */ +bool SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ +#ifdef SDL_JOYSTICK_IOKIT + if (joystick->driver != &SDL_DARWIN_JoystickDriver) { + return false; + } + if (IOObjectIsEqualTo((io_object_t)((size_t)haptic->hwdata->device), + joystick->hwdata->ffservice)) { + return true; + } +#endif + return false; +} + +/* + * Opens a SDL_Haptic from a SDL_Joystick. + */ +bool SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ +#ifdef SDL_JOYSTICK_IOKIT + SDL_hapticlist_item *item; + + if (joystick->driver != &SDL_DARWIN_JoystickDriver) { + return false; + } + for (item = SDL_hapticlist; item; item = item->next) { + if (IOObjectIsEqualTo((io_object_t)item->dev, + joystick->hwdata->ffservice)) { + haptic->instance_id = item->instance_id; + break; + } + } + + if (joystick->name) { + haptic->name = SDL_strdup(joystick->name); + } + + return SDL_SYS_HapticOpenFromService(haptic, joystick->hwdata->ffservice); +#else + return false; +#endif +} + +/* + * Closes the haptic device. + */ +void SDL_SYS_HapticClose(SDL_Haptic *haptic) +{ + if (haptic->hwdata) { + + // Free Effects. + SDL_free(haptic->effects); + haptic->effects = NULL; + haptic->neffects = 0; + + // Clean up + FFReleaseDevice(haptic->hwdata->device); + + // Free + SDL_free(haptic->hwdata); + haptic->hwdata = NULL; + } +} + +/* + * Clean up after system specific haptic stuff + */ +void SDL_SYS_HapticQuit(void) +{ + SDL_hapticlist_item *item; + SDL_hapticlist_item *next = NULL; + + for (item = SDL_hapticlist; item; item = next) { + next = item->next; + /* Opened and not closed haptics are leaked, this is on purpose. + * Close your haptic devices after usage. */ + + // Free the io_service_t + IOObjectRelease(item->dev); + SDL_free(item); + } + + numhaptics = -1; + SDL_hapticlist = NULL; + SDL_hapticlist_tail = NULL; +} + +/* + * Converts an SDL trigger button to an FFEFFECT trigger button. + */ +static DWORD FFGetTriggerButton(Uint16 button) +{ + DWORD dwTriggerButton; + + dwTriggerButton = FFEB_NOTRIGGER; + + if (button != 0) { + dwTriggerButton = FFJOFS_BUTTON(button - 1); + } + + return dwTriggerButton; +} + +/* + * Sets the direction. + */ +static bool SDL_SYS_SetDirection(FFEFFECT *effect, const SDL_HapticDirection *dir, int naxes) +{ + LONG *rglDir; + + // Handle no axes a part. + if (naxes == 0) { + effect->dwFlags |= FFEFF_SPHERICAL; // Set as default. + effect->rglDirection = NULL; + return true; + } + + // Has axes. + rglDir = SDL_malloc(sizeof(LONG) * naxes); + if (!rglDir) { + return false; + } + SDL_memset(rglDir, 0, sizeof(LONG) * naxes); + effect->rglDirection = rglDir; + + switch (dir->type) { + case SDL_HAPTIC_POLAR: + effect->dwFlags |= FFEFF_POLAR; + rglDir[0] = dir->dir[0]; + return true; + case SDL_HAPTIC_CARTESIAN: + effect->dwFlags |= FFEFF_CARTESIAN; + rglDir[0] = dir->dir[0]; + if (naxes > 1) { + rglDir[1] = dir->dir[1]; + } + if (naxes > 2) { + rglDir[2] = dir->dir[2]; + } + return true; + case SDL_HAPTIC_SPHERICAL: + effect->dwFlags |= FFEFF_SPHERICAL; + rglDir[0] = dir->dir[0]; + if (naxes > 1) { + rglDir[1] = dir->dir[1]; + } + if (naxes > 2) { + rglDir[2] = dir->dir[2]; + } + return true; + case SDL_HAPTIC_STEERING_AXIS: + effect->dwFlags |= FFEFF_CARTESIAN; + rglDir[0] = 0; + return true; + + default: + return SDL_SetError("Haptic: Unknown direction type."); + } +} + +// Clamps and converts. +#define CCONVERT(x) (((x) > 0x7FFF) ? 10000 : ((x)*10000) / 0x7FFF) +// Just converts. +#define CONVERT(x) (((x)*10000) / 0x7FFF) +/* + * Creates the FFEFFECT from a SDL_HapticEffect. + */ +static bool SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, const SDL_HapticEffect *src) +{ + int i; + FFCONSTANTFORCE *constant = NULL; + FFPERIODIC *periodic = NULL; + FFCONDITION *condition = NULL; // Actually an array of conditions - one per axis. + FFRAMPFORCE *ramp = NULL; + FFCUSTOMFORCE *custom = NULL; + FFENVELOPE *envelope = NULL; + const SDL_HapticConstant *hap_constant = NULL; + const SDL_HapticPeriodic *hap_periodic = NULL; + const SDL_HapticCondition *hap_condition = NULL; + const SDL_HapticRamp *hap_ramp = NULL; + const SDL_HapticCustom *hap_custom = NULL; + DWORD *axes = NULL; + + // Set global stuff. + SDL_memset(dest, 0, sizeof(FFEFFECT)); + dest->dwSize = sizeof(FFEFFECT); // Set the structure size. + dest->dwSamplePeriod = 0; // Not used by us. + dest->dwGain = 10000; // Gain is set globally, not locally. + dest->dwFlags = FFEFF_OBJECTOFFSETS; // Seems obligatory. + + // Envelope. + envelope = SDL_calloc(1, sizeof(FFENVELOPE)); + if (!envelope) { + return false; + } + dest->lpEnvelope = envelope; + envelope->dwSize = sizeof(FFENVELOPE); // Always should be this. + + // Axes. + if (src->constant.direction.type == SDL_HAPTIC_STEERING_AXIS) { + dest->cAxes = 1; + } else { + dest->cAxes = haptic->naxes; + } + if (dest->cAxes > 0) { + axes = SDL_malloc(sizeof(DWORD) * dest->cAxes); + if (!axes) { + return false; + } + axes[0] = haptic->hwdata->axes[0]; // Always at least one axis. + if (dest->cAxes > 1) { + axes[1] = haptic->hwdata->axes[1]; + } + if (dest->cAxes > 2) { + axes[2] = haptic->hwdata->axes[2]; + } + dest->rgdwAxes = axes; + } + + // The big type handling switch, even bigger then Linux's version. + switch (src->type) { + case SDL_HAPTIC_CONSTANT: + hap_constant = &src->constant; + constant = SDL_calloc(1, sizeof(FFCONSTANTFORCE)); + if (!constant) { + return false; + } + + // Specifics + constant->lMagnitude = CONVERT(hap_constant->level); + dest->cbTypeSpecificParams = sizeof(FFCONSTANTFORCE); + dest->lpvTypeSpecificParams = constant; + + // Generics + dest->dwDuration = hap_constant->length * 1000; // In microseconds. + dest->dwTriggerButton = FFGetTriggerButton(hap_constant->button); + dest->dwTriggerRepeatInterval = hap_constant->interval; + dest->dwStartDelay = hap_constant->delay * 1000; // In microseconds. + + // Direction. + if (!SDL_SYS_SetDirection(dest, &hap_constant->direction, dest->cAxes)) { + return false; + } + + // Envelope + if ((hap_constant->attack_length == 0) && (hap_constant->fade_length == 0)) { + SDL_free(envelope); + dest->lpEnvelope = NULL; + } else { + envelope->dwAttackLevel = CCONVERT(hap_constant->attack_level); + envelope->dwAttackTime = hap_constant->attack_length * 1000; + envelope->dwFadeLevel = CCONVERT(hap_constant->fade_level); + envelope->dwFadeTime = hap_constant->fade_length * 1000; + } + + break; + + case SDL_HAPTIC_SINE: + case SDL_HAPTIC_SQUARE: + case SDL_HAPTIC_TRIANGLE: + case SDL_HAPTIC_SAWTOOTHUP: + case SDL_HAPTIC_SAWTOOTHDOWN: + hap_periodic = &src->periodic; + periodic = SDL_calloc(1, sizeof(FFPERIODIC)); + if (!periodic) { + return false; + } + + // Specifics + periodic->dwMagnitude = CONVERT(SDL_abs(hap_periodic->magnitude)); + periodic->lOffset = CONVERT(hap_periodic->offset); + periodic->dwPhase = + (hap_periodic->phase + (hap_periodic->magnitude < 0 ? 18000 : 0)) % 36000; + periodic->dwPeriod = hap_periodic->period * 1000; + dest->cbTypeSpecificParams = sizeof(FFPERIODIC); + dest->lpvTypeSpecificParams = periodic; + + // Generics + dest->dwDuration = hap_periodic->length * 1000; // In microseconds. + dest->dwTriggerButton = FFGetTriggerButton(hap_periodic->button); + dest->dwTriggerRepeatInterval = hap_periodic->interval; + dest->dwStartDelay = hap_periodic->delay * 1000; // In microseconds. + + // Direction. + if (!SDL_SYS_SetDirection(dest, &hap_periodic->direction, dest->cAxes)) { + return false; + } + + // Envelope + if ((hap_periodic->attack_length == 0) && (hap_periodic->fade_length == 0)) { + SDL_free(envelope); + dest->lpEnvelope = NULL; + } else { + envelope->dwAttackLevel = CCONVERT(hap_periodic->attack_level); + envelope->dwAttackTime = hap_periodic->attack_length * 1000; + envelope->dwFadeLevel = CCONVERT(hap_periodic->fade_level); + envelope->dwFadeTime = hap_periodic->fade_length * 1000; + } + + break; + + case SDL_HAPTIC_SPRING: + case SDL_HAPTIC_DAMPER: + case SDL_HAPTIC_INERTIA: + case SDL_HAPTIC_FRICTION: + hap_condition = &src->condition; + if (dest->cAxes > 0) { + condition = SDL_calloc(dest->cAxes, sizeof(FFCONDITION)); + if (!condition) { + return false; + } + + // Specifics + for (i = 0; i < dest->cAxes; i++) { + condition[i].lOffset = CONVERT(hap_condition->center[i]); + condition[i].lPositiveCoefficient = + CONVERT(hap_condition->right_coeff[i]); + condition[i].lNegativeCoefficient = + CONVERT(hap_condition->left_coeff[i]); + condition[i].dwPositiveSaturation = + CCONVERT(hap_condition->right_sat[i] / 2); + condition[i].dwNegativeSaturation = + CCONVERT(hap_condition->left_sat[i] / 2); + condition[i].lDeadBand = CCONVERT(hap_condition->deadband[i] / 2); + } + } + + dest->cbTypeSpecificParams = sizeof(FFCONDITION) * dest->cAxes; + dest->lpvTypeSpecificParams = condition; + + // Generics + dest->dwDuration = hap_condition->length * 1000; // In microseconds. + dest->dwTriggerButton = FFGetTriggerButton(hap_condition->button); + dest->dwTriggerRepeatInterval = hap_condition->interval; + dest->dwStartDelay = hap_condition->delay * 1000; // In microseconds. + + // Direction. + if (!SDL_SYS_SetDirection(dest, &hap_condition->direction, dest->cAxes)) { + return false; + } + + // Envelope - Not actually supported by most CONDITION implementations. + SDL_free(dest->lpEnvelope); + dest->lpEnvelope = NULL; + + break; + + case SDL_HAPTIC_RAMP: + hap_ramp = &src->ramp; + ramp = SDL_calloc(1, sizeof(FFRAMPFORCE)); + if (!ramp) { + return false; + } + + // Specifics + ramp->lStart = CONVERT(hap_ramp->start); + ramp->lEnd = CONVERT(hap_ramp->end); + dest->cbTypeSpecificParams = sizeof(FFRAMPFORCE); + dest->lpvTypeSpecificParams = ramp; + + // Generics + dest->dwDuration = hap_ramp->length * 1000; // In microseconds. + dest->dwTriggerButton = FFGetTriggerButton(hap_ramp->button); + dest->dwTriggerRepeatInterval = hap_ramp->interval; + dest->dwStartDelay = hap_ramp->delay * 1000; // In microseconds. + + // Direction. + if (!SDL_SYS_SetDirection(dest, &hap_ramp->direction, dest->cAxes)) { + return false; + } + + // Envelope + if ((hap_ramp->attack_length == 0) && (hap_ramp->fade_length == 0)) { + SDL_free(envelope); + dest->lpEnvelope = NULL; + } else { + envelope->dwAttackLevel = CCONVERT(hap_ramp->attack_level); + envelope->dwAttackTime = hap_ramp->attack_length * 1000; + envelope->dwFadeLevel = CCONVERT(hap_ramp->fade_level); + envelope->dwFadeTime = hap_ramp->fade_length * 1000; + } + + break; + + case SDL_HAPTIC_CUSTOM: + hap_custom = &src->custom; + custom = SDL_calloc(1, sizeof(FFCUSTOMFORCE)); + if (!custom) { + return false; + } + + // Specifics + custom->cChannels = hap_custom->channels; + custom->dwSamplePeriod = hap_custom->period * 1000; + custom->cSamples = hap_custom->samples; + custom->rglForceData = + SDL_malloc(sizeof(LONG) * custom->cSamples * custom->cChannels); + for (i = 0; i < hap_custom->samples * hap_custom->channels; i++) { // Copy data. + custom->rglForceData[i] = CCONVERT(hap_custom->data[i]); + } + dest->cbTypeSpecificParams = sizeof(FFCUSTOMFORCE); + dest->lpvTypeSpecificParams = custom; + + // Generics + dest->dwDuration = hap_custom->length * 1000; // In microseconds. + dest->dwTriggerButton = FFGetTriggerButton(hap_custom->button); + dest->dwTriggerRepeatInterval = hap_custom->interval; + dest->dwStartDelay = hap_custom->delay * 1000; // In microseconds. + + // Direction. + if (!SDL_SYS_SetDirection(dest, &hap_custom->direction, dest->cAxes)) { + return false; + } + + // Envelope + if ((hap_custom->attack_length == 0) && (hap_custom->fade_length == 0)) { + SDL_free(envelope); + dest->lpEnvelope = NULL; + } else { + envelope->dwAttackLevel = CCONVERT(hap_custom->attack_level); + envelope->dwAttackTime = hap_custom->attack_length * 1000; + envelope->dwFadeLevel = CCONVERT(hap_custom->fade_level); + envelope->dwFadeTime = hap_custom->fade_length * 1000; + } + + break; + + default: + return SDL_SetError("Haptic: Unknown effect type."); + } + + return true; +} + +/* + * Frees an FFEFFECT allocated by SDL_SYS_ToFFEFFECT. + */ +static void SDL_SYS_HapticFreeFFEFFECT(FFEFFECT *effect, int type) +{ + FFCUSTOMFORCE *custom; + + SDL_free(effect->lpEnvelope); + effect->lpEnvelope = NULL; + SDL_free(effect->rgdwAxes); + effect->rgdwAxes = NULL; + if (effect->lpvTypeSpecificParams) { + if (type == SDL_HAPTIC_CUSTOM) { // Must free the custom data. + custom = (FFCUSTOMFORCE *)effect->lpvTypeSpecificParams; + SDL_free(custom->rglForceData); + custom->rglForceData = NULL; + } + SDL_free(effect->lpvTypeSpecificParams); + effect->lpvTypeSpecificParams = NULL; + } + SDL_free(effect->rglDirection); + effect->rglDirection = NULL; +} + +/* + * Gets the effect type from the generic SDL haptic effect wrapper. + */ +CFUUIDRef +SDL_SYS_HapticEffectType(Uint16 type) +{ + switch (type) { + case SDL_HAPTIC_CONSTANT: + return kFFEffectType_ConstantForce_ID; + + case SDL_HAPTIC_RAMP: + return kFFEffectType_RampForce_ID; + + case SDL_HAPTIC_SQUARE: + return kFFEffectType_Square_ID; + + case SDL_HAPTIC_SINE: + return kFFEffectType_Sine_ID; + + case SDL_HAPTIC_TRIANGLE: + return kFFEffectType_Triangle_ID; + + case SDL_HAPTIC_SAWTOOTHUP: + return kFFEffectType_SawtoothUp_ID; + + case SDL_HAPTIC_SAWTOOTHDOWN: + return kFFEffectType_SawtoothDown_ID; + + case SDL_HAPTIC_SPRING: + return kFFEffectType_Spring_ID; + + case SDL_HAPTIC_DAMPER: + return kFFEffectType_Damper_ID; + + case SDL_HAPTIC_INERTIA: + return kFFEffectType_Inertia_ID; + + case SDL_HAPTIC_FRICTION: + return kFFEffectType_Friction_ID; + + case SDL_HAPTIC_CUSTOM: + return kFFEffectType_CustomForce_ID; + + default: + SDL_SetError("Haptic: Unknown effect type."); + return NULL; + } +} + +/* + * Creates a new haptic effect. + */ +bool SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, + const SDL_HapticEffect *base) +{ + HRESULT ret; + CFUUIDRef type; + + // Alloc the effect. + effect->hweffect = (struct haptic_hweffect *) + SDL_calloc(1, sizeof(struct haptic_hweffect)); + if (!effect->hweffect) { + goto err_hweffect; + } + + // Get the type. + type = SDL_SYS_HapticEffectType(base->type); + if (!type) { + goto err_hweffect; + } + + // Get the effect. + if (!SDL_SYS_ToFFEFFECT(haptic, &effect->hweffect->effect, base)) { + goto err_effectdone; + } + + // Create the actual effect. + ret = FFDeviceCreateEffect(haptic->hwdata->device, type, + &effect->hweffect->effect, + &effect->hweffect->ref); + if (ret != FF_OK) { + SDL_SetError("Haptic: Unable to create effect: %s.", FFStrError(ret)); + goto err_effectdone; + } + + return true; + +err_effectdone: + SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, base->type); +err_hweffect: + SDL_free(effect->hweffect); + effect->hweffect = NULL; + return false; +} + +/* + * Updates an effect. + */ +bool SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, + struct haptic_effect *effect, + const SDL_HapticEffect *data) +{ + HRESULT ret; + FFEffectParameterFlag flags; + FFEFFECT temp; + + // Get the effect. + SDL_memset(&temp, 0, sizeof(FFEFFECT)); + if (!SDL_SYS_ToFFEFFECT(haptic, &temp, data)) { + goto err_update; + } + + /* Set the flags. Might be worthwhile to diff temp with loaded effect and + * only change those parameters. */ + flags = FFEP_DIRECTION | + FFEP_DURATION | + FFEP_ENVELOPE | + FFEP_STARTDELAY | + FFEP_TRIGGERBUTTON | + FFEP_TRIGGERREPEATINTERVAL | FFEP_TYPESPECIFICPARAMS; + + // Create the actual effect. + ret = FFEffectSetParameters(effect->hweffect->ref, &temp, flags); + if (ret != FF_OK) { + SDL_SetError("Haptic: Unable to update effect: %s.", FFStrError(ret)); + goto err_update; + } + + // Copy it over. + SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, data->type); + SDL_memcpy(&effect->hweffect->effect, &temp, sizeof(FFEFFECT)); + + return true; + +err_update: + SDL_SYS_HapticFreeFFEFFECT(&temp, data->type); + return false; +} + +/* + * Runs an effect. + */ +bool SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, + Uint32 iterations) +{ + HRESULT ret; + Uint32 iter; + + // Check if it's infinite. + if (iterations == SDL_HAPTIC_INFINITY) { + iter = FF_INFINITE; + } else { + iter = iterations; + } + + // Run the effect. + ret = FFEffectStart(effect->hweffect->ref, iter, 0); + if (ret != FF_OK) { + return SDL_SetError("Haptic: Unable to run the effect: %s.", + FFStrError(ret)); + } + + return true; +} + +/* + * Stops an effect. + */ +bool SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + HRESULT ret; + + ret = FFEffectStop(effect->hweffect->ref); + if (ret != FF_OK) { + return SDL_SetError("Haptic: Unable to stop the effect: %s.", + FFStrError(ret)); + } + + return true; +} + +/* + * Frees the effect. + */ +void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + HRESULT ret; + + ret = FFDeviceReleaseEffect(haptic->hwdata->device, effect->hweffect->ref); + if (ret != FF_OK) { + SDL_SetError("Haptic: Error removing the effect from the device: %s.", + FFStrError(ret)); + } + SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, + effect->effect.type); + SDL_free(effect->hweffect); + effect->hweffect = NULL; +} + +/* + * Gets the status of a haptic effect. + */ +int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic, + struct haptic_effect *effect) +{ + HRESULT ret; + FFEffectStatusFlag status; + + ret = FFEffectGetEffectStatus(effect->hweffect->ref, &status); + if (ret != FF_OK) { + SDL_SetError("Haptic: Unable to get effect status: %s.", FFStrError(ret)); + return -1; + } + + if (status == 0) { + return 0; + } + return 1; // Assume it's playing or emulated. +} + +/* + * Sets the gain. + */ +bool SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain) +{ + HRESULT ret; + Uint32 val; + + val = gain * 100; // macOS uses 0 to 10,000 + ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device, + FFPROP_FFGAIN, &val); + if (ret != FF_OK) { + return SDL_SetError("Haptic: Error setting gain: %s.", FFStrError(ret)); + } + + return true; +} + +/* + * Sets the autocentering. + */ +bool SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter) +{ + HRESULT ret; + Uint32 val; + + // macOS only has 0 (off) and 1 (on) + if (autocenter == 0) { + val = 0; + } else { + val = 1; + } + + ret = FFDeviceSetForceFeedbackProperty(haptic->hwdata->device, + FFPROP_AUTOCENTER, &val); + if (ret != FF_OK) { + return SDL_SetError("Haptic: Error setting autocenter: %s.", + FFStrError(ret)); + } + + return true; +} + +/* + * Pauses the device. + */ +bool SDL_SYS_HapticPause(SDL_Haptic *haptic) +{ + HRESULT ret; + + ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device, + FFSFFC_PAUSE); + if (ret != FF_OK) { + return SDL_SetError("Haptic: Error pausing device: %s.", FFStrError(ret)); + } + + return true; +} + +/* + * Unpauses the device. + */ +bool SDL_SYS_HapticResume(SDL_Haptic *haptic) +{ + HRESULT ret; + + ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device, + FFSFFC_CONTINUE); + if (ret != FF_OK) { + return SDL_SetError("Haptic: Error resuming device: %s.", FFStrError(ret)); + } + + return true; +} + +/* + * Stops all currently playing effects. + */ +bool SDL_SYS_HapticStopAll(SDL_Haptic *haptic) +{ + HRESULT ret; + + ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device, + FFSFFC_STOPALL); + if (ret != FF_OK) { + return SDL_SetError("Haptic: Error stopping device: %s.", FFStrError(ret)); + } + + return true; +} + +#endif // SDL_HAPTIC_IOKIT diff --git a/contrib/SDL-3.2.8/src/haptic/darwin/SDL_syshaptic_c.h b/contrib/SDL-3.2.8/src/haptic/darwin/SDL_syshaptic_c.h new file mode 100644 index 0000000..03c67ec --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/darwin/SDL_syshaptic_c.h @@ -0,0 +1,29 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +// Things named "Master" were renamed to "Main" in macOS 12.0's SDK. +#include +#if MAC_OS_X_VERSION_MIN_REQUIRED < 120000 +#define kIOMainPortDefault kIOMasterPortDefault +#endif + +extern bool MacHaptic_MaybeAddDevice(io_object_t device); +extern bool MacHaptic_MaybeRemoveDevice(io_object_t device); diff --git a/contrib/SDL-3.2.8/src/haptic/dummy/SDL_syshaptic.c b/contrib/SDL-3.2.8/src/haptic/dummy/SDL_syshaptic.c new file mode 100644 index 0000000..81f711f --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/dummy/SDL_syshaptic.c @@ -0,0 +1,151 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#if defined(SDL_HAPTIC_DUMMY) || defined(SDL_HAPTIC_DISABLED) + +#include "../SDL_syshaptic.h" + +static bool SDL_SYS_LogicError(void) +{ + return SDL_SetError("Logic error: No haptic devices available."); +} + +bool SDL_SYS_HapticInit(void) +{ + return true; +} + +int SDL_SYS_NumHaptics(void) +{ + return 0; +} + +SDL_HapticID SDL_SYS_HapticInstanceID(int index) +{ + SDL_SYS_LogicError(); + return 0; +} + +const char *SDL_SYS_HapticName(int index) +{ + SDL_SYS_LogicError(); + return NULL; +} + +bool SDL_SYS_HapticOpen(SDL_Haptic *haptic) +{ + return SDL_SYS_LogicError(); +} + +int SDL_SYS_HapticMouse(void) +{ + return -1; +} + +bool SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick) +{ + return false; +} + +bool SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ + return SDL_SYS_LogicError(); +} + +bool SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ + return false; +} + +void SDL_SYS_HapticClose(SDL_Haptic *haptic) +{ + return; +} + +void SDL_SYS_HapticQuit(void) +{ + return; +} + +bool SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, + struct haptic_effect *effect, const SDL_HapticEffect *base) +{ + return SDL_SYS_LogicError(); +} + +bool SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, + struct haptic_effect *effect, + const SDL_HapticEffect *data) +{ + return SDL_SYS_LogicError(); +} + +bool SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, + Uint32 iterations) +{ + return SDL_SYS_LogicError(); +} + +bool SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + return SDL_SYS_LogicError(); +} + +void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + SDL_SYS_LogicError(); + return; +} + +int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic, + struct haptic_effect *effect) +{ + SDL_SYS_LogicError(); + return -1; +} + +bool SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain) +{ + return SDL_SYS_LogicError(); +} + +bool SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter) +{ + return SDL_SYS_LogicError(); +} + +bool SDL_SYS_HapticPause(SDL_Haptic *haptic) +{ + return SDL_SYS_LogicError(); +} + +bool SDL_SYS_HapticResume(SDL_Haptic *haptic) +{ + return SDL_SYS_LogicError(); +} + +bool SDL_SYS_HapticStopAll(SDL_Haptic *haptic) +{ + return SDL_SYS_LogicError(); +} + +#endif // SDL_HAPTIC_DUMMY || SDL_HAPTIC_DISABLED diff --git a/contrib/SDL-3.2.8/src/haptic/linux/SDL_syshaptic.c b/contrib/SDL-3.2.8/src/haptic/linux/SDL_syshaptic.c new file mode 100644 index 0000000..842f577 --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/linux/SDL_syshaptic.c @@ -0,0 +1,1134 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_HAPTIC_LINUX + +#include "../SDL_syshaptic.h" +#include "../../joystick/SDL_sysjoystick.h" // For the real SDL_Joystick +#include "../../joystick/linux/SDL_sysjoystick_c.h" // For joystick hwdata +#include "../../core/linux/SDL_evdev_capabilities.h" +#include "../../core/linux/SDL_udev.h" + +#include // close +#include // Force feedback linux stuff. +#include // O_RDWR +#include // INT_MAX +#include // errno +#include // strerror +#include // stat + +#define MAX_HAPTICS 32 // It's doubtful someone has more then 32 evdev + +static bool MaybeAddDevice(const char *path); +#ifdef SDL_USE_LIBUDEV +static bool MaybeRemoveDevice(const char *path); +static void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath); +#endif // SDL_USE_LIBUDEV + +/* + * List of available haptic devices. + */ +typedef struct SDL_hapticlist_item +{ + SDL_HapticID instance_id; + char *fname; // Dev path name (like /dev/input/event1) + SDL_Haptic *haptic; // Associated haptic. + dev_t dev_num; + struct SDL_hapticlist_item *next; +} SDL_hapticlist_item; + +/* + * Haptic system hardware data. + */ +struct haptic_hwdata +{ + int fd; // File descriptor of the device. + char *fname; // Points to the name in SDL_hapticlist. +}; + +/* + * Haptic system effect data. + */ +struct haptic_hweffect +{ + struct ff_effect effect; // The linux kernel effect structure. +}; + +static SDL_hapticlist_item *SDL_hapticlist = NULL; +static SDL_hapticlist_item *SDL_hapticlist_tail = NULL; +static int numhaptics = 0; + +#define EV_TEST(ev, f) \ + if (test_bit((ev), features)) { \ + ret |= (f); \ + } +/* + * Test whether a device has haptic properties. + * Returns available properties or 0 if there are none. + */ +static Uint32 EV_IsHaptic(int fd) +{ + unsigned long features[1 + FF_MAX / sizeof(unsigned long)]; + Uint32 ret = 0; + + // Ask device for what it has. + if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(features)), features) < 0) { + SDL_SetError("Haptic: Unable to get device's features: %s", strerror(errno)); + return 0; + } + + // Convert supported features to SDL_HAPTIC platform-neutral features. + EV_TEST(FF_CONSTANT, SDL_HAPTIC_CONSTANT); + EV_TEST(FF_SINE, SDL_HAPTIC_SINE); + EV_TEST(FF_SQUARE, SDL_HAPTIC_SQUARE); + EV_TEST(FF_TRIANGLE, SDL_HAPTIC_TRIANGLE); + EV_TEST(FF_SAW_UP, SDL_HAPTIC_SAWTOOTHUP); + EV_TEST(FF_SAW_DOWN, SDL_HAPTIC_SAWTOOTHDOWN); + EV_TEST(FF_RAMP, SDL_HAPTIC_RAMP); + EV_TEST(FF_SPRING, SDL_HAPTIC_SPRING); + EV_TEST(FF_FRICTION, SDL_HAPTIC_FRICTION); + EV_TEST(FF_DAMPER, SDL_HAPTIC_DAMPER); + EV_TEST(FF_INERTIA, SDL_HAPTIC_INERTIA); + EV_TEST(FF_CUSTOM, SDL_HAPTIC_CUSTOM); + EV_TEST(FF_GAIN, SDL_HAPTIC_GAIN); + EV_TEST(FF_AUTOCENTER, SDL_HAPTIC_AUTOCENTER); + EV_TEST(FF_RUMBLE, SDL_HAPTIC_LEFTRIGHT); + + // Return what it supports. + return ret; +} + +/* + * Tests whether a device is a mouse or not. + */ +static bool EV_IsMouse(int fd) +{ + unsigned long argp[40]; + + // Ask for supported features. + if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(argp)), argp) < 0) { + return false; + } + + // Currently we only test for BTN_MOUSE which can give fake positives. + if (test_bit(BTN_MOUSE, argp) != 0) { + return true; + } + + return true; +} + +/* + * Initializes the haptic subsystem by finding available devices. + */ +bool SDL_SYS_HapticInit(void) +{ + const char joydev_pattern[] = "/dev/input/event%d"; + char path[PATH_MAX]; + int i, j; + + /* + * Limit amount of checks to MAX_HAPTICS since we may or may not have + * permission to some or all devices. + */ + i = 0; + for (j = 0; j < MAX_HAPTICS; ++j) { + (void)SDL_snprintf(path, PATH_MAX, joydev_pattern, i++); + MaybeAddDevice(path); + } + +#ifdef SDL_USE_LIBUDEV + if (!SDL_UDEV_Init()) { + return SDL_SetError("Could not initialize UDEV"); + } + + if (!SDL_UDEV_AddCallback(haptic_udev_callback)) { + SDL_UDEV_Quit(); + return SDL_SetError("Could not setup haptic <-> udev callback"); + } + + // Force a scan to build the initial device list + SDL_UDEV_Scan(); +#endif // SDL_USE_LIBUDEV + + return true; +} + +int SDL_SYS_NumHaptics(void) +{ + return numhaptics; +} + +static SDL_hapticlist_item *HapticByDevIndex(int device_index) +{ + SDL_hapticlist_item *item = SDL_hapticlist; + + if ((device_index < 0) || (device_index >= numhaptics)) { + return NULL; + } + + while (device_index > 0) { + SDL_assert(item != NULL); + --device_index; + item = item->next; + } + + return item; +} + +static SDL_hapticlist_item *HapticByInstanceID(SDL_HapticID instance_id) +{ + SDL_hapticlist_item *item; + for (item = SDL_hapticlist; item; item = item->next) { + if (instance_id == item->instance_id) { + return item; + } + } + return NULL; +} + +#ifdef SDL_USE_LIBUDEV +static void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath) +{ + if (!devpath || !(udev_class & SDL_UDEV_DEVICE_JOYSTICK)) { + return; + } + + switch (udev_type) { + case SDL_UDEV_DEVICEADDED: + MaybeAddDevice(devpath); + break; + + case SDL_UDEV_DEVICEREMOVED: + MaybeRemoveDevice(devpath); + break; + + default: + break; + } +} +#endif // SDL_USE_LIBUDEV + +static bool MaybeAddDevice(const char *path) +{ + struct stat sb; + int fd; + Uint32 supported; + SDL_hapticlist_item *item; + + if (!path) { + return false; + } + + // try to open + fd = open(path, O_RDWR | O_CLOEXEC, 0); + if (fd < 0) { + return false; + } + + // get file status + if (fstat(fd, &sb) != 0) { + close(fd); + return false; + } + + // check for duplicates + for (item = SDL_hapticlist; item; item = item->next) { + if (item->dev_num == sb.st_rdev) { + close(fd); + return false; // duplicate. + } + } + +#ifdef DEBUG_INPUT_EVENTS + printf("Checking %s\n", path); +#endif + + // see if it works + supported = EV_IsHaptic(fd); + close(fd); + if (!supported) { + return false; + } + + item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item)); + if (!item) { + return false; + } + + item->instance_id = SDL_GetNextObjectID(); + item->fname = SDL_strdup(path); + if (!item->fname) { + SDL_free(item); + return false; + } + + item->dev_num = sb.st_rdev; + + // TODO: should we add instance IDs? + if (!SDL_hapticlist_tail) { + SDL_hapticlist = SDL_hapticlist_tail = item; + } else { + SDL_hapticlist_tail->next = item; + SDL_hapticlist_tail = item; + } + + ++numhaptics; + + // !!! TODO: Send a haptic add event? + + return true; +} + +#ifdef SDL_USE_LIBUDEV +static bool MaybeRemoveDevice(const char *path) +{ + SDL_hapticlist_item *item; + SDL_hapticlist_item *prev = NULL; + + if (!path) { + return false; + } + + for (item = SDL_hapticlist; item; item = item->next) { + // found it, remove it. + if (SDL_strcmp(path, item->fname) == 0) { + const bool result = item->haptic ? true : false; + + if (prev) { + prev->next = item->next; + } else { + SDL_assert(SDL_hapticlist == item); + SDL_hapticlist = item->next; + } + if (item == SDL_hapticlist_tail) { + SDL_hapticlist_tail = prev; + } + + // Need to decrement the haptic count + --numhaptics; + // !!! TODO: Send a haptic remove event? + + SDL_free(item->fname); + SDL_free(item); + return result; + } + prev = item; + } + + return false; +} +#endif // SDL_USE_LIBUDEV + +/* + * Return the instance ID of a haptic device, does not need to be opened. + */ +SDL_HapticID SDL_SYS_HapticInstanceID(int index) +{ + SDL_hapticlist_item *item; + + item = HapticByDevIndex(index); + if (item) { + return item->instance_id; + } + return 0; +} + +/* + * Gets the name from a file descriptor. + */ +static const char *SDL_SYS_HapticNameFromFD(int fd) +{ + static char namebuf[128]; + + // We use the evdev name ioctl. + if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) { + return NULL; + } + + return namebuf; +} + +/* + * Return the name of a haptic device, does not need to be opened. + */ +const char *SDL_SYS_HapticName(int index) +{ + SDL_hapticlist_item *item; + int fd; + const char *name = NULL; + + item = HapticByDevIndex(index); + if (item) { + // Open the haptic device. + fd = open(item->fname, O_RDONLY | O_CLOEXEC, 0); + + if (fd >= 0) { + + name = SDL_SYS_HapticNameFromFD(fd); + if (!name) { + // No name found, return device character device + name = item->fname; + } + close(fd); + } + } + return name; +} + +/* + * Opens the haptic device from the file descriptor. + */ +static bool SDL_SYS_HapticOpenFromFD(SDL_Haptic *haptic, int fd) +{ + // Allocate the hwdata + haptic->hwdata = (struct haptic_hwdata *) + SDL_calloc(1, sizeof(*haptic->hwdata)); + if (!haptic->hwdata) { + goto open_err; + } + + // Set the data. + haptic->hwdata->fd = fd; + haptic->supported = EV_IsHaptic(fd); + haptic->naxes = 2; // Hardcoded for now, not sure if it's possible to find out. + + // Set the effects + if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) { + SDL_SetError("Haptic: Unable to query device memory: %s", + strerror(errno)); + goto open_err; + } + haptic->nplaying = haptic->neffects; // Linux makes no distinction. + haptic->effects = (struct haptic_effect *) + SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects); + if (!haptic->effects) { + goto open_err; + } + // Clear the memory + SDL_memset(haptic->effects, 0, + sizeof(struct haptic_effect) * haptic->neffects); + + return true; + + // Error handling +open_err: + close(fd); + if (haptic->hwdata) { + SDL_free(haptic->hwdata); + haptic->hwdata = NULL; + } + return false; +} + +/* + * Opens a haptic device for usage. + */ +bool SDL_SYS_HapticOpen(SDL_Haptic *haptic) +{ + int fd; + SDL_hapticlist_item *item; + + item = HapticByInstanceID(haptic->instance_id); + // Open the character device + fd = open(item->fname, O_RDWR | O_CLOEXEC, 0); + if (fd < 0) { + return SDL_SetError("Haptic: Unable to open %s: %s", + item->fname, strerror(errno)); + } + + // Try to create the haptic. + if (!SDL_SYS_HapticOpenFromFD(haptic, fd)) { + // Already closes on error. + return false; + } + + // Set the fname. + haptic->hwdata->fname = SDL_strdup(item->fname); + return true; +} + +/* + * Opens a haptic device from first mouse it finds for usage. + */ +int SDL_SYS_HapticMouse(void) +{ + int fd; + int device_index = 0; + SDL_hapticlist_item *item; + + for (item = SDL_hapticlist; item; item = item->next) { + // Open the device. + fd = open(item->fname, O_RDWR | O_CLOEXEC, 0); + if (fd < 0) { + return SDL_SetError("Haptic: Unable to open %s: %s", + item->fname, strerror(errno)); + } + + // Is it a mouse? + if (EV_IsMouse(fd)) { + close(fd); + return device_index; + } + + close(fd); + + ++device_index; + } + + return -1; +} + +/* + * Checks to see if a joystick has haptic features. + */ +bool SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick) +{ +#ifdef SDL_JOYSTICK_LINUX + SDL_AssertJoysticksLocked(); + + if (joystick->driver != &SDL_LINUX_JoystickDriver) { + return false; + } + if (EV_IsHaptic(joystick->hwdata->fd)) { + return true; + } +#endif + return false; +} + +/* + * Checks to see if the haptic device and joystick are in reality the same. + */ +bool SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ +#ifdef SDL_JOYSTICK_LINUX + SDL_AssertJoysticksLocked(); + + if (joystick->driver != &SDL_LINUX_JoystickDriver) { + return false; + } + /* We are assuming Linux is using evdev which should trump the old + * joystick methods. */ + if (SDL_strcmp(joystick->hwdata->fname, haptic->hwdata->fname) == 0) { + return true; + } +#endif + return false; +} + +/* + * Opens a SDL_Haptic from a SDL_Joystick. + */ +bool SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ +#ifdef SDL_JOYSTICK_LINUX + int fd; + SDL_hapticlist_item *item; + const char *name; + + SDL_AssertJoysticksLocked(); + + if (joystick->driver != &SDL_LINUX_JoystickDriver) { + return false; + } + // Find the joystick in the haptic list. + for (item = SDL_hapticlist; item; item = item->next) { + if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) { + haptic->instance_id = item->instance_id; + break; + } + } + + fd = open(joystick->hwdata->fname, O_RDWR | O_CLOEXEC, 0); + if (fd < 0) { + return SDL_SetError("Haptic: Unable to open %s: %s", + joystick->hwdata->fname, strerror(errno)); + } + if (!SDL_SYS_HapticOpenFromFD(haptic, fd)) { + // Already closes on error. + return false; + } + + haptic->hwdata->fname = SDL_strdup(joystick->hwdata->fname); + + name = SDL_SYS_HapticNameFromFD(fd); + if (name) { + haptic->name = SDL_strdup(name); + } + return true; +#else + return false; +#endif +} + +/* + * Closes the haptic device. + */ +void SDL_SYS_HapticClose(SDL_Haptic *haptic) +{ + if (haptic->hwdata) { + + // Free effects. + SDL_free(haptic->effects); + haptic->effects = NULL; + haptic->neffects = 0; + + // Clean up + close(haptic->hwdata->fd); + + // Free + SDL_free(haptic->hwdata->fname); + SDL_free(haptic->hwdata); + haptic->hwdata = NULL; + } + + // Clear the rest. + SDL_memset(haptic, 0, sizeof(SDL_Haptic)); +} + +/* + * Clean up after system specific haptic stuff + */ +void SDL_SYS_HapticQuit(void) +{ + SDL_hapticlist_item *item = NULL; + SDL_hapticlist_item *next = NULL; + + for (item = SDL_hapticlist; item; item = next) { + next = item->next; + /* Opened and not closed haptics are leaked, this is on purpose. + * Close your haptic devices after usage. */ + SDL_free(item->fname); + SDL_free(item); + } + +#ifdef SDL_USE_LIBUDEV + SDL_UDEV_DelCallback(haptic_udev_callback); + SDL_UDEV_Quit(); +#endif // SDL_USE_LIBUDEV + + numhaptics = 0; + SDL_hapticlist = NULL; + SDL_hapticlist_tail = NULL; +} + +/* + * Converts an SDL button to a ff_trigger button. + */ +static Uint16 SDL_SYS_ToButton(Uint16 button) +{ + Uint16 ff_button; + + ff_button = 0; + + /* + * Not sure what the proper syntax is because this actually isn't implemented + * in the current kernel from what I've seen (2.6.26). + */ + if (button != 0) { + ff_button = BTN_GAMEPAD + button - 1; + } + + return ff_button; +} + +/* + * Initializes the ff_effect usable direction from a SDL_HapticDirection. + */ +static bool SDL_SYS_ToDirection(Uint16 *dest, const SDL_HapticDirection *src) +{ + Uint32 tmp; + + switch (src->type) { + case SDL_HAPTIC_POLAR: + tmp = ((src->dir[0] % 36000) * 0x8000) / 18000; // convert to range [0,0xFFFF] + *dest = (Uint16)tmp; + break; + + case SDL_HAPTIC_SPHERICAL: + /* + We convert to polar, because that's the only supported direction on Linux. + The first value of a spherical direction is practically the same as a + Polar direction, except that we have to add 90 degrees. It is the angle + from EAST {1,0} towards SOUTH {0,1}. + --> add 9000 + --> finally convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR. + */ + tmp = ((src->dir[0]) + 9000) % 36000; // Convert to polars + tmp = (tmp * 0x8000) / 18000; // convert to range [0,0xFFFF] + *dest = (Uint16)tmp; + break; + + case SDL_HAPTIC_CARTESIAN: + if (!src->dir[1]) { + *dest = (src->dir[0] >= 0 ? 0x4000 : 0xC000); + } else if (!src->dir[0]) { + *dest = (src->dir[1] >= 0 ? 0x8000 : 0); + } else { + float f = SDL_atan2f(src->dir[1], src->dir[0]); // Ideally we'd use fixed point math instead of floats... + /* + SDL_atan2 takes the parameters: Y-axis-value and X-axis-value (in that order) + - Y-axis-value is the second coordinate (from center to SOUTH) + - X-axis-value is the first coordinate (from center to EAST) + We add 36000, because SDL_atan2 also returns negative values. Then we practically + have the first spherical value. Therefore we proceed as in case + SDL_HAPTIC_SPHERICAL and add another 9000 to get the polar value. + --> add 45000 in total + --> finally convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR. + */ + tmp = (((Sint32)(f * 18000.0 / SDL_PI_D)) + 45000) % 36000; + tmp = (tmp * 0x8000) / 18000; // convert to range [0,0xFFFF] + *dest = (Uint16)tmp; + } + break; + case SDL_HAPTIC_STEERING_AXIS: + *dest = 0x4000; + break; + default: + return SDL_SetError("Haptic: Unsupported direction type."); + } + + return true; +} + +#define CLAMP(x) (((x) > 32767) ? 32767 : x) +/* + * Initializes the Linux effect struct from a haptic_effect. + * Values above 32767 (for unsigned) are unspecified so we must clamp. + */ +static bool SDL_SYS_ToFFEffect(struct ff_effect *dest, const SDL_HapticEffect *src) +{ + const SDL_HapticConstant *constant; + const SDL_HapticPeriodic *periodic; + const SDL_HapticCondition *condition; + const SDL_HapticRamp *ramp; + const SDL_HapticLeftRight *leftright; + + // Clear up + SDL_memset(dest, 0, sizeof(struct ff_effect)); + + switch (src->type) { + case SDL_HAPTIC_CONSTANT: + constant = &src->constant; + + // Header + dest->type = FF_CONSTANT; + if (!SDL_SYS_ToDirection(&dest->direction, &constant->direction)) { + return false; + } + + // Replay + dest->replay.length = (constant->length == SDL_HAPTIC_INFINITY) ? 0 : CLAMP(constant->length); + dest->replay.delay = CLAMP(constant->delay); + + // Trigger + dest->trigger.button = SDL_SYS_ToButton(constant->button); + dest->trigger.interval = CLAMP(constant->interval); + + // Constant + dest->u.constant.level = constant->level; + + // Envelope + dest->u.constant.envelope.attack_length = + CLAMP(constant->attack_length); + dest->u.constant.envelope.attack_level = + CLAMP(constant->attack_level); + dest->u.constant.envelope.fade_length = CLAMP(constant->fade_length); + dest->u.constant.envelope.fade_level = CLAMP(constant->fade_level); + + break; + + case SDL_HAPTIC_SINE: + case SDL_HAPTIC_SQUARE: + case SDL_HAPTIC_TRIANGLE: + case SDL_HAPTIC_SAWTOOTHUP: + case SDL_HAPTIC_SAWTOOTHDOWN: + periodic = &src->periodic; + + // Header + dest->type = FF_PERIODIC; + if (!SDL_SYS_ToDirection(&dest->direction, &periodic->direction)) { + return false; + } + + // Replay + dest->replay.length = (periodic->length == SDL_HAPTIC_INFINITY) ? 0 : CLAMP(periodic->length); + dest->replay.delay = CLAMP(periodic->delay); + + // Trigger + dest->trigger.button = SDL_SYS_ToButton(periodic->button); + dest->trigger.interval = CLAMP(periodic->interval); + + // Periodic + if (periodic->type == SDL_HAPTIC_SINE) { + dest->u.periodic.waveform = FF_SINE; + } else if (periodic->type == SDL_HAPTIC_SQUARE) { + dest->u.periodic.waveform = FF_SQUARE; + } else if (periodic->type == SDL_HAPTIC_TRIANGLE) { + dest->u.periodic.waveform = FF_TRIANGLE; + } else if (periodic->type == SDL_HAPTIC_SAWTOOTHUP) { + dest->u.periodic.waveform = FF_SAW_UP; + } else if (periodic->type == SDL_HAPTIC_SAWTOOTHDOWN) { + dest->u.periodic.waveform = FF_SAW_DOWN; + } + dest->u.periodic.period = CLAMP(periodic->period); + dest->u.periodic.magnitude = periodic->magnitude; + dest->u.periodic.offset = periodic->offset; + // Linux phase is defined in interval "[0x0000, 0x10000[", corresponds with "[0deg, 360deg[" phase shift. + dest->u.periodic.phase = ((Uint32)periodic->phase * 0x10000U) / 36000; + + // Envelope + dest->u.periodic.envelope.attack_length = + CLAMP(periodic->attack_length); + dest->u.periodic.envelope.attack_level = + CLAMP(periodic->attack_level); + dest->u.periodic.envelope.fade_length = CLAMP(periodic->fade_length); + dest->u.periodic.envelope.fade_level = CLAMP(periodic->fade_level); + + break; + + case SDL_HAPTIC_SPRING: + case SDL_HAPTIC_DAMPER: + case SDL_HAPTIC_INERTIA: + case SDL_HAPTIC_FRICTION: + condition = &src->condition; + + // Header + if (condition->type == SDL_HAPTIC_SPRING) { + dest->type = FF_SPRING; + } else if (condition->type == SDL_HAPTIC_DAMPER) { + dest->type = FF_DAMPER; + } else if (condition->type == SDL_HAPTIC_INERTIA) { + dest->type = FF_INERTIA; + } else if (condition->type == SDL_HAPTIC_FRICTION) { + dest->type = FF_FRICTION; + } + + if (!SDL_SYS_ToDirection(&dest->direction, &condition->direction)) { + return false; + } + + // Replay + dest->replay.length = (condition->length == SDL_HAPTIC_INFINITY) ? 0 : CLAMP(condition->length); + dest->replay.delay = CLAMP(condition->delay); + + // Trigger + dest->trigger.button = SDL_SYS_ToButton(condition->button); + dest->trigger.interval = CLAMP(condition->interval); + + // Condition + // X axis + dest->u.condition[0].right_saturation = condition->right_sat[0]; + dest->u.condition[0].left_saturation = condition->left_sat[0]; + dest->u.condition[0].right_coeff = condition->right_coeff[0]; + dest->u.condition[0].left_coeff = condition->left_coeff[0]; + dest->u.condition[0].deadband = condition->deadband[0]; + dest->u.condition[0].center = condition->center[0]; + // Y axis + dest->u.condition[1].right_saturation = condition->right_sat[1]; + dest->u.condition[1].left_saturation = condition->left_sat[1]; + dest->u.condition[1].right_coeff = condition->right_coeff[1]; + dest->u.condition[1].left_coeff = condition->left_coeff[1]; + dest->u.condition[1].deadband = condition->deadband[1]; + dest->u.condition[1].center = condition->center[1]; + + /* + * There is no envelope in the linux force feedback api for conditions. + */ + + break; + + case SDL_HAPTIC_RAMP: + ramp = &src->ramp; + + // Header + dest->type = FF_RAMP; + if (!SDL_SYS_ToDirection(&dest->direction, &ramp->direction)) { + return false; + } + + // Replay + dest->replay.length = (ramp->length == SDL_HAPTIC_INFINITY) ? 0 : CLAMP(ramp->length); + dest->replay.delay = CLAMP(ramp->delay); + + // Trigger + dest->trigger.button = SDL_SYS_ToButton(ramp->button); + dest->trigger.interval = CLAMP(ramp->interval); + + // Ramp + dest->u.ramp.start_level = ramp->start; + dest->u.ramp.end_level = ramp->end; + + // Envelope + dest->u.ramp.envelope.attack_length = CLAMP(ramp->attack_length); + dest->u.ramp.envelope.attack_level = CLAMP(ramp->attack_level); + dest->u.ramp.envelope.fade_length = CLAMP(ramp->fade_length); + dest->u.ramp.envelope.fade_level = CLAMP(ramp->fade_level); + + break; + + case SDL_HAPTIC_LEFTRIGHT: + leftright = &src->leftright; + + // Header + dest->type = FF_RUMBLE; + dest->direction = 0x4000; + + // Replay + dest->replay.length = (leftright->length == SDL_HAPTIC_INFINITY) ? 0 : CLAMP(leftright->length); + + // Trigger + dest->trigger.button = 0; + dest->trigger.interval = 0; + + // Rumble (Linux expects 0-65535, so multiply by 2) + dest->u.rumble.strong_magnitude = CLAMP(leftright->large_magnitude) * 2; + dest->u.rumble.weak_magnitude = CLAMP(leftright->small_magnitude) * 2; + + break; + + default: + return SDL_SetError("Haptic: Unknown effect type."); + } + + return true; +} + +/* + * Creates a new haptic effect. + */ +bool SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, + const SDL_HapticEffect *base) +{ + struct ff_effect *linux_effect; + + // Allocate the hardware effect + effect->hweffect = (struct haptic_hweffect *) + SDL_calloc(1, sizeof(struct haptic_hweffect)); + if (!effect->hweffect) { + return false; + } + + // Prepare the ff_effect + linux_effect = &effect->hweffect->effect; + if (!SDL_SYS_ToFFEffect(linux_effect, base)) { + goto new_effect_err; + } + linux_effect->id = -1; // Have the kernel give it an id + + // Upload the effect + if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) { + SDL_SetError("Haptic: Error uploading effect to the device: %s", + strerror(errno)); + goto new_effect_err; + } + + return true; + +new_effect_err: + SDL_free(effect->hweffect); + effect->hweffect = NULL; + return false; +} + +/* + * Updates an effect. + * + * Note: Dynamically updating the direction can in some cases force + * the effect to restart and run once. + */ +bool SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, + struct haptic_effect *effect, + const SDL_HapticEffect *data) +{ + struct ff_effect linux_effect; + + // Create the new effect + if (!SDL_SYS_ToFFEffect(&linux_effect, data)) { + return false; + } + linux_effect.id = effect->hweffect->effect.id; + + // See if it can be uploaded. + if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) { + return SDL_SetError("Haptic: Error updating the effect: %s", + strerror(errno)); + } + + // Copy the new effect into memory. + SDL_memcpy(&effect->hweffect->effect, &linux_effect, + sizeof(struct ff_effect)); + + return true; +} + +/* + * Runs an effect. + */ +bool SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, + Uint32 iterations) +{ + struct input_event run; + + // Prepare to run the effect + run.type = EV_FF; + run.code = effect->hweffect->effect.id; + // We don't actually have infinity here, so we just do INT_MAX which is pretty damn close. + run.value = (iterations > INT_MAX) ? INT_MAX : iterations; + + if (write(haptic->hwdata->fd, (const void *)&run, sizeof(run)) < 0) { + return SDL_SetError("Haptic: Unable to run the effect: %s", strerror(errno)); + } + + return true; +} + +/* + * Stops an effect. + */ +bool SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + struct input_event stop; + + stop.type = EV_FF; + stop.code = effect->hweffect->effect.id; + stop.value = 0; + + if (write(haptic->hwdata->fd, (const void *)&stop, sizeof(stop)) < 0) { + return SDL_SetError("Haptic: Unable to stop the effect: %s", + strerror(errno)); + } + + return true; +} + +/* + * Frees the effect. + */ +void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) { + SDL_SetError("Haptic: Error removing the effect from the device: %s", + strerror(errno)); + } + SDL_free(effect->hweffect); + effect->hweffect = NULL; +} + +/* + * Gets the status of a haptic effect. + */ +int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic, + struct haptic_effect *effect) +{ +#if 0 // Not supported atm. + struct input_event ie; + + ie.type = EV_FF; + ie.type = EV_FF_STATUS; + ie.code = effect->hweffect->effect.id; + + if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) { + SDL_SetError("Haptic: Error getting device status."); + return -1; + } + + return 1; +#endif + + SDL_Unsupported(); + return -1; +} + +/* + * Sets the gain. + */ +bool SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain) +{ + struct input_event ie; + + ie.type = EV_FF; + ie.code = FF_GAIN; + ie.value = (0xFFFFUL * gain) / 100; + + if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) { + return SDL_SetError("Haptic: Error setting gain: %s", strerror(errno)); + } + + return true; +} + +/* + * Sets the autocentering. + */ +bool SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter) +{ + struct input_event ie; + + ie.type = EV_FF; + ie.code = FF_AUTOCENTER; + ie.value = (0xFFFFUL * autocenter) / 100; + + if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) { + return SDL_SetError("Haptic: Error setting autocenter: %s", strerror(errno)); + } + + return true; +} + +/* + * Pausing is not supported atm by linux. + */ +bool SDL_SYS_HapticPause(SDL_Haptic *haptic) +{ + return SDL_Unsupported(); +} + +/* + * Unpausing is not supported atm by linux. + */ +bool SDL_SYS_HapticResume(SDL_Haptic *haptic) +{ + return SDL_Unsupported(); +} + +/* + * Stops all the currently playing effects. + */ +bool SDL_SYS_HapticStopAll(SDL_Haptic *haptic) +{ + int i, ret; + + // Linux does not support this natively so we have to loop. + for (i = 0; i < haptic->neffects; i++) { + if (haptic->effects[i].hweffect != NULL) { + ret = SDL_SYS_HapticStopEffect(haptic, &haptic->effects[i]); + if (ret < 0) { + return SDL_SetError("Haptic: Error while trying to stop all playing effects."); + } + } + } + return true; +} + +#endif // SDL_HAPTIC_LINUX diff --git a/contrib/SDL-3.2.8/src/haptic/windows/SDL_dinputhaptic.c b/contrib/SDL-3.2.8/src/haptic/windows/SDL_dinputhaptic.c new file mode 100644 index 0000000..255aac0 --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/windows/SDL_dinputhaptic.c @@ -0,0 +1,1244 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#include "../SDL_syshaptic.h" + +#ifdef SDL_HAPTIC_DINPUT + +#include "SDL_windowshaptic_c.h" +#include "SDL_dinputhaptic_c.h" +#include "../../joystick/windows/SDL_windowsjoystick_c.h" + +/* + * External stuff. + */ +#ifdef SDL_VIDEO_DRIVER_WINDOWS +extern HWND SDL_HelperWindow; +#else +static const HWND SDL_HelperWindow = NULL; +#endif + +/* + * Internal stuff. + */ +static bool coinitialized = false; +static LPDIRECTINPUT8 dinput = NULL; + +/* + * Like SDL_SetError but for DX error codes. + */ +static bool DI_SetError(const char *str, HRESULT err) +{ + return SDL_SetError("Haptic error %s", str); +} + +/* + * Callback to find the haptic devices. + */ +static BOOL CALLBACK EnumHapticsCallback(const DIDEVICEINSTANCE *pdidInstance, VOID *pContext) +{ + (void)pContext; + SDL_DINPUT_HapticMaybeAddDevice(pdidInstance); + return DIENUM_CONTINUE; // continue enumerating +} + +bool SDL_DINPUT_HapticInit(void) +{ + HRESULT ret; + HINSTANCE instance; + DWORD devClass; + + if (dinput != NULL) { // Already open. + return SDL_SetError("Haptic: SubSystem already open."); + } + + if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_DIRECTINPUT, true)) { + // In some environments, IDirectInput8_Initialize / _EnumDevices can take a minute even with no controllers. + return true; + } + + ret = WIN_CoInitialize(); + if (FAILED(ret)) { + return DI_SetError("Coinitialize", ret); + } + + coinitialized = true; + + ret = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER, + &IID_IDirectInput8, (LPVOID *)&dinput); + if (FAILED(ret)) { + SDL_SYS_HapticQuit(); + return DI_SetError("CoCreateInstance", ret); + } + + // Because we used CoCreateInstance, we need to Initialize it, first. + instance = GetModuleHandle(NULL); + if (!instance) { + SDL_SYS_HapticQuit(); + return SDL_SetError("GetModuleHandle() failed with error code %lu.", + GetLastError()); + } + ret = IDirectInput8_Initialize(dinput, instance, DIRECTINPUT_VERSION); + if (FAILED(ret)) { + SDL_SYS_HapticQuit(); + return DI_SetError("Initializing DirectInput device", ret); + } + + // Look for haptic devices. + for (devClass = DI8DEVCLASS_DEVICE; devClass <= DI8DEVCLASS_GAMECTRL; devClass++) { + if (devClass == DI8DEVCLASS_GAMECTRL && SDL_WasInit(SDL_INIT_JOYSTICK)) { + // The joystick subsystem will manage adding DInput joystick haptic devices + continue; + } + + ret = IDirectInput8_EnumDevices(dinput, + devClass, + EnumHapticsCallback, + NULL, + DIEDFL_FORCEFEEDBACK | + DIEDFL_ATTACHEDONLY); + if (FAILED(ret)) { + SDL_SYS_HapticQuit(); + return DI_SetError("Enumerating DirectInput devices", ret); + } + } + + return true; +} + +bool SDL_DINPUT_HapticMaybeAddDevice(const DIDEVICEINSTANCE *pdidInstance) +{ + HRESULT ret; + LPDIRECTINPUTDEVICE8 device; + const DWORD needflags = DIDC_ATTACHED | DIDC_FORCEFEEDBACK; + DIDEVCAPS capabilities; + SDL_hapticlist_item *item = NULL; + + if (!dinput) { + return false; // not initialized. We'll pick these up on enumeration if we init later. + } + + // Make sure we don't already have it + for (item = SDL_hapticlist; item; item = item->next) { + if (SDL_memcmp(&item->instance, pdidInstance, sizeof(*pdidInstance)) == 0) { + return false; // Already added + } + } + + // Open the device + ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance, &device, NULL); + if (FAILED(ret)) { + // DI_SetError("Creating DirectInput device",ret); + return false; + } + + // Get capabilities. + SDL_zero(capabilities); + capabilities.dwSize = sizeof(DIDEVCAPS); + ret = IDirectInputDevice8_GetCapabilities(device, &capabilities); + IDirectInputDevice8_Release(device); + if (FAILED(ret)) { + // DI_SetError("Getting device capabilities",ret); + return false; + } + + if ((capabilities.dwFlags & needflags) != needflags) { + return false; // not a device we can use. + } + + item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item)); + if (!item) { + return false; + } + + item->instance_id = SDL_GetNextObjectID(); + item->name = WIN_StringToUTF8(pdidInstance->tszProductName); + if (!item->name) { + SDL_free(item); + return false; + } + + // Copy the instance over, useful for creating devices. + SDL_memcpy(&item->instance, pdidInstance, sizeof(DIDEVICEINSTANCE)); + SDL_memcpy(&item->capabilities, &capabilities, sizeof(capabilities)); + + return SDL_SYS_AddHapticDevice(item); +} + +bool SDL_DINPUT_HapticMaybeRemoveDevice(const DIDEVICEINSTANCE *pdidInstance) +{ + SDL_hapticlist_item *item; + SDL_hapticlist_item *prev = NULL; + + if (!dinput) { + return false; // not initialized, ignore this. + } + + for (item = SDL_hapticlist; item; item = item->next) { + if (SDL_memcmp(&item->instance, pdidInstance, sizeof(*pdidInstance)) == 0) { + // found it, remove it. + return SDL_SYS_RemoveHapticDevice(prev, item); + } + prev = item; + } + return false; +} + +/* + * Callback to get supported axes. + */ +static BOOL CALLBACK DI_DeviceObjectCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef) +{ + SDL_Haptic *haptic = (SDL_Haptic *)pvRef; + + if ((dev->dwType & DIDFT_AXIS) && (dev->dwFlags & DIDOI_FFACTUATOR)) { + const GUID *guid = &dev->guidType; + DWORD offset = 0; + if (WIN_IsEqualGUID(guid, &GUID_XAxis)) { + offset = DIJOFS_X; + } else if (WIN_IsEqualGUID(guid, &GUID_YAxis)) { + offset = DIJOFS_Y; + } else if (WIN_IsEqualGUID(guid, &GUID_ZAxis)) { + offset = DIJOFS_Z; + } else if (WIN_IsEqualGUID(guid, &GUID_RxAxis)) { + offset = DIJOFS_RX; + } else if (WIN_IsEqualGUID(guid, &GUID_RyAxis)) { + offset = DIJOFS_RY; + } else if (WIN_IsEqualGUID(guid, &GUID_RzAxis)) { + offset = DIJOFS_RZ; + } else { + return DIENUM_CONTINUE; // can't use this, go on. + } + + haptic->hwdata->axes[haptic->naxes] = offset; + haptic->naxes++; + + // Currently using the artificial limit of 3 axes. + if (haptic->naxes >= 3) { + return DIENUM_STOP; + } + } + + return DIENUM_CONTINUE; +} + +/* + * Callback to get all supported effects. + */ +#define EFFECT_TEST(e, s) \ + if (WIN_IsEqualGUID(&pei->guid, &(e))) \ + haptic->supported |= (s) +static BOOL CALLBACK DI_EffectCallback(LPCDIEFFECTINFO pei, LPVOID pv) +{ + // Prepare the haptic device. + SDL_Haptic *haptic = (SDL_Haptic *)pv; + + // Get supported. + EFFECT_TEST(GUID_Spring, SDL_HAPTIC_SPRING); + EFFECT_TEST(GUID_Damper, SDL_HAPTIC_DAMPER); + EFFECT_TEST(GUID_Inertia, SDL_HAPTIC_INERTIA); + EFFECT_TEST(GUID_Friction, SDL_HAPTIC_FRICTION); + EFFECT_TEST(GUID_ConstantForce, SDL_HAPTIC_CONSTANT); + EFFECT_TEST(GUID_CustomForce, SDL_HAPTIC_CUSTOM); + EFFECT_TEST(GUID_Sine, SDL_HAPTIC_SINE); + EFFECT_TEST(GUID_Square, SDL_HAPTIC_SQUARE); + EFFECT_TEST(GUID_Triangle, SDL_HAPTIC_TRIANGLE); + EFFECT_TEST(GUID_SawtoothUp, SDL_HAPTIC_SAWTOOTHUP); + EFFECT_TEST(GUID_SawtoothDown, SDL_HAPTIC_SAWTOOTHDOWN); + EFFECT_TEST(GUID_RampForce, SDL_HAPTIC_RAMP); + + // Check for more. + return DIENUM_CONTINUE; +} + +/* + * Opens the haptic device. + * + * Steps: + * - Set cooperative level. + * - Set data format. + * - Acquire exclusiveness. + * - Reset actuators. + * - Get supported features. + */ +static bool SDL_DINPUT_HapticOpenFromDevice(SDL_Haptic *haptic, LPDIRECTINPUTDEVICE8 device8, bool is_joystick) +{ + HRESULT ret; + DIPROPDWORD dipdw; + + // Allocate the hwdata + haptic->hwdata = (struct haptic_hwdata *)SDL_calloc(1, sizeof(*haptic->hwdata)); + if (!haptic->hwdata) { + return false; + } + + // We'll use the device8 from now on. + haptic->hwdata->device = device8; + haptic->hwdata->is_joystick = is_joystick; + + /* !!! FIXME: opening a haptic device here first will make an attempt to + !!! FIXME: SDL_OpenJoystick() that same device fail later, since we + !!! FIXME: have it open in exclusive mode. But this will allow + !!! FIXME: SDL_OpenJoystick() followed by SDL_OpenHapticFromJoystick() + !!! FIXME: to work, and that's probably the common case. Still, + !!! FIXME: ideally, We need to unify the opening code. */ + + if (!is_joystick) { // if is_joystick, we already set this up elsewhere. + // Grab it exclusively to use force feedback stuff. + ret = IDirectInputDevice8_SetCooperativeLevel(haptic->hwdata->device, + SDL_HelperWindow, + DISCL_EXCLUSIVE | + DISCL_BACKGROUND); + if (FAILED(ret)) { + DI_SetError("Setting cooperative level to exclusive", ret); + goto acquire_err; + } + + // Set data format. + ret = IDirectInputDevice8_SetDataFormat(haptic->hwdata->device, + &SDL_c_dfDIJoystick2); + if (FAILED(ret)) { + DI_SetError("Setting data format", ret); + goto acquire_err; + } + + // Acquire the device. + ret = IDirectInputDevice8_Acquire(haptic->hwdata->device); + if (FAILED(ret)) { + DI_SetError("Acquiring DirectInput device", ret); + goto acquire_err; + } + } + + // Get number of axes. + ret = IDirectInputDevice8_EnumObjects(haptic->hwdata->device, + DI_DeviceObjectCallback, + haptic, DIDFT_AXIS); + if (FAILED(ret)) { + DI_SetError("Getting device axes", ret); + goto acquire_err; + } + + // Reset all actuators - just in case. + ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device, + DISFFC_RESET); + if (FAILED(ret)) { + DI_SetError("Resetting device", ret); + goto acquire_err; + } + + // Enabling actuators. + ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device, + DISFFC_SETACTUATORSON); + if (FAILED(ret)) { + DI_SetError("Enabling actuators", ret); + goto acquire_err; + } + + // Get supported effects. + ret = IDirectInputDevice8_EnumEffects(haptic->hwdata->device, + DI_EffectCallback, haptic, + DIEFT_ALL); + if (FAILED(ret)) { + DI_SetError("Enumerating supported effects", ret); + goto acquire_err; + } + if (haptic->supported == 0) { // Error since device supports nothing. + SDL_SetError("Haptic: Internal error on finding supported effects."); + goto acquire_err; + } + + // Check autogain and autocenter. + dipdw.diph.dwSize = sizeof(DIPROPDWORD); + dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); + dipdw.diph.dwObj = 0; + dipdw.diph.dwHow = DIPH_DEVICE; + dipdw.dwData = 10000; + ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device, + DIPROP_FFGAIN, &dipdw.diph); + if (!FAILED(ret)) { // Gain is supported. + haptic->supported |= SDL_HAPTIC_GAIN; + } + dipdw.diph.dwObj = 0; + dipdw.diph.dwHow = DIPH_DEVICE; + dipdw.dwData = DIPROPAUTOCENTER_OFF; + ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device, + DIPROP_AUTOCENTER, &dipdw.diph); + if (!FAILED(ret)) { // Autocenter is supported. + haptic->supported |= SDL_HAPTIC_AUTOCENTER; + } + + // Status is always supported. + haptic->supported |= SDL_HAPTIC_STATUS | SDL_HAPTIC_PAUSE; + + // Check maximum effects. + haptic->neffects = 128; /* This is not actually supported as thus under windows, + there is no way to tell the number of EFFECTS that a + device can hold, so we'll just use a "random" number + instead and put warnings in SDL_haptic.h */ + haptic->nplaying = 128; // Even more impossible to get this then neffects. + + // Prepare effects memory. + haptic->effects = (struct haptic_effect *) + SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects); + if (!haptic->effects) { + goto acquire_err; + } + // Clear the memory + SDL_memset(haptic->effects, 0, + sizeof(struct haptic_effect) * haptic->neffects); + + return true; + + // Error handling +acquire_err: + IDirectInputDevice8_Unacquire(haptic->hwdata->device); + return false; +} + +bool SDL_DINPUT_HapticOpen(SDL_Haptic *haptic, SDL_hapticlist_item *item) +{ + HRESULT ret; + LPDIRECTINPUTDEVICE8 device; + + // Open the device + ret = IDirectInput8_CreateDevice(dinput, &item->instance.guidInstance, + &device, NULL); + if (FAILED(ret)) { + DI_SetError("Creating DirectInput device", ret); + return false; + } + + if (!SDL_DINPUT_HapticOpenFromDevice(haptic, device, false)) { + IDirectInputDevice8_Release(device); + return false; + } + return true; +} + +bool SDL_DINPUT_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ + HRESULT ret; + DIDEVICEINSTANCE hap_instance, joy_instance; + + hap_instance.dwSize = sizeof(DIDEVICEINSTANCE); + joy_instance.dwSize = sizeof(DIDEVICEINSTANCE); + + // Get the device instances. + ret = IDirectInputDevice8_GetDeviceInfo(haptic->hwdata->device, + &hap_instance); + if (FAILED(ret)) { + return false; + } + ret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice, + &joy_instance); + if (FAILED(ret)) { + return false; + } + + return (WIN_IsEqualGUID(&hap_instance.guidInstance, &joy_instance.guidInstance) == TRUE); +} + +bool SDL_DINPUT_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ + SDL_hapticlist_item *item; + HRESULT ret; + DIDEVICEINSTANCE joy_instance; + + joy_instance.dwSize = sizeof(DIDEVICEINSTANCE); + ret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice, &joy_instance); + if (FAILED(ret)) { + return false; + } + + // Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. + for (item = SDL_hapticlist; item; item = item->next) { + if (WIN_IsEqualGUID(&item->instance.guidInstance, &joy_instance.guidInstance)) { + haptic->instance_id = item->instance_id; + haptic->name = SDL_strdup(item->name); + return SDL_DINPUT_HapticOpenFromDevice(haptic, joystick->hwdata->InputDevice, true); + } + } + + return SDL_SetError("Couldn't find joystick in haptic device list"); +} + +void SDL_DINPUT_HapticClose(SDL_Haptic *haptic) +{ + IDirectInputDevice8_Unacquire(haptic->hwdata->device); + + // Only release if isn't grabbed by a joystick. + if (haptic->hwdata->is_joystick == 0) { + IDirectInputDevice8_Release(haptic->hwdata->device); + } +} + +void SDL_DINPUT_HapticQuit(void) +{ + if (dinput != NULL) { + IDirectInput8_Release(dinput); + dinput = NULL; + } + + if (coinitialized) { + WIN_CoUninitialize(); + coinitialized = false; + } +} + +/* + * Converts an SDL trigger button to an DIEFFECT trigger button. + */ +static DWORD DIGetTriggerButton(Uint16 button) +{ + DWORD dwTriggerButton; + + dwTriggerButton = DIEB_NOTRIGGER; + + if (button != 0) { + dwTriggerButton = DIJOFS_BUTTON(button - 1); + } + + return dwTriggerButton; +} + +/* + * Sets the direction. + */ +static bool SDL_SYS_SetDirection(DIEFFECT *effect, const SDL_HapticDirection *dir, int naxes) +{ + LONG *rglDir; + + // Handle no axes a part. + if (naxes == 0) { + effect->dwFlags |= DIEFF_SPHERICAL; // Set as default. + effect->rglDirection = NULL; + return true; + } + + // Has axes. + rglDir = (LONG *)SDL_malloc(sizeof(LONG) * naxes); + if (!rglDir) { + return false; + } + SDL_memset(rglDir, 0, sizeof(LONG) * naxes); + effect->rglDirection = rglDir; + + switch (dir->type) { + case SDL_HAPTIC_POLAR: + effect->dwFlags |= DIEFF_POLAR; + rglDir[0] = dir->dir[0]; + return true; + case SDL_HAPTIC_CARTESIAN: + effect->dwFlags |= DIEFF_CARTESIAN; + rglDir[0] = dir->dir[0]; + if (naxes > 1) { + rglDir[1] = dir->dir[1]; + } + if (naxes > 2) { + rglDir[2] = dir->dir[2]; + } + return true; + case SDL_HAPTIC_SPHERICAL: + effect->dwFlags |= DIEFF_SPHERICAL; + rglDir[0] = dir->dir[0]; + if (naxes > 1) { + rglDir[1] = dir->dir[1]; + } + if (naxes > 2) { + rglDir[2] = dir->dir[2]; + } + return true; + case SDL_HAPTIC_STEERING_AXIS: + effect->dwFlags |= DIEFF_CARTESIAN; + rglDir[0] = 0; + return true; + + default: + return SDL_SetError("Haptic: Unknown direction type."); + } +} + +// Clamps and converts. +#define CCONVERT(x) (((x) > 0x7FFF) ? 10000 : ((x)*10000) / 0x7FFF) +// Just converts. +#define CONVERT(x) (((x)*10000) / 0x7FFF) +/* + * Creates the DIEFFECT from a SDL_HapticEffect. + */ +static bool SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest, + const SDL_HapticEffect *src) +{ + int i; + DICONSTANTFORCE *constant; + DIPERIODIC *periodic; + DICONDITION *condition; // Actually an array of conditions - one per axis. + DIRAMPFORCE *ramp; + DICUSTOMFORCE *custom; + DIENVELOPE *envelope; + const SDL_HapticConstant *hap_constant; + const SDL_HapticPeriodic *hap_periodic; + const SDL_HapticCondition *hap_condition; + const SDL_HapticRamp *hap_ramp; + const SDL_HapticCustom *hap_custom; + DWORD *axes; + + // Set global stuff. + SDL_memset(dest, 0, sizeof(DIEFFECT)); + dest->dwSize = sizeof(DIEFFECT); // Set the structure size. + dest->dwSamplePeriod = 0; // Not used by us. + dest->dwGain = 10000; // Gain is set globally, not locally. + dest->dwFlags = DIEFF_OBJECTOFFSETS; // Seems obligatory. + + // Envelope. + envelope = (DIENVELOPE *)SDL_calloc(1, sizeof(DIENVELOPE)); + if (!envelope) { + return false; + } + dest->lpEnvelope = envelope; + envelope->dwSize = sizeof(DIENVELOPE); // Always should be this. + + // Axes. + if (src->constant.direction.type == SDL_HAPTIC_STEERING_AXIS) { + dest->cAxes = 1; + } else { + dest->cAxes = haptic->naxes; + } + if (dest->cAxes > 0) { + axes = (DWORD *)SDL_malloc(sizeof(DWORD) * dest->cAxes); + if (!axes) { + return false; + } + axes[0] = haptic->hwdata->axes[0]; // Always at least one axis. + if (dest->cAxes > 1) { + axes[1] = haptic->hwdata->axes[1]; + } + if (dest->cAxes > 2) { + axes[2] = haptic->hwdata->axes[2]; + } + dest->rgdwAxes = axes; + } + + // The big type handling switch, even bigger than Linux's version. + switch (src->type) { + case SDL_HAPTIC_CONSTANT: + hap_constant = &src->constant; + constant = (DICONSTANTFORCE *)SDL_calloc(1, sizeof(DICONSTANTFORCE)); + if (!constant) { + return false; + } + + // Specifics + constant->lMagnitude = CONVERT(hap_constant->level); + dest->cbTypeSpecificParams = sizeof(DICONSTANTFORCE); + dest->lpvTypeSpecificParams = constant; + + // Generics + dest->dwDuration = hap_constant->length * 1000UL; // In microseconds. + dest->dwTriggerButton = DIGetTriggerButton(hap_constant->button); + dest->dwTriggerRepeatInterval = hap_constant->interval; + dest->dwStartDelay = hap_constant->delay * 1000UL; // In microseconds. + + // Direction. + if (!SDL_SYS_SetDirection(dest, &hap_constant->direction, dest->cAxes)) { + return false; + } + + // Envelope + if ((hap_constant->attack_length == 0) && (hap_constant->fade_length == 0)) { + SDL_free(dest->lpEnvelope); + dest->lpEnvelope = NULL; + } else { + envelope->dwAttackLevel = CCONVERT(hap_constant->attack_level); + envelope->dwAttackTime = hap_constant->attack_length * 1000UL; + envelope->dwFadeLevel = CCONVERT(hap_constant->fade_level); + envelope->dwFadeTime = hap_constant->fade_length * 1000UL; + } + + break; + + case SDL_HAPTIC_SINE: + case SDL_HAPTIC_SQUARE: + case SDL_HAPTIC_TRIANGLE: + case SDL_HAPTIC_SAWTOOTHUP: + case SDL_HAPTIC_SAWTOOTHDOWN: + hap_periodic = &src->periodic; + periodic = (DIPERIODIC *)SDL_calloc(1, sizeof(DIPERIODIC)); + if (!periodic) { + return false; + } + + // Specifics + periodic->dwMagnitude = CONVERT(SDL_abs(hap_periodic->magnitude)); + periodic->lOffset = CONVERT(hap_periodic->offset); + periodic->dwPhase = + (hap_periodic->phase + (hap_periodic->magnitude < 0 ? 18000 : 0)) % 36000; + periodic->dwPeriod = hap_periodic->period * 1000; + dest->cbTypeSpecificParams = sizeof(DIPERIODIC); + dest->lpvTypeSpecificParams = periodic; + + // Generics + dest->dwDuration = hap_periodic->length * 1000UL; // In microseconds. + dest->dwTriggerButton = DIGetTriggerButton(hap_periodic->button); + dest->dwTriggerRepeatInterval = hap_periodic->interval; + dest->dwStartDelay = hap_periodic->delay * 1000UL; // In microseconds. + + // Direction. + if (!SDL_SYS_SetDirection(dest, &hap_periodic->direction, dest->cAxes)) { + return false; + } + + // Envelope + if ((hap_periodic->attack_length == 0) && (hap_periodic->fade_length == 0)) { + SDL_free(dest->lpEnvelope); + dest->lpEnvelope = NULL; + } else { + envelope->dwAttackLevel = CCONVERT(hap_periodic->attack_level); + envelope->dwAttackTime = hap_periodic->attack_length * 1000UL; + envelope->dwFadeLevel = CCONVERT(hap_periodic->fade_level); + envelope->dwFadeTime = hap_periodic->fade_length * 1000UL; + } + + break; + + case SDL_HAPTIC_SPRING: + case SDL_HAPTIC_DAMPER: + case SDL_HAPTIC_INERTIA: + case SDL_HAPTIC_FRICTION: + hap_condition = &src->condition; + condition = (DICONDITION *)SDL_calloc(dest->cAxes, sizeof(DICONDITION)); + if (!condition) { + return false; + } + + // Specifics + for (i = 0; i < (int)dest->cAxes; i++) { + condition[i].lOffset = CONVERT(hap_condition->center[i]); + condition[i].lPositiveCoefficient = + CONVERT(hap_condition->right_coeff[i]); + condition[i].lNegativeCoefficient = + CONVERT(hap_condition->left_coeff[i]); + condition[i].dwPositiveSaturation = + CCONVERT(hap_condition->right_sat[i] / 2); + condition[i].dwNegativeSaturation = + CCONVERT(hap_condition->left_sat[i] / 2); + condition[i].lDeadBand = CCONVERT(hap_condition->deadband[i] / 2); + } + dest->cbTypeSpecificParams = sizeof(DICONDITION) * dest->cAxes; + dest->lpvTypeSpecificParams = condition; + + // Generics + dest->dwDuration = hap_condition->length * 1000UL; // In microseconds. + dest->dwTriggerButton = DIGetTriggerButton(hap_condition->button); + dest->dwTriggerRepeatInterval = hap_condition->interval; + dest->dwStartDelay = hap_condition->delay * 1000UL; // In microseconds. + + // Direction. + if (!SDL_SYS_SetDirection(dest, &hap_condition->direction, dest->cAxes)) { + return false; + } + + // Envelope - Not actually supported by most CONDITION implementations. + SDL_free(dest->lpEnvelope); + dest->lpEnvelope = NULL; + + break; + + case SDL_HAPTIC_RAMP: + hap_ramp = &src->ramp; + ramp = (DIRAMPFORCE *)SDL_calloc(1, sizeof(DIRAMPFORCE)); + if (!ramp) { + return false; + } + + // Specifics + ramp->lStart = CONVERT(hap_ramp->start); + ramp->lEnd = CONVERT(hap_ramp->end); + dest->cbTypeSpecificParams = sizeof(DIRAMPFORCE); + dest->lpvTypeSpecificParams = ramp; + + // Generics + dest->dwDuration = hap_ramp->length * 1000UL; // In microseconds. + dest->dwTriggerButton = DIGetTriggerButton(hap_ramp->button); + dest->dwTriggerRepeatInterval = hap_ramp->interval; + dest->dwStartDelay = hap_ramp->delay * 1000UL; // In microseconds. + + // Direction. + if (!SDL_SYS_SetDirection(dest, &hap_ramp->direction, dest->cAxes)) { + return false; + } + + // Envelope + if ((hap_ramp->attack_length == 0) && (hap_ramp->fade_length == 0)) { + SDL_free(dest->lpEnvelope); + dest->lpEnvelope = NULL; + } else { + envelope->dwAttackLevel = CCONVERT(hap_ramp->attack_level); + envelope->dwAttackTime = hap_ramp->attack_length * 1000UL; + envelope->dwFadeLevel = CCONVERT(hap_ramp->fade_level); + envelope->dwFadeTime = hap_ramp->fade_length * 1000UL; + } + + break; + + case SDL_HAPTIC_CUSTOM: + hap_custom = &src->custom; + custom = (DICUSTOMFORCE *)SDL_calloc(1, sizeof(DICUSTOMFORCE)); + if (!custom) { + return false; + } + + // Specifics + custom->cChannels = hap_custom->channels; + custom->dwSamplePeriod = hap_custom->period * 1000UL; + custom->cSamples = hap_custom->samples; + custom->rglForceData = (LPLONG)SDL_malloc(sizeof(LONG) * custom->cSamples * custom->cChannels); + for (i = 0; i < hap_custom->samples * hap_custom->channels; i++) { // Copy data. + custom->rglForceData[i] = CCONVERT(hap_custom->data[i]); + } + dest->cbTypeSpecificParams = sizeof(DICUSTOMFORCE); + dest->lpvTypeSpecificParams = custom; + + // Generics + dest->dwDuration = hap_custom->length * 1000UL; // In microseconds. + dest->dwTriggerButton = DIGetTriggerButton(hap_custom->button); + dest->dwTriggerRepeatInterval = hap_custom->interval; + dest->dwStartDelay = hap_custom->delay * 1000UL; // In microseconds. + + // Direction. + if (!SDL_SYS_SetDirection(dest, &hap_custom->direction, dest->cAxes)) { + return false; + } + + // Envelope + if ((hap_custom->attack_length == 0) && (hap_custom->fade_length == 0)) { + SDL_free(dest->lpEnvelope); + dest->lpEnvelope = NULL; + } else { + envelope->dwAttackLevel = CCONVERT(hap_custom->attack_level); + envelope->dwAttackTime = hap_custom->attack_length * 1000UL; + envelope->dwFadeLevel = CCONVERT(hap_custom->fade_level); + envelope->dwFadeTime = hap_custom->fade_length * 1000UL; + } + + break; + + default: + return SDL_SetError("Haptic: Unknown effect type."); + } + + return true; +} + +/* + * Frees an DIEFFECT allocated by SDL_SYS_ToDIEFFECT. + */ +static void SDL_SYS_HapticFreeDIEFFECT(DIEFFECT *effect, int type) +{ + DICUSTOMFORCE *custom; + + SDL_free(effect->lpEnvelope); + effect->lpEnvelope = NULL; + SDL_free(effect->rgdwAxes); + effect->rgdwAxes = NULL; + if (effect->lpvTypeSpecificParams) { + if (type == SDL_HAPTIC_CUSTOM) { // Must free the custom data. + custom = (DICUSTOMFORCE *)effect->lpvTypeSpecificParams; + SDL_free(custom->rglForceData); + custom->rglForceData = NULL; + } + SDL_free(effect->lpvTypeSpecificParams); + effect->lpvTypeSpecificParams = NULL; + } + SDL_free(effect->rglDirection); + effect->rglDirection = NULL; +} + +/* + * Gets the effect type from the generic SDL haptic effect wrapper. + */ +// NOLINTNEXTLINE(readability-const-return-type): Can't fix Windows' headers +static REFGUID SDL_SYS_HapticEffectType(const SDL_HapticEffect *effect) +{ + switch (effect->type) { + case SDL_HAPTIC_CONSTANT: + return &GUID_ConstantForce; + + case SDL_HAPTIC_RAMP: + return &GUID_RampForce; + + case SDL_HAPTIC_SQUARE: + return &GUID_Square; + + case SDL_HAPTIC_SINE: + return &GUID_Sine; + + case SDL_HAPTIC_TRIANGLE: + return &GUID_Triangle; + + case SDL_HAPTIC_SAWTOOTHUP: + return &GUID_SawtoothUp; + + case SDL_HAPTIC_SAWTOOTHDOWN: + return &GUID_SawtoothDown; + + case SDL_HAPTIC_SPRING: + return &GUID_Spring; + + case SDL_HAPTIC_DAMPER: + return &GUID_Damper; + + case SDL_HAPTIC_INERTIA: + return &GUID_Inertia; + + case SDL_HAPTIC_FRICTION: + return &GUID_Friction; + + case SDL_HAPTIC_CUSTOM: + return &GUID_CustomForce; + + default: + return NULL; + } +} +bool SDL_DINPUT_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *base) +{ + HRESULT ret; + REFGUID type = SDL_SYS_HapticEffectType(base); + + if (!type) { + return SDL_SetError("Haptic: Unknown effect type."); + } + + // Get the effect. + if (!SDL_SYS_ToDIEFFECT(haptic, &effect->hweffect->effect, base)) { + goto err_effectdone; + } + + // Create the actual effect. + ret = IDirectInputDevice8_CreateEffect(haptic->hwdata->device, type, + &effect->hweffect->effect, + &effect->hweffect->ref, NULL); + if (FAILED(ret)) { + DI_SetError("Unable to create effect", ret); + goto err_effectdone; + } + + return true; + +err_effectdone: + SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, base->type); + return false; +} + +bool SDL_DINPUT_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *data) +{ + HRESULT ret; + DWORD flags; + DIEFFECT temp; + + // Get the effect. + SDL_memset(&temp, 0, sizeof(DIEFFECT)); + if (!SDL_SYS_ToDIEFFECT(haptic, &temp, data)) { + goto err_update; + } + + /* Set the flags. Might be worthwhile to diff temp with loaded effect and + * only change those parameters. */ + flags = DIEP_DIRECTION | + DIEP_DURATION | + DIEP_ENVELOPE | + DIEP_STARTDELAY | + DIEP_TRIGGERBUTTON | + DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS; + + // Create the actual effect. + ret = + IDirectInputEffect_SetParameters(effect->hweffect->ref, &temp, flags); + if (ret == DIERR_NOTEXCLUSIVEACQUIRED) { + IDirectInputDevice8_Unacquire(haptic->hwdata->device); + ret = IDirectInputDevice8_SetCooperativeLevel(haptic->hwdata->device, SDL_HelperWindow, DISCL_EXCLUSIVE | DISCL_BACKGROUND); + if (SUCCEEDED(ret)) { + ret = DIERR_NOTACQUIRED; + } + } + if (ret == DIERR_INPUTLOST || ret == DIERR_NOTACQUIRED) { + ret = IDirectInputDevice8_Acquire(haptic->hwdata->device); + if (SUCCEEDED(ret)) { + ret = IDirectInputEffect_SetParameters(effect->hweffect->ref, &temp, flags); + } + } + if (FAILED(ret)) { + DI_SetError("Unable to update effect", ret); + goto err_update; + } + + // Copy it over. + SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, data->type); + SDL_memcpy(&effect->hweffect->effect, &temp, sizeof(DIEFFECT)); + + return true; + +err_update: + SDL_SYS_HapticFreeDIEFFECT(&temp, data->type); + return false; +} + +bool SDL_DINPUT_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, Uint32 iterations) +{ + HRESULT ret; + DWORD iter; + + // Check if it's infinite. + if (iterations == SDL_HAPTIC_INFINITY) { + iter = INFINITE; + } else { + iter = iterations; + } + + // Run the effect. + ret = IDirectInputEffect_Start(effect->hweffect->ref, iter, 0); + if (FAILED(ret)) { + return DI_SetError("Running the effect", ret); + } + return true; +} + +bool SDL_DINPUT_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + HRESULT ret; + + ret = IDirectInputEffect_Stop(effect->hweffect->ref); + if (FAILED(ret)) { + return DI_SetError("Unable to stop effect", ret); + } + return true; +} + +void SDL_DINPUT_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + HRESULT ret; + + ret = IDirectInputEffect_Unload(effect->hweffect->ref); + if (FAILED(ret)) { + DI_SetError("Removing effect from the device", ret); + } + SDL_SYS_HapticFreeDIEFFECT(&effect->hweffect->effect, effect->effect.type); +} + +int SDL_DINPUT_HapticGetEffectStatus(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + HRESULT ret; + DWORD status; + + ret = IDirectInputEffect_GetEffectStatus(effect->hweffect->ref, &status); + if (FAILED(ret)) { + DI_SetError("Getting effect status", ret); + return -1; + } + + if (status == 0) { + return 0; + } + return 1; +} + +bool SDL_DINPUT_HapticSetGain(SDL_Haptic *haptic, int gain) +{ + HRESULT ret; + DIPROPDWORD dipdw; + + // Create the weird structure thingy. + dipdw.diph.dwSize = sizeof(DIPROPDWORD); + dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); + dipdw.diph.dwObj = 0; + dipdw.diph.dwHow = DIPH_DEVICE; + dipdw.dwData = (DWORD)gain * 100; // 0 to 10,000 + + // Try to set the autocenter. + ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device, + DIPROP_FFGAIN, &dipdw.diph); + if (FAILED(ret)) { + return DI_SetError("Setting gain", ret); + } + return true; +} + +bool SDL_DINPUT_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter) +{ + HRESULT ret; + DIPROPDWORD dipdw; + + // Create the weird structure thingy. + dipdw.diph.dwSize = sizeof(DIPROPDWORD); + dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); + dipdw.diph.dwObj = 0; + dipdw.diph.dwHow = DIPH_DEVICE; + dipdw.dwData = (autocenter == 0) ? DIPROPAUTOCENTER_OFF : DIPROPAUTOCENTER_ON; + + // Try to set the autocenter. + ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device, + DIPROP_AUTOCENTER, &dipdw.diph); + if (FAILED(ret)) { + return DI_SetError("Setting autocenter", ret); + } + return true; +} + +bool SDL_DINPUT_HapticPause(SDL_Haptic *haptic) +{ + HRESULT ret; + + // Pause the device. + ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device, + DISFFC_PAUSE); + if (FAILED(ret)) { + return DI_SetError("Pausing the device", ret); + } + return true; +} + +bool SDL_DINPUT_HapticResume(SDL_Haptic *haptic) +{ + HRESULT ret; + + // Unpause the device. + ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device, + DISFFC_CONTINUE); + if (FAILED(ret)) { + return DI_SetError("Pausing the device", ret); + } + return true; +} + +bool SDL_DINPUT_HapticStopAll(SDL_Haptic *haptic) +{ + HRESULT ret; + + // Try to stop the effects. + ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device, + DISFFC_STOPALL); + if (FAILED(ret)) { + return DI_SetError("Stopping the device", ret); + } + return true; +} + +#else // !SDL_HAPTIC_DINPUT + +typedef struct DIDEVICEINSTANCE DIDEVICEINSTANCE; +typedef struct SDL_hapticlist_item SDL_hapticlist_item; + +bool SDL_DINPUT_HapticInit(void) +{ + return true; +} + +bool SDL_DINPUT_HapticMaybeAddDevice(const DIDEVICEINSTANCE *pdidInstance) +{ + return SDL_Unsupported(); +} + +bool SDL_DINPUT_HapticMaybeRemoveDevice(const DIDEVICEINSTANCE *pdidInstance) +{ + return SDL_Unsupported(); +} + +bool SDL_DINPUT_HapticOpen(SDL_Haptic *haptic, SDL_hapticlist_item *item) +{ + return SDL_Unsupported(); +} + +bool SDL_DINPUT_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ + return false; +} + +bool SDL_DINPUT_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ + return SDL_Unsupported(); +} + +void SDL_DINPUT_HapticClose(SDL_Haptic *haptic) +{ +} + +void SDL_DINPUT_HapticQuit(void) +{ +} + +bool SDL_DINPUT_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *base) +{ + return SDL_Unsupported(); +} + +bool SDL_DINPUT_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *data) +{ + return SDL_Unsupported(); +} + +bool SDL_DINPUT_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, Uint32 iterations) +{ + return SDL_Unsupported(); +} + +bool SDL_DINPUT_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + return SDL_Unsupported(); +} + +void SDL_DINPUT_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ +} + +int SDL_DINPUT_HapticGetEffectStatus(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + SDL_Unsupported(); + return -1; +} + +bool SDL_DINPUT_HapticSetGain(SDL_Haptic *haptic, int gain) +{ + return SDL_Unsupported(); +} + +bool SDL_DINPUT_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter) +{ + return SDL_Unsupported(); +} + +bool SDL_DINPUT_HapticPause(SDL_Haptic *haptic) +{ + return SDL_Unsupported(); +} + +bool SDL_DINPUT_HapticResume(SDL_Haptic *haptic) +{ + return SDL_Unsupported(); +} + +bool SDL_DINPUT_HapticStopAll(SDL_Haptic *haptic) +{ + return SDL_Unsupported(); +} + +#endif // SDL_HAPTIC_DINPUT diff --git a/contrib/SDL-3.2.8/src/haptic/windows/SDL_dinputhaptic_c.h b/contrib/SDL-3.2.8/src/haptic/windows/SDL_dinputhaptic_c.h new file mode 100644 index 0000000..d6265c9 --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/windows/SDL_dinputhaptic_c.h @@ -0,0 +1,53 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#include "SDL_windowshaptic_c.h" + +// Set up for C function definitions, even when using C++ +#ifdef __cplusplus +extern "C" { +#endif + +extern bool SDL_DINPUT_HapticInit(void); +extern bool SDL_DINPUT_HapticMaybeAddDevice(const DIDEVICEINSTANCE *pdidInstance); +extern bool SDL_DINPUT_HapticMaybeRemoveDevice(const DIDEVICEINSTANCE *pdidInstance); +extern bool SDL_DINPUT_HapticOpen(SDL_Haptic *haptic, SDL_hapticlist_item *item); +extern bool SDL_DINPUT_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick); +extern bool SDL_DINPUT_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick); +extern void SDL_DINPUT_HapticClose(SDL_Haptic *haptic); +extern void SDL_DINPUT_HapticQuit(void); +extern bool SDL_DINPUT_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *base); +extern bool SDL_DINPUT_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *data); +extern bool SDL_DINPUT_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, Uint32 iterations); +extern bool SDL_DINPUT_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect); +extern void SDL_DINPUT_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect); +extern int SDL_DINPUT_HapticGetEffectStatus(SDL_Haptic *haptic, struct haptic_effect *effect); +extern bool SDL_DINPUT_HapticSetGain(SDL_Haptic *haptic, int gain); +extern bool SDL_DINPUT_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter); +extern bool SDL_DINPUT_HapticPause(SDL_Haptic *haptic); +extern bool SDL_DINPUT_HapticResume(SDL_Haptic *haptic); +extern bool SDL_DINPUT_HapticStopAll(SDL_Haptic *haptic); + +// Ends C function definitions when using C++ +#ifdef __cplusplus +} +#endif diff --git a/contrib/SDL-3.2.8/src/haptic/windows/SDL_windowshaptic.c b/contrib/SDL-3.2.8/src/haptic/windows/SDL_windowshaptic.c new file mode 100644 index 0000000..e21ab91 --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/windows/SDL_windowshaptic.c @@ -0,0 +1,369 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifdef SDL_HAPTIC_DINPUT + +#include "../SDL_syshaptic.h" +#include "../../joystick/SDL_sysjoystick.h" // For the real SDL_Joystick +#include "../../joystick/windows/SDL_windowsjoystick_c.h" // For joystick hwdata +#include "../../joystick/windows/SDL_xinputjoystick_c.h" // For xinput rumble + +#include "SDL_windowshaptic_c.h" +#include "SDL_dinputhaptic_c.h" + +// Set up for C function definitions, even when using C++ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Internal stuff. + */ +SDL_hapticlist_item *SDL_hapticlist = NULL; +static SDL_hapticlist_item *SDL_hapticlist_tail = NULL; +static int numhaptics = 0; + +/* + * Initializes the haptic subsystem. + */ +bool SDL_SYS_HapticInit(void) +{ + JoyStick_DeviceData *device; + + if (!SDL_DINPUT_HapticInit()) { + return false; + } + + /* The joystick subsystem will usually be initialized before haptics, + * so the initial HapticMaybeAddDevice() calls from the joystick + * subsystem will arrive too early to create haptic devices. We will + * invoke those callbacks again here to pick up any joysticks that + * were added prior to haptics initialization. */ + for (device = SYS_Joystick; device; device = device->pNext) { + SDL_DINPUT_HapticMaybeAddDevice(&device->dxdevice); + } + + return true; +} + +bool SDL_SYS_AddHapticDevice(SDL_hapticlist_item *item) +{ + if (!SDL_hapticlist_tail) { + SDL_hapticlist = SDL_hapticlist_tail = item; + } else { + SDL_hapticlist_tail->next = item; + SDL_hapticlist_tail = item; + } + + // Device has been added. + ++numhaptics; + + return true; +} + +bool SDL_SYS_RemoveHapticDevice(SDL_hapticlist_item *prev, SDL_hapticlist_item *item) +{ + const bool result = item->haptic ? true : false; + if (prev) { + prev->next = item->next; + } else { + SDL_assert(SDL_hapticlist == item); + SDL_hapticlist = item->next; + } + if (item == SDL_hapticlist_tail) { + SDL_hapticlist_tail = prev; + } + --numhaptics; + // !!! TODO: Send a haptic remove event? + SDL_free(item); + return result; +} + +int SDL_SYS_NumHaptics(void) +{ + return numhaptics; +} + +static SDL_hapticlist_item *HapticByDevIndex(int device_index) +{ + SDL_hapticlist_item *item = SDL_hapticlist; + + if ((device_index < 0) || (device_index >= numhaptics)) { + return NULL; + } + + while (device_index > 0) { + SDL_assert(item != NULL); + --device_index; + item = item->next; + } + return item; +} + +static SDL_hapticlist_item *HapticByInstanceID(SDL_HapticID instance_id) +{ + SDL_hapticlist_item *item; + for (item = SDL_hapticlist; item; item = item->next) { + if (instance_id == item->instance_id) { + return item; + } + } + return NULL; +} + +SDL_HapticID SDL_SYS_HapticInstanceID(int index) +{ + SDL_hapticlist_item *item = HapticByDevIndex(index); + if (item) { + return item->instance_id; + } + return 0; +} + +/* + * Return the name of a haptic device, does not need to be opened. + */ +const char *SDL_SYS_HapticName(int index) +{ + SDL_hapticlist_item *item = HapticByDevIndex(index); + return item->name; +} + +/* + * Opens a haptic device for usage. + */ +bool SDL_SYS_HapticOpen(SDL_Haptic *haptic) +{ + SDL_hapticlist_item *item = HapticByInstanceID(haptic->instance_id); + return SDL_DINPUT_HapticOpen(haptic, item); +} + +/* + * Opens a haptic device from first mouse it finds for usage. + */ +int SDL_SYS_HapticMouse(void) +{ +#ifdef SDL_HAPTIC_DINPUT + SDL_hapticlist_item *item; + int index = 0; + + // Grab the first mouse haptic device we find. + for (item = SDL_hapticlist; item; item = item->next) { + if (item->capabilities.dwDevType == DI8DEVCLASS_POINTER) { + return index; + } + ++index; + } +#endif // SDL_HAPTIC_DINPUT + return -1; +} + +/* + * Checks to see if a joystick has haptic features. + */ +bool SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick) +{ + if (joystick->driver != &SDL_WINDOWS_JoystickDriver) { + return false; + } + if (joystick->hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) { + return true; + } + return false; +} + +/* + * Checks to see if the haptic device and joystick are in reality the same. + */ +bool SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ + if (joystick->driver != &SDL_WINDOWS_JoystickDriver) { + return false; + } + return SDL_DINPUT_JoystickSameHaptic(haptic, joystick); +} + +/* + * Opens a SDL_Haptic from a SDL_Joystick. + */ +bool SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick) +{ + SDL_assert(joystick->driver == &SDL_WINDOWS_JoystickDriver); + + return SDL_DINPUT_HapticOpenFromJoystick(haptic, joystick); +} + +/* + * Closes the haptic device. + */ +void SDL_SYS_HapticClose(SDL_Haptic *haptic) +{ + if (haptic->hwdata) { + + // Free effects. + SDL_free(haptic->effects); + haptic->effects = NULL; + haptic->neffects = 0; + + // Clean up + SDL_DINPUT_HapticClose(haptic); + + // Free + SDL_free(haptic->hwdata); + haptic->hwdata = NULL; + } +} + +/* + * Clean up after system specific haptic stuff + */ +void SDL_SYS_HapticQuit(void) +{ + SDL_hapticlist_item *item; + SDL_hapticlist_item *next = NULL; + + for (item = SDL_hapticlist; item; item = next) { + /* Opened and not closed haptics are leaked, this is on purpose. + * Close your haptic devices after usage. */ + // !!! FIXME: (...is leaking on purpose a good idea?) - No, of course not. + next = item->next; + SDL_free(item->name); + SDL_free(item); + } + + SDL_DINPUT_HapticQuit(); + + numhaptics = 0; + SDL_hapticlist = NULL; + SDL_hapticlist_tail = NULL; +} + +/* + * Creates a new haptic effect. + */ +bool SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect, + const SDL_HapticEffect *base) +{ + bool result; + + // Alloc the effect. + effect->hweffect = (struct haptic_hweffect *) SDL_calloc(1, sizeof(struct haptic_hweffect)); + if (!effect->hweffect) { + return false; + } + + result = SDL_DINPUT_HapticNewEffect(haptic, effect, base); + if (!result) { + SDL_free(effect->hweffect); + effect->hweffect = NULL; + } + return result; +} + +/* + * Updates an effect. + */ +bool SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic, struct haptic_effect *effect, const SDL_HapticEffect *data) +{ + return SDL_DINPUT_HapticUpdateEffect(haptic, effect, data); +} + +/* + * Runs an effect. + */ +bool SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect, Uint32 iterations) +{ + return SDL_DINPUT_HapticRunEffect(haptic, effect, iterations); +} + +/* + * Stops an effect. + */ +bool SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + return SDL_DINPUT_HapticStopEffect(haptic, effect); +} + +/* + * Frees the effect. + */ +void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + SDL_DINPUT_HapticDestroyEffect(haptic, effect); + SDL_free(effect->hweffect); + effect->hweffect = NULL; +} + +/* + * Gets the status of a haptic effect. + */ +int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic, struct haptic_effect *effect) +{ + return SDL_DINPUT_HapticGetEffectStatus(haptic, effect); +} + +/* + * Sets the gain. + */ +bool SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain) +{ + return SDL_DINPUT_HapticSetGain(haptic, gain); +} + +/* + * Sets the autocentering. + */ +bool SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter) +{ + return SDL_DINPUT_HapticSetAutocenter(haptic, autocenter); +} + +/* + * Pauses the device. + */ +bool SDL_SYS_HapticPause(SDL_Haptic *haptic) +{ + return SDL_DINPUT_HapticPause(haptic); +} + +/* + * Pauses the device. + */ +bool SDL_SYS_HapticResume(SDL_Haptic *haptic) +{ + return SDL_DINPUT_HapticResume(haptic); +} + +/* + * Stops all the playing effects on the device. + */ +bool SDL_SYS_HapticStopAll(SDL_Haptic *haptic) +{ + return SDL_DINPUT_HapticStopAll(haptic); +} + +// Ends C function definitions when using C++ +#ifdef __cplusplus +} +#endif + +#endif // SDL_HAPTIC_DINPUT diff --git a/contrib/SDL-3.2.8/src/haptic/windows/SDL_windowshaptic_c.h b/contrib/SDL-3.2.8/src/haptic/windows/SDL_windowshaptic_c.h new file mode 100644 index 0000000..c4b6928 --- /dev/null +++ b/contrib/SDL-3.2.8/src/haptic/windows/SDL_windowshaptic_c.h @@ -0,0 +1,87 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ +#include "SDL_internal.h" + +#ifndef SDL_windowshaptic_c_h_ +#define SDL_windowshaptic_c_h_ + +#include "../SDL_syshaptic.h" +#include "../../core/windows/SDL_directx.h" +#include "../../core/windows/SDL_xinput.h" + +// Set up for C function definitions, even when using C++ +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Haptic system hardware data. + */ +struct haptic_hwdata +{ +#ifdef SDL_HAPTIC_DINPUT + LPDIRECTINPUTDEVICE8 device; +#endif + DWORD axes[3]; // Axes to use. + bool is_joystick; // Device is loaded as joystick. + SDL_Thread *thread; + SDL_Mutex *mutex; + Uint64 stopTicks; + SDL_AtomicInt stopThread; +}; + +/* + * Haptic system effect data. + */ +#ifdef SDL_HAPTIC_DINPUT +struct haptic_hweffect +{ + DIEFFECT effect; + LPDIRECTINPUTEFFECT ref; +}; +#endif + +/* + * List of available haptic devices. + */ +typedef struct SDL_hapticlist_item +{ + SDL_HapticID instance_id; + char *name; + SDL_Haptic *haptic; +#ifdef SDL_HAPTIC_DINPUT + DIDEVICEINSTANCE instance; + DIDEVCAPS capabilities; +#endif + struct SDL_hapticlist_item *next; +} SDL_hapticlist_item; + +extern SDL_hapticlist_item *SDL_hapticlist; + +extern bool SDL_SYS_AddHapticDevice(SDL_hapticlist_item *item); +extern bool SDL_SYS_RemoveHapticDevice(SDL_hapticlist_item *prev, SDL_hapticlist_item *item); + +// Ends C function definitions when using C++ +#ifdef __cplusplus +} +#endif + +#endif // SDL_windowshaptic_c_h_ -- cgit v1.2.3