mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-11-19 09:10:58 -06:00
Add stylus tip and barrel button support to cursor condition
This commit is contained in:
@@ -394,6 +394,8 @@ AdvSceneSwitcher.condition.cursor.type.held="button is held down"
|
||||
AdvSceneSwitcher.condition.cursor.button.left="Left mouse button"
|
||||
AdvSceneSwitcher.condition.cursor.button.middle="Middle mouse button"
|
||||
AdvSceneSwitcher.condition.cursor.button.right="Right mouse button"
|
||||
AdvSceneSwitcher.condition.cursor.button.penTip="Stylus tip"
|
||||
AdvSceneSwitcher.condition.cursor.button.penBarrel="Stylus barrel button"
|
||||
AdvSceneSwitcher.condition.cursor.showFrame="Show frame"
|
||||
AdvSceneSwitcher.condition.cursor.hideFrame="Hide frame"
|
||||
AdvSceneSwitcher.condition.cursor.drawFrame="Draw frame"
|
||||
|
||||
@@ -211,6 +211,7 @@ target_sources(
|
||||
|
||||
if(OS_WINDOWS)
|
||||
target_sources(${PROJECT_NAME} PRIVATE utils/windows/windows.cpp)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE hid)
|
||||
elseif(OS_MACOS)
|
||||
target_sources(${PROJECT_NAME} PRIVATE utils/osx/osx.mm)
|
||||
else()
|
||||
|
||||
@@ -37,6 +37,10 @@ const static std::map<MacroConditionCursor::Button, std::string> buttonTypes = {
|
||||
"AdvSceneSwitcher.condition.cursor.button.middle"},
|
||||
{MacroConditionCursor::Button::RIGHT,
|
||||
"AdvSceneSwitcher.condition.cursor.button.right"},
|
||||
{MacroConditionCursor::Button::PEN_TIP,
|
||||
"AdvSceneSwitcher.condition.cursor.button.penTip"},
|
||||
{MacroConditionCursor::Button::PEN_BARREL,
|
||||
"AdvSceneSwitcher.condition.cursor.button.penBarrel"},
|
||||
};
|
||||
|
||||
bool MacroConditionCursor::CheckClick()
|
||||
@@ -48,6 +52,10 @@ bool MacroConditionCursor::CheckClick()
|
||||
return _lastCheckTime < GetLastMouseMiddleClickTime();
|
||||
case MacroConditionCursor::Button::RIGHT:
|
||||
return _lastCheckTime < GetLastMouseRightClickTime();
|
||||
case MacroConditionCursor::Button::PEN_TIP:
|
||||
return _lastCheckTime < GetLastPenTipClickTime();
|
||||
case MacroConditionCursor::Button::PEN_BARREL:
|
||||
return _lastCheckTime < GetLastPenBarrelClickTime();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -61,6 +69,10 @@ bool MacroConditionCursor::CheckHeld()
|
||||
return IsMouseMiddleButtonDown() || CheckClick();
|
||||
case MacroConditionCursor::Button::RIGHT:
|
||||
return IsMouseRightButtonDown() || CheckClick();
|
||||
case MacroConditionCursor::Button::PEN_TIP:
|
||||
return IsPenTipDown() || CheckClick();
|
||||
case MacroConditionCursor::Button::PEN_BARREL:
|
||||
return IsPenBarrelDown() || CheckClick();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ public:
|
||||
LEFT,
|
||||
MIDDLE,
|
||||
RIGHT,
|
||||
PEN_TIP,
|
||||
PEN_BARREL,
|
||||
};
|
||||
|
||||
Condition _condition = Condition::REGION;
|
||||
|
||||
@@ -29,6 +29,22 @@ bool IsMouseRightButtonDown()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
std::chrono::high_resolution_clock::time_point GetLastPenTipClickTime()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
std::chrono::high_resolution_clock::time_point GetLastPenBarrelClickTime()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
bool IsPenTipDown()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool IsPenBarrelDown()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace advss
|
||||
|
||||
@@ -9,5 +9,9 @@ std::chrono::high_resolution_clock::time_point GetLastMouseRightClickTime();
|
||||
bool IsMouseLeftButtonDown();
|
||||
bool IsMouseMiddleButtonDown();
|
||||
bool IsMouseRightButtonDown();
|
||||
std::chrono::high_resolution_clock::time_point GetLastPenTipClickTime();
|
||||
std::chrono::high_resolution_clock::time_point GetLastPenBarrelClickTime();
|
||||
bool IsPenTipDown();
|
||||
bool IsPenBarrelDown();
|
||||
|
||||
} // namespace advss
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user