/* * Return true if this joystick is known to have all axes centered at zero * This isn't generally needed unless the joystick never generates an initial axis value near zero, * e.g. it's emulating axes with digital buttons */ static SDL_bool SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick) { static Uint32 zero_centered_joysticks[] = { MAKE_VIDPID(0x0e8f, 0x3013), /* HuiJia SNES USB adapter */ MAKE_VIDPID(0x05a0, 0x3232), /* 8Bitdo Zero Gamepad */ }; int i; Uint32 id = MAKE_VIDPID(SDL_JoystickGetVendor(joystick), SDL_JoystickGetProduct(joystick)); /*printf("JOYSTICK '%s' VID/PID 0x%.4x/0x%.4x AXES: %d\n", joystick->name, vendor, product, joystick->naxes);*/ if (joystick->naxes == 2) { /* Assume D-pad or thumbstick style axes are centered at 0 */ return SDL_TRUE; } for (i = 0; i < SDL_arraysize(zero_centered_joysticks); ++i) { if (id == zero_centered_joysticks[i]) { return SDL_TRUE; } } return SDL_FALSE; }
Uint16 SDL_GameControllerGetProduct(SDL_GameController * gamecontroller) { return SDL_JoystickGetProduct(SDL_GameControllerGetJoystick(gamecontroller)); }
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; } #else int main(int argc, char *argv[]) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n"); exit(1); }