Add stylus tip and barrel button support to cursor condition

This commit is contained in:
WarmUpTill
2026-11-06 21:50:35 +01:00
parent b3f47cdb68
commit 796cbf77e9
7 changed files with 155 additions and 0 deletions

View File

@@ -2,13 +2,19 @@
#include "log-helper.hpp"
#include "plugin-state-helpers.hpp"
#include <algorithm>
#include <atomic>
#include <obs-frontend-api.h>
#include <QAbstractEventDispatcher>
#include <QAbstractNativeEventFilter>
#include <unordered_map>
#include <util/platform.h>
#include <vector>
#include <windows.h>
// Must be included after windows.h
#include <hidsdi.h>
namespace advss {
static bool canSimulateKeyPresses = true;
@@ -293,6 +299,99 @@ bool IsMouseRightButtonDown()
return GetAsyncKeyState(VK_RBUTTON) & 0x8000;
}
static constexpr USAGE hidUsagePageDigitizer = 0x0D;
static constexpr USAGE hidUsageExternalPen = 0x01;
static constexpr USAGE hidUsageIntegratedPen = 0x02;
static constexpr USAGE hidUsageTipSwitch = 0x42;
static constexpr USAGE hidUsageBarrelSwitch = 0x44;
// Written on the Qt main thread and read on the macro thread
static std::atomic<bool> penTipDown = false;
static std::atomic<bool> penBarrelDown = false;
static std::atomic<std::chrono::high_resolution_clock::time_point>
lastPenTipClickTime{std::chrono::high_resolution_clock::time_point{}};
static std::atomic<std::chrono::high_resolution_clock::time_point>
lastPenBarrelClickTime{
std::chrono::high_resolution_clock::time_point{}};
std::chrono::high_resolution_clock::time_point GetLastPenTipClickTime()
{
return lastPenTipClickTime;
}
std::chrono::high_resolution_clock::time_point GetLastPenBarrelClickTime()
{
return lastPenBarrelClickTime;
}
bool IsPenTipDown()
{
return penTipDown;
}
bool IsPenBarrelDown()
{
return penBarrelDown;
}
static const std::vector<BYTE> &getPenPreparsedData(HANDLE device)
{
static std::unordered_map<HANDLE, std::vector<BYTE>> cache;
const auto it = cache.find(device);
if (it != cache.end()) {
return it->second;
}
UINT size = 0;
GetRawInputDeviceInfo(device, RIDI_PREPARSEDDATA, NULL, &size);
std::vector<BYTE> data(size);
if (size == 0 || GetRawInputDeviceInfo(device, RIDI_PREPARSEDDATA,
data.data(), &size) != size) {
data.clear();
}
return cache.emplace(device, std::move(data)).first->second;
}
static void updatePenButton(
bool down, std::atomic<bool> &state,
std::atomic<std::chrono::high_resolution_clock::time_point> &lastClick)
{
if (down && !state) {
lastClick = std::chrono::high_resolution_clock::now();
}
state = down;
}
static void handleRawPenInput(RAWINPUT *raw)
{
const auto &preparsedData = getPenPreparsedData(raw->header.hDevice);
if (preparsedData.empty()) {
return;
}
for (DWORD i = 0; i < raw->data.hid.dwCount; i++) {
auto report = (PCHAR)raw->data.hid.bRawData +
i * raw->data.hid.dwSizeHid;
USAGE usages[32];
ULONG count = 32;
const auto status = HidP_GetUsages(
HidP_Input, hidUsagePageDigitizer, 0, usages, &count,
(PHIDP_PREPARSED_DATA)preparsedData.data(), report,
raw->data.hid.dwSizeHid);
if (status != HIDP_STATUS_SUCCESS) {
continue;
}
const auto end = usages + count;
updatePenButton(std::find(usages, end, hidUsageTipSwitch) !=
end,
penTipDown, lastPenTipClickTime);
updatePenButton(std::find(usages, end, hidUsageBarrelSwitch) !=
end,
penBarrelDown, lastPenBarrelClickTime);
}
}
static void handleRawMouseInput(LPARAM lParam)
{
UINT dwSize;
@@ -326,6 +425,8 @@ static void handleRawMouseInput(LPARAM lParam)
default:
break;
}
} else if (raw->header.dwType == RIM_TYPEHID) {
handleRawPenInput(raw);
}
delete[] lpb;
}
@@ -348,6 +449,22 @@ public:
}
};
// A held pen is not visible through raw mouse input or GetAsyncKeyState
static void registerRawPenInput(HWND window)
{
RAWINPUTDEVICE devices[2];
devices[0].dwFlags = RIDEV_INPUTSINK;
devices[0].usUsagePage = hidUsagePageDigitizer;
devices[0].usUsage = hidUsageIntegratedPen;
devices[0].hwndTarget = window;
devices[1] = devices[0];
devices[1].usUsage = hidUsageExternalPen;
if (!RegisterRawInputDevices(devices, 2, sizeof(RAWINPUTDEVICE))) {
blog(LOG_WARNING, "Registering for raw pen input failed!\n"
"Pen click detection not functional!");
}
}
static void setupMouseEeventFilter()
{
mouseInputFilter = new RawMouseInputFilter;
@@ -363,6 +480,7 @@ static void setupMouseEeventFilter()
blog(LOG_WARNING, "Registering for raw mouse input failed!\n"
"Mouse click detection not functional!");
}
registerRawPenInput(rid.hwndTarget);
}
static bool setup()