mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-12-17 12:44:35 +08:00
Added a Windows Gaming Input joystick driver
This driver supports the Razer Atrox Arcade Stick Some of the quirks of this driver, inherent in Windows Gaming Input: * There will never appear to be controllers connected at startup. You must support hot-plugging in order to see these controllers. * You can't read the state of the guide button * You can't get controller events in the background
This commit is contained in:
@@ -418,11 +418,13 @@
|
||||
<ClCompile Include="..\..\src\events\SDL_windowevents.c" />
|
||||
<ClCompile Include="..\..\src\file\SDL_rwops.c" />
|
||||
<ClCompile Include="..\..\src\filesystem\windows\SDL_sysfilesystem.c" />
|
||||
<ClCompile Include="..\..\src\haptic\dummy\SDL_syshaptic.c" />
|
||||
<ClCompile Include="..\..\src\haptic\SDL_haptic.c" />
|
||||
<ClCompile Include="..\..\src\haptic\windows\SDL_dinputhaptic.c" />
|
||||
<ClCompile Include="..\..\src\haptic\windows\SDL_windowshaptic.c" />
|
||||
<ClCompile Include="..\..\src\haptic\windows\SDL_xinputhaptic.c" />
|
||||
<ClCompile Include="..\..\src\hidapi\windows\hid.c" />
|
||||
<ClCompile Include="..\..\src\joystick\dummy\SDL_sysjoystick.c" />
|
||||
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapijoystick.c" />
|
||||
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_gamecube.c" />
|
||||
<ClCompile Include="..\..\src\joystick\hidapi\SDL_hidapi_ps4.c" />
|
||||
@@ -438,6 +440,7 @@
|
||||
<ClCompile Include="..\..\src\joystick\windows\SDL_mmjoystick.c" />
|
||||
<ClCompile Include="..\..\src\joystick\windows\SDL_rawinputjoystick.c" />
|
||||
<ClCompile Include="..\..\src\joystick\windows\SDL_windowsjoystick.c" />
|
||||
<ClCompile Include="..\..\src\joystick\windows\SDL_windows_gaming_input.c" />
|
||||
<ClCompile Include="..\..\src\joystick\windows\SDL_xinputjoystick.c" />
|
||||
<ClCompile Include="..\..\src\libm\e_atan2.c" />
|
||||
<ClCompile Include="..\..\src\libm\e_exp.c" />
|
||||
|
||||
@@ -484,6 +484,9 @@
|
||||
<ClCompile Include="..\..\src\video\yuv2rgb\yuv_rgb.c" />
|
||||
<ClCompile Include="..\..\src\joystick\windows\SDL_rawinputjoystick.c" />
|
||||
<ClCompile Include="..\..\src\sensor\windows\SDL_windowssensor.c" />
|
||||
<ClCompile Include="..\..\src\haptic\dummy\SDL_syshaptic.c" />
|
||||
<ClCompile Include="..\..\src\joystick\dummy\SDL_sysjoystick.c" />
|
||||
<ClCompile Include="..\..\src\joystick\windows\SDL_windows_gaming_input.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="..\..\src\main\windows\version.rc" />
|
||||
|
||||
@@ -198,6 +198,7 @@ typedef unsigned int uintptr_t;
|
||||
#define SDL_JOYSTICK_HIDAPI 1
|
||||
#define SDL_JOYSTICK_RAWINPUT 1
|
||||
#define SDL_JOYSTICK_VIRTUAL 1
|
||||
/*#define SDL_JOYSTICK_WGI 1*/
|
||||
#define SDL_JOYSTICK_XINPUT 1
|
||||
#define SDL_HAPTIC_DINPUT 1
|
||||
#define SDL_HAPTIC_XINPUT 1
|
||||
@@ -260,3 +261,5 @@ typedef unsigned int uintptr_t;
|
||||
#endif
|
||||
|
||||
#endif /* SDL_config_windows_h_ */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
||||
@@ -1045,7 +1045,7 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForNameAndGUID(const
|
||||
}
|
||||
#endif /* __LINUX__ */
|
||||
|
||||
if (!mapping && name) {
|
||||
if (!mapping && name && !SDL_IsJoystickWGI(guid)) {
|
||||
if (SDL_strstr(name, "Xbox") || SDL_strstr(name, "X-Box") || SDL_strstr(name, "XBOX")) {
|
||||
mapping = s_pXInputMapping;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,9 @@ static const char *s_ControllerMappings [] =
|
||||
#if SDL_JOYSTICK_XINPUT
|
||||
"xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b8,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b9,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,",
|
||||
#endif
|
||||
#if SDL_JOYSTICK_WGI
|
||||
"0300000032150000000a000000007703,Razer Atrox Arcade Stick,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,dpup:b10,dpdown:b12,dpleft:b13,dpright:b11,lefttrigger:b8,righttrigger:b9,",
|
||||
#endif
|
||||
#if SDL_JOYSTICK_DINPUT
|
||||
"03000000fa2d00000100000000000000,3DRUDDER,leftx:a0,lefty:a1,rightx:a5,righty:a2,",
|
||||
"03000000c82d00000090000000000000,8BitDo FC30 Pro,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a3,righty:a4,start:b11,x:b4,y:b3,",
|
||||
|
||||
@@ -58,6 +58,9 @@ static SDL_JoystickDriver *SDL_joystick_drivers[] = {
|
||||
#ifdef SDL_JOYSTICK_HIDAPI /* Before WINDOWS_ driver, as WINDOWS wants to check if this driver is handling things */
|
||||
&SDL_HIDAPI_JoystickDriver,
|
||||
#endif
|
||||
#if defined(SDL_JOYSTICK_WGI)
|
||||
&SDL_WGI_JoystickDriver,
|
||||
#endif
|
||||
#if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT)
|
||||
&SDL_WINDOWS_JoystickDriver,
|
||||
#endif
|
||||
@@ -1731,6 +1734,12 @@ SDL_IsJoystickXInput(SDL_JoystickGUID guid)
|
||||
return (guid.data[14] == 'x') ? SDL_TRUE : SDL_FALSE;
|
||||
}
|
||||
|
||||
SDL_bool
|
||||
SDL_IsJoystickWGI(SDL_JoystickGUID guid)
|
||||
{
|
||||
return (guid.data[14] == 'w') ? SDL_TRUE : SDL_FALSE;
|
||||
}
|
||||
|
||||
SDL_bool
|
||||
SDL_IsJoystickHIDAPI(SDL_JoystickGUID guid)
|
||||
{
|
||||
@@ -1840,6 +1849,10 @@ static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)
|
||||
}
|
||||
}
|
||||
|
||||
if (SDL_IsJoystickWGI(guid)) {
|
||||
return (SDL_JoystickType)guid.data[15];
|
||||
}
|
||||
|
||||
if (SDL_IsJoystickVirtual(guid)) {
|
||||
return (SDL_JoystickType)guid.data[15];
|
||||
}
|
||||
|
||||
@@ -70,6 +70,9 @@ extern SDL_bool SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_i
|
||||
/* Function to return whether a joystick guid comes from the XInput driver */
|
||||
extern SDL_bool SDL_IsJoystickXInput(SDL_JoystickGUID guid);
|
||||
|
||||
/* Function to return whether a joystick guid comes from the WGI driver */
|
||||
extern SDL_bool SDL_IsJoystickWGI(SDL_JoystickGUID guid);
|
||||
|
||||
/* Function to return whether a joystick guid comes from the HIDAPI driver */
|
||||
extern SDL_bool SDL_IsJoystickHIDAPI(SDL_JoystickGUID guid);
|
||||
|
||||
|
||||
@@ -153,6 +153,7 @@ extern SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver;
|
||||
extern SDL_JoystickDriver SDL_IOS_JoystickDriver;
|
||||
extern SDL_JoystickDriver SDL_LINUX_JoystickDriver;
|
||||
extern SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver;
|
||||
extern SDL_JoystickDriver SDL_WGI_JoystickDriver;
|
||||
extern SDL_JoystickDriver SDL_WINDOWS_JoystickDriver;
|
||||
|
||||
#endif /* SDL_sysjoystick_h_ */
|
||||
|
||||
705
src/joystick/windows/SDL_windows_gaming_input.c
Normal file
705
src/joystick/windows/SDL_windows_gaming_input.c
Normal file
File diff suppressed because it is too large
Load Diff
@@ -41,10 +41,6 @@ DEFINE_GUID(IID_SensorManager, 0xBD77DB67, 0x45A8, 0x42DC, 0x8D, 0x00, 0x6D, 0xC
|
||||
DEFINE_GUID(IID_SensorManagerEvents, 0x9B3B0B86, 0x266A, 0x4AAD, 0xB2, 0x1F, 0xFD, 0xE5, 0x50, 0x10, 0x01, 0xB7);
|
||||
DEFINE_GUID(IID_SensorEvents, 0x5D8DCC91, 0x4641, 0x47E7, 0xB7, 0xC3, 0xB7, 0x4F, 0x48, 0xA6, 0xC3, 0x91);
|
||||
|
||||
DEFINE_PROPERTYKEY(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND, 0X3F8A69A2, 0X7C5, 0X4E48, 0XA9, 0X65, 0XCD, 0X79, 0X7A, 0XAB, 0X56, 0XD5, 10); //[VT_R8]
|
||||
DEFINE_PROPERTYKEY(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Y_DEGREES_PER_SECOND, 0X3F8A69A2, 0X7C5, 0X4E48, 0XA9, 0X65, 0XCD, 0X79, 0X7A, 0XAB, 0X56, 0XD5, 11); //[VT_R8]
|
||||
DEFINE_PROPERTYKEY(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, 0X3F8A69A2, 0X7C5, 0X4E48, 0XA9, 0X65, 0XCD, 0X79, 0X7A, 0XAB, 0X56, 0XD5, 12); //[VT_R8]
|
||||
|
||||
typedef struct
|
||||
{
|
||||
SDL_SensorID id;
|
||||
|
||||
@@ -155,6 +155,9 @@ static int s_iCurrentBinding;
|
||||
static Uint32 s_unPendingAdvanceTime;
|
||||
static SDL_bool s_bBindingComplete;
|
||||
|
||||
static SDL_Window *window;
|
||||
static SDL_bool done = SDL_FALSE;
|
||||
|
||||
SDL_Texture *
|
||||
LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
|
||||
{
|
||||
@@ -357,30 +360,18 @@ BMergeAxisBindings(int iIndex)
|
||||
static void
|
||||
WatchJoystick(SDL_Joystick * joystick)
|
||||
{
|
||||
SDL_Window *window = NULL;
|
||||
SDL_Renderer *screen = NULL;
|
||||
SDL_Texture *background, *button, *axis, *marker;
|
||||
const char *name = NULL;
|
||||
SDL_bool done = SDL_FALSE;
|
||||
SDL_Event event;
|
||||
SDL_Rect dst;
|
||||
Uint8 alpha=200, alpha_step = -1;
|
||||
Uint32 alpha_ticks = 0;
|
||||
SDL_JoystickID nJoystickID;
|
||||
|
||||
/* Create a window to display joystick axis position */
|
||||
window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT, 0);
|
||||
if (window == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
|
||||
screen = SDL_CreateRenderer(window, -1, 0);
|
||||
if (screen == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -705,7 +696,6 @@ WatchJoystick(SDL_Joystick * joystick)
|
||||
s_arrAxisState = NULL;
|
||||
|
||||
SDL_DestroyRenderer(screen);
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
|
||||
int
|
||||
@@ -724,6 +714,34 @@ main(int argc, char *argv[])
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Create a window to display joystick axis position */
|
||||
window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT, 0);
|
||||
if (window == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
|
||||
return 2;
|
||||
}
|
||||
|
||||
while (SDL_NumJoysticks() == 0) {
|
||||
SDL_Event event;
|
||||
|
||||
while (SDL_PollEvent(&event) > 0) {
|
||||
switch (event.type) {
|
||||
case SDL_KEYDOWN:
|
||||
if ((event.key.keysym.sym != SDLK_ESCAPE)) {
|
||||
break;
|
||||
}
|
||||
/* Fall through to signal quit */
|
||||
case SDL_QUIT:
|
||||
done = SDL_TRUE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Print information about the joysticks */
|
||||
SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
|
||||
for (i = 0; i < SDL_NumJoysticks(); ++i) {
|
||||
@@ -748,28 +766,16 @@ main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __ANDROID__
|
||||
if (SDL_NumJoysticks() > 0) {
|
||||
#else
|
||||
if (argv[1]) {
|
||||
#endif
|
||||
int device;
|
||||
#ifdef __ANDROID__
|
||||
device = 0;
|
||||
#else
|
||||
device = atoi(argv[1]);
|
||||
#endif
|
||||
joystick = SDL_JoystickOpen(device);
|
||||
if (joystick == NULL) {
|
||||
SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
|
||||
} else {
|
||||
WatchJoystick(joystick);
|
||||
SDL_JoystickClose(joystick);
|
||||
}
|
||||
}
|
||||
else {
|
||||
SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt");
|
||||
joystick = SDL_JoystickOpen(0);
|
||||
if (joystick == NULL) {
|
||||
SDL_Log("Couldn't open joystick 0: %s\n", SDL_GetError());
|
||||
} else {
|
||||
WatchJoystick(joystick);
|
||||
SDL_JoystickClose(joystick);
|
||||
}
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -61,10 +61,12 @@ static const struct { int x; int y; double angle; } axis_positions[] = {
|
||||
{375, -20, 0.0}, /* TRIGGERRIGHT */
|
||||
};
|
||||
|
||||
SDL_Window *window = NULL;
|
||||
SDL_Renderer *screen = NULL;
|
||||
SDL_bool retval = SDL_FALSE;
|
||||
SDL_bool done = SDL_FALSE;
|
||||
SDL_Texture *background, *button, *axis;
|
||||
SDL_GameController *gamecontroller;
|
||||
|
||||
static SDL_Texture *
|
||||
LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
|
||||
@@ -94,12 +96,29 @@ LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
|
||||
return texture;
|
||||
}
|
||||
|
||||
static void
|
||||
UpdateWindowTitle()
|
||||
{
|
||||
const char *name = SDL_GameControllerName(gamecontroller);
|
||||
const char *basetitle = "Game Controller Test: ";
|
||||
const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
|
||||
char *title = (char *)SDL_malloc(titlelen);
|
||||
|
||||
retval = SDL_FALSE;
|
||||
done = SDL_FALSE;
|
||||
|
||||
if (title) {
|
||||
SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
|
||||
SDL_SetWindowTitle(window, title);
|
||||
SDL_free(title);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
loop(void *arg)
|
||||
{
|
||||
SDL_Event event;
|
||||
int i;
|
||||
SDL_GameController *gamecontroller = (SDL_GameController *)arg;
|
||||
|
||||
/* blank screen, set up for drawing this frame. */
|
||||
SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
|
||||
@@ -108,6 +127,29 @@ loop(void *arg)
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_CONTROLLERDEVICEADDED:
|
||||
SDL_Log("Game controller device %d added.\n", (int) event.cdevice.which);
|
||||
if (!gamecontroller) {
|
||||
gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
|
||||
if (gamecontroller) {
|
||||
UpdateWindowTitle();
|
||||
} else {
|
||||
SDL_Log("Couldn't open controller: %s\n", SDL_GetError());
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_CONTROLLERDEVICEREMOVED:
|
||||
SDL_Log("Game controller device %d removed.\n", (int) event.cdevice.which);
|
||||
if (gamecontroller && event.cdevice.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) {
|
||||
SDL_GameControllerClose(gamecontroller);
|
||||
gamecontroller = SDL_GameControllerOpen(0);
|
||||
if (gamecontroller) {
|
||||
UpdateWindowTitle();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_CONTROLLERAXISMOTION:
|
||||
SDL_Log("Controller axis %s changed to %d\n", SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value);
|
||||
break;
|
||||
@@ -128,42 +170,39 @@ loop(void *arg)
|
||||
}
|
||||
}
|
||||
|
||||
/* Update visual controller state */
|
||||
for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
|
||||
if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
|
||||
const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
|
||||
SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, SDL_FLIP_NONE);
|
||||
if (gamecontroller) {
|
||||
/* Update visual controller state */
|
||||
for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
|
||||
if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
|
||||
const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
|
||||
SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, SDL_FLIP_NONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
|
||||
const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
|
||||
const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
|
||||
if (value < -deadzone) {
|
||||
const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
|
||||
const double angle = axis_positions[i].angle;
|
||||
SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
|
||||
} else if (value > deadzone) {
|
||||
const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
|
||||
const double angle = axis_positions[i].angle + 180.0;
|
||||
SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
|
||||
for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
|
||||
const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
|
||||
const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
|
||||
if (value < -deadzone) {
|
||||
const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
|
||||
const double angle = axis_positions[i].angle;
|
||||
SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
|
||||
} else if (value > deadzone) {
|
||||
const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
|
||||
const double angle = axis_positions[i].angle + 180.0;
|
||||
SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Update rumble based on trigger state */
|
||||
{
|
||||
Uint16 low_frequency_rumble = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_TRIGGERLEFT) * 2;
|
||||
Uint16 high_frequency_rumble = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) * 2;
|
||||
SDL_GameControllerRumble(gamecontroller, low_frequency_rumble, high_frequency_rumble, 250);
|
||||
/* Update rumble based on trigger state */
|
||||
{
|
||||
Uint16 low_frequency_rumble = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_TRIGGERLEFT) * 2;
|
||||
Uint16 high_frequency_rumble = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) * 2;
|
||||
SDL_GameControllerRumble(gamecontroller, low_frequency_rumble, high_frequency_rumble, 250);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_RenderPresent(screen);
|
||||
|
||||
if (!SDL_GameControllerGetAttached(gamecontroller)) {
|
||||
done = SDL_TRUE;
|
||||
retval = SDL_TRUE; /* keep going, wait for reattach. */
|
||||
}
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
if (done) {
|
||||
emscripten_cancel_main_loop();
|
||||
@@ -171,92 +210,12 @@ loop(void *arg)
|
||||
#endif
|
||||
}
|
||||
|
||||
SDL_bool
|
||||
WatchGameController(SDL_GameController * gamecontroller)
|
||||
{
|
||||
const char *name = SDL_GameControllerName(gamecontroller);
|
||||
const char *basetitle = "Game Controller Test: ";
|
||||
const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
|
||||
char *title = (char *)SDL_malloc(titlelen);
|
||||
SDL_Window *window = NULL;
|
||||
|
||||
retval = SDL_FALSE;
|
||||
done = SDL_FALSE;
|
||||
|
||||
if (title) {
|
||||
SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
|
||||
}
|
||||
|
||||
/* Create a window to display controller state */
|
||||
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT, 0);
|
||||
SDL_free(title);
|
||||
title = NULL;
|
||||
if (window == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
screen = SDL_CreateRenderer(window, -1, 0);
|
||||
if (screen == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderClear(screen);
|
||||
SDL_RenderPresent(screen);
|
||||
SDL_RaiseWindow(window);
|
||||
|
||||
/* scale for platforms that don't give you the window size you asked for. */
|
||||
SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
|
||||
background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
|
||||
button = LoadTexture(screen, "button.bmp", SDL_TRUE);
|
||||
axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
|
||||
|
||||
if (!background || !button || !axis) {
|
||||
SDL_DestroyRenderer(screen);
|
||||
SDL_DestroyWindow(window);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
SDL_SetTextureColorMod(button, 10, 255, 21);
|
||||
SDL_SetTextureColorMod(axis, 10, 255, 21);
|
||||
|
||||
/* !!! FIXME: */
|
||||
/*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
|
||||
|
||||
/* Print info about the controller we are watching */
|
||||
SDL_Log("Watching controller %s\n", name ? name : "Unknown Controller");
|
||||
|
||||
/* Loop, getting controller events! */
|
||||
#ifdef __EMSCRIPTEN__
|
||||
emscripten_set_main_loop_arg(loop, gamecontroller, 0, 1);
|
||||
#else
|
||||
while (!done) {
|
||||
loop(gamecontroller);
|
||||
}
|
||||
#endif
|
||||
|
||||
SDL_DestroyRenderer(screen);
|
||||
screen = NULL;
|
||||
background = NULL;
|
||||
button = NULL;
|
||||
axis = NULL;
|
||||
SDL_DestroyWindow(window);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int nController = 0;
|
||||
int retcode = 0;
|
||||
char guid[64];
|
||||
SDL_GameController *gamecontroller;
|
||||
|
||||
/* Enable standard application logging */
|
||||
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
||||
@@ -270,7 +229,7 @@ main(int argc, char *argv[])
|
||||
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
|
||||
|
||||
/* Print information about the mappings */
|
||||
if (!argv[1]) {
|
||||
if (argv[1] && SDL_strcmp(argv[1], "--mappings") == 0) {
|
||||
SDL_Log("Supported mappings:\n");
|
||||
for (i = 0; i < SDL_GameControllerNumMappings(); ++i) {
|
||||
char *mapping = SDL_GameControllerMappingForIndex(i);
|
||||
@@ -290,8 +249,7 @@ main(int argc, char *argv[])
|
||||
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
|
||||
guid, sizeof (guid));
|
||||
|
||||
if ( SDL_IsGameController(i) )
|
||||
{
|
||||
if ( SDL_IsGameController(i) ) {
|
||||
nController++;
|
||||
name = SDL_GameControllerNameForIndex(i);
|
||||
switch (SDL_GameControllerTypeForIndex(i)) {
|
||||
@@ -327,62 +285,62 @@ main(int argc, char *argv[])
|
||||
}
|
||||
SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
|
||||
|
||||
if (argv[1]) {
|
||||
SDL_bool reportederror = SDL_FALSE;
|
||||
SDL_bool keepGoing = SDL_TRUE;
|
||||
SDL_Event event;
|
||||
int device = atoi(argv[1]);
|
||||
if (device >= SDL_NumJoysticks()) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%i is an invalid joystick index.\n", device);
|
||||
retcode = 1;
|
||||
} else {
|
||||
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(device),
|
||||
guid, sizeof (guid));
|
||||
SDL_Log("Attempting to open device %i, guid %s\n", device, guid);
|
||||
gamecontroller = SDL_GameControllerOpen(device);
|
||||
|
||||
if (gamecontroller != NULL) {
|
||||
SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
|
||||
}
|
||||
|
||||
while (keepGoing) {
|
||||
if (gamecontroller == NULL) {
|
||||
if (!reportederror) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open gamecontroller %d: %s\n", device, SDL_GetError());
|
||||
retcode = 1;
|
||||
keepGoing = SDL_FALSE;
|
||||
reportederror = SDL_TRUE;
|
||||
}
|
||||
} else {
|
||||
reportederror = SDL_FALSE;
|
||||
keepGoing = WatchGameController(gamecontroller);
|
||||
SDL_GameControllerClose(gamecontroller);
|
||||
}
|
||||
|
||||
gamecontroller = NULL;
|
||||
if (keepGoing) {
|
||||
SDL_Log("Waiting for attach\n");
|
||||
}
|
||||
while (keepGoing) {
|
||||
SDL_WaitEvent(&event);
|
||||
if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
|
||||
|| (event.type == SDL_MOUSEBUTTONDOWN)) {
|
||||
keepGoing = SDL_FALSE;
|
||||
} else if (event.type == SDL_CONTROLLERDEVICEADDED) {
|
||||
gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
|
||||
if (gamecontroller != NULL) {
|
||||
SDL_assert(SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) == gamecontroller);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Create a window to display controller state */
|
||||
window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT, 0);
|
||||
if (window == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
|
||||
return 2;
|
||||
}
|
||||
|
||||
screen = SDL_CreateRenderer(window, -1, 0);
|
||||
if (screen == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
return 2;
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderClear(screen);
|
||||
SDL_RenderPresent(screen);
|
||||
|
||||
/* scale for platforms that don't give you the window size you asked for. */
|
||||
SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
|
||||
background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
|
||||
button = LoadTexture(screen, "button.bmp", SDL_TRUE);
|
||||
axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
|
||||
|
||||
if (!background || !button || !axis) {
|
||||
SDL_DestroyRenderer(screen);
|
||||
SDL_DestroyWindow(window);
|
||||
return 2;
|
||||
}
|
||||
SDL_SetTextureColorMod(button, 10, 255, 21);
|
||||
SDL_SetTextureColorMod(axis, 10, 255, 21);
|
||||
|
||||
/* !!! FIXME: */
|
||||
/*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
|
||||
|
||||
/* Loop, getting controller events! */
|
||||
#ifdef __EMSCRIPTEN__
|
||||
emscripten_set_main_loop_arg(loop, NULL, 0, 1);
|
||||
#else
|
||||
while (!done) {
|
||||
loop(NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
SDL_DestroyRenderer(screen);
|
||||
screen = NULL;
|
||||
background = NULL;
|
||||
button = NULL;
|
||||
axis = NULL;
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
|
||||
|
||||
return retcode;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
@@ -32,9 +32,62 @@
|
||||
#define SCREEN_HEIGHT 480
|
||||
#endif
|
||||
|
||||
SDL_Renderer *screen = NULL;
|
||||
SDL_bool retval = SDL_FALSE;
|
||||
SDL_bool done = SDL_FALSE;
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *screen = NULL;
|
||||
static SDL_Joystick *joystick = NULL;
|
||||
static SDL_bool done = SDL_FALSE;
|
||||
|
||||
static void
|
||||
PrintJoystick(SDL_Joystick *joystick)
|
||||
{
|
||||
const char *type;
|
||||
char guid[64];
|
||||
|
||||
SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
|
||||
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), guid, sizeof (guid));
|
||||
switch (SDL_JoystickGetType(joystick)) {
|
||||
case SDL_JOYSTICK_TYPE_GAMECONTROLLER:
|
||||
type = "Game Controller";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_WHEEL:
|
||||
type = "Wheel";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_ARCADE_STICK:
|
||||
type = "Arcade Stick";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_FLIGHT_STICK:
|
||||
type = "Flight Stick";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_DANCE_PAD:
|
||||
type = "Dance Pad";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_GUITAR:
|
||||
type = "Guitar";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_DRUM_KIT:
|
||||
type = "Drum Kit";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_ARCADE_PAD:
|
||||
type = "Arcade Pad";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_THROTTLE:
|
||||
type = "Throttle";
|
||||
break;
|
||||
default:
|
||||
type = "Unknown";
|
||||
break;
|
||||
}
|
||||
SDL_Log("Joystick\n");
|
||||
SDL_Log(" name: %s\n", SDL_JoystickName(joystick));
|
||||
SDL_Log(" type: %s\n", type);
|
||||
SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
|
||||
SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
|
||||
SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
|
||||
SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
|
||||
SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
|
||||
SDL_Log(" guid: %s\n", guid);
|
||||
SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
|
||||
}
|
||||
|
||||
static void
|
||||
DrawRect(SDL_Renderer *r, const int x, const int y, const int w, const int h)
|
||||
@@ -48,72 +101,89 @@ loop(void *arg)
|
||||
{
|
||||
SDL_Event event;
|
||||
int i;
|
||||
SDL_Joystick *joystick = (SDL_Joystick *)arg;
|
||||
|
||||
/* blank screen, set up for drawing this frame. */
|
||||
/* blank screen, set up for drawing this frame. */
|
||||
SDL_SetRenderDrawColor(screen, 0x0, 0x0, 0x0, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderClear(screen);
|
||||
SDL_RenderClear(screen);
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
|
||||
case SDL_JOYDEVICEREMOVED:
|
||||
SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which);
|
||||
SDL_Log("Our instance ID is %d\n", (int) SDL_JoystickInstanceID(joystick));
|
||||
break;
|
||||
|
||||
case SDL_JOYAXISMOTION:
|
||||
SDL_Log("Joystick %d axis %d value: %d\n",
|
||||
event.jaxis.which,
|
||||
event.jaxis.axis, event.jaxis.value);
|
||||
break;
|
||||
case SDL_JOYHATMOTION:
|
||||
SDL_Log("Joystick %d hat %d value:",
|
||||
event.jhat.which, event.jhat.hat);
|
||||
if (event.jhat.value == SDL_HAT_CENTERED)
|
||||
SDL_Log(" centered");
|
||||
if (event.jhat.value & SDL_HAT_UP)
|
||||
SDL_Log(" up");
|
||||
if (event.jhat.value & SDL_HAT_RIGHT)
|
||||
SDL_Log(" right");
|
||||
if (event.jhat.value & SDL_HAT_DOWN)
|
||||
SDL_Log(" down");
|
||||
if (event.jhat.value & SDL_HAT_LEFT)
|
||||
SDL_Log(" left");
|
||||
SDL_Log("\n");
|
||||
break;
|
||||
case SDL_JOYBALLMOTION:
|
||||
SDL_Log("Joystick %d ball %d delta: (%d,%d)\n",
|
||||
event.jball.which,
|
||||
event.jball.ball, event.jball.xrel, event.jball.yrel);
|
||||
break;
|
||||
case SDL_JOYBUTTONDOWN:
|
||||
SDL_Log("Joystick %d 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) {
|
||||
SDL_JoystickRumble(joystick, 0xFFFF, 0xFFFF, 500);
|
||||
case SDL_JOYDEVICEADDED:
|
||||
SDL_Log("Joystick device %d added.\n", (int) event.jdevice.which);
|
||||
if (!joystick) {
|
||||
joystick = SDL_JoystickOpen(event.jdevice.which);
|
||||
if (joystick) {
|
||||
PrintJoystick(joystick);
|
||||
} else {
|
||||
SDL_Log("Couldn't open joystick: %s\n", SDL_GetError());
|
||||
}
|
||||
break;
|
||||
case SDL_JOYBUTTONUP:
|
||||
SDL_Log("Joystick %d button %d up\n",
|
||||
event.jbutton.which, event.jbutton.button);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if ((event.key.keysym.sym != SDLK_ESCAPE) &&
|
||||
(event.key.keysym.sym != SDLK_AC_BACK)) {
|
||||
break;
|
||||
}
|
||||
/* Fall through to signal quit */
|
||||
case SDL_FINGERDOWN:
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_QUIT:
|
||||
done = SDL_TRUE;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_JOYDEVICEREMOVED:
|
||||
SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which);
|
||||
if (event.jdevice.which == SDL_JoystickInstanceID(joystick)) {
|
||||
SDL_JoystickClose(joystick);
|
||||
joystick = SDL_JoystickOpen(0);
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_JOYAXISMOTION:
|
||||
SDL_Log("Joystick %d axis %d value: %d\n",
|
||||
event.jaxis.which,
|
||||
event.jaxis.axis, event.jaxis.value);
|
||||
break;
|
||||
case SDL_JOYHATMOTION:
|
||||
SDL_Log("Joystick %d hat %d value:",
|
||||
event.jhat.which, event.jhat.hat);
|
||||
if (event.jhat.value == SDL_HAT_CENTERED)
|
||||
SDL_Log(" centered");
|
||||
if (event.jhat.value & SDL_HAT_UP)
|
||||
SDL_Log(" up");
|
||||
if (event.jhat.value & SDL_HAT_RIGHT)
|
||||
SDL_Log(" right");
|
||||
if (event.jhat.value & SDL_HAT_DOWN)
|
||||
SDL_Log(" down");
|
||||
if (event.jhat.value & SDL_HAT_LEFT)
|
||||
SDL_Log(" left");
|
||||
SDL_Log("\n");
|
||||
break;
|
||||
case SDL_JOYBALLMOTION:
|
||||
SDL_Log("Joystick %d ball %d delta: (%d,%d)\n",
|
||||
event.jball.which,
|
||||
event.jball.ball, event.jball.xrel, event.jball.yrel);
|
||||
break;
|
||||
case SDL_JOYBUTTONDOWN:
|
||||
SDL_Log("Joystick %d 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) {
|
||||
SDL_JoystickRumble(joystick, 0xFFFF, 0xFFFF, 500);
|
||||
}
|
||||
break;
|
||||
case SDL_JOYBUTTONUP:
|
||||
SDL_Log("Joystick %d button %d up\n",
|
||||
event.jbutton.which, event.jbutton.button);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if ((event.key.keysym.sym != SDLK_ESCAPE) &&
|
||||
(event.key.keysym.sym != SDLK_AC_BACK)) {
|
||||
break;
|
||||
}
|
||||
/* Fall through to signal quit */
|
||||
case SDL_FINGERDOWN:
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_QUIT:
|
||||
done = SDL_TRUE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (joystick) {
|
||||
|
||||
/* Update visual joystick state */
|
||||
SDL_SetRenderDrawColor(screen, 0x00, 0xFF, 0x00, SDL_ALPHA_OPAQUE);
|
||||
for (i = 0; i < SDL_JoystickNumButtons(joystick); ++i) {
|
||||
@@ -172,13 +242,9 @@ loop(void *arg)
|
||||
|
||||
DrawRect(screen, x, y, 8, 8);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_RenderPresent(screen);
|
||||
|
||||
if (SDL_JoystickGetAttached( joystick ) == 0) {
|
||||
done = SDL_TRUE;
|
||||
retval = SDL_TRUE; /* keep going, wait for reattach. */
|
||||
}
|
||||
SDL_RenderPresent(screen);
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
if (done) {
|
||||
@@ -187,14 +253,19 @@ loop(void *arg)
|
||||
#endif
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
WatchJoystick(SDL_Joystick * joystick)
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Window *window = NULL;
|
||||
const char *name = NULL;
|
||||
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
|
||||
|
||||
retval = SDL_FALSE;
|
||||
done = SDL_FALSE;
|
||||
/* Enable standard application logging */
|
||||
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
||||
|
||||
/* Initialize SDL (Note: video is required to start event loop) */
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Create a window to display joystick axis position */
|
||||
window = SDL_CreateWindow("Joystick Test", SDL_WINDOWPOS_CENTERED,
|
||||
@@ -215,159 +286,19 @@ WatchJoystick(SDL_Joystick * joystick)
|
||||
SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
|
||||
SDL_RenderClear(screen);
|
||||
SDL_RenderPresent(screen);
|
||||
SDL_RaiseWindow(window);
|
||||
|
||||
/* Print info about the joystick we are watching */
|
||||
name = SDL_JoystickName(joystick);
|
||||
SDL_Log("Watching joystick %d: (%s)\n", SDL_JoystickInstanceID(joystick),
|
||||
name ? name : "Unknown Joystick");
|
||||
SDL_Log("Joystick has %d axes, %d hats, %d balls, and %d buttons\n",
|
||||
SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
|
||||
SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
|
||||
|
||||
/* Loop, getting joystick events! */
|
||||
#ifdef __EMSCRIPTEN__
|
||||
emscripten_set_main_loop_arg(loop, joystick, 0, 1);
|
||||
emscripten_set_main_loop_arg(loop, NULL, 0, 1);
|
||||
#else
|
||||
while (!done) {
|
||||
loop(joystick);
|
||||
loop(NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
SDL_DestroyRenderer(screen);
|
||||
screen = NULL;
|
||||
SDL_DestroyWindow(window);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
const char *name, *type;
|
||||
int i;
|
||||
SDL_Joystick *joystick;
|
||||
|
||||
SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0");
|
||||
|
||||
/* Enable standard application logging */
|
||||
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
||||
|
||||
/* Initialize SDL (Note: video is required to start event loop) */
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Print information about the joysticks */
|
||||
SDL_Log("There are %d joysticks attached\n", SDL_NumJoysticks());
|
||||
for (i = 0; i < SDL_NumJoysticks(); ++i) {
|
||||
name = SDL_JoystickNameForIndex(i);
|
||||
SDL_Log("Joystick %d: %s\n", i, name ? name : "Unknown Joystick");
|
||||
joystick = SDL_JoystickOpen(i);
|
||||
if (joystick == NULL) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_JoystickOpen(%d) failed: %s\n", i,
|
||||
SDL_GetError());
|
||||
} else {
|
||||
char guid[64];
|
||||
SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
|
||||
SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick),
|
||||
guid, sizeof (guid));
|
||||
switch (SDL_JoystickGetType(joystick)) {
|
||||
case SDL_JOYSTICK_TYPE_GAMECONTROLLER:
|
||||
type = "Game Controller";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_WHEEL:
|
||||
type = "Wheel";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_ARCADE_STICK:
|
||||
type = "Arcade Stick";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_FLIGHT_STICK:
|
||||
type = "Flight Stick";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_DANCE_PAD:
|
||||
type = "Dance Pad";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_GUITAR:
|
||||
type = "Guitar";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_DRUM_KIT:
|
||||
type = "Drum Kit";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_ARCADE_PAD:
|
||||
type = "Arcade Pad";
|
||||
break;
|
||||
case SDL_JOYSTICK_TYPE_THROTTLE:
|
||||
type = "Throttle";
|
||||
break;
|
||||
default:
|
||||
type = "Unknown";
|
||||
break;
|
||||
}
|
||||
SDL_Log(" type: %s\n", type);
|
||||
SDL_Log(" axes: %d\n", SDL_JoystickNumAxes(joystick));
|
||||
SDL_Log(" balls: %d\n", SDL_JoystickNumBalls(joystick));
|
||||
SDL_Log(" hats: %d\n", SDL_JoystickNumHats(joystick));
|
||||
SDL_Log(" buttons: %d\n", SDL_JoystickNumButtons(joystick));
|
||||
SDL_Log("instance id: %d\n", SDL_JoystickInstanceID(joystick));
|
||||
SDL_Log(" guid: %s\n", guid);
|
||||
SDL_Log(" VID/PID: 0x%.4x/0x%.4x\n", SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick));
|
||||
SDL_JoystickClose(joystick);
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__ANDROID__) || defined(__IPHONEOS__)
|
||||
if (SDL_NumJoysticks() > 0) {
|
||||
#else
|
||||
if (argv[1]) {
|
||||
#endif
|
||||
SDL_bool reportederror = SDL_FALSE;
|
||||
SDL_bool keepGoing = SDL_TRUE;
|
||||
SDL_Event event;
|
||||
int device;
|
||||
#if defined(__ANDROID__) || defined(__IPHONEOS__)
|
||||
device = 0;
|
||||
#else
|
||||
device = atoi(argv[1]);
|
||||
#endif
|
||||
joystick = SDL_JoystickOpen(device);
|
||||
if (joystick != NULL) {
|
||||
SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
|
||||
}
|
||||
|
||||
while ( keepGoing ) {
|
||||
if (joystick == NULL) {
|
||||
if ( !reportederror ) {
|
||||
SDL_Log("Couldn't open joystick %d: %s\n", device, SDL_GetError());
|
||||
keepGoing = SDL_FALSE;
|
||||
reportederror = SDL_TRUE;
|
||||
}
|
||||
} else {
|
||||
reportederror = SDL_FALSE;
|
||||
keepGoing = WatchJoystick(joystick);
|
||||
SDL_JoystickClose(joystick);
|
||||
}
|
||||
|
||||
joystick = NULL;
|
||||
if (keepGoing) {
|
||||
SDL_Log("Waiting for attach\n");
|
||||
}
|
||||
while (keepGoing) {
|
||||
SDL_WaitEvent(&event);
|
||||
if ((event.type == SDL_QUIT) || (event.type == SDL_FINGERDOWN)
|
||||
|| (event.type == SDL_MOUSEBUTTONDOWN)) {
|
||||
keepGoing = SDL_FALSE;
|
||||
} else if (event.type == SDL_JOYDEVICEADDED) {
|
||||
device = event.jdevice.which;
|
||||
joystick = SDL_JoystickOpen(device);
|
||||
if (joystick != NULL) {
|
||||
SDL_assert(SDL_JoystickFromInstanceID(SDL_JoystickInstanceID(joystick)) == joystick);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user