Skip to content

Commit

Permalink
[WindowsDX/UAP] fix GamePad race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
nkast committed Jan 16, 2022
1 parent 38b209b commit 78a0e97
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
52 changes: 29 additions & 23 deletions MonoGame.Framework/Input/GamePad.UWP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
// file 'LICENSE.txt', which is part of this source code package.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using WGI = Windows.Gaming.Input;

namespace Microsoft.Xna.Framework.Input
Expand All @@ -18,30 +15,40 @@ static partial class GamePad

internal static bool Back;

private static Dictionary<int, WGI.Gamepad> _gamepads;
private static WGI.Gamepad[] _gamepads;
static int tmp;

static GamePad()
{
_gamepads = new Dictionary<int, WGI.Gamepad>();
_gamepads = new WGI.Gamepad[PlatformGetMaxNumberOfGamePads()];
var gamepadsTmp = WGI.Gamepad.Gamepads;
tmp = gamepadsTmp.Count; // workaround UAP bug. first call to 'WGI.Gamepad.Gamepads' returns an empty instance.
var gamepads = WGI.Gamepad.Gamepads;
for (int i = 0; i < gamepads.Count; i++)
for (int i = 0; i < _gamepads.Length && i < gamepads.Count; i++)
_gamepads[i] = gamepads[i];

WGI.Gamepad.GamepadAdded += (o, e) =>
{
var index = 0;
while (_gamepads.ContainsKey(index))
index++;

_gamepads[index] = e;
for (int i = 0; i < _gamepads.Length; i++)
{
if (_gamepads[i] == null)
{
_gamepads[i] = e;
break;
}
}
};

WGI.Gamepad.GamepadRemoved += (o, e) =>
{
int? key = _gamepads.FirstOrDefault(x => x.Value == e).Key;

if (key.HasValue)
_gamepads.Remove(key.Value);
for (int i = 0; i < _gamepads.Length; i++)
{
if (_gamepads[i] == e)
{
_gamepads[i] = null;
break;
}
}
};
}

Expand All @@ -52,10 +59,9 @@ private static int PlatformGetMaxNumberOfGamePads()

private static GamePadCapabilities PlatformGetCapabilities(int index)
{
if (!_gamepads.ContainsKey(index))
return new GamePadCapabilities();

var gamepad = _gamepads[index];
if (gamepad == null)
return new GamePadCapabilities();

// we can't check gamepad capabilities for most stuff with Windows.Gaming.Input.Gamepad
return new GamePadCapabilities
Expand Down Expand Up @@ -98,10 +104,11 @@ private static GamePadState GetDefaultState()

private static GamePadState PlatformGetState(int index, GamePadDeadZone leftDeadZoneMode, GamePadDeadZone rightDeadZoneMode)
{
if (!_gamepads.ContainsKey(index))
var gamepad = _gamepads[index];
if (gamepad == null)
return (index == 0 ? GetDefaultState() : GamePadState.Default);

var state = _gamepads[index].GetCurrentReading();
var state = gamepad.GetCurrentReading();

var sticks = new GamePadThumbSticks(
new Vector2((float)state.LeftThumbstickX, (float)state.LeftThumbstickY),
Expand Down Expand Up @@ -151,10 +158,9 @@ private static GamePadState PlatformGetState(int index, GamePadDeadZone leftDead

private static bool PlatformSetVibration(int index, float leftMotor, float rightMotor, float leftTrigger, float rightTrigger)
{
if (!_gamepads.ContainsKey(index))
return false;

var gamepad = _gamepads[index];
if (gamepad == null)
return false;

gamepad.Vibration = new WGI.GamepadVibration
{
Expand Down
18 changes: 17 additions & 1 deletion MonoGame.Framework/Input/GamePad.XInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,23 @@ private static GamePadCapabilities PlatformGetCapabilities(int index)
return new GamePadCapabilities();
}

var capabilities = controller.GetCapabilities(SharpDX.XInput.DeviceQueryType.Any);
SharpDX.XInput.Capabilities capabilities;
try
{
capabilities = controller.GetCapabilities(SharpDX.XInput.DeviceQueryType.Any);
}
catch (SharpDX.SharpDXException ex)
{
const int DeviceNotConnectedHResult = unchecked((int)0x8007048f);
if (ex.ResultCode.Code == DeviceNotConnectedHResult)
{
_connected[index] = false;
SetDisconnectedTimeout(index);
return new GamePadCapabilities();
}
throw;
}

var ret = new GamePadCapabilities();
switch (capabilities.SubType)
{
Expand Down

0 comments on commit 78a0e97

Please sign in to comment.