USB: lock raw input callbacks when adding/removing

This commit is contained in:
jackun 2020-11-17 23:44:06 +02:00
parent 191ebbcacf
commit df5cfb303b
2 changed files with 13 additions and 9 deletions

View File

@ -18,6 +18,7 @@
#include <cstdio> #include <cstdio>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <mutex>
#include "../platcompat.h" #include "../platcompat.h"
extern HINSTANCE hInst; extern HINSTANCE hInst;
@ -34,15 +35,18 @@ namespace shared
HWND eatenWnd = nullptr; HWND eatenWnd = nullptr;
HHOOK hHook = nullptr, hHookWnd = nullptr, hHookKB = nullptr; HHOOK hHook = nullptr, hHookWnd = nullptr, hHookKB = nullptr;
bool skipInput = false; bool skipInput = false;
std::mutex cb_mutex;
void RegisterCallback(ParseRawInputCB* cb) void RegisterCallback(ParseRawInputCB* cb)
{ {
std::scoped_lock<std::mutex> lk(cb_mutex);
if (cb && std::find(callbacks.begin(), callbacks.end(), cb) == callbacks.end()) if (cb && std::find(callbacks.begin(), callbacks.end(), cb) == callbacks.end())
callbacks.push_back(cb); callbacks.push_back(cb);
} }
void UnregisterCallback(ParseRawInputCB* cb) void UnregisterCallback(ParseRawInputCB* cb)
{ {
std::scoped_lock<std::mutex> lk(cb_mutex);
auto it = std::find(callbacks.begin(), callbacks.end(), cb); auto it = std::find(callbacks.begin(), callbacks.end(), cb);
if (it != callbacks.end()) if (it != callbacks.end())
callbacks.erase(it); callbacks.erase(it);
@ -189,6 +193,7 @@ namespace shared
if (pRawInput->header.dwType == RIM_TYPEKEYBOARD) if (pRawInput->header.dwType == RIM_TYPEKEYBOARD)
ToggleCursor(hWnd, pRawInput->data.keyboard); ToggleCursor(hWnd, pRawInput->data.keyboard);
std::lock_guard<std::mutex> lk(cb_mutex);
for (auto cb : callbacks) for (auto cb : callbacks)
cb->ParseRawInput(pRawInput); cb->ParseRawInput(pRawInput);
} }

View File

@ -182,13 +182,12 @@ namespace usb_hid
static void ParseRawInputMS(RAWMOUSE& m, HIDState* hs) static void ParseRawInputMS(RAWMOUSE& m, HIDState* hs)
{ {
int b = 0, z = 0; int z = 0;
InputEvent ev{}; InputEvent ev{};
if (m.usButtonFlags & RI_MOUSE_WHEEL) if (m.usButtonFlags & RI_MOUSE_WHEEL)
z = (short)m.usButtonData / WHEEL_DELTA; z = (short)m.usButtonData / WHEEL_DELTA;
ev.type = INPUT_EVENT_KIND_BTN; ev.type = INPUT_EVENT_KIND_BTN;
if (m.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN) if (m.ulButtons & RI_MOUSE_LEFT_BUTTON_DOWN)
@ -245,13 +244,13 @@ namespace usb_hid
if (m.usFlags & MOUSE_MOVE_ABSOLUTE) if (m.usFlags & MOUSE_MOVE_ABSOLUTE)
{ {
/*ev.type = INPUT_EVENT_KIND_ABS; /*ev.type = INPUT_EVENT_KIND_ABS;
ev.u.abs.axis = INPUT_AXIS_X; ev.u.abs.axis = INPUT_AXIS_X;
ev.u.abs.value = m.lLastX; ev.u.abs.value = m.lLastX;
SendPointerEvent(ev, hs); SendPointerEvent(ev, hs);
ev.u.abs.axis = INPUT_AXIS_Y; ev.u.abs.axis = INPUT_AXIS_Y;
ev.u.abs.value = m.lLastY; ev.u.abs.value = m.lLastY;
SendPointerEvent(ev, hs);*/ SendPointerEvent(ev, hs);*/
} }
else else
{ {
@ -289,8 +288,8 @@ namespace usb_hid
int RawInput::Close() int RawInput::Close()
{ {
Reset();
shared::rawinput::UnregisterCallback(this); shared::rawinput::UnregisterCallback(this);
Reset();
return 0; return 0;
} }