HRESULT _stdcall GetDeviceInfo(LPDIDEVICEINSTANCEW a) { return RealDevice->GetDeviceInfo(a); }
// **if passed an existing DirectInputDevice which matches gGuid // unacquires it, and sets its cooperative level (reinitialize) // **if the existing device does not match the passed gGuid, the existing device is released // **if no device was passed or gGuid did not match // searches for the controller matching gGuid in connected and available devices // creates a DirectInputDevice // sets its data format // sets its cooperative level // for joysticks, calls EnumSetObjectsAxis for each axis // GetInputDevice always leaves the returned device in an UNACQUIRED state. bool GetInputDevice( HWND hWnd, LPDIRECTINPUTDEVICE8 &lpDirectInputDevice, GUID gGuid, DWORD dwDevType, DWORD dwCooperativeLevel ) { DebugWriteA("GetInputDevice: gGuid is {%08.8lX-%04.4hX-%04.4hX-%02.2X%02.2X-%02.2X%02.2X%02.2X%02.2X%02.2X%02.2X}\n", gGuid.Data1, gGuid.Data2, gGuid.Data3, gGuid.Data4[0], gGuid.Data4[1], gGuid.Data4[2], gGuid.Data4[3], gGuid.Data4[4], gGuid.Data4[5], gGuid.Data4[6], gGuid.Data4[7]); if( lpDirectInputDevice != NULL) { DIDEVICEINSTANCE didDev; didDev.dwSize = sizeof(DIDEVICEINSTANCE); lpDirectInputDevice->GetDeviceInfo( &didDev ); if( didDev.guidInstance == gGuid ) { // we've already gotten this device; unacquire it and initialize DebugWriteA("GetInputDevice: already created, attempting to reinit\n"); lpDirectInputDevice->Unacquire(); lpDirectInputDevice->SetCooperativeLevel( hWnd, dwCooperativeLevel ); return true; } else ReleaseDevice( lpDirectInputDevice ); } HRESULT hResult; LPCDIDATAFORMAT ppDiDataFormat = NULL; bool Success = false; switch( LOBYTE(dwDevType) ) { case DI8DEVTYPE_KEYBOARD: ppDiDataFormat = &c_dfDIKeyboard; break; case DI8DEVTYPE_MOUSE: ppDiDataFormat = &c_dfDIMouse2; break; //case DI8DEVTYPE_GAMEPAD: //case DI8DEVTYPE_JOYSTICK: //case DI8DEVTYPE_DRIVING: //case DI8DEVTYPE_1STPERSON: //case DI8DEVTYPE_FLIGHT: default: // assume everything else is a gamepad; probably not the best idea but it works ppDiDataFormat = &c_dfDIJoystick; break; } bool bDeviceAvailable = false; VOID* aRef[2] = { &gGuid, &bDeviceAvailable }; // for each available device in our dwDevType category, run EnumIsDeviceAvailable with params "aRef" g_pDIHandle->EnumDevices( DI8DEVCLASS_ALL, EnumIsDeviceAvailable, (LPVOID)aRef, DIEDFL_ATTACHEDONLY ); if( !bDeviceAvailable ) { DebugWriteA("GetInputDevice: Device does not appear available\n"); return false; } hResult = g_pDIHandle->CreateDevice( gGuid, &lpDirectInputDevice, NULL ); if( SUCCEEDED( hResult )) { hResult = lpDirectInputDevice->SetDataFormat( ppDiDataFormat ); hResult = lpDirectInputDevice->SetCooperativeLevel( hWnd, dwCooperativeLevel ); Success = SUCCEEDED( hResult ); if (!Success) { DebugWriteA("GetInputDevice: SetCooperativeLevel failed\n"); } } else DebugWriteA("GetInputDevice: CreateDevice failed\n"); if( Success && ( ppDiDataFormat == &c_dfDIJoystick )) lpDirectInputDevice->EnumObjects( EnumSetObjectsAxis, lpDirectInputDevice, DIDFT_AXIS ); return Success; }