-
Notifications
You must be signed in to change notification settings - Fork 50
[+] Adx hot plug #108
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
[+] Adx hot plug #108
Conversation
|
You have run out of free Bugbot PR reviews for this billing cycle. This will reset on February 11. To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial. |
Summary of ChangesHello @clansty, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the input system by introducing hot-plugging support for ADX HID devices. This allows the application to dynamically detect and manage device connections and disconnections, improving resilience and user convenience. The changes involve a refactoring of the device handling logic, making it more robust against device state changes. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces hot-plug support for ADX HID controllers, which is a great feature. The implementation is mostly solid, with background threads handling device connection, disconnection, and input reading. I've identified a few areas for improvement regarding code clarity, robustness, and efficiency. My suggestions include replacing magic numbers with constants, improving exception handling to avoid swallowing errors silently, removing a redundant check, and reducing CPU usage in a polling loop.
| var device = p == 0 | ||
| ? HidDevices.Enumerate(0x2E3C, [0x5750, 0x5767]).FirstOrDefault(it => !it.DevicePath.EndsWith("kbd")) | ||
| : HidDevices.Enumerate(0x2E4C, 0x5750).Concat(HidDevices.Enumerate(0x2E3C, 0x5768)).FirstOrDefault(it => !it.DevicePath.EndsWith("kbd")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Vendor IDs (e.g., 0x2E3C) and Product IDs (e.g., 0x5750) are hardcoded as magic numbers. Using named constants for these values would significantly improve code readability and maintainability. I recommend defining constants for these IDs at the class level and using them here and in the NeedsButtonInput method.
| try | ||
| { | ||
| return device.IsConnected; | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching a generic Exception and silently returning false can hide unexpected errors and make debugging difficult. It's better to catch a more specific exception if the HidLibrary provides one. If not, at least log the exception to aid in troubleshooting.
try
{
return device.IsConnected;
}
catch (Exception e)
{
MelonLogger.Warning($"[HidInput] Error checking device {p + 1}P connection: {e.Message}");
return false;
}| try | ||
| { | ||
| device.CloseDevice(); | ||
| } | ||
| catch | ||
| { | ||
| // ignore | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to other try-catch blocks in this file, catching a generic Exception is not ideal. Even if the intention is to ignore the error (as the device is likely disconnected), logging the exception can be valuable for debugging unexpected behavior.
try
{
device.CloseDevice();
}
catch (Exception e)
{
// It's likely the device was already disconnected, but we log it just in case.
MelonLogger.Warning($"[HidInput] Error closing device {p + 1}P: {e.Message}");
}| try | ||
| { | ||
| return device.Attributes.ProductId is not (0x5767 or 0x5768); | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This try-catch block catches a generic Exception and silently returns false, which can mask underlying problems. It's better to log the exception for debugging purposes, similar to the suggestions for IsDeviceAvailable and DisconnectDevice.
try
{
return device.Attributes.ProductId is not (0x5767 or 0x5768);
}
catch (Exception e)
{
MelonLogger.Warning($"[HidInput] Error reading attributes for device {p + 1}P: {e.Message}");
return false;
}| if (device == null) return false; | ||
| try | ||
| { | ||
| return device.Attributes.ProductId is not (0x5767 or 0x5768); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (!NeedsButtonInput(p)) | ||
| { | ||
| var newState = report1P.Data[i]; | ||
| if (newState == 1 && inputBuf[p, i] == 0) | ||
| Thread.Sleep(500); | ||
| continue; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a connected device does not require button input, the thread enters a polling loop, sleeping for 500ms and then re-checking the device status. This is inefficient and consumes CPU resources unnecessarily. Consider increasing the sleep duration to reduce the polling frequency and lower the performance impact.
if (!NeedsButtonInput(p))
{
Thread.Sleep(2000); // Increase sleep time to reduce CPU usage
continue;
}
No description provided.