2017-02-19 22:15:44 +00:00
|
|
|
|
|
|
|
#include "windows_inc.h"
|
|
|
|
|
2014-11-28 08:14:00 +00:00
|
|
|
#include <malloc.h>
|
2010-05-02 23:27:30 +00:00
|
|
|
#include "rawinput.h"
|
|
|
|
#include "winlayer.h"
|
|
|
|
#include "scancodes.h"
|
|
|
|
#include "build.h"
|
|
|
|
|
2010-05-07 20:45:40 +00:00
|
|
|
static BOOL rawinput_started = 0;
|
2010-05-05 07:31:38 +00:00
|
|
|
static uint8_t KeyboardState[256] = {0}; // VKeys
|
2010-05-02 23:27:30 +00:00
|
|
|
|
2014-09-30 04:11:50 +00:00
|
|
|
extern uint8_t moustat, mousegrab;
|
2010-05-03 08:30:25 +00:00
|
|
|
extern void SetKey(int32_t key, int32_t state);
|
2010-05-02 23:27:30 +00:00
|
|
|
|
2012-01-12 20:48:57 +00:00
|
|
|
//#define MASK_DOWN (1<<(i<<1))
|
|
|
|
//#define MASK_UP (MASK_DOWN<<1)
|
2010-05-03 08:30:25 +00:00
|
|
|
#ifndef GET_RAWINPUT_CODE_WPARAM
|
2011-01-16 02:50:27 +00:00
|
|
|
#define GET_RAWINPUT_CODE_WPARAM(wParam) ((wParam) & 0xff)
|
2010-05-03 08:30:25 +00:00
|
|
|
#endif
|
|
|
|
|
2011-01-16 02:50:27 +00:00
|
|
|
static inline void RI_ProcessMouse(const RAWMOUSE *rmouse)
|
2010-05-02 23:27:30 +00:00
|
|
|
{
|
|
|
|
int32_t i, mask;
|
2013-08-06 23:51:10 +00:00
|
|
|
int8_t MWheel = 0;
|
2010-05-02 23:27:30 +00:00
|
|
|
|
2010-05-08 20:15:24 +00:00
|
|
|
if (!mousegrab || !appactive)
|
2010-05-02 23:27:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
mousex += rmouse->lLastX;
|
|
|
|
mousey += rmouse->lLastY;
|
|
|
|
|
|
|
|
if (rmouse->usFlags & MOUSE_MOVE_ABSOLUTE)
|
|
|
|
{
|
|
|
|
// untested... maybe devices like wacom tablets set this flag?
|
|
|
|
POINT pos = { xdim>>1, ydim>>1 };
|
|
|
|
|
|
|
|
ClientToScreen((HWND)win_gethwnd(), &pos);
|
|
|
|
|
|
|
|
mousex -= pos.x;
|
|
|
|
mousey -= pos.y;
|
|
|
|
}
|
|
|
|
|
2012-02-13 21:38:16 +00:00
|
|
|
for (i = 0, mask = (1<<0); mask <= (1<<8); i++, mask<<=2)
|
2010-05-02 23:27:30 +00:00
|
|
|
{
|
2012-01-12 20:48:57 +00:00
|
|
|
// usButtonFlags:
|
|
|
|
// 1<<0: left down -> 1 / 1<<0
|
|
|
|
// 1<<2: middle down -> 2 / 1<<1
|
|
|
|
// 1<<4: right down -> 3 / 1<<2
|
|
|
|
// 1<<6: x1 down -> 4 / 1<<3
|
|
|
|
// ----------- mwheel here
|
|
|
|
// 1<<8: x2 down -> 7 / 1<<6
|
|
|
|
|
|
|
|
if (mask == 1<<8)
|
|
|
|
i = 6;
|
|
|
|
|
2010-05-02 23:27:30 +00:00
|
|
|
if (rmouse->usButtonFlags & mask) // button down
|
|
|
|
{
|
|
|
|
if (mousepresscallback)
|
2012-01-12 20:48:57 +00:00
|
|
|
mousepresscallback(i+1, 1);
|
2010-05-02 23:27:30 +00:00
|
|
|
mouseb |= 1<<i;
|
|
|
|
}
|
|
|
|
else if (rmouse->usButtonFlags & (mask<<1)) // button up
|
|
|
|
{
|
|
|
|
if (mousepresscallback)
|
2012-01-12 20:48:57 +00:00
|
|
|
mousepresscallback(i+1, 0);
|
2010-05-02 23:27:30 +00:00
|
|
|
mouseb &= ~(1<<i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MWheel = (rmouse->usButtonFlags & RI_MOUSE_WHEEL) ? rmouse->usButtonData : 0;
|
|
|
|
|
|
|
|
if (MWheel > 0) // wheel up
|
|
|
|
{
|
|
|
|
mouseb |= 16;
|
|
|
|
if (mousepresscallback) mousepresscallback(5, 1);
|
|
|
|
}
|
|
|
|
else if (MWheel < 0) // wheel down
|
|
|
|
{
|
|
|
|
mouseb |= 32;
|
|
|
|
if (mousepresscallback) mousepresscallback(6, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-16 02:50:27 +00:00
|
|
|
static inline void RI_ProcessKeyboard(const RAWKEYBOARD *rkbd)
|
2010-05-02 23:27:30 +00:00
|
|
|
{
|
2010-05-05 07:31:38 +00:00
|
|
|
uint8_t key = rkbd->MakeCode, VKey = rkbd->VKey;
|
2010-05-02 23:27:30 +00:00
|
|
|
|
2011-01-16 02:50:27 +00:00
|
|
|
// for some reason rkbd->MakeCode is wrong for these
|
2010-05-02 23:27:30 +00:00
|
|
|
// even though rkbd->VKey is right...
|
|
|
|
|
|
|
|
switch (VKey)
|
|
|
|
{
|
|
|
|
case VK_SHIFT:
|
2016-09-16 21:55:30 +00:00
|
|
|
if (rkbd->Flags & RI_KEY_E0)
|
|
|
|
VKey = VK_RSHIFT, key = sc_RightShift;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_CONTROL:
|
2016-09-16 21:55:30 +00:00
|
|
|
if (rkbd->Flags & RI_KEY_E0)
|
|
|
|
VKey = VK_RCONTROL, key = sc_RightControl;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_MENU:
|
2016-09-16 21:55:30 +00:00
|
|
|
if (rkbd->Flags & RI_KEY_E0)
|
|
|
|
VKey = VK_RMENU, key = sc_RightAlt;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_UP:
|
2010-05-03 10:32:38 +00:00
|
|
|
case VK_NUMPAD8:
|
2016-09-16 21:55:30 +00:00
|
|
|
if (rkbd->Flags & RI_KEY_E0)
|
|
|
|
VKey = VK_UP, key = sc_UpArrow;
|
|
|
|
else
|
|
|
|
VKey = VK_NUMPAD8, key = sc_kpad_8;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_DOWN:
|
2010-05-03 10:32:38 +00:00
|
|
|
case VK_NUMPAD2:
|
2016-09-16 21:55:30 +00:00
|
|
|
if (rkbd->Flags & RI_KEY_E0)
|
|
|
|
VKey = VK_DOWN, key = sc_DownArrow;
|
|
|
|
else
|
|
|
|
VKey = VK_NUMPAD2, key = sc_kpad_2;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_LEFT:
|
2010-05-03 10:32:38 +00:00
|
|
|
case VK_NUMPAD4:
|
2016-09-16 21:55:30 +00:00
|
|
|
if (rkbd->Flags & RI_KEY_E0)
|
|
|
|
VKey = VK_LEFT, key = sc_LeftArrow;
|
|
|
|
else
|
|
|
|
VKey = VK_NUMPAD4, key = sc_kpad_4;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_RIGHT:
|
2010-05-03 10:32:38 +00:00
|
|
|
case VK_NUMPAD6:
|
2016-09-16 21:55:30 +00:00
|
|
|
if (rkbd->Flags & RI_KEY_E0)
|
|
|
|
VKey = VK_RIGHT, key = sc_RightArrow;
|
|
|
|
else
|
|
|
|
VKey = VK_NUMPAD6, key = sc_kpad_6;
|
|
|
|
break;
|
2010-05-25 10:56:00 +00:00
|
|
|
case VK_DIVIDE:
|
2016-09-16 21:55:30 +00:00
|
|
|
#if 0
|
|
|
|
if (rkbd->Flags & RI_KEY_E0)
|
|
|
|
key = sc_Slash;
|
|
|
|
else
|
|
|
|
#endif
|
2010-05-25 10:56:00 +00:00
|
|
|
key = sc_kpad_Slash;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_INSERT:
|
2016-09-16 21:55:30 +00:00
|
|
|
key = sc_Insert;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_HOME:
|
2016-09-16 21:55:30 +00:00
|
|
|
key = sc_Home;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_DELETE:
|
2016-09-16 21:55:30 +00:00
|
|
|
key = sc_Delete;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_END:
|
2016-09-16 21:55:30 +00:00
|
|
|
key = sc_End;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_PRIOR:
|
2016-09-16 21:55:30 +00:00
|
|
|
key = sc_PgUp;
|
|
|
|
break;
|
2010-05-02 23:27:30 +00:00
|
|
|
case VK_NEXT:
|
2016-09-16 21:55:30 +00:00
|
|
|
key = sc_PgDn;
|
|
|
|
break;
|
2010-05-03 10:32:38 +00:00
|
|
|
case VK_RETURN:
|
2016-09-16 21:55:30 +00:00
|
|
|
if (rkbd->Flags & RI_KEY_E0)
|
|
|
|
key = sc_kpad_Enter;
|
|
|
|
break;
|
2010-05-07 20:45:40 +00:00
|
|
|
case VK_PAUSE:
|
|
|
|
KeyboardState[VKey] = 1 - (rkbd->Flags & RI_KEY_BREAK);
|
2016-09-16 21:55:30 +00:00
|
|
|
if (rkbd->Flags & RI_KEY_BREAK)
|
|
|
|
return;
|
2010-05-07 20:45:40 +00:00
|
|
|
|
|
|
|
SetKey(sc_Pause, 1);
|
|
|
|
|
|
|
|
if (keypresscallback)
|
|
|
|
keypresscallback(sc_Pause, 1);
|
2013-08-06 23:51:10 +00:00
|
|
|
case 0xFF:
|
2010-05-07 20:45:40 +00:00
|
|
|
return;
|
2010-05-02 23:27:30 +00:00
|
|
|
}
|
|
|
|
|
2010-05-07 20:45:40 +00:00
|
|
|
KeyboardState[VKey] = 1 - (rkbd->Flags & RI_KEY_BREAK);
|
2010-05-02 23:27:30 +00:00
|
|
|
|
2014-01-27 10:30:25 +00:00
|
|
|
if (OSD_HandleScanCode(key, KeyboardState[VKey] > 0))
|
2010-05-02 23:27:30 +00:00
|
|
|
{
|
2010-05-07 20:45:40 +00:00
|
|
|
SetKey(key, KeyboardState[VKey] != 0);
|
2010-05-02 23:27:30 +00:00
|
|
|
|
|
|
|
if (keypresscallback)
|
2010-05-07 20:45:40 +00:00
|
|
|
keypresscallback(key, KeyboardState[VKey] != 0);
|
2010-05-02 23:27:30 +00:00
|
|
|
}
|
2010-05-07 20:45:40 +00:00
|
|
|
|
|
|
|
if (rkbd->Flags & RI_KEY_BREAK) return;
|
|
|
|
if (((keyasciififoend+1)&(KEYFIFOSIZ-1)) == keyasciififoplc) return;
|
|
|
|
if ((keyasciififoend - keyasciififoplc) > 0) return;
|
|
|
|
|
2010-05-16 22:53:08 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[2];
|
|
|
|
|
|
|
|
if (ToAscii(VKey, key, &KeyboardState[0], (LPWORD)&buf[0], 0) != 1) return;
|
|
|
|
if ((OSD_OSDKey() < 128) && (Btolower(scantoasc[OSD_OSDKey()]) == Btolower(buf[0]))) return;
|
|
|
|
if (OSD_HandleChar(buf[0]) == 0) return;
|
|
|
|
|
|
|
|
keyasciififo[keyasciififoend] = buf[0];
|
|
|
|
keyasciififoend = ((keyasciififoend+1)&(KEYFIFOSIZ-1));
|
|
|
|
}
|
2010-05-02 23:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// keyboard is always captured regardless of what we tell this function
|
2010-05-16 22:53:08 +00:00
|
|
|
BOOL RI_CaptureInput(BOOL grab, HWND target)
|
2010-05-02 23:27:30 +00:00
|
|
|
{
|
|
|
|
RAWINPUTDEVICE raw[2];
|
|
|
|
|
|
|
|
raw[0].usUsagePage = 0x01;
|
|
|
|
raw[0].usUsage = 0x02;
|
|
|
|
raw[0].dwFlags = grab ? (RIDEV_NOLEGACY | RIDEV_CAPTUREMOUSE) : 0;
|
|
|
|
raw[0].hwndTarget = grab ? target : NULL;
|
|
|
|
|
|
|
|
raw[1].usUsagePage = 0x01;
|
|
|
|
raw[1].usUsage = 0x06;
|
2010-06-07 09:03:16 +00:00
|
|
|
raw[1].dwFlags = 0;
|
2010-05-07 20:45:40 +00:00
|
|
|
raw[1].hwndTarget = target;
|
2010-05-02 23:27:30 +00:00
|
|
|
|
|
|
|
mousegrab = grab;
|
|
|
|
|
|
|
|
return (RegisterRawInputDevices(raw, 2, sizeof(raw[0])) == FALSE);
|
|
|
|
}
|
|
|
|
|
2010-05-16 22:53:08 +00:00
|
|
|
void RI_ProcessMessage(MSG *msg)
|
|
|
|
{
|
|
|
|
if (GET_RAWINPUT_CODE_WPARAM(msg->wParam) == RIM_INPUT)
|
|
|
|
{
|
|
|
|
UINT dwSize = sizeof(RAWINPUT);
|
|
|
|
RAWINPUT raw;
|
|
|
|
|
|
|
|
GetRawInputData((HRAWINPUT)msg->lParam, RID_INPUT, &raw, &dwSize, sizeof(RAWINPUTHEADER));
|
|
|
|
|
|
|
|
if (raw.header.dwType == RIM_TYPEKEYBOARD)
|
|
|
|
RI_ProcessKeyboard(&raw.data.keyboard);
|
|
|
|
else if (raw.header.dwType == RIM_TYPEMOUSE)
|
|
|
|
RI_ProcessMouse(&raw.data.mouse);
|
|
|
|
}
|
|
|
|
|
|
|
|
DefWindowProc(msg->hwnd, msg->message, msg->wParam, msg->lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RI_PollDevices(BOOL loop)
|
2010-05-02 23:27:30 +00:00
|
|
|
{
|
|
|
|
int32_t i;
|
|
|
|
MSG msg;
|
|
|
|
|
2010-05-07 20:45:40 +00:00
|
|
|
if (!rawinput_started)
|
2010-05-02 23:27:30 +00:00
|
|
|
{
|
|
|
|
if (RI_CaptureInput(1, (HWND)win_gethwnd()))
|
|
|
|
return;
|
2010-05-07 20:45:40 +00:00
|
|
|
rawinput_started = 1;
|
2010-05-02 23:27:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 07:24:09 +00:00
|
|
|
if (inputchecked)
|
|
|
|
{
|
|
|
|
if (mousepresscallback)
|
|
|
|
{
|
|
|
|
if (mouseb & 16)
|
|
|
|
mousepresscallback(5, 0);
|
|
|
|
if (mouseb & 32)
|
|
|
|
mousepresscallback(6, 0);
|
|
|
|
}
|
|
|
|
mouseb &= ~(16|32);
|
|
|
|
}
|
|
|
|
|
2010-05-07 20:45:40 +00:00
|
|
|
// snapshot the whole keyboard state so we can translate key presses into ascii later
|
2010-05-02 23:27:30 +00:00
|
|
|
for (i = 0; i < 256; i++)
|
2010-05-07 20:45:40 +00:00
|
|
|
KeyboardState[i] = GetAsyncKeyState(i) >> 8;
|
2010-05-02 23:27:30 +00:00
|
|
|
|
2010-05-16 22:53:08 +00:00
|
|
|
while (loop && PeekMessage(&msg, 0, WM_INPUT, WM_INPUT, PM_REMOVE | PM_QS_INPUT))
|
|
|
|
RI_ProcessMessage(&msg);
|
2010-05-02 23:27:30 +00:00
|
|
|
|
2010-05-08 20:15:24 +00:00
|
|
|
if (mousegrab && appactive)
|
2010-05-02 23:27:30 +00:00
|
|
|
{
|
|
|
|
// center the cursor in the window
|
|
|
|
POINT pt = { xdim>>1, ydim>>1 };
|
|
|
|
|
|
|
|
ClientToScreen((HWND)win_gethwnd(), &pt);
|
|
|
|
SetCursorPos(pt.x, pt.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t initmouse(void)
|
|
|
|
{
|
|
|
|
if (moustat) return 0;
|
2014-11-17 07:39:12 +00:00
|
|
|
grabmouse(moustat = AppMouseGrab);
|
2010-05-02 23:27:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uninitmouse(void)
|
|
|
|
{
|
|
|
|
if (!moustat) return;
|
|
|
|
grabmouse(moustat = 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void grabmouse(char a)
|
|
|
|
{
|
|
|
|
static POINT pos;
|
|
|
|
static int32_t d = 0;
|
2011-01-16 02:50:27 +00:00
|
|
|
|
2010-05-02 23:27:30 +00:00
|
|
|
if (!moustat) return;
|
|
|
|
|
|
|
|
if (!mousegrab || !d)
|
|
|
|
{
|
2011-01-16 02:50:27 +00:00
|
|
|
GetCursorPos(&pos);
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShowCursor(a == 0);
|
|
|
|
RI_CaptureInput(a, (HWND)win_gethwnd());
|
|
|
|
SetCursorPos(pos.x, pos.y);
|
2010-05-02 23:27:30 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 07:39:12 +00:00
|
|
|
void AppGrabMouse(char a)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(a);
|
|
|
|
}
|