Skip to content

Commit

Permalink
Improve ROGAlly OEM buttons support (#54)
Browse files Browse the repository at this point in the history
* tentative

* improve ROGAlly OEM buttons reading
  • Loading branch information
Valkirie authored Nov 1, 2023
1 parent 9a3e78e commit 2ef7c44
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
77 changes: 58 additions & 19 deletions HandheldCompanion/Devices/ASUS/ROGAlly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
using System;
using System.Collections.Generic;
using System.Windows.Media;
using System.Linq;
using System.Management;
using System.Numerics;
using System.Text;
using WindowsInput.Events;
using Task = System.Threading.Tasks.Task;
using static HandheldCompanion.Utils.DeviceUtils;
using System.Threading.Tasks;

namespace HandheldCompanion.Devices;

Expand All @@ -22,13 +22,11 @@ public class ROGAlly : IDevice
private readonly Dictionary<byte, ButtonFlags> keyMapping = new()
{
{ 0, ButtonFlags.None },
{ 56, ButtonFlags.OEM2 },
{ 162, ButtonFlags.None },
{ 166, ButtonFlags.OEM1 },
{ 56, ButtonFlags.OEM2 },
{ 165, ButtonFlags.OEM3 },
{ 167, ButtonFlags.OEM4 },
{ 168, ButtonFlags.OEM4 },
{ 236, ButtonFlags.None }
};

private HidDevice hidDevice;
Expand All @@ -47,7 +45,7 @@ public class ROGAlly : IDevice
static byte[] MESSAGE_APPLY = { AURA_HID_ID, 0xb4 };
static byte[] MESSAGE_SET = { AURA_HID_ID, 0xb5, 0, 0, 0 };

public override bool IsOpen => hidDevice is not null && asusACPI is not null && asusACPI.IsOpen();
public override bool IsOpen => hidDevice is not null && hidDevice.IsOpen && asusACPI is not null && asusACPI.IsOpen();

private enum AuraMode
{
Expand Down Expand Up @@ -167,15 +165,13 @@ public override bool Open()
if (!success)
return false;

if (hidDevice is not null)
hidDevice.OpenDevice();

// try open asus ACPI
asusACPI = new AsusACPI();
if (asusACPI is null)
return false;

asusACPI.SubscribeToEvents(WatcherEventArrived);
// deprecated
// asusACPI.SubscribeToEvents(WatcherEventArrived);

return true;
}
Expand Down Expand Up @@ -203,20 +199,58 @@ public void WatcherEventArrived(object sender, EventArrivedEventArgs e)

public override bool IsReady()
{
hidDevice = GetHidDevices(_vid, _pid).FirstOrDefault();
IEnumerable<HidDevice> devices = GetHidDevices(_vid, _pid);
foreach (HidDevice device in devices)
{
if (!device.IsConnected)
continue;

try
{
device.OpenDevice();
device.MonitorDeviceEvents = true;
}
catch
{
continue;
}

Task<HidReport> ReportDevice = Task.Run(async () => await device.ReadReportAsync());
ReportDevice.ContinueWith(t => OnReport(ReportDevice.Result, device));

hidDevice = device;
}

if (hidDevice is null)
return false;

var pnpDevice = PnPDevice.GetDeviceByInterfaceId(hidDevice.DevicePath);
var device_parent = pnpDevice.GetProperty<string>(DevicePropertyKey.Device_Parent);
PnPDevice pnpDevice = PnPDevice.GetDeviceByInterfaceId(hidDevice.DevicePath);
string device_parent = pnpDevice.GetProperty<string>(DevicePropertyKey.Device_Parent);

var pnpParent = PnPDevice.GetDeviceByInstanceId(device_parent);
var parent_guid = pnpParent.GetProperty<Guid>(DevicePropertyKey.Device_ClassGuid);
var parent_instanceId = pnpParent.GetProperty<string>(DevicePropertyKey.Device_InstanceId);
PnPDevice pnpParent = PnPDevice.GetDeviceByInstanceId(device_parent);
Guid parent_guid = pnpParent.GetProperty<Guid>(DevicePropertyKey.Device_ClassGuid);
string parent_instanceId = pnpParent.GetProperty<string>(DevicePropertyKey.Device_InstanceId);

return DeviceHelper.IsDeviceAvailable(parent_guid, parent_instanceId);
}

private void OnReport(HidReport result, HidDevice device)
{
Task<HidReport> ReportDevice = Task.Run(async () => await device.ReadReportAsync());
ReportDevice.ContinueWith(t => OnReport(ReportDevice.Result, device));

switch(result.ReportId)
{
case 90:
{
// get key
byte key = result.Data[0];
HandleEvent(key);
}
break;
}
}

public override void SetFanControl(bool enable, int mode = 0)
{
if (!IsOpen)
Expand Down Expand Up @@ -268,10 +302,16 @@ private void HandleEvent(byte key)
return;

// get button
var button = keyMapping[key];

ButtonFlags button = keyMapping[key];
switch (key)
{
case 0: // Back paddles: Release
{
KeyRelease(ButtonFlags.OEM3);
}
return;

case 165: // Back paddles: Press
case 167: // Armory crate: Hold
KeyPress(button);
break;
Expand All @@ -282,10 +322,9 @@ private void HandleEvent(byte key)

default:
case 56: // Armory crate: Click
case 165: // Back paddles: Click
case 166: // Command center: Click
{
Task.Run(async () =>
Task.Factory.StartNew(async () =>
{
KeyPress(button);
await Task.Delay(KeyPressDelay);
Expand Down
4 changes: 3 additions & 1 deletion HandheldCompanion/HandheldCompanion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@
<PackageReference Include="Crc32.NET" Version="1.2.0" />
<PackageReference Include="GregsStack.InputSimulatorStandard" Version="1.3.5" />
<PackageReference Include="HelixToolkit.Core.Wpf" Version="2.24.0" />
<PackageReference Include="hidlibrary" Version="3.3.40" />
<PackageReference Include="LibreHardwareMonitorLib" Version="0.9.3-pre256" />
<PackageReference Include="LiveCharts.Wpf.Core" Version="0.9.8" />
<PackageReference Include="MathConverter" Version="2.2.0" />
Expand Down Expand Up @@ -2270,6 +2269,9 @@
<Reference Include="Gma.System.MouseKeyHook">
<HintPath>..\Resources\Gma.System.MouseKeyHook.dll</HintPath>
</Reference>
<Reference Include="HidLibrary">
<HintPath>..\Resources\HidLibrary.dll</HintPath>
</Reference>
<Reference Include="Inkore.UI.WPF.Modern">
<HintPath>..\Resources\Inkore.UI.WPF.Modern.dll</HintPath>
</Reference>
Expand Down
Binary file added Resources/HidLibrary.dll
Binary file not shown.

0 comments on commit 2ef7c44

Please sign in to comment.