00001 /************************************************************************************ 00002 00003 Filename : Win32_Gamepad.cpp 00004 Content : Win32 implementation of Platform app infrastructure 00005 Created : May 6, 2013 00006 Authors : Lee Cooper 00007 00008 Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved. 00009 00010 Licensed under the Apache License, Version 2.0 (the "License"); 00011 you may not use this file except in compliance with the License. 00012 You may obtain a copy of the License at 00013 00014 http://www.apache.org/licenses/LICENSE-2.0 00015 00016 Unless required by applicable law or agreed to in writing, software 00017 distributed under the License is distributed on an "AS IS" BASIS, 00018 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 See the License for the specific language governing permissions and 00020 limitations under the License. 00021 00022 ************************************************************************************/ 00023 00024 #include "Win32_Gamepad.h" 00025 00026 namespace OVR { namespace Platform { namespace Win32 { 00027 00028 GamepadManager::GamepadManager() 00029 { 00030 hXInputModule = ::LoadLibraryA("Xinput9_1_0.dll"); 00031 if (hXInputModule) 00032 { 00033 pXInputGetState = (PFn_XInputGetState) 00034 ::GetProcAddress(hXInputModule, "XInputGetState"); 00035 } 00036 } 00037 00038 GamepadManager::~GamepadManager() 00039 { 00040 if (hXInputModule) 00041 ::FreeLibrary(hXInputModule); 00042 } 00043 00044 static inline float GamepadStick(short in) 00045 { 00046 float v; 00047 if (abs(in) < 9000) 00048 return 0; 00049 else if (in > 9000) 00050 v = (float) in - 9000; 00051 else 00052 v = (float) in + 9000; 00053 return v / (32767 - 9000); 00054 } 00055 00056 static inline float GamepadTrigger(BYTE in) 00057 { 00058 if (in < 30) 00059 return 0; 00060 else 00061 return float(in-30) / 225; 00062 } 00063 00064 UInt32 GamepadManager::GetGamepadCount() 00065 { 00066 return 1; 00067 } 00068 00069 bool GamepadManager::GetGamepadState(UInt32 index, GamepadState* pState) 00070 { 00071 // For now we just support one gamepad. 00072 OVR_UNUSED(index); 00073 00074 if (pXInputGetState) 00075 { 00076 XINPUT_STATE xis; 00077 00078 if (pXInputGetState(0, &xis)) 00079 return false; 00080 00081 if (xis.dwPacketNumber == LastPadPacketNo) 00082 return false; 00083 00084 // State changed. 00085 pState->Buttons = xis.Gamepad.wButtons; // Currently matches Xinput 00086 pState->LT = GamepadTrigger(xis.Gamepad.bLeftTrigger); 00087 pState->RT = GamepadTrigger(xis.Gamepad.bRightTrigger); 00088 pState->LX = GamepadStick(xis.Gamepad.sThumbLX); 00089 pState->LY = GamepadStick(xis.Gamepad.sThumbLY); 00090 pState->RX = GamepadStick(xis.Gamepad.sThumbRX); 00091 pState->RY = GamepadStick(xis.Gamepad.sThumbRY); 00092 00093 LastPadPacketNo = xis.dwPacketNumber; 00094 00095 return true; 00096 } 00097 00098 return false; 00099 } 00100 00101 }}} // OVR::Platform::Win32