From ffb39b86ade6e6d1ddbdf709e2f53ad336ac28f6 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Wed, 16 Apr 2003 19:06:20 +0000 Subject: [PATCH] *** empty log message *** --- Cxbx.dsp | 4 + Doc/Todo.txt | 4 - Include/Win32/Cxbx/ResCxbx.h | 2 +- Include/Win32/CxbxKrnl/Emu.h | 5 + Include/Win32/CxbxKrnl/EmuD3D8.h | 9 +- Include/Win32/CxbxKrnl/EmuDInput.h | 13 +- Include/Win32/XBController.h | 38 +- Resource/Cxbx.rc | 5 +- Source/Win32/Cxbx/DlgControllerConfig.cpp | 2 +- Source/Win32/CxbxKrnl/Emu.cpp | 30 +- Source/Win32/CxbxKrnl/EmuD3D8.cpp | 60 +-- Source/Win32/CxbxKrnl/EmuDInput.cpp | 20 +- Source/Win32/CxbxKrnl/EmuXapi.cpp | 2 +- Source/Win32/Mutex.cpp | 8 +- Source/Win32/XBController.cpp | 500 +++++++++++++++++----- 15 files changed, 522 insertions(+), 180 deletions(-) diff --git a/Cxbx.dsp b/Cxbx.dsp index 8fe0f2228..a5f387397 100644 --- a/Cxbx.dsp +++ b/Cxbx.dsp @@ -165,6 +165,10 @@ SOURCE=.\Include\Win32\CxbxKrnl\EmuShared.h # End Source File # Begin Source File +SOURCE=.\Include\Win32\CxbxKrnl\EmuXapi.h +# End Source File +# Begin Source File + SOURCE=.\Include\Core\Error.h # End Source File # Begin Source File diff --git a/Doc/Todo.txt b/Doc/Todo.txt index 0cc138904..a5924efd9 100644 --- a/Doc/Todo.txt +++ b/Doc/Todo.txt @@ -1,7 +1,5 @@ Cxbx Todo: - Encapsulate XBController. Remove all relevent old implementation modules. - Batch config all buttons (should be very easy..just click one by one) Encapsulate RecentFiles into a nice little class @@ -12,8 +10,6 @@ Cxbx Todo: Closing a console should not terminate the entire process. - Everything that is initialized should be cleaned up before termination. - If possible, Direct3D Rendering window should inherit from Wnd. Try to add compatibility with Windows ME using LLDT assembly. diff --git a/Include/Win32/Cxbx/ResCxbx.h b/Include/Win32/Cxbx/ResCxbx.h index f7a64265f..d3f758b14 100644 --- a/Include/Win32/Cxbx/ResCxbx.h +++ b/Include/Win32/Cxbx/ResCxbx.h @@ -1,6 +1,6 @@ //{{NO_DEPENDENCIES}} // Microsoft Developer Studio generated include file. -// Used by C:\Aaron\Projects\OpenXDK\cxbx\Resource\Cxbx.rc +// Used by D:\OpenXDK\cxbx\Resource\Cxbx.rc // #define IDI_CXBX 101 #define IDB_SPLASH 102 diff --git a/Include/Win32/CxbxKrnl/Emu.h b/Include/Win32/CxbxKrnl/Emu.h index bda44c16d..c68e66f0e 100644 --- a/Include/Win32/CxbxKrnl/Emu.h +++ b/Include/Win32/CxbxKrnl/Emu.h @@ -49,6 +49,11 @@ extern "C" CXBXKRNL_API void NTAPI EmuNoFunc(); // ****************************************************************** extern "C" CXBXKRNL_API void NTAPI EmuInit(uint32 TlsAdjust, Xbe::LibraryVersion *LibraryVersion, DebugMode DbgMode, char *szDebugFilename, Xbe::Header *XbeHeader, uint32 XbeHeaderSize, void (*Entry)()); +// ****************************************************************** +// * func: EmuCleanup +// ****************************************************************** +extern "C" CXBXKRNL_API void NTAPI EmuCleanup(); + // ****************************************************************** // * func: EmuPanic // ****************************************************************** diff --git a/Include/Win32/CxbxKrnl/EmuD3D8.h b/Include/Win32/CxbxKrnl/EmuD3D8.h index d4cfb887b..27b9920b9 100644 --- a/Include/Win32/CxbxKrnl/EmuD3D8.h +++ b/Include/Win32/CxbxKrnl/EmuD3D8.h @@ -47,8 +47,13 @@ extern HWND g_EmuWindow; // Rendering Window // ****************************************************************** -// * func: EmuInitD3D +// * func: EmuD3DInit // ****************************************************************** -void EmuInitD3D(Xbe::Header *XbeHeader, uint32 XbeHeaderSize); +void EmuD3DInit(Xbe::Header *XbeHeader, uint32 XbeHeaderSize); + +// ****************************************************************** +// * func: EmuD3DCleanup +// ****************************************************************** +void EmuD3DCleanup(); #endif diff --git a/Include/Win32/CxbxKrnl/EmuDInput.h b/Include/Win32/CxbxKrnl/EmuDInput.h index 1a758b834..830f5c97a 100644 --- a/Include/Win32/CxbxKrnl/EmuDInput.h +++ b/Include/Win32/CxbxKrnl/EmuDInput.h @@ -48,14 +48,19 @@ namespace xapi extern xapi::XINPUT_STATE g_EmuController1; // Input Controller // ****************************************************************** -// * func: EmuPollInput +// * func: EmuDInputInit // ****************************************************************** -extern void EmuPollController(); +extern void EmuDInputInit(); // ****************************************************************** -// * func: EmuInitDInput +// * func: EmuDInputCleanup // ****************************************************************** -extern void EmuInitDInput(); +extern void EmuDInputCleanup(); + +// ****************************************************************** +// * func: EmuDInputPoll +// ****************************************************************** +extern void EmuDInputPoll(); // ****************************************************************** // * offsets into analog button array diff --git a/Include/Win32/XBController.h b/Include/Win32/XBController.h index 517c6827e..3c4284a2b 100644 --- a/Include/Win32/XBController.h +++ b/Include/Win32/XBController.h @@ -93,6 +93,16 @@ enum XBCtrlObject XBCTRL_OBJECT_COUNT }; +// ****************************************************************** +// * DirectInput Enumeration Types +// ****************************************************************** +enum XBCtrlState +{ + XBCTRL_STATE_NONE = 0, + XBCTRL_STATE_CONFIG, + XBCTRL_STATE_LISTEN +}; + // ****************************************************************** // * Maximum number of devices allowed // ****************************************************************** @@ -111,14 +121,14 @@ struct XBCtrlObjectCfg // ****************************************************************** // * class: XBController // ****************************************************************** -class XBController : public Error, Mutex +class XBController : public Error { public: // ****************************************************************** // * Initialization // ****************************************************************** XBController(); - ~XBController() {}; + ~XBController(); // ****************************************************************** // * Registry Load/Save @@ -136,9 +146,20 @@ class XBController : public Error, Mutex // ****************************************************************** // * Listening // ****************************************************************** - void ListeningBegin(HWND hwnd); - void ListeningPoll(xapi::XINPUT_STATE &Controller); - void ListeningEnd(); + void ListenBegin(HWND hwnd); + void ListenPoll(xapi::XINPUT_STATE *Controller); + void ListenEnd(); + + // ****************************************************************** + // * DirectInput Init / Cleanup + // ****************************************************************** + void DInputInit(HWND hwnd); + void DInputCleanup(); + + // ****************************************************************** + // * Check if a device is currently in the configuration + // ****************************************************************** + bool DeviceIsUsed(const char *szDeviceName); // ****************************************************************** // * Input Device Name Lookup Table @@ -193,10 +214,15 @@ class XBController : public Error, Mutex } m_InputDevice[XBCTRL_MAX_DEVICES]; + // ****************************************************************** + // * Current State + // ****************************************************************** + XBCtrlState m_CurrentState; + // ****************************************************************** // * Config State Variables // ****************************************************************** - LONG LastMouse_lX, LastMouse_lY, LastMouse_lZ; + LONG lPrevMouseX, lPrevMouseY, lPrevMouseZ; XBCtrlObject CurConfigObject; // ****************************************************************** diff --git a/Resource/Cxbx.rc b/Resource/Cxbx.rc index 7a653513f..01ac9aa29 100644 --- a/Resource/Cxbx.rc +++ b/Resource/Cxbx.rc @@ -235,9 +235,10 @@ BEGIN GROUPBOX "Digital Buttons",IDC_STATIC,2,51,220,46 GROUPBOX "Analog Thumbstick (Left)",IDC_STATIC,226,1,115,47 GROUPBOX "Analog Thumbstick (Right)",IDC_STATIC,227,51,114,46 - PUSHBUTTON "Load Configuration...",IDC_BUTTON1,6,102,104,14,BS_FLAT + PUSHBUTTON "Load Configuration...",IDC_BUTTON1,6,102,104,14,BS_FLAT | + WS_DISABLED PUSHBUTTON "Save Configuration...",IDC_BUTTON2,113,102,104,14, - BS_FLAT + BS_FLAT | WS_DISABLED CTEXT "Please choose one of the controller components from above...", IDC_CONFIG_STATUS,6,121,329,12,SS_CENTERIMAGE, WS_EX_STATICEDGE diff --git a/Source/Win32/Cxbx/DlgControllerConfig.cpp b/Source/Win32/Cxbx/DlgControllerConfig.cpp index ed333b92b..cc13bd8e0 100644 --- a/Source/Win32/Cxbx/DlgControllerConfig.cpp +++ b/Source/Win32/Cxbx/DlgControllerConfig.cpp @@ -230,7 +230,7 @@ cleanup: // ****************************************************************** { if(g_XBController.GetError()) - strcpy(szNewText, g_XBController.GetError()); + sprintf(szNewText, "%s", g_XBController.GetError()); SetWindowText(hWndButton, szOrgText); diff --git a/Source/Win32/CxbxKrnl/Emu.cpp b/Source/Win32/CxbxKrnl/Emu.cpp index 1a7fbba89..6274adbff 100644 --- a/Source/Win32/CxbxKrnl/Emu.cpp +++ b/Source/Win32/CxbxKrnl/Emu.cpp @@ -229,7 +229,7 @@ extern "C" CXBXKRNL_API void NTAPI EmuInit(uint32 TlsAdjust, Xbe::LibraryVersion printf("Skipped\n"); } - EmuInitD3D(XbeHeader, XbeHeaderSize); + EmuD3DInit(XbeHeader, XbeHeaderSize); } printf("Emu (0x%.08X): Initial thread starting.\n", GetCurrentThreadId()); @@ -260,6 +260,21 @@ extern "C" CXBXKRNL_API void NTAPI EmuInit(uint32 TlsAdjust, Xbe::LibraryVersion return; } +// ****************************************************************** +// * func: EmuCleanup +// ****************************************************************** +extern "C" CXBXKRNL_API void NTAPI EmuCleanup() +{ + if(EmuIsXboxFS()) + EmuSwapFS(); // Win2k/XP FS + + EmuD3DCleanup(); + + ExitProcess(0); + + return; +} + // ****************************************************************** // * func: EmuPanic // ****************************************************************** @@ -277,7 +292,7 @@ extern "C" CXBXKRNL_API void NTAPI EmuPanic() MessageBox(NULL, "Kernel Panic! Process will now terminate.", "CxbxKrnl", MB_OK | MB_ICONEXCLAMATION); #endif - ExitProcess(1); + EmuCleanup(); EmuSwapFS(); // XBox FS } @@ -439,16 +454,7 @@ void EmuInstallWrappers(OOVPATable *OovpaTable, uint32 OovpaTableSize, void (*En // ****************************************************************** int EmuException(LPEXCEPTION_POINTERS e) { - static int count = 0; - - count++; - - if(count < 20000) - return EXCEPTION_CONTINUE_EXECUTION; - else - count = 0; - - int ret = MessageBox(NULL, "ERROR: Maximum exception count reached.\n\nPress 'OK' to terminate emulation.\nPress 'Cancel' to debug.", "Cxbx", MB_ICONSTOP | MB_OKCANCEL); + int ret = MessageBox(NULL, "ERROR: Recieved Exception.\n\nPress 'OK' to terminate emulation.\nPress 'Cancel' to debug.", "Cxbx", MB_ICONSTOP | MB_OKCANCEL); if(ret == IDOK) ExitProcess(1); diff --git a/Source/Win32/CxbxKrnl/EmuD3D8.cpp b/Source/Win32/CxbxKrnl/EmuD3D8.cpp index 3675c989b..5f943bcd8 100644 --- a/Source/Win32/CxbxKrnl/EmuD3D8.cpp +++ b/Source/Win32/CxbxKrnl/EmuD3D8.cpp @@ -78,9 +78,9 @@ static LRESULT WINAPI EmuMsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPar static void EmuRenderWindow(PVOID); // ****************************************************************** -// * func: EmuInitD3D +// * func: EmuD3DInit // ****************************************************************** -VOID EmuInitD3D(Xbe::Header *XbeHeader, uint32 XbeHeaderSize) +VOID EmuD3DInit(Xbe::Header *XbeHeader, uint32 XbeHeaderSize) { // ****************************************************************** // * store XbeHeader and XbeHeaderSize for further use @@ -90,6 +90,8 @@ VOID EmuInitD3D(Xbe::Header *XbeHeader, uint32 XbeHeaderSize) g_XbeHeaderSize = XbeHeaderSize; } + g_ThreadInitialized = false; + // ****************************************************************** // * spark up a new thread to handle window message processing // ****************************************************************** @@ -113,6 +115,16 @@ VOID EmuInitD3D(Xbe::Header *XbeHeader, uint32 XbeHeaderSize) } } +// ****************************************************************** +// * func: EmuD3DCleanup +// ****************************************************************** +VOID EmuD3DCleanup() +{ + EmuDInputCleanup(); + + return; +} + // ****************************************************************** // * func: EmuRenderWindow // ****************************************************************** @@ -187,7 +199,7 @@ void EmuRenderWindow(PVOID) // ****************************************************************** // * initialize direct input // ****************************************************************** - EmuInitDInput(); + EmuDInputInit(); // ****************************************************************** // * the other thread can continue now @@ -213,7 +225,7 @@ void EmuRenderWindow(PVOID) Sleep(10); } - ExitProcess(0); + EmuCleanup(); } } @@ -228,50 +240,24 @@ LRESULT WINAPI EmuMsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) PostQuitMessage(0); return 0; - case WM_CREATE: - { - /* We aren't using a menu anymore - HMODULE hCxbxDll = GetModuleHandle("Cxbx.dll"); - - HMENU hMenu = LoadMenu(hCxbxDll, MAKEINTRESOURCE(IDR_RENDERMENU)); - - SetMenu(hWnd, hMenu); - */ - } - break; - - /* - case WM_COMMAND: - { - switch(LOWORD(wParam)) - { - case ID_EMULATION_EXIT: - SendMessage(hWnd, WM_CLOSE, 0, 0); - break; - } - } - break; - */ - case WM_KEYDOWN: - switch (wParam) - { - case VK_ESCAPE: - PostMessage(hWnd, WM_CLOSE, 0, 0); - break; - } + if(wParam == VK_ESCAPE) + PostMessage(hWnd, WM_CLOSE, 0, 0); break; case WM_CLOSE: DestroyWindow(hWnd); break; - +/* case WM_SETCURSOR: SetCursor(NULL); break; +*/ + default: + return DefWindowProc(hWnd, msg, wParam, lParam); } - return DefWindowProc(hWnd, msg, wParam, lParam); + return 0; } // ****************************************************************** diff --git a/Source/Win32/CxbxKrnl/EmuDInput.cpp b/Source/Win32/CxbxKrnl/EmuDInput.cpp index 0f3976682..5b8f6e021 100644 --- a/Source/Win32/CxbxKrnl/EmuDInput.cpp +++ b/Source/Win32/CxbxKrnl/EmuDInput.cpp @@ -45,6 +45,7 @@ // * exported globals // ****************************************************************** xapi::XINPUT_STATE g_EmuController1; +XBController g_XBController; // ****************************************************************** // * prevent name collisions @@ -55,17 +56,30 @@ namespace xapi }; // ****************************************************************** -// * func: EmuInitDInput +// * func: EmuDInputInit // ****************************************************************** -void EmuInitDInput() +void EmuDInputInit() { + g_EmuShared->GetXBController(&g_XBController); + + g_XBController.ListenBegin(g_EmuWindow); + return; } +// ****************************************************************** +// * func: EmuDInputCleanup +// ****************************************************************** +void EmuDInputCleanup() +{ + g_XBController.ListenEnd(); +} + // ****************************************************************** // * func: EmuPollController // ****************************************************************** -void EmuPollController() +void EmuDInputPoll() { + g_XBController.ListenPoll(&g_EmuController1); return; } \ No newline at end of file diff --git a/Source/Win32/CxbxKrnl/EmuXapi.cpp b/Source/Win32/CxbxKrnl/EmuXapi.cpp index 63ad60021..e7e707941 100644 --- a/Source/Win32/CxbxKrnl/EmuXapi.cpp +++ b/Source/Win32/CxbxKrnl/EmuXapi.cpp @@ -276,7 +276,7 @@ DWORD WINAPI xapi::EmuXInputGetState // and they are always normal Controllers if((int)hDevice >= 1 && (int)hDevice <= 4) { - EmuPollController(); + EmuDInputPoll(); if((int)hDevice == 1) memcpy(pState, &g_EmuController1, sizeof(XINPUT_STATE)); diff --git a/Source/Win32/Mutex.cpp b/Source/Win32/Mutex.cpp index 08894279e..6ac299eb1 100644 --- a/Source/Win32/Mutex.cpp +++ b/Source/Win32/Mutex.cpp @@ -52,8 +52,8 @@ void Mutex::Lock() while(true) { // Grab the lock, letting us look at the variables -// while(InterlockedCompareExchange((LPVOID*)&m_MutexLock, (LPVOID)1, (LPVOID)0)) - while(InterlockedCompareExchange((LPLONG)&m_MutexLock, (LONG)1, (LONG)0)) + while(InterlockedCompareExchange((LPVOID*)&m_MutexLock, (LPVOID)1, (LPVOID)0)) +// while(InterlockedCompareExchange((LPLONG)&m_MutexLock, (LONG)1, (LONG)0)) Sleep(1); // Are we the the new owner? @@ -99,8 +99,8 @@ void Mutex::Lock() void Mutex::Unlock() { // Grab the lock, letting us look at the variables -// while(InterlockedCompareExchange((LPVOID*)&m_MutexLock, (LPVOID)1, (LPVOID)0)) - while (InterlockedCompareExchange((LPLONG)&m_MutexLock, (LONG)1, (LONG)0)) + while(InterlockedCompareExchange((LPVOID*)&m_MutexLock, (LPVOID)1, (LPVOID)0)) +// while (InterlockedCompareExchange((LPLONG)&m_MutexLock, (LONG)1, (LONG)0)) Sleep(1); // Decrement the lock count diff --git a/Source/Win32/XBController.cpp b/Source/Win32/XBController.cpp index d46b390b3..dc58232fe 100644 --- a/Source/Win32/XBController.cpp +++ b/Source/Win32/XBController.cpp @@ -40,17 +40,39 @@ // ****************************************************************** XBController::XBController() { + m_CurrentState = XBCTRL_STATE_NONE; + int v=0; for(v=0;vEnumDevices - ( - DI8DEVCLASS_GAMECTRL, - WrapEnumGameCtrlCallback, - this, - DIEDFL_ATTACHEDONLY - ); + m_CurrentState = XBCTRL_STATE_CONFIG; - hRet = m_pDirectInput8->CreateDevice(GUID_SysKeyboard, &m_InputDevice[m_dwInputDeviceCount].m_Device, NULL); + DInputInit(hwnd); - if(!FAILED(hRet)) - { - m_InputDevice[m_dwInputDeviceCount].m_Flags = DEVICE_FLAG_KEYBOARD; + if(GetError() != 0) + return; - m_InputDevice[m_dwInputDeviceCount++].m_Device->SetDataFormat(&c_dfDIKeyboard); - } - - hRet = m_pDirectInput8->CreateDevice(GUID_SysMouse, &m_InputDevice[m_dwInputDeviceCount].m_Device, NULL); - - if(!FAILED(hRet)) - { - m_InputDevice[m_dwInputDeviceCount].m_Flags = DEVICE_FLAG_MOUSE; - - m_InputDevice[m_dwInputDeviceCount++].m_Device->SetDataFormat(&c_dfDIMouse2); - } - } - - // ****************************************************************** - // * Set cooperative level and acquire - // ****************************************************************** - { - for(int v=m_dwInputDeviceCount-1;v>=0;v--) - { - m_InputDevice[v].m_Device->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); - m_InputDevice[v].m_Device->Acquire(); - - HRESULT hRet = m_InputDevice[v].m_Device->Poll(); - - if(FAILED(hRet)) - { - hRet = m_InputDevice[v].m_Device->Acquire(); - - while(hRet == DIERR_INPUTLOST) - hRet = m_InputDevice[v].m_Device->Acquire(); - - if(hRet != DIERR_INPUTLOST) - break; - } - } - } - - // ****************************************************************** - // * Enumerate Controller objects - // ****************************************************************** - { - for(m_dwCurObject=0;m_dwCurObjectEnumObjects(WrapEnumObjectsCallback, this, DIDFT_ALL); - } - - // ****************************************************************** - // * Last Mouse Input for Delta Calculations - // ****************************************************************** - LastMouse_lX = -1; - LastMouse_lY = -1; - LastMouse_lZ = -1; + lPrevMouseX = -1; + lPrevMouseY = -1; + lPrevMouseZ = -1; CurConfigObject = object; -cleanup: - return; } @@ -283,6 +235,12 @@ cleanup: // ****************************************************************** bool XBController::ConfigPoll(char *szStatus) { + if(m_CurrentState != XBCTRL_STATE_CONFIG) + { + SetError("Invalid State", false); + return false; + } + DIDEVICEINSTANCE DeviceInstance; DIDEVICEOBJECTINSTANCE ObjectInstance; @@ -392,7 +350,154 @@ bool XBController::ConfigPoll(char *szStatus) return true; } + } + // ****************************************************************** + // * Detect Keyboard Input + // ****************************************************************** + else if(m_InputDevice[v].m_Flags & DEVICE_FLAG_KEYBOARD) + { + BYTE KeyState[256]; + m_InputDevice[v].m_Device->GetDeviceState(256, KeyState); + + dwFlags = DEVICE_FLAG_KEYBOARD; + + // ****************************************************************** + // * Check for Mouse Key State Change + // ****************************************************************** + for(int r=0;r<256;r++) + { + if(KeyState[r] != 0) + { + dwHow = r; + break; + } + } + + // ****************************************************************** + // * Check for Success + // ****************************************************************** + if(dwHow != -1) + { + Map(CurConfigObject, "SysKeyboard", dwHow, dwFlags); + + printf("Cxbx: Detected Key %d on SysKeyboard\n", dwHow); + + sprintf(szStatus, "Success: %s Mapped to Key %d on SysKeyboard", m_DeviceNameLookup[CurConfigObject], dwHow); + + return true; + } + } + // ****************************************************************** + // * Detect Mouse Input + // ****************************************************************** + else if(m_InputDevice[v].m_Flags & DEVICE_FLAG_MOUSE) + { + DIMOUSESTATE2 MouseState; + + m_InputDevice[v].m_Device->GetDeviceState(sizeof(MouseState), &MouseState); + + dwFlags = DEVICE_FLAG_MOUSE; + + // ****************************************************************** + // * Detect Button State Change + // ****************************************************************** + for(int r=0;r<8;r++) + { + // 0x80 is the mask for button push + if(MouseState.rgbButtons[r] & 0x80) + { + dwHow = r; + dwFlags |= DEVICE_FLAG_MOUSE_CLICK; + break; + } + } + // ****************************************************************** + // * Check for Success + // ****************************************************************** + if(dwHow != -1) + { + Map(CurConfigObject, "SysMouse", dwHow, dwFlags); + + printf("Cxbx: Detected Button %d on SysMouse\n", dwHow); + + sprintf(szStatus, "Success: %s Mapped to Button %d on SysMouse", m_DeviceNameLookup[CurConfigObject], dwHow); + + return true; + } + // ****************************************************************** + // * Check for Mouse Movement + // ****************************************************************** + else + { + LONG lAbsDeltaX=0, lAbsDeltaY=0, lAbsDeltaZ=0; + LONG lDeltaX=0, lDeltaY=0, lDeltaZ=0; + + if(lPrevMouseX == -1 || lPrevMouseY == -1 || lPrevMouseZ == -1) + lDeltaX = lDeltaY = lDeltaZ = 0; + else + { + lDeltaX = MouseState.lX - lPrevMouseX; + lDeltaY = MouseState.lY - lPrevMouseY; + lDeltaZ = MouseState.lZ - lPrevMouseZ; + + lAbsDeltaX = abs(lDeltaX); + lAbsDeltaY = abs(lDeltaY); + lAbsDeltaZ = abs(lDeltaZ); + } + + LONG lMax = (lAbsDeltaX > lAbsDeltaY) ? lAbsDeltaX : lAbsDeltaY; + + if(lAbsDeltaZ > lMax) + lMax = lAbsDeltaZ; + + lPrevMouseX = MouseState.lX; + lPrevMouseY = MouseState.lY; + lPrevMouseZ = MouseState.lZ; + + if(lMax > DETECT_SENSITIVITY_MOUSE) + { + dwFlags |= DEVICE_FLAG_AXIS; + + if(lMax == lAbsDeltaX) + { + dwHow = DIMOFS_X; + dwFlags |= (lDeltaX > 0) ? DEVICE_FLAG_POSITIVE : DEVICE_FLAG_NEGATIVE; + } + else if(lMax == lAbsDeltaY) + { + dwHow = DIMOFS_Y; + dwFlags |= (lDeltaY > 0) ? DEVICE_FLAG_POSITIVE : DEVICE_FLAG_NEGATIVE; + } + else if(lMax == lAbsDeltaZ) + { + dwHow = DIMOFS_Z; + dwFlags |= (lDeltaZ > 0) ? DEVICE_FLAG_POSITIVE : DEVICE_FLAG_NEGATIVE; + } + } + + // ****************************************************************** + // * Check for Success + // ****************************************************************** + if(dwHow != -1) + { + char *szDirection = (dwFlags & DEVICE_FLAG_POSITIVE) ? "Positive" : "Negative"; + char *szObjName = "Unknown"; + + ObjectInstance.dwSize = sizeof(ObjectInstance); + + if(m_InputDevice[v].m_Device->GetObjectInfo(&ObjectInstance, dwHow, DIPH_BYOFFSET) == DI_OK) + szObjName = ObjectInstance.tszName; + + Map(CurConfigObject, "SysMouse", dwHow, dwFlags); + + printf("Cxbx: Detected Movement on the %s %s on SysMouse\n", szDirection, szObjName); + + sprintf(szStatus, "Success: %s Mapped to %s %s on SysMouse", m_DeviceNameLookup[CurConfigObject], szDirection, szObjName); + + return true; + } + } } } @@ -403,27 +508,207 @@ bool XBController::ConfigPoll(char *szStatus) // * func: XBController::ConfigEnd // ****************************************************************** void XBController::ConfigEnd() +{ + if(m_CurrentState != XBCTRL_STATE_CONFIG) + { + SetError("Invalid State", false); + return; + } + + DInputCleanup(); + + m_CurrentState = XBCTRL_STATE_NONE; + + return; +} + +// ****************************************************************** +// * func: XBController::ListenBegin +// ****************************************************************** +void XBController::ListenBegin(HWND hwnd) +{ + if(m_CurrentState != XBCTRL_STATE_NONE) + { + SetError("Invalid State", false); + return; + } + + m_CurrentState = XBCTRL_STATE_LISTEN; + + DInputInit(hwnd); + + return; +} + +// ****************************************************************** +// * func: XBController::ListenPoll +// ****************************************************************** +void XBController::ListenPoll(xapi::XINPUT_STATE *Controller) +{ + // TODO: For each device with a configured input device, update changes + static int step = 500; + + Controller->Gamepad.sThumbLX += step; + + if(Controller->Gamepad.sThumbLX < 0 - 32768 - 2*step || + Controller->Gamepad.sThumbLX > 0 + 32767 - 2*step) + { + step = -step; + Controller->Gamepad.sThumbLX += step; + } + + return; +} + +// ****************************************************************** +// * func: XBController::ListenEnd +// ****************************************************************** +void XBController::ListenEnd() +{ + if(m_CurrentState != XBCTRL_STATE_LISTEN) + { + SetError("Invalid State", false); + return; + } + + DInputCleanup(); + + m_CurrentState = XBCTRL_STATE_NONE; + + return; +} + +// ****************************************************************** +// * func: XBController::DeviceIsUsed +// ****************************************************************** +bool XBController::DeviceIsUsed(const char *szDeviceName) +{ + for(int v=0;vEnumDevices + ( + DI8DEVCLASS_GAMECTRL, + WrapEnumGameCtrlCallback, + this, + DIEDFL_ATTACHEDONLY + ); + + if(m_CurrentState == XBCTRL_STATE_CONFIG || DeviceIsUsed("SysKeyboard")) + { + hRet = m_pDirectInput8->CreateDevice(GUID_SysKeyboard, &m_InputDevice[m_dwInputDeviceCount].m_Device, NULL); + + if(!FAILED(hRet)) + { + m_InputDevice[m_dwInputDeviceCount].m_Flags = DEVICE_FLAG_KEYBOARD; + + m_InputDevice[m_dwInputDeviceCount++].m_Device->SetDataFormat(&c_dfDIKeyboard); + } + } + + if(m_CurrentState == XBCTRL_STATE_CONFIG || DeviceIsUsed("SysMouse")) + { + hRet = m_pDirectInput8->CreateDevice(GUID_SysMouse, &m_InputDevice[m_dwInputDeviceCount].m_Device, NULL); + + if(!FAILED(hRet)) + { + m_InputDevice[m_dwInputDeviceCount].m_Flags = DEVICE_FLAG_MOUSE; + + m_InputDevice[m_dwInputDeviceCount++].m_Device->SetDataFormat(&c_dfDIMouse2); + } + } + } + + // ****************************************************************** + // * Set cooperative level and acquire // ****************************************************************** { for(int v=m_dwInputDeviceCount-1;v>=0;v--) { - m_InputDevice[v].m_Device->Unacquire(); - m_InputDevice[v].m_Device->Release(); - m_InputDevice[v].m_Device = 0; - } + m_InputDevice[v].m_Device->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); + m_InputDevice[v].m_Device->Acquire(); - m_dwInputDeviceCount = 0; + HRESULT hRet = m_InputDevice[v].m_Device->Poll(); - if(m_pDirectInput8 != 0) - { - m_pDirectInput8->Release(); - m_pDirectInput8 = 0; + if(FAILED(hRet)) + { + hRet = m_InputDevice[v].m_Device->Acquire(); + + while(hRet == DIERR_INPUTLOST) + hRet = m_InputDevice[v].m_Device->Acquire(); + + if(hRet != DIERR_INPUTLOST) + break; + } } } + // ****************************************************************** + // * Enumerate Controller objects + // ****************************************************************** + for(m_dwCurObject=0;m_dwCurObjectEnumObjects(WrapEnumObjectsCallback, this, DIDFT_ALL); +} + +// ****************************************************************** +// * func: XBController::DInputCleanup +// ****************************************************************** +void XBController::DInputCleanup() +{ + for(int v=m_dwInputDeviceCount-1;v>=0;v--) + { + m_InputDevice[v].m_Device->Unacquire(); + m_InputDevice[v].m_Device->Release(); + m_InputDevice[v].m_Device = 0; + } + + m_dwInputDeviceCount = 0; + + if(m_pDirectInput8 != 0) + { + m_pDirectInput8->Release(); + m_pDirectInput8 = 0; + } + return; } @@ -443,8 +728,10 @@ void XBController::Map(XBCtrlObject object, const char *szDeviceName, int dwInfo bool inuse = false; for(int r=0;rtszInstanceName)) + return DIENUM_CONTINUE; + HRESULT hRet = m_pDirectInput8->CreateDevice(lpddi->guidInstance, &m_InputDevice[m_dwInputDeviceCount].m_Device, NULL); if(!FAILED(hRet)) @@ -493,6 +785,8 @@ BOOL XBController::EnumGameCtrlCallback(LPCDIDEVICEINSTANCE lpddi) m_InputDevice[m_dwInputDeviceCount++].m_Device->SetDataFormat(&c_dfDIJoystick); } + printf("Emu: Monitoring Device %s\n", lpddi->tszInstanceName); + return DIENUM_CONTINUE; }