bool CMouse::SetExclusiveMode(bool value) { if (!GetDirectInputDevice()) return false; HRESULT hr; Unacquire(); if (value) { hr = GetDirectInputDevice()->SetCooperativeLevel(GetDXInput().GetHWnd(), DISCL_EXCLUSIVE | DISCL_FOREGROUND); } else { hr = GetDirectInputDevice()->SetCooperativeLevel(GetDXInput().GetHWnd(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); } if (FAILED(hr)) { gEnv->pLog->Log("Mouse:SetExclusiveMode(%s) failed\n", value?"true":"false"); return false; } return Acquire(); }
bool gkMouse::SetExclusiveMode(bool value) { if (!GetDirectInputDevice()) return false; HRESULT hr; Unacquire(); if (value) { hr = GetDirectInputDevice()->SetCooperativeLevel(GetInputManager().getHwnd(), DISCL_EXCLUSIVE | DISCL_FOREGROUND); } else { hr = GetDirectInputDevice()->SetCooperativeLevel(GetInputManager().getHwnd(), DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); } if (FAILED(hr)) { gkLogError(_T("Mouse:SetExclusiveMode(%s) failed\n"), value?"true":"false"); return false; } return Acquire(); }
// 開放 void CMglDirectInputDeviceBase::Release() { if( m_pDevice ) Unacquire(); // Release any DirectInput objects. SAFE_RELEASE( m_pDevice ); }
DInputDevice::~DInputDevice() { if (_did) { Unacquire(); _did->Release(); _did = nullptr; } if (_dataPacket) { delete[] _dataPacket; _dataPacket = nullptr; } }
void r3dKeyboard::ReleaseCapture() { if(!KbdAcquired) return; if(pdiKbd != NULL) { Unacquire(); pdiKbd->Release(); } KbdAcquired = 0; return; }
void DInputDevice::GetState(struct DeviceState &state) { if (_dataPacketSize == 0) { Log(Warn, "Device \"%S\" Data packet size is 0", _instanceName.c_str()); return; } bool wasAcquired = _acquired; if (!_acquired) { Acquire(); } _did->Poll(); ZeroMemory(_dataPacket, _dataPacketSize); HRESULT result = _did->GetDeviceState(_dataPacketSize, (LPVOID)_dataPacket); if (result != DI_OK) { if (result == DIERR_INPUTLOST) { _attached = false; Log(Debug, "Device \"%S\" disconnected", _instanceName.c_str()); } else { Log(Error, "Error reading device \"%S\" input", _instanceName.c_str()); } } else { int AxesOffset = ((_numButtons + 3) / 4) * 4; int POVsOffset = AxesOffset + _numAxes * 4; state.buttons.clear(); state.axes.clear(); state.povs.clear(); for (int i = 0; i < _numButtons; i++) { state.buttons.push_back(_dataPacket[i] != 0); } for (int i = 0; i < _numAxes; i++) { float axisValue = float(((int *)(_dataPacket + AxesOffset))[i]); int minVal = _infoList.axes[i].min; int maxVal = _infoList.axes[i].max; axisValue = 2.0f * (axisValue - minVal) / float(maxVal - minVal) - 1.0f; state.axes.push_back(axisValue); } for (int i = 0; i < _numPOVs; i++) { state.axes.push_back(((int *)(_dataPacket + POVsOffset))[i]); } } if (!wasAcquired) { Unacquire(); } }
/*------------------------------------------------------------------------------*/ void AkindDI::Message( UINT msg, WPARAM wParam, LPARAM lParam ) { switch ( msg ){ case WM_ACTIVATE: if ( m_BackGround == FALSE ){ if ( WA_INACTIVE == wParam ){ Unacquire(); } else { Acquire(); } } break; case WM_DEVICECHANGE: switch ( wParam ){ case DBT_DEVICEARRIVAL : JoyAdd(); break; case DBT_DEVICEREMOVECOMPLETE : JoyDel(); break; } break; } }
/*------------------------------------------------------------------------------*/ void AkindDI::Message( UINT msg, WPARAM wParam, LPARAM lParam ) { switch ( msg ){ case WM_ACTIVATE: if ( m_BackGround == FALSE ){ if ( WA_INACTIVE == wParam ){ Unacquire(); } else { Acquire(); } } break; case WM_DEVICECHANGE: if (wParam == DBT_DEVICEARRIVAL && IsInputDevice((DEV_BROADCAST_HDR*)lParam)) { JoyAdd(); } else if (wParam == DBT_DEVICEREMOVECOMPLETE && IsInputDevice((DEV_BROADCAST_HDR*)lParam)) { JoyDel(); } break; } }
void DInputDevice::SetCooperativeLevel(bool foreground, bool exclusive) { HRESULT result; bool wasAcquired = _acquired; if (_acquired) { Unacquire(); } DWORD flags = 0; flags |= foreground ? DISCL_FOREGROUND : DISCL_BACKGROUND; flags |= exclusive ? DISCL_EXCLUSIVE : DISCL_NONEXCLUSIVE; result = _did->SetCooperativeLevel(_hwnd, flags); if (result != DI_OK) { Log(Error, "Could not set direct input cooperative level for device \"%S\": 0x%08x", _instanceName.c_str(), result); } if (wasAcquired) { Acquire(); } }
bool CKeyboard::SetExclusiveMode(bool value) { Unacquire(); // get the current toggle key states /* if (GetKeyState(VK_NUMLOCK) & 0x01) { m_toggleState |= TOGGLE_NUMLOCK; m_cKeysState[DIK_NUMLOCK] |= 0x01; } else { m_cKeysState[DIK_NUMLOCK] &= ~0x01; } if (GetKeyState(VK_CAPITAL) & 0x01) { m_toggleState |= TOGGLE_CAPSLOCK; m_cKeysState[DIK_CAPSLOCK] |= 0x01; } else { m_cKeysState[DIK_CAPSLOCK] &= ~0x01; } if (GetKeyState(VK_SCROLL) & 0x01) { m_toggleState |= TOGGLE_SCROLLLOCK; m_cKeysState[DIK_SCROLL] |= 0x01; } else { m_cKeysState[DIK_SCROLL] &= ~0x01; }*/ HRESULT hr; if (value) { hr = GetDirectInputDevice()->SetCooperativeLevel(GetDXInput().GetHWnd(), DISCL_FOREGROUND|DISCL_EXCLUSIVE|DISCL_NOWINKEY); if (FAILED(hr)) { gEnv->pLog->LogToFile("Cannot Set Keyboard Exclusive Mode\n"); return false; } } else { hr = GetDirectInputDevice()->SetCooperativeLevel(GetDXInput().GetHWnd(), DISCL_FOREGROUND|DISCL_NONEXCLUSIVE|DISCL_NOWINKEY); if(FAILED(hr)) { gEnv->pLog->LogToFile("Cannot Set Keyboard Non-Exclusive Mode\n"); return false; } } if (!Acquire()) return false; return true; }
InputDeviceBase::~InputDeviceBase() { Unacquire(); m_pDevice->Release(); }
void DInputDevice::SetDataFormat() { HRESULT result; bool wasAcquired = _acquired; if (_acquired) { Unacquire(); } int numObjects = _numButtons + _numAxes + _numPOVs; int AxesOffset = ((_numButtons + 3) / 4) * 4; int POVsOffset = AxesOffset + _numAxes * 4; int dataPacketSize = POVsOffset + _numPOVs * 4; DIOBJECTDATAFORMAT *objectDataFormats = new DIOBJECTDATAFORMAT[numObjects]; ZeroMemory(objectDataFormats, sizeof(DIOBJECTDATAFORMAT) * numObjects); // Setup buttons for (int i = 0; i < _numButtons; i++) { objectDataFormats[i].pguid = 0; objectDataFormats[i].dwOfs = i; objectDataFormats[i].dwType = _infoList.buttons[i].type; objectDataFormats[i].dwFlags = 0; } // Setup axes for (int i = 0; i < _numAxes; i++) { int idx = _numButtons + i; objectDataFormats[idx].pguid = 0; objectDataFormats[idx].dwOfs = AxesOffset + i * 4; objectDataFormats[idx].dwType = _infoList.axes[i].type; objectDataFormats[idx].dwFlags = 0; } // Setup POVs for (int i = 0; i < _numPOVs; i++) { int idx = _numButtons + _numAxes + i; objectDataFormats[idx].pguid = 0; objectDataFormats[idx].dwOfs = POVsOffset + i * 4; objectDataFormats[idx].dwType = _infoList.povs[i].type; objectDataFormats[idx].dwFlags = 0; } DIDATAFORMAT dataFormat; ZeroMemory(&dataFormat, sizeof(DIDATAFORMAT)); dataFormat.dwSize = sizeof(DIDATAFORMAT); dataFormat.dwObjSize = sizeof(DIOBJECTDATAFORMAT); dataFormat.dwFlags = 0; dataFormat.dwDataSize = dataPacketSize; dataFormat.dwNumObjs = numObjects; dataFormat.rgodf = objectDataFormats; _dataPacketSize = dataPacketSize; if (_dataPacket) { delete[] _dataPacket; _dataPacket = nullptr; } _dataPacket = new unsigned char[_dataPacketSize]; ZeroMemory(_dataPacket, _dataPacketSize); result = _did->SetDataFormat(&dataFormat); if (result != DI_OK) { Log(Error, "Could not set device \"%S\" data format: 0x%08x", _instanceName.c_str(), result); _dataPacketSize = 0; } delete[] objectDataFormats; if (wasAcquired) { Acquire(); } }