void SDL2Manager::pluginUpdate(float deltaTime, bool jointsCaptured) { #ifdef HAVE_SDL2 if (_isInitialized) { auto userInputMapper = DependencyManager::get<UserInputMapper>(); for (auto joystick : _openJoysticks) { joystick->update(deltaTime, jointsCaptured); } PerformanceTimer perfTimer("SDL2Manager::update"); SDL_GameControllerUpdate(); SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_CONTROLLERAXISMOTION) { Joystick* joystick = _openJoysticks[event.caxis.which]; if (joystick) { joystick->handleAxisEvent(event.caxis); } } else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) { Joystick* joystick = _openJoysticks[event.cbutton.which]; if (joystick) { joystick->handleButtonEvent(event.cbutton); } if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK) { // this will either start or stop a global back event QEvent::Type backType = (event.type == SDL_CONTROLLERBUTTONDOWN) ? HFBackEvent::startType() : HFBackEvent::endType(); HFBackEvent backEvent(backType); qApp->sendEvent(qApp, &backEvent); } } else if (event.type == SDL_CONTROLLERDEVICEADDED) { SDL_GameController* controller = SDL_GameControllerOpen(event.cdevice.which); SDL_JoystickID id = getInstanceId(controller); if (!_openJoysticks.contains(id)) { Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller); _openJoysticks[id] = joystick; joystick->registerToUserInputMapper(*userInputMapper); joystick->assignDefaultInputMapping(*userInputMapper); emit joystickAdded(joystick); } } else if (event.type == SDL_CONTROLLERDEVICEREMOVED) { Joystick* joystick = _openJoysticks[event.cdevice.which]; _openJoysticks.remove(event.cdevice.which); userInputMapper->removeDevice(joystick->getDeviceID()); emit joystickRemoved(joystick); } } } #endif }
void JoystickScriptingInterface::update() { #ifdef HAVE_SDL2 if (_isInitialized) { PerformanceTimer perfTimer("JoystickScriptingInterface::update"); SDL_GameControllerUpdate(); SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_CONTROLLERAXISMOTION) { Joystick* joystick = _openJoysticks[event.caxis.which]; if (joystick) { joystick->handleAxisEvent(event.caxis); } } else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) { Joystick* joystick = _openJoysticks[event.cbutton.which]; if (joystick) { joystick->handleButtonEvent(event.cbutton); } if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK) { // this will either start or stop a global back event QEvent::Type backType = (event.type == SDL_CONTROLLERBUTTONDOWN) ? HFBackEvent::startType() : HFBackEvent::endType(); HFBackEvent backEvent(backType); qApp->sendEvent(qApp, &backEvent); } else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A) { // this will either start or stop a global action event QEvent::Type actionType = (event.type == SDL_CONTROLLERBUTTONDOWN) ? HFActionEvent::startType() : HFActionEvent::endType(); // global action events fire in the center of the screen HFActionEvent actionEvent(actionType, Application::getInstance()->getCamera()->computeViewPickRay(0.5f, 0.5f)); qApp->sendEvent(qApp, &actionEvent); } } else if (event.type == SDL_CONTROLLERDEVICEADDED) { SDL_GameController* controller = SDL_GameControllerOpen(event.cdevice.which); SDL_JoystickID id = getInstanceId(controller); Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller); _openJoysticks[id] = joystick; emit joystickAdded(joystick); } else if (event.type == SDL_CONTROLLERDEVICEREMOVED) { Joystick* joystick = _openJoysticks[event.cdevice.which]; _openJoysticks.remove(event.cdevice.which); emit joystickRemoved(joystick); } } } #endif }
bool DecafSDL::run(const std::string &gamePath) { auto shouldQuit = false; // Setup some basic window stuff auto window = mGraphicsDriver->getWindow(); setWindowIcon(window); if (config::display::mode == config::display::Fullscreen) { SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP); } // Setup OpenGL graphics driver auto graphicsDriver = mGraphicsDriver->getDecafDriver(); decaf::setGraphicsDriver(graphicsDriver); auto debugUiRenderer = mGraphicsDriver->getDecafDebugUiRenderer(); decaf::setDebugUiRenderer(debugUiRenderer); // Set input provider decaf::setInputDriver(this); decaf::addEventListener(this); openInputDevices(); // Set sound driver decaf::setSoundDriver(mSoundDriver); decaf::setClipboardTextCallbacks( [](void *context) -> const char * { return SDL_GetClipboardText(); }, [](void *context, const char *text) { SDL_SetClipboardText(text); }); // Initialise emulator if (!decaf::initialise(gamePath)) { return false; } // Start emulator decaf::start(); while (!shouldQuit && !decaf::hasExited()) { if (mVpad0Controller) { SDL_GameControllerUpdate(); } SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_WINDOWEVENT: if (event.window.event == SDL_WINDOWEVENT_CLOSE) { shouldQuit = true; } break; case SDL_MOUSEBUTTONDOWN: decaf::injectMouseButtonInput(translateMouseButton(event.button.button), decaf::input::MouseAction::Press); break; case SDL_MOUSEBUTTONUP: decaf::injectMouseButtonInput(translateMouseButton(event.button.button), decaf::input::MouseAction::Release); break; case SDL_MOUSEWHEEL: decaf::injectScrollInput(static_cast<float>(event.wheel.x), static_cast<float>(event.wheel.y)); break; case SDL_MOUSEMOTION: decaf::injectMousePos(static_cast<float>(event.motion.x), static_cast<float>(event.motion.y)); break; case SDL_KEYDOWN: decaf::injectKeyInput(translateKeyCode(event.key.keysym), decaf::input::KeyboardAction::Press); break; case SDL_KEYUP: if (event.key.keysym.sym == SDLK_TAB) { mToggleDRC = !mToggleDRC; } if (event.key.keysym.sym == SDLK_ESCAPE) { shouldQuit = true; } decaf::injectKeyInput(translateKeyCode(event.key.keysym), decaf::input::KeyboardAction::Release); break; case SDL_TEXTINPUT: decaf::injectTextInput(event.text.text); break; case SDL_QUIT: shouldQuit = true; break; } } Viewport tvViewport, drcViewport; calculateScreenViewports(tvViewport, drcViewport); mGraphicsDriver->renderFrame(tvViewport, drcViewport); } // Shut down decaf decaf::shutdown(); // Shut down graphics mGraphicsDriver->shutdown(); return true; }
static int lua_SDL_GameControllerUpdate(State & state){ SDL_GameControllerUpdate(); return 0; }
/* * IN_SDL_JoyCommands * * SDL game controller code called in IN_Commands. */ void IN_SDL_JoyCommands( void ) { int i, buttons = 0, buttonsDiff; static int buttonsOld; const int keys[] = { K_A_BUTTON, K_B_BUTTON, K_X_BUTTON, K_Y_BUTTON, K_ESCAPE, 0, 0, K_LSTICK, K_RSTICK, K_LSHOULDER, K_RSHOULDER, K_DPAD_UP, K_DPAD_DOWN, K_DPAD_LEFT, K_DPAD_RIGHT, K_LTRIGGER, K_RTRIGGER }; if( in_sdl_joyInitialized ) { SDL_GameControllerUpdate(); if( in_sdl_joyController && !SDL_GameControllerGetAttached( in_sdl_joyController ) ) { SDL_GameControllerClose( in_sdl_joyController ); in_sdl_joyController = NULL; } if( !in_sdl_joyController ) { int num = SDL_NumJoysticks(); for( i = 0; i < num; i++ ) { in_sdl_joyController = SDL_GameControllerOpen( i ); if( in_sdl_joyController ) break; } } } if( in_sdl_joyActive ) { SDL_GameController *controller = in_sdl_joyController; if( controller ) { for( i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++ ) { if( keys[i] && SDL_GameControllerGetButton( controller, i ) ) buttons |= 1 << i; } if( SDL_GameControllerGetButton( controller, SDL_CONTROLLER_BUTTON_START ) ) buttons |= 1 << SDL_CONTROLLER_BUTTON_BACK; if( SDL_GameControllerGetAxis( controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT ) > ( 30 * 128 ) ) buttons |= 1 << SDL_CONTROLLER_BUTTON_MAX; if( SDL_GameControllerGetAxis( controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT ) > ( 30 * 128 ) ) buttons |= 1 << ( SDL_CONTROLLER_BUTTON_MAX + 1 ); } } buttonsDiff = buttons ^ buttonsOld; if( buttonsDiff ) { unsigned int time = Sys_Milliseconds(); for( i = 0; i < ( sizeof( keys ) / sizeof( keys[0] ) ); i++ ) { if( buttonsDiff & ( 1 << i ) ) Key_Event( keys[i], ( buttons & ( 1 << i ) ) ? true : false, time ); } buttonsOld = buttons; } }