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

View File

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