Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ControllerManager.cs #872

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Comment on lines 332 to 336
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialization of connectedJoys to -1 and the subsequent loop to connect devices may result in an infinite loop if JslConnectDevices() throws an exception that is not caught. Ensure that exceptions are handled properly to avoid an infinite loop.

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);
Comment on lines 347 to 358
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [350-369]

The check if (connectedJoys > 0) ensures that the following logic is only executed if there are connected devices. However, there is no else block to handle the case when there are no connected devices. Consider adding logic to handle this case appropriately.

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
Loading