// Callback that lists all gamepads. static BOOL CALLBACK gamepadCallback(LPCDIDEVICEINSTANCE device, LPVOID userData) { Impl* pimpl = static_cast<Impl*>(userData); IDirectInputDevice8* gamepad; if (FAILED(pimpl->input->CreateDevice(device->guidInstance, &gamepad, 0))) { return DIENUM_CONTINUE; } if (FAILED(gamepad->SetDataFormat(&c_dfDIJoystick)) || FAILED(gamepad->SetCooperativeLevel(pimpl->window, DISCL_EXCLUSIVE | DISCL_FOREGROUND)) || FAILED(gamepad->EnumObjects(axisCallback, gamepad, DIDFT_AXIS))) { gamepad->Release(); return DIENUM_CONTINUE; } pimpl->gamepads.push_back(Win::shareComPtr(gamepad)); return DIENUM_CONTINUE; }
Gosu::Input::Input(HWND window) : pimpl(new Impl) { pimpl->window = window; pimpl->mouseFactorX = pimpl->mouseFactorY = 1.0; // Create the main input object (only necessary for setup). IDirectInput8* inputRaw; Impl::check("creating the main DirectInput object", ::DirectInput8Create(Win::instance(), DIRECTINPUT_VERSION, IID_IDirectInput8, reinterpret_cast<void**>(&inputRaw), 0)); pimpl->input = Win::shareComPtr(inputRaw); // Prepare property struct for setting the amount of data to buffer. DIPROPDWORD bufferSize; bufferSize.diph.dwSize = sizeof(DIPROPDWORD); bufferSize.diph.dwHeaderSize = sizeof(DIPROPHEADER); bufferSize.diph.dwHow = DIPH_DEVICE; bufferSize.diph.dwObj = 0; bufferSize.dwData = Impl::inputBufferSize; // Set up the system keyboard. IDirectInputDevice8* kbRaw; Impl::check("creating the keyboard device object", pimpl->input->CreateDevice(GUID_SysKeyboard, &kbRaw, 0)); pimpl->keyboard = Win::shareComPtr(kbRaw); Impl::check("setting the keyboard's data format", kbRaw->SetDataFormat(&c_dfDIKeyboard)); Impl::check("setting the keyboard's cooperative level", kbRaw->SetCooperativeLevel(window, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)); Impl::check("setting the keyboard's buffer size", kbRaw->SetProperty(DIPROP_BUFFERSIZE, &bufferSize.diph)); kbRaw->Acquire(); // Set up the system mouse. IDirectInputDevice8* mouseRaw; Impl::check("creating the mouse device object", pimpl->input->CreateDevice(GUID_SysMouse, &mouseRaw, 0)); pimpl->mouse = Win::shareComPtr(mouseRaw); Impl::check("setting the mouse's data format", mouseRaw->SetDataFormat(&c_dfDIMouse)); Impl::check("setting the mouse's cooperative level", mouseRaw->SetCooperativeLevel(window, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)); Impl::check("setting the mouse's buffer size", mouseRaw->SetProperty(DIPROP_BUFFERSIZE, &bufferSize.diph)); mouseRaw->Acquire(); pimpl->swapMouse = ::GetSystemMetrics(SM_SWAPBUTTON) != 0; // Set up all gamepads. pimpl->input->EnumDevices(DI8DEVCLASS_GAMECTRL, Impl::gamepadCallback, pimpl.get(), DIEDFL_ATTACHEDONLY); // Get into a usable default state. pimpl->mouseX = pimpl->mouseY = 0; pimpl->updateMousePos(); buttons.assign(false); }
HRESULT _stdcall SetDataFormat(const DIDATAFORMAT* a) { return RealDevice->SetDataFormat(a); }