1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
// 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);
DWORD processId;
GetWindowThreadProcessId(hwnd, &processId);
if (processId == GetCurrentProcessId())
{
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;
}
else
{
*x = (ControlState)1;
*y = (ControlState)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));
}
}
}
|