mirror of
https://github.com/LizardByte/libvirtualhid.git
synced 2026-12-03 13:45:45 +09:00
fix(windows): restore the fourth XInput slot for VHF Xbox gamepads (#244)
This commit is contained in:
1 parent
20298972c5
commit
7c2b82e5d7
4 files changed
+114
-25
No files matched your search
@@ -551,7 +551,7 @@ namespace {
|
||||
*/
|
||||
NTSTATUS create_vhf_device(WDFDEVICE device, const std::shared_ptr<DeviceRecord> &record) {
|
||||
GUID container_id {};
|
||||
auto status = lvh::detail::windows::assign_vhf_container_id(record->request.device_type, container_id);
|
||||
auto status = lvh::detail::windows::assign_vhf_container_id(record->request, container_id);
|
||||
if (!NT_SUCCESS(status)) {
|
||||
trace_status("create_vhf_device assign_vhf_container_id", status);
|
||||
return status;
|
||||
|
||||
@@ -45,25 +45,6 @@ namespace lvh::detail::windows {
|
||||
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];
|
||||
@@ -118,6 +99,26 @@ namespace lvh::detail::windows {
|
||||
return is_xbox_gamepad(gamepad_kind);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Select the Plug and Play container for a VHF device.
|
||||
*
|
||||
* Separate containers distinguish virtual mice from integrated pointing
|
||||
* devices subject to touchpad click suppression. Each XInputHID gamepad
|
||||
* also gets its own container so its device nodes can be grouped as one
|
||||
* controller.
|
||||
*
|
||||
* @param request Device creation request.
|
||||
* @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(const LvhWindowsCreateDeviceRequest &request, GUID &container_id) {
|
||||
container_id = {};
|
||||
if (request.device_type == LVH_WINDOWS_DEVICE_MOUSE || (request.device_type == LVH_WINDOWS_DEVICE_GAMEPAD && uses_xinputhid_match_id(request.gamepad_kind))) {
|
||||
return generate_random_guid(container_id);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline std::wstring make_hardware_ids(const LvhWindowsCreateDeviceRequest &request) {
|
||||
const auto &ids = request.hardware_ids;
|
||||
std::wstring hardware_ids;
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
@@ -825,6 +826,70 @@ TEST_F(WindowsConsumerTest, XInputDoesNotExposeSubmittedXboxBattery) {
|
||||
ASSERT_TRUE(created.adapter->close().ok());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Verify that four simultaneous VHF Xbox controllers each supply XInput state.
|
||||
*/
|
||||
TEST_F(WindowsConsumerTest, FourVhfXboxGamepadsPublishDistinctXInputState) {
|
||||
XInputApi xinput;
|
||||
ASSERT_TRUE(xinput);
|
||||
const auto previous_slots = current_xinput_slots(xinput);
|
||||
if (!previous_slots.empty()) {
|
||||
GTEST_SKIP() << "The four-controller test requires all XInput slots to be free";
|
||||
}
|
||||
|
||||
lvh::RuntimeOptions runtime_options;
|
||||
runtime_options.backend = lvh::BackendKind::platform_default;
|
||||
auto runtime = lvh::Runtime::create(runtime_options);
|
||||
ASSERT_TRUE(runtime->capabilities().supports_gamepad)
|
||||
<< "The installed libvirtualhid Windows driver is required for this integration test";
|
||||
|
||||
using Button = lvh::GamepadButton;
|
||||
constexpr std::array markers {Button::a, Button::b, Button::x, Button::y};
|
||||
const std::set<WORD> expected {XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y};
|
||||
constexpr WORD marker_mask = XINPUT_GAMEPAD_A | XINPUT_GAMEPAD_B | XINPUT_GAMEPAD_X | XINPUT_GAMEPAD_Y;
|
||||
std::array<std::unique_ptr<lvh::GamepadStateAdapter>, XUSER_MAX_COUNT> gamepads;
|
||||
for (std::size_t index = 0; index < gamepads.size(); ++index) {
|
||||
SCOPED_TRACE(index);
|
||||
lvh::CreateGamepadOptions options;
|
||||
options.profile = lvh::profiles::xbox_series();
|
||||
options.metadata.global_index = static_cast<int>(index);
|
||||
options.metadata.client_relative_index = static_cast<int>(index);
|
||||
options.metadata.client_type = lvh::ClientControllerType::xbox;
|
||||
options.metadata.stable_id = std::format("xinput-four-gamepads-{}", index);
|
||||
auto created = lvh::GamepadStateAdapter::create(*runtime, options);
|
||||
ASSERT_TRUE(created) << created.status.message();
|
||||
ASSERT_TRUE(created.adapter->set_button(markers.at(index), true).ok());
|
||||
gamepads.at(index) = std::move(created.adapter);
|
||||
if (index == 0 && !wait_for_new_xinput_slot(xinput, previous_slots).has_value()) {
|
||||
GTEST_SKIP() << "XInput did not enumerate even one VHF gamepad in this Windows environment";
|
||||
}
|
||||
}
|
||||
|
||||
std::set<WORD> observed;
|
||||
const auto deadline = std::chrono::steady_clock::now() + 5s;
|
||||
// A changed report delivers the held marker if the initial report preceded XInput enumeration.
|
||||
bool shoulder_pressed = false;
|
||||
do {
|
||||
shoulder_pressed = !shoulder_pressed;
|
||||
for (const auto &gamepad : gamepads) {
|
||||
ASSERT_TRUE(gamepad->set_button(Button::left_shoulder, shoulder_pressed).ok());
|
||||
}
|
||||
std::this_thread::sleep_for(50ms);
|
||||
observed.clear();
|
||||
for (DWORD slot = 0; slot < XUSER_MAX_COUNT; ++slot) {
|
||||
XINPUT_STATE state {};
|
||||
if (xinput.state(slot, &state) == ERROR_SUCCESS) {
|
||||
observed.insert(static_cast<WORD>(state.Gamepad.wButtons & marker_mask));
|
||||
}
|
||||
}
|
||||
} while (observed != expected && std::chrono::steady_clock::now() < deadline);
|
||||
|
||||
EXPECT_EQ(observed, expected);
|
||||
for (const auto &gamepad : gamepads) {
|
||||
ASSERT_TRUE(gamepad->close().ok());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(WindowsConsumerTest, Xbox360PublishesXInputStateAndRumble) {
|
||||
XInputApi xinput;
|
||||
ASSERT_TRUE(xinput);
|
||||
|
||||
@@ -163,10 +163,12 @@ TEST_F(WindowsDriverProtocolTest, VhfResponseAcceptsExactlySizedRequests) {
|
||||
* @brief Verify that separate VHF mouse instances receive distinct container GUIDs.
|
||||
*/
|
||||
TEST_F(WindowsDriverProtocolTest, VhfMiceUseSeparateDeviceContainers) {
|
||||
LvhWindowsCreateDeviceRequest request {};
|
||||
request.device_type = LVH_WINDOWS_DEVICE_MOUSE;
|
||||
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);
|
||||
ASSERT_EQ(lvh::detail::windows::assign_vhf_container_id(request, first_container), 0);
|
||||
ASSERT_EQ(lvh::detail::windows::assign_vhf_container_id(request, second_container), 0);
|
||||
EXPECT_NE(first_container, second_container);
|
||||
for (const auto &container : {first_container, second_container}) {
|
||||
EXPECT_EQ(container.Data3 & 0xF000U, 0x4000U);
|
||||
@@ -175,13 +177,34 @@ TEST_F(WindowsDriverProtocolTest, VhfMiceUseSeparateDeviceContainers) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Verify that VHF keyboards and gamepads keep using the default container.
|
||||
* @brief Verify that VHF Xbox One and Xbox Series instances get unique containers.
|
||||
*/
|
||||
TEST_F(WindowsDriverProtocolTest, VhfKeyboardsAndGamepadsUseDefaultDeviceContainer) {
|
||||
TEST_F(WindowsDriverProtocolTest, VhfXboxGamepadsUseSeparateDeviceContainers) {
|
||||
auto request = series_request();
|
||||
for (const auto gamepad_kind : {LVH_WINDOWS_GAMEPAD_XBOX_ONE, LVH_WINDOWS_GAMEPAD_XBOX_SERIES}) {
|
||||
SCOPED_TRACE(gamepad_kind);
|
||||
request.gamepad_kind = gamepad_kind;
|
||||
std::array<GUID, 4> containers {};
|
||||
for (auto &container : containers) {
|
||||
ASSERT_EQ(lvh::detail::windows::assign_vhf_container_id(request, container), 0);
|
||||
EXPECT_EQ(container.Data3 & 0xF000U, 0x4000U);
|
||||
EXPECT_EQ(container.Data4[0] & 0xC0U, 0x80U);
|
||||
EXPECT_EQ(std::ranges::count(containers, container), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Verify that VHF keyboards and non-Xbox gamepads use the default container.
|
||||
*/
|
||||
TEST_F(WindowsDriverProtocolTest, VhfKeyboardAndNonXboxGamepadUseDefaultDeviceContainer) {
|
||||
for (const auto device_type : {LVH_WINDOWS_DEVICE_KEYBOARD, LVH_WINDOWS_DEVICE_GAMEPAD}) {
|
||||
SCOPED_TRACE(device_type);
|
||||
LvhWindowsCreateDeviceRequest request {};
|
||||
request.device_type = device_type;
|
||||
request.gamepad_kind = LVH_WINDOWS_GAMEPAD_GENERIC;
|
||||
GUID container {.Data1 = 1};
|
||||
ASSERT_EQ(lvh::detail::windows::assign_vhf_container_id(device_type, container), 0);
|
||||
ASSERT_EQ(lvh::detail::windows::assign_vhf_container_id(request, container), 0);
|
||||
EXPECT_EQ(container, GUID {});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user