-
-
Notifications
You must be signed in to change notification settings - Fork 1k
[Native] - Implement IsXInputDevice method in C# #1926
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
Conversation
using SharpDX; | ||
using SharpDX.DirectInput; | ||
using Stride.Native.DirectInput; | ||
using Microsoft.Win32; |
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.
would that make a problem with linux migration?
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.
Note that this file compiles only when STRIDE_UI_WINFORMS or STRIDE_UI_WPF and are set only when StrideFramework is Windows
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.
so propably no
While it's amazing to see so much C++ code replaced by so little C#, I'm worried we might not be able to accept code copied from a GPL licensed project into an MIT licensed project, unless with the explicit permission of the copyright holder to re-license the code. |
@manio143 Oh my. Good point, I forgot about the fact that GNU is not a completely "free" license. I'll try to investigate my own implementation then |
You could also ask the author if he allows us to use that part of the code. The author always has full control over his work, no matter the license the code is published with. |
The problem is that the GPL code doesnt work with sharpdx Sharpdx takes the HID path from a device and somehow builds an product ID ( GUID ) from it
to convert a sharpdx guid to a hid i only found that one option would be to accurately query with wql to get all xdevices and comparing the device path instead of the guid the gpl code is just doing alot of fancy things but in the end it just converts this example hid path this shouldnt sound negative about the gpl code, i just couldnt find an alignment between sharpdx and the guid part of the code if you need testing i can use my playstation controllers as xinput and help with testing |
I haven't checked if this code works or not, but chatgpt says you can do this, using SharpDX.XInput;
// Create a controller object for the first player
var controller = new Controller(UserIndex.One);
// Check if the controller is connected
if (controller.IsConnected)
{
// Get the controller capabilities
var capabilities = controller.GetCapabilities(DeviceQueryType.Any);
// Check if the controller is an XInput device
if (capabilities.Type == DeviceType.Gamepad)
{
// Do something with the controller
Console.WriteLine("XInput device detected");
}
else
{
// The controller is not an XInput device
Console.WriteLine("Non-XInput device detected");
}
}
else
{
// No controller is connected
Console.WriteLine("No controller detected");
} And using SharpDX.DirectInput;
// Create a DirectInput object
var directInput = new DirectInput();
// Create a device instance from the product GUID
var deviceInstance = directInput.GetDevice(productGuid);
// Check if the device is an XInput device
if (deviceInstance.Type == DeviceType.Gamepad)
{
// The device is an XInput device
Console.WriteLine("XInput device detected");
}
else
{
// The device is not an XInput device
Console.WriteLine("Non-XInput device detected");
} |
anything can be a xinput device, even a mouse, it depends how windows tries to use it |
dcaa8a5
to
08269c3
Compare
|
||
var mySession = CimSession.Create(null, DComOptions); | ||
|
||
IEnumerable<CimInstance> allDevices = mySession.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_PNPEntity"); |
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.
I imagine this query is not super cheap and results won't be different if called more than once a frame. Consider calling the query and transforming the IEnumerable with regex once, caching the result in the Scan()
method, so that IsXInputDevice
can be a simpler lookup.
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.
@Jklawreszuk - do you think your WQL optimization is enough here? The Scan()
method goes in a loop over the devices and checks for each one if it's XInput - are we confident to say there usually wouldn't be many devices returned by DirectInput and thus this isn't a big perf issue? Otherwise we make a Windows call a few times in a tight loop and I imagine those are usually slower. However, at the same time Scan is called on initialization and then only upon manual request it seems.
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.
@manio143 Yea, Very good suggestion 👍. I guess, It is unlikely that something will change under the scan() method call. I updated changes, Is this what you had in mind?
Sorry, I might have misunderstood the purpose of the function being replaced. Btw, cheers for the thumbs down @Jklawreszuk... |
Sorry @archanox, I didn't have time to reply. I want to thank you for your help, however, both examples do not compile , because API doesn't even exist. So, that's why I gave you thumbs down, nothing personal.. 😅 |
sources/engine/Stride.Input/Windows/InputSourceWindowsDirectInput.cs
Outdated
Show resolved
Hide resolved
c774cb4
to
62abf51
Compare
@@ -18,11 +20,15 @@ internal class InputSourceWindowsDirectInput : InputSourceBase | |||
private readonly HashSet<Guid> devicesToRemove = new HashSet<Guid>(); | |||
private InputManager inputManager; | |||
private DirectInput directInput; | |||
private IEnumerable<CimInstance> allDevices; | |||
private Regex regex; |
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.
Consider renaming it to xInputDeviceIdRegex
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.
done
DComSessionOptions DComOptions = new DComSessionOptions(); | ||
DComOptions.Impersonation = ImpersonationType.Impersonate; | ||
|
||
var mySession = CimSession.Create(null, DComOptions); |
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.
I believe all above and including this line has to be moved to GetAllXInputDevices()
f071bb4
to
37f8135
Compare
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 wise it's good. If you can confirm it got tested I'll merge it.
i can test it with a playstation controller and emulate that it should be treated as xinput, dunnno if thats sufficient the query works so far |
There seems to be an issue with the build about no input files to clang - if there's no more cop files, we may need to turn off the property which triggers native lib compilation https://teamcity.stride3d.net/buildConfiguration/Engine_BuildWindowsOpenGL/23137?hideProblemsFromDependencies=false&hideTestsFromDependencies=false |
@manio143 I've managed to fix building my PR by simply checking if the input (c files collection) is empty. |
i tried your query and it works, but i cant tell if my xinput is even valid, as the playstation controller is emulated and my mouse too .. .i get with your query ( your test script ) these two devices... i think a real xinput device is needed which i dont have , only the emulated one so dont count on my test the emulator converts falsely everything to xinput for reasons..so i cant test if a device is not an xinput |
Build looks good. I'll see if this works with my Xbox controller. |
Ok, so this doesn't fully work for me.
should be
@IXLLEGACYIXL you mentioned this was working for you, can you provide what was the device Id in your case? Also, there were duplicates in the deviceId list from Cim query - maybe we can run a |
Alright, good to hear 👍. Anyway, yes, let's filter duplicates. I hope that string concatenation is deterministic (i.e. PID + VID). Otherwise method needs to exctract PID and VID separately(??) |
ive only tested the query itself, not the entire thing the guid is built but hidden and internal thats what ive written earlier |
The way your regex is written guarantees determinism here. |
so no for me its not working and i cant try as i lose my mouse with the emulator |
PR Details
Description
My PR implements IsXInputDevice method in C#
Related Issue
#1394
Todo