urkle@8164: /* icculus@8180: Copyright (C) 1997-2014 Sam Lantinga urkle@8164: urkle@8164: This software is provided 'as-is', without any express or implied urkle@8164: warranty. In no event will the authors be held liable for any damages urkle@8164: arising from the use of this software. urkle@8164: urkle@8164: Permission is granted to anyone to use this software for any purpose, urkle@8164: including commercial applications, and to alter it and redistribute it urkle@8164: freely. urkle@8164: */ urkle@8164: urkle@8164: /* Simple program to test the SDL joystick hotplugging */ urkle@8164: urkle@8164: #include urkle@8164: #include urkle@8164: #include urkle@8164: urkle@8164: #include "SDL.h" urkle@8164: urkle@8188: #if !defined SDL_JOYSTICK_DISABLED && !defined SDL_HAPTIC_DISABLED urkle@8164: urkle@8164: int urkle@8164: main(int argc, char *argv[]) urkle@8164: { urkle@8164: SDL_Joystick *joystick = NULL; urkle@8164: SDL_Haptic *haptic = NULL; urkle@8164: SDL_JoystickID instance = -1; urkle@8164: SDL_bool keepGoing = SDL_TRUE; urkle@8188: int i; urkle@8188: SDL_bool enable_haptic = SDL_TRUE; urkle@8188: Uint32 init_subsystems = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK; urkle@8188: urkle@8188: for (i = 1; i < argc; ++i) { urkle@8188: if (SDL_strcasecmp(argv[i], "--nohaptic") == 0) { urkle@8188: enable_haptic = SDL_FALSE; urkle@8188: } urkle@8188: } urkle@8164: urkle@8188: if(enable_haptic) { urkle@8188: init_subsystems |= SDL_INIT_HAPTIC; urkle@8188: } urkle@8188: urkle@8164: /* Enable standard application logging */ urkle@8164: SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); urkle@8164: urkle@8164: SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); urkle@8164: urkle@8164: /* Initialize SDL (Note: video is required to start event loop) */ urkle@8188: if (SDL_Init(init_subsystems) < 0) { urkle@8164: SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); urkle@8164: exit(1); urkle@8164: } urkle@8164: philipp@8776: /* urkle@8164: //SDL_CreateWindow("Dummy", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 128, 128, 0); philipp@8776: */ urkle@8164: urkle@8164: SDL_Log("There are %d joysticks at startup\n", SDL_NumJoysticks()); urkle@8188: if (enable_haptic) urkle@8188: SDL_Log("There are %d haptic devices at startup\n", SDL_NumHaptics()); urkle@8164: urkle@8164: while(keepGoing) urkle@8164: { urkle@8164: SDL_Event event; urkle@8164: while(SDL_PollEvent(&event)) urkle@8164: { urkle@8164: switch(event.type) urkle@8164: { urkle@8164: case SDL_QUIT: urkle@8164: keepGoing = SDL_FALSE; urkle@8164: break; urkle@8164: case SDL_JOYDEVICEADDED: urkle@8164: if (joystick != NULL) urkle@8164: { urkle@8164: SDL_Log("Only one joystick supported by this test\n"); urkle@8164: } urkle@8164: else urkle@8164: { urkle@8164: joystick = SDL_JoystickOpen(event.jdevice.which); urkle@8164: instance = SDL_JoystickInstanceID(joystick); urkle@8164: SDL_Log("Joy Added : %d : %s\n", event.jdevice.which, SDL_JoystickName(joystick)); urkle@8188: if (enable_haptic) urkle@8164: { urkle@8188: if (SDL_JoystickIsHaptic(joystick)) urkle@8164: { urkle@8188: haptic = SDL_HapticOpenFromJoystick(joystick); urkle@8188: if (haptic) urkle@8164: { urkle@8188: SDL_Log("Joy Haptic Opened\n"); urkle@8188: if (SDL_HapticRumbleInit( haptic ) != 0) urkle@8188: { urkle@8188: SDL_Log("Could not init Rumble!: %s\n", SDL_GetError()); urkle@8188: SDL_HapticClose(haptic); urkle@8188: haptic = NULL; urkle@8188: } urkle@8188: } else { urkle@8188: SDL_Log("Joy haptic open FAILED!: %s\n", SDL_GetError()); urkle@8164: } urkle@8164: } urkle@8188: else urkle@8188: { urkle@8188: SDL_Log("No haptic found\n"); urkle@8188: } urkle@8165: } urkle@8164: } urkle@8164: break; urkle@8164: case SDL_JOYDEVICEREMOVED: urkle@8164: if (instance == event.jdevice.which) urkle@8164: { urkle@8164: SDL_Log("Joy Removed: %d\n", event.jdevice.which); urkle@8164: instance = -1; urkle@8188: if(enable_haptic && haptic) urkle@8164: { urkle@8164: SDL_HapticClose(haptic); urkle@8165: haptic = NULL; urkle@8164: } urkle@8164: SDL_JoystickClose(joystick); urkle@8164: joystick = NULL; urkle@8164: } else { urkle@8164: SDL_Log("Unknown joystick diconnected\n"); urkle@8164: } urkle@8164: break; urkle@8164: case SDL_JOYAXISMOTION: philipp@8778: /* urkle@8164: // SDL_Log("Axis Move: %d\n", event.jaxis.axis); philipp@8778: */ urkle@8188: if (enable_haptic) urkle@8188: SDL_HapticRumblePlay(haptic, 0.25, 250); urkle@8164: break; urkle@8164: case SDL_JOYBUTTONDOWN: urkle@8164: SDL_Log("Button Press: %d\n", event.jbutton.button); urkle@8188: if(enable_haptic && haptic) urkle@8164: { urkle@8188: SDL_HapticRumblePlay(haptic, 0.25, 250); urkle@8164: } urkle@8169: if (event.jbutton.button == 0) { urkle@8169: SDL_Log("Exiting due to button press of button 0\n"); urkle@8169: keepGoing = SDL_FALSE; urkle@8169: } urkle@8164: break; urkle@8164: case SDL_JOYBUTTONUP: urkle@8164: SDL_Log("Button Release: %d\n", event.jbutton.button); urkle@8164: break; urkle@8164: } urkle@8164: } urkle@8164: } urkle@8164: urkle@8188: SDL_Quit(); icculus@8181: icculus@8181: return 0; urkle@8164: } urkle@8164: #else urkle@8164: urkle@8164: int urkle@8164: main(int argc, char *argv[]) urkle@8164: { urkle@8188: SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick and haptic support.\n"); icculus@8181: return 1; urkle@8164: } urkle@8164: urkle@8164: #endif