Skip to content

Commit

Permalink
Merge pull request #40 from evilC/feature/vigem-live
Browse files Browse the repository at this point in the history
Feature/vigem live
  • Loading branch information
evilC authored Dec 14, 2019
2 parents e4eb9f4 + c007a1d commit 5c158d0
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 43 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]
### Added
- Provider Report now contains `ErrorMessage` property
If the provider is not live, this should contain a string indicating why
### Changed
- [ViGEm Provider] Do not show devices if Bus Driver not installed
### Deprecated
### Removed
### Fixed
Expand Down
70 changes: 43 additions & 27 deletions Source/Core Providers/Core_ViGEm/Core_ViGEm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,68 @@
using Hidwizards.IOWrapper.Libraries.ProviderLogger;
using HidWizards.IOWrapper.DataTransferObjects;
using HidWizards.IOWrapper.ProviderInterface.Interfaces;
using HidWizards.IOWrapper.Core.Devcon;

namespace Core_ViGEm
{
[Export(typeof(IProvider))]
public partial class Core_ViGEm : IOutputProvider
//public class Core_ViGEm
{
public bool IsLive { get { return isLive; } }
private bool isLive;
public bool IsLive => _isLive;
private bool _isLive;

private Logger logger;

private static ViGEmClient client;
Xbox360Controller[] xboxControllers = new Xbox360Controller[4];
private OutputDevicesHandler devicesHandler = new OutputDevicesHandler();
private static ViGEmClient _client;
private readonly OutputDevicesHandler devicesHandler = new OutputDevicesHandler();

private ProviderReport providerReport;
private readonly ProviderReport _providerReport;

private static Guid InterfaceGuid => Guid.Parse("{96E42B22-F5E9-42F8-B043-ED0F932F014F}");

public Core_ViGEm()
{
logger = new Logger(ProviderName);
_providerReport = new ProviderReport
{
Title = "ViGEm",
API = "ViGEm",
Description = "Allows emulation of Gamepads (Xbox, PS etc)",
ProviderDescriptor = new ProviderDescriptor
{
ProviderName = ProviderName
}
};

InitLibrary();
}

private void InitLibrary()
{
if (client == null)
string msg;
var busFound = Devcon.FindDeviceByInterfaceId(InterfaceGuid, out var path, out _);
if (busFound)
{
try
{
client = new ViGEmClient();
_client = new ViGEmClient();
_isLive = true;
msg = "ViGem Client Loaded OK";
}
catch
{
_isLive = false;
msg = "ViGem Bus driver does seems to be present, but initialization of ViGEm Client failed";
_providerReport.ErrorMessage = msg;
}
catch { }
}
isLive = (client != null);
logger.Log("ViGem Client is {0}!", (isLive ? "Loaded" : "NOT Loaded"));
else
{
_isLive = false;
msg = "ViGem Bus driver does not seem to be installed";
_providerReport.ErrorMessage = msg;
}
logger.Log(msg);
}

#region IProvider Members
Expand All @@ -54,18 +80,8 @@ private void InitLibrary()

public ProviderReport GetOutputList()
{
providerReport = new ProviderReport
{
Title = "ViGEm",
API = "ViGEm",
Description = "Allows emulation of Gamepads (Xbox, PS etc)",
ProviderDescriptor = new ProviderDescriptor
{
ProviderName = ProviderName
}
};
providerReport.Devices = devicesHandler.GetDeviceList();
return providerReport;
_providerReport.Devices = _isLive ? devicesHandler.GetDeviceList() : null;
return _providerReport;
}

public DeviceReport GetOutputDeviceReport(DeviceDescriptor deviceDescriptor)
Expand All @@ -75,21 +91,21 @@ public DeviceReport GetOutputDeviceReport(DeviceDescriptor deviceDescriptor)

public bool SetOutputState(OutputSubscriptionRequest subReq, BindingDescriptor bindingDescriptor, int state)
{
if (!isLive)
if (!_isLive)
return false;
return devicesHandler.SetOutputState(subReq, bindingDescriptor, state);
}

public bool SubscribeOutputDevice(OutputSubscriptionRequest subReq)
{
if (!isLive)
if (!_isLive)
return false;
return devicesHandler.SubscribeOutput(subReq);
}

public bool UnSubscribeOutputDevice(OutputSubscriptionRequest subReq)
{
if (!isLive)
if (!_isLive)
return false;
return devicesHandler.UnsubscribeOutput(subReq);
}
Expand Down
4 changes: 4 additions & 0 deletions Source/Core Providers/Core_ViGEm/Core_ViGEm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
<Compile Include="Xb360Handler.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Core\Core.csproj">
<Project>{D9447B59-4B62-4BD8-BE91-5F5AB1D8F1BD}</Project>
<Name>Core</Name>
</ProjectReference>
<ProjectReference Include="..\..\DataObjects\DTOs.csproj">
<Project>{81851977-0421-4ebd-b066-0fe03f02589e}</Project>
<Name>DTOs</Name>
Expand Down
26 changes: 13 additions & 13 deletions Source/Core Providers/Core_ViGEm/DS4Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public partial class Core_ViGEm
/// </summary>
private class DS4Handler : DeviceHandler
{
private DualShock4Report report = new DualShock4Report();
private readonly DualShock4Report _report = new DualShock4Report();

private static readonly List<DualShock4Axes> axisIndexes = new List<DualShock4Axes>
private static readonly List<DualShock4Axes> AxisIndexes = new List<DualShock4Axes>
{
DualShock4Axes.LeftThumbX, DualShock4Axes.LeftThumbY, DualShock4Axes.RightThumbX, DualShock4Axes.RightThumbY,
DualShock4Axes.LeftTrigger, DualShock4Axes.RightTrigger
Expand All @@ -28,15 +28,15 @@ private class DS4Handler : DeviceHandler
"LX", "LY", "RX", "RY", "L2 (LT)", "R2 (RT)"
};

private static readonly List<DualShock4Buttons> buttonIndexes = new List<DualShock4Buttons>
private static readonly List<DualShock4Buttons> ButtonIndexes = new List<DualShock4Buttons>
{
DualShock4Buttons.Cross, DualShock4Buttons.Circle, DualShock4Buttons.Square, DualShock4Buttons.Triangle,
DualShock4Buttons.ShoulderLeft, DualShock4Buttons.ShoulderRight, DualShock4Buttons.ThumbLeft, DualShock4Buttons.ThumbRight,
DualShock4Buttons.Share, DualShock4Buttons.Options,
DualShock4Buttons.TriggerLeft, DualShock4Buttons.TriggerRight
};

private static readonly List<DualShock4SpecialButtons> specialButtonIndexes = new List<DualShock4SpecialButtons>
private static readonly List<DualShock4SpecialButtons> SpecialButtonIndexes = new List<DualShock4SpecialButtons>
{
DualShock4SpecialButtons.Ps, DualShock4SpecialButtons.Touchpad
};
Expand All @@ -46,7 +46,7 @@ private class DS4Handler : DeviceHandler
"Cross", "Circle", "Square", "Triangle", "L1", "R1", "LS", "RS", "Share", "Options", "L2", "R2", "PS", "TouchPad Click"
};

private static readonly List<DualShock4DPadValues> povIndexes = new List<DualShock4DPadValues>
private static readonly List<DualShock4DPadValues> PovIndexes = new List<DualShock4DPadValues>
{
DualShock4DPadValues.North, DualShock4DPadValues.East, DualShock4DPadValues.South, DualShock4DPadValues.West
};
Expand Down Expand Up @@ -83,7 +83,7 @@ public DS4Handler(DeviceClassDescriptor descriptor, int index) : base(descriptor

protected override void AcquireTarget()
{
target = new DualShock4Controller(client);
target = new DualShock4Controller(_client);
target.Connect();
}

Expand All @@ -96,20 +96,20 @@ protected override void RelinquishTarget()
protected override void SetAxisState(BindingDescriptor bindingDescriptor, int state)
{
var inputId = bindingDescriptor.Index;
report.SetAxis(axisIndexes[inputId], (byte)((state + 32768) / 256));
_report.SetAxis(AxisIndexes[inputId], (byte)((state + 32768) / 256));
SendReport();
}

protected override void SetButtonState(BindingDescriptor bindingDescriptor, int state)
{
var inputId = bindingDescriptor.Index;
if (inputId >= buttonIndexes.Count)
if (inputId >= ButtonIndexes.Count)
{
report.SetSpecialButtonState(specialButtonIndexes[inputId - buttonIndexes.Count], state != 0);
_report.SetSpecialButtonState(SpecialButtonIndexes[inputId - ButtonIndexes.Count], state != 0);
}
else
{
report.SetButtonState(buttonIndexes[inputId], state != 0);
_report.SetButtonState(ButtonIndexes[inputId], state != 0);
}
SendReport();
}
Expand All @@ -123,17 +123,17 @@ protected override void SetPovState(BindingDescriptor bindingDescriptor, int sta
if (axisState == newState) return;
_povAxisStates[mapping.Axis] = newState;

var buttons = (int)report.Buttons;
var buttons = (int)_report.Buttons;
buttons &= ~15; // Clear all the Dpad bits

buttons |= (int)AxisStatesToDpadValue[(_povAxisStates["x"], _povAxisStates["y"])];
report.Buttons = (ushort) buttons;
_report.Buttons = (ushort) buttons;
SendReport();
}

private void SendReport()
{
((DualShock4Controller)target).SendReport(report);
((DualShock4Controller)target).SendReport(_report);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core Providers/Core_ViGEm/Xb360Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Xb360Handler(DeviceClassDescriptor descriptor, int index) : base(descript

protected override void AcquireTarget()
{
target = new Xbox360Controller(client);
target = new Xbox360Controller(_client);
target.Connect();
}

Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Devcon\Devcon.cs" />
<Compile Include="Devcon\Devcon.Native.cs" />
<Compile Include="GenericMEFPluginLoader.cs" />
<Compile Include="IOController.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Loading

0 comments on commit 5c158d0

Please sign in to comment.