2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2010-07-03 08:04:10 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include "InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h"
|
|
|
|
#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
|
|
|
|
|
|
|
namespace ciface
|
|
|
|
{
|
|
|
|
namespace DInput
|
|
|
|
{
|
|
|
|
|
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-17 10:18:15 +00:00
|
|
|
#include "InputCommon/ControllerInterface/DInput/NamedKeys.h"
|
2010-07-03 08:04:10 +00:00
|
|
|
};
|
|
|
|
|
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_lights[] =
|
|
|
|
{
|
|
|
|
{ VK_NUMLOCK, "NUM LOCK" },
|
|
|
|
{ VK_CAPITAL, "CAPS LOCK" },
|
|
|
|
{ VK_SCROLL, "SCROLL LOCK" }
|
|
|
|
};
|
|
|
|
|
2010-07-10 06:48:24 +00:00
|
|
|
// lil silly
|
|
|
|
static HWND hwnd;
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
void InitKeyboardMouse(IDirectInput8* const idi8, std::vector<Core::Device*>& devices, HWND _hwnd)
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2010-07-10 06:48:24 +00:00
|
|
|
hwnd = _hwnd;
|
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
|
LPDIRECTINPUTDEVICE8 kb_device = NULL;
|
|
|
|
LPDIRECTINPUTDEVICE8 mo_device = NULL;
|
|
|
|
|
|
|
|
if (SUCCEEDED(idi8->CreateDevice( GUID_SysKeyboard, &kb_device, NULL)))
|
|
|
|
{
|
|
|
|
if (SUCCEEDED(kb_device->SetDataFormat(&c_dfDIKeyboard)))
|
|
|
|
{
|
2013-04-15 02:53:10 +00:00
|
|
|
if (SUCCEEDED(kb_device->SetCooperativeLevel(NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2013-04-15 02:53:10 +00:00
|
|
|
if (SUCCEEDED(idi8->CreateDevice( GUID_SysMouse, &mo_device, NULL )))
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2013-04-15 02:53:10 +00:00
|
|
|
if (SUCCEEDED(mo_device->SetDataFormat(&c_dfDIMouse2)))
|
|
|
|
{
|
|
|
|
if (SUCCEEDED(mo_device->SetCooperativeLevel(NULL, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
|
|
|
|
{
|
|
|
|
devices.push_back(new KeyboardMouse(kb_device, mo_device));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-07-10 06:48:24 +00:00
|
|
|
KeyboardMouse::KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device, const LPDIRECTINPUTDEVICE8 mo_device)
|
2010-07-03 08:04:10 +00:00
|
|
|
: m_kb_device(kb_device)
|
|
|
|
, m_mo_device(mo_device)
|
|
|
|
{
|
|
|
|
m_kb_device->Acquire();
|
|
|
|
m_mo_device->Acquire();
|
|
|
|
|
|
|
|
m_last_update = GetTickCount();
|
|
|
|
|
2010-07-10 06:48:24 +00:00
|
|
|
ZeroMemory(&m_state_in, sizeof(m_state_in));
|
|
|
|
ZeroMemory(m_state_out, sizeof(m_state_out));
|
|
|
|
ZeroMemory(&m_current_state_out, sizeof(m_current_state_out));
|
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]));
|
2010-07-03 08:04:10 +00:00
|
|
|
// add lights
|
2011-03-14 01:20:11 +00:00
|
|
|
for (u8 i = 0; i < sizeof(named_lights)/sizeof(*named_lights); ++i)
|
2010-07-03 08:04:10 +00:00
|
|
|
AddOutput(new Light(i));
|
|
|
|
|
|
|
|
// 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
|
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];
|
|
|
|
|
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
|
|
|
}
|
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)));
|
2010-07-10 06:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GetMousePos(float* const x, float* const y)
|
|
|
|
{
|
|
|
|
unsigned int win_width = 2, win_height = 2;
|
|
|
|
POINT point = { 1, 1 };
|
|
|
|
GetCursorPos(&point);
|
|
|
|
// Get the cursor position relative to the upper left corner of the rendering window
|
|
|
|
ScreenToClient(hwnd, &point);
|
|
|
|
|
|
|
|
// Get the size of the rendering 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
|
|
|
|
win_width = rect.right - rect.left;
|
|
|
|
win_height = rect.bottom - rect.top;
|
|
|
|
|
|
|
|
// Return the mouse position as a range from -1 to 1
|
|
|
|
*x = (float)point.x / (float)win_width * 2 - 1;
|
|
|
|
*y = (float)point.y / (float)win_height * 2 - 1;
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool 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))
|
|
|
|
{
|
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
|
2010-07-10 06:48:24 +00:00
|
|
|
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);
|
|
|
|
|
2010-07-03 08:04:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KeyboardMouse::UpdateOutput()
|
|
|
|
{
|
|
|
|
class KInput : public INPUT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
KInput( const unsigned char key, const bool up = false )
|
|
|
|
{
|
|
|
|
memset( this, 0, sizeof(*this) );
|
|
|
|
type = INPUT_KEYBOARD;
|
|
|
|
ki.wVk = key;
|
2013-04-15 02:53:10 +00:00
|
|
|
|
|
|
|
if (up)
|
|
|
|
ki.dwFlags = KEYEVENTF_KEYUP;
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector< KInput > kbinputs;
|
2010-07-10 06:48:24 +00:00
|
|
|
for (unsigned int i = 0; i < sizeof(m_state_out)/sizeof(*m_state_out); ++i)
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
|
|
|
bool want_on = false;
|
2010-07-10 06:48:24 +00:00
|
|
|
if (m_state_out[i])
|
2014-02-17 04:51:41 +00:00
|
|
|
want_on = m_state_out[i] > GetTickCount() % 255 ; // light should flash when output is 0.5
|
2010-07-03 08:04:10 +00:00
|
|
|
|
|
|
|
// lights are set to their original state when output is zero
|
2010-07-10 06:48:24 +00:00
|
|
|
if (want_on ^ m_current_state_out[i])
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2014-02-17 04:51:41 +00:00
|
|
|
kbinputs.push_back(KInput(named_lights[i].code)); // press
|
2010-07-10 06:48:24 +00:00
|
|
|
kbinputs.push_back(KInput(named_lights[i].code, true)); // release
|
2010-07-03 08:04:10 +00:00
|
|
|
|
|
|
|
m_current_state_out[i] ^= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kbinputs.size())
|
|
|
|
return ( kbinputs.size() == SendInput( (UINT)kbinputs.size(), &kbinputs[0], sizeof( kbinputs[0] ) ) );
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
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
|
|
|
|
{
|
2010-07-16 03:43:11 +00:00
|
|
|
static char tmpstr[] = "Axis ..";
|
|
|
|
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
|
|
|
|
{
|
2010-07-16 03:43:11 +00:00
|
|
|
static char tmpstr[] = "Cursor ..";
|
|
|
|
tmpstr[7] = (char)('X' + m_index);
|
|
|
|
tmpstr[8] = (m_positive ? '+' : '-');
|
2010-07-03 08:04:10 +00:00
|
|
|
return tmpstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string KeyboardMouse::Light::GetName() const
|
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
return named_lights[m_index].name;
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
return std::max(0.0f, 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
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
return std::max(0.0f, ControlState(m_axis) / (m_positive ? 1.0f : -1.0f));
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 01:20:11 +00:00
|
|
|
void KeyboardMouse::Light::SetState(const ControlState state)
|
2010-07-03 08:04:10 +00:00
|
|
|
{
|
2011-03-14 01:20:11 +00:00
|
|
|
//state_out[m_index] = (unsigned char)(state * 255);
|
2010-07-03 08:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|