SDL2's current event handler is this:
case WM_MOUSEMOVE:
{
SDL_Mouse *mouse = SDL_GetMouse();
if (!mouse->relative_mode || mouse->relative_mode_warp) {
/* Only generate mouse events for real mouse */
if (GetMouseMessageSource() != SDL_MOUSE_EVENT_SOURCE_TOUCH &&
lParam != data->last_pointer_update) {
SDL_SendMouseMotion(data->window, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
if (isWin10FCUorNewer && mouse->relative_mode_warp) {
/* To work around #3931, Win10 bug introduced in Fall Creators Update, where
SetCursorPos() (SDL_WarpMouseInWindow()) doesn't reliably generate mouse events anymore,
after each windows mouse event generate a fake event for the middle of the window
if relative_mode_warp is used */
int center_x = 0, center_y = 0;
SDL_GetWindowSize(data->window, ¢er_x, ¢er_y);
center_x /= 2;
center_y /= 2;
SDL_SendMouseMotion(data->window, 0, 0, center_x, center_y);
}
}
} else {
/* We still need to update focus */
SDL_SetMouseFocus(data->window);
}
}
/* don't break here, fall through to check the wParam like the button presses */
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP:
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN:
case WM_MBUTTONDBLCLK:
case WM_XBUTTONDOWN:
case WM_XBUTTONDBLCLK:
{
SDL_Mouse *mouse = SDL_GetMouse();
if (!mouse->relative_mode || mouse->relative_mode_warp) {
if (GetMouseMessageSource() != SDL_MOUSE_EVENT_SOURCE_TOUCH &&
lParam != data->last_pointer_update) {
WIN_CheckWParamMouseButtons(wParam, data, 0);
}
}
}
break;
case WM_INPUT:
{
SDL_Mouse *mouse = SDL_GetMouse();
HRAWINPUT hRawInput = (HRAWINPUT)lParam;
RAWINPUT inp;
UINT size = sizeof(inp);
const SDL_bool isRelative = mouse->relative_mode || mouse->relative_mode_warp;
const SDL_bool isCapture = ((data->window->flags & SDL_WINDOW_MOUSE_CAPTURE) != 0);
if (!isRelative || mouse->focus != data->window) {
if (!isCapture) {
break;
}
}
GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
/* Mouse data (ignoring synthetic mouse events generated for touchscreens) */
if (inp.header.dwType == RIM_TYPEMOUSE) {
if (GetMouseMessageSource() == SDL_MOUSE_EVENT_SOURCE_TOUCH ||
(GetMessageExtraInfo() & 0x82) == 0x82) {
break;
}
if (isRelative) {
RAWMOUSE* rawmouse = &inp.data.mouse;
if ((rawmouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE) {
SDL_SendMouseMotion(data->window, 0, 1, (int)rawmouse->lLastX, (int)rawmouse->lLastY);
} else if (rawmouse->lLastX || rawmouse->lLastY) {
/* synthesize relative moves from the abs position */
static SDL_Point lastMousePoint;
SDL_bool virtual_desktop = (rawmouse->usFlags & MOUSE_VIRTUAL_DESKTOP) ? SDL_TRUE : SDL_FALSE;
int w = GetSystemMetrics(virtual_desktop ? SM_CXVIRTUALSCREEN : SM_CXSCREEN);
int h = GetSystemMetrics(virtual_desktop ? SM_CYVIRTUALSCREEN : SM_CYSCREEN);
int x = (int)(((float)rawmouse->lLastX / 65535.0f) * w);
int y = (int)(((float)rawmouse->lLastY / 65535.0f) * h);
if (lastMousePoint.x == 0 && lastMousePoint.y == 0) {
lastMousePoint.x = x;
lastMousePoint.y = y;
}
SDL_SendMouseMotion(data->window, 0, 1, (int)(x-lastMousePoint.x), (int)(y-lastMousePoint.y));
lastMousePoint.x = x;
lastMousePoint.y = y;
}
WIN_CheckRawMouseButtons(rawmouse->usButtonFlags, data);
} else if (isCapture) {
/* we check for where Windows thinks the system cursor lives in this case, so we don't really lose mouse accel, etc. */
POINT pt;
RECT hwndRect;
HWND currentHnd;
GetCursorPos(&pt);
currentHnd = WindowFromPoint(pt);
ScreenToClient(hwnd, &pt);
GetClientRect(hwnd, &hwndRect);
/* if in the window, WM_MOUSEMOVE, etc, will cover it. */
if(currentHnd != hwnd || pt.x < 0 || pt.y < 0 || pt.x > hwndRect.right || pt.y > hwndRect.right) {
SDL_bool swapButtons = GetSystemMetrics(SM_SWAPBUTTON) != 0;
SDL_SendMouseMotion(data->window, 0, 0, (int)pt.x, (int)pt.y);
SDL_SendMouseButton(data->window, 0, GetAsyncKeyState(VK_LBUTTON) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, !swapButtons ? SDL_BUTTON_LEFT : SDL_BUTTON_RIGHT);
SDL_SendMouseButton(data->window, 0, GetAsyncKeyState(VK_RBUTTON) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, !swapButtons ? SDL_BUTTON_RIGHT : SDL_BUTTON_LEFT);
SDL_SendMouseButton(data->window, 0, GetAsyncKeyState(VK_MBUTTON) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_MIDDLE);
SDL_SendMouseButton(data->window, 0, GetAsyncKeyState(VK_XBUTTON1) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_X1);
SDL_SendMouseButton(data->window, 0, GetAsyncKeyState(VK_XBUTTON2) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_X2);
}
} else {
SDL_assert(0 && "Shouldn't happen");
}
}
}
break;
And the detection code is this:
/* We want to generate mouse events from mouse and pen, and touch events from touchscreens */
#define MI_WP_SIGNATURE 0xFF515700
#define MI_WP_SIGNATURE_MASK 0xFFFFFF00
#define IsTouchEvent(dw) ((dw) & MI_WP_SIGNATURE_MASK) == MI_WP_SIGNATURE
typedef enum
{
SDL_MOUSE_EVENT_SOURCE_UNKNOWN,
SDL_MOUSE_EVENT_SOURCE_MOUSE,
SDL_MOUSE_EVENT_SOURCE_TOUCH,
SDL_MOUSE_EVENT_SOURCE_PEN,
} SDL_MOUSE_EVENT_SOURCE;
static SDL_MOUSE_EVENT_SOURCE GetMouseMessageSource()
{
LPARAM extrainfo = GetMessageExtraInfo();
/* Mouse data (ignoring synthetic mouse events generated for touchscreens) */
/* Versions below Vista will set the low 7 bits to the Mouse ID and don't use bit 7:
Check bits 8-32 for the signature (which will indicate a Tablet PC Pen or Touch Device).
Only check bit 7 when Vista and up(Cleared=Pen, Set=Touch(which we need to filter out)),
when the signature is set. The Mouse ID will be zero for an actual mouse. */
if (IsTouchEvent(extrainfo)) {
if (extrainfo & 0x80) {
return SDL_MOUSE_EVENT_SOURCE_TOUCH;
} else {
return SDL_MOUSE_EVENT_SOURCE_PEN;
}
}
return SDL_MOUSE_EVENT_SOURCE_MOUSE;
}
But since the signature is missing in this case, it leaks through when it shouldn't? The values reported for the lower 8 bits of getMessageExtraInfo's result are C7/C8/F7/F8/F9 in the GetMessageExtraInfo result when using RDP in touch input mode, tapping and dragging accross the screen.
The signature in the upper 24-bits is 0 in this case, which is obviously incorrect.