diff --git a/OpenParrot/src/Functions/Games/ES3X/WMMT6.cpp b/OpenParrot/src/Functions/Games/ES3X/WMMT6.cpp index c63d27f..72e9a9c 100644 --- a/OpenParrot/src/Functions/Games/ES3X/WMMT6.cpp +++ b/OpenParrot/src/Functions/Games/ES3X/WMMT6.cpp @@ -167,56 +167,31 @@ static HWND mt6Hwnd; typedef BOOL (WINAPI* ShowWindow_t)(HWND, int); static ShowWindow_t pShowWindow; - // Hello Win32 my old friend... typedef LRESULT (WINAPI* WindowProcedure_t)(HWND, UINT, WPARAM, LPARAM); static WindowProcedure_t pMaxituneWndProc; static BOOL gotWindowSize = FALSE; -static unsigned displaySizeX = 0; -static unsigned displaySizeY = 0; -static float scaleFactorX = 0.0f; -static float scaleFactorY = 0.0f; static LRESULT Hook_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { if (!gotWindowSize) { - displaySizeX = GetSystemMetrics(SM_CXSCREEN); - displaySizeY = GetSystemMetrics(SM_CYSCREEN); - scaleFactorX = static_cast(displaySizeX) / 1360.0f; - scaleFactorY = static_cast(displaySizeY) / 768.0f; - printf("display is %dx%d (scale factor of %f, %f)\n", displaySizeX, displaySizeY, scaleFactorX, scaleFactorY); + mt6SetDisplayParams(hwnd); gotWindowSize = TRUE; } if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { - unsigned short mx = GET_X_LPARAM(lParam); - unsigned short my = GET_Y_LPARAM(lParam); + mt6SetTouchData(lParam, msg == WM_LBUTTONDOWN, false); + return 0; + } - //unsigned short trueMy = 768 - my; - - float scaledMx = static_cast(mx) / 1360.f; - float scaledMy = static_cast(my) / 768.f; - - scaledMy = 1.0f - scaledMy; - - scaledMx *= scaleFactorX; - scaledMy *= scaleFactorY; - - unsigned short trueMx = static_cast(scaledMx * 16383.0f); - unsigned short trueMy = static_cast(scaledMy * 16383.0f); - trueMy += 9500; // Cheap hack, todo do the math better!! - - //mx *= (16383 / 1360); - //trueMy *= (16383 / 1360); - - printf("%d %d\n", trueMx, trueMy); - mt6SetTouchParams(trueMx, trueMy, msg == WM_LBUTTONDOWN); - - printf("MOUSE %s (%d, %d)\n", msg == WM_LBUTTONDOWN ? "DOWN" : "UP ", mx, my); + if (msg == WM_POINTERDOWN || + msg == WM_POINTERUP) + { + mt6SetTouchData(lParam, msg == WM_POINTERDOWN, true); return 0; } @@ -252,9 +227,6 @@ static WsaStringToAddressA_t gWsaStringToAddressA; //#define ROUTER_IP "192.168.100.1" #define LOCALHOST "127.0.0.1" - - - static INT WSAAPI Hook_WsaStringToAddressA( _In_ LPSTR AddressString, _In_ INT AddressFamily, @@ -527,7 +499,6 @@ static InitFunction Wmmt6Func([]() MH_CreateHookApi(L"kernel32", "OutputDebugStringA", Hook_OutputDebugStringA, NULL); // CreateFile* hooks are in the JVS FILE - // Network hooks MH_CreateHookApi(L"Ws2_32", "WSAStringToAddressA", Hook_WsaStringToAddressA, reinterpret_cast(&gWsaStringToAddressA)); MH_CreateHookApi(L"Ws2_32", "getaddrinfo", Hook_getaddrinfo, reinterpret_cast(&ggetaddrinfo)); diff --git a/OpenParrot/src/Functions/Games/ES3X/WMMT6R.cpp b/OpenParrot/src/Functions/Games/ES3X/WMMT6R.cpp index e691adf..38bfcfb 100644 --- a/OpenParrot/src/Functions/Games/ES3X/WMMT6R.cpp +++ b/OpenParrot/src/Functions/Games/ES3X/WMMT6R.cpp @@ -450,50 +450,26 @@ typedef LRESULT(WINAPI* WindowProcedure_t)(HWND, UINT, WPARAM, LPARAM); static WindowProcedure_t pMaxituneWndProc; static BOOL gotWindowSize = FALSE; -static unsigned displaySizeX = 0; -static unsigned displaySizeY = 0; -static float scaleFactorX = 0.0f; -static float scaleFactorY = 0.0f; static LRESULT Hook_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { if (!gotWindowSize) { - displaySizeX = GetSystemMetrics(SM_CXSCREEN); - displaySizeY = GetSystemMetrics(SM_CYSCREEN); - scaleFactorX = static_cast(displaySizeX) / 1360.0f; - scaleFactorY = static_cast(displaySizeY) / 768.0f; - printf("display is %dx%d (scale factor of %f, %f)\n", displaySizeX, displaySizeY, scaleFactorX, scaleFactorY); + mt6SetDisplayParams(hwnd); gotWindowSize = TRUE; } if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { - unsigned short mx = GET_X_LPARAM(lParam); - unsigned short my = GET_Y_LPARAM(lParam); + mt6SetTouchData(lParam, msg == WM_LBUTTONDOWN, false); + return 0; + } - //unsigned short trueMy = 768 - my; - - float scaledMx = static_cast(mx) / 1360.f; - float scaledMy = static_cast(my) / 768.f; - - scaledMy = 1.0f - scaledMy; - - scaledMx *= scaleFactorX; - scaledMy *= scaleFactorY; - - unsigned short trueMx = static_cast(scaledMx * 16383.0f); - unsigned short trueMy = static_cast(scaledMy * 16383.0f); - trueMy += 9500; // Cheap hack, todo do the math better!! - - //mx *= (16383 / 1360); - //trueMy *= (16383 / 1360); - - printf("%d %d\n", trueMx, trueMy); - mt6SetTouchParams(trueMx, trueMy, msg == WM_LBUTTONDOWN); - - printf("MOUSE %s (%d, %d)\n", msg == WM_LBUTTONDOWN ? "DOWN" : "UP ", mx, my); + if (msg == WM_POINTERDOWN || + msg == WM_POINTERUP) + { + mt6SetTouchData(lParam, msg == WM_POINTERDOWN, true); return 0; } diff --git a/OpenParrot/src/Utility/TouchSerial/MT6.cpp b/OpenParrot/src/Utility/TouchSerial/MT6.cpp index 909fedb..5dfb5f6 100644 --- a/OpenParrot/src/Utility/TouchSerial/MT6.cpp +++ b/OpenParrot/src/Utility/TouchSerial/MT6.cpp @@ -1,4 +1,6 @@ #include "MT6.h" +#include "MinHook.h" +#include #include #define TS_PIPE_TIMEOUT 16 @@ -24,12 +26,51 @@ volatile unsigned short touchy = 0; volatile BOOL touchpressed = FALSE; volatile BOOL touchlift = FALSE; -void mt6SetTouchParams(unsigned short x, unsigned short y, BOOL down) +static unsigned displaySizeX = 0; +static unsigned displaySizeY = 0; +static float scaleFactorX = 0.0f; +static float scaleFactorY = 0.0f; + +static HWND gameWindow; + +typedef INT(WINAPI* GetSystemMetrics_t)(int); +static GetSystemMetrics_t pGetSystemMetrics; + +// Telling wangan that the system res is 1360x768, aka stock game res +// This makes scaling the touch input across resolutions a breeze +int WINAPI hook_GetSystemMetrics(int nIndex) +{ + if (nIndex == 0) { + return 1360; + } + + if (nIndex == 1) { + return 768; + } + return pGetSystemMetrics(nIndex); +} + +void mt6SetTouchData(LPARAM lParam, BOOL down, BOOL isTouchScreen) { if (bHasBooted) { - touchx = x; - touchy = y; + unsigned short mx; + unsigned short my; + if (isTouchScreen) { + POINT point = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)}; + ScreenToClient(gameWindow, &point); + mx = point.x; + my = point.y; + } + else { + mx = GET_X_LPARAM(lParam); + my = GET_Y_LPARAM(lParam); + } + + // Touchscreen y coordinates are inverted + my = displaySizeY - my; + touchx = static_cast(mx * scaleFactorX); + touchy = static_cast(my * scaleFactorY); touchpressed = down; } } @@ -156,9 +197,9 @@ DWORD mt6SerialTouchThread(HANDLE port) memset(touchResp, 0, 5); touchResp[0] = (char)0b11000000; touchResp[1] = (touchx & 0b01111111); - touchResp[2] = ((touchx >> 8) & 0b01111111); + touchResp[2] = ((touchx >> 7) & 0b01111111); touchResp[3] = (touchy & 0b01111111); - touchResp[4] = ((touchy >> 8) & 0b01111111); + touchResp[4] = ((touchy >> 7) & 0b01111111); mt6WritePort(port, touchResp, 5); } else @@ -169,9 +210,9 @@ DWORD mt6SerialTouchThread(HANDLE port) memset(touchResp, 0, 5); touchResp[0] = (char)0b10000000; touchResp[1] = (touchx & 0b01111111); - touchResp[2] = ((touchx >> 8) & 0b01111111); + touchResp[2] = ((touchx >> 7) & 0b01111111); touchResp[3] = (touchy & 0b01111111); - touchResp[4] = ((touchy >> 8) & 0b01111111); + touchResp[4] = ((touchy >> 7) & 0b01111111); mt6WritePort(port, touchResp, 5); touchlift = false; } @@ -208,7 +249,7 @@ DWORD mt6SerialNamedPipeServer(LPVOID _) if (connected) { puts("client connection established, spawning thread"); - + DWORD tid = 0; CreateThread(NULL, 0, mt6SerialTouchThread, pipe, 0, &tid); printf("thread spawned, tid=%d\n", tid); @@ -217,8 +258,25 @@ DWORD mt6SerialNamedPipeServer(LPVOID _) return 0; } +void mt6SetDisplayParams(HWND hwnd) { + RECT rect; + GetClientRect(hwnd, &rect); + displaySizeX = rect.right; + displaySizeY = rect.bottom; + scaleFactorX = (float)16383 / displaySizeX; + scaleFactorY = (float)16383 / displaySizeY; + RegisterTouchWindow(hwnd, 0); + gameWindow = hwnd; + printf("display is %dx%d (scale factor of %f, %f)\n", displaySizeX, displaySizeY, scaleFactorX, scaleFactorY); +} + void mt6SerialTouchInit() { + // System metrics hook to decouple game res from touchscreen res + MH_Initialize(); + MH_CreateHookApi(L"user32", "GetSystemMetrics", hook_GetSystemMetrics, reinterpret_cast(&pGetSystemMetrics)); + MH_EnableHook(MH_ALL_HOOKS); + // This is on another thread so we can wait a bit to make sure the pipe server is actually init'd CreateThread(NULL, 0, mt6SerialNamedPipeServer, NULL, 0, NULL); Sleep(20); diff --git a/OpenParrot/src/Utility/TouchSerial/MT6.h b/OpenParrot/src/Utility/TouchSerial/MT6.h index 792242e..7392eac 100644 --- a/OpenParrot/src/Utility/TouchSerial/MT6.h +++ b/OpenParrot/src/Utility/TouchSerial/MT6.h @@ -3,5 +3,6 @@ static HANDLE touchDevice; -void mt6SetTouchParams(unsigned short x, unsigned short y, BOOL down); +void mt6SetTouchData(LPARAM lParam, BOOL down, BOOL isTouchScreen); +void mt6SetDisplayParams(HWND hwnd); void mt6SerialTouchInit();