diff options
| author | Jules Blok <jules.blok@gmail.com> | 2015-06-10 00:00:12 +0200 |
|---|---|---|
| committer | Jules Blok <jules.blok@gmail.com> | 2015-06-10 00:00:12 +0200 |
| commit | 7dfced21a2e5aac7195e0e1ad76468e30306766b (patch) | |
| tree | bd671b639d3d911460b767caabad8fc8a759a6bf /Source/Core/InputCommon/ControllerInterface/DInput | |
| parent | c25be031fc1031d795157d8344b87b7841145197 (diff) | |
| parent | 7b0a65e295c784f9d573f2ef52d4e2a7b9bb2889 (diff) | |
Merge branch 'master' into stable
Diffstat (limited to 'Source/Core/InputCommon/ControllerInterface/DInput')
10 files changed, 1090 insertions, 0 deletions
diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp b/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp new file mode 100644 index 0000000000..4a68474618 --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInput.cpp @@ -0,0 +1,65 @@ +// Copyright 2010 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "Common/StringUtil.h" + +#include "InputCommon/ControllerInterface/DInput/DInput.h" + +#include "InputCommon/ControllerInterface/DInput/DInputJoystick.h" +#include "InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h" + +#pragma comment(lib, "Dinput8.lib") +#pragma comment(lib, "dxguid.lib") + +namespace ciface +{ +namespace DInput +{ + +BOOL CALLBACK DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) +{ + ((std::list<DIDEVICEOBJECTINSTANCE>*)pvRef)->push_back(*lpddoi); + return DIENUM_CONTINUE; +} + +BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef) +{ + ((std::list<DIDEVICEINSTANCE>*)pvRef)->push_back(*lpddi); + return DIENUM_CONTINUE; +} + +std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device) +{ + DIPROPSTRING str = {}; + str.diph.dwSize = sizeof(str); + str.diph.dwHeaderSize = sizeof(str.diph); + str.diph.dwHow = DIPH_DEVICE; + + std::string result; + if (SUCCEEDED(device->GetProperty(DIPROP_PRODUCTNAME, &str.diph))) + { + result = StripSpaces(UTF16ToUTF8(str.wsz)); + } + + return result; +} + +void Init(std::vector<Core::Device*>& devices, HWND hwnd) +{ + IDirectInput8* idi8; + if (FAILED(DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, + IID_IDirectInput8, (LPVOID*)&idi8, nullptr))) + { + return; + } + + InitKeyboardMouse(idi8, devices, hwnd); + InitJoystick(idi8, devices, hwnd); + + idi8->Release(); + +} + +} +} diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInput.h b/Source/Core/InputCommon/ControllerInterface/DInput/DInput.h new file mode 100644 index 0000000000..1823499e98 --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInput.h @@ -0,0 +1,28 @@ +// Copyright 2010 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#define DINPUT_SOURCE_NAME "DInput" + +#include <list> +#include <windows.h> + +#include "InputCommon/ControllerInterface/Device.h" +#include "InputCommon/ControllerInterface/DInput/DInput8.h" + +namespace ciface +{ +namespace DInput +{ + +//BOOL CALLBACK DIEnumEffectsCallback(LPCDIEFFECTINFO pdei, LPVOID pvRef); +BOOL CALLBACK DIEnumDeviceObjectsCallback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef); +BOOL CALLBACK DIEnumDevicesCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef); +std::string GetDeviceName(const LPDIRECTINPUTDEVICE8 device); + +void Init(std::vector<Core::Device*>& devices, HWND hwnd); + +} +} diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInput8.h b/Source/Core/InputCommon/ControllerInterface/DInput/DInput8.h new file mode 100644 index 0000000000..4a32edb632 --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInput8.h @@ -0,0 +1,8 @@ +// Copyright 2008 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#define DIRECTINPUT_VERSION 0x0800 +#include <dinput.h> diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.cpp b/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.cpp new file mode 100644 index 0000000000..aa1e972126 --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.cpp @@ -0,0 +1,281 @@ +// Copyright 2010 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include <algorithm> +#include <map> +#include <sstream> + +#include "InputCommon/ControllerInterface/DInput/DInput.h" +#include "InputCommon/ControllerInterface/DInput/DInputJoystick.h" +#include "InputCommon/ControllerInterface/DInput/XInputFilter.h" + +namespace ciface +{ +namespace DInput +{ + +#define DATA_BUFFER_SIZE 32 + +void InitJoystick(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND hwnd) +{ + std::list<DIDEVICEINSTANCE> joysticks; + idi8->EnumDevices(DI8DEVCLASS_GAMECTRL, DIEnumDevicesCallback, (LPVOID)&joysticks, DIEDFL_ATTACHEDONLY); + + // this is used to number the joysticks + // multiple joysticks with the same name shall get unique ids starting at 0 + std::map< std::basic_string<TCHAR>, int> name_counts; + + std::vector<DWORD> xinput_guids; + GetXInputGUIDS(&xinput_guids); + + for (DIDEVICEINSTANCE& joystick : joysticks) + { + // skip XInput Devices + if (std::find(xinput_guids.begin(), xinput_guids.end(), joystick.guidProduct.Data1) != xinput_guids.end()) + { + continue; + } + + LPDIRECTINPUTDEVICE8 js_device; + if (SUCCEEDED(idi8->CreateDevice(joystick.guidInstance, &js_device, nullptr))) + { + if (SUCCEEDED(js_device->SetDataFormat(&c_dfDIJoystick))) + { + if (FAILED(js_device->SetCooperativeLevel(GetAncestor(hwnd, GA_ROOT), DISCL_BACKGROUND | DISCL_EXCLUSIVE))) + { + //PanicAlert("SetCooperativeLevel(DISCL_EXCLUSIVE) failed!"); + // fall back to non-exclusive mode, with no rumble + if (FAILED(js_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) + { + //PanicAlert("SetCooperativeLevel failed!"); + js_device->Release(); + continue; + } + } + + Joystick* js = new Joystick(/*&*i, */js_device, name_counts[joystick.tszInstanceName]++); + // only add if it has some inputs/outputs + if (js->Inputs().size() || js->Outputs().size()) + devices.push_back(js); + else + delete js; + } + else + { + //PanicAlert("SetDataFormat failed!"); + js_device->Release(); + } + } + } +} + +Joystick::Joystick( /*const LPCDIDEVICEINSTANCE lpddi, */const LPDIRECTINPUTDEVICE8 device, const unsigned int index ) + : m_device(device) + , m_index(index) + //, m_name(TStringToString(lpddi->tszInstanceName)) +{ + // seems this needs to be done before GetCapabilities + // polled or buffered data + DIPROPDWORD dipdw; + dipdw.diph.dwSize = sizeof(DIPROPDWORD); + dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER); + dipdw.diph.dwObj = 0; + dipdw.diph.dwHow = DIPH_DEVICE; + dipdw.dwData = DATA_BUFFER_SIZE; + // set the buffer size, + // if we can't set the property, we can't use buffered data + m_buffered = SUCCEEDED(m_device->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)); + + // seems this needs to be done after SetProperty of buffer size + m_device->Acquire(); + + // get joystick caps + DIDEVCAPS js_caps; + js_caps.dwSize = sizeof(js_caps); + if (FAILED(m_device->GetCapabilities(&js_caps))) + return; + + // max of 32 buttons and 4 hats / the limit of the data format I am using + js_caps.dwButtons = std::min((DWORD)32, js_caps.dwButtons); + js_caps.dwPOVs = std::min((DWORD)4, js_caps.dwPOVs); + + //m_must_poll = (js_caps.dwFlags & DIDC_POLLEDDATAFORMAT) != 0; + + // buttons + for (u8 i = 0; i != js_caps.dwButtons; ++i) + AddInput(new Button(i, m_state_in.rgbButtons[i])); + + // hats + for (u8 i = 0; i != js_caps.dwPOVs; ++i) + { + // each hat gets 4 input instances associated with it, (up down left right) + for (u8 d = 0; d != 4; ++d) + AddInput(new Hat(i, m_state_in.rgdwPOV[i], d)); + } + + // get up to 6 axes and 2 sliders + DIPROPRANGE range; + range.diph.dwSize = sizeof(range); + range.diph.dwHeaderSize = sizeof(range.diph); + range.diph.dwHow = DIPH_BYOFFSET; + // screw EnumObjects, just go through all the axis offsets and try to GetProperty + // this should be more foolproof, less code, and probably faster + for (unsigned int offset = 0; offset < DIJOFS_BUTTON(0) / sizeof(LONG); ++offset) + { + range.diph.dwObj = offset * sizeof(LONG); + // try to set some nice power of 2 values (128) to match the GameCube controls + range.lMin = -(1 << 7); + range.lMax = (1 << 7); + m_device->SetProperty(DIPROP_RANGE, &range.diph); + // but I guess not all devices support setting range + // so I getproperty right afterward incase it didn't set. + // This also checks that the axis is present + if (SUCCEEDED(m_device->GetProperty(DIPROP_RANGE, &range.diph))) + { + const LONG base = (range.lMin + range.lMax) / 2; + const LONG& ax = (&m_state_in.lX)[offset]; + + // each axis gets a negative and a positive input instance associated with it + AddAnalogInputs(new Axis(offset, ax, base, range.lMin-base), + new Axis(offset, ax, base, range.lMax-base)); + } + } + + // force feedback + std::list<DIDEVICEOBJECTINSTANCE> objects; + if (SUCCEEDED(m_device->EnumObjects(DIEnumDeviceObjectsCallback, (LPVOID)&objects, DIDFT_AXIS))) + { + InitForceFeedback(m_device, (int)objects.size()); + } + + ZeroMemory(&m_state_in, sizeof(m_state_in)); + // set hats to center + memset(m_state_in.rgdwPOV, 0xFF, sizeof(m_state_in.rgdwPOV)); +} + +Joystick::~Joystick() +{ + m_device->Unacquire(); + m_device->Release(); +} + +std::string Joystick::GetName() const +{ + return GetDeviceName(m_device); +} + +int Joystick::GetId() const +{ + return m_index; +} + +std::string Joystick::GetSource() const +{ + return DINPUT_SOURCE_NAME; +} + +// update IO + +void Joystick::UpdateInput() +{ + HRESULT hr = 0; + + // just always poll, + // MSDN says if this isn't needed it doesn't do anything + m_device->Poll(); + + if (m_buffered) + { + DIDEVICEOBJECTDATA evtbuf[DATA_BUFFER_SIZE]; + DWORD numevents = DATA_BUFFER_SIZE; + hr = m_device->GetDeviceData(sizeof(*evtbuf), evtbuf, &numevents, 0); + + if (SUCCEEDED(hr)) + { + for (LPDIDEVICEOBJECTDATA evt = evtbuf; evt != (evtbuf + numevents); ++evt) + { + // all the buttons are at the end of the data format + // they are bytes rather than longs + if (evt->dwOfs < DIJOFS_BUTTON(0)) + *(DWORD*)(((BYTE*)&m_state_in) + evt->dwOfs) = evt->dwData; + else + ((BYTE*)&m_state_in)[evt->dwOfs] = (BYTE)evt->dwData; + } + + // seems like this needs to be done maybe... + if (DI_BUFFEROVERFLOW == hr) + hr = m_device->GetDeviceState(sizeof(m_state_in), &m_state_in); + } + } + else + { + hr = m_device->GetDeviceState(sizeof(m_state_in), &m_state_in); + } + + // try reacquire if input lost + if (DIERR_INPUTLOST == hr || DIERR_NOTACQUIRED == hr) + m_device->Acquire(); +} + +// get name + +std::string Joystick::Button::GetName() const +{ + std::ostringstream ss; + ss << "Button " << (int)m_index; + return ss.str(); +} + +std::string Joystick::Axis::GetName() const +{ + std::ostringstream ss; + // axis + if (m_index < 6) + { + ss << "Axis " << (char)('X' + (m_index % 3)); + if (m_index > 2) + ss << 'r'; + } + // slider + else + { + ss << "Slider " << (int)(m_index - 6); + } + + ss << (m_range < 0 ? '-' : '+'); + return ss.str(); +} + +std::string Joystick::Hat::GetName() const +{ + static char tmpstr[] = "Hat . ."; + tmpstr[4] = (char)('0' + m_index); + tmpstr[6] = "NESW"[m_direction]; + return tmpstr; +} + +// get / set state + +ControlState Joystick::Axis::GetState() const +{ + return std::max(0.0, ControlState(m_axis - m_base) / m_range); +} + +ControlState Joystick::Button::GetState() const +{ + return ControlState(m_button > 0); +} + +ControlState Joystick::Hat::GetState() const +{ + // can this func be simplified ? + // hat centered code from MSDN + if (0xFFFF == LOWORD(m_hat)) + return 0; + + return (abs((int)(m_hat / 4500 - m_direction * 2 + 8) % 8 - 4) > 2); +} + +} +} diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.h b/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.h new file mode 100644 index 0000000000..c89ba509d3 --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInputJoystick.h @@ -0,0 +1,74 @@ +// Copyright 2010 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include "InputCommon/ControllerInterface/Device.h" +#include "InputCommon/ControllerInterface/ForceFeedback/ForceFeedbackDevice.h" + +namespace ciface +{ +namespace DInput +{ + +void InitJoystick(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND hwnd); + +class Joystick : public ForceFeedback::ForceFeedbackDevice +{ +private: + class Button : public Input + { + public: + std::string GetName() const; + Button(u8 index, const BYTE& button) : m_index(index), m_button(button) {} + ControlState GetState() const; + private: + const BYTE& m_button; + const u8 m_index; + }; + + class Axis : public Input + { + public: + std::string GetName() const; + Axis(u8 index, const LONG& axis, LONG base, LONG range) : m_index(index), m_axis(axis), m_base(base), m_range(range) {} + ControlState GetState() const; + private: + const LONG& m_axis; + const LONG m_base, m_range; + const u8 m_index; + }; + + class Hat : public Input + { + public: + std::string GetName() const; + Hat(u8 index, const DWORD& hat, u8 direction) : m_index(index), m_hat(hat), m_direction(direction) {} + ControlState GetState() const; + private: + const DWORD& m_hat; + const u8 m_index, m_direction; + }; + +public: + void UpdateInput() override; + + Joystick(const LPDIRECTINPUTDEVICE8 device, const unsigned int index); + ~Joystick(); + + std::string GetName() const; + int GetId() const; + std::string GetSource() const; + +private: + const LPDIRECTINPUTDEVICE8 m_device; + const unsigned int m_index; + + DIJOYSTATE m_state_in; + + bool m_buffered; +}; + +} +} diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp b/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp new file mode 100644 index 0000000000..6db8a86a3a --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.cpp @@ -0,0 +1,247 @@ +// Copyright 2010 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include <algorithm> + +#include "InputCommon/ControllerInterface/DInput/DInput.h" +#include "InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h" + + // (lower would be more sensitive) user can lower sensitivity by setting range + // seems decent here ( at 8 ), I don't think anyone would need more sensitive than this + // and user can lower it much farther than they would want to with the range +#define MOUSE_AXIS_SENSITIVITY 8 + + // if input hasn't been received for this many ms, mouse input will be skipped + // otherwise it is just some crazy value +#define DROP_INPUT_TIME 250 + +namespace ciface +{ +namespace DInput +{ + +static const struct +{ + const BYTE code; + const char* const name; +} named_keys[] = +{ +#include "InputCommon/ControllerInterface/DInput/NamedKeys.h" // NOLINT +}; + +// lil silly +static HWND m_hwnd; + +void InitKeyboardMouse(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND _hwnd) +{ + m_hwnd = _hwnd; + + // mouse and keyboard are a combined device, to allow shift+click and stuff + // if that's dumb, I will make a VirtualDevice class that just uses ranges of inputs/outputs from other devices + // so there can be a separated Keyboard and mouse, as well as combined KeyboardMouse + + LPDIRECTINPUTDEVICE8 kb_device = nullptr; + LPDIRECTINPUTDEVICE8 mo_device = nullptr; + + if (SUCCEEDED(idi8->CreateDevice( GUID_SysKeyboard, &kb_device, nullptr))) + { + if (SUCCEEDED(kb_device->SetDataFormat(&c_dfDIKeyboard))) + { + if (SUCCEEDED(kb_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) + { + if (SUCCEEDED(idi8->CreateDevice( GUID_SysMouse, &mo_device, nullptr ))) + { + if (SUCCEEDED(mo_device->SetDataFormat(&c_dfDIMouse2))) + { + if (SUCCEEDED(mo_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) + { + devices.push_back(new KeyboardMouse(kb_device, mo_device)); + return; + } + } + } + } + } + } + + if (kb_device) + kb_device->Release(); + if (mo_device) + mo_device->Release(); +} + +KeyboardMouse::~KeyboardMouse() +{ + // kb + m_kb_device->Unacquire(); + m_kb_device->Release(); + // mouse + m_mo_device->Unacquire(); + m_mo_device->Release(); +} + +KeyboardMouse::KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device, const LPDIRECTINPUTDEVICE8 mo_device) + : m_kb_device(kb_device) + , m_mo_device(mo_device) +{ + m_kb_device->Acquire(); + m_mo_device->Acquire(); + + m_last_update = GetTickCount(); + + ZeroMemory(&m_state_in, sizeof(m_state_in)); + + // KEYBOARD + // add keys + for (u8 i = 0; i < sizeof(named_keys)/sizeof(*named_keys); ++i) + AddInput(new Key(i, m_state_in.keyboard[named_keys[i].code])); + + // MOUSE + // get caps + DIDEVCAPS mouse_caps; + ZeroMemory( &mouse_caps, sizeof(mouse_caps) ); + mouse_caps.dwSize = sizeof(mouse_caps); + m_mo_device->GetCapabilities(&mouse_caps); + // mouse buttons + for (u8 i = 0; i < mouse_caps.dwButtons; ++i) + AddInput(new Button(i, m_state_in.mouse.rgbButtons[i])); + // mouse axes + for (unsigned int i = 0; i < mouse_caps.dwAxes; ++i) + { + const LONG& ax = (&m_state_in.mouse.lX)[i]; + + // each axis gets a negative and a positive input instance associated with it + AddInput(new Axis(i, ax, (2==i) ? -1 : -MOUSE_AXIS_SENSITIVITY)); + AddInput(new Axis(i, ax, -(2==i) ? 1 : MOUSE_AXIS_SENSITIVITY)); + } + // cursor, with a hax for-loop + for (unsigned int i=0; i<4; ++i) + AddInput(new Cursor(!!(i&2), (&m_state_in.cursor.x)[i/2], !!(i&1))); +} + +void GetMousePos(ControlState* const x, ControlState* const y) +{ + POINT point = { 1, 1 }; + GetCursorPos(&point); + // Get the cursor position relative to the upper left corner of the current window (separate or render to main) + HWND hwnd = WindowFromPoint(point); + ScreenToClient(hwnd, &point); + + // Get the size of the current window. (In my case Rect.top and Rect.left was zero.) + RECT rect; + GetClientRect(hwnd, &rect); + // Width and height is the size of the rendering window + unsigned int win_width = rect.right - rect.left; + unsigned int win_height = rect.bottom - rect.top; + + // Return the mouse position as a range from -1 to 1 + *x = (ControlState)point.x / (ControlState)win_width * 2 - 1; + *y = (ControlState)point.y / (ControlState)win_height * 2 - 1; +} + +void KeyboardMouse::UpdateInput() +{ + DIMOUSESTATE2 tmp_mouse; + + // if mouse position hasn't been updated in a short while, skip a dev state + DWORD cur_time = GetTickCount(); + if (cur_time - m_last_update > DROP_INPUT_TIME) + { + // set axes to zero + ZeroMemory(&m_state_in.mouse, sizeof(m_state_in.mouse)); + // skip this input state + m_mo_device->GetDeviceState(sizeof(tmp_mouse), &tmp_mouse); + } + + m_last_update = cur_time; + + HRESULT kb_hr = m_kb_device->GetDeviceState(sizeof(m_state_in.keyboard), &m_state_in.keyboard); + HRESULT mo_hr = m_mo_device->GetDeviceState(sizeof(tmp_mouse), &tmp_mouse); + + if (DIERR_INPUTLOST == kb_hr || DIERR_NOTACQUIRED == kb_hr) + m_kb_device->Acquire(); + + if (DIERR_INPUTLOST == mo_hr || DIERR_NOTACQUIRED == mo_hr) + m_mo_device->Acquire(); + + if (SUCCEEDED(kb_hr) && SUCCEEDED(mo_hr)) + { + // need to smooth out the axes, otherwise it doesn't work for shit + for (unsigned int i = 0; i < 3; ++i) + ((&m_state_in.mouse.lX)[i] += (&tmp_mouse.lX)[i]) /= 2; + + // copy over the buttons + memcpy(m_state_in.mouse.rgbButtons, tmp_mouse.rgbButtons, sizeof(m_state_in.mouse.rgbButtons)); + + // update mouse cursor + GetMousePos(&m_state_in.cursor.x, &m_state_in.cursor.y); + } +} + +std::string KeyboardMouse::GetName() const +{ + return "Keyboard Mouse"; +} + +int KeyboardMouse::GetId() const +{ + // should this be -1, idk + return 0; +} + +std::string KeyboardMouse::GetSource() const +{ + return DINPUT_SOURCE_NAME; +} + +// names +std::string KeyboardMouse::Key::GetName() const +{ + return named_keys[m_index].name; +} + +std::string KeyboardMouse::Button::GetName() const +{ + return std::string("Click ") + char('0' + m_index); +} + +std::string KeyboardMouse::Axis::GetName() const +{ + static char tmpstr[] = "Axis .."; + tmpstr[5] = (char)('X' + m_index); + tmpstr[6] = (m_range<0 ? '-' : '+'); + return tmpstr; +} + +std::string KeyboardMouse::Cursor::GetName() const +{ + static char tmpstr[] = "Cursor .."; + tmpstr[7] = (char)('X' + m_index); + tmpstr[8] = (m_positive ? '+' : '-'); + return tmpstr; +} + +// get/set state +ControlState KeyboardMouse::Key::GetState() const +{ + return (m_key != 0); +} + +ControlState KeyboardMouse::Button::GetState() const +{ + return (m_button != 0); +} + +ControlState KeyboardMouse::Axis::GetState() const +{ + return std::max(0.0, ControlState(m_axis) / m_range); +} + +ControlState KeyboardMouse::Cursor::GetState() const +{ + return std::max(0.0, ControlState(m_axis) / (m_positive ? 1.0 : -1.0)); +} + +} +} diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h b/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h new file mode 100644 index 0000000000..db4d363df0 --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h @@ -0,0 +1,98 @@ +// Copyright 2010 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include <windows.h> + +#include "InputCommon/ControllerInterface/Device.h" +#include "InputCommon/ControllerInterface/DInput/DInput8.h" + +namespace ciface +{ +namespace DInput +{ + +void InitKeyboardMouse(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND _hwnd); + +class KeyboardMouse : public Core::Device +{ +private: + struct State + { + BYTE keyboard[256]; + DIMOUSESTATE2 mouse; + struct + { + ControlState x, y; + } cursor; + }; + + class Key : public Input + { + public: + std::string GetName() const; + Key(u8 index, const BYTE& key) : m_index(index), m_key(key) {} + ControlState GetState() const; + private: + const BYTE& m_key; + const u8 m_index; + }; + + class Button : public Input + { + public: + std::string GetName() const; + Button(u8 index, const BYTE& button) : m_index(index), m_button(button) {} + ControlState GetState() const; + private: + const BYTE& m_button; + const u8 m_index; + }; + + class Axis : public Input + { + public: + std::string GetName() const; + Axis(u8 index, const LONG& axis, LONG range) : m_index(index), m_axis(axis), m_range(range) {} + ControlState GetState() const; + private: + const LONG& m_axis; + const LONG m_range; + const u8 m_index; + }; + + class Cursor : public Input + { + public: + std::string GetName() const; + bool IsDetectable() { return false; } + Cursor(u8 index, const ControlState& axis, const bool positive) : m_index(index), m_axis(axis), m_positive(positive) {} + ControlState GetState() const; + private: + const ControlState& m_axis; + const u8 m_index; + const bool m_positive; + }; + +public: + void UpdateInput() override; + + KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device, const LPDIRECTINPUTDEVICE8 mo_device); + ~KeyboardMouse(); + + std::string GetName() const; + int GetId() const; + std::string GetSource() const; + +private: + const LPDIRECTINPUTDEVICE8 m_kb_device; + const LPDIRECTINPUTDEVICE8 m_mo_device; + + DWORD m_last_update; + State m_state_in; +}; + +} +} diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/NamedKeys.h b/Source/Core/InputCommon/ControllerInterface/DInput/NamedKeys.h new file mode 100644 index 0000000000..6802fd7888 --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/DInput/NamedKeys.h @@ -0,0 +1,148 @@ +// Copyright 2010 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +{ DIK_A, "A" }, +{ DIK_B, "B" }, +{ DIK_C, "C" }, +{ DIK_D, "D" }, +{ DIK_E, "E" }, +{ DIK_F, "F" }, +{ DIK_G, "G" }, +{ DIK_H, "H" }, +{ DIK_I, "I" }, +{ DIK_J, "J" }, +{ DIK_K, "K" }, +{ DIK_L, "L" }, +{ DIK_M, "M" }, +{ DIK_N, "N" }, +{ DIK_O, "O" }, +{ DIK_P, "P" }, +{ DIK_Q, "Q" }, +{ DIK_R, "R" }, +{ DIK_S, "S" }, +{ DIK_T, "T" }, +{ DIK_U, "U" }, +{ DIK_V, "V" }, +{ DIK_W, "W" }, +{ DIK_X, "X" }, +{ DIK_Y, "Y" }, +{ DIK_Z, "Z" }, +{ DIK_0, "0" }, +{ DIK_1, "1" }, +{ DIK_2, "2" }, +{ DIK_3, "3" }, +{ DIK_4, "4" }, +{ DIK_5, "5" }, +{ DIK_6, "6" }, +{ DIK_7, "7" }, +{ DIK_8, "8" }, +{ DIK_9, "9" }, +{ DIK_UP, "UP" }, +{ DIK_DOWN, "DOWN" }, +{ DIK_LEFT, "LEFT" }, +{ DIK_RIGHT, "RIGHT" }, +{ DIK_ABNT_C1, "ABNT_C1" }, +{ DIK_ABNT_C2, "ABNT_C2" }, +{ DIK_ADD, "ADD" }, +{ DIK_APOSTROPHE, "APOSTROPHE" }, +{ DIK_APPS, "APPS" }, +{ DIK_AT, "AT" }, +{ DIK_AX, "AX" }, +{ DIK_BACK, "BACK" }, +{ DIK_BACKSLASH, "BACKSLASH" }, +{ DIK_CALCULATOR, "CALCULATOR" }, +{ DIK_CAPITAL, "CAPITAL" }, +{ DIK_COLON, "COLON" }, +{ DIK_COMMA, "COMMA" }, +{ DIK_CONVERT, "CONVERT" }, +{ DIK_DECIMAL, "DECIMAL" }, +{ DIK_DELETE, "DELETE" }, +{ DIK_DIVIDE, "DIVIDE" }, +{ DIK_EQUALS, "EQUALS" }, +{ DIK_ESCAPE, "ESCAPE" }, +{ DIK_F1, "F1" }, +{ DIK_F2, "F2" }, +{ DIK_F3, "F3" }, +{ DIK_F4, "F4" }, +{ DIK_F5, "F5" }, +{ DIK_F6, "F6" }, +{ DIK_F7, "F7" }, +{ DIK_F8, "F8" }, +{ DIK_F9, "F9" }, +{ DIK_F10, "F10" }, +{ DIK_F11, "F11" }, +{ DIK_F12, "F12" }, +{ DIK_F13, "F13" }, +{ DIK_F14, "F14" }, +{ DIK_F15, "F15" }, +{ DIK_GRAVE, "GRAVE" }, +{ DIK_HOME, "HOME" }, +{ DIK_END, "END" }, +{ DIK_INSERT, "INSERT" }, +{ DIK_KANA, "KANA" }, +{ DIK_KANJI, "KANJI" }, +{ DIK_MAIL, "MAIL" }, +{ DIK_MEDIASELECT, "MEDIASELECT" }, +{ DIK_MEDIASTOP, "MEDIASTOP" }, +{ DIK_MINUS, "MINUS" }, +{ DIK_MULTIPLY, "MULTIPLY" }, +{ DIK_MUTE, "MUTE" }, +{ DIK_MYCOMPUTER, "MYCOMPUTER" }, +{ DIK_NEXTTRACK, "NEXTTRACK" }, +{ DIK_NOCONVERT, "NOCONVERT" }, +{ DIK_NUMLOCK, "NUMLOCK" }, +{ DIK_NUMPAD0, "NUMPAD0" }, +{ DIK_NUMPAD1, "NUMPAD1" }, +{ DIK_NUMPAD2, "NUMPAD2" }, +{ DIK_NUMPAD3, "NUMPAD3" }, +{ DIK_NUMPAD4, "NUMPAD4" }, +{ DIK_NUMPAD5, "NUMPAD5" }, +{ DIK_NUMPAD6, "NUMPAD6" }, +{ DIK_NUMPAD7, "NUMPAD7" }, +{ DIK_NUMPAD8, "NUMPAD8" }, +{ DIK_NUMPAD9, "NUMPAD9" }, +{ DIK_NUMPADCOMMA, "NUMPADCOMMA" }, +{ DIK_NUMPADENTER, "NUMPADENTER" }, +{ DIK_NUMPADEQUALS, "NUMPADEQUALS" }, +{ DIK_OEM_102, "OEM_102" }, +{ DIK_PAUSE, "PAUSE" }, +{ DIK_PERIOD, "PERIOD" }, +{ DIK_PLAYPAUSE, "PLAYPAUSE" }, +{ DIK_POWER, "POWER" }, +{ DIK_PREVTRACK, "PREVTRACK" }, +{ DIK_PRIOR, "PRIOR" }, +{ DIK_NEXT, "NEXT" }, +{ DIK_RETURN, "RETURN" }, +{ DIK_LBRACKET, "LBRACKET" }, +{ DIK_RBRACKET, "RBRACKET" }, +{ DIK_LCONTROL, "LCONTROL" }, +{ DIK_RCONTROL, "RCONTROL" }, +{ DIK_LMENU, "LMENU" }, +{ DIK_RMENU, "RMENU" }, +{ DIK_LSHIFT, "LSHIFT" }, +{ DIK_RSHIFT, "RSHIFT" }, +{ DIK_LWIN, "LWIN" }, +{ DIK_RWIN, "RWIN" }, +{ DIK_SCROLL, "SCROLL" }, +{ DIK_SEMICOLON, "SEMICOLON" }, +{ DIK_SLASH, "SLASH" }, +{ DIK_SLEEP, "SLEEP" }, +{ DIK_SPACE, "SPACE" }, +{ DIK_STOP, "STOP" }, +{ DIK_SUBTRACT, "SUBTRACT" }, +{ DIK_SYSRQ, "SYSRQ" }, +{ DIK_TAB, "TAB" }, +{ DIK_UNDERLINE, "UNDERLINE" }, +{ DIK_UNLABELED, "UNLABELED" }, +{ DIK_VOLUMEDOWN, "VOLUMEDOWN" }, +{ DIK_VOLUMEUP, "VOLUMEUP" }, +{ DIK_WAKE, "WAKE" }, +{ DIK_WEBBACK, "WEBBACK" }, +{ DIK_WEBFAVORITES, "WEBFAVORITES" }, +{ DIK_WEBFORWARD, "WEBFORWARD" }, +{ DIK_WEBHOME, "WEBHOME" }, +{ DIK_WEBREFRESH, "WEBREFRESH" }, +{ DIK_WEBSEARCH, "WEBSEARCH" }, +{ DIK_WEBSTOP, "WEBSTOP" }, +{ DIK_YEN, "YEN" }, diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/XInputFilter.cpp b/Source/Core/InputCommon/ControllerInterface/DInput/XInputFilter.cpp new file mode 100644 index 0000000000..aedfc5dc44 --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/DInput/XInputFilter.cpp @@ -0,0 +1,123 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +// This function is contained in a separate file because WbemIdl.h pulls in files which break on +// /Zc:strictStrings, so this compilation unit is compiled without /Zc:strictStrings. + +#include <vector> +#include <WbemIdl.h> +#include <Windows.h> + +#define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p)=nullptr; } } + +namespace ciface +{ +namespace DInput +{ + +//----------------------------------------------------------------------------- +// Modified some MSDN code to get all the XInput device GUID.Data1 values in a vector, +// faster than checking all the devices for each DirectInput device, like MSDN says to do +//----------------------------------------------------------------------------- +void GetXInputGUIDS(std::vector<DWORD>* guids) +{ + IWbemLocator* pIWbemLocator = nullptr; + IEnumWbemClassObject* pEnumDevices = nullptr; + IWbemClassObject* pDevices[20] = {0}; + IWbemServices* pIWbemServices = nullptr; + BSTR bstrNamespace = nullptr; + BSTR bstrDeviceID = nullptr; + BSTR bstrClassName = nullptr; + DWORD uReturned = 0; + VARIANT var; + HRESULT hr; + + // CoInit if needed + hr = CoInitialize(nullptr); + bool bCleanupCOM = SUCCEEDED(hr); + + // Create WMI + hr = CoCreateInstance(__uuidof(WbemLocator), + nullptr, + CLSCTX_INPROC_SERVER, + __uuidof(IWbemLocator), + (LPVOID*) &pIWbemLocator); + if (FAILED(hr) || pIWbemLocator == nullptr) + goto LCleanup; + + bstrNamespace = SysAllocString(L"\\\\.\\root\\cimv2"); if (bstrNamespace == nullptr) goto LCleanup; + bstrClassName = SysAllocString(L"Win32_PNPEntity"); if (bstrClassName == nullptr) goto LCleanup; + bstrDeviceID = SysAllocString(L"DeviceID"); if (bstrDeviceID == nullptr) goto LCleanup; + + // Connect to WMI + hr = pIWbemLocator->ConnectServer(bstrNamespace, nullptr, nullptr, 0L, 0L, nullptr, nullptr, &pIWbemServices); + if (FAILED(hr) || pIWbemServices == nullptr) + goto LCleanup; + + // Switch security level to IMPERSONATE. + CoSetProxyBlanket(pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr, + RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE); + + hr = pIWbemServices->CreateInstanceEnum(bstrClassName, 0, nullptr, &pEnumDevices); + if (FAILED(hr) || pEnumDevices == nullptr) + goto LCleanup; + + // Loop over all devices + while (true) + { + // Get 20 at a time + hr = pEnumDevices->Next(10000, 20, pDevices, &uReturned); + if (FAILED(hr) || uReturned == 0) + break; + + for (UINT iDevice = 0; iDevice < uReturned; ++iDevice) + { + // For each device, get its device ID + hr = pDevices[iDevice]->Get(bstrDeviceID, 0L, &var, nullptr, nullptr); + if (SUCCEEDED(hr) && var.vt == VT_BSTR && var.bstrVal != nullptr) + { + // Check if the device ID contains "IG_". If it does, then it's an XInput device + // This information can not be found from DirectInput + if (wcsstr(var.bstrVal, L"IG_")) + { + // If it does, then get the VID/PID from var.bstrVal + DWORD dwPid = 0, dwVid = 0; + WCHAR* strVid = wcsstr(var.bstrVal, L"VID_"); + if (strVid && swscanf(strVid, L"VID_%4X", &dwVid) != 1) + dwVid = 0; + WCHAR* strPid = wcsstr(var.bstrVal, L"PID_"); + if (strPid && swscanf(strPid, L"PID_%4X", &dwPid) != 1) + dwPid = 0; + + // Compare the VID/PID to the DInput device + DWORD dwVidPid = MAKELONG(dwVid, dwPid); + guids->push_back(dwVidPid); + //bIsXinputDevice = true; + } + } + SAFE_RELEASE(pDevices[iDevice]); + } + } + +LCleanup: + if (bstrNamespace) + SysFreeString(bstrNamespace); + if (bstrDeviceID) + SysFreeString(bstrDeviceID); + if (bstrClassName) + SysFreeString(bstrClassName); + for (UINT iDevice = 0; iDevice < 20; iDevice++) + SAFE_RELEASE(pDevices[iDevice]); + SAFE_RELEASE(pEnumDevices); + SAFE_RELEASE(pIWbemLocator); + SAFE_RELEASE(pIWbemServices); + + if (bCleanupCOM) + CoUninitialize(); +} + +} +} + +#undef SAFE_RELEASE diff --git a/Source/Core/InputCommon/ControllerInterface/DInput/XInputFilter.h b/Source/Core/InputCommon/ControllerInterface/DInput/XInputFilter.h new file mode 100644 index 0000000000..6506dd156e --- /dev/null +++ b/Source/Core/InputCommon/ControllerInterface/DInput/XInputFilter.h @@ -0,0 +1,18 @@ +// Copyright 2014 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include <vector> +#include <Windows.h> + +namespace ciface +{ +namespace DInput +{ + +void GetXInputGUIDS(std::vector<DWORD>* guids); + +} +} |
