int main(int argc, char *argv[]) { const char *name; int i; SDL_Joystick *joystick; /* Initialize SDL (Note: video is required to start event loop) */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } /* Print information about the joysticks */ printf("There are %d joysticks attached\n", SDL_NumJoysticks()); for (i = 0; i < SDL_NumJoysticks(); ++i) { name = SDL_JoystickName(i); printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick"); joystick = SDL_JoystickOpen(i); if (joystick == NULL) { fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i, SDL_GetError()); } else { printf(" axes: %d\n", SDL_JoystickNumAxes(joystick)); printf(" balls: %d\n", SDL_JoystickNumBalls(joystick)); printf(" hats: %d\n", SDL_JoystickNumHats(joystick)); printf(" buttons: %d\n", SDL_JoystickNumButtons(joystick)); SDL_JoystickClose(joystick); } } if (argv[1]) { joystick = SDL_JoystickOpen(atoi(argv[1])); if (joystick == NULL) { printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError()); } else { WatchJoystick(joystick); SDL_JoystickClose(joystick); } } SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); return (0); }
int main(int argc, char *argv[]) { const char *name; int i; SDL_Joystick *joystick; /* 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_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), guid, sizeof (guid)); 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_JoystickClose(joystick); } } #ifdef ANDROID if (SDL_NumJoysticks() > 0) { #else if (argv[1]) { #endif SDL_bool reportederror = SDL_FALSE; SDL_bool keepGoing = SDL_TRUE; SDL_Event event; int device; #ifdef ANDROID device = 0; #else device = atoi(argv[1]); #endif joystick = SDL_JoystickOpen(device); 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) { joystick = SDL_JoystickOpen(device); break; } } } } else { SDL_Log("\n\nUsage: ./controllermap number\nFor example: ./controllermap 0\nOr: ./controllermap 0 >> gamecontrollerdb.txt"); } 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); }
int main(int argc, char *argv[]) { const char *name; int i; SDL_Joystick *joystick; /* Initialize SDL (Note: video is required to start event loop) */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); exit(1); } /* Print information about the joysticks */ printf("There are %d joysticks attached\n", SDL_NumJoysticks()); for (i = 0; i < SDL_NumJoysticks(); ++i) { name = SDL_JoystickNameForIndex(i); printf("Joystick %d: %s\n", i, name ? name : "Unknown Joystick"); joystick = SDL_JoystickOpen(i); if (joystick == NULL) { fprintf(stderr, "SDL_JoystickOpen(%d) failed: %s\n", i, SDL_GetError()); } else { char guid[64]; SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), guid, sizeof (guid)); printf(" axes: %d\n", SDL_JoystickNumAxes(joystick)); printf(" balls: %d\n", SDL_JoystickNumBalls(joystick)); printf(" hats: %d\n", SDL_JoystickNumHats(joystick)); printf(" buttons: %d\n", SDL_JoystickNumButtons(joystick)); printf("instance id: %d\n", SDL_JoystickInstanceID(joystick)); printf(" guid: %s\n", guid); SDL_JoystickClose(joystick); } } #ifdef ANDROID if (SDL_NumJoysticks() > 0) { #else if (argv[1]) { #endif SDL_bool reportederror = SDL_FALSE; SDL_bool keepGoing = SDL_TRUE; SDL_Event event; #ifdef ANDROID joystick = SDL_JoystickOpen(0); #else joystick = SDL_JoystickOpen(atoi(argv[1])); #endif while ( keepGoing ) { if (joystick == NULL) { if ( !reportederror ) { printf("Couldn't open joystick %d: %s\n", atoi(argv[1]), SDL_GetError()); keepGoing = SDL_FALSE; reportederror = SDL_TRUE; } } else { reportederror = SDL_FALSE; keepGoing = WatchJoystick(joystick); SDL_JoystickClose(joystick); } joystick = NULL; if (keepGoing) { printf("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) { joystick = SDL_JoystickOpen(atoi(argv[1])); break; } } } } SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); #ifdef ANDROID exit(0); #else return 0; #endif } #else int main(int argc, char *argv[]) { fprintf(stderr, "SDL compiled without Joystick support.\n"); exit(1); }
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); }