void GameWindow::HandleControllerInput() { //If three seconds have passed, check to see if new controllers have been plugged in if(ServiceLocator::GetPlatformLayer()->GetTicks() > m_LastControllerDetection + 3000) { DetectControllers(); } //Local variable used in the for loop below XINPUT_STATE state; //Cycle through all the connected controllers and check their input state unsigned int deviceIndex = 0; for(unsigned int deviceIndex = 0; deviceIndex < GameDev2D::ServiceLocator::GetInputManager()->GetNumberOfControllers(); deviceIndex++) { //Get the controller object and the device identifer from the index ControllerGeneric* controller = GameDev2D::ServiceLocator::GetInputManager()->GetControllerForIndex(deviceIndex); UINT controllerPort = (UINT)controller->GetDevice(); //Zero the memory of the state struct ZeroMemory(&state, sizeof(XINPUT_STATE)); //Can we get the controller state for this port if(XInputGetState(controllerPort, &state) == ERROR_SUCCESS) { //Handle the button input if(state.Gamepad.wButtons != controller->GetControllerData()->GetStateInfo().Gamepad.wButtons) { HandleControllerButtonInput(controller, controller->GetControllerData()->GetStateInfo().Gamepad.wButtons, state.Gamepad.wButtons); } //Handle left trigger input if(state.Gamepad.bLeftTrigger != controller->GetControllerData()->GetStateInfo().Gamepad.bLeftTrigger) { GameDev2D::ServiceLocator::GetInputManager()->HandleControllerInput(controller, VK_PAD_LTRIGGER, state.Gamepad.bLeftTrigger); } //Handle right trigger input if(state.Gamepad.bRightTrigger != controller->GetControllerData()->GetStateInfo().Gamepad.bRightTrigger) { GameDev2D::ServiceLocator::GetInputManager()->HandleControllerInput(controller, VK_PAD_RTRIGGER, state.Gamepad.bRightTrigger); } //Handle left analog stick x-axis input if(state.Gamepad.sThumbLX != controller->GetControllerData()->GetStateInfo().Gamepad.sThumbLX) { GameDev2D::ServiceLocator::GetInputManager()->HandleControllerInput(controller, VK_PAD_LTHUMB_LEFT, state.Gamepad.sThumbLX); } //Handle left analog stick y-axis input if(state.Gamepad.sThumbLY != controller->GetControllerData()->GetStateInfo().Gamepad.sThumbLY) { GameDev2D::ServiceLocator::GetInputManager()->HandleControllerInput(controller, VK_PAD_LTHUMB_UP, state.Gamepad.sThumbLY); } //Handle right analog stick x-axis input if(state.Gamepad.sThumbRX != controller->GetControllerData()->GetStateInfo().Gamepad.sThumbRX) { GameDev2D::ServiceLocator::GetInputManager()->HandleControllerInput(controller, VK_PAD_RTHUMB_LEFT, state.Gamepad.sThumbRX); } //Handle right analog stick y-axis input if(state.Gamepad.sThumbRY != controller->GetControllerData()->GetStateInfo().Gamepad.sThumbRY) { GameDev2D::ServiceLocator::GetInputManager()->HandleControllerInput(controller, VK_PAD_RTHUMB_UP, state.Gamepad.sThumbRY); } //Update the controllers state info controller->GetControllerData()->SetStateInfo(state); } else { //Notify the Input manager of the unplugged controller GameDev2D::ServiceLocator::GetInputManager()->HandleRemovedController((void*)controllerPort); } } }
//return true if succeded bool InputHandler::getState(int controllerNum,inputState& out) { XINPUT_STATE xInputState; float temp; //clear input for(int i = 0; i < 9;++i) out.buttons[i] = false; out.lX = 0; out.lY = 0; out.rX = 0; out.rY = 0; for(int i = 0; i < 9; ++i) { out.buttonLast[i] = state[controllerNum].buttons[i]; } //get input if(controllerNum >= 0 && controllerNum < 4) { if(XInputGetState(controllerNum,&xInputState) == ERROR_SUCCESS) { //left thumb stick x temp = xInputState.Gamepad.sThumbLX; if(temp < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE&&temp > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) temp = 0; temp /= MAXSHORT; out.lX = temp; //left thumb stick y temp = xInputState.Gamepad.sThumbLY; if(temp < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE&&temp > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) temp = 0; temp /= MAXSHORT; out.lY = temp; //right thumb stick x temp = xInputState.Gamepad.sThumbRX; if(temp < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE&&temp > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) temp = 0; temp /= MAXSHORT; temp *= sens[controllerNum].xSens; out.rX = temp; //right thump stick y temp = xInputState.Gamepad.sThumbRY; if(temp < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE&&temp > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) temp = 0; temp /= MAXSHORT; temp *= sens[controllerNum].ySens; out.rY = temp; //set buttons //get jump out.buttons[binds::jump] = xInputState.Gamepad.wButtons&XINPUT_GAMEPAD_A; //get legpower out.buttons[binds::legPower] = xInputState.Gamepad.wButtons&XINPUT_GAMEPAD_B; //get use out.buttons[binds::use] = xInputState.Gamepad.wButtons&XINPUT_GAMEPAD_X; //get Head power out.buttons[binds::headPower] = xInputState.Gamepad.wButtons&XINPUT_GAMEPAD_Y; //get Pause out.buttons[binds::pause] = xInputState.Gamepad.wButtons&XINPUT_GAMEPAD_START; //get left attack out.buttons[binds::leftAttack] = xInputState.Gamepad.bLeftTrigger >fireTriggerPoint; //get left alt attack out.buttons[binds::leftAltAttack] = xInputState.Gamepad.wButtons&XINPUT_GAMEPAD_LEFT_SHOULDER; //get right attack out.buttons[binds::rightAttack] = xInputState.Gamepad.bRightTrigger > fireTriggerPoint; //get right alt attack out.buttons[binds::rightAltAttack] = xInputState.Gamepad.wButtons&XINPUT_GAMEPAD_RIGHT_SHOULDER; state[controllerNum] = out; return true; } else return false; } else if(controllerNum == 4) { temp = mouseState.lX; temp *= sens[4].xSens; out.rX = temp; temp = mouseState.lY; temp *= sens[4].ySens; out.rY = temp; //check all binds if(getBoundKey(binds[binds::sprint])) temp = 1; else temp = 0.5f; // if(getBoundKey(binds[binds::forward])) { out.lY = temp; } else if(getBoundKey(binds[binds::back])) { out.lY = -temp; } // if(getBoundKey(binds[binds::right])) { out.lX = temp; } else if(getBoundKey(binds[binds::left])) { out.lX = -temp; } for(int i = 0; i < 9; ++i) { out.buttons[i] = getBoundKey(i); } state[controllerNum] = out; return true; } return false; }
static void xdk_input_poll(void *data) { xdk_input_t *xdk = (xdk_input_t*)data; #if defined(_XBOX1) unsigned int dwInsertions, dwRemovals; XGetDeviceChanges(XDEVICE_TYPE_GAMEPAD, reinterpret_cast<PDWORD>(&dwInsertions), reinterpret_cast<PDWORD>(&dwRemovals)); #endif xdk->analog_state[0][0][0] = xdk->analog_state[0][0][1] = xdk->analog_state[0][1][0] = xdk->analog_state[0][1][1] = 0; xdk->analog_state[1][0][0] = xdk->analog_state[1][0][1] = xdk->analog_state[1][1][0] = xdk->analog_state[1][1][1] = 0; xdk->analog_state[2][0][0] = xdk->analog_state[2][0][1] = xdk->analog_state[2][1][0] = xdk->analog_state[2][1][1] = 0; xdk->analog_state[3][0][0] = xdk->analog_state[3][0][1] = xdk->analog_state[3][1][0] = xdk->analog_state[3][1][1] = 0; for (unsigned port = 0; port < MAX_PADS; port++) { #ifdef _XBOX1 XINPUT_CAPABILITIES caps[MAX_PADS]; (void)caps; // handle removed devices xdk->bRemoved[port] = (dwRemovals & (1 << port)) ? true : false; if(xdk->bRemoved[port]) { // if the controller was removed after XGetDeviceChanges but before // XInputOpen, the device handle will be NULL if(xdk->gamepads[port]) XInputClose(xdk->gamepads[port]); xdk->gamepads[port] = 0; xdk->pad_state[port] = 0; } // handle inserted devices xdk->bInserted[port] = (dwInsertions & (1 << port)) ? true : false; if(xdk->bInserted[port]) { XINPUT_POLLING_PARAMETERS m_pollingParameters; m_pollingParameters.fAutoPoll = FALSE; m_pollingParameters.fInterruptOut = TRUE; m_pollingParameters.bInputInterval = 8; m_pollingParameters.bOutputInterval = 8; xdk->gamepads[port] = XInputOpen(XDEVICE_TYPE_GAMEPAD, port, XDEVICE_NO_SLOT, NULL); } if (!xdk->gamepads[port]) continue; // if the controller is removed after XGetDeviceChanges but before // XInputOpen, the device handle will be NULL #endif XINPUT_STATE state_tmp; #if defined(_XBOX1) if (XInputPoll(xdk->gamepads[port]) != ERROR_SUCCESS) continue; if (XInputGetState(xdk->gamepads[port], &state_tmp) != ERROR_SUCCESS) continue; #elif defined(_XBOX360) if (XInputGetState(port, &state_tmp) == ERROR_DEVICE_NOT_CONNECTED) continue; #endif uint64_t *state_cur = &xdk->pad_state[port]; *state_cur = 0; *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_LEFT) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_RIGHT) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_UP) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_DOWN) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_START) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_START) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_SELECT) : 0); #if defined(_XBOX1) *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_B]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_A) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_A]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_B) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_Y]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_X) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_X]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_Y) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_L) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_R) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_WHITE]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_L2) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_BLACK]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_R2) : 0); #elif defined(_XBOX360) *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_B) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_A) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_A) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_B) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_Y) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_X) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_X) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_Y) : 0); *state_cur |= ((state_tmp.Gamepad.bLeftTrigger > 128) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_L) : 0); *state_cur |= ((state_tmp.Gamepad.bRightTrigger > 128) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_R) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_L2) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_R2) : 0); #endif *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_L3) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_R3) : 0); xdk->analog_state[port][RETRO_DEVICE_INDEX_ANALOG_LEFT][RETRO_DEVICE_ID_ANALOG_X] = state_tmp.Gamepad.sThumbLX; xdk->analog_state[port][RETRO_DEVICE_INDEX_ANALOG_LEFT][RETRO_DEVICE_ID_ANALOG_Y] = state_tmp.Gamepad.sThumbLY; xdk->analog_state[port][RETRO_DEVICE_INDEX_ANALOG_RIGHT][RETRO_DEVICE_ID_ANALOG_X] = state_tmp.Gamepad.sThumbRX; xdk->analog_state[port][RETRO_DEVICE_INDEX_ANALOG_RIGHT][RETRO_DEVICE_ID_ANALOG_Y] = state_tmp.Gamepad.sThumbRY; if (g_settings.input.autodetect_enable) { if (strcmp(g_settings.input.device_names[port], "Xbox") != 0) xdk_input_set_keybinds(NULL, DEVICE_XBOX_PAD, port, 0, (1ULL << KEYBINDS_ACTION_SET_DEFAULT_BINDS)); } for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) if (xdk->analog_state[port][i][j] == -0x8000) xdk->analog_state[port][i][j] = -0x7fff; } uint64_t *state_p1 = &xdk->pad_state[0]; uint64_t *lifecycle_state = &g_extern.lifecycle_state; *lifecycle_state &= ~((1ULL << RARCH_MENU_TOGGLE)); if((*state_p1 & (1ULL << RETRO_DEVICE_ID_JOYPAD_L3)) && (*state_p1 & (1ULL << RETRO_DEVICE_ID_JOYPAD_R3))) *lifecycle_state |= (1ULL << RARCH_MENU_TOGGLE); }
void update() { connected = XInputGetState(index, &raw_state) == ERROR_SUCCESS; }
DLL_IMPORT DWORD XINPUT_GetState(DWORD dwUserIndex, XINPUT_STATE *pState) { printf("XInput_GetState(0x%X, 0x%X)\n", dwUserIndex, pState); return XInputGetState(dwUserIndex, pState); }
void Game::Input(void) { // Get state of any 360 controllers XInputGetState(black.controllernum, &black.controllerState); XInputGetState(grey.controllernum, &grey.controllerState); if (keys[VK_ESCAPE] & 0x80) { PostMessage(hWnd, WM_CLOSE, 0, 0); } //Black Ninja Input if (keys[VK_UP] & 0x80 && currLevel == Level2) { if (GetTickCount() > black.frameDuration && currLevel == Level2) { black.frame++; black.frameDuration = GetTickCount() + FRAMEDURATION; } black.vector.height = -DEFAULTSPEED; } if (keys[VK_DOWN] & 0x80 && currLevel == Level2) { if (GetTickCount() > black.frameDuration && currLevel == Level2) { black.frame++; black.frameDuration = GetTickCount() + FRAMEDURATION; } black.vector.height = DEFAULTSPEED; } if (keys[VK_LEFT] & 0x80 || black.controllerState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT || black.controllerState.Gamepad.sThumbLX < -DEADZONEBUFFER) { if (GetTickCount() > black.frameDuration && currLevel == Level2) { black.frame++; black.frameDuration = GetTickCount() + FRAMEDURATION; } black.vector.width = -DEFAULTSPEED; } if (keys[VK_RIGHT] & 0x80 || black.controllerState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT || black.controllerState.Gamepad.sThumbLX > DEADZONEBUFFER) { if (GetTickCount() > black.frameDuration && currLevel == Level2) { black.frame++; black.frameDuration = GetTickCount() + FRAMEDURATION; } black.vector.width = DEFAULTSPEED; } if (black.frame == 5) black.frame = 0; // Grey Ninja Input if (keys['A'] & 0x80 || grey.controllerState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT || grey.controllerState.Gamepad.sThumbLX < -DEADZONEBUFFER) { if (GetTickCount() > grey.frameDuration && currLevel == Level2) { grey.frame++; grey.frameDuration = GetTickCount() + FRAMEDURATION; } grey.vector.width = -DEFAULTSPEED; } if (keys['W'] & 0x80 && currLevel == Level2) { if (GetTickCount() > grey.frameDuration && currLevel == Level2) { grey.frame++; grey.frameDuration = GetTickCount() + FRAMEDURATION; } grey.vector.height = -DEFAULTSPEED; } if (keys['D'] & 0x80 || grey.controllerState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT || grey.controllerState.Gamepad.sThumbLX > DEADZONEBUFFER) { if (GetTickCount() > grey.frameDuration && currLevel == Level2) { grey.frame++; grey.frameDuration = GetTickCount() + FRAMEDURATION; } grey.vector.width = DEFAULTSPEED; } if (keys['S'] & 0x80 && currLevel == Level2) { if (GetTickCount() > grey.frameDuration && currLevel == Level2) { grey.frame++; grey.frameDuration = GetTickCount() + FRAMEDURATION; } grey.vector.height = DEFAULTSPEED; } if (grey.frame == 5) grey.frame = 0; ZeroMemory(&black.controllerState, sizeof(XINPUT_STATE)); ZeroMemory(&grey.controllerState, sizeof(XINPUT_STATE)); }
void WindowsXbox360ControllerDevice::update( const unsigned long id ) { Core::Message parameters(5,0,0); HWND foreground = GetForegroundWindow(); if ( this->_window_handle == foreground ) { parameters.add_integral_data(0,id); parameters.add_integral_data(2,this->_id); memcpy(&this->_previous_state,&this->_current_state,sizeof(XINPUT_STATE)); if ( XInputGetState(static_cast<DWORD>(this->_id),&this->_current_state) == ERROR_SUCCESS ) { parameters.add_integral_data(3,static_cast<Core::MESSAGE_INTEGRAL_DATA_TYPE>(this->_current_state.Gamepad.sThumbLX)); parameters.add_integral_data(4,static_cast<Core::MESSAGE_INTEGRAL_DATA_TYPE>(this->_current_state.Gamepad.sThumbLY)); trigger_event(Core::XBOX360_CONTROLLER_LEFTSTICK_POSITION,parameters); parameters.add_integral_data(3,static_cast<Core::MESSAGE_INTEGRAL_DATA_TYPE>(this->_current_state.Gamepad.sThumbRX)); parameters.add_integral_data(4,static_cast<Core::MESSAGE_INTEGRAL_DATA_TYPE>(this->_current_state.Gamepad.sThumbRY)); trigger_event(Core::XBOX360_CONTROLLER_RIGHTSTICK_POSITION,parameters); if ( this->_previous_state.dwPacketNumber != this->_current_state.dwPacketNumber ) { int difference_left_x = this->_current_state.Gamepad.sThumbLX - this->_previous_state.Gamepad.sThumbLX; int difference_left_y = this->_current_state.Gamepad.sThumbLY - this->_previous_state.Gamepad.sThumbLY; int difference_right_x = this->_current_state.Gamepad.sThumbRX - this->_previous_state.Gamepad.sThumbRX; int difference_right_y = this->_current_state.Gamepad.sThumbRY - this->_previous_state.Gamepad.sThumbRY; if ( abs(difference_left_x) > this->_deadzone || abs(difference_left_y) > this->_deadzone ) { parameters.add_integral_data(3,static_cast<Core::MESSAGE_INTEGRAL_DATA_TYPE>(difference_left_x)); parameters.add_integral_data(4,static_cast<Core::MESSAGE_INTEGRAL_DATA_TYPE>(difference_left_y)); trigger_event(Core::XBOX360_CONTROLLER_LEFTSTICK_POSITION_CHANGED,parameters); } if ( abs(difference_right_x) > this->_deadzone || abs(difference_right_y) > this->_deadzone ) { parameters.add_integral_data(3,static_cast<Core::MESSAGE_INTEGRAL_DATA_TYPE>(difference_right_x)); parameters.add_integral_data(4,static_cast<Core::MESSAGE_INTEGRAL_DATA_TYPE>(difference_right_y)); trigger_event(Core::XBOX360_CONTROLLER_RIGHTSTICK_POSITION_CHANGED,parameters); } int up = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP); int down = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN); int left = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT); int right = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT); int start = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_START) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_START); int back = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK); int left_thumb = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB); int right_thumb = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB); int left_shoulder = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER); int right_shoulder = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER); int a = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_A) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_A); int b = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_B) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_B); int x = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_X) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_X); int y = (this->_current_state.Gamepad.wButtons & XINPUT_GAMEPAD_Y) - (this->_previous_state.Gamepad.wButtons & XINPUT_GAMEPAD_Y); int left_trigger = this->_current_state.Gamepad.bLeftTrigger - this->_previous_state.Gamepad.bLeftTrigger; int right_trigger = this->_current_state.Gamepad.bRightTrigger - this->_previous_state.Gamepad.bRightTrigger; parameters.add_integral_data(3,0); parameters.add_integral_data(4,0); if ( up < 0 ) trigger_event(Core::XBOX360_CONTROLLER_DPAD_UP_UP,parameters); else if ( up > 0 ) trigger_event(Core::XBOX360_CONTROLLER_DPAD_UP_DOWN,parameters); if ( down < 0 ) trigger_event(Core::XBOX360_CONTROLLER_DPAD_DOWN_UP,parameters); else if ( down > 0 ) trigger_event(Core::XBOX360_CONTROLLER_DPAD_DOWN_DOWN,parameters); if ( left < 0 ) trigger_event(Core::XBOX360_CONTROLLER_DPAD_LEFT_UP,parameters); else if ( left > 0 ) trigger_event(Core::XBOX360_CONTROLLER_DPAD_LEFT_DOWN,parameters); if ( right < 0 ) trigger_event(Core::XBOX360_CONTROLLER_DPAD_RIGHT_UP,parameters); else if ( right > 0 ) trigger_event(Core::XBOX360_CONTROLLER_DPAD_RIGHT_DOWN,parameters); if ( start < 0 ) trigger_event(Core::XBOX360_CONTROLLER_START_UP,parameters); else if ( start > 0 ) trigger_event(Core::XBOX360_CONTROLLER_START_DOWN,parameters); if ( back < 0 ) trigger_event(Core::XBOX360_CONTROLLER_BACK_UP,parameters); else if ( back > 0 ) trigger_event(Core::XBOX360_CONTROLLER_BACK_DOWN,parameters); if ( left_thumb < 0 ) trigger_event(Core::XBOX360_CONTROLLER_LEFTSTICK_UP,parameters); else if ( left_thumb > 0 ) trigger_event(Core::XBOX360_CONTROLLER_LEFTSTICK_DOWN,parameters); if ( right_thumb < 0 ) trigger_event(Core::XBOX360_CONTROLLER_RIGHTSTICK_UP,parameters); else if ( right_thumb > 0 ) trigger_event(Core::XBOX360_CONTROLLER_RIGHTSTICK_DOWN,parameters); if ( left_shoulder < 0 ) trigger_event(Core::XBOX360_CONTROLLER_LEFTSHOULDER_UP,parameters); else if ( left_shoulder > 0 ) trigger_event(Core::XBOX360_CONTROLLER_LEFTSHOULDER_DOWN,parameters); if ( right_shoulder < 0 ) trigger_event(Core::XBOX360_CONTROLLER_RIGHTSHOULDER_UP,parameters); else if ( right_shoulder > 0 ) trigger_event(Core::XBOX360_CONTROLLER_RIGHTSHOULDER_DOWN,parameters); if ( a < 0 ) trigger_event(Core::XBOX360_CONTROLLER_A_UP,parameters); else if ( a > 0 ) trigger_event(Core::XBOX360_CONTROLLER_A_DOWN,parameters); if ( b < 0 ) trigger_event(Core::XBOX360_CONTROLLER_B_UP,parameters); else if ( b > 0 ) trigger_event(Core::XBOX360_CONTROLLER_B_DOWN,parameters); if ( x < 0 ) trigger_event(Core::XBOX360_CONTROLLER_X_UP,parameters); else if ( x > 0 ) trigger_event(Core::XBOX360_CONTROLLER_X_DOWN,parameters); if ( y < 0 ) trigger_event(Core::XBOX360_CONTROLLER_Y_UP,parameters); else if ( y > 0 ) trigger_event(Core::XBOX360_CONTROLLER_Y_DOWN,parameters); parameters.add_integral_data(3,static_cast<Core::MESSAGE_INTEGRAL_DATA_TYPE>(left_trigger)); if ( left_trigger < 0 ) trigger_event(Core::XBOX360_CONTROLLER_LEFTTRIGGER_UP,parameters); else if ( left_trigger > 0 ) trigger_event(Core::XBOX360_CONTROLLER_LEFTTRIGGER_DOWN,parameters); parameters.add_integral_data(3,static_cast<Core::MESSAGE_INTEGRAL_DATA_TYPE>(right_trigger)); if ( right_trigger < 0 ) trigger_event(Core::XBOX360_CONTROLLER_RIGHTTRIGGER_UP,parameters); else if ( right_trigger > 0 ) trigger_event(Core::XBOX360_CONTROLLER_RIGHTTRIGGER_DOWN,parameters); } } } };
void JoystickSamplingThread( void* data ) { static int prevTime = 0; static uint64 nextCheck[MAX_JOYSTICKS] = { 0 }; const uint64 waitTime = 5000000; // poll every 5 seconds to see if a controller was connected while( 1 ) { // hopefully we see close to 4000 usec each loop int now = Sys_Microseconds(); int delta; if( prevTime == 0 ) { delta = 4000; } else { delta = now - prevTime; } prevTime = now; threadTimeDeltas[threadCount & 255] = delta; threadCount++; { XINPUT_STATE joyData[MAX_JOYSTICKS]; bool validData[MAX_JOYSTICKS]; for( int i = 0 ; i < MAX_JOYSTICKS ; i++ ) { if( now >= nextCheck[i] ) { // XInputGetState might block... for a _really_ long time.. validData[i] = XInputGetState( i, &joyData[i] ) == ERROR_SUCCESS; // allow an immediate data poll if the input device is connected else // wait for some time to see if another device was reconnected. // Checking input state infrequently for newly connected devices prevents // severe slowdowns on PC, especially on WinXP64. if( validData[i] ) { nextCheck[i] = 0; } else { nextCheck[i] = now + waitTime; } } } // do this short amount of processing inside a critical section idScopedCriticalSection cs( win32.g_Joystick.mutexXis ); for( int i = 0 ; i < MAX_JOYSTICKS ; i++ ) { controllerState_t* cs = &win32.g_Joystick.controllers[i]; if( !validData[i] ) { cs->valid = false; continue; } cs->valid = true; XINPUT_STATE& current = joyData[i]; cs->current = current; // Switch from using cs->current to current to reduce chance of Load-Hit-Store on consoles threadPacket[threadCount & 255] = current.dwPacketNumber; #if 0 if( xis.dwPacketNumber == oldXis[ inputDeviceNum ].dwPacketNumber ) { return numEvents; } #endif cs->buttonBits |= current.Gamepad.wButtons; } } // we want this to be processed at least 250 times a second WaitForSingleObject( win32.g_Joystick.timer, INFINITE ); } }
/* ======================== idJoystickWin32::PollInputEvents ======================== */ int idJoystickWin32::PollInputEvents( int inputDeviceNum ) { numEvents = 0; if( !win32.activeApp ) { return numEvents; } assert( inputDeviceNum < 4 ); // if ( inputDeviceNum > in_joystick.GetInteger() ) { // return numEvents; // } controllerState_t* cs = &controllers[ inputDeviceNum ]; // grab the current packet under a critical section XINPUT_STATE xis; XINPUT_STATE old; int orBits; { idScopedCriticalSection crit( mutexXis ); xis = cs->current; old = cs->previous; cs->previous = xis; // fetch or'd button bits orBits = cs->buttonBits; cs->buttonBits = 0; } #if 0 if( XInputGetState( inputDeviceNum, &xis ) != ERROR_SUCCESS ) { return numEvents; } #endif for( int i = 0 ; i < 32 ; i++ ) { int bit = 1 << i; if( ( ( xis.Gamepad.wButtons | old.Gamepad.wButtons ) & bit ) == 0 && ( orBits & bit ) ) { idLib::Printf( "Dropped button press on bit %i\n", i ); } } if( session->IsSystemUIShowing() ) { // memset xis so the current input does not get latched if the UI is showing memset( &xis, 0, sizeof( XINPUT_STATE ) ); } int joyRemap[16] = { J_DPAD_UP, J_DPAD_DOWN, // Up, Down J_DPAD_LEFT, J_DPAD_RIGHT, // Left, Right J_ACTION9, J_ACTION10, // Start, Back J_ACTION7, J_ACTION8, // Left Stick Down, Right Stick Down J_ACTION5, J_ACTION6, // Black, White (Left Shoulder, Right Shoulder) 0, 0, // Unused J_ACTION1, J_ACTION2, // A, B J_ACTION3, J_ACTION4, // X, Y }; // Check the digital buttons for( int i = 0; i < 16; i++ ) { int mask = ( 1 << i ); if( ( xis.Gamepad.wButtons & mask ) != ( old.Gamepad.wButtons & mask ) ) { PostInputEvent( inputDeviceNum, joyRemap[i], ( xis.Gamepad.wButtons & mask ) > 0 ); } } // Check the triggers if( xis.Gamepad.bLeftTrigger != old.Gamepad.bLeftTrigger ) { PostInputEvent( inputDeviceNum, J_AXIS_LEFT_TRIG, xis.Gamepad.bLeftTrigger * 128 ); } if( xis.Gamepad.bRightTrigger != old.Gamepad.bRightTrigger ) { PostInputEvent( inputDeviceNum, J_AXIS_RIGHT_TRIG, xis.Gamepad.bRightTrigger * 128 ); } if( xis.Gamepad.sThumbLX != old.Gamepad.sThumbLX ) { PostInputEvent( inputDeviceNum, J_AXIS_LEFT_X, xis.Gamepad.sThumbLX ); } if( xis.Gamepad.sThumbLY != old.Gamepad.sThumbLY ) { PostInputEvent( inputDeviceNum, J_AXIS_LEFT_Y, -xis.Gamepad.sThumbLY ); } if( xis.Gamepad.sThumbRX != old.Gamepad.sThumbRX ) { PostInputEvent( inputDeviceNum, J_AXIS_RIGHT_X, xis.Gamepad.sThumbRX ); } if( xis.Gamepad.sThumbRY != old.Gamepad.sThumbRY ) { PostInputEvent( inputDeviceNum, J_AXIS_RIGHT_Y, -xis.Gamepad.sThumbRY ); } return numEvents; }
void OBSAPIInterface::HandleHotkeys() { List<DWORD> hitKeys; static bool allow_other_hotkey_modifiers; static bool uplay_overlay_compatibility; static bool set_vars = false; /* only query these config variables once */ if (!set_vars) { allow_other_hotkey_modifiers = !!GlobalConfig->GetInt(TEXT("General"), TEXT("AllowOtherHotkeyModifiers"), true); uplay_overlay_compatibility = !!GlobalConfig->GetInt(L"General", L"UplayOverlayCompatibility", false); set_vars = true; } DWORD modifiers = 0; if(GetAsyncKeyState(VK_MENU) & 0x8000) modifiers |= HOTKEYF_ALT; if(GetAsyncKeyState(VK_CONTROL) & 0x8000) modifiers |= HOTKEYF_CONTROL; if (!uplay_overlay_compatibility) if (GetAsyncKeyState(VK_SHIFT) & 0x8000) modifiers |= HOTKEYF_SHIFT; OSEnterMutex(App->hHotkeyMutex); for(UINT i=0; i<hotkeys.Num(); i++) { HotkeyInfo &info = hotkeys[i]; DWORD hotkeyVK = LOBYTE(info.hotkey); DWORD hotkeyModifiers = HIBYTE(info.hotkey); DWORD xinputNum = LOWORD(info.hotkey); DWORD xinputButton = HIWORD(info.hotkey); hotkeyModifiers &= ~(HOTKEYF_EXT); if(xinputButton) { XINPUT_STATE state = { 0 }; if(XInputGetState(xinputNum, &state) == ERROR_SUCCESS) { if(state.Gamepad.bLeftTrigger >= 85) state.Gamepad.wButtons |= XINPUT_GAMEPAD_LEFT_TRIGGER; if(state.Gamepad.bRightTrigger >= 85) state.Gamepad.wButtons |= XINPUT_GAMEPAD_RIGHT_TRIGGER; if((state.Gamepad.wButtons & xinputButton) != 0 && !info.bHotkeyDown) { PostMessage(hwndMain, OBS_CALLHOTKEY, TRUE, info.hotkeyID); info.bDownSent = true; info.bHotkeyDown = true; } } info.bModifiersDown = 0; } else { bool bModifiersMatch = false; if(allow_other_hotkey_modifiers) bModifiersMatch = ((hotkeyModifiers & modifiers) == hotkeyModifiers); //allows other modifiers to be pressed else bModifiersMatch = (hotkeyModifiers == modifiers); if(hotkeyModifiers && !hotkeyVK) //modifier-only hotkey { if((hotkeyModifiers & modifiers) == hotkeyModifiers) { if(!info.bHotkeyDown) { PostMessage(hwndMain, OBS_CALLHOTKEY, TRUE, info.hotkeyID); info.bDownSent = true; info.bHotkeyDown = true; } continue; } } else { if (bModifiersMatch && !(uplay_overlay_compatibility && hotkeyVK == VK_F2)) { short keyState = GetAsyncKeyState(hotkeyVK); bool bDown = (keyState & 0x8000) != 0; bool bWasPressed = (keyState & 0x1) != 0; if(bDown || bWasPressed) { if(!info.bHotkeyDown && info.bModifiersDown) //only triggers the hotkey if the actual main key was pressed second { PostMessage(hwndMain, OBS_CALLHOTKEY, TRUE, info.hotkeyID); info.bDownSent = true; } info.bHotkeyDown = true; if(bDown) continue; } } } info.bModifiersDown = bModifiersMatch; } if(info.bHotkeyDown) //key up { if(info.bDownSent) { PostMessage(hwndMain, OBS_CALLHOTKEY, FALSE, info.hotkeyID); info.bDownSent = false; } info.bHotkeyDown = false; } } OSLeaveMutex(App->hHotkeyMutex); }
static void xdk_joypad_poll(void) { uint64_t *state_p1, *lifecycle_state; unsigned port; #if defined(_XBOX1) unsigned int dwInsertions, dwRemovals; XGetDeviceChanges(XDEVICE_TYPE_GAMEPAD, reinterpret_cast<PDWORD>(&dwInsertions), reinterpret_cast<PDWORD>(&dwRemovals)); #endif for (port = 0; port < MAX_PADS; port++) { #ifdef _XBOX1 XINPUT_CAPABILITIES caps[MAX_PADS]; (void)caps; /* handle removed devices. */ bRemoved[port] = (dwRemovals & (1 << port)) ? true : false; if(bRemoved[port]) { /* if the controller was removed after * XGetDeviceChanges but before * XInputOpen, the device handle will be NULL. */ if(gamepads[port]) XInputClose(gamepads[port]); gamepads[port] = 0; pad_state[port] = 0; } /* handle inserted devices. */ bInserted[port] = (dwInsertions & (1 << port)) ? true : false; if(bInserted[port]) { XINPUT_POLLING_PARAMETERS m_pollingParameters; m_pollingParameters.fAutoPoll = FALSE; m_pollingParameters.fInterruptOut = TRUE; m_pollingParameters.bInputInterval = 8; m_pollingParameters.bOutputInterval = 8; gamepads[port] = XInputOpen(XDEVICE_TYPE_GAMEPAD, port, XDEVICE_NO_SLOT, NULL); } if (!gamepads[port]) continue; /* if the controller is removed after * XGetDeviceChanges but before XInputOpen, * the device handle will be NULL. */ #endif XINPUT_STATE state_tmp; #if defined(_XBOX1) if (XInputPoll(gamepads[port]) != ERROR_SUCCESS) continue; if (XInputGetState(gamepads[port], &state_tmp) != ERROR_SUCCESS) continue; #elif defined(_XBOX360) if (XInputGetState(port, &state_tmp) == ERROR_DEVICE_NOT_CONNECTED) continue; #endif uint64_t *state_cur = &pad_state[port]; *state_cur = 0; *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_LEFT) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_RIGHT) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_UP) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_DOWN) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_START) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_START) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_SELECT) : 0); #if defined(_XBOX1) *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_B]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_A) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_A]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_B) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_Y]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_X) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_X]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_Y) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_L) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_R) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_WHITE]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_L2) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_BLACK]) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_R2) : 0); #elif defined(_XBOX360) *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_B) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_A) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_A) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_B) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_Y) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_X) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_X) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_Y) : 0); *state_cur |= ((state_tmp.Gamepad.bLeftTrigger > 128) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_L) : 0); *state_cur |= ((state_tmp.Gamepad.bRightTrigger > 128) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_R) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_L2) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_R2) : 0); #endif *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_L3) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) ? (1ULL << RETRO_DEVICE_ID_JOYPAD_R3) : 0); analog_state[port][RETRO_DEVICE_INDEX_ANALOG_LEFT][RETRO_DEVICE_ID_ANALOG_X] = state_tmp.Gamepad.sThumbLX; analog_state[port][RETRO_DEVICE_INDEX_ANALOG_LEFT][RETRO_DEVICE_ID_ANALOG_Y] = state_tmp.Gamepad.sThumbLY; analog_state[port][RETRO_DEVICE_INDEX_ANALOG_RIGHT][RETRO_DEVICE_ID_ANALOG_X] = state_tmp.Gamepad.sThumbRX; analog_state[port][RETRO_DEVICE_INDEX_ANALOG_RIGHT][RETRO_DEVICE_ID_ANALOG_Y] = state_tmp.Gamepad.sThumbRY; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) if (analog_state[port][i][j] == -0x8000) analog_state[port][i][j] = -0x7fff; } state_p1 = &pad_state[0]; lifecycle_state = &g_extern.lifecycle_state; *lifecycle_state &= ~((1ULL << RARCH_MENU_TOGGLE)); if((*state_p1 & (1ULL << RETRO_DEVICE_ID_JOYPAD_L3)) && (*state_p1 & (1ULL << RETRO_DEVICE_ID_JOYPAD_R3))) *lifecycle_state |= (1ULL << RARCH_MENU_TOGGLE); }
void Controller::UpdateState() { if(!_connected) { return; } #if defined(WIN32) _dwResult = XInputGetState(_controllerID, &_currentControllerRawState); // We either don't have a controller or the Input has failed. Keep clearing the input // data so we don't get garbage input. if (_dwResult != ERROR_SUCCESS) { if (_connected) { sysLog.Printf("Uh oh, we lost controller %d...", _controllerID+1); } _connected = false; ZeroMemory( &_currentControllerRawState, sizeof(XINPUT_STATE)); _currentControllerInput.LeftThumbstickX = _currentControllerInput.LeftThumbstickY = _currentControllerInput.RightThumbstickX = _currentControllerInput.RightThumbstickY = _currentControllerInput.LeftTriggerValue = _currentControllerInput.RightTriggerValue = _currentControllerInput.Buttons = 0; return; } _connected = true; _currentControllerInput.Buttons = _currentControllerRawState.Gamepad.wButtons; //TODO: dpad _currentControllerInput.LeftTriggerValue = (int)_currentControllerRawState.Gamepad.bLeftTrigger; _currentControllerInput.RightTriggerValue = (int)_currentControllerRawState.Gamepad.bRightTrigger; _currentControllerInput.LeftThumbstickX = _currentControllerRawState.Gamepad.sThumbLX; _currentControllerInput.LeftThumbstickY = _currentControllerRawState.Gamepad.sThumbLY; _currentControllerInput.RightThumbstickX = _currentControllerRawState.Gamepad.sThumbRX; _currentControllerInput.RightThumbstickY = _currentControllerRawState.Gamepad.sThumbRY; #elif defined(__APPLE__) _currentControllerInput.LeftThumbstickX = _getValueForCookie(0x1b); _currentControllerInput.LeftThumbstickY = -_getValueForCookie(0x1c); _currentControllerInput.RightThumbstickX = _getValueForCookie(0x1d); _currentControllerInput.RightThumbstickY = -_getValueForCookie(0x1e); _currentControllerInput.LeftTriggerValue = _getValueForCookie(0x19); _currentControllerInput.RightTriggerValue = _getValueForCookie(0x1a); _currentControllerInput.Buttons = 0; if (_getValueForCookie(0x0a)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_DPAD_UP; if (_getValueForCookie(0x0b)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_DPAD_DOWN; if (_getValueForCookie(0x0c)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_DPAD_LEFT; if (_getValueForCookie(0x0d)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_DPAD_RIGHT; if (_getValueForCookie(0x0e)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_START; if (_getValueForCookie(0x0f)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_BACK; if (_getValueForCookie(0x10)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_LEFT_THUMB; if (_getValueForCookie(0x11)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_RIGHT_THUMB; if (_getValueForCookie(0x12)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_LEFT_SHOULDER; if (_getValueForCookie(0x13)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_RIGHT_SHOULDER; if (_getValueForCookie(0x15)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_A; if (_getValueForCookie(0x16)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_B; if (_getValueForCookie(0x17)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_X; if (_getValueForCookie(0x18)) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_Y; #elif defined(__linux__) struct js_event js; while (read(_deviceFD, &js, sizeof(struct js_event)) == sizeof(struct js_event)) { switch (js.type) { case JS_EVENT_BUTTON: unsigned int currButton; switch (js.number) { case 0: currButton = XINPUT_GAMEPAD_A; break; case 1: currButton = XINPUT_GAMEPAD_B; break; case 2: currButton = XINPUT_GAMEPAD_X; break; case 3: currButton = XINPUT_GAMEPAD_Y; break; case 4: currButton = XINPUT_GAMEPAD_LEFT_SHOULDER; break; case 5: currButton = XINPUT_GAMEPAD_RIGHT_SHOULDER; break; case 6: currButton = XINPUT_GAMEPAD_START; break; case 7: // Big 'X' button - not used. continue; case 8: currButton = XINPUT_GAMEPAD_LEFT_THUMB; break; case 9: currButton = XINPUT_GAMEPAD_RIGHT_THUMB; break; case 10: currButton = XINPUT_GAMEPAD_BACK; break; default: sysLog.Printf("Error! Unknown button press event received!\n"); continue; } if (js.value) // button pressed _currentControllerInput.Buttons |= currButton; else // button released _currentControllerInput.Buttons &= ~currButton; break; case JS_EVENT_AXIS: switch (js.number) { case 0: _currentControllerInput.LeftThumbstickX = js.value; break; case 1: _currentControllerInput.LeftThumbstickY = -js.value; break; case 2: _currentControllerInput.LeftTriggerValue = js.value; break; case 3: _currentControllerInput.RightThumbstickX = js.value; break; case 4: _currentControllerInput.RightThumbstickY = -js.value; break; case 5: _currentControllerInput.RightTriggerValue = js.value; break; case 6: if (js.value == 0) _currentControllerInput.Buttons &= ~(XINPUT_GAMEPAD_DPAD_LEFT & XINPUT_GAMEPAD_DPAD_RIGHT); else if (js.value < 0) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_DPAD_LEFT; else _currentControllerInput.Buttons |= XINPUT_GAMEPAD_DPAD_RIGHT; break; case 7: if (js.value == 0) _currentControllerInput.Buttons &= ~(XINPUT_GAMEPAD_DPAD_UP & XINPUT_GAMEPAD_DPAD_DOWN); else if (js.value < 0) _currentControllerInput.Buttons |= XINPUT_GAMEPAD_DPAD_UP; else _currentControllerInput.Buttons |= XINPUT_GAMEPAD_DPAD_DOWN; break; default: sysLog.Printf("Error! Unknown axis event received!\n"); break; } break; default: break; } } #endif //Using the built-in deadzone -- comment out if you don't like it if (_currentControllerInput.LeftThumbstickX < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && _currentControllerInput.LeftThumbstickX > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && _currentControllerInput.LeftThumbstickY < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && _currentControllerInput.LeftThumbstickY > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) { _currentControllerInput.LeftThumbstickX = 0; _currentControllerInput.LeftThumbstickY = 0; } if (_currentControllerInput.RightThumbstickX < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && _currentControllerInput.RightThumbstickX > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && _currentControllerInput.RightThumbstickY < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && _currentControllerInput.RightThumbstickY > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) { _currentControllerInput.RightThumbstickX = 0; _currentControllerInput.RightThumbstickY = 0; } theInput.HandleControl( *this ); }
void Controller::Setup() { #if defined(WIN32) ZeroMemory( &_currentControllerRawState, sizeof(XINPUT_STATE)); _dwResult = XInputGetState(0, &_currentControllerRawState); if (_dwResult == ERROR_SUCCESS) { sysLog.Printf("Controller %d connected!", _controllerID+1); _connected = true; } else { sysLog.Printf("Controller %d not present...", _controllerID+1); ZeroMemory( &_currentControllerRawState, sizeof(XINPUT_STATE)); _connected = false; } #elif defined(__APPLE__) unsigned long usagePage = 0; unsigned long usage = 0; if (!HIDHaveDeviceList()) { HIDBuildDeviceList(usagePage, usage); } _device = HIDGetFirstDevice(); while (_device != NULL) { //is this device already taken by another controller? bool breakIt = false; for (int i=0; i < MAX_CONTROLLERS; i++) { Controller* check = &theControllerManager.GetController(i); if ((check != this) && (check->_device == _device)) { _device = HIDGetNextDevice(_device); if (_device == NULL) { breakIt = true; } } } if (breakIt) { break; } std::string manufacturer = _device->manufacturer; std::string product = _device->product; if (manufacturer.length() > 0) manufacturer = manufacturer.substr(1, manufacturer.length()-1).c_str(); //trimming off the initial copyright symbol so matching won't be dumb if ((manufacturer == "Microsoft Corporation") && (product == "Controller")) { sysLog.Printf("Controller %d connected!", _controllerID+1); _connected = true; break; } _device = HIDGetNextDevice(_device); } if (_device == NULL) { sysLog.Printf("Controller %d not present...", _controllerID+1); _connected = false; } else { pRecElement current_element = HIDGetFirstDeviceElement(_device, kHIDElementTypeIO); while (current_element != NULL) { _elements[(unsigned int)current_element->cookie] = current_element; current_element = HIDGetNextDeviceElement(current_element, kHIDElementTypeIO); } } #elif defined(__linux__) char* devicePath; if (_controllerID == 0) devicePath = LINUX_CONTROLLER_1_PATH; else devicePath = LINUX_CONTROLLER_2_PATH; _deviceFD = open(devicePath, O_RDONLY | O_NONBLOCK); if (_deviceFD < 0) { sysLog.Printf("Controller %d not present...", _controllerID+1); _connected = false; } else { sysLog.Printf("Controller %d connected!", _controllerID+1); _connected = true; } // Discover the force feedback device. bool foundFirstController = false; _ffFD = -1; for (int i = 0; i < MAX_LINUX_EVENT_INTERFACES; i++) { std::stringstream ss; ss << i; String eventDev = LINUX_EVENT_INTERFACE + ss.str(); int fd = open(eventDev.c_str(), O_RDWR); if (fd >= 0) { char name[256] = "Unknown"; ioctl(fd, EVIOCGNAME(sizeof(name)), name); if (strcmp(name, "Microsoft X-Box 360 pad") == 0) { if (_controllerID == 0) { _ffFD = fd; break; } else { if (foundFirstController) { _ffFD = fd; break; } else { foundFirstController = true; close(fd); } } } else { close(fd); } } } if (_ffFD < 0) { sysLog.Printf("Error opening Force Feedback device for controller %d!", _controllerID+1); } else { _ffEffect.type = FF_RUMBLE; _ffEffect.id = -1; _ffEffect.u.rumble.strong_magnitude = 0; _ffEffect.u.rumble.weak_magnitude = 0; _ffEffect.replay.length = 0x7fff; _ffEffect.replay.delay = 0; _ffPlay.type = EV_FF; _ffPlay.value = 1; } #endif }
void Input::PollXInputDevice(int deviceNum, Controller* pad) { XINPUT_STATE state; ZeroMemory(&state, sizeof state); DWORD result = XInputGetState(deviceNum, &state); if(result != ERROR_SUCCESS) return; //left thumbstick handling float LX = state.Gamepad.sThumbLX; float LY = state.Gamepad.sThumbLY; float magnitudeL = sqrt(LX * LX + LY * LY); float normalizedLX = LX / magnitudeL; float normalizedLY = LY / magnitudeL; float normalizedMagnitudeL = 0; if(magnitudeL > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) { if(magnitudeL > 32767) magnitudeL = 32767; magnitudeL -= XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE; normalizedMagnitudeL = magnitudeL / (32767 - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE); } else { magnitudeL = 0.0; normalizedMagnitudeL = 0.0; } pad->leftAnalog[0] = normalizedMagnitudeL * normalizedLX; pad->leftAnalog[1] = normalizedMagnitudeL * normalizedLY; //right thumbstick handling float RX = state.Gamepad.sThumbRX; float RY = state.Gamepad.sThumbRY; float magnitudeR = sqrt(RX * RX + RY * RY); float normalizedRX = RX / magnitudeR; float normalizedRY = RY / magnitudeR; float normalizedMagnitudeR = 0; if (magnitudeR > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) { if (magnitudeR > 32767) magnitudeR = 32767; magnitudeR -= XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE; normalizedMagnitudeR = magnitudeR / (32767 - XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE); } else { magnitudeR = 0.0; normalizedMagnitudeR = 0.0; } pad->rightAnalog[0] = normalizedMagnitudeR * normalizedRX; pad->rightAnalog[1] = normalizedMagnitudeR * normalizedRY; //trigger handling float triggerMagnitudeL = state.Gamepad.bLeftTrigger; float normalizedTriggerMagnitudeL = 0.0f; if(triggerMagnitudeL > XINPUT_GAMEPAD_TRIGGER_THRESHOLD) { if(triggerMagnitudeL > 255) triggerMagnitudeL = 255; triggerMagnitudeL -= XINPUT_GAMEPAD_TRIGGER_THRESHOLD; normalizedTriggerMagnitudeL = triggerMagnitudeL / (255 - XINPUT_GAMEPAD_TRIGGER_THRESHOLD); } pad->leftTrigger = normalizedTriggerMagnitudeL; float triggerMagnitudeR = state.Gamepad.bRightTrigger; float normalizedTriggerMagnitudeR = 0.0f; if(triggerMagnitudeR > XINPUT_GAMEPAD_TRIGGER_THRESHOLD) { if(triggerMagnitudeR > 255) triggerMagnitudeR = 255; triggerMagnitudeR -= XINPUT_GAMEPAD_TRIGGER_THRESHOLD; normalizedTriggerMagnitudeR = triggerMagnitudeR / (255 - XINPUT_GAMEPAD_TRIGGER_THRESHOLD); } pad->rightTrigger = normalizedTriggerMagnitudeR; //buttons handling pad->buttons[A] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_A) != 0; pad->buttons[B] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_B) != 0; pad->buttons[X] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_X) != 0; pad->buttons[Y] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_Y) != 0; pad->buttons[START] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_START) != 0; pad->buttons[SELECT] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) != 0; pad->buttons[L_SHOULDER] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) != 0; pad->buttons[R_SHOULDER] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) != 0; pad->buttons[D_UP] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) != 0; pad->buttons[D_LEFT] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) != 0; pad->buttons[D_DOWN] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) != 0; pad->buttons[D_RIGHT] = (state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) != 0; }
HRESULT InputGamepad::UpdateControllerState() { DWORD dwResult; for( DWORD i = 0; i < MAX_CONTROLLERS; i++ ) { // Simply get the state of the controller from XInput. dwResult = XInputGetState( i, &m_Controllers[i].state ); if (dwResult == ERROR_SUCCESS){m_Controllers[i].bConnected = true;} else {m_Controllers[i].bConnected = false;} // つながってないときはチェックしない if( !m_Controllers[i].bConnected ) continue ; // スティックの状態を取得(無効領域はOFF,有効領域はON) // 左 bool stick = m_Controllers[i].state.Gamepad.sThumbLX < -INPUT_DEADZONE; if (stick && ((gamepadState & 0x10000) != 0)) { gamepadAction[GamePadLStick_LEFT] = KEY_ON; } if (stick && ((gamepadState & 0x10000) == 0)) { gamepadAction[GamePadLStick_LEFT] = KEY_PUSH; } if (!stick && ((gamepadState & 0x10000) == 0)){ gamepadAction[GamePadLStick_LEFT] = KEY_OFF; } if (!stick && ((gamepadState & 0x10000) != 0)) { gamepadAction[GamePadLStick_LEFT] = KEY_RELEASE; } // 前回のキーの状態として保存 if (stick) { gamepadState |= 0x10000; } else { gamepadState &= ~0x10000; } // 右 stick = m_Controllers[i].state.Gamepad.sThumbLX > INPUT_DEADZONE; if (stick && ((gamepadState & 0x20000) != 0)) { gamepadAction[GamePadLStick_RIGHT] = KEY_ON; } if (stick && ((gamepadState & 0x20000) == 0)) { gamepadAction[GamePadLStick_RIGHT] = KEY_PUSH; } if (!stick && ((gamepadState & 0x20000) == 0)) { gamepadAction[GamePadLStick_RIGHT] = KEY_OFF; } if (!stick && ((gamepadState & 0x20000) != 0)) { gamepadAction[GamePadLStick_RIGHT] = KEY_RELEASE; } // 前回のキーの状態として保存 if (stick) { gamepadState |= 0x20000; } else { gamepadState &= ~0x20000; } // 上 stick = m_Controllers[i].state.Gamepad.sThumbLY > INPUT_DEADZONE; if (stick && ((gamepadState & 0x40000) != 0)) { gamepadAction[GamePadLStick_UP] = KEY_ON; } if (stick && ((gamepadState & 0x40000) == 0)) { gamepadAction[GamePadLStick_UP] = KEY_PUSH; } if (!stick && ((gamepadState & 0x40000) == 0)) { gamepadAction[GamePadLStick_UP] = KEY_OFF; } if (!stick && ((gamepadState & 0x40000) != 0)) { gamepadAction[GamePadLStick_UP] = KEY_RELEASE; } // 前回のキーの状態として保存 if (stick) { gamepadState |= 0x40000; } else { gamepadState &= ~0x40000; } // 下 stick = m_Controllers[i].state.Gamepad.sThumbLY < -INPUT_DEADZONE; if (stick && ((gamepadState & 0x80000) != 0)) { gamepadAction[GamePadLStick_DOWN] = KEY_ON; } if (stick && ((gamepadState & 0x80000) == 0)) { gamepadAction[GamePadLStick_DOWN] = KEY_PUSH; } if (!stick && ((gamepadState & 0x80000) == 0)) { gamepadAction[GamePadLStick_DOWN] = KEY_OFF; } if (!stick && ((gamepadState & 0x80000) != 0)) { gamepadAction[GamePadLStick_DOWN] = KEY_RELEASE; } // 前回のキーの状態として保存 if (stick) { gamepadState |= 0x80000; } else { gamepadState &= ~0x80000; } // 各キーの状態を取得 for( int i = 0; i < XINPUT_KEY_MAX; i++ ) { // ボタンの状態をチェック bool now = (m_Controllers->state.Gamepad.wButtons & XINPUT_KEY_LIST[i]) != 0; // 前回押されてて今回押されてるとき if( now && ((gamepadState & XINPUT_KEY_LIST[i]) != 0) ) { gamepadAction[ i ] = KEY_ON; } // 前回押されてなくて今回押されてるとき if( now && ((gamepadState & XINPUT_KEY_LIST[i]) == 0) ) { gamepadAction[ i ] = KEY_PUSH; } // 前回押されてなくて今回押されていないとき if( !now && ((gamepadState & XINPUT_KEY_LIST[i]) == 0) ) { gamepadAction[ i ] = KEY_OFF; } // 前回押されていて今回押されていないと if( !now && ((gamepadState & XINPUT_KEY_LIST[i]) != 0) ) { gamepadAction[ i ] = KEY_RELEASE; } // 前回のキーの状態として保存 if (now) { gamepadState |= XINPUT_KEY_LIST[i]; } else { gamepadState &= ~XINPUT_KEY_LIST[i]; } } } return S_OK; }
DWORD ControllerBase::GetKeystroke(DWORD dwReserved, XINPUT_KEYSTROKE* pKeystroke) { if (m_passthrough) return XInputModuleManager::Get().XInputGetKeystroke(m_passthroughindex, dwReserved, pKeystroke); XINPUT_STATE xstate; ZeroMemory(&xstate, sizeof(XINPUT_STATE)); XInputGetState(m_user, &xstate); static WORD repeat[14]; static WORD flags[14]; ZeroMemory(pKeystroke, sizeof(XINPUT_KEYSTROKE)); pKeystroke->UserIndex = (BYTE)m_user; static const WORD allButtonIDs[14] = { XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y, XINPUT_GAMEPAD_LEFT_SHOULDER, XINPUT_GAMEPAD_RIGHT_SHOULDER, XINPUT_GAMEPAD_BACK, XINPUT_GAMEPAD_START, XINPUT_GAMEPAD_LEFT_THUMB, XINPUT_GAMEPAD_RIGHT_THUMB, XINPUT_GAMEPAD_DPAD_UP, XINPUT_GAMEPAD_DPAD_DOWN, XINPUT_GAMEPAD_DPAD_LEFT, XINPUT_GAMEPAD_DPAD_RIGHT }; static const u16 keyIDs[14] = { VK_PAD_A, VK_PAD_B, VK_PAD_X, VK_PAD_Y, VK_PAD_LSHOULDER, VK_PAD_RSHOULDER, VK_PAD_BACK, VK_PAD_START, VK_PAD_LTHUMB_PRESS, VK_PAD_RTHUMB_PRESS, VK_PAD_DPAD_UP, VK_PAD_DPAD_DOWN, VK_PAD_DPAD_LEFT, VK_PAD_DPAD_RIGHT }; for (int i = 0; i < 14; i++) { if (xstate.Gamepad.wButtons & allButtonIDs[i]) { if (flags[i] == NULL) { pKeystroke->VirtualKey = keyIDs[i]; pKeystroke->Flags = flags[i] = XINPUT_KEYSTROKE_KEYDOWN; break; } if ((flags[i] & XINPUT_KEYSTROKE_KEYDOWN)) { if (repeat[i] <= 0) { repeat[i] = 5; pKeystroke->VirtualKey = keyIDs[i]; pKeystroke->Flags = flags[i] = XINPUT_KEYSTROKE_KEYDOWN | XINPUT_KEYSTROKE_REPEAT; break; } else { repeat[i]--; continue; } } } if (!(xstate.Gamepad.wButtons & allButtonIDs[i])) { if (flags[i] & XINPUT_KEYSTROKE_KEYDOWN) { repeat[i] = 5 * 4; pKeystroke->VirtualKey = keyIDs[i]; pKeystroke->Flags = flags[i] = XINPUT_KEYSTROKE_KEYUP; break; } if (flags[i] & XINPUT_KEYSTROKE_KEYUP) { pKeystroke->Flags = flags[i] = NULL; break; } } } //PrintLog("ret: %u, flags: %u, hid: %u, unicode: %c, user: %u, vk: 0x%X",ret,pKeystroke->Flags,pKeystroke->HidCode,pKeystroke->Unicode,pKeystroke->UserIndex,pKeystroke->VirtualKey); if (pKeystroke->VirtualKey) return ERROR_SUCCESS; else return ERROR_EMPTY; }
/** * * This function updates the controller in order to see if buttons are being pushed * * @Author Carsten Scholz * @return Returns true if the update was successful * */ bool CXInputController::Update() { DWORD dwResult; XINPUT_STATE ControllerState; dwResult = XInputGetState(m_uiController, &ControllerState); if(dwResult == ERROR_SUCCESS) { m_bIsConnected = true; } else if(ERROR_DEVICE_NOT_CONNECTED == dwResult) { m_bIsConnected = false; return (false); } uint32* pTemp = m_pActiveButtons; m_pActiveButtons = m_pInactiveButtons; m_pInactiveButtons = pTemp; *m_pActiveButtons = ControllerState.Gamepad.wButtons; //LEFT ANALOG STICK if (abs(ControllerState.Gamepad.sThumbLX) > m_iLeftThumbStickDeadZone) { m_fLeftThumbStickX = (float32)ControllerState.Gamepad.sThumbLX / (float32)(SHRT_MAX - m_iLeftThumbStickDeadZone); } else { m_fLeftThumbStickX = 0.0f; } if (abs(ControllerState.Gamepad.sThumbLY) > m_iLeftThumbStickDeadZone) { m_fLeftThumbStickY = (float32)ControllerState.Gamepad.sThumbLY / (float32)(SHRT_MAX - m_iLeftThumbStickDeadZone); } else { m_fLeftThumbStickY = 0.0f; } //RIGHT ANALOG STICK int32 iRightThumbStickTotal = abs(ControllerState.Gamepad.sThumbRX) + abs(ControllerState.Gamepad.sThumbRY); if(iRightThumbStickTotal > m_iRightThumbStickDeadZone) { m_fRightThumbStickX = (float32)ControllerState.Gamepad.sThumbRX / (float32)(SHRT_MAX - m_iRightThumbStickDeadZone); m_fRightThumbStickY = (float32)ControllerState.Gamepad.sThumbRY / (float32)(SHRT_MAX - m_iRightThumbStickDeadZone); } else { m_fRightThumbStickX = 0.0f; m_fRightThumbStickY = 0.0f; } if(ControllerState.Gamepad.bRightTrigger < m_iRightTriggerDeadZone) { m_fRightTrigger = 0.0f; } else { m_fRightTrigger = (float32)ControllerState.Gamepad.bRightTrigger / (float32)(UCHAR_MAX - m_iRightTriggerDeadZone); } if(ControllerState.Gamepad.bLeftTrigger < m_fLeftTrigger) { m_fLeftTrigger = 0.0f; } else { m_fLeftTrigger = (float32)ControllerState.Gamepad.bLeftTrigger / (float32)(UCHAR_MAX - m_iLeftTriggerDeadZone); } return true; }
bool IsConnected(int XPadPlayer) { XINPUT_STATE xstate; DWORD xresult = XInputGetState(XPadPlayer, &xstate); return (xresult == ERROR_SUCCESS); }
void Game::Startup(void) { //TODO: Create Back Buffer // Initialize DirectX. HRESULT hr = CreateGraphics(hWnd); if (FAILED(hr)) { return; // Return -1 to abort Window Creation. } black.frame = 0; black.health = 100; black.position = BLACKINITPOS; black.vector = ZEROVECTOR; black.frameDuration = GetTickCount() + FRAMEDURATION; black.controllernum = 0; XInputGetState(black.controllernum, &black.controllerState); // TESTING CONTROLLER //black.vibrateState.wLeftMotorSpeed = 60000; //XInputSetState(black.controllernum, &black.vibrateState); grey.frame = 0; grey.health = 100; grey.position = GREYINITPOS; grey.vector = ZEROVECTOR; grey.frameDuration = GetTickCount() + FRAMEDURATION; grey.controllernum = 1; XInputGetState(grey.controllernum, &grey.controllerState); floor1pos = D2D1::RectF(400, 0, pRT->GetSize().width - 400, pRT->GetSize().height); floor2pos = D2D1::RectF(400, -pRT->GetSize().height, pRT->GetSize().width - 400, 0); floor3pos = D2D1::RectF(400, -2 * pRT->GetSize().height, pRT->GetSize().width - 400, -pRT->GetSize().height); floorvec = D2D1::SizeF(0, 15); D2D1_RECT_F divider = D2D1::RectF(900, 0, 910, pRT->GetSize().height); D2D1_RECT_F leftwall = D2D1::RectF(397, 0, 403, pRT->GetSize().height); D2D1_RECT_F rightwall = D2D1::RectF(pRT->GetSize().width - 403, 0, pRT->GetSize().width - 397, pRT->GetSize().height); rects.push_front(divider); rects.push_front(leftwall); rects.push_front(rightwall); // Left Lane Rects for (size_t i = 0; i < NUMRECTS; i++) { Obstacle temp; temp.position.left = (rand() % 396) + 400; temp.position.right = temp.position.left + ((rand() % 30) + 75); temp.position.top = -(rand() % (int)(3 * pRT->GetSize().height)); temp.position.bottom = temp.position.top + 20; temp.hitbox.left = temp.position.left + 18; temp.hitbox.right = temp.position.right - 18; temp.hitbox.top = temp.position.top +5; temp.hitbox.bottom = temp.position.bottom -5; obstacles.push_back(temp); } // Right Lane Rects for (size_t i = 0; i < NUMRECTS; i++) { Obstacle temp; temp.position.left = (rand() % 396) + 900; temp.position.right = temp.position.left + ((rand() % 30) + 75); temp.position.top = -(rand() % (int)(3 * pRT->GetSize().height)); temp.position.bottom = temp.position.top + 20; temp.hitbox.left = temp.position.left + 15; temp.hitbox.right = temp.position.right - 15; temp.hitbox.top = temp.position.top; temp.hitbox.bottom = temp.position.bottom; obstacles.push_back(temp); } obstVec = D2D1::SizeF(0, OBSTACLESPEED); currState = Playing; /*nNinjaFrame = 0; nGreyNinjaFrame = 0; playerpos = D2D1::RectF(300, 300, 350, 350); player2pos = D2D1::RectF(200, 200, 250, 250); dwGreyNinjaTime = GetTickCount() + 100; dwBlackNinjaTime = GetTickCount() + 100;*/ //TODO: Initialize Game Components }
bool Read(int XPadPlayer, u32 deviceType, EmulatedDevices::FT0::SStatus* status) { const int base = 0x80; XINPUT_STATE xstate; DWORD xresult = XInputGetState(XPadPlayer, &xstate); // Let's .. yes, let's use XINPUT! if (xresult == ERROR_SUCCESS) { const XINPUT_GAMEPAD& xpad = xstate.Gamepad; switch (deviceType) { case ID_STDCONTROLLER: ScaleStickValues(&status->joyx, &status->joyy, xpad.sThumbLX, xpad.sThumbLY); ScaleStickValues(&status->joy2x, &status->joy2y, xpad.sThumbRX, xpad.sThumbRY); status->ltrig = xpad.bLeftTrigger; status->rtrig = xpad.bRightTrigger; if (xpad.wButtons & XINPUT_GAMEPAD_A) {status->buttons ^= CONT_BUTTON_A;} if (xpad.wButtons & XINPUT_GAMEPAD_B) {status->buttons ^= CONT_BUTTON_B;} if (xpad.wButtons & XINPUT_GAMEPAD_X) {status->buttons ^= CONT_BUTTON_X;} if (xpad.wButtons & XINPUT_GAMEPAD_Y) {status->buttons ^= CONT_BUTTON_Y;} if (xpad.wButtons & XINPUT_GAMEPAD_START) {status->buttons ^= CONT_BUTTON_START;} if (xpad.wButtons & XINPUT_GAMEPAD_DPAD_UP) {status->buttons ^= CONT_DPAD_UP;} if (xpad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) {status->buttons ^= CONT_DPAD_DOWN;} if (xpad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) {status->buttons ^= CONT_DPAD_LEFT;} if (xpad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) {status->buttons ^= CONT_DPAD_RIGHT;} break; case ID_TWINSTICK: { ScaleStickValues(&status->joyx, &status->joyy, xpad.sThumbLX, xpad.sThumbLY); ScaleStickValues(&status->joy2x, &status->joy2y, xpad.sThumbRX, xpad.sThumbRY); int x = status->joyx; int y = status->joyy; int x2 = status->joy2x; int y2 = status->joy2y; if (x != 0) { if (x > 168) status->buttons ^= CONT_DPAD_RIGHT; if (x < 40) status->buttons ^= CONT_DPAD_LEFT; } if (y != 0) { if (y > 168) status->buttons ^= CONT_DPAD_DOWN; if (y < 40) status->buttons ^= CONT_DPAD_UP; } if (x2 != 0) { if (x2 > 168) status->buttons ^= CONT_DPAD2_RIGHT; if (x2 < 40) status->buttons ^= CONT_DPAD2_LEFT; } if (y2 != 0) { if (y2 > 168) status->buttons ^= CONT_DPAD2_DOWN; if (y2 < 40) status->buttons ^= CONT_DPAD2_UP; } if (xpad.bLeftTrigger > 0x10) {status->buttons ^= CONT_BUTTON_X;} if (xpad.bRightTrigger > 0x10) {status->buttons ^= CONT_BUTTON_A;} if (xpad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) {status->buttons ^= CONT_BUTTON_Y;} if (xpad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) {status->buttons ^= CONT_BUTTON_B;} if (xpad.wButtons & XINPUT_GAMEPAD_BACK) {status->buttons ^= CONT_BUTTON_D;} if (xpad.wButtons & XINPUT_GAMEPAD_START) {status->buttons ^= CONT_BUTTON_START;} } break; case ID_ARCADESTICK: { ScaleStickValues(&status->joyx, &status->joyy, xpad.sThumbLX, xpad.sThumbLY); int x = status->joyx; int y = status->joyy; if (x != 0) { if (x > 168) status->buttons ^= CONT_DPAD_RIGHT; if (x < 40) status->buttons ^= CONT_DPAD_LEFT; } if (y != 0) { if (y > 168) status->buttons ^= CONT_DPAD_DOWN; if (y < 40) status->buttons ^= CONT_DPAD_UP; } if (xpad.wButtons & XINPUT_GAMEPAD_A) {status->buttons ^= CONT_BUTTON_A;} if (xpad.wButtons & XINPUT_GAMEPAD_B) {status->buttons ^= CONT_BUTTON_B;} if (xpad.wButtons & XINPUT_GAMEPAD_X) {status->buttons ^= CONT_BUTTON_X;} if (xpad.wButtons & XINPUT_GAMEPAD_Y) {status->buttons ^= CONT_BUTTON_Y;} if (xpad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER){status->buttons ^= CONT_BUTTON_C;} if (xpad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER){status->buttons ^= CONT_BUTTON_Z;} if (xpad.wButtons & XINPUT_GAMEPAD_START) {status->buttons ^= CONT_BUTTON_START;} } break; } return true; } else { if (xresult == ERROR_DEVICE_NOT_CONNECTED) printf("x360 pad %i not connected\n", XPadPlayer); return false; } }
void r3dGamepad::GetState() { isConnected = false; memcpy(&PrevState, &State, sizeof(XINPUT_STATE)); leftTrigger = 0.0f; rightTrigger = 0.0f; leftThumbX = 0.0f; leftThumbY = 0.0f; rightThumbX = 0.0f; rightThumbY = 0.0f; if( XInputGetState( 0, &State ) == ERROR_SUCCESS ) { isConnected = true; // left thumb { float LX = State.Gamepad.sThumbLX; float LY = State.Gamepad.sThumbLY; //determine how far the controller is pushed float magnitude = sqrt(LX*LX + LY*LY); if(magnitude == 0.0f) magnitude = 1.0f; //determine the direction the controller is pushed float normalizedLX = LX / magnitude; float normalizedLY = LY / magnitude; float normalizedMagnitude = 0; //check if the controller is outside a circular dead zone if (magnitude > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) { //clip the magnitude at its expected maximum value if (magnitude > 32767) magnitude = 32767; //adjust magnitude relative to the end of the dead zone magnitude -= XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE; //optionally normalize the magnitude with respect to its expected range //giving a magnitude value of 0.0 to 1.0 normalizedMagnitude = magnitude / (32767 - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE); } else //if the controller is in the deadzone zero out the magnitude { magnitude = 0.0; normalizedMagnitude = 0.0; } leftThumbX = normalizedLX * normalizedMagnitude; leftThumbY = normalizedLY * normalizedMagnitude; } // right thumb { float LX = State.Gamepad.sThumbRX; float LY = State.Gamepad.sThumbRY; //determine how far the controller is pushed float magnitude = sqrt(LX*LX + LY*LY); if(magnitude == 0.0f) magnitude = 1.0f; //determine the direction the controller is pushed float normalizedLX = LX / magnitude; float normalizedLY = LY / magnitude; float normalizedMagnitude = 0; //check if the controller is outside a circular dead zone if (magnitude > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE ) { //clip the magnitude at its expected maximum value if (magnitude > 32767) magnitude = 32767; //adjust magnitude relative to the end of the dead zone magnitude -= XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE ; //optionally normalize the magnitude with respect to its expected range //giving a magnitude value of 0.0 to 1.0 normalizedMagnitude = magnitude / (32767 - XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE ); } else //if the controller is in the deadzone zero out the magnitude { magnitude = 0.0; normalizedMagnitude = 0.0; } rightThumbX = normalizedLX * normalizedMagnitude; rightThumbY = normalizedLY * normalizedMagnitude; } // trigger { float LX = State.Gamepad.bLeftTrigger; float LY = State.Gamepad.bRightTrigger; //determine how far the controller is pushed float magnitude = sqrt(LX*LX + LY*LY); if(magnitude == 0.0f) magnitude = 1.0f; //determine the direction the controller is pushed float normalizedLX = LX / magnitude; float normalizedLY = LY / magnitude; float normalizedMagnitude = 0; //check if the controller is outside a circular dead zone if (magnitude > XINPUT_GAMEPAD_TRIGGER_THRESHOLD ) { //clip the magnitude at its expected maximum value if (magnitude > 255) magnitude = 255; //adjust magnitude relative to the end of the dead zone magnitude -= XINPUT_GAMEPAD_TRIGGER_THRESHOLD ; //optionally normalize the magnitude with respect to its expected range //giving a magnitude value of 0.0 to 1.0 normalizedMagnitude = magnitude / (255 - XINPUT_GAMEPAD_TRIGGER_THRESHOLD ); } else //if the controller is in the deadzone zero out the magnitude { magnitude = 0.0; normalizedMagnitude = 0.0; } leftTrigger = normalizedLX * normalizedMagnitude; rightTrigger = normalizedLY * normalizedMagnitude; } } }
void Win360GamepadController::PollData() { XINPUT_STATE state; ZeroMemory(&state, sizeof(state)); changeCount = 0; DWORD dwResult = XInputGetState(index, &state); DWORD timeStamp = GetTickCount(); if (dwResult == ERROR_SUCCESS) { if (state.dwPacketNumber != prevState.dwPacketNumber) { // we have some changes we will need to register InputEventList& iel = inputEvents[currBuffer]; if (state.Gamepad.wButtons != prevState.Gamepad.wButtons) { // insert button events #define XBUTSTATE(state, i) (state.Gamepad.wButtons & (1 << i)) for (uint32 i = 0; i < 16; ++i) { if (XBUTSTATE(state, i) != XBUTSTATE(prevState, i)) { if (buttonMap[i] != KeyName::KEY_INVALID_CODE) { InputEvent &ev = iel[changeCount++]; KeyState k = buttonStates[buttonMap[i] - NEX_XB360_CTRL_BUTTON_START].value = XBUTSTATE(state, i) ? KeyState::KEY_STATE_DOWN : KeyState::KEY_STATE_UP; ev.key = buttonMap[i]; ev.keyState = k; ev.timeStamp = timeStamp; } } } #undef XBUTSTATE } if (state.Gamepad.bLeftTrigger != prevState.Gamepad.bLeftTrigger) { InputEvent &ev = iel[changeCount++]; if (state.Gamepad.bLeftTrigger < triggerDeadZone) state.Gamepad.bLeftTrigger = 0; else state.Gamepad.bLeftTrigger -= triggerDeadZone; ev.analogValue = trigValues[0].value = (float)state.Gamepad.bLeftTrigger / (255.0f - triggerDeadZone); ev.key = KeyName::XBOX_TRIG_LEFT; ev.timeStamp = timeStamp; } if (state.Gamepad.bRightTrigger != prevState.Gamepad.bRightTrigger) { InputEvent &ev = iel[changeCount++]; if (state.Gamepad.bRightTrigger < triggerDeadZone) state.Gamepad.bRightTrigger = 0; else state.Gamepad.bRightTrigger -= triggerDeadZone; ev.analogValue = trigValues[1].value = (float)state.Gamepad.bRightTrigger / (255.0f - triggerDeadZone); ev.key = KeyName::XBOX_TRIG_RIGHT; ev.timeStamp = timeStamp; } if (state.Gamepad.sThumbLX != prevState.Gamepad.sThumbLX || state.Gamepad.sThumbLY != prevState.Gamepad.sThumbLY) { InputEvent &ev = iel[changeCount++]; ev.analogDir = axes[0].value = GetCircular(state.Gamepad.sThumbLX, state.Gamepad.sThumbLY, thumbDeadZone[0]); ev.key = KeyName::XBOX_AXIS_LEFT; ev.timeStamp = timeStamp; } if (state.Gamepad.sThumbRX != prevState.Gamepad.sThumbRX || state.Gamepad.sThumbRY != prevState.Gamepad.sThumbRY) { InputEvent &ev = iel[changeCount++]; ev.analogDir = axes[0].value = GetCircular(state.Gamepad.sThumbRX, state.Gamepad.sThumbRY, thumbDeadZone[1]); ev.key = KeyName::XBOX_AXIS_RIGHT; ev.timeStamp = timeStamp; } prevState = state; } } }
void Xbox360Input_Win32::update() { DWORD dwResult; XINPUT_STATE state; for(int id=0;id<MAX_CONTROLLERS;id++) { ZeroMemory(&state,sizeof(XINPUT_STATE)); dwResult = XInputGetState(id,&state); if (dwResult == ERROR_SUCCESS) { bConnected[id] = true; //printf("XBOX -- Controller %i connected\n",id); if (state.dwPacketNumber != dwPacketNumber[id]) { fLeftTrigger[id] = (float) (state.Gamepad.bLeftTrigger-XINPUT_GAMEPAD_TRIGGER_THRESHOLD < 0 ? 0 : state.Gamepad.bLeftTrigger-XINPUT_GAMEPAD_TRIGGER_THRESHOLD) / (float) (255-XINPUT_GAMEPAD_TRIGGER_THRESHOLD); fRightTrigger[id] = (float) (state.Gamepad.bRightTrigger-XINPUT_GAMEPAD_TRIGGER_THRESHOLD < 0 ? 0 : state.Gamepad.bRightTrigger-XINPUT_GAMEPAD_TRIGGER_THRESHOLD) / (float) (255-XINPUT_GAMEPAD_TRIGGER_THRESHOLD); bool neg = false; SHORT thumbRawX, thumbRawY; SHORT fixedX, fixedY, deadzone; fixedX = -1000; fixedY = -1000; thumbRawX = state.Gamepad.sThumbLX; thumbRawY = state.Gamepad.sThumbLY; //printf("XBOX -- Controller %i raw left thumb values: %i %i\n",id,state.Gamepad.sThumbLX,state.Gamepad.sThumbLY); deadzone = 32767-XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE; if (thumbRawX < 0) if (thumbRawX > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) fixedX = 0; if (thumbRawX > 0) if (thumbRawX < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) fixedX = 0; if (thumbRawX < 0 && fixedX == -1000) fixedX = thumbRawX+XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE; if (thumbRawX > 0 && fixedX == -1000) fixedX = thumbRawX-XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE; if (thumbRawY < 0) if (thumbRawY > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) fixedY = 0; if (thumbRawY > 0) if (thumbRawY < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) fixedY = 0; if (thumbRawY < 0 && fixedY == -1000) fixedY = thumbRawY+XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE; if (thumbRawY > 0 && fixedY == -1000) fixedY = thumbRawY-XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE; if (fixedX != -1000) fThumbLX[id] = abs((float)(fixedX)) / (float)(deadzone) * (thumbRawX < 0 ? -1 : 1); if (fixedY != -1000) fThumbLY[id] = abs((float)(fixedY)) / (float)(deadzone) * (thumbRawY < 0 ? 1 : -1); //printf("XBOX -- Controller %i fixed left thumb values: %f %f\n",id,fThumbLX[id],fThumbLY[id]); fixedX = -1000; fixedY = -1000; thumbRawX = state.Gamepad.sThumbRX; thumbRawY = state.Gamepad.sThumbRY; deadzone = 32767-XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE; if (thumbRawX < 0) if (thumbRawX > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) fixedX = 0; if (thumbRawX > 0) if (thumbRawX < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) fixedX = 0; if (thumbRawX < 0 && fixedX == -1000) fixedX = thumbRawX+XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE; if (thumbRawX > 0 && fixedX == -1000) fixedX = thumbRawX-XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE; if (thumbRawY < 0) if (thumbRawY > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) fixedY = 0; if (thumbRawY > 0) if (thumbRawY < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) fixedY = 0; if (thumbRawY < 0 && fixedY == -1000) fixedY = thumbRawY+XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE; if (thumbRawY > 0 && fixedY == -1000) fixedY = thumbRawY-XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE; if (fixedX != -1000) fThumbRX[id] = abs((float)(fixedX)) / (float)(deadzone) * (thumbRawX < 0 ? -1 : 1); if (fixedY != -1000) fThumbRY[id] = abs((float)(fixedY)) / (float)(deadzone) * (thumbRawY < 0 ? 1 : -1); wButtons[id] = state.Gamepad.wButtons; } dwPacketNumber[id] = state.dwPacketNumber; } else bConnected[id] = false; } }
int main(int argc, char *argv[]) { int ret; #if defined(_XBOX) XINPUT_STATE state; get_environment_settings(); XInputGetState(0, &state); if(state.Gamepad.wButtons & XINPUT_GAMEPAD_Y) { //override path, boot first executable in cores directory RARCH_LOG("Fallback - Will boot first executable in RetroArch cores directory.\n"); find_and_set_first_file(); } else { //normal executable loading path init_settings(); } XLaunchNewImage(libretro_path, NULL); RARCH_LOG("Launch libretro core: [%s] (return code: %x]).\n", libretro_path, ret); #elif defined(__CELLOS_LV2__) CellPadData pad_data; char spawn_data[256], spawn_data_size[16]; SceNpDrmKey * k_licensee = NULL; cellSysmoduleLoadModule(CELL_SYSMODULE_IO); cellSysmoduleLoadModule(CELL_SYSMODULE_FS); cellSysmoduleLoadModule(CELL_SYSMODULE_SYSUTIL_GAME); cellSysmoduleLoadModule(CELL_SYSMODULE_NET); cellSysmoduleLoadModule(CELL_SYSMODULE_SYSUTIL_NP); sys_net_initialize_network(); #ifdef HAVE_LOGGER logger_init(); #endif sceNpInit(NP_POOL_SIZE, np_pool); get_environment_settings(); cellPadInit(7); cellPadGetData(0, &pad_data); if(pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_PAD_CTRL_TRIANGLE) { //override path, boot first executable in cores directory RARCH_LOG("Fallback - Will boot first executable in RetroArch cores/ directory.\n"); find_and_set_first_file(); } else { //normal executable loading path init_settings(); } cellPadEnd(); #ifdef HAVE_LOGGER logger_shutdown(); #endif for(unsigned int i = 0; i < sizeof(spawn_data); ++i) spawn_data[i] = i & 0xff; sprintf(spawn_data_size, "%d", 256); const char * const spawn_argv[] = { spawn_data_size, "test argv for", "sceNpDrmProcessExitSpawn2()", NULL }; ret = sceNpDrmProcessExitSpawn2(k_licensee, libretro_path, (const char** const)spawn_argv, NULL, (sys_addr_t)spawn_data, 256, 1000, SYS_PROCESS_PRIMARY_STACK_SIZE_1M); RARCH_LOG("Launch libretro core: [%s] (return code: %x]).\n", libretro_path, ret); if(ret < 0) { RARCH_LOG("Executable file is not of NPDRM type, trying another approach to boot it...\n"); sys_game_process_exitspawn2(libretro_path, NULL, NULL, NULL, 0, 1000, SYS_PROCESS_PRIMARY_STACK_SIZE_1M); } sceNpTerm(); sys_net_finalize_network(); cellSysmoduleUnloadModule(CELL_SYSMODULE_SYSUTIL_NP); cellSysmoduleUnloadModule(CELL_SYSMODULE_NET); cellSysmoduleUnloadModule(CELL_SYSMODULE_SYSUTIL_GAME); cellSysmoduleLoadModule(CELL_SYSMODULE_FS); cellSysmoduleLoadModule(CELL_SYSMODULE_IO); #endif return 1; }
int CALLBACK WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CommandLine, int ShowCode) { Win32LoadXInput(); WNDCLASSA WindowClass = {}; Win32ResizeDIBSection(&GlobalBackbuffer, 1280, 720); WindowClass.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC; WindowClass.lpfnWndProc = Win32MainWindowCallback; WindowClass.hInstance = Instance; // WindowClass.hIcon; WindowClass.lpszClassName = "HandmadeHeroWindowClass"; if(RegisterClassA(&WindowClass)) { HWND Window = CreateWindowExA( 0, WindowClass.lpszClassName, "Handmade Hero", WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, Instance, 0); if(Window) { // NOTE(casey): Since we specified CS_OWNDC, we can just // get one device context and use it forever because we // are not sharing it with anyone. HDC DeviceContext = GetDC(Window); int XOffset = 0; int YOffset = 0; GlobalRunning = true; while(GlobalRunning) { MSG Message; while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) { if(Message.message == WM_QUIT) { GlobalRunning = false; } TranslateMessage(&Message); DispatchMessageA(&Message); } // TODO(casey): Should we poll this more frequently for (DWORD ControllerIndex = 0; ControllerIndex < XUSER_MAX_COUNT; ++ControllerIndex) { XINPUT_STATE ControllerState; if(XInputGetState(ControllerIndex, &ControllerState) == ERROR_SUCCESS) { // NOTE(casey): This controller is plugged in // TODO(casey): See if ControllerState.dwPacketNumber increments too rapidly XINPUT_GAMEPAD *Pad = &ControllerState.Gamepad; bool Up = (Pad->wButtons & XINPUT_GAMEPAD_DPAD_UP); bool Down = (Pad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN); bool Left = (Pad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT); bool Right = (Pad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT); bool Start = (Pad->wButtons & XINPUT_GAMEPAD_START); bool Back = (Pad->wButtons & XINPUT_GAMEPAD_BACK); bool LeftShoulder = (Pad->wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER); bool RightShoulder = (Pad->wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER); bool AButton = (Pad->wButtons & XINPUT_GAMEPAD_A); bool BButton = (Pad->wButtons & XINPUT_GAMEPAD_B); bool XButton = (Pad->wButtons & XINPUT_GAMEPAD_X); bool YButton = (Pad->wButtons & XINPUT_GAMEPAD_Y); int16 StickX = Pad->sThumbLX; int16 StickY = Pad->sThumbLY; XOffset += StickX >> 12; YOffset += StickY >> 12; } else { // NOTE(casey): The controller is not available } } RenderWeirdGradient(&GlobalBackbuffer, XOffset, YOffset); win32_window_dimension Dimension = Win32GetWindowDimension(Window); Win32DisplayBufferInWindow(&GlobalBackbuffer, DeviceContext, Dimension.Width, Dimension.Height); }
bool X360Controller::update() { uint32_t connectedControllerIndex = 0; for (uint32_t gamePadIndex = 0; gamePadIndex < MAX_GAME_PAD_COUNT; gamePadIndex++) { Gamepad& gamepad = _inputState->_gamepads[connectedControllerIndex]; memset (&_currentInputState, 0, sizeof (XINPUT_STATE)); if (XInputGetState(gamePadIndex, &_currentInputState) == ERROR_SUCCESS) { gamepad._controllerLeftTriggerValue = (float)_currentInputState.Gamepad.bLeftTrigger / 256.0f; gamepad._controllerRightTriggerValue = (float)_currentInputState.Gamepad.bRightTrigger / 256.0f; gamepad._hatLeftTriggerValue = (float)((_currentInputState.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? 1 : 0); gamepad._hatRightTriggerValue = (float)((_currentInputState.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? 1 : 0); gamepad._controllerAButton = (float)((_currentInputState.Gamepad.wButtons & XINPUT_GAMEPAD_A) ? 1 : 0); gamepad._controllerBButton = (float)((_currentInputState.Gamepad.wButtons & XINPUT_GAMEPAD_B) ? 1 : 0); gamepad._controllerXButton = (float)((_currentInputState.Gamepad.wButtons & XINPUT_GAMEPAD_X) ? 1 : 0); gamepad._controllerYButton = (float)((_currentInputState.Gamepad.wButtons & XINPUT_GAMEPAD_Y) ? 1 : 0); float x = 0; float y = 0; if( (_currentInputState.Gamepad.sThumbRX < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && _currentInputState.Gamepad.sThumbRX > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) && (_currentInputState.Gamepad.sThumbRY < XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE && _currentInputState.Gamepad.sThumbRY > -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) ) { x = 0; y = 0; } else { x = (float)_currentInputState.Gamepad.sThumbRX / (float)MAX_THUMBSTICK; y = (float)_currentInputState.Gamepad.sThumbRY / (float)MAX_THUMBSTICK; } gamepad._rightThumbX = x; gamepad._rightThumbY = y; // Global states if( (_currentInputState.Gamepad.sThumbLX < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && _currentInputState.Gamepad.sThumbLX > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) && (_currentInputState.Gamepad.sThumbLY < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE && _currentInputState.Gamepad.sThumbLY > -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) ) { x = 0; y = 0; } else { x = (float)_currentInputState.Gamepad.sThumbLX / (float)MAX_THUMBSTICK; y = (float)_currentInputState.Gamepad.sThumbLY / (float)MAX_THUMBSTICK; } gamepad._leftThumbX = x; gamepad._leftThumbY = y; /* _inputState->rotation = Ibl::Vector3f (-(y* 3), (x* 3), 0); */ connectedControllerIndex++; } else { gamepad.makeIdentity(); } } return true; }
//retun false if failed bool InputHandler::getState(int padNum,XINPUT_STATE& state) { if(XInputGetState(padNum,&state)==ERROR_SUCCESS) { return true; } return false; }
int CALLBACK WinMain(HINSTANCE Instance, HINSTANCE PrevInstance, LPSTR CommandLine, int ShowCode) { Wind32LoadXInput(); WNDCLASSA WindowClass = {}; Win32ResizeDIBSection(&GlobalBackbuffer, 1280, 720); WindowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; WindowClass.lpfnWndProc = Win32MainWindowCallback; WindowClass.hInstance = Instance; // WindowClass.hIcon; WindowClass.lpszClassName = "HandmadeHeroWindowClass"; if(RegisterClassA(&WindowClass)) { HWND Window = CreateWindowExA( 0, WindowClass.lpszClassName, "Handmade Hero", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, Instance, 0); if(Window) { // NOTE: Since we specified CS_OWNDC, we can just // create it once since we aren't sharing it with anybody HDC DeviceContext = GetDC(Window); // NOTE: Graphics test int XOffset = 0; int YOffset = 0; // NOTE: Sound test int SamplesPerSecond = 48000; int ToneHz = 256; int16 ToneVolume = 3000; uint32 RunningSampleIndex = 0; int SquareWavePeriod = SamplesPerSecond / ToneHz; int HalfSquareWavePeriod = SquareWavePeriod / 2; int BytesPerSample = sizeof(int16) * 2; int SecondaryBufferSize = SamplesPerSecond * BytesPerSample; Win32InitDSound(Window, SamplesPerSecond, SecondaryBufferSize); GlobalSecondaryBuffer->Play(0, 0, DSBPLAY_LOOPING); GlobalRunning = true; while(GlobalRunning) { MSG Message; while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) { if(Message.message == WM_QUIT) { GlobalRunning = false; } TranslateMessage(&Message); DispatchMessageA(&Message); } // TODO: Should we poll this more frequently? for(DWORD ControllerIndex = 0; ControllerIndex < XUSER_MAX_COUNT; ++ControllerIndex) { XINPUT_STATE ControllerState; if(XInputGetState(ControllerIndex, &ControllerState) == ERROR_SUCCESS) { // NOTE: This controller is plugged in // TODO: See if controllerstate.dwpacketnumber increments too rapidly XINPUT_GAMEPAD *Pad = &ControllerState.Gamepad; bool Up = (Pad->wButtons & XINPUT_GAMEPAD_DPAD_UP); bool Down = (Pad->wButtons & XINPUT_GAMEPAD_DPAD_DOWN); bool Left = (Pad->wButtons & XINPUT_GAMEPAD_DPAD_LEFT); bool Right = (Pad->wButtons & XINPUT_GAMEPAD_DPAD_RIGHT); bool Start = (Pad->wButtons & XINPUT_GAMEPAD_START); bool Back = (Pad->wButtons & XINPUT_GAMEPAD_BACK); bool LeftShoulder = (Pad->wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER); bool RightShoulder = (Pad->wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER); bool AButton = (Pad->wButtons & XINPUT_GAMEPAD_A); bool BButton = (Pad->wButtons & XINPUT_GAMEPAD_B); bool XButton = (Pad->wButtons & XINPUT_GAMEPAD_X); bool YButton = (Pad->wButtons & XINPUT_GAMEPAD_Y); int16 stickX = Pad->sThumbLX; int16 stickY = Pad->sThumbLY; XOffset += stickX >> 12; YOffset += stickY >> 12; } else { // NOTE: The controller is not available } } RenderWeirdGradient(&GlobalBackbuffer, XOffset, YOffset); // NOTE DirectSound output test DWORD PlayCursor; DWORD WriteCursor; if(SUCCEEDED(GlobalSecondaryBuffer->GetCurrentPosition(&PlayCursor, &WriteCursor))) { DWORD ByteToLock = RunningSampleIndex * BytesPerSample % SecondaryBufferSize; DWORD BytesToWrite; if(ByteToLock == PlayCursor) { BytesToWrite = SecondaryBufferSize; } else if(ByteToLock > PlayCursor) { BytesToWrite = SecondaryBufferSize - ByteToLock; BytesToWrite += PlayCursor; } else { BytesToWrite = PlayCursor - ByteToLock; } // TODO: More strenuous test VOID *Region1; DWORD Region1Size; VOID *Region2; DWORD Region2Size; if(SUCCEEDED(GlobalSecondaryBuffer->Lock(ByteToLock, BytesToWrite, &Region1, &Region1Size, &Region2, &Region2Size, 0))) { // TODO: assert that Region1SizeRegion2Size is valid int16 *SampleOut = (int16 *)Region1; DWORD Region1SampleCount = Region1Size / BytesPerSample; for(DWORD SampleIndex = 0; SampleIndex < Region1SampleCount; ++SampleIndex) { int16 SampleValue = ((RunningSampleIndex++ / HalfSquareWavePeriod) % 2) ? ToneVolume : -ToneVolume; *SampleOut++ = SampleValue; *SampleOut++ = SampleValue; } DWORD Region2SampleCount = Region2Size / BytesPerSample; SampleOut = (int16 *)Region2; for(DWORD SampleIndex = 0; SampleIndex < Region2SampleCount; ++SampleIndex) { int16 SampleValue = ((RunningSampleIndex++ / HalfSquareWavePeriod) % 2) ? ToneVolume : -ToneVolume; *SampleOut++ = SampleValue; *SampleOut++ = SampleValue; } GlobalSecondaryBuffer->Unlock(Region1, Region1Size, Region2, Region2Size); } } win32_window_dimension Dimension = Win32GetWindowDimension(Window); Win32DisplayBufferInWindow(&GlobalBackbuffer, DeviceContext, Dimension.Width, Dimension.Height); }
static void xinput_input_poll(void *data) { (void)data; unsigned int dwInsertions, dwRemovals; XGetDeviceChanges(XDEVICE_TYPE_GAMEPAD, reinterpret_cast<PDWORD>(&dwInsertions), reinterpret_cast<PDWORD>(&dwRemovals)); pads_connected = 0; for (unsigned i = 0; i < 4; i++) { XINPUT_STATE state[4]; XINPUT_CAPABILITIES caps[4]; (void)caps; real_state[i] = 0; // handle removed devices bRemoved[i] = (dwRemovals & (1<<i)) ? true : false; if(bRemoved[i]) { // if the controller was removed after XGetDeviceChanges but before // XInputOpen, the device handle will be NULL if(gamepads[i]) XInputClose(gamepads[i]); gamepads[i] = NULL; } // handle inserted devices bInserted[i] = (dwInsertions & (1<<i)) ? true : false; if(bInserted[i]) { XINPUT_POLLING_PARAMETERS m_pollingParameters; m_pollingParameters.fAutoPoll = TRUE; m_pollingParameters.fInterruptOut = TRUE; m_pollingParameters.bInputInterval = 8; m_pollingParameters.bOutputInterval = 8; gamepads[i] = XInputOpen(XDEVICE_TYPE_GAMEPAD, i, XDEVICE_NO_SLOT, NULL); } if (gamepads[i]) { unsigned long retval; // if the controller is removed after XGetDeviceChanges but before // XInputOpen, the device handle will be NULL retval = XInputGetState(gamepads[i], &state[i]); if(retval == ERROR_SUCCESS) { pads_connected++; real_state[i] |= ((state[i].Gamepad.bAnalogButtons[XINPUT_GAMEPAD_B]) ? XINPUT1_GAMEPAD_B : 0); real_state[i] |= ((state[i].Gamepad.bAnalogButtons[XINPUT_GAMEPAD_A]) ? XINPUT1_GAMEPAD_A : 0); real_state[i] |= ((state[i].Gamepad.bAnalogButtons[XINPUT_GAMEPAD_Y]) ? XINPUT1_GAMEPAD_Y : 0); real_state[i] |= ((state[i].Gamepad.bAnalogButtons[XINPUT_GAMEPAD_X]) ? XINPUT1_GAMEPAD_X : 0); real_state[i] |= ((state[i].Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) || (state[i].Gamepad.sThumbLX < -DEADZONE) ? XINPUT1_GAMEPAD_DPAD_LEFT : 0); real_state[i] |= ((state[i].Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) || (state[i].Gamepad.sThumbLX > DEADZONE) ? XINPUT1_GAMEPAD_DPAD_RIGHT : 0); real_state[i] |= ((state[i].Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) || (state[i].Gamepad.sThumbLY > DEADZONE) ? XINPUT1_GAMEPAD_DPAD_UP : 0); real_state[i] |= ((state[i].Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN)|| (state[i].Gamepad.sThumbLY < -DEADZONE) ? XINPUT1_GAMEPAD_DPAD_DOWN : 0); real_state[i] |= ((state[i].Gamepad.wButtons & XINPUT_GAMEPAD_START) ? XINPUT1_GAMEPAD_START : 0); real_state[i] |= ((state[i].Gamepad.wButtons & XINPUT_GAMEPAD_BACK) ? XINPUT1_GAMEPAD_BACK : 0); real_state[i] |= ((state[i].Gamepad.bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER]) ? XINPUT1_GAMEPAD_LEFT_TRIGGER : 0); real_state[i] |= ((state[i].Gamepad.bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER]) ? XINPUT1_GAMEPAD_RIGHT_TRIGGER : 0); real_state[i] |= ((state[i].Gamepad.bAnalogButtons[XINPUT_GAMEPAD_WHITE]) ? XINPUT1_GAMEPAD_WHITE : 0); real_state[i] |= ((state[i].Gamepad.bAnalogButtons[XINPUT_GAMEPAD_BLACK]) ? XINPUT1_GAMEPAD_BLACK : 0); real_state[i] |= ((state[i].Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) ? XINPUT1_GAMEPAD_LEFT_THUMB : 0); real_state[i] |= ((state[i].Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) ? XINPUT1_GAMEPAD_RIGHT_THUMB : 0); } } } }
void GameWindow::DetectControllers() { //Cache the last controller detection time stamp m_LastControllerDetection = ServiceLocator::GetPlatformLayer()->GetTicks(); //Local variables used in the for loop below bool duplicate = false; XINPUT_STATE state; //Cycle through available controller ports for(unsigned int controllerPort = 0; controllerPort < XUSER_MAX_COUNT; controllerPort++) { //Zero out the memory of the state struct ZeroMemory(&state, sizeof(XINPUT_STATE)); //Can we get the state of a controller on this port if(XInputGetState(controllerPort, &state) == ERROR_SUCCESS) { duplicate = false; for(unsigned int i = 0; i < GameDev2D::ServiceLocator::GetInputManager()->GetNumberOfControllers(); i++) { //Check if this port already has a registered contoller if(GameDev2D::ServiceLocator::GetInputManager()->GetControllerForIndex(i) != nullptr) { if((uintptr_t)GameDev2D::ServiceLocator::GetInputManager()->GetControllerForIndex(i)->GetDevice() == (uintptr_t)controllerPort) { duplicate = true; break; } } } //If its a duplicate, continue to the next port if(duplicate == true) { continue; } //Used to get the vendor id and the product ids int vendorId = -1; int productId = -1; //Determine the vendor id and product id JOYCAPS caps; if(joyGetDevCaps(controllerPort, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR) { vendorId = caps.wMid; productId = caps.wPid; } //Create a ControllerData object, pass it the device, vendor id and product id GameDev2D::ControllerData* controllerData = new GameDev2D::ControllerData((void*)controllerPort, vendorId, productId); assert(controllerData != nullptr); //Pass the controller data onto the input manager, it will GameDev2D::ControllerGeneric* controller = GameDev2D::ServiceLocator::GetInputManager()->HandleMatchedController(controllerData); assert(controller != nullptr); //Set the controller data's initial state controllerData->SetStateInfo(state); //Add all the controller buttons controllerData->AddButton(XINPUT_GAMEPAD_DPAD_UP); controllerData->AddButton(XINPUT_GAMEPAD_DPAD_DOWN); controllerData->AddButton(XINPUT_GAMEPAD_DPAD_LEFT); controllerData->AddButton(XINPUT_GAMEPAD_DPAD_RIGHT); controllerData->AddButton(XINPUT_GAMEPAD_START); controllerData->AddButton(XINPUT_GAMEPAD_BACK); controllerData->AddButton(XINPUT_GAMEPAD_LEFT_THUMB); controllerData->AddButton(XINPUT_GAMEPAD_RIGHT_THUMB); controllerData->AddButton(XINPUT_GAMEPAD_LEFT_SHOULDER); controllerData->AddButton(XINPUT_GAMEPAD_RIGHT_SHOULDER); controllerData->AddButton(XINPUT_GAMEPAD_A); controllerData->AddButton(XINPUT_GAMEPAD_B); controllerData->AddButton(XINPUT_GAMEPAD_X); controllerData->AddButton(XINPUT_GAMEPAD_Y); //Add the analog trugger controllerData->AddAnalog(VK_PAD_LTRIGGER, 0, 255, 0); controllerData->AddAnalog(VK_PAD_RTRIGGER, 0, 255, 0); //Add the left analog stick controllerData->AddAnalog(VK_PAD_LTHUMB_UP, -32768, 32767, 0); controllerData->AddAnalog(VK_PAD_LTHUMB_LEFT, -32768, 32767, 0); //Add the right analog stick controllerData->AddAnalog(VK_PAD_RTHUMB_UP, -32768, 32767, 0); controllerData->AddAnalog(VK_PAD_RTHUMB_LEFT, -32768, 32767, 0); } } }