IGameInputDevice::GetDeviceInfo() can never return a null pointer

The IGameInputDevice instance can't be internally instantiated without it, so it's always guaranteed to be present.
This commit is contained in:
Sam Lantinga
2024-02-17 19:18:04 -08:00
parent fbe4153214
commit 85ac0381b7

View File

@@ -76,15 +76,7 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
char tmp[4]; char tmp[4];
int idx = 0; int idx = 0;
if (!pDevice) {
return SDL_SetError("GAMEINPUT_InternalAddOrFind argument pDevice cannot be NULL");
}
devinfo = IGameInputDevice_GetDeviceInfo(pDevice); devinfo = IGameInputDevice_GetDeviceInfo(pDevice);
if (!devinfo) {
return SDL_SetError("GAMEINPUT_InternalAddOrFind GetDeviceInfo returned NULL");
}
if (devinfo->capabilities & GameInputDeviceCapabilityWireless) { if (devinfo->capabilities & GameInputDeviceCapabilityWireless) {
bus = SDL_HARDWARE_BUS_BLUETOOTH; bus = SDL_HARDWARE_BUS_BLUETOOTH;
} else { } else {
@@ -102,7 +94,7 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
elem = g_GameInputList.devices[idx]; elem = g_GameInputList.devices[idx];
if (elem && elem->device == pDevice) { if (elem && elem->device == pDevice) {
/* we're already added */ /* we're already added */
return idx; return 0;
} }
} }
@@ -123,7 +115,6 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
SDL_strlcat(elem->path, tmp, SDL_arraysize(tmp)); SDL_strlcat(elem->path, tmp, SDL_arraysize(tmp));
} }
g_GameInputList.devices = devicelist;
IGameInputDevice_AddRef(pDevice); IGameInputDevice_AddRef(pDevice);
elem->device = pDevice; elem->device = pDevice;
elem->name = "GameInput Gamepad"; elem->name = "GameInput Gamepad";
@@ -133,10 +124,11 @@ static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
elem->device_instance = SDL_GetNextObjectID(); elem->device_instance = SDL_GetNextObjectID();
elem->wireless = (devinfo->capabilities & GameInputDeviceCapabilityWireless); elem->wireless = (devinfo->capabilities & GameInputDeviceCapabilityWireless);
elem->supportedRumbleMotors = devinfo->supportedRumbleMotors; elem->supportedRumbleMotors = devinfo->supportedRumbleMotors;
g_GameInputList.devices[g_GameInputList.count] = elem;
/* finally increment the count and return */ g_GameInputList.devices = devicelist;
return g_GameInputList.count++; g_GameInputList.devices[g_GameInputList.count++] = elem;
return 0;
} }
static int GAMEINPUT_InternalRemoveByIndex(int idx) static int GAMEINPUT_InternalRemoveByIndex(int idx)
@@ -194,6 +186,11 @@ static void CALLBACK GAMEINPUT_InternalJoystickDeviceCallback(
int idx = 0; int idx = 0;
GAMEINPUT_InternalDevice *elem = NULL; GAMEINPUT_InternalDevice *elem = NULL;
if (!device) {
/* This should never happen, but ignore it if it does */
return;
}
if (currentStatus & GameInputDeviceConnected) { if (currentStatus & GameInputDeviceConnected) {
GAMEINPUT_InternalAddOrFind(device); GAMEINPUT_InternalAddOrFind(device);
} else { } else {