Skip to content

Commit

Permalink
allow app to run if RawInput API fails for gamepad
Browse files Browse the repository at this point in the history
  • Loading branch information
Zergatul committed May 4, 2022
1 parent 670e0b6 commit 9457ccf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/RawInput/Device/RawDeviceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,19 @@ private RawGamepadDevice CreateGamepadDevice(IntPtr hDevice, RID_DEVICE_INFO_HID
NativeMemoryBlock preparsedData = _buffer.ToMemoryBlock(size);

HIDP_CAPS caps = default;
if (HidP_GetCaps(preparsedData.Pointer, ref caps) != HidPStatus.HIDP_STATUS_SUCCESS)
HidPStatus status = HidP_GetCaps(preparsedData.Pointer, ref caps);
if (status != HidPStatus.HIDP_STATUS_SUCCESS)
{
_logger.LogError($"Cannot get gamepad caps {FormatWin32Error(Marshal.GetLastWin32Error())}.");
_logger.LogError($"Cannot get gamepad caps {FormatHidPStatus(status)} {FormatWin32Error(Marshal.GetLastWin32Error())}.");
return null;
}

ushort numberButtonsCaps = caps.NumberInputButtonCaps;
HIDP_BUTTON_CAPS[] buttonCaps = new HIDP_BUTTON_CAPS[numberButtonsCaps];
if (HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, buttonCaps, ref numberButtonsCaps, preparsedData.Pointer) != HidPStatus.HIDP_STATUS_SUCCESS)
status = HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, buttonCaps, ref numberButtonsCaps, preparsedData.Pointer);
if (status != HidPStatus.HIDP_STATUS_SUCCESS)
{
_logger.LogError($"Cannot get gamepad button caps {FormatWin32Error(Marshal.GetLastWin32Error())}.");
_logger.LogError($"Cannot get gamepad button caps {FormatHidPStatus(status)} {FormatWin32Error(Marshal.GetLastWin32Error())}.");
return null;
}

Expand All @@ -168,9 +170,10 @@ private RawGamepadDevice CreateGamepadDevice(IntPtr hDevice, RID_DEVICE_INFO_HID

ushort numberValueCaps = caps.NumberInputValueCaps;
HIDP_VALUE_CAPS[] valueCaps = new HIDP_VALUE_CAPS[numberValueCaps];
if (HidP_GetValueCaps(HIDP_REPORT_TYPE.HidP_Input, valueCaps, ref numberValueCaps, preparsedData.Pointer) != HidPStatus.HIDP_STATUS_SUCCESS)
status = HidP_GetValueCaps(HIDP_REPORT_TYPE.HidP_Input, valueCaps, ref numberValueCaps, preparsedData.Pointer);
if (status != HidPStatus.HIDP_STATUS_SUCCESS)
{
_logger.LogError($"Cannot get gamepad value caps {FormatWin32Error(Marshal.GetLastWin32Error())}.");
_logger.LogError($"Cannot get gamepad value caps {FormatHidPStatus(status)} {FormatWin32Error(Marshal.GetLastWin32Error())}.");
return null;
}

Expand Down
6 changes: 6 additions & 0 deletions src/RawInput/RawDeviceInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ private void ProcessHidEvent(IntPtr hDevice, RAWHID hid)
}
}

if (device == null)
{
// device was not properly initialized
return;
}

if (device is not RawGamepadDevice gamepad)
{
_logger.LogWarning($"RawHid event invalid device: {device.GetType()}.");
Expand Down
12 changes: 12 additions & 0 deletions src/WinApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public static string FormatWin32Error(int errorCode)
}
}

internal static string FormatHidPStatus(WinApi.Hid.HidPStatus status)
{
if (Enum.IsDefined(status))
{
return $"({status} hex={FormatErrorCode((int)status)} dec={(int)status})";
}
else
{
return $"(hex={FormatErrorCode((int)status)} dec={(int)status})";
}
}

private static string FormatErrorCode(int code)
{
return "0x" + code.ToString("X2").PadLeft(8, '0');
Expand Down
2 changes: 1 addition & 1 deletion src/Zergatul.Obs.InputOverlay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RootNamespace>Zergatul.Obs.InputOverlay</RootNamespace>
<Authors>Zergatul</Authors>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>2.2.1</Version>
<Version>2.2.2</Version>
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
<StartupObject>Zergatul.Obs.InputOverlay.Program</StartupObject>
<PackageProjectUrl>https://github.com/Zergatul/Zergatul.Obs.InputOverlay</PackageProjectUrl>
Expand Down

0 comments on commit 9457ccf

Please sign in to comment.