SDL_GetHaptics() follows the SDL_GetStringRule

This commit is contained in:
Sam Lantinga 2024-07-18 17:08:30 -07:00
parent 9de8cb888a
commit b32c9615a7
7 changed files with 12 additions and 23 deletions

View file

@ -695,7 +695,7 @@ Rather than iterating over haptic devices using device index, there is a new fun
{ {
if (SDL_InitSubSystem(SDL_INIT_HAPTIC) == 0) { if (SDL_InitSubSystem(SDL_INIT_HAPTIC) == 0) {
int i, num_haptics; int i, num_haptics;
SDL_HapticID *haptics = SDL_GetHaptics(&num_haptics); const SDL_HapticID *haptics = SDL_GetHaptics(&num_haptics);
if (haptics) { if (haptics) {
for (i = 0; i < num_haptics; ++i) { for (i = 0; i < num_haptics; ++i) {
SDL_HapticID instance_id = haptics[i]; SDL_HapticID instance_id = haptics[i];
@ -704,7 +704,6 @@ Rather than iterating over haptic devices using device index, there is a new fun
SDL_Log("Haptic %" SDL_PRIu32 ": %s\n", SDL_Log("Haptic %" SDL_PRIu32 ": %s\n",
instance_id, name ? name : "Unknown"); instance_id, name ? name : "Unknown");
} }
SDL_free(haptics);
} }
SDL_QuitSubSystem(SDL_INIT_HAPTIC); SDL_QuitSubSystem(SDL_INIT_HAPTIC);
} }

View file

@ -45,7 +45,6 @@
* SDL_HapticID *haptics = SDL_GetHaptics(NULL); * SDL_HapticID *haptics = SDL_GetHaptics(NULL);
* if (haptics) { * if (haptics) {
* haptic = SDL_OpenHaptic(haptics[0]); * haptic = SDL_OpenHaptic(haptics[0]);
* SDL_free(haptics);
* } * }
* if (haptic == NULL) * if (haptic == NULL)
* return -1; * return -1;
@ -932,17 +931,18 @@ typedef Uint32 SDL_HapticID;
/** /**
* Get a list of currently connected haptic devices. * Get a list of currently connected haptic devices.
* *
* The returned array follows the SDL_GetStringRule, and will be automatically freed later.
*
* \param count a pointer filled in with the number of haptic devices * \param count a pointer filled in with the number of haptic devices
* returned. * returned, may be NULL.
* \returns a 0 terminated array of haptic device instance IDs which should be * \returns a 0 terminated array of haptic device instance IDs or NULL on failure; call SDL_GetError() for
* freed with SDL_free(), or NULL on failure; call SDL_GetError() for
* more information. * more information.
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_OpenHaptic * \sa SDL_OpenHaptic
*/ */
extern SDL_DECLSPEC SDL_HapticID * SDLCALL SDL_GetHaptics(int *count); extern SDL_DECLSPEC const SDL_HapticID * SDLCALL SDL_GetHaptics(int *count);
/** /**
* Get the implementation dependent name of a haptic device. * Get the implementation dependent name of a haptic device.

View file

@ -327,7 +327,7 @@ SDL_DYNAPI_PROC(SDL_Haptic*,SDL_GetHapticFromID,(SDL_HapticID a),(a),return)
SDL_DYNAPI_PROC(SDL_HapticID,SDL_GetHapticID,(SDL_Haptic *a),(a),return) SDL_DYNAPI_PROC(SDL_HapticID,SDL_GetHapticID,(SDL_Haptic *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetHapticName,(SDL_Haptic *a),(a),return) SDL_DYNAPI_PROC(const char*,SDL_GetHapticName,(SDL_Haptic *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetHapticNameForID,(SDL_HapticID a),(a),return) SDL_DYNAPI_PROC(const char*,SDL_GetHapticNameForID,(SDL_HapticID a),(a),return)
SDL_DYNAPI_PROC(SDL_HapticID*,SDL_GetHaptics,(int *a),(a),return) SDL_DYNAPI_PROC(const SDL_HapticID*,SDL_GetHaptics,(int *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetHint,(const char *a),(a),return) SDL_DYNAPI_PROC(const char*,SDL_GetHint,(const char *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_GetHintBoolean,(const char *a, SDL_bool b),(a,b),return) SDL_DYNAPI_PROC(SDL_bool,SDL_GetHintBoolean,(const char *a, SDL_bool b),(a,b),return)
SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetIOProperties,(SDL_IOStream *a),(a),return) SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetIOProperties,(SDL_IOStream *a),(a),return)

View file

@ -63,7 +63,7 @@ static SDL_bool SDL_GetHapticIndex(SDL_HapticID instance_id, int *driver_index)
return SDL_FALSE; return SDL_FALSE;
} }
SDL_HapticID *SDL_GetHaptics(int *count) const SDL_HapticID *SDL_GetHaptics(int *count)
{ {
int device_index; int device_index;
int haptic_index = 0, num_haptics = 0; int haptic_index = 0, num_haptics = 0;
@ -89,7 +89,7 @@ SDL_HapticID *SDL_GetHaptics(int *count)
} }
} }
return haptics; return SDL_FreeLater(haptics);
} }
const char *SDL_GetHapticNameForID(SDL_HapticID instance_id) const char *SDL_GetHapticNameForID(SDL_HapticID instance_id)

View file

@ -40,7 +40,7 @@ int main(int argc, char **argv)
int id[9]; int id[9];
int nefx; int nefx;
unsigned int supported; unsigned int supported;
SDL_HapticID *haptics; const SDL_HapticID *haptics;
int num_haptics; int num_haptics;
/* Initialize test framework */ /* Initialize test framework */
@ -91,7 +91,6 @@ int main(int argc, char **argv)
if (haptics) { if (haptics) {
if (num_haptics == 0) { if (num_haptics == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
SDL_free(haptics);
return 1; return 1;
} }
@ -101,7 +100,6 @@ int main(int argc, char **argv)
if (i >= num_haptics) { if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index out of range, aborting.\n"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index out of range, aborting.\n");
SDL_free(haptics);
return 1; return 1;
} }
} }
@ -115,7 +113,6 @@ int main(int argc, char **argv)
if (i >= num_haptics) { if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n", name); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n", name);
SDL_free(haptics);
return 1; return 1;
} }
} }
@ -123,12 +120,10 @@ int main(int argc, char **argv)
haptic = SDL_OpenHaptic(haptics[i]); haptic = SDL_OpenHaptic(haptics[i]);
if (!haptic) { if (!haptic) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n", SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n", SDL_GetError());
SDL_free(haptics);
return 1; return 1;
} }
SDL_Log("Device: %s\n", SDL_GetHapticName(haptic)); SDL_Log("Device: %s\n", SDL_GetHapticName(haptic));
HapticPrintSupported(haptic); HapticPrintSupported(haptic);
SDL_free(haptics);
} }
/* We only want force feedback errors. */ /* We only want force feedback errors. */

View file

@ -91,7 +91,7 @@ int main(int argc, char *argv[])
if (enable_haptic) { if (enable_haptic) {
int num_haptics; int num_haptics;
SDL_free(SDL_GetHaptics(&num_haptics)); SDL_GetHaptics(&num_haptics);
SDL_Log("There are %d haptic devices at startup\n", num_haptics); SDL_Log("There are %d haptic devices at startup\n", num_haptics);
} }

View file

@ -39,7 +39,7 @@ int main(int argc, char **argv)
char *name = NULL; char *name = NULL;
int index; int index;
SDLTest_CommonState *state; SDLTest_CommonState *state;
SDL_HapticID *haptics; const SDL_HapticID *haptics;
int num_haptics; int num_haptics;
/* Initialize test framework */ /* Initialize test framework */
@ -92,7 +92,6 @@ int main(int argc, char **argv)
if (haptics) { if (haptics) {
if (num_haptics == 0) { if (num_haptics == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
SDL_free(haptics);
return 1; return 1;
} }
@ -102,7 +101,6 @@ int main(int argc, char **argv)
if (i >= num_haptics) { if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index out of range, aborting.\n"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Index out of range, aborting.\n");
SDL_free(haptics);
return 1; return 1;
} }
} }
@ -116,7 +114,6 @@ int main(int argc, char **argv)
if (i >= num_haptics) { if (i >= num_haptics) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n", name); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n", name);
SDL_free(haptics);
return 1; return 1;
} }
} }
@ -124,11 +121,9 @@ int main(int argc, char **argv)
haptic = SDL_OpenHaptic(haptics[i]); haptic = SDL_OpenHaptic(haptics[i]);
if (!haptic) { if (!haptic) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n", SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n", SDL_GetError());
SDL_free(haptics);
return 1; return 1;
} }
SDL_Log("Device: %s\n", SDL_GetHapticName(haptic)); SDL_Log("Device: %s\n", SDL_GetHapticName(haptic));
SDL_free(haptics);
} }
/* We only want force feedback errors. */ /* We only want force feedback errors. */