Skip to content

Instantly share code, notes, and snippets.

@pinumbernumber
Created August 2, 2014 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinumbernumber/93c6d6387845065dc076 to your computer and use it in GitHub Desktop.
Save pinumbernumber/93c6d6387845065dc076 to your computer and use it in GitHub Desktop.
Reading the 360 pad's Guide button
// Part of button_on_360_guide by https://github.com/pinumbernumber
// Distributed under the highly permissive WTFPL, see COPYING.txt
// Untested, not even compiled. I don't have a Windows machine handy.
using namespace std;
#include <windows.h>
// We don't need all of XInput so no need to require the official headers.
// Just redefine stuff here.
typedef struct
{
WORD wButtons;
BYTE bLeftTrigger;
BYTE bRightTrigger;
SHORT sThumbLX;
SHORT sThumbLY;
SHORT xThumbRX;
SHORT xThumbRY;
} XINPUT_GAMEPAD;
typedef struct
{
DWORD dwPacketNumber;
XINPUT_GAMEPAD Gamepad;
} XINPUT_STATE;
#define ERROR_DEVICE_NOT_CONNECTED 1167
#define XINPUT_GAMEPAD_GUIDE 0x0400
// For cleaner syntax, define XInputGetStateEx_t as a function
// returning DWORD and accepting a DWORD and an XINPUT_STATE*
typedef DWORD (__stdcall *XInputGetStateEx_t) (DWORD, XINPUT_STATE*);
int main()
{
// Try to load 1.4 (Windows 8)
HINSTANCE xinputDll = LoadLibrary("xinput1_4.dll");
if (!xinputDll)
{
// Try to load 1.3 (Windows <8)
xinputDll = LoadLibrary("xinput1_3.dll");
if (!xinputDll)
{
// Still nothing? You should probably exit with a fatal error here.
}
}
// Load the actual function. C++ requires an ugly cast here, what can you do...
// The function isn't exported with a name but we can still import it by ordinal
// using a strange cast of an integer to a string.
XInputGetStateEx_t XInputGetStateEx = (XInputGetStateEx_t) GetProcAddress(xinputDll, (LPCSTR)100);
// Only consider one player for now.
XINPUT_STATE playerOneState;
ZeroMemory(&playerOneState, sizeof(XINPUT_STATE)); // This might or mightn't be neccesary. No harm in it anyway.
// Infinite loop. You'd want a Sleep() command or something somehwere in it to avoid
// consuming all CPU time.
while(1)
{
// 0 as in player one. Do it in a conditional since the return value
// indicates connectivity.
if (XInputGetStateEx(0, &playerOneState) == ERROR_DEVICE_NOT_CONNECTED)
{
// The pad is disconnected.
}
else
{
// Pad's connected. Is guide down?
if (playerOneState.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE)
{
// Yep!
// Only shows whether it's down right now, of course, not whether it
// was just pressed. I kept variables indicating the previous state
// and compared with them.
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment