void InputDriver::update() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_JOYDEVICEADDED: { auto joystick = SDL_JoystickOpen(event.jdevice.which); auto guid = SDL_JoystickGetGUID(joystick); auto instanceId = SDL_JoystickInstanceID(joystick); { std::unique_lock lock { mConfigurationMutex }; auto connected = ConnectedJoystick { }; connected.joystick = joystick; connected.guid = guid; connected.instanceId = instanceId; connected.duplicateId = 0; for (auto &others : mJoysticks) { if (others.guid == connected.guid) { connected.duplicateId = std::max(connected.duplicateId, others.duplicateId + 1); } } for (auto &controller : mConfiguration.controllers) { for (auto &input : controller.inputs) { if (input.joystickGuid == guid && input.joystickDuplicateId == connected.duplicateId) { input.joystick = joystick; input.joystickInstanceId = instanceId; } } } mJoysticks.push_back(connected); } emit joystickConnected(instanceId, guid, SDL_JoystickName(joystick)); break; } case SDL_JOYDEVICEREMOVED: { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickDisconnected(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick)); { std::unique_lock lock { mConfigurationMutex }; for (auto itr = mJoysticks.begin(); itr != mJoysticks.end(); ++itr) { if (itr->joystick == joystick) { itr = mJoysticks.erase(itr); } } for (auto &controller : mConfiguration.controllers) { for (auto &input : controller.inputs) { if (input.joystick == joystick) { input.joystick = nullptr; } } } } SDL_JoystickClose(joystick); } break; } case SDL_JOYBUTTONDOWN: { if (mButtonEventsEnabled) { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickButtonDown(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick), event.jbutton.button); } } break; } case SDL_JOYAXISMOTION: { if (mButtonEventsEnabled) { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickAxisMotion(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick), event.jaxis.axis, translateAxisValue(event.jaxis.value)); } } break; } case SDL_JOYHATMOTION: { if (mButtonEventsEnabled) { auto joystick = SDL_JoystickFromInstanceID(event.jdevice.which); if (joystick) { joystickHatMotion(SDL_JoystickInstanceID(joystick), SDL_JoystickGetGUID(joystick), event.jhat.hat, event.jhat.value); } } break; } // I don't think we actually care about game controllers? case SDL_CONTROLLERDEVICEADDED: qDebug("SDL_CONTROLLERDEVICEADDED"); break; case SDL_CONTROLLERDEVICEREMAPPED: qDebug("SDL_CONTROLLERDEVICEREMAPPED"); break; case SDL_CONTROLLERDEVICEREMOVED: qDebug("SDL_CONTROLLERDEVICEREMOVED"); break; case SDL_CONTROLLERBUTTONDOWN: qDebug("SDL_CONTROLLERBUTTONDOWN"); break; } } QTimer::singleShot(10, Qt::PreciseTimer, this, SLOT(update())); }
int main(int argc, char *argv[]) { const char *name; 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)); 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); } } #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) { 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); }
static void pollEvent(Window* w, SDL_Event event) { if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) { Key* key = malloc(sizeof(Key)); key->isPress = event.key.type == SDL_KEYDOWN; key->isRepeat = event.key.repeat; key->code = event.key.keysym.sym; addEvent((void**) w->keys, key); } else if (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP) { MouseButton* mouseButton = malloc(sizeof(MouseButton)); mouseButton->isPress = event.button.type == SDL_MOUSEBUTTONDOWN; mouseButton->code = event.button.button; addEvent((void**) w->mouseButtons, mouseButton); } else if (event.type == SDL_MOUSEMOTION) { MouseMotion* mouseMotion = malloc(sizeof(MouseMotion)); mouseMotion->positionX = event.motion.x; mouseMotion->positionY = event.motion.y; mouseMotion->motionX = event.motion.xrel; mouseMotion->motionY = event.motion.yrel; addEvent((void**) w->mouseMotions, mouseMotion); } else if (event.type == SDL_MOUSEWHEEL) { Scroll* scroll = malloc(sizeof(Scroll)); scroll->x = event.wheel.x; scroll->y = event.wheel.y; addEvent((void**) w->scrolls, scroll); } else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) { JoystickButton* joystickButton = malloc(sizeof(JoystickButton)); joystickButton->id = event.jbutton.which; joystickButton->isPress = event.jbutton.state == SDL_PRESSED; joystickButton->code = event.jbutton.button; addEvent((void**) w->joystickButtons, joystickButton); } else if (event.type == SDL_JOYAXISMOTION) { JoystickAxis* joystickAxis = malloc(sizeof(JoystickAxis)); joystickAxis->id = event.jaxis.which; joystickAxis->axis = event.jaxis.axis; double value = event.jaxis.value / 32767.0; value = value < -1.0 ? -1.0 : value; joystickAxis->value = event.jaxis.value; addEvent((void**) w->joystickAxii, joystickAxis); } else if (event.type == SDL_JOYDEVICEADDED || event.type == SDL_JOYDEVICEREMOVED) { Joystick* joystick = malloc(sizeof(Joystick)); joystick->isAdd = event.type == SDL_JOYDEVICEADDED; SDL_Joystick* sdlJoystick; if (joystick->isAdd) { sdlJoystick = SDL_JoystickOpen(event.jdevice.which); } else { sdlJoystick = SDL_JoystickFromInstanceID(event.jdevice.which); } joystick->id = SDL_JoystickInstanceID(sdlJoystick); joystick->numberOfAxii = SDL_JoystickNumAxes(sdlJoystick); if (!joystick->isAdd) { SDL_JoystickClose(sdlJoystick); } addEvent((void**) w->joysticks, joystick); } else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { Size* size = malloc(sizeof(Size)); size->w = event.window.data1; size->h = event.window.data2; glViewport(0, 0, size->w, size->h); addEvent((void**) w->sizes, size); } else if (event.type == SDL_QUIT) { w->quit = 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); }