From 0648ec7ab22021c49d7f2f782c7309c48db9babc Mon Sep 17 00:00:00 2001 From: doomertheboomer Date: Thu, 27 Jul 2023 14:41:50 +0700 Subject: [PATCH] steal pipe server from project asakura --- depthrush/drs.cpp | 145 +++++++++++++++++++++++++++++++++++++++++-- depthrush/drs.h | 20 ++---- depthrush/includes.h | 1 + depthrush/main.cpp | 25 ++------ 4 files changed, 152 insertions(+), 39 deletions(-) diff --git a/depthrush/drs.cpp b/depthrush/drs.cpp index 1248622..580c8a8 100644 --- a/depthrush/drs.cpp +++ b/depthrush/drs.cpp @@ -6,7 +6,8 @@ bool kinectRunning = false; bool kinectStarted = false; -extern VRFoot VR_FOOTS[2]; +VRFoot feet[16]; + typedef struct { union { @@ -158,6 +159,43 @@ inline DWORD scale_double_to_width(double val) { return static_cast(val * 1696); } +DWORD depthrushWritePort(HANDLE port, char data[], unsigned length) +{ + DWORD numWritten = 0; + + OVERLAPPED ol = { 0, 0, 0, 0, NULL }; + ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + + BOOL status = WriteFile(port, data, length, &numWritten, &ol); + DWORD xferBytes = 0; + + if (!status) + { + switch (GetLastError()) + { + case ERROR_SUCCESS: + break; + case ERROR_IO_PENDING: + // Wait for 16ms + if (WaitForSingleObject(ol.hEvent, 16) == WAIT_OBJECT_0) + { + status = GetOverlappedResult(port, &ol, &xferBytes, FALSE); + } + else + { + CancelIo(port); + } + break; + } + } + + CloseHandle(ol.hEvent); + + FlushFileBuffers(port); + return numWritten; +} + + void fire_touches(drs_touch_t* events, size_t event_count) { // check callback first @@ -209,9 +247,107 @@ void fire_touches(drs_touch_t* events, size_t event_count) { touch_callback(&dev, game_touches.get(), (int)event_count, 0, user_data); } -VRFoot feet[11]; -unsigned long holdcounters[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -unsigned long releasecounters[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +DWORD depthrushTouchThread(HANDLE port) +{ + char fileBuf[32]; + puts("starting serial touch thread"); + + DWORD times = 0; + + for (;;) + { + DWORD bytesRead = 0; + memset(fileBuf, 0, 32); + + OVERLAPPED ol = { 0, 0, 0, 0, NULL }; + BOOL ret = 0; + ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + + BOOL rfResult = ReadFile(port, fileBuf, 32, &bytesRead, &ol); + DWORD xferBytes = 0; + + if (!rfResult) + { + switch (GetLastError()) + { + case ERROR_SUCCESS: + break; + case ERROR_IO_PENDING: + // Wait for 16ms + if (WaitForSingleObject(ol.hEvent, 16) == WAIT_OBJECT_0) + { + rfResult = GetOverlappedResult(port, &ol, &xferBytes, FALSE); + } + else + { + CancelIo(port); + } + break; + } + } + + CloseHandle(ol.hEvent); + + if (xferBytes > 0) + { + printf("IN: xferred %d bytes\n", xferBytes); + } + + if (bytesRead > 0) + { + printf("Read %d bytes: ", bytesRead); + for (unsigned x = 0; x < bytesRead; x++) + { + printf("%02X ", fileBuf[x]); + } + printf("\n"); + + BOOL packetRecognised = FALSE; + + if (!packetRecognised) + { + puts("unknown packet, responding with OK"); + depthrushWritePort(port, (char*)"1", 1); + } + } + Sleep(16); + } +} + +DWORD depthrushNamedPipeServer(LPVOID _) +{ + puts("init depthrush pipe server"); + + HANDLE pipe = CreateNamedPipeW( + L"\\\\.\\pipe\\depthrush-api", + PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, + PIPE_TYPE_BYTE | PIPE_WAIT, + PIPE_UNLIMITED_INSTANCES, + 255, + 255, + 25, + NULL + ); + + if (!pipe) + { + puts("named pipe creation failed!"); + return 1; + } + + BOOL connected = ConnectNamedPipe(pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); + + if (connected) + { + puts("client connection established, spawning thread"); + + DWORD tid = 0; + CreateThread(NULL, 0, depthrushTouchThread, pipe, 0, &tid); + printf("thread spawned, tid=%d\n", tid); + } + + return 0; +} void start_kinect() { @@ -308,6 +444,7 @@ void hookDancepad() { MH_CreateHookApi(L"TouchSDKDll.dll", "?InitTouch@TouchSDK@@QEAAHPEAU_DeviceInfo@@HP6AXU2@PEBU_TouchPointData@@HHPEBX@ZP6AX1_N3@ZPEAX@Z", TouchSDK_InitTouch, NULL); MH_EnableHook(MH_ALL_HOOKS); + CreateThread(NULL, 0, depthrushNamedPipeServer, NULL, 0, NULL); start_kinect(); } \ No newline at end of file diff --git a/depthrush/drs.h b/depthrush/drs.h index a0d9496..974a014 100644 --- a/depthrush/drs.h +++ b/depthrush/drs.h @@ -8,10 +8,10 @@ enum DRS_TOUCH_TYPE { typedef struct drs_touch_t { int type = DRS_UP; int id = 0; - double x = 0.0; //x = pixelX/1920 - double y = 0.0; //y = pixelY/1920 - double width = 1; //width = pixelWidth/1920 - double height = 1; //height = pixelHeight/1920 + double x = 0.0; // x = pixelX/1920 + double y = 0.0; // y = pixelY/1920 + double width = 1; // width = pixelWidth/1920 + double height = 1; // height = pixelHeight/1920 }; struct VRFoot { @@ -21,20 +21,12 @@ struct VRFoot { float size_base = 0.05; float size_scale = 0.1; float height = 3; - //linalg::aliases::float4 rotation {0, 0, 0, 1}; drs_touch_t event{}; - //linalg::aliases::float3 to_world(linalg::aliases::float3 pos); + bool touching = false; }; -//extern char DRS_TAPELED[38 * 49][3]; -//extern linalg::aliases::float3 VR_SCALE; -//extern linalg::aliases::float3 VR_OFFSET; -//extern float VR_ROTATION; -//extern VRFoot VR_FOOTS[2]; - - - void fire_touches(drs_touch_t* events, size_t event_count); + void start_kinect(); void hookDancepad(); \ No newline at end of file diff --git a/depthrush/includes.h b/depthrush/includes.h index 6519dca..46b25c9 100644 --- a/depthrush/includes.h +++ b/depthrush/includes.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include diff --git a/depthrush/main.cpp b/depthrush/main.cpp index ee4c88d..4a9260b 100644 --- a/depthrush/main.cpp +++ b/depthrush/main.cpp @@ -23,30 +23,13 @@ static int returnFalse() { } -bool init = false; -/* -DWORD WINAPI MainThread(LPVOID lpReserved) -{ - bool init_hook = false; - do - { - - } while (!init_hook); - return TRUE; -} -*/ BOOL WINAPI DllMain(HMODULE hMod, DWORD dwReason, LPVOID lpReserved) { - switch (dwReason) - { - case DLL_PROCESS_ATTACH: - DisableThreadLibraryCalls(hMod); - imageBase = (uintptr_t)GetModuleHandleA(0); - hookDancepad(); + DisableThreadLibraryCalls(hMod); + imageBase = (uintptr_t)GetModuleHandleA(0); - break; - } - return TRUE; + hookDancepad(); // drs.cpp + return true; } \ No newline at end of file