void Joystick_DX5::RequestExclusive(bool value) { if(value != have_exclusive_access) { if(value && SUCCEEDED(dev->SetCooperativeLevel(GetDesktopWindow(), DISCL_BACKGROUND | DISCL_EXCLUSIVE)) && SUCCEEDED(dev->Acquire())) have_exclusive_access = true; else { have_exclusive_access = false; dev->SetCooperativeLevel(GetDesktopWindow(), DISCL_BACKGROUND | DISCL_NONEXCLUSIVE); dev->Acquire(); } } }
bool InitJoystick (LPDIRECTINPUTDEVICE2 &pJoystick_) { HRESULT hr; // Set the joystick device to use the joystick format if (FAILED(hr = pJoystick_->SetDataFormat(&c_dfDIJoystick))) TRACE("!!! Failed to set data format of joystick device (%#08lx)\n", hr); else if (FAILED(hr = pJoystick_->SetCooperativeLevel(g_hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND))) TRACE("!!! Failed to set cooperative level of joystick device (%#08lx)\n", hr); else { // Deadzone tolerance percentage and range of each axis (-100 to +100) DIPROPDWORD diPdw = { { sizeof(diPdw), sizeof(diPdw.diph), 0, DIPH_BYOFFSET }, 10000UL * JOYSTICK_DEADZONE / 100UL }; DIPROPRANGE diPrg = { { sizeof(diPrg), sizeof(diPrg.diph), 0, DIPH_BYOFFSET }, -100, +100 }; int anAxes[] = { DIJOFS_X, DIJOFS_Y, DIJOFS_RX, DIJOFS_RY }; for (int i = 0 ; i < _countof(anAxes) ; i++) { diPdw.diph.dwObj = diPrg.diph.dwObj = anAxes[i]; if (FAILED(hr = pJoystick_->SetProperty(DIPROP_DEADZONE, &diPdw.diph))) TRACE("!!! Failed to set joystick deadzone (%#08lx)\n", hr); else if (FAILED(hr = pJoystick_->SetProperty(DIPROP_RANGE, &diPrg.diph))) TRACE("!!! Failed to set joystick range (%#08lx)\n", hr); } return true; } // Clean up pJoystick_->Release(); pJoystick_ = NULL; return false; }