mirror of
https://github.com/optiscaler/OptiScaler.git
synced 2026-11-22 13:47:11 +00:00
1079 lines
35 KiB
C++
1079 lines
35 KiB
C++
#include "pch.h"
|
|
#include "input_system_internal.h"
|
|
|
|
#include <hooks/Kernel_Hooks.h>
|
|
|
|
#include <detours/detours.h>
|
|
|
|
#include <cstring>
|
|
|
|
namespace OptiInput
|
|
{
|
|
// One instance per hook slot so each calls its own slot's trampoline, another mod can replace the vtable entry
|
|
template <std::size_t Slot>
|
|
HRESULT WINAPI hkDirectInputCreateDevice(void* directInput, REFGUID guid, void** device, LPUNKNOWN outer);
|
|
template <std::size_t Slot> HRESULT WINAPI hkDirectInputGetDeviceState(void* device, DWORD dataSize, LPVOID data);
|
|
template <std::size_t Slot>
|
|
HRESULT WINAPI hkDirectInputGetDeviceData(void* device, DWORD objectDataSize, LPDIDEVICEOBJECTDATA data, LPDWORD inOut,
|
|
DWORD flags);
|
|
template <std::size_t Slot> ULONG WINAPI hkDirectInputDeviceRelease(void* device);
|
|
|
|
namespace
|
|
{
|
|
constexpr wchar_t DirectInput8ModuleName[] = L"dinput8.dll";
|
|
constexpr wchar_t DirectInputLegacyModuleName[] = L"dinput.dll";
|
|
|
|
constexpr char DirectInput8CreateExportName[] = "DirectInput8Create";
|
|
constexpr char DirectInputCreateAExportName[] = "DirectInputCreateA";
|
|
constexpr char DirectInputCreateWExportName[] = "DirectInputCreateW";
|
|
constexpr char DirectInputCreateExExportName[] = "DirectInputCreateEx";
|
|
|
|
constexpr GUID DirectInputSysKeyboardGuid = {
|
|
0x6f1d2b61, 0xd5a0, 0x11cf, { 0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00 }
|
|
};
|
|
constexpr GUID DirectInputSysMouseGuid = {
|
|
0x6f1d2b60, 0xd5a0, 0x11cf, { 0xbf, 0xc7, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00 }
|
|
};
|
|
|
|
constexpr std::size_t MaxDirectInputMethodHooks = 8;
|
|
|
|
template <typename T> struct DirectInputMethodHookSlot
|
|
{
|
|
bool InUse = false;
|
|
T Target = nullptr;
|
|
T Trampoline = nullptr;
|
|
};
|
|
|
|
std::array<DirectInputMethodHookSlot<DirectInputCreateDevice_t>, MaxDirectInputMethodHooks>
|
|
DirectInputCreateDeviceHooks {};
|
|
std::array<DirectInputMethodHookSlot<DirectInputDeviceRelease_t>, MaxDirectInputMethodHooks> DirectInputReleaseHooks {};
|
|
std::array<DirectInputMethodHookSlot<DirectInputGetDeviceState_t>, MaxDirectInputMethodHooks>
|
|
DirectInputGetDeviceStateHooks {};
|
|
std::array<DirectInputMethodHookSlot<DirectInputGetDeviceData_t>, MaxDirectInputMethodHooks>
|
|
DirectInputGetDeviceDataHooks {};
|
|
|
|
template <std::size_t... I> constexpr auto CreateDeviceDetours(std::index_sequence<I...>)
|
|
{
|
|
return std::array { &hkDirectInputCreateDevice<I>... };
|
|
}
|
|
template <std::size_t... I> constexpr auto GetDeviceStateDetours(std::index_sequence<I...>)
|
|
{
|
|
return std::array { &hkDirectInputGetDeviceState<I>... };
|
|
}
|
|
template <std::size_t... I> constexpr auto GetDeviceDataDetours(std::index_sequence<I...>)
|
|
{
|
|
return std::array { &hkDirectInputGetDeviceData<I>... };
|
|
}
|
|
template <std::size_t... I> constexpr auto ReleaseDetours(std::index_sequence<I...>)
|
|
{
|
|
return std::array { &hkDirectInputDeviceRelease<I>... };
|
|
}
|
|
|
|
constexpr auto DirectInputCreateDeviceDetours =
|
|
CreateDeviceDetours(std::make_index_sequence<MaxDirectInputMethodHooks> {});
|
|
constexpr auto DirectInputGetDeviceStateDetours =
|
|
GetDeviceStateDetours(std::make_index_sequence<MaxDirectInputMethodHooks> {});
|
|
constexpr auto DirectInputGetDeviceDataDetours =
|
|
GetDeviceDataDetours(std::make_index_sequence<MaxDirectInputMethodHooks> {});
|
|
constexpr auto DirectInputReleaseDetours = ReleaseDetours(std::make_index_sequence<MaxDirectInputMethodHooks> {});
|
|
|
|
// The slot's position in its table, which picks its hook instance
|
|
template <typename T, std::size_t N>
|
|
std::size_t SlotIndex(const std::array<DirectInputMethodHookSlot<T>, N>& hooks,
|
|
const DirectInputMethodHookSlot<T>* slot)
|
|
{
|
|
return static_cast<std::size_t>(slot - hooks.data());
|
|
}
|
|
|
|
bool IsDirectInputKeyboardGuid(REFGUID guid) { return IsEqualGUID(guid, DirectInputSysKeyboardGuid) != FALSE; }
|
|
|
|
bool IsDirectInputMouseGuid(REFGUID guid) { return IsEqualGUID(guid, DirectInputSysMouseGuid) != FALSE; }
|
|
|
|
DirectInputDeviceKind GetDirectInputDeviceKind(REFGUID guid)
|
|
{
|
|
if (IsDirectInputKeyboardGuid(guid))
|
|
return DirectInputDeviceKind::Keyboard;
|
|
|
|
if (IsDirectInputMouseGuid(guid))
|
|
return DirectInputDeviceKind::Mouse;
|
|
|
|
return DirectInputDeviceKind::Other;
|
|
}
|
|
|
|
bool ShouldBlockDirectInputKeyboardLocked()
|
|
{
|
|
return _state.Initialized && _state.Focused && ShouldBlockKeyboardInputLocked();
|
|
}
|
|
|
|
bool ShouldBlockDirectInputMouseLocked()
|
|
{
|
|
return _state.Initialized && _state.Focused && ShouldBlockMouseInputLocked();
|
|
}
|
|
|
|
bool ShouldBlockDirectInputOtherLocked()
|
|
{
|
|
return _state.Initialized && (ShouldBlockKeyboardInputLocked() || ShouldBlockMouseInputLocked());
|
|
}
|
|
|
|
bool ShouldBlockDirectInputDeviceLocked(DirectInputDeviceKind kind)
|
|
{
|
|
switch (kind)
|
|
{
|
|
case DirectInputDeviceKind::Keyboard:
|
|
return ShouldBlockDirectInputKeyboardLocked();
|
|
|
|
case DirectInputDeviceKind::Mouse:
|
|
return ShouldBlockDirectInputMouseLocked();
|
|
|
|
case DirectInputDeviceKind::Other:
|
|
default:
|
|
return ShouldBlockDirectInputOtherLocked();
|
|
}
|
|
}
|
|
|
|
const char* DirectInputDeviceKindName(DirectInputDeviceKind kind)
|
|
{
|
|
switch (kind)
|
|
{
|
|
case DirectInputDeviceKind::Keyboard:
|
|
return "keyboard";
|
|
|
|
case DirectInputDeviceKind::Mouse:
|
|
return "mouse";
|
|
|
|
case DirectInputDeviceKind::Other:
|
|
default:
|
|
return "other";
|
|
}
|
|
}
|
|
|
|
template <typename T, std::size_t N>
|
|
DirectInputMethodHookSlot<T>*
|
|
FindDirectInputMethodHookByTargetLocked(std::array<DirectInputMethodHookSlot<T>, N>& hooks, T target)
|
|
{
|
|
if (target == nullptr)
|
|
return nullptr;
|
|
|
|
for (auto& slot : hooks)
|
|
{
|
|
if (slot.InUse && slot.Target == target)
|
|
return &slot;
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
template <typename T, std::size_t N>
|
|
DirectInputMethodHookSlot<T>* PrepareDirectInputMethodHookLocked(std::array<DirectInputMethodHookSlot<T>, N>& hooks,
|
|
T target, bool* needsAttach)
|
|
{
|
|
if (needsAttach != nullptr)
|
|
*needsAttach = false;
|
|
|
|
if (target == nullptr)
|
|
return nullptr;
|
|
|
|
if (auto* existing = FindDirectInputMethodHookByTargetLocked(hooks, target); existing != nullptr)
|
|
return existing;
|
|
|
|
for (auto& slot : hooks)
|
|
{
|
|
if (slot.InUse)
|
|
continue;
|
|
|
|
slot.InUse = true;
|
|
slot.Target = target;
|
|
slot.Trampoline = target;
|
|
|
|
if (needsAttach != nullptr)
|
|
*needsAttach = true;
|
|
|
|
return &slot;
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
template <typename T, std::size_t N>
|
|
bool HasDirectInputMethodHooksLocked(const std::array<DirectInputMethodHookSlot<T>, N>& hooks)
|
|
{
|
|
for (const auto& slot : hooks)
|
|
{
|
|
if (slot.InUse)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
template <typename T, std::size_t N>
|
|
T FirstDirectInputMethodTrampolineLocked(const std::array<DirectInputMethodHookSlot<T>, N>& hooks)
|
|
{
|
|
for (const auto& slot : hooks)
|
|
{
|
|
if (slot.InUse && slot.Trampoline != nullptr)
|
|
return slot.Trampoline;
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
void RefreshDirectInputDeviceHookStateLocked()
|
|
{
|
|
_state.DirectInputDeviceReleaseHookInstalled = HasDirectInputMethodHooksLocked(DirectInputReleaseHooks);
|
|
_state.DirectInputGetDeviceStateHookInstalled = HasDirectInputMethodHooksLocked(DirectInputGetDeviceStateHooks);
|
|
_state.DirectInputGetDeviceDataHookInstalled = HasDirectInputMethodHooksLocked(DirectInputGetDeviceDataHooks);
|
|
|
|
// Keep the legacy globals valid for diagnostics/compatibility, but do not use them to identify a target.
|
|
o_DirectInputCreateDeviceA = FirstDirectInputMethodTrampolineLocked(DirectInputCreateDeviceHooks);
|
|
o_DirectInputCreateDeviceW = o_DirectInputCreateDeviceA;
|
|
o_DirectInputDeviceRelease = FirstDirectInputMethodTrampolineLocked(DirectInputReleaseHooks);
|
|
o_DirectInputDeviceGetDeviceState = FirstDirectInputMethodTrampolineLocked(DirectInputGetDeviceStateHooks);
|
|
o_DirectInputDeviceGetDeviceData = FirstDirectInputMethodTrampolineLocked(DirectInputGetDeviceDataHooks);
|
|
}
|
|
|
|
void ClearDirectInputMethodHooksLocked()
|
|
{
|
|
DirectInputCreateDeviceHooks = {};
|
|
_state.DirectInputCreateDeviceAHookInstalled = false;
|
|
_state.DirectInputCreateDeviceWHookInstalled = false;
|
|
DirectInputReleaseHooks = {};
|
|
DirectInputGetDeviceStateHooks = {};
|
|
DirectInputGetDeviceDataHooks = {};
|
|
RefreshDirectInputDeviceHookStateLocked();
|
|
}
|
|
|
|
void MarkDirectInputDeviceKindSeenLocked(DirectInputDeviceKind kind)
|
|
{
|
|
if (kind == DirectInputDeviceKind::Keyboard)
|
|
_state.DirectInputKeyboardDeviceSeen = true;
|
|
else if (kind == DirectInputDeviceKind::Mouse)
|
|
_state.DirectInputMouseDeviceSeen = true;
|
|
else
|
|
_state.DirectInputOtherDeviceSeen = true;
|
|
}
|
|
|
|
HMODULE FindLoadedDirectInput8Module() { return GetModuleHandleW(DirectInput8ModuleName); }
|
|
|
|
HMODULE FindLoadedDirectInputLegacyModule() { return GetModuleHandleW(DirectInputLegacyModuleName); }
|
|
|
|
void ClearDirectInputHookPointersLocked()
|
|
{
|
|
o_DirectInput8Create = nullptr;
|
|
o_DirectInputCreateA = nullptr;
|
|
o_DirectInputCreateW = nullptr;
|
|
o_DirectInputCreateEx = nullptr;
|
|
o_DirectInputCreateDeviceA = nullptr;
|
|
o_DirectInputCreateDeviceW = nullptr;
|
|
|
|
_state.DirectInput8CreateHookInstalled = false;
|
|
_state.DirectInputCreateAHookInstalled = false;
|
|
_state.DirectInputCreateWHookInstalled = false;
|
|
_state.DirectInputCreateExHookInstalled = false;
|
|
_state.DirectInputCreateDeviceAHookInstalled = false;
|
|
_state.DirectInputCreateDeviceWHookInstalled = false;
|
|
|
|
ClearDirectInputMethodHooksLocked();
|
|
}
|
|
|
|
std::size_t FindDirectInputDeviceSlotLocked(void* device)
|
|
{
|
|
if (device == nullptr)
|
|
return MaxTrackedDirectInputDevices;
|
|
|
|
for (std::size_t i = 0; i < _state.DirectInputDeviceSlots.size(); ++i)
|
|
{
|
|
if (_state.DirectInputDeviceSlots[i].InUse && _state.DirectInputDeviceSlots[i].Device == device)
|
|
return i;
|
|
}
|
|
|
|
return MaxTrackedDirectInputDevices;
|
|
}
|
|
|
|
DirectInputDeviceKind GetDirectInputDeviceKindLocked(void* device)
|
|
{
|
|
const std::size_t slot = FindDirectInputDeviceSlotLocked(device);
|
|
|
|
if (slot >= MaxTrackedDirectInputDevices)
|
|
return DirectInputDeviceKind::Other;
|
|
|
|
return _state.DirectInputDeviceSlots[slot].Kind;
|
|
}
|
|
|
|
void ClearDirectInputDeviceSlotLocked(std::size_t slot)
|
|
{
|
|
if (slot >= MaxTrackedDirectInputDevices)
|
|
return;
|
|
|
|
if (_state.DirectInputDeviceSlots[slot].InUse && _state.DirectInputTrackedDeviceCount > 0)
|
|
_state.DirectInputTrackedDeviceCount--;
|
|
|
|
_state.DirectInputDeviceSlots[slot] = {};
|
|
}
|
|
|
|
void ClearAllDirectInputDeviceSlotsLocked()
|
|
{
|
|
_state.DirectInputDeviceSlots = {};
|
|
_state.DirectInputTrackedDeviceCount = 0;
|
|
}
|
|
|
|
void TrackDirectInputDeviceLocked(void* device, DirectInputDeviceKind kind,
|
|
std::optional<std::size_t> getDeviceDataHook)
|
|
{
|
|
if (device == nullptr)
|
|
return;
|
|
|
|
std::size_t freeSlot = MaxTrackedDirectInputDevices;
|
|
|
|
for (std::size_t i = 0; i < _state.DirectInputDeviceSlots.size(); ++i)
|
|
{
|
|
auto& slot = _state.DirectInputDeviceSlots[i];
|
|
|
|
if (slot.InUse && slot.Device == device)
|
|
{
|
|
if (getDeviceDataHook.has_value())
|
|
slot.GetDeviceDataHook = getDeviceDataHook;
|
|
|
|
// A later CreateDevice call may use an instance GUID that we cannot classify and
|
|
// therefore reports Other. Never downgrade a known keyboard/mouse classification.
|
|
if (slot.Kind == DirectInputDeviceKind::Other && kind != DirectInputDeviceKind::Other)
|
|
{
|
|
slot.Kind = kind;
|
|
MarkDirectInputDeviceKindSeenLocked(kind);
|
|
LOG_INFO("DirectInput device reclassified device:{} kind:{}", device, DirectInputDeviceKindName(kind));
|
|
}
|
|
else if (slot.Kind != DirectInputDeviceKind::Other && kind != DirectInputDeviceKind::Other &&
|
|
slot.Kind != kind)
|
|
{
|
|
LOG_WARN("DirectInput device kind mismatch device:{} existing:{} new:{}; preserving existing kind",
|
|
device, DirectInputDeviceKindName(slot.Kind), DirectInputDeviceKindName(kind));
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (!slot.InUse && freeSlot >= MaxTrackedDirectInputDevices)
|
|
freeSlot = i;
|
|
}
|
|
|
|
if (freeSlot >= MaxTrackedDirectInputDevices)
|
|
{
|
|
LOG_WARN("DirectInput device tracking table is full device:{} kind:{}", device,
|
|
DirectInputDeviceKindName(kind));
|
|
return;
|
|
}
|
|
|
|
auto& slot = _state.DirectInputDeviceSlots[freeSlot];
|
|
slot.InUse = true;
|
|
slot.Device = device;
|
|
slot.Kind = kind;
|
|
slot.GetDeviceDataHook = getDeviceDataHook;
|
|
|
|
_state.DirectInputTrackedDeviceCount++;
|
|
|
|
MarkDirectInputDeviceKindSeenLocked(kind);
|
|
|
|
LOG_INFO("DirectInput device captured device:{} kind:{}", device, DirectInputDeviceKindName(kind));
|
|
}
|
|
|
|
bool HookDirectInputDeviceLocked(void* device, DirectInputDeviceKind kind)
|
|
{
|
|
if (device == nullptr)
|
|
return false;
|
|
|
|
PVOID* vtable = *reinterpret_cast<PVOID**>(device);
|
|
|
|
auto release = reinterpret_cast<DirectInputDeviceRelease_t>(vtable[2]);
|
|
auto getDeviceState = reinterpret_cast<DirectInputGetDeviceState_t>(vtable[9]);
|
|
auto getDeviceData = reinterpret_cast<DirectInputGetDeviceData_t>(vtable[10]);
|
|
|
|
bool attachRelease = false;
|
|
bool attachGetDeviceState = false;
|
|
bool attachGetDeviceData = false;
|
|
|
|
auto* releaseHook = PrepareDirectInputMethodHookLocked(DirectInputReleaseHooks, release, &attachRelease);
|
|
auto* getDeviceStateHook =
|
|
PrepareDirectInputMethodHookLocked(DirectInputGetDeviceStateHooks, getDeviceState, &attachGetDeviceState);
|
|
auto* getDeviceDataHook =
|
|
PrepareDirectInputMethodHookLocked(DirectInputGetDeviceDataHooks, getDeviceData, &attachGetDeviceData);
|
|
|
|
bool completeCoverage = true;
|
|
|
|
if (release != nullptr && releaseHook == nullptr)
|
|
{
|
|
LOG_WARN("DirectInput Release hook table is full, device:{} target:{}", device,
|
|
reinterpret_cast<void*>(release));
|
|
completeCoverage = false;
|
|
}
|
|
|
|
if (getDeviceState != nullptr && getDeviceStateHook == nullptr)
|
|
{
|
|
LOG_WARN("DirectInput GetDeviceState hook table is full, device:{} target:{}", device,
|
|
reinterpret_cast<void*>(getDeviceState));
|
|
completeCoverage = false;
|
|
}
|
|
|
|
if (getDeviceData != nullptr && getDeviceDataHook == nullptr)
|
|
{
|
|
LOG_WARN("DirectInput GetDeviceData hook table is full, device:{} target:{}", device,
|
|
reinterpret_cast<void*>(getDeviceData));
|
|
completeCoverage = false;
|
|
}
|
|
|
|
std::optional<std::size_t> getDeviceDataIndex;
|
|
if (getDeviceDataHook != nullptr)
|
|
getDeviceDataIndex = SlotIndex(DirectInputGetDeviceDataHooks, getDeviceDataHook);
|
|
|
|
if (!attachRelease && !attachGetDeviceState && !attachGetDeviceData)
|
|
{
|
|
TrackDirectInputDeviceLocked(device, kind, getDeviceDataIndex);
|
|
return completeCoverage;
|
|
}
|
|
|
|
LONG result = NO_ERROR;
|
|
|
|
{
|
|
std::scoped_lock detourLock(GetDetourTransactionMutex());
|
|
DetourTransactionBegin();
|
|
DetourUpdateThread(GetCurrentThread());
|
|
|
|
if (attachRelease)
|
|
DetourAttach(reinterpret_cast<PVOID*>(&releaseHook->Trampoline),
|
|
DirectInputReleaseDetours[SlotIndex(DirectInputReleaseHooks, releaseHook)]);
|
|
|
|
if (attachGetDeviceState)
|
|
DetourAttach(
|
|
reinterpret_cast<PVOID*>(&getDeviceStateHook->Trampoline),
|
|
DirectInputGetDeviceStateDetours[SlotIndex(DirectInputGetDeviceStateHooks, getDeviceStateHook)]);
|
|
|
|
if (attachGetDeviceData)
|
|
DetourAttach(reinterpret_cast<PVOID*>(&getDeviceDataHook->Trampoline),
|
|
DirectInputGetDeviceDataDetours[*getDeviceDataIndex]);
|
|
|
|
result = DetourTransactionCommit();
|
|
}
|
|
|
|
if (result != NO_ERROR)
|
|
{
|
|
LOG_ERROR("DirectInput device hook installation failed result:{} device:{} kind:{}", result, device,
|
|
DirectInputDeviceKindName(kind));
|
|
|
|
if (attachRelease)
|
|
*releaseHook = {};
|
|
|
|
if (attachGetDeviceState)
|
|
*getDeviceStateHook = {};
|
|
|
|
if (attachGetDeviceData)
|
|
*getDeviceDataHook = {};
|
|
|
|
RefreshDirectInputDeviceHookStateLocked();
|
|
return false;
|
|
}
|
|
|
|
RefreshDirectInputDeviceHookStateLocked();
|
|
|
|
if (attachRelease)
|
|
LOG_INFO("DirectInput Release target detoured target:{} device:{}", reinterpret_cast<void*>(release), device);
|
|
|
|
if (attachGetDeviceState)
|
|
LOG_INFO("DirectInput GetDeviceState target detoured target:{} device:{}",
|
|
reinterpret_cast<void*>(getDeviceState), device);
|
|
|
|
if (attachGetDeviceData)
|
|
LOG_INFO("DirectInput GetDeviceData target detoured target:{} device:{}",
|
|
reinterpret_cast<void*>(getDeviceData), device);
|
|
|
|
TrackDirectInputDeviceLocked(device, kind, getDeviceDataIndex);
|
|
return completeCoverage;
|
|
}
|
|
|
|
bool HookDirectInputInterfaceLocked(void* directInput, bool wide)
|
|
{
|
|
if (directInput == nullptr)
|
|
return false;
|
|
|
|
PVOID* vtable = *reinterpret_cast<PVOID**>(directInput);
|
|
auto createDevice = reinterpret_cast<DirectInputCreateDevice_t>(vtable[3]);
|
|
|
|
if (createDevice == nullptr)
|
|
return false;
|
|
|
|
bool needsAttach = false;
|
|
auto* slot = PrepareDirectInputMethodHookLocked(DirectInputCreateDeviceHooks, createDevice, &needsAttach);
|
|
|
|
if (slot == nullptr)
|
|
{
|
|
LOG_ERROR("DirectInput CreateDevice hook table full wide:{} target:{}", wide ? 1 : 0,
|
|
reinterpret_cast<void*>(createDevice));
|
|
return false;
|
|
}
|
|
|
|
if (needsAttach)
|
|
{
|
|
// ANSI and Unicode CreateDevice have the same ABI, a shared A/W implementation is one slot
|
|
LONG result = NO_ERROR;
|
|
|
|
{
|
|
std::scoped_lock detourLock(GetDetourTransactionMutex());
|
|
DetourTransactionBegin();
|
|
DetourUpdateThread(GetCurrentThread());
|
|
DetourAttach(reinterpret_cast<PVOID*>(&slot->Trampoline),
|
|
DirectInputCreateDeviceDetours[SlotIndex(DirectInputCreateDeviceHooks, slot)]);
|
|
result = DetourTransactionCommit();
|
|
}
|
|
|
|
if (result != NO_ERROR)
|
|
{
|
|
LOG_ERROR("DirectInput CreateDevice hook installation failed result:{} wide:{} target:{}", result,
|
|
wide ? 1 : 0, reinterpret_cast<void*>(createDevice));
|
|
*slot = {};
|
|
RefreshDirectInputDeviceHookStateLocked();
|
|
return false;
|
|
}
|
|
|
|
LOG_INFO("DirectInput CreateDevice target detoured target:{}", reinterpret_cast<void*>(createDevice));
|
|
}
|
|
|
|
if (wide)
|
|
_state.DirectInputCreateDeviceWHookInstalled = true;
|
|
else
|
|
_state.DirectInputCreateDeviceAHookInstalled = true;
|
|
|
|
RefreshDirectInputDeviceHookStateLocked();
|
|
return true;
|
|
}
|
|
|
|
bool TryGetDirectInputInterfaceWidth(REFIID riid, bool* wide)
|
|
{
|
|
if (wide == nullptr)
|
|
return false;
|
|
|
|
if (IsEqualGUID(riid, IID_IDirectInput8W) || IsEqualGUID(riid, IID_IDirectInput7W) ||
|
|
IsEqualGUID(riid, IID_IDirectInput2W) || IsEqualGUID(riid, IID_IDirectInputW))
|
|
{
|
|
*wide = true;
|
|
return true;
|
|
}
|
|
|
|
if (IsEqualGUID(riid, IID_IDirectInput8A) || IsEqualGUID(riid, IID_IDirectInput7A) ||
|
|
IsEqualGUID(riid, IID_IDirectInput2A) || IsEqualGUID(riid, IID_IDirectInputA))
|
|
{
|
|
*wide = false;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void HandleDirectInputCreatedLocked(REFIID riid, void** out, const char* source)
|
|
{
|
|
if (out == nullptr || *out == nullptr)
|
|
return;
|
|
|
|
bool wide = false;
|
|
if (TryGetDirectInputInterfaceWidth(riid, &wide))
|
|
{
|
|
HookDirectInputInterfaceLocked(*out, wide);
|
|
return;
|
|
}
|
|
|
|
OPTIINPUT_LOG_VERBOSE("{} returned unsupported riid directInput:{} riid:{}",
|
|
source != nullptr ? source : "DirectInput", *out, static_cast<const void*>(&riid));
|
|
}
|
|
|
|
void HandleLegacyDirectInputCreatedLocked(void** out, bool wide)
|
|
{
|
|
if (out == nullptr || *out == nullptr)
|
|
return;
|
|
|
|
HookDirectInputInterfaceLocked(*out, wide);
|
|
}
|
|
|
|
bool InstallDirectInputExportHook(HMODULE module, const char* exportName, void** original, void* hook, bool* installed)
|
|
{
|
|
if (module == nullptr || exportName == nullptr || original == nullptr || hook == nullptr || installed == nullptr)
|
|
return false;
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
|
|
if (*installed)
|
|
return true;
|
|
}
|
|
|
|
void* resolved = reinterpret_cast<void*>(KernelBaseProxy::GetProcAddress_()(module, exportName));
|
|
|
|
if (resolved == nullptr)
|
|
return false;
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
|
|
if (*installed)
|
|
return true;
|
|
|
|
*original = resolved;
|
|
}
|
|
|
|
LONG result = NO_ERROR;
|
|
|
|
{
|
|
std::scoped_lock detourLock(GetDetourTransactionMutex());
|
|
DetourTransactionBegin();
|
|
DetourUpdateThread(GetCurrentThread());
|
|
DetourAttach(reinterpret_cast<PVOID*>(original), hook);
|
|
result = DetourTransactionCommit();
|
|
}
|
|
|
|
std::unique_lock lock(_state.Mutex);
|
|
|
|
if (result != NO_ERROR)
|
|
{
|
|
LOG_ERROR("{} hook installation failed result:{}", exportName, result);
|
|
*original = nullptr;
|
|
return false;
|
|
}
|
|
|
|
*installed = true;
|
|
LOG_INFO("{} hook installed module:{}", exportName, static_cast<void*>(module));
|
|
return true;
|
|
}
|
|
|
|
// A joystick read with c_dfDIJoystick(2) at rest: 0 is full left/up for axes and north for POV hats
|
|
void FillNeutralJoystickState(void* device, DIJOYSTATE* state)
|
|
{
|
|
std::memset(state->rgdwPOV, 0xFF, sizeof(state->rgdwPOV));
|
|
|
|
using GetProperty_t = HRESULT(WINAPI*)(void* device, REFGUID property, LPDIPROPHEADER header);
|
|
auto getProperty = reinterpret_cast<GetProperty_t>((*reinterpret_cast<PVOID**>(device))[5]);
|
|
|
|
// lX to lRz and the two sliders are consecutive LONGs, each centered in its range
|
|
for (DWORD offset = 0; offset <= offsetof(DIJOYSTATE, rglSlider[1]); offset += sizeof(LONG))
|
|
{
|
|
DIPROPRANGE range {};
|
|
range.diph = { sizeof(DIPROPRANGE), sizeof(DIPROPHEADER), offset, DIPH_BYOFFSET };
|
|
|
|
if (SUCCEEDED(getProperty(device, DIPROP_RANGE, &range.diph)))
|
|
*reinterpret_cast<LONG*>(reinterpret_cast<BYTE*>(state) + offset) =
|
|
range.lMin + (range.lMax - range.lMin) / 2;
|
|
}
|
|
}
|
|
|
|
HRESULT CallDirectInputCreateDeviceOriginal(DirectInputCreateDevice_t original, void* directInput, REFGUID guid,
|
|
void** device, LPUNKNOWN outer)
|
|
{
|
|
if (original == nullptr)
|
|
return DIERR_GENERIC;
|
|
|
|
ScopedHookBypass bypass;
|
|
return original(directInput, guid, device, outer);
|
|
}
|
|
} // namespace
|
|
|
|
void UpdateDirectInputIntegration()
|
|
{
|
|
// Module discovery can enter the loader. Do it before publishing state or installing hooks.
|
|
HMODULE module8 = FindLoadedDirectInput8Module();
|
|
HMODULE legacyModule = FindLoadedDirectInputLegacyModule();
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
_state.DirectInputModule = module8;
|
|
_state.DirectInputLegacyModule = legacyModule;
|
|
_state.DirectInputModuleLoaded = module8 != nullptr || legacyModule != nullptr;
|
|
_state.DirectInputLegacyModuleLoaded = legacyModule != nullptr;
|
|
}
|
|
|
|
if (module8 != nullptr)
|
|
{
|
|
if (!InstallDirectInputExportHook(module8, DirectInput8CreateExportName,
|
|
reinterpret_cast<void**>(&o_DirectInput8Create), hkDirectInput8Create,
|
|
&_state.DirectInput8CreateHookInstalled))
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
|
|
if (o_DirectInput8Create == nullptr)
|
|
{
|
|
OPTIINPUT_LOG_VERBOSE("DirectInput8Create export was not found module:{}", static_cast<void*>(module8));
|
|
}
|
|
}
|
|
}
|
|
|
|
if (legacyModule != nullptr)
|
|
{
|
|
InstallDirectInputExportHook(legacyModule, DirectInputCreateAExportName,
|
|
reinterpret_cast<void**>(&o_DirectInputCreateA), hkDirectInputCreateA,
|
|
&_state.DirectInputCreateAHookInstalled);
|
|
|
|
InstallDirectInputExportHook(legacyModule, DirectInputCreateWExportName,
|
|
reinterpret_cast<void**>(&o_DirectInputCreateW), hkDirectInputCreateW,
|
|
&_state.DirectInputCreateWHookInstalled);
|
|
|
|
InstallDirectInputExportHook(legacyModule, DirectInputCreateExExportName,
|
|
reinterpret_cast<void**>(&o_DirectInputCreateEx), hkDirectInputCreateEx,
|
|
&_state.DirectInputCreateExHookInstalled);
|
|
}
|
|
}
|
|
|
|
bool RemoveDirectInputHooksLocked()
|
|
{
|
|
if (!_state.DirectInput8CreateHookInstalled && !_state.DirectInputCreateAHookInstalled &&
|
|
!_state.DirectInputCreateWHookInstalled && !_state.DirectInputCreateExHookInstalled &&
|
|
!_state.DirectInputCreateDeviceAHookInstalled && !_state.DirectInputCreateDeviceWHookInstalled &&
|
|
!_state.DirectInputGetDeviceStateHookInstalled && !_state.DirectInputGetDeviceDataHookInstalled &&
|
|
!_state.DirectInputDeviceReleaseHookInstalled)
|
|
{
|
|
ClearDirectInputHookPointersLocked();
|
|
ClearAllDirectInputDeviceSlotsLocked();
|
|
return true;
|
|
}
|
|
|
|
DetourTransactionBegin();
|
|
DetourUpdateThread(GetCurrentThread());
|
|
|
|
if (_state.DirectInput8CreateHookInstalled && o_DirectInput8Create != nullptr)
|
|
DetourDetach(reinterpret_cast<PVOID*>(&o_DirectInput8Create), hkDirectInput8Create);
|
|
|
|
if (_state.DirectInputCreateAHookInstalled && o_DirectInputCreateA != nullptr)
|
|
DetourDetach(reinterpret_cast<PVOID*>(&o_DirectInputCreateA), hkDirectInputCreateA);
|
|
|
|
if (_state.DirectInputCreateWHookInstalled && o_DirectInputCreateW != nullptr)
|
|
DetourDetach(reinterpret_cast<PVOID*>(&o_DirectInputCreateW), hkDirectInputCreateW);
|
|
|
|
if (_state.DirectInputCreateExHookInstalled && o_DirectInputCreateEx != nullptr)
|
|
DetourDetach(reinterpret_cast<PVOID*>(&o_DirectInputCreateEx), hkDirectInputCreateEx);
|
|
|
|
for (std::size_t i = 0; i < DirectInputCreateDeviceHooks.size(); ++i)
|
|
{
|
|
auto& slot = DirectInputCreateDeviceHooks[i];
|
|
if (slot.InUse && slot.Trampoline != nullptr)
|
|
DetourDetach(reinterpret_cast<PVOID*>(&slot.Trampoline), DirectInputCreateDeviceDetours[i]);
|
|
}
|
|
|
|
for (std::size_t i = 0; i < DirectInputGetDeviceStateHooks.size(); ++i)
|
|
{
|
|
auto& slot = DirectInputGetDeviceStateHooks[i];
|
|
if (slot.InUse && slot.Trampoline != nullptr)
|
|
DetourDetach(reinterpret_cast<PVOID*>(&slot.Trampoline), DirectInputGetDeviceStateDetours[i]);
|
|
}
|
|
|
|
for (std::size_t i = 0; i < DirectInputGetDeviceDataHooks.size(); ++i)
|
|
{
|
|
auto& slot = DirectInputGetDeviceDataHooks[i];
|
|
if (slot.InUse && slot.Trampoline != nullptr)
|
|
DetourDetach(reinterpret_cast<PVOID*>(&slot.Trampoline), DirectInputGetDeviceDataDetours[i]);
|
|
}
|
|
|
|
for (std::size_t i = 0; i < DirectInputReleaseHooks.size(); ++i)
|
|
{
|
|
auto& slot = DirectInputReleaseHooks[i];
|
|
if (slot.InUse && slot.Trampoline != nullptr)
|
|
DetourDetach(reinterpret_cast<PVOID*>(&slot.Trampoline), DirectInputReleaseDetours[i]);
|
|
}
|
|
|
|
const LONG result = DetourTransactionCommit();
|
|
|
|
if (result != NO_ERROR)
|
|
{
|
|
LOG_WARN("DirectInput hook removal failed result:{}; retaining trampoline tables for a safe retry", result);
|
|
return false;
|
|
}
|
|
|
|
ClearDirectInputHookPointersLocked();
|
|
ClearAllDirectInputDeviceSlotsLocked();
|
|
return true;
|
|
}
|
|
|
|
void DrainDirectInputBufferedDataLocked()
|
|
{
|
|
for (DirectInputDeviceSlot& deviceSlot : _state.DirectInputDeviceSlots)
|
|
{
|
|
if (!deviceSlot.InUse || deviceSlot.Device == nullptr || !deviceSlot.GetDeviceDataHook.has_value())
|
|
continue;
|
|
|
|
DirectInputGetDeviceData_t original = DirectInputGetDeviceDataHooks[*deviceSlot.GetDeviceDataHook].Trampoline;
|
|
|
|
if (original == nullptr)
|
|
continue;
|
|
|
|
const DWORD objectDataSize =
|
|
deviceSlot.LastObjectDataSize != 0 ? deviceSlot.LastObjectDataSize : sizeof(DIDEVICEOBJECTDATA);
|
|
DWORD flushCount = INFINITE;
|
|
|
|
ScopedHookBypass bypass;
|
|
original(deviceSlot.Device, objectDataSize, nullptr, &flushCount, 0);
|
|
}
|
|
}
|
|
|
|
HRESULT WINAPI hkDirectInput8Create(HINSTANCE instance, DWORD version, REFIID riid, LPVOID* out, LPUNKNOWN outer)
|
|
{
|
|
HRESULT result = DIERR_GENERIC;
|
|
|
|
if (o_DirectInput8Create != nullptr)
|
|
{
|
|
ScopedHookBypass bypass;
|
|
result = o_DirectInput8Create(instance, version, riid, out, outer);
|
|
}
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
_state.DirectInputCreateCallCount++;
|
|
|
|
if (SUCCEEDED(result))
|
|
{
|
|
_state.DirectInputCreateSucceededCount++;
|
|
HandleDirectInputCreatedLocked(riid, reinterpret_cast<void**>(out), "DirectInput8Create");
|
|
}
|
|
else
|
|
{
|
|
_state.DirectInputCreateFailedCount++;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
HRESULT WINAPI hkDirectInputCreateA(HINSTANCE instance, DWORD version, void** out, LPUNKNOWN outer)
|
|
{
|
|
HRESULT result = DIERR_GENERIC;
|
|
|
|
if (o_DirectInputCreateA != nullptr)
|
|
{
|
|
ScopedHookBypass bypass;
|
|
result = o_DirectInputCreateA(instance, version, out, outer);
|
|
}
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
_state.DirectInputCreateCallCount++;
|
|
|
|
if (SUCCEEDED(result))
|
|
{
|
|
_state.DirectInputCreateSucceededCount++;
|
|
HandleLegacyDirectInputCreatedLocked(out, false);
|
|
}
|
|
else
|
|
{
|
|
_state.DirectInputCreateFailedCount++;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
HRESULT WINAPI hkDirectInputCreateW(HINSTANCE instance, DWORD version, void** out, LPUNKNOWN outer)
|
|
{
|
|
HRESULT result = DIERR_GENERIC;
|
|
|
|
if (o_DirectInputCreateW != nullptr)
|
|
{
|
|
ScopedHookBypass bypass;
|
|
result = o_DirectInputCreateW(instance, version, out, outer);
|
|
}
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
_state.DirectInputCreateCallCount++;
|
|
|
|
if (SUCCEEDED(result))
|
|
{
|
|
_state.DirectInputCreateSucceededCount++;
|
|
HandleLegacyDirectInputCreatedLocked(out, true);
|
|
}
|
|
else
|
|
{
|
|
_state.DirectInputCreateFailedCount++;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
HRESULT WINAPI hkDirectInputCreateEx(HINSTANCE instance, DWORD version, REFIID riid, LPVOID* out, LPUNKNOWN outer)
|
|
{
|
|
HRESULT result = DIERR_GENERIC;
|
|
|
|
if (o_DirectInputCreateEx != nullptr)
|
|
{
|
|
ScopedHookBypass bypass;
|
|
result = o_DirectInputCreateEx(instance, version, riid, out, outer);
|
|
}
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
_state.DirectInputCreateCallCount++;
|
|
|
|
if (SUCCEEDED(result))
|
|
{
|
|
_state.DirectInputCreateSucceededCount++;
|
|
HandleDirectInputCreatedLocked(riid, reinterpret_cast<void**>(out), "DirectInputCreateEx");
|
|
}
|
|
else
|
|
{
|
|
_state.DirectInputCreateFailedCount++;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
template <std::size_t Slot>
|
|
HRESULT WINAPI hkDirectInputCreateDevice(void* directInput, REFGUID guid, void** device, LPUNKNOWN outer)
|
|
{
|
|
DirectInputCreateDevice_t original = nullptr;
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
original = DirectInputCreateDeviceHooks[Slot].Trampoline;
|
|
}
|
|
|
|
HRESULT result = CallDirectInputCreateDeviceOriginal(original, directInput, guid, device, outer);
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
_state.DirectInputCreateDeviceCallCount++;
|
|
|
|
if (SUCCEEDED(result) && device != nullptr && *device != nullptr)
|
|
{
|
|
_state.DirectInputCreateDeviceSucceededCount++;
|
|
HookDirectInputDeviceLocked(*device, GetDirectInputDeviceKind(guid));
|
|
}
|
|
else if (FAILED(result))
|
|
{
|
|
_state.DirectInputCreateDeviceFailedCount++;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
template <std::size_t Slot> HRESULT WINAPI hkDirectInputGetDeviceState(void* device, DWORD dataSize, LPVOID data)
|
|
{
|
|
DirectInputGetDeviceState_t original = nullptr;
|
|
DirectInputDeviceKind kind = DirectInputDeviceKind::Other;
|
|
bool shouldBlock = false;
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
kind = GetDirectInputDeviceKindLocked(device);
|
|
_state.DirectInputGetDeviceStateCallCount++;
|
|
shouldBlock = ShouldBlockDirectInputDeviceLocked(kind);
|
|
|
|
if (shouldBlock)
|
|
_state.DirectInputGetDeviceStateBlockedCount++;
|
|
else
|
|
_state.DirectInputGetDeviceStatePassedCount++;
|
|
|
|
original = DirectInputGetDeviceStateHooks[Slot].Trampoline;
|
|
}
|
|
|
|
if (shouldBlock)
|
|
{
|
|
if (data != nullptr && dataSize > 0)
|
|
std::memset(data, 0, dataSize);
|
|
|
|
// Outside the lock, it asks the device for its axis ranges
|
|
if (data != nullptr && kind == DirectInputDeviceKind::Other &&
|
|
(dataSize == sizeof(DIJOYSTATE) || dataSize == sizeof(DIJOYSTATE2)))
|
|
{
|
|
ScopedHookBypass bypass;
|
|
FillNeutralJoystickState(device, static_cast<DIJOYSTATE*>(data));
|
|
}
|
|
|
|
OPTIINPUT_LOG_VERBOSE("blocking DirectInput GetDeviceState device:{} kind:{} size:{}", device,
|
|
DirectInputDeviceKindName(kind), dataSize);
|
|
return DI_OK;
|
|
}
|
|
|
|
if (original == nullptr)
|
|
return DIERR_GENERIC;
|
|
|
|
ScopedHookBypass bypass;
|
|
return original(device, dataSize, data);
|
|
}
|
|
|
|
template <std::size_t Slot>
|
|
HRESULT WINAPI hkDirectInputGetDeviceData(void* device, DWORD objectDataSize, LPDIDEVICEOBJECTDATA data, LPDWORD inOut,
|
|
DWORD flags)
|
|
{
|
|
DirectInputGetDeviceData_t original = nullptr;
|
|
DirectInputDeviceKind kind = DirectInputDeviceKind::Other;
|
|
bool shouldBlock = false;
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
kind = GetDirectInputDeviceKindLocked(device);
|
|
_state.DirectInputGetDeviceDataCallCount++;
|
|
shouldBlock = ShouldBlockDirectInputDeviceLocked(kind);
|
|
|
|
if (shouldBlock)
|
|
_state.DirectInputGetDeviceDataBlockedCount++;
|
|
else
|
|
_state.DirectInputGetDeviceDataPassedCount++;
|
|
|
|
const std::size_t deviceSlotIndex = FindDirectInputDeviceSlotLocked(device);
|
|
if (deviceSlotIndex < MaxTrackedDirectInputDevices && objectDataSize != 0)
|
|
_state.DirectInputDeviceSlots[deviceSlotIndex].LastObjectDataSize = objectDataSize;
|
|
|
|
original = DirectInputGetDeviceDataHooks[Slot].Trampoline;
|
|
}
|
|
|
|
if (!shouldBlock)
|
|
{
|
|
if (original == nullptr)
|
|
return DIERR_GENERIC;
|
|
|
|
ScopedHookBypass bypass;
|
|
return original(device, objectDataSize, data, inOut, flags);
|
|
}
|
|
|
|
// GetDeviceData is backed by a buffered event queue. Returning zero without
|
|
// touching the real queue lets menu-time events replay after closing the
|
|
// overlay. Flush the device buffer, then present an empty successful read.
|
|
if (original != nullptr)
|
|
{
|
|
DWORD flushCount = INFINITE;
|
|
ScopedHookBypass bypass;
|
|
original(device, objectDataSize, nullptr, &flushCount, 0);
|
|
}
|
|
|
|
if (inOut != nullptr)
|
|
*inOut = 0;
|
|
|
|
OPTIINPUT_LOG_VERBOSE("blocking DirectInput GetDeviceData device:{} kind:{} flags:{}", device,
|
|
DirectInputDeviceKindName(kind), flags);
|
|
return DI_OK;
|
|
}
|
|
|
|
template <std::size_t Slot> ULONG WINAPI hkDirectInputDeviceRelease(void* device)
|
|
{
|
|
DirectInputDeviceRelease_t original = nullptr;
|
|
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
original = DirectInputReleaseHooks[Slot].Trampoline;
|
|
}
|
|
|
|
ULONG result = 0;
|
|
|
|
if (original != nullptr)
|
|
{
|
|
ScopedHookBypass bypass;
|
|
result = original(device);
|
|
}
|
|
|
|
if (result == 0)
|
|
{
|
|
std::unique_lock lock(_state.Mutex);
|
|
const std::size_t slot = FindDirectInputDeviceSlotLocked(device);
|
|
|
|
if (slot < MaxTrackedDirectInputDevices)
|
|
ClearDirectInputDeviceSlotLocked(slot);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
} // namespace OptiInput
|