//----------------------------------------------------------------------------- // Name: RegisterRemoteTalker // Desc: Registers a new remote talker //----------------------------------------------------------------------------- HRESULT CXHVVoiceManager::RegisterRemoteTalker( XUID xuidRemoteTalker ) { // XHV allows you to call RegisterRemoteTalker more than once with the // same XUID. For convenience, we'll emulate that behavior if( SUCCEEDED( FindPlayerVoiceInfo( xuidRemoteTalker, NULL ) ) ) return S_OK; // Verify we're within our limits assert( m_dwNumRemoteTalkers < m_dwMaxRemoteTalkers ); // Register the remote talker with XHV HRESULT hr; hr = m_pXHVEngine->RegisterRemoteTalker( xuidRemoteTalker ); if( FAILED( hr ) ) return hr; // Set up a new PlayerVoiceInfo struct for the player PlayerVoiceInfo* pRemoteTalker = &m_pPlayerVoiceInfo[ m_dwNumRemoteTalkers ]; pRemoteTalker->xuid = xuidRemoteTalker; for( DWORD i = 0; i < XGetPortCount(); i++ ) { pRemoteTalker->bMuted[ i ] = FALSE; pRemoteTalker->bRemoteMuted[ i ] = FALSE; pRemoteTalker->priority[ i ] = XHV_PLAYBACK_PRIORITY_NEVER; } ++m_dwNumRemoteTalkers; return S_OK; }
//----------------------------------------------------------------------------- // 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; }
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); } } }
//----------------------------------------------------------------------------- // 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; }