| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2010 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2014-02-10 13:54:46 -05:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-02-19 02:17:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-12 17:08:04 +02:00
										 |  |  | #include "InputCommon/ControllerInterface/ControllerInterface.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "InputCommon/ControllerInterface/DInput/DInput.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-19 02:17:31 +01:00
										 |  |  | #include "InputCommon/ControllerInterface/DInput/DInputKeyboardMouse.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | // (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
 | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | // 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
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   const BYTE code; | 
					
						
							|  |  |  |   const char* const name; | 
					
						
							|  |  |  | } named_keys[] = { | 
					
						
							|  |  |  | #include "InputCommon/ControllerInterface/DInput/NamedKeys.h"  // NOLINT
 | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-10 06:48:24 +00:00
										 |  |  | // lil silly
 | 
					
						
							| 
									
										
										
										
											2015-03-28 11:38:14 +11:00
										 |  |  | static HWND m_hwnd; | 
					
						
							| 
									
										
										
										
											2010-07-10 06:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-12 17:08:04 +02:00
										 |  |  | void InitKeyboardMouse(IDirectInput8* const idi8, HWND _hwnd) | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 16:28:27 +02: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))) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2016-06-25 21:46:39 +02:00
										 |  |  |     g_controller_interface.AddDevice(std::make_shared<KeyboardMouse>(kb_device, mo_device)); | 
					
						
							| 
									
										
										
										
											2016-06-24 16:28:27 +02:00
										 |  |  |     return; | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (kb_device) | 
					
						
							|  |  |  |     kb_device->Release(); | 
					
						
							|  |  |  |   if (mo_device) | 
					
						
							|  |  |  |     mo_device->Release(); | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | KeyboardMouse::~KeyboardMouse() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // kb
 | 
					
						
							|  |  |  |   m_kb_device->Unacquire(); | 
					
						
							|  |  |  |   m_kb_device->Release(); | 
					
						
							|  |  |  |   // mouse
 | 
					
						
							|  |  |  |   m_mo_device->Unacquire(); | 
					
						
							|  |  |  |   m_mo_device->Release(); | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | KeyboardMouse::KeyboardMouse(const LPDIRECTINPUTDEVICE8 kb_device, | 
					
						
							|  |  |  |                              const LPDIRECTINPUTDEVICE8 mo_device) | 
					
						
							|  |  |  |     : m_kb_device(kb_device), m_mo_device(mo_device) | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   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))); | 
					
						
							| 
									
										
										
										
											2010-07-10 06:48:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-09 15:02:18 -05:00
										 |  |  | void GetMousePos(ControlState* const x, ControlState* const y) | 
					
						
							| 
									
										
										
										
											2010-07-10 06:48:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   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; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 00:55:14 -08:00
										 |  |  | void KeyboardMouse::UpdateInput() | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   DIMOUSESTATE2 tmp_mouse; | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // 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); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   m_last_update = cur_time; | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   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); | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (DIERR_INPUTLOST == kb_hr || DIERR_NOTACQUIRED == kb_hr) | 
					
						
							|  |  |  |     m_kb_device->Acquire(); | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (DIERR_INPUTLOST == mo_hr || DIERR_NOTACQUIRED == mo_hr) | 
					
						
							|  |  |  |     m_mo_device->Acquire(); | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   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; | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     // copy over the buttons
 | 
					
						
							|  |  |  |     memcpy(m_state_in.mouse.rgbButtons, tmp_mouse.rgbButtons, sizeof(m_state_in.mouse.rgbButtons)); | 
					
						
							| 
									
										
										
										
											2010-07-10 06:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     // update mouse cursor
 | 
					
						
							|  |  |  |     GetMousePos(&m_state_in.cursor.x, &m_state_in.cursor.y); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string KeyboardMouse::GetName() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return "Keyboard Mouse"; | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string KeyboardMouse::GetSource() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return DINPUT_SOURCE_NAME; | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // names
 | 
					
						
							|  |  |  | std::string KeyboardMouse::Key::GetName() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return named_keys[m_index].name; | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string KeyboardMouse::Button::GetName() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return std::string("Click ") + char('0' + m_index); | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string KeyboardMouse::Axis::GetName() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   static char tmpstr[] = "Axis .."; | 
					
						
							|  |  |  |   tmpstr[5] = (char)('X' + m_index); | 
					
						
							|  |  |  |   tmpstr[6] = (m_range < 0 ? '-' : '+'); | 
					
						
							|  |  |  |   return tmpstr; | 
					
						
							| 
									
										
										
										
											2010-07-10 06:48:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string KeyboardMouse::Cursor::GetName() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   static char tmpstr[] = "Cursor .."; | 
					
						
							|  |  |  |   tmpstr[7] = (char)('X' + m_index); | 
					
						
							|  |  |  |   tmpstr[8] = (m_positive ? '+' : '-'); | 
					
						
							|  |  |  |   return tmpstr; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02: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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02: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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return std::max(0.0, 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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return std::max(0.0, ControlState(m_axis) / (m_positive ? 1.0 : -1.0)); | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | } |