2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-07-03 08:04:10 +00:00
|
|
|
|
2020-04-21 04:16:07 +00:00
|
|
|
#include "InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h"
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
2014-02-19 01:17:31 +00:00
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2022-07-15 19:32:19 +00:00
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
#include "Core/Core.h"
|
2022-07-15 19:32:19 +00:00
|
|
|
#include "Core/Host.h"
|
2020-04-21 04:16:07 +00:00
|
|
|
|
2016-06-12 15:08:04 +00:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "InputCommon/ControllerInterface/DInput/DInput.h"
|
2010-07-03 08:04:10 +00:00
|
|
|
|
|
|
|
// (lower would be more sensitive) user can lower sensitivity by setting range
|
2013-04-19 13:21:45 +00:00
|
|
|
// seems decent here ( at 8 ), I don't think anyone would need more sensitive than this
|
2010-07-03 08:04:10 +00:00
|
|
|
// and user can lower it much farther than they would want to with the range
|
2014-01-31 00:51:21 +00:00
|
|
|
#define MOUSE_AXIS_SENSITIVITY 8
|
2010-07-03 08:04:10 +00:00
|
|
|
|
|
|
|
// if input hasn't been received for this many ms, mouse input will be skipped
|
|
|
|
// otherwise it is just some crazy value
|
2014-01-31 00:51:21 +00:00
|
|
|
#define DROP_INPUT_TIME 250
|
2010-07-03 08:04:10 +00:00
|
|
|
|
2019-06-17 20:39:24 +00:00
|
|
|
namespace ciface::DInput
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2020-04-21 04:16:07 +00:00
|
|
|
class RelativeMouseAxis final : public Core::Device::RelativeInput
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string GetName() const override
|
|
|
|
{
|
|
|
|
return fmt::format("RelativeMouse {}{}", char('X' + m_index), (m_scale > 0) ? '+' : '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
RelativeMouseAxis(u8 index, bool positive, const RelativeMouseState* state)
|
|
|
|
: m_state(*state), m_index(index), m_scale(positive * 2 - 1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlState GetState() const override
|
|
|
|
{
|
|
|
|
return ControlState(m_state.GetValue().data[m_index] * m_scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const RelativeMouseState& m_state;
|
|
|
|
const u8 m_index;
|
|
|
|
const s8 m_scale;
|
|
|
|
};
|
|
|
|
|
2010-07-26 05:30:50 +00:00
|
|
|
static const struct
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2014-01-31 00:51:21 +00:00
|
|
|
const BYTE code;
|
|
|
|
const char* const name;
|
2010-07-03 08:04:10 +00:00
|
|
|
} named_keys[] = {
|
2014-02-19 01:17:31 +00:00
|
|
|
#include "InputCommon/ControllerInterface/DInput/NamedKeys.h" // NOLINT
|
2010-07-03 08:04:10 +00:00
|
|
|
};
|
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
// Prevent duplicate keyboard/mouse devices. Modified by more threads.
|
|
|
|
static bool s_keyboard_mouse_exists;
|
|
|
|
static HWND s_hwnd;
|
2010-07-10 06:48:24 +00:00
|
|
|
|
2019-04-20 14:25:11 +00:00
|
|
|
void InitKeyboardMouse(IDirectInput8* const idi8, HWND hwnd)
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2019-03-09 15:57:37 +00:00
|
|
|
if (s_keyboard_mouse_exists)
|
|
|
|
return;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
s_hwnd = hwnd;
|
|
|
|
|
|
|
|
// Mouse and keyboard are a combined device, to allow shift+click and stuff
|
2013-04-19 13:21:45 +00:00
|
|
|
// if that's dumb, I will make a VirtualDevice class that just uses ranges of inputs/outputs from
|
|
|
|
// other devices
|
2010-07-03 08:04:10 +00:00
|
|
|
// so there can be a separated Keyboard and mouse, as well as combined KeyboardMouse
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
LPDIRECTINPUTDEVICE8 kb_device = nullptr;
|
|
|
|
LPDIRECTINPUTDEVICE8 mo_device = nullptr;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
// These are "virtual" system devices, so they are always there even if we have no physical
|
|
|
|
// mouse and keyboard plugged into the computer
|
2016-06-24 14:28:27 +00:00
|
|
|
if (SUCCEEDED(idi8->CreateDevice(GUID_SysKeyboard, &kb_device, nullptr)) &&
|
|
|
|
SUCCEEDED(kb_device->SetDataFormat(&c_dfDIKeyboard)) &&
|
|
|
|
SUCCEEDED(kb_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)) &&
|
|
|
|
SUCCEEDED(idi8->CreateDevice(GUID_SysMouse, &mo_device, nullptr)) &&
|
|
|
|
SUCCEEDED(mo_device->SetDataFormat(&c_dfDIMouse2)) &&
|
|
|
|
SUCCEEDED(mo_device->SetCooperativeLevel(nullptr, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2021-05-15 09:20:20 +00:00
|
|
|
g_controller_interface.AddDevice(std::make_shared<KeyboardMouse>(kb_device, mo_device));
|
2016-06-24 14:28:27 +00:00
|
|
|
return;
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "KeyboardMouse device failed to be created");
|
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
if (kb_device)
|
|
|
|
kb_device->Release();
|
|
|
|
if (mo_device)
|
|
|
|
mo_device->Release();
|
|
|
|
}
|
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
void SetKeyboardMouseWindow(HWND hwnd)
|
|
|
|
{
|
|
|
|
s_hwnd = hwnd;
|
|
|
|
}
|
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
KeyboardMouse::~KeyboardMouse()
|
|
|
|
{
|
2019-03-09 15:57:37 +00:00
|
|
|
s_keyboard_mouse_exists = false;
|
|
|
|
|
2021-05-15 08:32:00 +00:00
|
|
|
// Independently of the order in which we do these, if we put a breakpoint on Unacquire() (or in
|
|
|
|
// any place in the call stack before this), when refreshing devices from the UI, on the second
|
|
|
|
// attempt, it will get stuck in an infinite (while) loop inside dinput8.dll. Given that it can't
|
|
|
|
// be otherwise be reproduced (not even with sleeps), we can just ignore the problem.
|
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
// kb
|
|
|
|
m_kb_device->Unacquire();
|
|
|
|
m_kb_device->Release();
|
|
|
|
// mouse
|
|
|
|
m_mo_device->Unacquire();
|
|
|
|
m_mo_device->Release();
|
|
|
|
}
|
|
|
|
|
2010-07-10 06:48:24 +00:00
|
|
|
KeyboardMouse::KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device,
|
2021-05-15 09:20:20 +00:00
|
|
|
const LPDIRECTINPUTDEVICE8 mo_device)
|
|
|
|
: m_kb_device(kb_device), m_mo_device(mo_device), m_last_update(GetTickCount()), m_state_in()
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2019-03-09 15:57:37 +00:00
|
|
|
s_keyboard_mouse_exists = true;
|
|
|
|
|
2021-05-15 09:21:43 +00:00
|
|
|
if (FAILED(m_kb_device->Acquire()))
|
|
|
|
WARN_LOG_FMT(CONTROLLERINTERFACE, "Keyboard device failed to acquire. We'll retry later");
|
|
|
|
if (FAILED(m_mo_device->Acquire()))
|
|
|
|
WARN_LOG_FMT(CONTROLLERINTERFACE, "Mouse device failed to acquire. We'll retry later");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
// KEYBOARD
|
|
|
|
// add keys
|
2011-03-14 01:20:11 +00:00
|
|
|
for (u8 i = 0; i < sizeof(named_keys) / sizeof(*named_keys); ++i)
|
|
|
|
AddInput(new Key(i, m_state_in.keyboard[named_keys[i].code]));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-10-26 00:33:59 +00:00
|
|
|
// Add combined left/right modifiers with consistent naming across platforms.
|
|
|
|
AddCombinedInput("Alt", {"LMENU", "RMENU"});
|
|
|
|
AddCombinedInput("Shift", {"LSHIFT", "RSHIFT"});
|
|
|
|
AddCombinedInput("Ctrl", {"LCONTROL", "RCONTROL"});
|
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
// MOUSE
|
2019-04-20 14:25:11 +00:00
|
|
|
DIDEVCAPS mouse_caps = {};
|
2010-07-03 08:04:10 +00:00
|
|
|
mouse_caps.dwSize = sizeof(mouse_caps);
|
|
|
|
m_mo_device->GetCapabilities(&mouse_caps);
|
|
|
|
// mouse buttons
|
2011-03-14 01:20:11 +00:00
|
|
|
for (u8 i = 0; i < mouse_caps.dwButtons; ++i)
|
|
|
|
AddInput(new Button(i, m_state_in.mouse.rgbButtons[i]));
|
2010-07-03 08:04:10 +00:00
|
|
|
// mouse axes
|
2010-07-10 06:48:24 +00:00
|
|
|
for (unsigned int i = 0; i < mouse_caps.dwAxes; ++i)
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
const LONG& ax = (&m_state_in.mouse.lX)[i];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
// each axis gets a negative and a positive input instance associated with it
|
2011-03-14 01:20:11 +00:00
|
|
|
AddInput(new Axis(i, ax, (2 == i) ? -1 : -MOUSE_AXIS_SENSITIVITY));
|
|
|
|
AddInput(new Axis(i, ax, -(2 == i) ? 1 : MOUSE_AXIS_SENSITIVITY));
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
2020-04-21 04:16:07 +00:00
|
|
|
|
2010-07-10 06:48:24 +00:00
|
|
|
// cursor, with a hax for-loop
|
|
|
|
for (unsigned int i = 0; i < 4; ++i)
|
2011-03-14 01:20:11 +00:00
|
|
|
AddInput(new Cursor(!!(i & 2), (&m_state_in.cursor.x)[i / 2], !!(i & 1)));
|
2020-04-21 04:16:07 +00:00
|
|
|
|
|
|
|
// Raw relative mouse movement.
|
|
|
|
for (unsigned int i = 0; i != mouse_caps.dwAxes; ++i)
|
|
|
|
{
|
|
|
|
AddInput(new RelativeMouseAxis(i, false, &m_state_in.relative_mouse));
|
|
|
|
AddInput(new RelativeMouseAxis(i, true, &m_state_in.relative_mouse));
|
|
|
|
}
|
2010-07-10 06:48:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 14:25:11 +00:00
|
|
|
void KeyboardMouse::UpdateCursorInput()
|
2010-07-10 06:48:24 +00:00
|
|
|
{
|
2021-05-15 09:20:20 +00:00
|
|
|
// Get the size of the current window (in my case Rect.top and Rect.left was zero).
|
2019-04-20 14:25:11 +00:00
|
|
|
RECT rect;
|
2021-05-15 09:20:20 +00:00
|
|
|
GetClientRect(s_hwnd, &rect);
|
2019-04-20 14:25:11 +00:00
|
|
|
|
2021-01-02 16:33:13 +00:00
|
|
|
// Width and height are the size of the rendering window. They could be 0
|
|
|
|
const auto win_width = std::max(rect.right - rect.left, 1l);
|
|
|
|
const auto win_height = std::max(rect.bottom - rect.top, 1l);
|
2019-04-20 14:25:11 +00:00
|
|
|
|
2022-07-15 19:32:19 +00:00
|
|
|
POINT point = {};
|
|
|
|
if (g_controller_interface.IsMouseCenteringRequested() && Host_RendererHasFocus())
|
|
|
|
{
|
|
|
|
point.x = win_width / 2;
|
|
|
|
point.y = win_height / 2;
|
|
|
|
|
|
|
|
POINT screen_point = point;
|
|
|
|
ClientToScreen(s_hwnd, &screen_point);
|
|
|
|
SetCursorPos(screen_point.x, screen_point.y);
|
|
|
|
g_controller_interface.SetMouseCenteringRequested(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetCursorPos(&point);
|
|
|
|
|
|
|
|
// Get the cursor position relative to the upper left corner of the current window
|
|
|
|
// (separate or render to main)
|
|
|
|
ScreenToClient(s_hwnd, &point);
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:06:39 +00:00
|
|
|
const auto window_scale = g_controller_interface.GetWindowInputScale();
|
|
|
|
|
2019-04-20 14:25:11 +00:00
|
|
|
// Convert the cursor position to a range from -1 to 1.
|
2020-01-24 06:06:39 +00:00
|
|
|
m_state_in.cursor.x = (ControlState(point.x) / win_width * 2 - 1) * window_scale.x;
|
|
|
|
m_state_in.cursor.y = (ControlState(point.y) / win_height * 2 - 1) * window_scale.y;
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 08:55:14 +00:00
|
|
|
void KeyboardMouse::UpdateInput()
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2021-05-15 09:21:43 +00:00
|
|
|
UpdateCursorInput();
|
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
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
|
2019-04-20 14:25:11 +00:00
|
|
|
m_state_in.mouse = {};
|
2020-04-21 04:16:07 +00:00
|
|
|
m_state_in.relative_mouse = {};
|
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
// skip this input state
|
|
|
|
m_mo_device->GetDeviceState(sizeof(tmp_mouse), &tmp_mouse);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_last_update = cur_time;
|
|
|
|
|
|
|
|
HRESULT mo_hr = m_mo_device->GetDeviceState(sizeof(tmp_mouse), &tmp_mouse);
|
|
|
|
if (DIERR_INPUTLOST == mo_hr || DIERR_NOTACQUIRED == mo_hr)
|
2021-05-15 09:21:43 +00:00
|
|
|
{
|
|
|
|
INFO_LOG_FMT(CONTROLLERINTERFACE, "Mouse device failed to get state");
|
|
|
|
if (FAILED(m_mo_device->Acquire()))
|
|
|
|
INFO_LOG_FMT(CONTROLLERINTERFACE, "Mouse device failed to re-acquire, we'll retry later");
|
|
|
|
}
|
|
|
|
else if (SUCCEEDED(mo_hr))
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2020-04-21 04:16:07 +00:00
|
|
|
m_state_in.relative_mouse.Move({tmp_mouse.lX, tmp_mouse.lY, tmp_mouse.lZ});
|
|
|
|
m_state_in.relative_mouse.Update();
|
|
|
|
|
2013-04-19 13:21:45 +00:00
|
|
|
// need to smooth out the axes, otherwise it doesn't work for shit
|
2010-07-10 06:48:24 +00:00
|
|
|
for (unsigned int i = 0; i < 3; ++i)
|
2010-07-03 08:04:10 +00:00
|
|
|
((&m_state_in.mouse.lX)[i] += (&tmp_mouse.lX)[i]) /= 2;
|
|
|
|
|
|
|
|
// copy over the buttons
|
2020-04-21 04:16:07 +00:00
|
|
|
std::copy_n(tmp_mouse.rgbButtons, std::size(tmp_mouse.rgbButtons), m_state_in.mouse.rgbButtons);
|
2021-05-15 09:21:43 +00:00
|
|
|
}
|
2010-07-10 06:48:24 +00:00
|
|
|
|
2021-05-15 09:21:43 +00:00
|
|
|
HRESULT kb_hr = m_kb_device->GetDeviceState(sizeof(m_state_in.keyboard), &m_state_in.keyboard);
|
|
|
|
if (kb_hr == DIERR_INPUTLOST || kb_hr == DIERR_NOTACQUIRED)
|
|
|
|
{
|
|
|
|
INFO_LOG_FMT(CONTROLLERINTERFACE, "Keyboard device failed to get state");
|
|
|
|
if (SUCCEEDED(m_kb_device->Acquire()))
|
|
|
|
m_kb_device->GetDeviceState(sizeof(m_state_in.keyboard), &m_state_in.keyboard);
|
|
|
|
else
|
|
|
|
INFO_LOG_FMT(CONTROLLERINTERFACE, "Keyboard device failed to re-acquire, we'll retry later");
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string KeyboardMouse::GetName() const
|
|
|
|
{
|
|
|
|
return "Keyboard Mouse";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string KeyboardMouse::GetSource() const
|
|
|
|
{
|
|
|
|
return DINPUT_SOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2021-05-15 09:14:11 +00:00
|
|
|
// Give this device a higher priority to make sure it shows first
|
|
|
|
int KeyboardMouse::GetSortPriority() const
|
|
|
|
{
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2021-05-15 09:20:20 +00:00
|
|
|
bool KeyboardMouse::IsVirtualDevice() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
// names
|
|
|
|
std::string KeyboardMouse::Key::GetName() const
|
|
|
|
{
|
|
|
|
return named_keys[m_index].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string KeyboardMouse::Button::GetName() const
|
|
|
|
{
|
2010-10-19 23:11:22 +00:00
|
|
|
return std::string("Click ") + char('0' + m_index);
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string KeyboardMouse::Axis::GetName() const
|
|
|
|
{
|
2022-07-15 17:53:10 +00:00
|
|
|
char tmpstr[] = "Axis ..";
|
2010-07-16 03:43:11 +00:00
|
|
|
tmpstr[5] = (char)('X' + m_index);
|
|
|
|
tmpstr[6] = (m_range < 0 ? '-' : '+');
|
2010-07-10 06:48:24 +00:00
|
|
|
return tmpstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string KeyboardMouse::Cursor::GetName() const
|
|
|
|
{
|
2022-07-15 17:53:10 +00:00
|
|
|
char tmpstr[] = "Cursor ..";
|
2010-07-16 03:43:11 +00:00
|
|
|
tmpstr[7] = (char)('X' + m_index);
|
|
|
|
tmpstr[8] = (m_positive ? '+' : '-');
|
2010-07-03 08:04:10 +00:00
|
|
|
return tmpstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get/set state
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState KeyboardMouse::Key::GetState() const
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
return (m_key != 0);
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState KeyboardMouse::Button::GetState() const
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
return (m_button != 0);
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState KeyboardMouse::Axis::GetState() const
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2019-04-20 14:25:11 +00:00
|
|
|
return ControlState(m_axis) / m_range;
|
2010-07-10 06:48:24 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
ControlState KeyboardMouse::Cursor::GetState() const
|
2010-07-10 06:48:24 +00:00
|
|
|
{
|
2019-04-20 14:25:11 +00:00
|
|
|
return m_axis / (m_positive ? 1.0 : -1.0);
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
2019-06-17 20:39:24 +00:00
|
|
|
} // namespace ciface::DInput
|