Skip to content

Commit

Permalink
Add virtual controller service
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesCJ60 committed Dec 11, 2023
1 parent c6767c9 commit e566d93
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 2 deletions.
128 changes: 128 additions & 0 deletions Universal x86 Tuning Utility Handheld/Scripts/ControllerControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using Nefarius.Drivers.HidHide;
using Nefarius.ViGEm.Client;
using Nefarius.ViGEm.Client.Targets;
using Nefarius.ViGEm.Client.Targets.Xbox360;
using SharpDX.XInput;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Universal_x86_Tuning_Utility_Handheld.Scripts.Misc;

namespace Universal_x86_Tuning_Utility_Handheld.Scripts
{
internal class ControllerControl
{

static HidHideControlService con = new HidHideControlService();
static ViGEmClient viGEmClient = new ViGEmClient();
static IXbox360Controller targetController = viGEmClient.CreateXbox360Controller();

public static bool isStarted = false;

public static async void SetUp()
{
await Task.Run(() =>
{
HideOriginalController(false);
targetController.Connect();
while (isStarted)
{
EmulateController();
Thread.Sleep(10);
}
});
}

public static void Stop()
{
HideOriginalController(true);
targetController.Disconnect();
viGEmClient.Dispose();
}

static void HideOriginalController(bool isEnabled = false)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity");

foreach (ManagementObject device in searcher.Get())
{
object caption = device["Caption"];
if (caption != null && caption.ToString().Contains("Xbox"))
{
object deviceId = device["DeviceID"];
Console.WriteLine($"{deviceId}");

if (isEnabled == true)
{
con.IsActive = false;
con.RemoveBlockedInstanceId(deviceId.ToString());
con.ClearBlockedInstancesList();
con.IsActive = false;
}
else if (!isEnabled)
{
con.IsActive = true;
con.AddApplicationPath(Assembly.GetEntryAssembly().Location.Replace(".dll", ".exe"));
con.AddApplicationPath(Assembly.GetEntryAssembly().Location);
con.AddBlockedInstanceId(deviceId.ToString());
con.IsActive = true;
}
}
}
}

public static async void EmulateController()
{
try
{
UpdateController(UserIndex.One);
UpdateController(UserIndex.Two);
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}

static void UpdateController(UserIndex index)
{

Controller physicalController = new Controller(index);
State state = physicalController.GetState();

targetController.SetButtonState(Xbox360Button.Start, (state.Gamepad.Buttons & GamepadButtonFlags.Start) != 0);
targetController.SetButtonState(Xbox360Button.Back, (state.Gamepad.Buttons & GamepadButtonFlags.Back) != 0);

if (Global._appVis != Visibility.Visible)
{
targetController.SetButtonState(Xbox360Button.A, (state.Gamepad.Buttons & GamepadButtonFlags.A) != 0);
targetController.SetButtonState(Xbox360Button.B, (state.Gamepad.Buttons & GamepadButtonFlags.B) != 0);
targetController.SetButtonState(Xbox360Button.X, (state.Gamepad.Buttons & GamepadButtonFlags.X) != 0);
targetController.SetButtonState(Xbox360Button.Y, (state.Gamepad.Buttons & GamepadButtonFlags.Y) != 0);
targetController.SetButtonState(Xbox360Button.LeftShoulder, (state.Gamepad.Buttons & GamepadButtonFlags.LeftShoulder) != 0);
targetController.SetButtonState(Xbox360Button.RightShoulder, (state.Gamepad.Buttons & GamepadButtonFlags.RightShoulder) != 0);
targetController.SetButtonState(Xbox360Button.LeftThumb, (state.Gamepad.Buttons & GamepadButtonFlags.LeftThumb) != 0);
targetController.SetButtonState(Xbox360Button.RightThumb, (state.Gamepad.Buttons & GamepadButtonFlags.RightThumb) != 0);

targetController.SetButtonState(Xbox360Button.Up, (state.Gamepad.Buttons & GamepadButtonFlags.DPadUp) != 0);
targetController.SetButtonState(Xbox360Button.Down, (state.Gamepad.Buttons & GamepadButtonFlags.DPadDown) != 0);
targetController.SetButtonState(Xbox360Button.Left, (state.Gamepad.Buttons & GamepadButtonFlags.DPadLeft) != 0);
targetController.SetButtonState(Xbox360Button.Right, (state.Gamepad.Buttons & GamepadButtonFlags.DPadRight) != 0);

targetController.SetAxisValue(0, state.Gamepad.LeftThumbX);
targetController.SetAxisValue(1, state.Gamepad.LeftThumbY);
targetController.SetAxisValue(2, state.Gamepad.RightThumbX);
targetController.SetAxisValue(3, state.Gamepad.RightThumbY);
targetController.SetSliderValue(0, state.Gamepad.LeftTrigger);
targetController.SetSliderValue(1, state.Gamepad.RightTrigger);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<FileVersion>0.0.9</FileVersion>
<AssemblyVersion>0.0.9</AssemblyVersion>
<Version>$(VersionPrefix)</Version>
<UseWindowsForms>True</UseWindowsForms>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -32,6 +33,8 @@
<PackageReference Include="LibreHardwareMonitorLib" Version="0.9.2" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
<PackageReference Include="NAudio" Version="2.1.0" />
<PackageReference Include="Nefarius.Drivers.HidHide" Version="1.12.2" />
<PackageReference Include="Nefarius.ViGEm.Client" Version="1.21.256" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SecretNest.SequentialScheduler" Version="1.1.0" />
<PackageReference Include="SharpDX.XInput" Version="4.2.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Input;
using System.Windows.Threading;
using Universal_x86_Tuning_Utility_Handheld.Properties;
using Universal_x86_Tuning_Utility_Handheld.Scripts;
using Universal_x86_Tuning_Utility_Handheld.Scripts.Fan_Control;
using Universal_x86_Tuning_Utility_Handheld.Scripts.Misc;
using Universal_x86_Tuning_Utility_Handheld.Views.Windows;
Expand Down Expand Up @@ -76,11 +77,16 @@ public void OnNavigatedFrom()
{
}

private void Functions(string parameter)
private async void Functions(string parameter)
{
switch (parameter)
{
case "close":
if (ControllerControl.isStarted)
{
ControllerControl.isStarted = false;
ControllerControl.Stop();
}
if (Fan_Control.isSupported) Fan_Control.disableFanControl();
Process.GetCurrentProcess().Kill();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;

Fan_Control.UpdateAddresses();

ControllerControl.isStarted = true;
ControllerControl.SetUp();
}

int i = 0;
Expand Down Expand Up @@ -535,6 +538,9 @@ protected override void OnClosed(EventArgs e)
base.OnClosed(e);

// Make sure that closing this window will begin the process of closing the application.
isControllerEmu = false;
ControllerControl.isStarted = false;
ControllerControl.Stop();
Application.Current.Shutdown();
Environment.Exit(0);
}
Expand Down Expand Up @@ -775,7 +781,7 @@ void WatcherEventArrived(object sender, EventArrivedEventArgs e)
break;
}
}

bool isControllerEmu = false;
async void Mouse_Tick(object sender, EventArgs e)
{
if (Settings.Default.isMouse == true)
Expand Down

0 comments on commit e566d93

Please sign in to comment.