Skip to content

Commit

Permalink
Fix RogAlly button support (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie authored Nov 3, 2023
1 parent bfbb07b commit bf12a03
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 54 deletions.
111 changes: 58 additions & 53 deletions HandheldCompanion/Devices/ASUS/ROGAlly.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
using HandheldCompanion.Devices.ASUS;
using HandheldCompanion.Inputs;
using HandheldCompanion.Managers;
using HandheldCompanion.Utils;
using HidLibrary;
using Nefarius.Utilities.DeviceManagement.PnP;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Numerics;
using System.Text;
using WindowsInput.Events;
using Task = System.Threading.Tasks.Task;
using System.Threading.Tasks;

namespace HandheldCompanion.Devices;

Expand All @@ -21,33 +17,19 @@ 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;
private Dictionary<byte, HidDevice> hidDevices = new();
private AsusACPI asusACPI;

private enum AuraMode
{
Static = 0,
Breathe = 1,
Cycle = 2,
Rainbow = 3,
Strobe = 4,
}
private const byte INPUT_HID_ID = 0x5a;

private enum AuraSpeed
{
Slow = 0xeb,
Medium = 0xf5,
Fast = 0xe1,
}
public override bool IsOpen => hidDevices.ContainsKey(INPUT_HID_ID) && hidDevices[INPUT_HID_ID].IsOpen && asusACPI is not null && asusACPI.IsOpen();

public ROGAlly()
{
Expand Down Expand Up @@ -106,16 +88,11 @@ 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);

return true;
}

Expand All @@ -125,40 +102,60 @@ public override void Close()
if (asusACPI is not null)
asusACPI.Close();

// clear array
if (hidDevice is not null)
hidDevice.CloseDevice();
// close devices
foreach (HidDevice hidDevice in hidDevices.Values)
hidDevice.CloseDevice();

base.Close();
}

public void WatcherEventArrived(object sender, EventArrivedEventArgs e)
{
if (e.NewEvent is null) return;
int EventID = int.Parse(e.NewEvent["EventID"].ToString());
LogManager.LogDebug("WMI event {0}", EventID);
HandleEvent((byte)EventID);
}

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

if (device.ReadFeatureData(out byte[] data, INPUT_HID_ID))
{
device.OpenDevice();
device.MonitorDeviceEvents = true;

hidDevices[INPUT_HID_ID] = device;

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

hidDevices.TryGetValue(INPUT_HID_ID, out HidDevice hidDevice);
if (hidDevice is null || !hidDevice.IsConnected)
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));

// get key
byte key = result.Data[0];
HandleEvent(key);
}

public override void SetFanControl(bool enable)
{
if (!asusACPI.IsOpen())
if (!IsOpen)
return;

switch (enable)
Expand All @@ -171,7 +168,7 @@ public override void SetFanControl(bool enable)

public override void SetFanDuty(double percent)
{
if (!asusACPI.IsOpen())
if (!IsOpen)
return;

asusACPI.SetFanSpeed(AsusFan.CPU, Convert.ToByte(percent));
Expand All @@ -180,6 +177,9 @@ public override void SetFanDuty(double percent)

public override float ReadFanDuty()
{
if (!IsOpen)
return 100.0f;

int cpuFan = asusACPI.DeviceGet(AsusACPI.CPU_Fan);
int gpuFan = asusACPI.DeviceGet(AsusACPI.GPU_Fan);
return (cpuFan + gpuFan) / 2 * 100;
Expand All @@ -204,10 +204,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 @@ -218,10 +224,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 All @@ -231,4 +236,4 @@ private void HandleEvent(byte key)
break;
}
}
}
}
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="LiveCharts.Wpf.Core" Version="0.9.8" />
<PackageReference Include="MathConverter" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
Expand Down Expand Up @@ -2268,6 +2267,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 bf12a03

Please sign in to comment.