Updated gamepad, joystick, sensor APIs, removing device indices
Instead of indexing into an internal list of devices which requires locking, we return a list of device IDs which can then be queried individually. Reference: https://github.com/libsdl-org/SDL/issues/6889
This commit is contained in:
@@ -704,8 +704,10 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
const char *name;
|
||||
int i;
|
||||
SDL_JoystickID *joysticks;
|
||||
int num_joysticks = 0;
|
||||
int joystick_index;
|
||||
SDL_Joystick *joystick;
|
||||
SDL_Joystick *joystick = NULL;
|
||||
|
||||
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
|
||||
|
||||
@@ -737,7 +739,7 @@ int main(int argc, char *argv[])
|
||||
return 2;
|
||||
}
|
||||
|
||||
while (!done && SDL_GetNumJoysticks() == 0) {
|
||||
while (!done && !SDL_HasJoysticks()) {
|
||||
SDL_Event event;
|
||||
|
||||
while (SDL_PollEvent(&event) > 0) {
|
||||
@@ -758,25 +760,30 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
/* Print information about the joysticks */
|
||||
SDL_Log("There are %d joysticks attached\n", SDL_GetNumJoysticks());
|
||||
for (i = 0; i < SDL_GetNumJoysticks(); ++i) {
|
||||
name = SDL_GetJoystickNameForIndex(i);
|
||||
SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
|
||||
joystick = SDL_OpenJoystick(i);
|
||||
if (joystick == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_OpenJoystick(%d) failed: %s\n", i,
|
||||
SDL_GetError());
|
||||
} else {
|
||||
char guid[64];
|
||||
SDL_GetJoystickGUIDString(SDL_GetJoystickGUID(joystick),
|
||||
guid, sizeof(guid));
|
||||
SDL_Log(" axes: %d\n", SDL_GetNumJoystickAxes(joystick));
|
||||
SDL_Log(" hats: %d\n", SDL_GetNumJoystickHats(joystick));
|
||||
SDL_Log(" buttons: %d\n", SDL_GetNumJoystickButtons(joystick));
|
||||
SDL_Log("instance id: %" SDL_PRIu32 "\n", SDL_GetJoystickInstanceID(joystick));
|
||||
SDL_Log(" guid: %s\n", guid);
|
||||
SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_GetJoystickVendor(joystick), SDL_GetJoystickProduct(joystick));
|
||||
SDL_CloseJoystick(joystick);
|
||||
joysticks = SDL_GetJoysticks(&num_joysticks);
|
||||
if (joysticks) {
|
||||
SDL_Log("There are %d joysticks attached\n", num_joysticks);
|
||||
for (i = 0; joysticks[i]; ++i) {
|
||||
SDL_JoystickID instance_id = joysticks[i];
|
||||
|
||||
name = SDL_GetJoystickInstanceName(instance_id);
|
||||
SDL_Log("Joystick %" SDL_PRIu32 ": %s\n", instance_id, name ? name : "Unknown Joystick");
|
||||
joystick = SDL_OpenJoystick(instance_id);
|
||||
if (joystick == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_OpenJoystick(%" SDL_PRIu32 ") failed: %s\n", instance_id,
|
||||
SDL_GetError());
|
||||
} else {
|
||||
char guid[64];
|
||||
SDL_GetJoystickGUIDString(SDL_GetJoystickGUID(joystick),
|
||||
guid, sizeof(guid));
|
||||
SDL_Log(" axes: %d\n", SDL_GetNumJoystickAxes(joystick));
|
||||
SDL_Log(" hats: %d\n", SDL_GetNumJoystickHats(joystick));
|
||||
SDL_Log(" buttons: %d\n", SDL_GetNumJoystickButtons(joystick));
|
||||
SDL_Log("instance id: %" SDL_PRIu32 "\n", instance_id);
|
||||
SDL_Log(" guid: %s\n", guid);
|
||||
SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_GetJoystickVendor(joystick), SDL_GetJoystickProduct(joystick));
|
||||
SDL_CloseJoystick(joystick);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -787,7 +794,9 @@ int main(int argc, char *argv[])
|
||||
break;
|
||||
}
|
||||
}
|
||||
joystick = SDL_OpenJoystick(joystick_index);
|
||||
if (joysticks && joystick_index < num_joysticks) {
|
||||
joystick = SDL_OpenJoystick(joysticks[joystick_index]);
|
||||
}
|
||||
if (joystick == NULL) {
|
||||
SDL_Log("Couldn't open joystick %d: %s\n", joystick_index, SDL_GetError());
|
||||
} else {
|
||||
@@ -795,6 +804,8 @@ int main(int argc, char *argv[])
|
||||
SDL_CloseJoystick(joystick);
|
||||
}
|
||||
|
||||
SDL_free(joysticks);
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
|
||||
|
||||
@@ -20,7 +20,7 @@ TestVirtualJoystick(void *arg)
|
||||
{
|
||||
SDL_VirtualJoystickDesc desc;
|
||||
SDL_Joystick *joystick = NULL;
|
||||
int device_index;
|
||||
SDL_JoystickID device_id;
|
||||
|
||||
SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMEPAD) == 0, "SDL_InitSubSystem(SDL_INIT_GAMEPAD)");
|
||||
|
||||
@@ -32,11 +32,11 @@ TestVirtualJoystick(void *arg)
|
||||
desc.vendor_id = USB_VENDOR_NVIDIA;
|
||||
desc.product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104;
|
||||
desc.name = "Virtual NVIDIA SHIELD Controller";
|
||||
device_index = SDL_AttachVirtualJoystickEx(&desc);
|
||||
SDLTest_AssertCheck(device_index >= 0, "SDL_AttachVirtualJoystickEx()");
|
||||
SDLTest_AssertCheck(SDL_IsJoystickVirtual(device_index), "SDL_IsJoystickVirtual()");
|
||||
if (device_index >= 0) {
|
||||
joystick = SDL_OpenJoystick(device_index);
|
||||
device_id = SDL_AttachVirtualJoystickEx(&desc);
|
||||
SDLTest_AssertCheck(device_id > 0, "SDL_AttachVirtualJoystickEx()");
|
||||
SDLTest_AssertCheck(SDL_IsJoystickVirtual(device_id), "SDL_IsJoystickVirtual()");
|
||||
if (device_id > 0) {
|
||||
joystick = SDL_OpenJoystick(device_id);
|
||||
SDLTest_AssertCheck(joystick != NULL, "SDL_OpenJoystick()");
|
||||
if (joystick) {
|
||||
SDLTest_AssertCheck(SDL_strcmp(SDL_GetJoystickName(joystick), desc.name) == 0, "SDL_GetJoystickName()");
|
||||
@@ -59,9 +59,9 @@ TestVirtualJoystick(void *arg)
|
||||
|
||||
SDL_CloseJoystick(joystick);
|
||||
}
|
||||
SDLTest_AssertCheck(SDL_DetachVirtualJoystick(device_index) == 0, "SDL_DetachVirtualJoystick()");
|
||||
SDLTest_AssertCheck(SDL_DetachVirtualJoystick(device_id) == 0, "SDL_DetachVirtualJoystick()");
|
||||
}
|
||||
SDLTest_AssertCheck(!SDL_IsJoystickVirtual(device_index), "!SDL_IsJoystickVirtual()");
|
||||
SDLTest_AssertCheck(!SDL_IsJoystickVirtual(device_id), "!SDL_IsJoystickVirtual()");
|
||||
|
||||
SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
|
||||
|
||||
|
||||
@@ -165,9 +165,8 @@ static int FindGamepad(SDL_JoystickID gamepad_id)
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void AddGamepad(int device_index, SDL_bool verbose)
|
||||
static void AddGamepad(SDL_JoystickID gamepad_id, SDL_bool verbose)
|
||||
{
|
||||
SDL_JoystickID gamepad_id = SDL_GetJoystickDeviceInstanceID(device_index);
|
||||
SDL_Gamepad **new_gamepads;
|
||||
SDL_Gamepad *new_gamepad;
|
||||
Uint16 firmware_version;
|
||||
@@ -181,18 +180,12 @@ static void AddGamepad(int device_index, SDL_bool verbose)
|
||||
};
|
||||
unsigned int i;
|
||||
|
||||
gamepad_id = SDL_GetJoystickDeviceInstanceID(device_index);
|
||||
if (gamepad_id < 0) {
|
||||
SDL_Log("Couldn't get gamepad ID: %s\n", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
|
||||
if (FindGamepad(gamepad_id) >= 0) {
|
||||
/* We already have this gamepad */
|
||||
return;
|
||||
}
|
||||
|
||||
new_gamepad = SDL_OpenGamepad(device_index);
|
||||
new_gamepad = SDL_OpenGamepad(gamepad_id);
|
||||
if (new_gamepad == NULL) {
|
||||
SDL_Log("Couldn't open gamepad: %s\n", SDL_GetError());
|
||||
return;
|
||||
@@ -388,7 +381,7 @@ static int SDLCALL VirtualGamepadSetLED(void *userdata, Uint8 red, Uint8 green,
|
||||
static void OpenVirtualGamepad()
|
||||
{
|
||||
SDL_VirtualJoystickDesc desc;
|
||||
int virtual_index;
|
||||
SDL_JoystickID virtual_id;
|
||||
|
||||
SDL_zero(desc);
|
||||
desc.version = SDL_VIRTUAL_JOYSTICK_DESC_VERSION;
|
||||
@@ -400,11 +393,11 @@ static void OpenVirtualGamepad()
|
||||
desc.RumbleTriggers = VirtualGamepadRumbleTriggers;
|
||||
desc.SetLED = VirtualGamepadSetLED;
|
||||
|
||||
virtual_index = SDL_AttachVirtualJoystickEx(&desc);
|
||||
if (virtual_index < 0) {
|
||||
SDL_Log("Couldn't open virtual device: %s\n", SDL_GetError());
|
||||
virtual_id = SDL_AttachVirtualJoystickEx(&desc);
|
||||
if (virtual_id == 0) {
|
||||
SDL_Log("Couldn't attach virtual device: %s\n", SDL_GetError());
|
||||
} else {
|
||||
virtual_joystick = SDL_OpenJoystick(virtual_index);
|
||||
virtual_joystick = SDL_OpenJoystick(virtual_id);
|
||||
if (virtual_joystick == NULL) {
|
||||
SDL_Log("Couldn't open virtual device: %s\n", SDL_GetError());
|
||||
}
|
||||
@@ -414,11 +407,15 @@ static void OpenVirtualGamepad()
|
||||
static void CloseVirtualGamepad()
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = SDL_GetNumJoysticks(); i--;) {
|
||||
if (SDL_IsJoystickVirtual(i)) {
|
||||
SDL_DetachVirtualJoystick(i);
|
||||
SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL);
|
||||
if (joysticks) {
|
||||
for (i = 0; joysticks[i]; ++i) {
|
||||
SDL_JoystickID instance_id = joysticks[i];
|
||||
if (SDL_IsJoystickVirtual(instance_id)) {
|
||||
SDL_DetachVirtualJoystick(instance_id);
|
||||
}
|
||||
}
|
||||
SDL_free(joysticks);
|
||||
}
|
||||
|
||||
if (virtual_joystick) {
|
||||
@@ -566,19 +563,19 @@ void loop(void *arg)
|
||||
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1) {
|
||||
switch (event.type) {
|
||||
case SDL_GAMEPADADDED:
|
||||
SDL_Log("Gamepad device %d added.\n", (int)SDL_GetJoystickDeviceInstanceID(event.cdevice.which));
|
||||
SDL_Log("Gamepad device %" SDL_PRIu32 " added.\n", event.cdevice.which);
|
||||
AddGamepad(event.cdevice.which, SDL_TRUE);
|
||||
break;
|
||||
|
||||
case SDL_GAMEPADREMOVED:
|
||||
SDL_Log("Gamepad device %d removed.\n", (int)event.cdevice.which);
|
||||
SDL_Log("Gamepad device %" SDL_PRIu32 " removed.\n", event.cdevice.which);
|
||||
DelGamepad(event.cdevice.which);
|
||||
break;
|
||||
|
||||
case SDL_GAMEPADTOUCHPADDOWN:
|
||||
case SDL_GAMEPADTOUCHPADMOTION:
|
||||
case SDL_GAMEPADTOUCHPADUP:
|
||||
SDL_Log("Gamepad %" SDL_PRIs32 " touchpad %" SDL_PRIs32 " finger %" SDL_PRIs32 " %s %.2f, %.2f, %.2f\n",
|
||||
SDL_Log("Gamepad %" SDL_PRIu32 " touchpad %" SDL_PRIs32 " finger %" SDL_PRIs32 " %s %.2f, %.2f, %.2f\n",
|
||||
event.ctouchpad.which,
|
||||
event.ctouchpad.touchpad,
|
||||
event.ctouchpad.finger,
|
||||
@@ -591,7 +588,7 @@ void loop(void *arg)
|
||||
#define VERBOSE_SENSORS
|
||||
#ifdef VERBOSE_SENSORS
|
||||
case SDL_GAMEPADSENSORUPDATE:
|
||||
SDL_Log("Gamepad %" SDL_PRIs32 " sensor %s: %.2f, %.2f, %.2f (%" SDL_PRIu64 ")\n",
|
||||
SDL_Log("Gamepad %" SDL_PRIu32 " sensor %s: %.2f, %.2f, %.2f (%" SDL_PRIu64 ")\n",
|
||||
event.csensor.which,
|
||||
GetSensorName((SDL_SensorType)event.csensor.sensor),
|
||||
event.csensor.data[0],
|
||||
@@ -607,7 +604,7 @@ void loop(void *arg)
|
||||
if (event.caxis.value <= (-SDL_JOYSTICK_AXIS_MAX / 2) || event.caxis.value >= (SDL_JOYSTICK_AXIS_MAX / 2)) {
|
||||
SetGamepad(event.caxis.which);
|
||||
}
|
||||
SDL_Log("Gamepad %" SDL_PRIs32 " axis %s changed to %d\n", event.caxis.which, SDL_GetGamepadStringForAxis((SDL_GamepadAxis)event.caxis.axis), event.caxis.value);
|
||||
SDL_Log("Gamepad %" SDL_PRIu32 " axis %s changed to %d\n", event.caxis.which, SDL_GetGamepadStringForAxis((SDL_GamepadAxis)event.caxis.axis), event.caxis.value);
|
||||
break;
|
||||
#endif /* VERBOSE_AXES */
|
||||
|
||||
@@ -616,7 +613,7 @@ void loop(void *arg)
|
||||
if (event.type == SDL_GAMEPADBUTTONDOWN) {
|
||||
SetGamepad(event.cbutton.which);
|
||||
}
|
||||
SDL_Log("Gamepad %" SDL_PRIs32 " button %s %s\n", event.cbutton.which, SDL_GetGamepadStringForButton((SDL_GamepadButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released");
|
||||
SDL_Log("Gamepad %" SDL_PRIu32 " button %s %s\n", event.cbutton.which, SDL_GetGamepadStringForButton((SDL_GamepadButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released");
|
||||
|
||||
/* Cycle PS5 trigger effects when the microphone button is pressed */
|
||||
if (event.type == SDL_GAMEPADBUTTONDOWN &&
|
||||
@@ -627,7 +624,7 @@ void loop(void *arg)
|
||||
break;
|
||||
|
||||
case SDL_JOYBATTERYUPDATED:
|
||||
SDL_Log("Gamepad %" SDL_PRIs32 " battery state changed to %s\n", event.jbattery.which, power_level_strings[event.jbattery.level + 1]);
|
||||
SDL_Log("Gamepad %" SDL_PRIu32 " battery state changed to %s\n", event.jbattery.which, power_level_strings[event.jbattery.level + 1]);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
@@ -786,6 +783,8 @@ void loop(void *arg)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
SDL_JoystickID *joysticks;
|
||||
int joystick_count = 0;
|
||||
int gamepad_count = 0;
|
||||
int gamepad_index = 0;
|
||||
char guid[64];
|
||||
@@ -822,67 +821,74 @@ int main(int argc, char *argv[])
|
||||
SDL_Log("\n");
|
||||
}
|
||||
|
||||
/* Print information about the gamepad */
|
||||
for (i = 0; i < SDL_GetNumJoysticks(); ++i) {
|
||||
const char *name;
|
||||
const char *path;
|
||||
const char *description;
|
||||
/* Print information about the gamepads */
|
||||
joysticks = SDL_GetJoysticks(&joystick_count);
|
||||
if (joysticks) {
|
||||
for (i = 0; joysticks[i]; ++i) {
|
||||
SDL_JoystickID instance_id = joysticks[i];
|
||||
const char *name;
|
||||
const char *path;
|
||||
const char *description;
|
||||
|
||||
SDL_GetJoystickGUIDString(SDL_GetJoystickDeviceGUID(i),
|
||||
guid, sizeof(guid));
|
||||
SDL_GetJoystickGUIDString(SDL_GetJoystickInstanceGUID(instance_id),
|
||||
guid, sizeof(guid));
|
||||
|
||||
if (SDL_IsGamepad(i)) {
|
||||
gamepad_count++;
|
||||
name = SDL_GetGamepadNameForIndex(i);
|
||||
path = SDL_GetGamepadPathForIndex(i);
|
||||
switch (SDL_GetGamepadTypeForIndex(i)) {
|
||||
case SDL_GAMEPAD_TYPE_AMAZON_LUNA:
|
||||
description = "Amazon Luna Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_GOOGLE_STADIA:
|
||||
description = "Google Stadia Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT:
|
||||
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT:
|
||||
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR:
|
||||
description = "Nintendo Switch Joy-Con";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
|
||||
description = "Nintendo Switch Pro Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_PS3:
|
||||
description = "PS3 Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_PS4:
|
||||
description = "PS4 Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_PS5:
|
||||
description = "PS5 Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_XBOX360:
|
||||
description = "XBox 360 Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_XBOXONE:
|
||||
description = "XBox One Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_VIRTUAL:
|
||||
description = "Virtual Gamepad";
|
||||
break;
|
||||
default:
|
||||
description = "Gamepad";
|
||||
break;
|
||||
if (SDL_IsGamepad(instance_id)) {
|
||||
gamepad_count++;
|
||||
name = SDL_GetGamepadInstanceName(instance_id);
|
||||
path = SDL_GetGamepadInstancePath(instance_id);
|
||||
switch (SDL_GetGamepadInstanceType(instance_id)) {
|
||||
case SDL_GAMEPAD_TYPE_AMAZON_LUNA:
|
||||
description = "Amazon Luna Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_GOOGLE_STADIA:
|
||||
description = "Google Stadia Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT:
|
||||
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT:
|
||||
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR:
|
||||
description = "Nintendo Switch Joy-Con";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
|
||||
description = "Nintendo Switch Pro Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_PS3:
|
||||
description = "PS3 Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_PS4:
|
||||
description = "PS4 Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_PS5:
|
||||
description = "PS5 Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_XBOX360:
|
||||
description = "XBox 360 Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_XBOXONE:
|
||||
description = "XBox One Controller";
|
||||
break;
|
||||
case SDL_GAMEPAD_TYPE_VIRTUAL:
|
||||
description = "Virtual Gamepad";
|
||||
break;
|
||||
default:
|
||||
description = "Gamepad";
|
||||
break;
|
||||
}
|
||||
AddGamepad(instance_id, SDL_FALSE);
|
||||
} else {
|
||||
name = SDL_GetJoystickInstanceName(instance_id);
|
||||
path = SDL_GetJoystickInstancePath(instance_id);
|
||||
description = "Joystick";
|
||||
}
|
||||
AddGamepad(i, SDL_FALSE);
|
||||
} else {
|
||||
name = SDL_GetJoystickNameForIndex(i);
|
||||
path = SDL_GetJoystickPathForIndex(i);
|
||||
description = "Joystick";
|
||||
SDL_Log("%s %d: %s%s%s (guid %s, VID 0x%.4x, PID 0x%.4x, player index = %d)\n",
|
||||
description, i, name ? name : "Unknown", path ? ", " : "", path ? path : "", guid,
|
||||
SDL_GetJoystickInstanceVendor(instance_id),
|
||||
SDL_GetJoystickInstanceProduct(instance_id),
|
||||
SDL_GetJoystickInstancePlayerIndex(instance_id));
|
||||
}
|
||||
SDL_Log("%s %d: %s%s%s (guid %s, VID 0x%.4x, PID 0x%.4x, player index = %d)\n",
|
||||
description, i, name ? name : "Unknown", path ? ", " : "", path ? path : "", guid,
|
||||
SDL_GetJoystickDeviceVendor(i), SDL_GetJoystickDeviceProduct(i), SDL_GetJoystickDevicePlayerIndex(i));
|
||||
SDL_free(joysticks);
|
||||
}
|
||||
SDL_Log("There are %d gamepad(s) attached (%d joystick(s))\n", gamepad_count, SDL_GetNumJoysticks());
|
||||
SDL_Log("There are %d gamepad(s) attached (%d joystick(s))\n", gamepad_count, joystick_count);
|
||||
|
||||
/* Create a window to display gamepad state */
|
||||
window = SDL_CreateWindow("Gamepad Test", SDL_WINDOWPOS_CENTERED,
|
||||
|
||||
@@ -19,9 +19,10 @@
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int num_joysticks = 0;
|
||||
SDL_Joystick *joystick = NULL;
|
||||
SDL_Haptic *haptic = NULL;
|
||||
SDL_JoystickID instance = -1;
|
||||
SDL_JoystickID instance = 0;
|
||||
SDL_bool keepGoing = SDL_TRUE;
|
||||
int i;
|
||||
SDL_bool enable_haptic = SDL_TRUE;
|
||||
@@ -52,7 +53,8 @@ int main(int argc, char *argv[])
|
||||
//SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0);
|
||||
*/
|
||||
|
||||
SDL_Log("There are %d joysticks at startup\n", SDL_GetNumJoysticks());
|
||||
SDL_free(SDL_GetJoysticks(&num_joysticks));
|
||||
SDL_Log("There are %d joysticks at startup\n", num_joysticks);
|
||||
if (enable_haptic) {
|
||||
SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics());
|
||||
}
|
||||
@@ -69,8 +71,8 @@ int main(int argc, char *argv[])
|
||||
SDL_Log("Only one joystick supported by this test\n");
|
||||
} else {
|
||||
joystick = SDL_OpenJoystick(event.jdevice.which);
|
||||
instance = SDL_GetJoystickInstanceID(joystick);
|
||||
SDL_Log("Joy Added : %" SDL_PRIs32 " : %s\n", event.jdevice.which, SDL_GetJoystickName(joystick));
|
||||
instance = event.jdevice.which;
|
||||
SDL_Log("Joy Added : %" SDL_PRIu32 " : %s\n", event.jdevice.which, SDL_GetJoystickName(joystick));
|
||||
if (enable_haptic) {
|
||||
if (SDL_JoystickIsHaptic(joystick)) {
|
||||
haptic = SDL_HapticOpenFromJoystick(joystick);
|
||||
@@ -93,7 +95,7 @@ int main(int argc, char *argv[])
|
||||
case SDL_JOYDEVICEREMOVED:
|
||||
if (instance == event.jdevice.which) {
|
||||
SDL_Log("Joy Removed: %" SDL_PRIs32 "\n", event.jdevice.which);
|
||||
instance = -1;
|
||||
instance = 0;
|
||||
if (enable_haptic && haptic) {
|
||||
SDL_HapticClose(haptic);
|
||||
haptic = NULL;
|
||||
|
||||
@@ -83,7 +83,7 @@ PrintJoystick(SDL_Joystick *joy)
|
||||
SDL_Log(" axes: %d\n", SDL_GetNumJoystickAxes(joy));
|
||||
SDL_Log(" hats: %d\n", SDL_GetNumJoystickHats(joy));
|
||||
SDL_Log(" buttons: %d\n", SDL_GetNumJoystickButtons(joy));
|
||||
SDL_Log(" instance id: %" SDL_PRIs32 "\n", SDL_GetJoystickInstanceID(joy));
|
||||
SDL_Log(" instance id: %" SDL_PRIu32 "\n", SDL_GetJoystickInstanceID(joy));
|
||||
SDL_Log(" guid: %s\n", guid);
|
||||
SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_GetJoystickVendor(joy), SDL_GetJoystickProduct(joy));
|
||||
}
|
||||
@@ -112,7 +112,7 @@ void loop(void *arg)
|
||||
switch (event.type) {
|
||||
|
||||
case SDL_JOYDEVICEADDED:
|
||||
SDL_Log("Joystick device %d added.\n", (int)event.jdevice.which);
|
||||
SDL_Log("Joystick device %" SDL_PRIu32 " added.\n", event.jdevice.which);
|
||||
if (joystick == NULL) {
|
||||
joystick = SDL_OpenJoystick(event.jdevice.which);
|
||||
if (joystick) {
|
||||
@@ -124,20 +124,35 @@ void loop(void *arg)
|
||||
break;
|
||||
|
||||
case SDL_JOYDEVICEREMOVED:
|
||||
SDL_Log("Joystick device %d removed.\n", (int)event.jdevice.which);
|
||||
SDL_Log("Joystick device %" SDL_PRIu32 " removed.\n", event.jdevice.which);
|
||||
if (event.jdevice.which == SDL_GetJoystickInstanceID(joystick)) {
|
||||
SDL_JoystickID *joysticks;
|
||||
|
||||
SDL_CloseJoystick(joystick);
|
||||
joystick = SDL_OpenJoystick(0);
|
||||
joystick = NULL;
|
||||
|
||||
joysticks = SDL_GetJoysticks(NULL);
|
||||
if (joysticks) {
|
||||
if (joysticks[0]) {
|
||||
joystick = SDL_OpenJoystick(joysticks[0]);
|
||||
if (joystick) {
|
||||
PrintJoystick(joystick);
|
||||
} else {
|
||||
SDL_Log("Couldn't open joystick: %s\n", SDL_GetError());
|
||||
}
|
||||
}
|
||||
SDL_free(joysticks);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_JOYAXISMOTION:
|
||||
SDL_Log("Joystick %" SDL_PRIs32 " axis %d value: %d\n",
|
||||
SDL_Log("Joystick %" SDL_PRIu32 " axis %d value: %d\n",
|
||||
event.jaxis.which,
|
||||
event.jaxis.axis, event.jaxis.value);
|
||||
break;
|
||||
case SDL_JOYHATMOTION:
|
||||
SDL_Log("Joystick %" SDL_PRIs32 " hat %d value:",
|
||||
SDL_Log("Joystick %" SDL_PRIu32 " hat %d value:",
|
||||
event.jhat.which, event.jhat.hat);
|
||||
if (event.jhat.value == SDL_HAT_CENTERED) {
|
||||
SDL_Log(" centered");
|
||||
@@ -157,7 +172,7 @@ void loop(void *arg)
|
||||
SDL_Log("\n");
|
||||
break;
|
||||
case SDL_JOYBUTTONDOWN:
|
||||
SDL_Log("Joystick %" SDL_PRIs32 " button %d down\n",
|
||||
SDL_Log("Joystick %" SDL_PRIu32 " button %d down\n",
|
||||
event.jbutton.which, event.jbutton.button);
|
||||
/* First button triggers a 0.5 second full strength rumble */
|
||||
if (event.jbutton.button == 0) {
|
||||
@@ -165,7 +180,7 @@ void loop(void *arg)
|
||||
}
|
||||
break;
|
||||
case SDL_JOYBUTTONUP:
|
||||
SDL_Log("Joystick %" SDL_PRIs32 " button %d up\n",
|
||||
SDL_Log("Joystick %" SDL_PRIu32 " button %d up\n",
|
||||
event.jbutton.which, event.jbutton.button);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
|
||||
@@ -57,8 +57,8 @@ static void HandleSensorEvent(SDL_SensorEvent *event)
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
int num_sensors, num_opened;
|
||||
SDL_SensorID *sensors;
|
||||
int i, num_sensors, num_opened;
|
||||
|
||||
/* Load the SDL library */
|
||||
if (SDL_Init(SDL_INIT_SENSOR) < 0) {
|
||||
@@ -66,25 +66,28 @@ int main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
num_sensors = SDL_GetNumSensors();
|
||||
sensors = SDL_GetSensors(&num_sensors);
|
||||
num_opened = 0;
|
||||
|
||||
SDL_Log("There are %d sensors available\n", num_sensors);
|
||||
for (i = 0; i < num_sensors; ++i) {
|
||||
SDL_Log("Sensor %" SDL_PRIs32 ": %s, type %s, platform type %d\n",
|
||||
SDL_GetSensorDeviceInstanceID(i),
|
||||
SDL_GetSensorDeviceName(i),
|
||||
GetSensorTypeString(SDL_GetSensorDeviceType(i)),
|
||||
SDL_GetSensorDeviceNonPortableType(i));
|
||||
if (sensors) {
|
||||
for (i = 0; i < num_sensors; ++i) {
|
||||
SDL_Log("Sensor %" SDL_PRIu32 ": %s, type %s, platform type %d\n",
|
||||
sensors[i],
|
||||
SDL_GetSensorInstanceName(sensors[i]),
|
||||
GetSensorTypeString(SDL_GetSensorInstanceType(sensors[i])),
|
||||
SDL_GetSensorInstanceNonPortableType(sensors[i]));
|
||||
|
||||
if (SDL_GetSensorDeviceType(i) != SDL_SENSOR_UNKNOWN) {
|
||||
SDL_Sensor *sensor = SDL_OpenSensor(i);
|
||||
if (sensor == NULL) {
|
||||
SDL_Log("Couldn't open sensor %" SDL_PRIs32 ": %s\n", SDL_GetSensorDeviceInstanceID(i), SDL_GetError());
|
||||
} else {
|
||||
++num_opened;
|
||||
if (SDL_GetSensorInstanceType(sensors[i]) != SDL_SENSOR_UNKNOWN) {
|
||||
SDL_Sensor *sensor = SDL_OpenSensor(sensors[i]);
|
||||
if (sensor == NULL) {
|
||||
SDL_Log("Couldn't open sensor %" SDL_PRIu32 ": %s\n", sensors[i], SDL_GetError());
|
||||
} else {
|
||||
++num_opened;
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_free(sensors);
|
||||
}
|
||||
SDL_Log("Opened %d sensors\n", num_opened);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user