Skip to content

Instantly share code, notes, and snippets.

@Bigpet
Last active April 30, 2024 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Bigpet/1c115bf3694fcfe31996ca3dbf34c424 to your computer and use it in GitHub Desktop.
Save Bigpet/1c115bf3694fcfe31996ca3dbf34c424 to your computer and use it in GitHub Desktop.
Reverse engineered sources to this weird Konami PS3 Dance-Mat Input converter
// DDR_Fix_reverse_engineer.cpp
// Reverse engineered code from the rawinput_ps3_ddr.exe floating around on the net
//
#include <iostream>
#include "Windows.h"
const int ARR_SZ = 16;
DWORD g_PreviousPadState[ARR_SZ];
void UsrSendKey(int key, bool bDownState)
{
UINT numInserted;
const char* lpState;
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wVk = 0;
input.ki.wScan =(WORD)key;
if (bDownState == false) {
input.ki.dwFlags = KEYEVENTF_KEYUP;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
numInserted = SendInput(1, &input, 0x1c);
lpState = "up";
}
else {
input.ki.dwFlags = KEYEVENTF_KEYUP;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
numInserted = SendInput(1, &input, 0x1c);
lpState = "down";
}
printf("SendInput: k=%d %s r=%d\n", key, lpState, numInserted);
return;
}
LRESULT CALLBACK WindowProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
LRESULT ret;
HRAWINPUT hRawInput = (HRAWINPUT)lParam;
RAWINPUTHEADER pRawHeader;
unsigned uLen;
size_t size;
char* currentPadState = nullptr;
if (uMsg == WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
if (uMsg == WM_INPUT)
{
uLen = sizeof(pRawHeader);
UINT uBytesCopied = GetRawInputData(hRawInput, RID_HEADER, &pRawHeader, &uLen, sizeof(RAWINPUTHEADER));
if (uBytesCopied != UINT(-1))
{
//get required length
GetRawInputData(hRawInput, RID_INPUT, (LPVOID)0x0, &uLen, sizeof(RAWINPUTHEADER));
currentPadState = new char[uLen];
RAWINPUT* inp = (RAWINPUT*)currentPadState;
//fetch data
GetRawInputData(hRawInput, RID_INPUT, currentPadState, &uLen, sizeof(RAWINPUTHEADER));
if (((char*)g_PreviousPadState)[9] != inp->data.hid.bRawData[9]) {
UsrSendKey(0x41e, /*currentPadState[0x21]*/inp->data.hid.bRawData[9] != '\0');//1e = A
}
if (((char*)g_PreviousPadState)[8] != inp->data.hid.bRawData[8]) {
UsrSendKey(0x430, /*currentPadState[0x20]*/inp->data.hid.bRawData[8] != '\0');//30 = B
}
if (((char*)g_PreviousPadState)[10] != inp->data.hid.bRawData[10]) {
UsrSendKey(0x42e, /*currentPadState[0x22]*/inp->data.hid.bRawData[10] != '\0');//2e = C
}
if (((char*)g_PreviousPadState)[11] != inp->data.hid.bRawData[11]) {
UsrSendKey(0x420, /*currentPadState[0x23]*/inp->data.hid.bRawData[11] != '\0');//20 = D
}
UINT numBytes = inp->data.hid.dwSizeHid;
DWORD* startStateData = (DWORD*)(&inp->data.hid.bRawData);
memcpy(g_PreviousPadState, startStateData, numBytes);
delete[] currentPadState;
return 0;
}
fwrite("error getting RID_HEADER\n", 1, 0x19, stderr);
ret = -1;
}
else
{
ret = DefWindowProcA(hwnd,uMsg,wParam, lParam);;
}
return ret;
}
BOOL RegisterRawInputDevice(HWND hWnd)
{
RAWINPUTDEVICE inpDevice;
inpDevice.hwndTarget = hWnd;
inpDevice.usUsagePage = 1;
inpDevice.usUsage = 5;
inpDevice.dwFlags = RIDEV_INPUTSINK;
BOOL bSuccess = RegisterRawInputDevices(&inpDevice,1,sizeof(RAWINPUTDEVICE));
if (bSuccess == FALSE)
{
fwrite("Error registering input", 1, 0x17, stderr);
}
return bSuccess;
}
int WINAPI WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nShowCmd
)
{
HWND hWnd;
MSG msg;
const char* className = "WindowsApp";
WNDCLASSEXA wndClass;
wndClass.cbSize = sizeof(WNDCLASSEXA);
wndClass.style = CS_DBLCLKS;
wndClass.lpfnWndProc = &WindowProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIconA(0x0, IDI_APPLICATION);
wndClass.hCursor = LoadCursorA(0x0, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
wndClass.lpszMenuName = (LPCSTR)0x0;
wndClass.lpszClassName = className;
wndClass.hIconSm = LoadIconA((HINSTANCE)0x0, IDI_APPLICATION);
ATOM cls = RegisterClassExA(&wndClass);
int return_value = 0;
if (cls)
{
hWnd = CreateWindowExA(0,className, "PS3-DDR-DanceMat Input Converter", WS_GROUP| WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, 320, 200, (HWND)0x0, (HMENU)0x0, hInstance, (LPVOID)0x0);
ShowWindow(hWnd, nShowCmd);
memset(g_PreviousPadState,0,sizeof(g_PreviousPadState));
BOOL bSuccess = RegisterRawInputDevice(hWnd);
if (bSuccess)
{
while (true) {
BOOL bContinue = GetMessageA(&msg,(HWND)0,0,0);
if (!bContinue)
break;
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
return msg.wParam;
}
}
return return_value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment