//----------------------------------------------------------------------------- // Name: XBInput_CreateGamepads() // Desc: Creates the gamepad devices //----------------------------------------------------------------------------- HRESULT XBInput_CreateGamepads( XBGAMEPAD** ppGamepads ) { // Get a mask of all currently available devices DWORD dwDeviceMask = XGetDevices( XDEVICE_TYPE_GAMEPAD ); // Open the devices for( DWORD i=0; i < XGetPortCount(); i++ ) { ZeroMemory( &g_InputStates[i], sizeof(XINPUT_STATE) ); ZeroMemory( &g_Gamepads[i], sizeof(XBGAMEPAD) ); if( dwDeviceMask & (1<<i) ) { // Get a handle to the device g_Gamepads[i].hDevice = XInputOpen( XDEVICE_TYPE_GAMEPAD, i, XDEVICE_NO_SLOT, NULL ); // Store capabilities of the device XInputGetCapabilities( g_Gamepads[i].hDevice, &g_Gamepads[i].caps ); } } // Created devices are kept global, but for those who prefer member // variables, they can get a pointer to the gamepads returned. if( ppGamepads ) (*ppGamepads) = g_Gamepads; return S_OK; }
static void xinput_input_poll(void *data) { (void)data; pads_connected = 0; for (unsigned i = 0; i < 4; i++) { XINPUT_STATE state_tmp; unsigned long retval; gamepads[i] = XInputOpen(XDEVICE_TYPE_GAMEPAD, i, XDEVICE_NO_SLOT, NULL); if(gamepads[i] != NULL) { retval = XInputGetState(gamepads[i], &state_tmp); pads_connected += (retval != ERROR_SUCCESS) ? 0 : 1; state[i] = state_tmp.Gamepad.wButtons; state[i] |= ((state_tmp.Gamepad.sThumbLX < -DEADZONE)) << 16; state[i] |= ((state_tmp.Gamepad.sThumbLX > DEADZONE)) << 17; state[i] |= ((state_tmp.Gamepad.sThumbLY > DEADZONE)) << 18; state[i] |= ((state_tmp.Gamepad.sThumbLY < -DEADZONE)) << 19; state[i] |= ((state_tmp.Gamepad.sThumbRX < -DEADZONE)) << 20; state[i] |= ((state_tmp.Gamepad.sThumbRX > DEADZONE)) << 21; state[i] |= ((state_tmp.Gamepad.sThumbRY > DEADZONE)) << 22; state[i] |= ((state_tmp.Gamepad.sThumbRY < -DEADZONE)) << 23; state[i] |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER] > 128 ? 1 : 0)) << 24; state[i] |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER] > 128 ? 1 : 0)) << 25; } } }
void CXBoxMouse::Initialize(void *appData) { m_dwMousePort = XGetDevices( XDEVICE_TYPE_DEBUG_MOUSE ); // See if a mouse is attached and get a handle to it, if it is. for ( DWORD i = 0; i < XGetPortCount()*2; i++ ) { if ( ( m_hMouseDevice[i] == NULL ) && ( m_dwMousePort & anMouseBitmapTable[i] ) ) { // Get a handle to the device if (i < XGetPortCount()) { m_hMouseDevice[i] = XInputOpen( XDEVICE_TYPE_DEBUG_MOUSE, i, XDEVICE_NO_SLOT, NULL ); } else { m_hMouseDevice[i] = XInputOpen( XDEVICE_TYPE_DEBUG_MOUSE, i - XGetPortCount(), XDEVICE_BOTTOM_SLOT, NULL ); } CLog::Log(LOGINFO, "Found mouse on port %i", i); } } }
// Process all the insertions and removals, updating handles and such void IN_ProcessChanges(DWORD dwInsert, DWORD dwRemove) { for(int port = 0; port < IN_MAX_CONTROLLERS; ++port) { // Close removals. if( ((1 << port) & dwRemove) && in_state->controllers[port].handle ) { XInputClose( in_state->controllers[port].handle ); in_state->controllers[port].handle = 0; IN_PadUnplugged(port); } // Open insertions. if( (1 << port) & dwInsert ) { in_state->controllers[port].handle = XInputOpen( XDEVICE_TYPE_GAMEPAD, port, XDEVICE_NO_SLOT, NULL ); IN_PadPlugged(port); } } return; }
static void xdk_joypad_poll(void) { unsigned port; #ifdef _XBOX1 unsigned int dwInsertions, dwRemovals; #endif #if defined(_XBOX1) 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; input_autoconfigure_disconnect(port, xdk_joypad.ident); } /* 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); xdk_joypad_autodetect_add(port); } 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) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_LEFT) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_RIGHT) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_UP) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_DOWN) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_START) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_START) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_SELECT) : 0); #if defined(_XBOX1) *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_B]) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_A) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_A]) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_B) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_Y]) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_X) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_X]) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_Y) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_LEFT_TRIGGER]) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_L) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_RIGHT_TRIGGER]) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_R) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_WHITE]) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_L2) : 0); *state_cur |= ((state_tmp.Gamepad.bAnalogButtons[XINPUT_GAMEPAD_BLACK]) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_R2) : 0); #elif defined(_XBOX360) *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_B) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_A) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_A) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_B) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_Y) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_X) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_X) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_Y) : 0); *state_cur |= ((state_tmp.Gamepad.bLeftTrigger > 128) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_L) : 0); *state_cur |= ((state_tmp.Gamepad.bRightTrigger > 128) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_R) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_L2) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_R2) : 0); #endif *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) ? (UINT64_C(1) << RETRO_DEVICE_ID_JOYPAD_L3) : 0); *state_cur |= ((state_tmp.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) ? (UINT64_C(1) << 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; } }
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 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); }
//----------------------------------------------------------------------------- // Name: XBInput_GetInput() // Desc: Processes input from the gamepads //----------------------------------------------------------------------------- VOID XBInput_GetInput( XBGAMEPAD* pGamepads ) { if( NULL == pGamepads ) pGamepads = g_Gamepads; // TCR 3-21 Controller Discovery // Get status about gamepad insertions and removals. Note that, in order to // not miss devices, we will check for removed device BEFORE checking for // insertions DWORD dwInsertions, dwRemovals; XGetDeviceChanges( XDEVICE_TYPE_GAMEPAD, &dwInsertions, &dwRemovals ); // Loop through all gamepads for( DWORD i=0; i < XGetPortCount(); i++ ) { // Handle removed devices. pGamepads[i].bRemoved = ( dwRemovals & (1<<i) ) ? TRUE : FALSE; if( pGamepads[i].bRemoved ) { // if the controller was removed after XGetDeviceChanges but before // XInputOpen, the device handle will be NULL if( pGamepads[i].hDevice ) XInputClose( pGamepads[i].hDevice ); pGamepads[i].hDevice = NULL; pGamepads[i].Feedback.Rumble.wLeftMotorSpeed = 0; pGamepads[i].Feedback.Rumble.wRightMotorSpeed = 0; } // Handle inserted devices pGamepads[i].bInserted = ( dwInsertions & (1<<i) ) ? TRUE : FALSE; if( pGamepads[i].bInserted ) { // TCR 1-14 Device Types pGamepads[i].hDevice = XInputOpen( XDEVICE_TYPE_GAMEPAD, i, XDEVICE_NO_SLOT, NULL ); // if the controller is removed after XGetDeviceChanges but before // XInputOpen, the device handle will be NULL if( pGamepads[i].hDevice ) XInputGetCapabilities( pGamepads[i].hDevice, &pGamepads[i].caps ); } // If we have a valid device, poll it's state and track button changes if( pGamepads[i].hDevice ) { // Read the input state XInputGetState( pGamepads[i].hDevice, &g_InputStates[i] ); // Copy gamepad to local structure memcpy( &pGamepads[i], &g_InputStates[i].Gamepad, sizeof(XINPUT_GAMEPAD) ); // Put Xbox device input for the gamepad into our custom format FLOAT fX1 = (pGamepads[i].sThumbLX+0.5f)/32767.5f; pGamepads[i].fX1 = ( fX1 >= 0.0f ? 1.0f : -1.0f ) * max( 0.0f, (fabsf(fX1)-XBINPUT_DEADZONE)/(1.0f-XBINPUT_DEADZONE) ); FLOAT fY1 = (pGamepads[i].sThumbLY+0.5f)/32767.5f; pGamepads[i].fY1 = ( fY1 >= 0.0f ? 1.0f : -1.0f ) * max( 0.0f, (fabsf(fY1)-XBINPUT_DEADZONE)/(1.0f-XBINPUT_DEADZONE) ); FLOAT fX2 = (pGamepads[i].sThumbRX+0.5f)/32767.5f; pGamepads[i].fX2 = ( fX2 >= 0.0f ? 1.0f : -1.0f ) * max( 0.0f, (fabsf(fX2)-XBINPUT_DEADZONE)/(1.0f-XBINPUT_DEADZONE) ); FLOAT fY2 = (pGamepads[i].sThumbRY+0.5f)/32767.5f; pGamepads[i].fY2 = ( fY2 >= 0.0f ? 1.0f : -1.0f ) * max( 0.0f, (fabsf(fY2)-XBINPUT_DEADZONE)/(1.0f-XBINPUT_DEADZONE) ); // Get the boolean buttons that have been pressed since the last // call. Each button is represented by one bit. pGamepads[i].wPressedButtons = ( pGamepads[i].wLastButtons ^ pGamepads[i].wButtons ) & pGamepads[i].wButtons; pGamepads[i].wLastButtons = pGamepads[i].wButtons; // Get the analog buttons that have been pressed or released since // the last call. for( DWORD b=0; b<8; b++ ) { // Turn the 8-bit polled value into a boolean value BOOL bPressed = ( pGamepads[i].bAnalogButtons[b] > XINPUT_GAMEPAD_MAX_CROSSTALK ); if( bPressed ) pGamepads[i].bPressedAnalogButtons[b] = !pGamepads[i].bLastAnalogButtons[b]; else pGamepads[i].bPressedAnalogButtons[b] = FALSE; // Store the current state for the next time pGamepads[i].bLastAnalogButtons[b] = bPressed; } } } }
bool CXBoxMouse::Update(MouseState &state) { // Check if mouse or mice were removed or attached. // We'll get the handle(s) next frame in the above code. DWORD dwNumInsertions, dwNumRemovals; if ( XGetDeviceChanges( XDEVICE_TYPE_DEBUG_MOUSE, &dwNumInsertions, &dwNumRemovals ) ) { // Loop through all ports and remove any mice that have been unplugged for ( DWORD i = 0; i < XGetPortCount()*2; i++ ) { if ( ( dwNumRemovals & anMouseBitmapTable[i]) && ( m_hMouseDevice[i] != NULL ) ) { XInputClose( m_hMouseDevice[i] ); m_hMouseDevice[i] = NULL; CLog::Log(LOGINFO, "Mouse removed from port %i", i); } } // Set the bits for all of the mice plugged in. // We get the handles on the next pass through. m_dwMousePort = dwNumInsertions; for ( DWORD i = 0; i < XGetPortCount()*2; i++ ) { if ( ( m_hMouseDevice[i] == NULL ) && ( m_dwMousePort & anMouseBitmapTable[i] ) ) { // Get a handle to the device if (i < XGetPortCount()) { m_hMouseDevice[i] = XInputOpen( XDEVICE_TYPE_DEBUG_MOUSE, i, XDEVICE_NO_SLOT, NULL ); } else { m_hMouseDevice[i] = XInputOpen( XDEVICE_TYPE_DEBUG_MOUSE, i - XGetPortCount(), XDEVICE_BOTTOM_SLOT, NULL ); } CLog::Log(LOGINFO, "Mouse inserted on port %i", i); } } } // Poll the mouse. DWORD bMouseMoved = 0; for ( DWORD i = 0; i < XGetPortCount()*2; i++ ) { if ( m_hMouseDevice[i] ) XInputGetState( m_hMouseDevice[i], &m_MouseState[i] ); if ( m_dwLastMousePacket[i] != m_MouseState[i].dwPacketNumber ) { bMouseMoved |= anMouseBitmapTable[i]; m_dwLastMousePacket[i] = m_MouseState[i].dwPacketNumber; } } // Check if we have an update... if (bMouseMoved) { // Yes - update our current state for ( DWORD i = 0; i < XGetPortCount()*2; i++ ) { if ( bMouseMoved & anMouseBitmapTable[i] ) { m_CurrentState = m_MouseState[i]; } } state.dx = m_CurrentState.DebugMouse.cMickeysX; state.dy = m_CurrentState.DebugMouse.cMickeysY; state.x += state.dx; state.y += state.dy; state.dz = m_CurrentState.DebugMouse.cWheel; state.button[MOUSE_LEFT_BUTTON] = (m_CurrentState.DebugMouse.bButtons & XINPUT_DEBUG_MOUSE_LEFT_BUTTON) != 0; state.button[MOUSE_RIGHT_BUTTON] = (m_CurrentState.DebugMouse.bButtons & XINPUT_DEBUG_MOUSE_RIGHT_BUTTON) != 0; state.button[MOUSE_MIDDLE_BUTTON] = (m_CurrentState.DebugMouse.bButtons & XINPUT_DEBUG_MOUSE_MIDDLE_BUTTON) != 0; state.button[MOUSE_EXTRA_BUTTON1] = (m_CurrentState.DebugMouse.bButtons & XINPUT_DEBUG_MOUSE_XBUTTON1) != 0; state.button[MOUSE_EXTRA_BUTTON2] = (m_CurrentState.DebugMouse.bButtons & XINPUT_DEBUG_MOUSE_XBUTTON2) != 0; return true; } return false; }
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); } } } }