Giter Club home page Giter Club logo

Comments (17)

bjadamson avatar bjadamson commented on September 13, 2024 1

It's okay, you're raising valid questions here. I don't believe there is any way to "catch" a Gamepad before it's been physically disconnected. It will always be too late to do anything with a Gamepad after it's removed.

Okay, that's just a misunderstanding on my part. Thanks!

from sdl.

AntTheAlchemist avatar AntTheAlchemist commented on September 13, 2024

I'm using SDL_GetGamepadFromInstanceID on SDL_EVENT_GAMEPAD_REMOVED without a crash using the latest SDL3 source. Which version of SDL are you using?

I've found none of the Gamepad functions return anything useful during the SDL_EVENT_GAMEPAD_REMOVED event. SDL_IsGamepad even returns false (because it decides based on if the Gamepad has mapping, which will be false because the GUID is zeroed out at this point), which is very misleading - maybe this should be considered a bug at least. SDL_GetGamepadInstanceName will return null. SDL_GetGamepadFromInstanceID DOES work though, and returns a valid pointer.

Just to clarify: when you say crash, do you mean assertion fail?

If you've not done a SDL_OpenGamepad, then that might be why you're getting null from SDL_GetGamepadFromInstanceID?

So, yes, you're not opening a Gamepad. "Get the SDL_Gamepad associated with a joystick instance ID, if it has been opened." is what SDL_GetGamepadFromInstanceID says.

from sdl.

bjadamson avatar bjadamson commented on September 13, 2024

If you've not done a SDL_OpenGamepad, then that might be why you're getting null from SDL_GetGamepadFromInstanceID?

Sorry, that was a mistake when I reduced the program from my actual program to the example code above. I am indeed calling SDL_OpenGamepad(id) in the add() function. I've updated the original example program to include this now.

Just to clarify: when you say crash, do you mean assertion fail?

Yes.

I've found none of the Gamepad functions return anything useful during the SDL_EVENT_GAMEPAD_REMOVED event. SDL_IsGamepad even returns false (because it decides based on if the Gamepad has mapping, which will be false because the GUID is zeroed out at this point), which is very misleading - maybe this should be considered a bug at least.

I'm not so familiar yet with how it's supposed to work, but yeah -- it does seem weird to me.

I'm using SDL_GetGamepadFromInstanceID on SDL_EVENT_GAMEPAD_REMOVED without a crash using the latest SDL3 source. Which version of SDL are you using?

I downloaded the latest source today from this repo, just to make sure it wasn't already fixed. Are you also running it on windows?

image

from sdl.

bjadamson avatar bjadamson commented on September 13, 2024

I took a video running the program -- I don't know if it provides more information than the code does, but it's here if it will help.

from sdl.

AntTheAlchemist avatar AntTheAlchemist commented on September 13, 2024

Yes, I'm on Windows 10.

SDL_IsGamepad on an open Gamepad does return false, after SDL_EVENT_GAMEPAD_REMOVED, which I think is kind of a bug, or misleading at worst. A Gamepad loses mapping when it's removed, so it becomes invalid for anything useful.

Moving passed "CRASH #1", can you now double-check if SDL_GetGamepadFromInstanceID is returning null or not?

from sdl.

bjadamson avatar bjadamson commented on September 13, 2024

Moving passed "CRASH #1", can you now double-check if SDL_GetGamepadFromInstanceID is returning null or not?

Tested, and it does not return null, BUT SDL_GamepadConnected is returning false.
image

That's happening because it's being set to FALSE in SDL_PrivateJoystickRemoved

image

This happens directly from the windows event removing the controller:
image

from sdl.

AntTheAlchemist avatar AntTheAlchemist commented on September 13, 2024

I would expect SDL_GamepadConnected to return false if it's been unplugged?

from sdl.

bjadamson avatar bjadamson commented on September 13, 2024

SDL_IsGamepad on an open Gamepad does return false, after SDL_EVENT_GAMEPAD_REMOVED, which I think is kind of a bug, or misleading at worst. A Gamepad loses mapping when it's removed, so it becomes invalid for anything useful.

I just wanted to respond to this, I feel like it's invalid in the sense that the way I understand it works is that I'm supposed to receive the event before the system does anything to my controller.

from sdl.

AntTheAlchemist avatar AntTheAlchemist commented on September 13, 2024

It's okay, you're raising valid questions here. I don't believe there is any way to "catch" a Gamepad before it's been physically disconnected. It will always be too late to do anything with a Gamepad after it's removed.

from sdl.

AntTheAlchemist avatar AntTheAlchemist commented on September 13, 2024

Okay, that's just a misunderstanding on my part. Thanks!

I'm glad we were able to figure it out. Do you think there is a way to clarify this for other users in the documents somewhere?

from sdl.

bjadamson avatar bjadamson commented on September 13, 2024

Okay, that's just a misunderstanding on my part. Thanks!

I'm glad we were able to figure it out. Do you think there is a way to clarify this for other users in the documents somewhere?

Yeah, I do think just adding some documentation to the event itself stating that once the event has been received by the application it has already been "disconnected." Specifically calling on SDL_GamepadConnected() on the id is either expected to return false, or undefined behavior (idk, maybe another controller gets plugged at the exact right time).

from sdl.

AntTheAlchemist avatar AntTheAlchemist commented on September 13, 2024

@slouken The only issue here is a misunderstanding of what's possible after a SDL_EVENT_GAMEPAD_REMOVED, and that SDL_IsGamepad always returns false after a removed Gamepad. Is this considered a bug? Shall we keep this issue open?

from sdl.

AntTheAlchemist avatar AntTheAlchemist commented on September 13, 2024

Specifically calling on SDL_GamepadConnected() on the id is either expected to return false, or undefined behavior (idk, maybe another controller gets plugged at the exact right time).

I believe the instance ID remains valid until you close the Gamepad. And I believe if the gamepad was never opened, though, then the ID will be invalid after you return from handling the removed event.

from sdl.

bjadamson avatar bjadamson commented on September 13, 2024

Specifically calling on SDL_GamepadConnected() on the id is either expected to return false, or undefined behavior (idk, maybe another controller gets plugged at the exact right time).

I believe the instance ID remains valid until you close the Gamepad. And I believe if the gamepad was never opened, though, then the ID will be invalid after you return from handling the removed event.

That's super clear. Thanks.

I would like to add, it does seem very unclear that I cannot query if a joystick ID was a gamepad in the SDL_EVENT_GAMEPAD_REMOVED event -- I mean it's right there in the name of the event.

However it does seem a redundant to have the check in my code after discussing it with you.

from sdl.

slouken avatar slouken commented on September 13, 2024

I have a fix for SDL_IsGamepad() so it will return true even after the controller was disconnected.

With this change, if you also make the following change to your code, it will work as expected:

assert(SDL_FALSE == ::SDL_GamepadConnected(data));

from sdl.

bjadamson avatar bjadamson commented on September 13, 2024

Wow you work quick! Thanks. 👍

from sdl.

slouken avatar slouken commented on September 13, 2024

You're welcome! :)

from sdl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.