2019-08-16 04:39:18 +00:00
|
|
|
|
|
|
|
#include "Precomp.h"
|
|
|
|
#include "RawMouse.h"
|
|
|
|
|
2019-08-18 01:29:33 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
|
2019-08-16 04:39:18 +00:00
|
|
|
#ifndef HID_USAGE_PAGE_GENERIC
|
|
|
|
#define HID_USAGE_PAGE_GENERIC ((USHORT) 0x01)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HID_USAGE_GENERIC_MOUSE
|
|
|
|
#define HID_USAGE_GENERIC_MOUSE ((USHORT) 0x02)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HID_USAGE_GENERIC_JOYSTICK
|
|
|
|
#define HID_USAGE_GENERIC_JOYSTICK ((USHORT) 0x04)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HID_USAGE_GENERIC_GAMEPAD
|
|
|
|
#define HID_USAGE_GENERIC_GAMEPAD ((USHORT) 0x05)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef RIDEV_INPUTSINK
|
|
|
|
#define RIDEV_INPUTSINK (0x100)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class RawMouseWindowClass
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RawMouseWindowClass()
|
|
|
|
{
|
|
|
|
WNDCLASSEX windowClassDesc;
|
|
|
|
memset(&windowClassDesc, 0, sizeof(WNDCLASSEX));
|
|
|
|
windowClassDesc.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
windowClassDesc.lpszClassName = ClassName;
|
|
|
|
windowClassDesc.hInstance = GetModuleHandle(nullptr);
|
|
|
|
windowClassDesc.lpfnWndProc = &RawMouse::WindowProc;
|
|
|
|
RegisterClassEx(&windowClassDesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
const TCHAR* ClassName = TEXT("RawMouseWindow");
|
|
|
|
};
|
|
|
|
|
2019-08-18 01:29:33 +00:00
|
|
|
RawMouse::RawMouse(void* ownerWindow)
|
2019-08-16 04:39:18 +00:00
|
|
|
{
|
|
|
|
static RawMouseWindowClass win32class;
|
|
|
|
handle = CreateWindowEx(0, win32class.ClassName, TEXT(""), WS_POPUP, 0, 0, 100, 100, 0, 0, GetModuleHandle(nullptr), this);
|
|
|
|
|
|
|
|
RAWINPUTDEVICE rid;
|
|
|
|
rid.usUsagePage = HID_USAGE_PAGE_GENERIC;
|
|
|
|
rid.usUsage = HID_USAGE_GENERIC_MOUSE;
|
|
|
|
rid.dwFlags = RIDEV_INPUTSINK;
|
|
|
|
rid.hwndTarget = handle;
|
|
|
|
RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
|
|
|
|
}
|
|
|
|
|
|
|
|
RawMouse::~RawMouse()
|
|
|
|
{
|
|
|
|
if (handle)
|
|
|
|
DestroyWindow(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
float RawMouse::GetX()
|
|
|
|
{
|
|
|
|
float result = x;
|
|
|
|
x = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
float RawMouse::GetY()
|
|
|
|
{
|
|
|
|
float result = y;
|
|
|
|
y = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT RawMouse::OnMessage(INT message, WPARAM wparam, LPARAM lparam)
|
|
|
|
{
|
|
|
|
if (message == WM_INPUT)
|
|
|
|
{
|
|
|
|
HRAWINPUT rawinputHandle = (HRAWINPUT)lparam;
|
|
|
|
UINT size = 0;
|
|
|
|
UINT result = GetRawInputData(rawinputHandle, RID_INPUT, 0, &size, sizeof(RAWINPUTHEADER));
|
|
|
|
if (result == 0 && size > 0)
|
|
|
|
{
|
|
|
|
std::vector<uint32_t> buf((size + 3) / 4);
|
|
|
|
result = GetRawInputData(rawinputHandle, RID_INPUT, buf.data(), &size, sizeof(RAWINPUTHEADER));
|
|
|
|
if (result >= 0)
|
|
|
|
{
|
|
|
|
RAWINPUT* rawinput = (RAWINPUT*)buf.data();
|
|
|
|
if (rawinput->header.dwType == RIM_TYPEMOUSE)
|
|
|
|
{
|
|
|
|
x += rawinput->data.mouse.lLastX;
|
|
|
|
y += rawinput->data.mouse.lLastY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return DefWindowProc(handle, message, wparam, lparam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT RawMouse::WindowProc(HWND handle, UINT message, WPARAM wparam, LPARAM lparam)
|
|
|
|
{
|
|
|
|
if (message == WM_CREATE)
|
|
|
|
{
|
|
|
|
CREATESTRUCT* createInfo = (CREATESTRUCT*)lparam;
|
|
|
|
auto window = reinterpret_cast<RawMouse*>(createInfo->lpCreateParams);
|
|
|
|
window->handle = handle;
|
|
|
|
SetWindowLongPtr(handle, GWLP_USERDATA, reinterpret_cast<ULONG_PTR>(window));
|
|
|
|
return window->OnMessage(message, wparam, lparam);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto window = reinterpret_cast<RawMouse*>(GetWindowLongPtr(handle, GWLP_USERDATA));
|
|
|
|
if (window)
|
|
|
|
return window->OnMessage(message, wparam, lparam);
|
|
|
|
else
|
|
|
|
return DefWindowProc(handle, message, wparam, lparam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:29:33 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
RawMouse::RawMouse(void* ownerWindow)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RawMouse::~RawMouse()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
float RawMouse::GetX()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float RawMouse::GetY()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-08-16 04:39:18 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-08-30 08:30:28 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
2019-08-18 01:29:33 +00:00
|
|
|
RawMouse* RawMouse_New(void* hwnd)
|
2019-08-16 04:39:18 +00:00
|
|
|
{
|
|
|
|
return new RawMouse(hwnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RawMouse_Delete(RawMouse* mouse)
|
|
|
|
{
|
|
|
|
delete mouse;
|
|
|
|
}
|
|
|
|
|
|
|
|
float RawMouse_GetX(RawMouse* mouse)
|
|
|
|
{
|
|
|
|
return mouse->GetX();
|
|
|
|
}
|
|
|
|
|
|
|
|
float RawMouse_GetY(RawMouse* mouse)
|
|
|
|
{
|
|
|
|
return mouse->GetY();
|
|
|
|
}
|
2019-08-30 08:30:28 +00:00
|
|
|
|
|
|
|
}
|