Skip to content

Instantly share code, notes, and snippets.

@CookiePLMonster
Created September 9, 2023 18:05
Show Gist options
  • Save CookiePLMonster/250787d4bb8c0a831e3b9d7840cadad4 to your computer and use it in GitHub Desktop.
Save CookiePLMonster/250787d4bb8c0a831e3b9d7840cadad4 to your computer and use it in GitHub Desktop.
DirectInput 3-7 test app, for testing dinputto8
#include <Windows.h>
#include <iomanip>
#include <iostream>
#pragma comment(lib, "dxguid.lib")
#define DIRECTINPUT_VERSION 0x700
#include <dinput.h>
#include <wil/com.h>
#include <wil/win32_helpers.h>
static BOOL WINAPI DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
{
GUID* pGuid = static_cast<GUID*>(pvRef);
*pGuid = lpddi->guidInstance;
std::wcout << "Found device: " << lpddi->tszProductName << std::endl << std::endl;
return DIENUM_STOP;
}
BOOL WINAPI DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef)
{
uint32_t* pEnumCount = static_cast<uint32_t*>(pvRef);
std::wcout << (*pEnumCount)++ << ". dwOfs=" << static_cast<int32_t>(lpddoi->dwOfs) << ", tszName=" << lpddoi->tszName << ", dwType=" << std::hex << lpddoi->dwType << std::dec << std::endl;
return DIENUM_CONTINUE;
}
HRESULT TestVersion(decltype(DirectInputCreateW) func, DWORD version)
{
std::cout << "Testing version 0x" << std::hex << version << ":" << std::endl;
wil::com_ptr_nothrow<IDirectInput> dinput;
RETURN_IF_FAILED(func(GetModuleHandle(nullptr), version, dinput.put(), nullptr));
// 2. Enumerate DirectInput devices and get the very first gamepad
GUID instanceGuid(GUID_NULL);
RETURN_IF_FAILED(dinput->EnumDevices(DIDEVTYPE_JOYSTICK, DIEnumDevicesCallback, &instanceGuid, DIEDFL_ATTACHEDONLY));
if (instanceGuid == GUID_NULL)
{
return E_FAIL;
}
// Create a DirectInput device
wil::com_ptr_nothrow<IDirectInputDevice> dinputDevice;
RETURN_IF_FAILED(dinput->CreateDevice(instanceGuid, dinputDevice.put(), nullptr));
std::cout << "Enumerating objects WITHOUT data format set" << std::endl;
{
uint32_t enumCount = 1;
dinputDevice->EnumObjects(DIEnumDeviceObjectsCallback, &enumCount, DIDFT_ALL);
std::cout << std::endl;
}
{
DIOBJECTDATAFORMAT joystick[] = {
{ &GUID_ZAxis, 0, DIDFT_AXIS|DIDFT_ANYINSTANCE, 0 },
{ &GUID_XAxis, 12, DIDFT_AXIS|DIDFT_ANYINSTANCE, 0 },
{ nullptr, DIJOFS_BUTTON(5), DIDFT_BUTTON|DIDFT_ANYINSTANCE, 0 },
{ nullptr, DIJOFS_BUTTON(7), DIDFT_BUTTON|DIDFT_ANYINSTANCE, 0 },
{ nullptr, DIJOFS_BUTTON(3), DIDFT_BUTTON|DIDFT_ANYINSTANCE, 0 },
};
const DIDATAFORMAT format = {
sizeof(DIDATAFORMAT),
sizeof(DIOBJECTDATAFORMAT),
DIDF_ABSAXIS,
sizeof(DIJOYSTATE),
std::size(joystick),
joystick
};
RETURN_IF_FAILED(dinputDevice->SetDataFormat(&format));
}
std::cout << "Enumerating objects WITH data format set" << std::endl;
{
uint32_t enumCount = 1;
dinputDevice->EnumObjects(DIEnumDeviceObjectsCallback, &enumCount, DIDFT_ALL);
std::cout << std::endl;
}
return S_OK;
}
int main(int argc, char* argv[])
{
// 1. Create a DirectInput object
wil::unique_hmodule dinputLib(LoadLibrary(TEXT("dinput")));
RETURN_LAST_ERROR_IF_NULL(dinputLib);
auto pDirectInputCreate = GetProcAddressByFunctionDeclaration(dinputLib.get(), DirectInputCreateW);
RETURN_LAST_ERROR_IF_NULL(pDirectInputCreate);
for (DWORD ver : { 0x300, 0x500, 0x50a, 0x5b2, 0x602, 0x61A, 0x700 })
{
TestVersion(pDirectInputCreate, ver);
std::cout << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment