mirror of
https://github.com/encounter/SDL.git
synced 2025-12-08 13:15:10 +00: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:
@@ -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