Skip to content

Commit

Permalink
Update ControllerManager.cs (#872)
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie authored Dec 11, 2023
1 parent aad9748 commit 2b48336
Showing 1 changed file with 37 additions and 42 deletions.
79 changes: 37 additions & 42 deletions HandheldCompanion/Managers/ControllerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static ControllerManager()
watchdogThread.IsBackground = true;
}

public static void Start()
public static Task Start()
{
// Flushing possible JoyShocks...
JslDisconnectAndDisposeAll();
Expand Down Expand Up @@ -94,6 +94,8 @@ public static void Start()
ControllerSelected?.Invoke(GetEmulatedController());

LogManager.LogInformation("{0} has started", "ControllerManager");

return Task.CompletedTask;
}

public static void Stop()
Expand Down Expand Up @@ -328,6 +330,8 @@ private static async void HidDeviceArrived(PnPDetails details, DeviceEventArgs o

// JoyShockLibrary
int connectedJoys = -1;
int joyShockId = -1;
JOY_SETTINGS settings = new();

DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(4));
while (DateTime.Now < timeout && connectedJoys == -1)
Expand All @@ -343,15 +347,12 @@ private static async void HidDeviceArrived(PnPDetails details, DeviceEventArgs o
}
}

if (connectedJoys >= 0)
if (connectedJoys > 0)
{
int[] joysHandle = new int[connectedJoys];
JslGetConnectedDeviceHandles(joysHandle, connectedJoys);

// scroll handles until we find matching device path
int joyShockId = -1;
JOY_SETTINGS settings = new();

foreach (int i in joysHandle)
{
settings = JslGetControllerInfoAndSettings(i);
Expand All @@ -365,51 +366,45 @@ private static async void HidDeviceArrived(PnPDetails details, DeviceEventArgs o
break;
}
}
}

// device found
if (joyShockId != -1)
{
// use handle
settings.playerNumber = joyShockId;
// device found
if (joyShockId != -1)
{
// use handle
settings.playerNumber = joyShockId;

JOY_TYPE joyShockType = (JOY_TYPE)JslGetControllerType(joyShockId);
JOY_TYPE joyShockType = (JOY_TYPE)JslGetControllerType(joyShockId);

if (controller is not null)
{
((JSController)controller).AttachDetails(details);
((JSController)controller).AttachJoySettings(settings);
if (controller is not null)
{
((JSController)controller).AttachDetails(details);
((JSController)controller).AttachJoySettings(settings);

// hide new InstanceID (HID)
if (controller.IsHidden())
controller.Hide(false);
// hide new InstanceID (HID)
if (controller.IsHidden())
controller.Hide(false);

IsPowerCycling = true;
}
else
{
// UI thread (sync)
Application.Current.Dispatcher.Invoke(() =>
{
switch (joyShockType)
{
case JOY_TYPE.DualSense:
controller = new DualSenseController(settings, details);
break;
case JOY_TYPE.DualShock4:
controller = new DS4Controller(settings, details);
break;
case JOY_TYPE.ProController:
controller = new ProController(settings, details);
break;
}
});
}
IsPowerCycling = true;
}
else
{
// unsupported controller
LogManager.LogError("Couldn't find matching JoyShock controller: VID:{0} and PID:{1}",
details.GetVendorID(), details.GetProductID());
// UI thread (sync)
Application.Current.Dispatcher.Invoke(() =>
{
switch (joyShockType)
{
case JOY_TYPE.DualSense:
controller = new DualSenseController(settings, details);
break;
case JOY_TYPE.DualShock4:
controller = new DS4Controller(settings, details);
break;
case JOY_TYPE.ProController:
controller = new ProController(settings, details);
break;
}
});
}
}
else
Expand Down

0 comments on commit 2b48336

Please sign in to comment.