MobiFlight / MobiFlight-Connector

MobiFlight is an open source project that allows you to create your own home cockpit for your favorite flight simulator in a flexible, affordable and extremely user-friendly way.
https://mobiflight.com
MIT License
232 stars 103 forks source link

Spacemouse compact left/right button click causes .net exception #883

Closed windingdk closed 2 years ago

windingdk commented 2 years ago

Describe the bug I use this device (https://3dconnexion.com/uk/product/spacemouse-compact/) to control views. When I click on left/right mouse button I get an exception (see attached txt file) bug_spacemouse_exception.txt

Issue is 100% reproducible.

I have installed VS and tried to debug code. In UpdateButtons method in joystick.cs the Buttons list is empty (DeviceId = Buttons[i].Label).

private void UpdateButtons(JoystickState newState)
        {
            for (int i = 0; i != newState.Buttons.Length; i++)
            {
                if (!StateExists() || state.Buttons.Length < i || state.Buttons[i] != newState.Buttons[i])
                {
                    if (newState.Buttons[i] || (state != null))

                            OnButtonPressed?.Invoke(this, new InputEventArgs()
                            {
                                DeviceId = Buttons[i].Label,
                                Serial = SerialPrefix + joystick.Information.InstanceGuid.ToString(),
                                Type = DeviceType.Button,
                                Value = newState.Buttons[i] ? 0 : 1
                            });
                }
            }
        }

I have checkedout main/ 582465a322be48d5505773c4cdaf768da50dcb08

I initialy found the bug in version 9.4.0 download here https://www.mobiflight.com/en/download.html

Further details: I have single stepped in EnumerateDevices to get more details

bug_mobiflight

Method never finds IsAxis, IsButton, IsPOV. It finds a single "DeviceObjectTypeFlags.RelativeAxis".

private void EnumerateDevices()
        {
            foreach (DeviceObjectInstance device in this.joystick.GetObjects())
            {

                this.joystick.GetObjectInfoById(device.ObjectId);

                int offset = device.Offset;
                int usage = device.Usage;
                ObjectAspect aspect = device.Aspect;
                String name = device.Name;

                bool IsAxis = (device.ObjectId.Flags & DeviceObjectTypeFlags.AbsoluteAxis) > 0;
                bool IsButton = (device.ObjectId.Flags & DeviceObjectTypeFlags.Button) > 0;
                bool IsPOV = (device.ObjectId.Flags & DeviceObjectTypeFlags.PointOfViewController) > 0;

Please let me know if I shall do some more debugging to solve this issue.

DocMoebiuz commented 2 years ago

Please try this branch and let me know if that fixes your problem. https://github.com/MobiFlight/MobiFlight-Connector/tree/883-spacemouse @windingdk

windingdk commented 2 years ago

Tested it and it works. LGTM :-) thanks. @DocMoebiuz

One note. With the solution that you have implemented I guess that the buttons on the spacemouse will not work in Mobiflight, right?

windingdk commented 2 years ago

@DocMoebiuz Shall I do more or will you create the pull request yourself?

DocMoebiuz commented 2 years ago

Do the space mouse buttons show? I now changed it so that it only checks for button presses if buttons were found when querying the capabilities.

windingdk commented 2 years ago

@DocMoebiuz not sure what you mean by show. Do you mean when the EnumerateDevices method runs? I think what you implemented is working. But I guess that a mouse click on the space mouse cannot be used with that implementation, right?

DocMoebiuz commented 2 years ago

go to game controller settings in windows and check whether the space mouse buttons show there. To me it looks like they are not interpreted as Buttons in DirectX

windingdk commented 2 years ago

@DocMoebiuz : I have this:

image
DocMoebiuz commented 2 years ago

then everything should also show up in MobiFlight. That's very strange.

DocMoebiuz commented 2 years ago

@windingdk since you have Visual Studio and Mobiflight Source Code, please do me a favor:

You said that isButton is not True, so what does the descriptor is telling us about the Buttons?

See https://github.com/MobiFlight/MobiFlight-Connector/blob/058ee7eafcb71638430c983717e14cc163f75713/MobiFlight/Joystick.cs#L90

Can you find anything?

Also it might be helpful to log the information available about the aspect and share it with me. For example the 4 attributes before https://github.com/MobiFlight/MobiFlight-Connector/blob/058ee7eafcb71638430c983717e14cc163f75713/MobiFlight/Joystick.cs#L87

windingdk commented 2 years ago

@DocMoebiuz It is bit strange.

Some OBS: When the program cycles through private void EnumerateDevices() it actually find axis and buttons. And the Buttons list also contains 4 buttons when it is done.

When I goto the game controller settings in Windows and pushes the buttons and/or control the axis nothing happens (buttons does not light up, no axis is moving. )

But when the exceptions occurs in the update method the Buttons list is empty! Does it make any sense to you.

I have made a screen dump of the buttons list when the exception occurs ( I disable the Buttons.Count statement that you inserted)

image

newState.Buttons[0] is true

image

One note: The space mouse if a configuable unit, depending on what program that runs. I am not sure if that can affect anything.

DocMoebiuz commented 2 years ago

That indeed is very strange. The only reason i can think of why the button list is empty and then not might be that two devices are detected. Could that be the case?

DocMoebiuz commented 2 years ago

@windingdk - i don't know how to proceed with this problem since I cannot reproduce it. If you can find out what the problem is let me know, but I will close this issue for now.

windingdk commented 2 years ago

@DocMoebiuz for now i think it is a ok solution from my point of view. I have not been able to get closer to the cause.

DocMoebiuz commented 2 years ago

@windingdk ok - I had added the fix into the official mobiflight beta already. You can try there and confirm that the exception is not raised anymore.

windingdk commented 2 years ago

@DocMoebiuz I have just tested version 9.5.0.3. The fix works as expected.