Skip to content

Commit

Permalink
Improve controller manager
Browse files Browse the repository at this point in the history
- fix an issue with NeptuneController
- fix an issue with IsBusy check
- fix an issue where VirtualManager vClient is null when profile HIDmode is applied (this should not happen!)
  • Loading branch information
Valkirie committed Dec 11, 2023
1 parent 04e6129 commit 01e9337
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
18 changes: 3 additions & 15 deletions HandheldCompanion/Controllers/IController.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public bool IsBusy
{
get
{
return IsEnabled;
return !IsEnabled;
}
set
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
// UI thread
Application.Current.Dispatcher.Invoke(() =>
{
IsEnabled = !value;
ProgressBarPanel.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
Expand Down Expand Up @@ -444,17 +444,14 @@ protected virtual void Calibrate()

protected virtual void ui_button_calibrate_Click(object sender, RoutedEventArgs e)
{
CalibrateClicked?.Invoke(this);
}

protected virtual void ui_button_hide_Click(object sender, RoutedEventArgs e)
{
HideClicked?.Invoke(this);
}

protected virtual void ui_button_hook_Click(object sender, RoutedEventArgs e)
{
HookClicked?.Invoke(this);
}

public virtual string GetGlyph(ButtonFlags button)
Expand Down Expand Up @@ -636,15 +633,6 @@ public string GetAxisName(AxisLayoutFlags axis)
public event InputsUpdatedEventHandler InputsUpdated;
public delegate void InputsUpdatedEventHandler(ControllerState Inputs);

public event HookClickedEventHandler HookClicked;
public delegate void HookClickedEventHandler(IController controller);

public event HideClickedEventHandler HideClicked;
public delegate void HideClickedEventHandler(IController controller);

public event CalibrateClickedEventHandler CalibrateClicked;
public delegate void CalibrateClickedEventHandler(IController controller);

#endregion
}
}
20 changes: 12 additions & 8 deletions HandheldCompanion/Controllers/NeptuneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ private void Open()
{
Controller.Open();
isConnected = true;

// disable lizard state
Controller.RequestLizardMode(false);

// create handler
Controller.OnControllerInputReceived += input => Task.Run(() => OnControllerInputReceived(input));
}
catch { }
}
Expand All @@ -260,6 +266,12 @@ private void Close()
{
try
{
// disable lizard state
Controller.RequestLizardMode(true);

// remove handler
Controller.OnControllerInputReceived = null;

Controller.Close();
isConnected = false;
}
Expand Down Expand Up @@ -291,8 +303,6 @@ public override void Plug()
{
try
{
Controller.OnControllerInputReceived = input => Task.Run(() => OnControllerInputReceived(input));

// open controller
Open();
}
Expand All @@ -302,9 +312,6 @@ public override void Plug()
return;
}

// disable lizard state
Controller.RequestLizardMode(false);

// manage rumble thread
rumbleThreadRunning = true;
rumbleThread = new Thread(RumbleThreadLoop);
Expand All @@ -322,9 +329,6 @@ public override void Unplug()
{
try
{
// restore lizard state
Controller.RequestLizardMode(true);

// kill rumble thread
rumbleThreadRunning = false;
rumbleThread.Join();
Expand Down
4 changes: 4 additions & 0 deletions HandheldCompanion/Managers/ControllerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ private static async void HidDeviceArrived(PnPDetails details, DeviceEventArgs o
// hide new InstanceID (HID)
if (controller.IsHidden())
controller.Hide(false);
else
controller.Unhide(false);

IsPowerCycling = true;
}
Expand Down Expand Up @@ -745,6 +747,8 @@ private static async void XUsbDeviceArrived(PnPDetails details, DeviceEventArgs
// hide new InstanceID (HID)
if (controller.IsHidden())
controller.Hide(false);
else
controller.Unhide(false);

IsPowerCycling = true;
}
Expand Down
6 changes: 6 additions & 0 deletions HandheldCompanion/Targets/DualShock4Target.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HandheldCompanion.Inputs;
using HandheldCompanion.Managers;
using HandheldCompanion.Utils;
using Nefarius.ViGEm.Client;
using Nefarius.ViGEm.Client.Exceptions;
using Nefarius.ViGEm.Client.Targets;
using Nefarius.ViGEm.Client.Targets.DualShock4;
Expand Down Expand Up @@ -39,6 +40,11 @@ public DualShock4Target() : base()
// initialize controller
HID = HIDmode.DualShock4Controller;

// create new ViGEm client
// this shouldn't happen, caused by profile HIDmode logic, fixme!
if (VirtualManager.vClient is null)
VirtualManager.vClient = new ViGEmClient();

virtualController = VirtualManager.vClient.CreateDualShock4Controller(0x054C, 0x09CC);
virtualController.AutoSubmitReport = false;
virtualController.FeedbackReceived += FeedbackReceived;
Expand Down
6 changes: 6 additions & 0 deletions HandheldCompanion/Targets/Xbox360Target.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using HandheldCompanion.Inputs;
using HandheldCompanion.Managers;
using HandheldCompanion.Utils;
using Nefarius.ViGEm.Client;
using Nefarius.ViGEm.Client.Exceptions;
using Nefarius.ViGEm.Client.Targets;
using Nefarius.ViGEm.Client.Targets.Xbox360;
Expand All @@ -17,6 +18,11 @@ public Xbox360Target(ushort vendorId, ushort productId) : base()
// initialize controller
HID = HIDmode.Xbox360Controller;

// create new ViGEm client
// this shouldn't happen, caused by profile HIDmode logic, fixme!
if (VirtualManager.vClient is null)
VirtualManager.vClient = new ViGEmClient();

virtualController = VirtualManager.vClient.CreateXbox360Controller(vendorId, productId);
virtualController.AutoSubmitReport = false;
virtualController.FeedbackReceived += FeedbackReceived;
Expand Down
8 changes: 6 additions & 2 deletions HandheldCompanion/Views/Pages/ControllerPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ private void VirtualManager_ControllerSelected(HIDmode mode)

private void ControllerHookClicked(IController Controller)
{
// todo: move me
if (Controller.IsBusy)
return;

var path = Controller.GetContainerInstancePath();
ControllerManager.SetTargetController(path, false);

Expand All @@ -321,7 +323,9 @@ private void ControllerHookClicked(IController Controller)

private void ControllerHideClicked(IController Controller)
{
// todo: move me
if (Controller.IsBusy)
return;

if (Controller.IsHidden())
Controller.Unhide();
else
Expand Down

0 comments on commit 01e9337

Please sign in to comment.