mirror of
https://github.com/LizardByte/libvirtualhid.git
synced 2026-12-03 13:45:45 +09:00
fix(windows): isolate virtual mice from touchpad click suppression (#238)
This commit is contained in:
1 parent
45443d6f4a
commit
20298972c5
4 files changed
+97
-18
No files matched your search
@@ -8,11 +8,11 @@
|
||||
|
||||
#include "xbox_360_software_device.hpp"
|
||||
|
||||
#include "windows_device_identity.hpp"
|
||||
#include "xbox_360_protocol.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bcrypt.h>
|
||||
#include <bit>
|
||||
#include <cfgmgr32.h>
|
||||
#include <combaseapi.h>
|
||||
@@ -144,20 +144,6 @@ namespace lvh::windows {
|
||||
return message;
|
||||
}
|
||||
|
||||
bool random_guid(GUID &guid) {
|
||||
std::array<std::byte, sizeof(GUID)> random_bytes {};
|
||||
if (const auto status = ::BCryptGenRandom(nullptr, std::bit_cast<PUCHAR>(random_bytes.data()), static_cast<ULONG>(random_bytes.size()), BCRYPT_USE_SYSTEM_PREFERRED_RNG); status != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
constexpr auto variant_byte_offset = offsetof(GUID, Data4);
|
||||
random_bytes[variant_byte_offset] =
|
||||
(random_bytes[variant_byte_offset] & std::byte {0x3FU}) | std::byte {0x80U};
|
||||
guid = std::bit_cast<GUID>(random_bytes);
|
||||
guid.Data3 = static_cast<unsigned short>((guid.Data3 & 0x0FFFU) | 0x4000U);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::wstring guid_string(const GUID &guid) {
|
||||
std::array<wchar_t, 40> buffer {};
|
||||
if (::StringFromGUID2(guid, buffer.data(), static_cast<int>(buffer.size())) == 0) {
|
||||
@@ -387,7 +373,7 @@ namespace lvh::windows {
|
||||
|
||||
GUID container_id {};
|
||||
GUID instance_guid {};
|
||||
if (!random_guid(container_id) || !random_guid(instance_guid)) {
|
||||
if (detail::windows::generate_random_guid(container_id) != 0 || detail::windows::generate_random_guid(instance_guid) != 0) {
|
||||
message = "Unable to generate Xbox 360 software-device identities.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -542,8 +542,22 @@ namespace {
|
||||
delete_vhf_devices_for_device(device);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create and start a VHF device with the container selected for its type.
|
||||
*
|
||||
* @param device UMDF control device owning the VHF target.
|
||||
* @param record State for the new virtual device.
|
||||
* @return Windows status for device creation and startup.
|
||||
*/
|
||||
NTSTATUS create_vhf_device(WDFDEVICE device, const std::shared_ptr<DeviceRecord> &record) {
|
||||
auto status = initialize_vhf_target(device, record);
|
||||
GUID container_id {};
|
||||
auto status = lvh::detail::windows::assign_vhf_container_id(record->request.device_type, container_id);
|
||||
if (!NT_SUCCESS(status)) {
|
||||
trace_status("create_vhf_device assign_vhf_container_id", status);
|
||||
return status;
|
||||
}
|
||||
|
||||
status = initialize_vhf_target(device, record);
|
||||
if (!NT_SUCCESS(status)) {
|
||||
return status;
|
||||
}
|
||||
@@ -566,6 +580,7 @@ namespace {
|
||||
vhf_config.VendorID = record->request.hardware_ids.vendor_id;
|
||||
vhf_config.ProductID = record->request.hardware_ids.product_id;
|
||||
vhf_config.VersionNumber = record->request.hardware_ids.device_version;
|
||||
vhf_config.ContainerID = container_id;
|
||||
vhf_config.HardwareIDsLength = static_cast<USHORT>(record->hardware_ids.size() * sizeof(wchar_t));
|
||||
vhf_config.HardwareIDs = record->hardware_ids.data();
|
||||
vhf_config.EvtVhfReadyForNextReadReport = LvhEvtVhfReadyForNextReadReport;
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
/**
|
||||
* @file src/platform/windows/shared/windows_device_identity.hpp
|
||||
* @brief Windows virtual HID device hardware-ID helpers shared by the driver and tests.
|
||||
* @brief Windows virtual HID device identity helpers.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
// platform includes
|
||||
#include <Windows.h>
|
||||
// BCrypt declarations depend on Windows types.
|
||||
#include <bcrypt.h>
|
||||
|
||||
// standard includes
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
@@ -13,6 +21,49 @@
|
||||
|
||||
namespace lvh::detail::windows {
|
||||
|
||||
/**
|
||||
* @brief Generate a version 4 GUID for one virtual device instance.
|
||||
*
|
||||
* @param guid Destination for the generated GUID on success.
|
||||
* @return Status from the Windows random number generator.
|
||||
*/
|
||||
inline NTSTATUS generate_random_guid(GUID &guid) {
|
||||
std::array<std::byte, sizeof(GUID)> random_bytes {};
|
||||
const auto status = ::BCryptGenRandom(
|
||||
nullptr,
|
||||
std::bit_cast<PUCHAR>(random_bytes.data()),
|
||||
static_cast<ULONG>(random_bytes.size()),
|
||||
BCRYPT_USE_SYSTEM_PREFERRED_RNG
|
||||
);
|
||||
if (status == 0) {
|
||||
constexpr auto variant_byte_offset = offsetof(GUID, Data4);
|
||||
random_bytes[variant_byte_offset] =
|
||||
(random_bytes[variant_byte_offset] & std::byte {0x3FU}) | std::byte {0x80U};
|
||||
guid = std::bit_cast<GUID>(random_bytes);
|
||||
guid.Data3 = static_cast<unsigned short>((guid.Data3 & 0x0FFFU) | 0x4000U);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Select the Plug and Play container for a VHF device.
|
||||
*
|
||||
* A separate container lets Windows distinguish each virtual mouse from an
|
||||
* integrated pointing device eligible for touchpad click suppression.
|
||||
* Keyboards and gamepads use VHF's default container selection.
|
||||
*
|
||||
* @param device_type Windows protocol device type.
|
||||
* @param container_id Destination for the GUID, or a zero GUID for the default.
|
||||
* @return Zero on success or a Windows error if GUID generation fails.
|
||||
*/
|
||||
inline NTSTATUS assign_vhf_container_id(std::uint32_t device_type, GUID &container_id) {
|
||||
container_id = {};
|
||||
if (device_type == LVH_WINDOWS_DEVICE_MOUSE) {
|
||||
return generate_random_guid(container_id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
constexpr wchar_t hardware_id_hex_digit(std::uint16_t value) {
|
||||
constexpr auto digits = L"0123456789ABCDEF";
|
||||
return digits[value & 0x0FU];
|
||||
|
||||
@@ -159,6 +159,33 @@ TEST_F(WindowsDriverProtocolTest, VhfResponseAcceptsExactlySizedRequests) {
|
||||
EXPECT_EQ(unnumbered_buffer, (std::array<std::uint8_t, 4> {0, 1, 2, 3}));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Verify that separate VHF mouse instances receive distinct container GUIDs.
|
||||
*/
|
||||
TEST_F(WindowsDriverProtocolTest, VhfMiceUseSeparateDeviceContainers) {
|
||||
GUID first_container {};
|
||||
GUID second_container {};
|
||||
ASSERT_EQ(lvh::detail::windows::assign_vhf_container_id(LVH_WINDOWS_DEVICE_MOUSE, first_container), 0);
|
||||
ASSERT_EQ(lvh::detail::windows::assign_vhf_container_id(LVH_WINDOWS_DEVICE_MOUSE, second_container), 0);
|
||||
EXPECT_NE(first_container, second_container);
|
||||
for (const auto &container : {first_container, second_container}) {
|
||||
EXPECT_EQ(container.Data3 & 0xF000U, 0x4000U);
|
||||
EXPECT_EQ(container.Data4[0] & 0xC0U, 0x80U);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Verify that VHF keyboards and gamepads keep using the default container.
|
||||
*/
|
||||
TEST_F(WindowsDriverProtocolTest, VhfKeyboardsAndGamepadsUseDefaultDeviceContainer) {
|
||||
for (const auto device_type : {LVH_WINDOWS_DEVICE_KEYBOARD, LVH_WINDOWS_DEVICE_GAMEPAD}) {
|
||||
SCOPED_TRACE(device_type);
|
||||
GUID container {.Data1 = 1};
|
||||
ASSERT_EQ(lvh::detail::windows::assign_vhf_container_id(device_type, container), 0);
|
||||
EXPECT_EQ(container, GUID {});
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(WindowsDriverProtocolTest, SeriesUsesCapturedXInputHidIdentity) {
|
||||
auto request = series_request();
|
||||
request.hardware_ids.product_id = 0x0B12;
|
||||
|
||||
Reference in new issue
Block a user